blob: 27d7b9c93fbcad587df5ca556f955c8b824844e9 [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
3774 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
3775 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.
3788 mDispatcher->onWindowInfosChanged({}, {});
3789
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);
3804 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
3805
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));
3851 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
3852 }
3853
3854 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
3855 mWindowInfos.push_back(*windowHandle->getInfo());
3856 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
3857 }
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
4955 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo});
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);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005287 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5288 ASSERT_EQ(OK, mDispatcher->start());
5289
5290 setUpWindow();
5291 }
5292
5293 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005294 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005295 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005296
Vishnu Nair47074b82020-08-14 11:54:47 -07005297 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005298 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005299 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005300 mWindow->consumeFocusEvent(true);
5301 }
5302
Chris Ye2ad95392020-09-01 13:44:44 -07005303 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005304 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005305 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005306 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005307 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005308
5309 // Window should receive key down event.
5310 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5311 }
5312
5313 void expectKeyRepeatOnce(int32_t repeatCount) {
5314 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5315 InputEvent* repeatEvent = mWindow->consume();
5316 ASSERT_NE(nullptr, repeatEvent);
5317
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005318 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005319
5320 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5321 uint32_t eventAction = repeatKeyEvent->getAction();
5322 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5323 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5324 }
5325
Chris Ye2ad95392020-09-01 13:44:44 -07005326 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005327 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005328 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005329 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005330 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005331
5332 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005333 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005334 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005335 }
5336};
5337
5338TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005339 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005340 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5341 expectKeyRepeatOnce(repeatCount);
5342 }
5343}
5344
5345TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005346 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005347 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5348 expectKeyRepeatOnce(repeatCount);
5349 }
Harry Cutts33476232023-01-30 19:57:29 +00005350 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005351 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005352 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5353 expectKeyRepeatOnce(repeatCount);
5354 }
5355}
5356
5357TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005358 sendAndConsumeKeyDown(/*deviceId=*/1);
5359 expectKeyRepeatOnce(/*repeatCount=*/1);
5360 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005361 mWindow->assertNoEvents();
5362}
5363
5364TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005365 sendAndConsumeKeyDown(/*deviceId=*/1);
5366 expectKeyRepeatOnce(/*repeatCount=*/1);
5367 sendAndConsumeKeyDown(/*deviceId=*/2);
5368 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005369 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005370 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005371 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005372 expectKeyRepeatOnce(/*repeatCount=*/2);
5373 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005374 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005375 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005376 mWindow->assertNoEvents();
5377}
5378
5379TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005380 sendAndConsumeKeyDown(/*deviceId=*/1);
5381 expectKeyRepeatOnce(/*repeatCount=*/1);
5382 sendAndConsumeKeyDown(/*deviceId=*/2);
5383 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005384 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005385 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005386 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005387 mWindow->assertNoEvents();
5388}
5389
liushenxiang42232912021-05-21 20:24:09 +08005390TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5391 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005392 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005393 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005394 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5395 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5396 mWindow->assertNoEvents();
5397}
5398
Garfield Tan1c7bc862020-01-28 13:24:04 -08005399TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005400 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005401 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005402 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5403 InputEvent* repeatEvent = mWindow->consume();
5404 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5405 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5406 IdGenerator::getSource(repeatEvent->getId()));
5407 }
5408}
5409
5410TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005411 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005412 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005413
5414 std::unordered_set<int32_t> idSet;
5415 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5416 InputEvent* repeatEvent = mWindow->consume();
5417 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5418 int32_t id = repeatEvent->getId();
5419 EXPECT_EQ(idSet.end(), idSet.find(id));
5420 idSet.insert(id);
5421 }
5422}
5423
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005424/* Test InputDispatcher for MultiDisplay */
5425class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5426public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005427 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005428 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005429
Chris Yea209fde2020-07-22 13:54:51 -07005430 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005431 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005432 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005433
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005434 // Set focus window for primary display, but focused display would be second one.
5435 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005436 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005437 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005438 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005439 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005440
Chris Yea209fde2020-07-22 13:54:51 -07005441 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005442 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005443 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005444 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005445 // Set focus display to second one.
5446 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5447 // Set focus window for second display.
5448 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005449 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005450 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005451 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005452 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005453 }
5454
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005455 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005456 InputDispatcherTest::TearDown();
5457
Chris Yea209fde2020-07-22 13:54:51 -07005458 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005459 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005460 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005461 windowInSecondary.clear();
5462 }
5463
5464protected:
Chris Yea209fde2020-07-22 13:54:51 -07005465 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005466 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005467 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005468 sp<FakeWindowHandle> windowInSecondary;
5469};
5470
5471TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5472 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005473 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5474 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5475 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005476 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005477 windowInSecondary->assertNoEvents();
5478
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005479 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005480 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5481 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5482 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005483 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005484 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005485}
5486
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005487TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005488 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005489 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5490 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005491 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005492 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005493 windowInSecondary->assertNoEvents();
5494
5495 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005496 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005497 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005498 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005499 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005500
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005501 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005502 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005503
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005504 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005505 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005506 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005507
5508 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005509 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005510 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005511 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005512 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005513 windowInSecondary->assertNoEvents();
5514}
5515
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005516// Test per-display input monitors for motion event.
5517TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005518 FakeMonitorReceiver monitorInPrimary =
5519 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5520 FakeMonitorReceiver monitorInSecondary =
5521 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005522
5523 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005524 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5525 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5526 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005527 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005528 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005529 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005530 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005531
5532 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005533 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5534 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5535 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005536 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005537 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005538 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005539 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005540
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005541 // Lift up the touch from the second display
5542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5543 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5544 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5545 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5546 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5547
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005548 // Test inject a non-pointer motion event.
5549 // If specific a display, it will dispatch to the focused window of particular display,
5550 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005551 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5552 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5553 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005554 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005555 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005556 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005557 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005558}
5559
5560// Test per-display input monitors for key event.
5561TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005562 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005563 FakeMonitorReceiver monitorInPrimary =
5564 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5565 FakeMonitorReceiver monitorInSecondary =
5566 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005567
5568 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005569 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5570 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005571 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005572 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005573 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005574 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005575}
5576
Vishnu Nair958da932020-08-21 17:12:37 -07005577TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
5578 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005579 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005580 secondWindowInPrimary->setFocusable(true);
5581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
5582 setFocusedWindow(secondWindowInPrimary);
5583 windowInPrimary->consumeFocusEvent(false);
5584 secondWindowInPrimary->consumeFocusEvent(true);
5585
5586 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005587 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
5588 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005589 windowInPrimary->assertNoEvents();
5590 windowInSecondary->assertNoEvents();
5591 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5592}
5593
Arthur Hungdfd528e2021-12-08 13:23:04 +00005594TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
5595 FakeMonitorReceiver monitorInPrimary =
5596 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5597 FakeMonitorReceiver monitorInSecondary =
5598 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
5599
5600 // Test touch down on primary display.
5601 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5602 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5603 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5604 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5605 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5606
5607 // Test touch down on second display.
5608 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5609 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5610 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5611 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
5612 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
5613
5614 // Trigger cancel touch.
5615 mDispatcher->cancelCurrentTouch();
5616 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5617 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5618 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
5619 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
5620
5621 // Test inject a move motion event, no window/monitor should receive the event.
5622 ASSERT_EQ(InputEventInjectionResult::FAILED,
5623 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5624 ADISPLAY_ID_DEFAULT, {110, 200}))
5625 << "Inject motion event should return InputEventInjectionResult::FAILED";
5626 windowInPrimary->assertNoEvents();
5627 monitorInPrimary.assertNoEvents();
5628
5629 ASSERT_EQ(InputEventInjectionResult::FAILED,
5630 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5631 SECOND_DISPLAY_ID, {110, 200}))
5632 << "Inject motion event should return InputEventInjectionResult::FAILED";
5633 windowInSecondary->assertNoEvents();
5634 monitorInSecondary.assertNoEvents();
5635}
5636
Jackal Guof9696682018-10-05 12:23:23 +08005637class InputFilterTest : public InputDispatcherTest {
5638protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005639 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
5640 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08005641 NotifyMotionArgs motionArgs;
5642
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005643 motionArgs =
5644 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005645 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005646 motionArgs =
5647 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005648 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005649 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005650 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005651 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
5652 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08005653 } else {
5654 mFakePolicy->assertFilterInputEventWasNotCalled();
5655 }
5656 }
5657
5658 void testNotifyKey(bool expectToBeFiltered) {
5659 NotifyKeyArgs keyArgs;
5660
5661 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005662 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005663 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005664 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005665 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005666
5667 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08005668 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005669 } else {
5670 mFakePolicy->assertFilterInputEventWasNotCalled();
5671 }
5672 }
5673};
5674
5675// Test InputFilter for MotionEvent
5676TEST_F(InputFilterTest, MotionEvent_InputFilter) {
5677 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
5678 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5679 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5680
5681 // Enable InputFilter
5682 mDispatcher->setInputFilterEnabled(true);
5683 // Test touch on both primary and second display, and check if both events are filtered.
5684 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
5685 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
5686
5687 // Disable InputFilter
5688 mDispatcher->setInputFilterEnabled(false);
5689 // Test touch on both primary and second display, and check if both events aren't filtered.
5690 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5691 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5692}
5693
5694// Test InputFilter for KeyEvent
5695TEST_F(InputFilterTest, KeyEvent_InputFilter) {
5696 // Since the InputFilter is disabled by default, check if key event aren't filtered.
5697 testNotifyKey(/*expectToBeFiltered*/ false);
5698
5699 // Enable InputFilter
5700 mDispatcher->setInputFilterEnabled(true);
5701 // Send a key event, and check if it is filtered.
5702 testNotifyKey(/*expectToBeFiltered*/ true);
5703
5704 // Disable InputFilter
5705 mDispatcher->setInputFilterEnabled(false);
5706 // Send a key event, and check if it isn't filtered.
5707 testNotifyKey(/*expectToBeFiltered*/ false);
5708}
5709
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005710// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
5711// logical display coordinate space.
5712TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
5713 ui::Transform firstDisplayTransform;
5714 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5715 ui::Transform secondDisplayTransform;
5716 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
5717
5718 std::vector<gui::DisplayInfo> displayInfos(2);
5719 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
5720 displayInfos[0].transform = firstDisplayTransform;
5721 displayInfos[1].displayId = SECOND_DISPLAY_ID;
5722 displayInfos[1].transform = secondDisplayTransform;
5723
5724 mDispatcher->onWindowInfosChanged({}, displayInfos);
5725
5726 // Enable InputFilter
5727 mDispatcher->setInputFilterEnabled(true);
5728
5729 // Ensure the correct transforms are used for the displays.
5730 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
5731 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
5732}
5733
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005734class InputFilterInjectionPolicyTest : public InputDispatcherTest {
5735protected:
5736 virtual void SetUp() override {
5737 InputDispatcherTest::SetUp();
5738
5739 /**
5740 * We don't need to enable input filter to test the injected event policy, but we enabled it
5741 * here to make the tests more realistic, since this policy only matters when inputfilter is
5742 * on.
5743 */
5744 mDispatcher->setInputFilterEnabled(true);
5745
5746 std::shared_ptr<InputApplicationHandle> application =
5747 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005748 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
5749 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005750
5751 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5752 mWindow->setFocusable(true);
5753 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5754 setFocusedWindow(mWindow);
5755 mWindow->consumeFocusEvent(true);
5756 }
5757
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005758 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5759 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005760 KeyEvent event;
5761
5762 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5763 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
5764 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00005765 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005766 const int32_t additionalPolicyFlags =
5767 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
5768 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005769 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005770 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5771 policyFlags | additionalPolicyFlags));
5772
5773 InputEvent* received = mWindow->consume();
5774 ASSERT_NE(nullptr, received);
5775 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005776 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005777 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
5778 ASSERT_EQ(flags, keyEvent.getFlags());
5779 }
5780
5781 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5782 int32_t flags) {
5783 MotionEvent event;
5784 PointerProperties pointerProperties[1];
5785 PointerCoords pointerCoords[1];
5786 pointerProperties[0].clear();
5787 pointerProperties[0].id = 0;
5788 pointerCoords[0].clear();
5789 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
5790 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
5791
5792 ui::Transform identityTransform;
5793 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5794 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
5795 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
5796 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
5797 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07005798 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07005799 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005800 /*pointerCount*/ 1, pointerProperties, pointerCoords);
5801
5802 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
5803 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005804 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005805 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5806 policyFlags | additionalPolicyFlags));
5807
5808 InputEvent* received = mWindow->consume();
5809 ASSERT_NE(nullptr, received);
5810 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005811 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005812 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
5813 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005814 }
5815
5816private:
5817 sp<FakeWindowHandle> mWindow;
5818};
5819
5820TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005821 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
5822 // filter. Without it, the event will no different from a regularly injected event, and the
5823 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00005824 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
5825 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005826}
5827
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005828TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005829 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005830 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005831 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
5832}
5833
5834TEST_F(InputFilterInjectionPolicyTest,
5835 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
5836 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005837 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005838 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005839}
5840
5841TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00005842 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
5843 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005844}
5845
chaviwfd6d3512019-03-25 13:23:49 -07005846class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005847 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07005848 InputDispatcherTest::SetUp();
5849
Chris Yea209fde2020-07-22 13:54:51 -07005850 std::shared_ptr<FakeApplicationHandle> application =
5851 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005852 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005853 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005854 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07005855
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005856 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005857 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005858 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07005859
5860 // Set focused application.
5861 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005862 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07005863
5864 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005865 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005866 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005867 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07005868 }
5869
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005870 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07005871 InputDispatcherTest::TearDown();
5872
5873 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005874 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07005875 }
5876
5877protected:
5878 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005879 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005880 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07005881};
5882
5883// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5884// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
5885// the onPointerDownOutsideFocus callback.
5886TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005887 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005888 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5889 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005890 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005891 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005892
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005893 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07005894 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
5895}
5896
5897// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
5898// DOWN on the window that doesn't have focus. Ensure no window received the
5899// onPointerDownOutsideFocus callback.
5900TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005901 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005902 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005903 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005904 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005905
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005906 ASSERT_TRUE(mDispatcher->waitForIdle());
5907 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005908}
5909
5910// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
5911// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
5912TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005913 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5914 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005915 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005916 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005917
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005918 ASSERT_TRUE(mDispatcher->waitForIdle());
5919 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005920}
5921
5922// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5923// DOWN on the window that already has focus. Ensure no window received the
5924// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005925TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005926 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005927 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005928 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005929 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005930 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005931
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005932 ASSERT_TRUE(mDispatcher->waitForIdle());
5933 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005934}
5935
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005936// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
5937// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
5938TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
5939 const MotionEvent event =
5940 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
5941 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005942 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005943 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
5944 .build();
5945 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
5946 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5947 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
5948
5949 ASSERT_TRUE(mDispatcher->waitForIdle());
5950 mFakePolicy->assertOnPointerDownWasNotCalled();
5951 // Ensure that the unfocused window did not receive any FOCUS events.
5952 mUnfocusedWindow->assertNoEvents();
5953}
5954
chaviwaf87b3e2019-10-01 16:59:28 -07005955// These tests ensures we can send touch events to a single client when there are multiple input
5956// windows that point to the same client token.
5957class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
5958 virtual void SetUp() override {
5959 InputDispatcherTest::SetUp();
5960
Chris Yea209fde2020-07-22 13:54:51 -07005961 std::shared_ptr<FakeApplicationHandle> application =
5962 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005963 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
5964 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07005965 mWindow1->setFrame(Rect(0, 0, 100, 100));
5966
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005967 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
5968 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07005969 mWindow2->setFrame(Rect(100, 100, 200, 200));
5970
Arthur Hung72d8dc32020-03-28 00:48:39 +00005971 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07005972 }
5973
5974protected:
5975 sp<FakeWindowHandle> mWindow1;
5976 sp<FakeWindowHandle> mWindow2;
5977
5978 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05005979 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07005980 vec2 vals = windowInfo->transform.transform(point.x, point.y);
5981 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07005982 }
5983
5984 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
5985 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005986 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07005987 InputEvent* event = window->consume();
5988
5989 ASSERT_NE(nullptr, event) << name.c_str()
5990 << ": consumer should have returned non-NULL event.";
5991
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005992 ASSERT_EQ(InputEventType::MOTION, event->getType())
5993 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07005994
5995 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005996 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08005997 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07005998
5999 for (size_t i = 0; i < points.size(); i++) {
6000 float expectedX = points[i].x;
6001 float expectedY = points[i].y;
6002
6003 EXPECT_EQ(expectedX, motionEvent.getX(i))
6004 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6005 << ", got " << motionEvent.getX(i);
6006 EXPECT_EQ(expectedY, motionEvent.getY(i))
6007 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6008 << ", got " << motionEvent.getY(i);
6009 }
6010 }
chaviw9eaa22c2020-07-01 16:21:27 -07006011
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006012 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006013 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006014 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6015 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006016
6017 // Always consume from window1 since it's the window that has the InputReceiver
6018 consumeMotionEvent(mWindow1, action, expectedPoints);
6019 }
chaviwaf87b3e2019-10-01 16:59:28 -07006020};
6021
6022TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6023 // Touch Window 1
6024 PointF touchedPoint = {10, 10};
6025 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006026 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006027
6028 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006029 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006030
6031 // Touch Window 2
6032 touchedPoint = {150, 150};
6033 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006034 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006035}
6036
chaviw9eaa22c2020-07-01 16:21:27 -07006037TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6038 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006039 mWindow2->setWindowScale(0.5f, 0.5f);
6040
6041 // Touch Window 1
6042 PointF touchedPoint = {10, 10};
6043 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006044 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006045 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006046 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006047
6048 // Touch Window 2
6049 touchedPoint = {150, 150};
6050 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006051 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6052 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006053
chaviw9eaa22c2020-07-01 16:21:27 -07006054 // Update the transform so rotation is set
6055 mWindow2->setWindowTransform(0, -1, 1, 0);
6056 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6057 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006058}
6059
chaviw9eaa22c2020-07-01 16:21:27 -07006060TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006061 mWindow2->setWindowScale(0.5f, 0.5f);
6062
6063 // Touch Window 1
6064 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6065 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006066 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006067
6068 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006069 touchedPoints.push_back(PointF{150, 150});
6070 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006071 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006072
chaviw9eaa22c2020-07-01 16:21:27 -07006073 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006074 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006075 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006076
chaviw9eaa22c2020-07-01 16:21:27 -07006077 // Update the transform so rotation is set for Window 2
6078 mWindow2->setWindowTransform(0, -1, 1, 0);
6079 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006080 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006081}
6082
chaviw9eaa22c2020-07-01 16:21:27 -07006083TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006084 mWindow2->setWindowScale(0.5f, 0.5f);
6085
6086 // Touch Window 1
6087 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6088 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006089 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006090
6091 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006092 touchedPoints.push_back(PointF{150, 150});
6093 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006094
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006095 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006096
6097 // Move both windows
6098 touchedPoints = {{20, 20}, {175, 175}};
6099 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6100 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6101
chaviw9eaa22c2020-07-01 16:21:27 -07006102 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006103
chaviw9eaa22c2020-07-01 16:21:27 -07006104 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006105 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006106 expectedPoints.pop_back();
6107
6108 // Touch Window 2
6109 mWindow2->setWindowTransform(0, -1, 1, 0);
6110 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006111 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006112
6113 // Move both windows
6114 touchedPoints = {{20, 20}, {175, 175}};
6115 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6116 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6117
6118 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006119}
6120
6121TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6122 mWindow1->setWindowScale(0.5f, 0.5f);
6123
6124 // Touch Window 1
6125 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6126 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006127 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006128
6129 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006130 touchedPoints.push_back(PointF{150, 150});
6131 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006132
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006133 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006134
6135 // Move both windows
6136 touchedPoints = {{20, 20}, {175, 175}};
6137 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6138 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6139
chaviw9eaa22c2020-07-01 16:21:27 -07006140 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006141}
6142
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006143class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6144 virtual void SetUp() override {
6145 InputDispatcherTest::SetUp();
6146
Chris Yea209fde2020-07-22 13:54:51 -07006147 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006148 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006149 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6150 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006151 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006152 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006153 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006154
6155 // Set focused application.
6156 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6157
6158 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006159 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006160 mWindow->consumeFocusEvent(true);
6161 }
6162
6163 virtual void TearDown() override {
6164 InputDispatcherTest::TearDown();
6165 mWindow.clear();
6166 }
6167
6168protected:
Chris Yea209fde2020-07-22 13:54:51 -07006169 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006170 sp<FakeWindowHandle> mWindow;
6171 static constexpr PointF WINDOW_LOCATION = {20, 20};
6172
6173 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006174 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006175 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6176 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006178 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6179 WINDOW_LOCATION));
6180 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006181
6182 sp<FakeWindowHandle> addSpyWindow() {
6183 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006184 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006185 spy->setTrustedOverlay(true);
6186 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006187 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006188 spy->setDispatchingTimeout(30ms);
6189 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6190 return spy;
6191 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006192};
6193
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006194// Send a tap and respond, which should not cause an ANR.
6195TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6196 tapOnWindow();
6197 mWindow->consumeMotionDown();
6198 mWindow->consumeMotionUp();
6199 ASSERT_TRUE(mDispatcher->waitForIdle());
6200 mFakePolicy->assertNotifyAnrWasNotCalled();
6201}
6202
6203// Send a regular key and respond, which should not cause an ANR.
6204TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006205 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006206 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6207 ASSERT_TRUE(mDispatcher->waitForIdle());
6208 mFakePolicy->assertNotifyAnrWasNotCalled();
6209}
6210
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006211TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6212 mWindow->setFocusable(false);
6213 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6214 mWindow->consumeFocusEvent(false);
6215
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006216 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006217 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6218 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6219 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006220 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006221 // Key will not go to window because we have no focused window.
6222 // The 'no focused window' ANR timer should start instead.
6223
6224 // Now, the focused application goes away.
6225 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6226 // The key should get dropped and there should be no ANR.
6227
6228 ASSERT_TRUE(mDispatcher->waitForIdle());
6229 mFakePolicy->assertNotifyAnrWasNotCalled();
6230}
6231
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006232// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006233// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6234// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006235TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006236 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006237 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6238 WINDOW_LOCATION));
6239
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006240 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6241 ASSERT_TRUE(sequenceNum);
6242 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006243 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006244
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006245 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006246 mWindow->consumeMotionEvent(
6247 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006248 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006249 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006250}
6251
6252// Send a key to the app and have the app not respond right away.
6253TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6254 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006255 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006256 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6257 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006258 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006259 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006260 ASSERT_TRUE(mDispatcher->waitForIdle());
6261}
6262
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006263// We have a focused application, but no focused window
6264TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006265 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006266 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6267 mWindow->consumeFocusEvent(false);
6268
6269 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006270 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006271 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6272 WINDOW_LOCATION));
6273 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6274 mDispatcher->waitForIdle();
6275 mFakePolicy->assertNotifyAnrWasNotCalled();
6276
6277 // Once a focused event arrives, we get an ANR for this application
6278 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6279 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006280 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006281 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6282 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006283 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006284 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006285 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006286 ASSERT_TRUE(mDispatcher->waitForIdle());
6287}
6288
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006289/**
6290 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6291 * there will not be an ANR.
6292 */
6293TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6294 mWindow->setFocusable(false);
6295 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6296 mWindow->consumeFocusEvent(false);
6297
6298 KeyEvent event;
6299 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6300 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6301
6302 // Define a valid key down event that is stale (too old).
6303 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6304 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006305 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006306
6307 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6308
6309 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006310 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006311 InputEventInjectionSync::WAIT_FOR_RESULT,
6312 INJECT_EVENT_TIMEOUT, policyFlags);
6313 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6314 << "Injection should fail because the event is stale";
6315
6316 ASSERT_TRUE(mDispatcher->waitForIdle());
6317 mFakePolicy->assertNotifyAnrWasNotCalled();
6318 mWindow->assertNoEvents();
6319}
6320
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006321// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006322// Make sure that we don't notify policy twice about the same ANR.
6323TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006324 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006325 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6326 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006327
6328 // Once a focused event arrives, we get an ANR for this application
6329 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6330 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006331 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006332 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6333 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006334 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006335 const std::chrono::duration appTimeout =
6336 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6337 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006338
Vishnu Naire4df8752022-09-08 09:17:55 -07006339 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006340 // ANR should not be raised again. It is up to policy to do that if it desires.
6341 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006342
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006343 // If we now get a focused window, the ANR should stop, but the policy handles that via
6344 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006345 ASSERT_TRUE(mDispatcher->waitForIdle());
6346}
6347
6348// We have a focused application, but no focused window
6349TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006350 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006351 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6352 mWindow->consumeFocusEvent(false);
6353
6354 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006355 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006356 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006357 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6358 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006359
Vishnu Naire4df8752022-09-08 09:17:55 -07006360 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6361 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006362
6363 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006364 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006365 ASSERT_TRUE(mDispatcher->waitForIdle());
6366 mWindow->assertNoEvents();
6367}
6368
6369/**
6370 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6371 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6372 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6373 * the ANR mechanism should still work.
6374 *
6375 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6376 * DOWN event, while not responding on the second one.
6377 */
6378TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6379 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6380 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6381 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6382 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6383 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006384 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006385
6386 // Now send ACTION_UP, with identical timestamp
6387 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6388 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6389 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6390 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006391 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006392
6393 // We have now sent down and up. Let's consume first event and then ANR on the second.
6394 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6395 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006396 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006397}
6398
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006399// A spy window can receive an ANR
6400TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6401 sp<FakeWindowHandle> spy = addSpyWindow();
6402
6403 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6404 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6405 WINDOW_LOCATION));
6406 mWindow->consumeMotionDown();
6407
6408 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6409 ASSERT_TRUE(sequenceNum);
6410 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006411 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006412
6413 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006414 spy->consumeMotionEvent(
6415 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006416 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006417 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006418}
6419
6420// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006421// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006422TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6423 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006424
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006425 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6426 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006427 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006428 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006429
6430 // Stuck on the ACTION_UP
6431 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006432 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006433
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006434 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006435 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006436 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6437 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006438
6439 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6440 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006441 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006442 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006443 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006444}
6445
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006446// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006447// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006448TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6449 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006450
6451 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006452 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6453 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006454
6455 mWindow->consumeMotionDown();
6456 // Stuck on the ACTION_UP
6457 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006458 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006459
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006460 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006461 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006462 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6463 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006464
6465 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6466 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006467 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006468 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006469 spy->assertNoEvents();
6470}
6471
6472TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6473 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6474
6475 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6476
6477 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6478 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6479 WINDOW_LOCATION));
6480
6481 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6482 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6483 ASSERT_TRUE(consumeSeq);
6484
Prabir Pradhanedd96402022-02-15 01:46:16 -08006485 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006486
6487 monitor.finishEvent(*consumeSeq);
6488 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6489
6490 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006491 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006492}
6493
6494// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6495// process events, you don't get an anr. When the window later becomes unresponsive again, you
6496// get an ANR again.
6497// 1. tap -> block on ACTION_UP -> receive ANR
6498// 2. consume all pending events (= queue becomes healthy again)
6499// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6500TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6501 tapOnWindow();
6502
6503 mWindow->consumeMotionDown();
6504 // Block on ACTION_UP
6505 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006506 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006507 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6508 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006509 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006510 mWindow->assertNoEvents();
6511
6512 tapOnWindow();
6513 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006514 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006515 mWindow->consumeMotionUp();
6516
6517 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006518 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006519 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006520 mWindow->assertNoEvents();
6521}
6522
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006523// If a connection remains unresponsive for a while, make sure policy is only notified once about
6524// it.
6525TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006526 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006527 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6528 WINDOW_LOCATION));
6529
6530 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006531 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006532 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006533 // 'notifyConnectionUnresponsive' should only be called once per connection
6534 mFakePolicy->assertNotifyAnrWasNotCalled();
6535 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006536 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006537 mWindow->consumeMotionEvent(
6538 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006539 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006540 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}
6544
6545/**
6546 * If a window is processing a motion event, and then a key event comes in, the key event should
6547 * not to to the focused window until the motion is processed.
6548 *
6549 * Warning!!!
6550 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6551 * and the injection timeout that we specify when injecting the key.
6552 * We must have the injection timeout (10ms) be smaller than
6553 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6554 *
6555 * If that value changes, this test should also change.
6556 */
6557TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
6558 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6559 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6560
6561 tapOnWindow();
6562 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6563 ASSERT_TRUE(downSequenceNum);
6564 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6565 ASSERT_TRUE(upSequenceNum);
6566 // Don't finish the events yet, and send a key
6567 // Injection will "succeed" because we will eventually give up and send the key to the focused
6568 // window even if motions are still being processed. But because the injection timeout is short,
6569 // we will receive INJECTION_TIMED_OUT as the result.
6570
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006571 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006572 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006573 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6574 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006575 // Key will not be sent to the window, yet, because the window is still processing events
6576 // and the key remains pending, waiting for the touch events to be processed
6577 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6578 ASSERT_FALSE(keySequenceNum);
6579
6580 std::this_thread::sleep_for(500ms);
6581 // if we wait long enough though, dispatcher will give up, and still send the key
6582 // to the focused window, even though we have not yet finished the motion event
6583 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6584 mWindow->finishEvent(*downSequenceNum);
6585 mWindow->finishEvent(*upSequenceNum);
6586}
6587
6588/**
6589 * If a window is processing a motion event, and then a key event comes in, the key event should
6590 * not go to the focused window until the motion is processed.
6591 * If then a new motion comes in, then the pending key event should be going to the currently
6592 * focused window right away.
6593 */
6594TEST_F(InputDispatcherSingleWindowAnr,
6595 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
6596 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6597 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6598
6599 tapOnWindow();
6600 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6601 ASSERT_TRUE(downSequenceNum);
6602 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6603 ASSERT_TRUE(upSequenceNum);
6604 // Don't finish the events yet, and send a key
6605 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006606 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006607 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6608 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006609 // At this point, key is still pending, and should not be sent to the application yet.
6610 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6611 ASSERT_FALSE(keySequenceNum);
6612
6613 // Now tap down again. It should cause the pending key to go to the focused window right away.
6614 tapOnWindow();
6615 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
6616 // the other events yet. We can finish events in any order.
6617 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
6618 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
6619 mWindow->consumeMotionDown();
6620 mWindow->consumeMotionUp();
6621 mWindow->assertNoEvents();
6622}
6623
6624class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
6625 virtual void SetUp() override {
6626 InputDispatcherTest::SetUp();
6627
Chris Yea209fde2020-07-22 13:54:51 -07006628 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006629 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006630 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
6631 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006632 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006633 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006634 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006635
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006636 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
6637 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006638 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006639 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006640
6641 // Set focused application.
6642 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07006643 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006644
6645 // Expect one focus window exist in display.
6646 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006647 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006648 mFocusedWindow->consumeFocusEvent(true);
6649 }
6650
6651 virtual void TearDown() override {
6652 InputDispatcherTest::TearDown();
6653
6654 mUnfocusedWindow.clear();
6655 mFocusedWindow.clear();
6656 }
6657
6658protected:
Chris Yea209fde2020-07-22 13:54:51 -07006659 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006660 sp<FakeWindowHandle> mUnfocusedWindow;
6661 sp<FakeWindowHandle> mFocusedWindow;
6662 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
6663 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
6664 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
6665
6666 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
6667
6668 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
6669
6670private:
6671 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006672 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006673 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6674 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006675 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006676 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6677 location));
6678 }
6679};
6680
6681// If we have 2 windows that are both unresponsive, the one with the shortest timeout
6682// should be ANR'd first.
6683TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006685 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6686 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006687 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006688 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006689 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006690 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006691 // We consumed all events, so no ANR
6692 ASSERT_TRUE(mDispatcher->waitForIdle());
6693 mFakePolicy->assertNotifyAnrWasNotCalled();
6694
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 FOCUSED_WINDOW_LOCATION));
6698 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
6699 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006700
6701 const std::chrono::duration timeout =
6702 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006703 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006704 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
6705 // sequence to make it consistent
6706 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006707 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006708 mFocusedWindow->consumeMotionDown();
6709 // This cancel is generated because the connection was unresponsive
6710 mFocusedWindow->consumeMotionCancel();
6711 mFocusedWindow->assertNoEvents();
6712 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006713 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006714 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6715 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006716 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006717}
6718
6719// If we have 2 windows with identical timeouts that are both unresponsive,
6720// it doesn't matter which order they should have ANR.
6721// But we should receive ANR for both.
6722TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
6723 // Set the timeout for unfocused window to match the focused window
6724 mUnfocusedWindow->setDispatchingTimeout(10ms);
6725 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6726
6727 tapOnFocusedWindow();
6728 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08006729 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
6730 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
6731 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006732
6733 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006734 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
6735 mFocusedWindow->getToken() == anrConnectionToken2);
6736 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
6737 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006738
6739 ASSERT_TRUE(mDispatcher->waitForIdle());
6740 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006741
6742 mFocusedWindow->consumeMotionDown();
6743 mFocusedWindow->consumeMotionUp();
6744 mUnfocusedWindow->consumeMotionOutside();
6745
Prabir Pradhanedd96402022-02-15 01:46:16 -08006746 sp<IBinder> responsiveToken1, responsiveToken2;
6747 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
6748 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006749
6750 // Both applications should be marked as responsive, in any order
6751 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
6752 mFocusedWindow->getToken() == responsiveToken2);
6753 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
6754 mUnfocusedWindow->getToken() == responsiveToken2);
6755 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006756}
6757
6758// If a window is already not responding, the second tap on the same window should be ignored.
6759// We should also log an error to account for the dropped event (not tested here).
6760// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
6761TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
6762 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006763 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006764 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006765 // Receive the events, but don't respond
6766 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
6767 ASSERT_TRUE(downEventSequenceNum);
6768 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
6769 ASSERT_TRUE(upEventSequenceNum);
6770 const std::chrono::duration timeout =
6771 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006772 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006773
6774 // Tap once again
6775 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006776 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006777 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6778 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006779 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006780 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6781 FOCUSED_WINDOW_LOCATION));
6782 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
6783 // valid touch target
6784 mUnfocusedWindow->assertNoEvents();
6785
6786 // Consume the first tap
6787 mFocusedWindow->finishEvent(*downEventSequenceNum);
6788 mFocusedWindow->finishEvent(*upEventSequenceNum);
6789 ASSERT_TRUE(mDispatcher->waitForIdle());
6790 // The second tap did not go to the focused window
6791 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006792 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08006793 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6794 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006795 mFakePolicy->assertNotifyAnrWasNotCalled();
6796}
6797
6798// If you tap outside of all windows, there will not be ANR
6799TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006800 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006801 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6802 LOCATION_OUTSIDE_ALL_WINDOWS));
6803 ASSERT_TRUE(mDispatcher->waitForIdle());
6804 mFakePolicy->assertNotifyAnrWasNotCalled();
6805}
6806
6807// Since the focused window is paused, tapping on it should not produce any events
6808TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
6809 mFocusedWindow->setPaused(true);
6810 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6811
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006812 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006813 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6814 FOCUSED_WINDOW_LOCATION));
6815
6816 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
6817 ASSERT_TRUE(mDispatcher->waitForIdle());
6818 // Should not ANR because the window is paused, and touches shouldn't go to it
6819 mFakePolicy->assertNotifyAnrWasNotCalled();
6820
6821 mFocusedWindow->assertNoEvents();
6822 mUnfocusedWindow->assertNoEvents();
6823}
6824
6825/**
6826 * If a window is processing a motion event, and then a key event comes in, the key event should
6827 * not to to the focused window until the motion is processed.
6828 * If a different window becomes focused at this time, the key should go to that window instead.
6829 *
6830 * Warning!!!
6831 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6832 * and the injection timeout that we specify when injecting the key.
6833 * We must have the injection timeout (10ms) be smaller than
6834 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6835 *
6836 * If that value changes, this test should also change.
6837 */
6838TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
6839 // Set a long ANR timeout to prevent it from triggering
6840 mFocusedWindow->setDispatchingTimeout(2s);
6841 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6842
6843 tapOnUnfocusedWindow();
6844 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
6845 ASSERT_TRUE(downSequenceNum);
6846 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
6847 ASSERT_TRUE(upSequenceNum);
6848 // Don't finish the events yet, and send a key
6849 // Injection will succeed because we will eventually give up and send the key to the focused
6850 // window even if motions are still being processed.
6851
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006852 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006853 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6854 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006855 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006856 // Key will not be sent to the window, yet, because the window is still processing events
6857 // and the key remains pending, waiting for the touch events to be processed
6858 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
6859 ASSERT_FALSE(keySequenceNum);
6860
6861 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07006862 mFocusedWindow->setFocusable(false);
6863 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006864 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006865 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006866
6867 // Focus events should precede the key events
6868 mUnfocusedWindow->consumeFocusEvent(true);
6869 mFocusedWindow->consumeFocusEvent(false);
6870
6871 // Finish the tap events, which should unblock dispatcher
6872 mUnfocusedWindow->finishEvent(*downSequenceNum);
6873 mUnfocusedWindow->finishEvent(*upSequenceNum);
6874
6875 // Now that all queues are cleared and no backlog in the connections, the key event
6876 // can finally go to the newly focused "mUnfocusedWindow".
6877 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6878 mFocusedWindow->assertNoEvents();
6879 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006880 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006881}
6882
6883// When the touch stream is split across 2 windows, and one of them does not respond,
6884// then ANR should be raised and the touch should be canceled for the unresponsive window.
6885// The other window should not be affected by that.
6886TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
6887 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00006888 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6889 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6890 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006891 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006892 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006893
6894 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00006895 mDispatcher->notifyMotion(
6896 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6897 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006898
6899 const std::chrono::duration timeout =
6900 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006901 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006902
6903 mUnfocusedWindow->consumeMotionDown();
6904 mFocusedWindow->consumeMotionDown();
6905 // Focused window may or may not receive ACTION_MOVE
6906 // But it should definitely receive ACTION_CANCEL due to the ANR
6907 InputEvent* event;
6908 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
6909 ASSERT_TRUE(moveOrCancelSequenceNum);
6910 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
6911 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006912 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006913 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
6914 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
6915 mFocusedWindow->consumeMotionCancel();
6916 } else {
6917 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
6918 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006919 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006920 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6921 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006922
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006923 mUnfocusedWindow->assertNoEvents();
6924 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006925 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006926}
6927
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006928/**
6929 * If we have no focused window, and a key comes in, we start the ANR timer.
6930 * The focused application should add a focused window before the timer runs out to prevent ANR.
6931 *
6932 * If the user touches another application during this time, the key should be dropped.
6933 * Next, if a new focused window comes in, without toggling the focused application,
6934 * then no ANR should occur.
6935 *
6936 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
6937 * but in some cases the policy may not update the focused application.
6938 */
6939TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
6940 std::shared_ptr<FakeApplicationHandle> focusedApplication =
6941 std::make_shared<FakeApplicationHandle>();
6942 focusedApplication->setDispatchingTimeout(60ms);
6943 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
6944 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
6945 mFocusedWindow->setFocusable(false);
6946
6947 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6948 mFocusedWindow->consumeFocusEvent(false);
6949
6950 // Send a key. The ANR timer should start because there is no focused window.
6951 // 'focusedApplication' will get blamed if this timer completes.
6952 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006953 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006954 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6955 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6956 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006957 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006958
6959 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
6960 // then the injected touches won't cause the focused event to get dropped.
6961 // The dispatcher only checks for whether the queue should be pruned upon queueing.
6962 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
6963 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
6964 // For this test, it means that the key would get delivered to the window once it becomes
6965 // focused.
6966 std::this_thread::sleep_for(10ms);
6967
6968 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00006969 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6970 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6971 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006972
6973 // We do not consume the motion right away, because that would require dispatcher to first
6974 // process (== drop) the key event, and by that time, ANR will be raised.
6975 // Set the focused window first.
6976 mFocusedWindow->setFocusable(true);
6977 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6978 setFocusedWindow(mFocusedWindow);
6979 mFocusedWindow->consumeFocusEvent(true);
6980 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
6981 // to another application. This could be a bug / behaviour in the policy.
6982
6983 mUnfocusedWindow->consumeMotionDown();
6984
6985 ASSERT_TRUE(mDispatcher->waitForIdle());
6986 // Should not ANR because we actually have a focused window. It was just added too slowly.
6987 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
6988}
6989
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006990// These tests ensure we cannot send touch events to a window that's positioned behind a window
6991// that has feature NO_INPUT_CHANNEL.
6992// Layout:
6993// Top (closest to user)
6994// mNoInputWindow (above all windows)
6995// mBottomWindow
6996// Bottom (furthest from user)
6997class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
6998 virtual void SetUp() override {
6999 InputDispatcherTest::SetUp();
7000
7001 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007002 mNoInputWindow =
7003 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7004 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007005 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007006 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007007 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7008 // It's perfectly valid for this window to not have an associated input channel
7009
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007010 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7011 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007012 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7013
7014 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7015 }
7016
7017protected:
7018 std::shared_ptr<FakeApplicationHandle> mApplication;
7019 sp<FakeWindowHandle> mNoInputWindow;
7020 sp<FakeWindowHandle> mBottomWindow;
7021};
7022
7023TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7024 PointF touchedPoint = {10, 10};
7025
Prabir Pradhan678438e2023-04-13 19:32:51 +00007026 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7027 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7028 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007029
7030 mNoInputWindow->assertNoEvents();
7031 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7032 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7033 // and therefore should prevent mBottomWindow from receiving touches
7034 mBottomWindow->assertNoEvents();
7035}
7036
7037/**
7038 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7039 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7040 */
7041TEST_F(InputDispatcherMultiWindowOcclusionTests,
7042 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007043 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7044 "Window with input channel and NO_INPUT_CHANNEL",
7045 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007046
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007047 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007048 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7049 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7050
7051 PointF touchedPoint = {10, 10};
7052
Prabir Pradhan678438e2023-04-13 19:32:51 +00007053 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7054 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7055 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007056
7057 mNoInputWindow->assertNoEvents();
7058 mBottomWindow->assertNoEvents();
7059}
7060
Vishnu Nair958da932020-08-21 17:12:37 -07007061class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7062protected:
7063 std::shared_ptr<FakeApplicationHandle> mApp;
7064 sp<FakeWindowHandle> mWindow;
7065 sp<FakeWindowHandle> mMirror;
7066
7067 virtual void SetUp() override {
7068 InputDispatcherTest::SetUp();
7069 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007070 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7071 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7072 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007073 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7074 mWindow->setFocusable(true);
7075 mMirror->setFocusable(true);
7076 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7077 }
7078};
7079
7080TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7081 // Request focus on a mirrored window
7082 setFocusedWindow(mMirror);
7083
7084 // window gets focused
7085 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007086 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7087 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007088 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7089}
7090
7091// A focused & mirrored window remains focused only if the window and its mirror are both
7092// focusable.
7093TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7094 setFocusedWindow(mMirror);
7095
7096 // window gets focused
7097 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007098 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7099 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007100 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007101 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7102 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007103 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7104
7105 mMirror->setFocusable(false);
7106 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7107
7108 // window loses focus since one of the windows associated with the token in not focusable
7109 mWindow->consumeFocusEvent(false);
7110
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007111 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7112 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007113 mWindow->assertNoEvents();
7114}
7115
7116// A focused & mirrored window remains focused until the window and its mirror both become
7117// invisible.
7118TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7119 setFocusedWindow(mMirror);
7120
7121 // window gets focused
7122 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007123 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7124 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007125 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007126 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7127 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007128 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7129
7130 mMirror->setVisible(false);
7131 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7132
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007133 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7134 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007135 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007136 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7137 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007138 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7139
7140 mWindow->setVisible(false);
7141 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7142
7143 // window loses focus only after all windows associated with the token become invisible.
7144 mWindow->consumeFocusEvent(false);
7145
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007146 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7147 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007148 mWindow->assertNoEvents();
7149}
7150
7151// A focused & mirrored window remains focused until both windows are removed.
7152TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7153 setFocusedWindow(mMirror);
7154
7155 // window gets focused
7156 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007157 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7158 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007159 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007160 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7161 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007162 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7163
7164 // single window is removed but the window token remains focused
7165 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7166
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007167 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7168 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007169 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007170 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7171 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007172 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7173
7174 // Both windows are removed
7175 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7176 mWindow->consumeFocusEvent(false);
7177
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007178 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7179 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007180 mWindow->assertNoEvents();
7181}
7182
7183// Focus request can be pending until one window becomes visible.
7184TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7185 // Request focus on an invisible mirror.
7186 mWindow->setVisible(false);
7187 mMirror->setVisible(false);
7188 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7189 setFocusedWindow(mMirror);
7190
7191 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007192 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007193 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7194 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007195
7196 mMirror->setVisible(true);
7197 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7198
7199 // window gets focused
7200 mWindow->consumeFocusEvent(true);
7201 // window gets the pending key event
7202 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7203}
Prabir Pradhan99987712020-11-10 18:43:05 -08007204
7205class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7206protected:
7207 std::shared_ptr<FakeApplicationHandle> mApp;
7208 sp<FakeWindowHandle> mWindow;
7209 sp<FakeWindowHandle> mSecondWindow;
7210
7211 void SetUp() override {
7212 InputDispatcherTest::SetUp();
7213 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007214 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007215 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007216 mSecondWindow =
7217 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007218 mSecondWindow->setFocusable(true);
7219
7220 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7221 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7222
7223 setFocusedWindow(mWindow);
7224 mWindow->consumeFocusEvent(true);
7225 }
7226
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007227 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007228 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007229 }
7230
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007231 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7232 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007233 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007234 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7235 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007236 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007237 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007238 }
7239};
7240
7241TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7242 // Ensure that capture cannot be obtained for unfocused windows.
7243 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7244 mFakePolicy->assertSetPointerCaptureNotCalled();
7245 mSecondWindow->assertNoEvents();
7246
7247 // Ensure that capture can be enabled from the focus window.
7248 requestAndVerifyPointerCapture(mWindow, true);
7249
7250 // Ensure that capture cannot be disabled from a window that does not have capture.
7251 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7252 mFakePolicy->assertSetPointerCaptureNotCalled();
7253
7254 // Ensure that capture can be disabled from the window with capture.
7255 requestAndVerifyPointerCapture(mWindow, false);
7256}
7257
7258TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007259 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007260
7261 setFocusedWindow(mSecondWindow);
7262
7263 // Ensure that the capture disabled event was sent first.
7264 mWindow->consumeCaptureEvent(false);
7265 mWindow->consumeFocusEvent(false);
7266 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007267 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007268
7269 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007270 notifyPointerCaptureChanged({});
7271 notifyPointerCaptureChanged(request);
7272 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007273 mWindow->assertNoEvents();
7274 mSecondWindow->assertNoEvents();
7275 mFakePolicy->assertSetPointerCaptureNotCalled();
7276}
7277
7278TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007279 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007280
7281 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007282 notifyPointerCaptureChanged({});
7283 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007284
7285 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007286 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007287 mWindow->consumeCaptureEvent(false);
7288 mWindow->assertNoEvents();
7289}
7290
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007291TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7292 requestAndVerifyPointerCapture(mWindow, true);
7293
7294 // The first window loses focus.
7295 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007296 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007297 mWindow->consumeCaptureEvent(false);
7298
7299 // Request Pointer Capture from the second window before the notification from InputReader
7300 // arrives.
7301 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007302 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007303
7304 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007305 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007306
7307 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007308 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007309
7310 mSecondWindow->consumeFocusEvent(true);
7311 mSecondWindow->consumeCaptureEvent(true);
7312}
7313
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007314TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7315 // App repeatedly enables and disables capture.
7316 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7317 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7318 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7319 mFakePolicy->assertSetPointerCaptureCalled(false);
7320 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7321 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7322
7323 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7324 // first request is now stale, this should do nothing.
7325 notifyPointerCaptureChanged(firstRequest);
7326 mWindow->assertNoEvents();
7327
7328 // InputReader notifies that the second request was enabled.
7329 notifyPointerCaptureChanged(secondRequest);
7330 mWindow->consumeCaptureEvent(true);
7331}
7332
Prabir Pradhan7092e262022-05-03 16:51:09 +00007333TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7334 requestAndVerifyPointerCapture(mWindow, true);
7335
7336 // App toggles pointer capture off and on.
7337 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7338 mFakePolicy->assertSetPointerCaptureCalled(false);
7339
7340 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7341 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7342
7343 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7344 // preceding "disable" request.
7345 notifyPointerCaptureChanged(enableRequest);
7346
7347 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7348 // any notifications.
7349 mWindow->assertNoEvents();
7350}
7351
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007352class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7353protected:
7354 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007355
7356 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7357 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7358
7359 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7360 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7361
7362 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7363 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7364 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7365 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7366 MAXIMUM_OBSCURING_OPACITY);
7367
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007368 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007369 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007370 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007371
7372 sp<FakeWindowHandle> mTouchWindow;
7373
7374 virtual void SetUp() override {
7375 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007376 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007377 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7378 }
7379
7380 virtual void TearDown() override {
7381 InputDispatcherTest::TearDown();
7382 mTouchWindow.clear();
7383 }
7384
chaviw3277faf2021-05-19 16:45:23 -05007385 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7386 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007387 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007388 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007389 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007390 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007391 return window;
7392 }
7393
7394 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7395 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7396 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007397 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007398 // Generate an arbitrary PID based on the UID
7399 window->setOwnerInfo(1777 + (uid % 10000), uid);
7400 return window;
7401 }
7402
7403 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007404 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7405 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7406 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007407 }
7408};
7409
7410TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007411 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007412 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007413 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007414
7415 touch();
7416
7417 mTouchWindow->assertNoEvents();
7418}
7419
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007420TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007421 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7422 const sp<FakeWindowHandle>& w =
7423 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7424 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7425
7426 touch();
7427
7428 mTouchWindow->assertNoEvents();
7429}
7430
7431TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007432 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7433 const sp<FakeWindowHandle>& w =
7434 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7435 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7436
7437 touch();
7438
7439 w->assertNoEvents();
7440}
7441
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007442TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007443 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7444 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007445
7446 touch();
7447
7448 mTouchWindow->consumeAnyMotionDown();
7449}
7450
7451TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007452 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007453 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007454 w->setFrame(Rect(0, 0, 50, 50));
7455 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007456
7457 touch({PointF{100, 100}});
7458
7459 mTouchWindow->consumeAnyMotionDown();
7460}
7461
7462TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007463 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007464 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007465 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7466
7467 touch();
7468
7469 mTouchWindow->consumeAnyMotionDown();
7470}
7471
7472TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7473 const sp<FakeWindowHandle>& w =
7474 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7475 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007476
7477 touch();
7478
7479 mTouchWindow->consumeAnyMotionDown();
7480}
7481
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007482TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7483 const sp<FakeWindowHandle>& w =
7484 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7485 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7486
7487 touch();
7488
7489 w->assertNoEvents();
7490}
7491
7492/**
7493 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7494 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7495 * window, the occluding window will still receive ACTION_OUTSIDE event.
7496 */
7497TEST_F(InputDispatcherUntrustedTouchesTest,
7498 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7499 const sp<FakeWindowHandle>& w =
7500 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007501 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007502 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7503
7504 touch();
7505
7506 w->consumeMotionOutside();
7507}
7508
7509TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
7510 const sp<FakeWindowHandle>& w =
7511 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007512 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007513 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7514
7515 touch();
7516
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007517 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007518}
7519
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007520TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007521 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007522 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7523 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007524 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7525
7526 touch();
7527
7528 mTouchWindow->consumeAnyMotionDown();
7529}
7530
7531TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
7532 const sp<FakeWindowHandle>& w =
7533 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7534 MAXIMUM_OBSCURING_OPACITY);
7535 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007536
7537 touch();
7538
7539 mTouchWindow->consumeAnyMotionDown();
7540}
7541
7542TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007543 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007544 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7545 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007546 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7547
7548 touch();
7549
7550 mTouchWindow->assertNoEvents();
7551}
7552
7553TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
7554 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
7555 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007556 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7557 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007558 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007559 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7560 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007561 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7562
7563 touch();
7564
7565 mTouchWindow->assertNoEvents();
7566}
7567
7568TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
7569 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
7570 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007571 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7572 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007573 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007574 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7575 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007576 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7577
7578 touch();
7579
7580 mTouchWindow->consumeAnyMotionDown();
7581}
7582
7583TEST_F(InputDispatcherUntrustedTouchesTest,
7584 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
7585 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007586 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7587 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007588 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007589 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7590 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007591 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7592
7593 touch();
7594
7595 mTouchWindow->consumeAnyMotionDown();
7596}
7597
7598TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
7599 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007600 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7601 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007602 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007603 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7604 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007605 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007606
7607 touch();
7608
7609 mTouchWindow->assertNoEvents();
7610}
7611
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007612TEST_F(InputDispatcherUntrustedTouchesTest,
7613 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
7614 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007615 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7616 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007617 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007618 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7619 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007620 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7621
7622 touch();
7623
7624 mTouchWindow->assertNoEvents();
7625}
7626
7627TEST_F(InputDispatcherUntrustedTouchesTest,
7628 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
7629 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007630 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7631 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007632 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007633 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7634 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007635 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7636
7637 touch();
7638
7639 mTouchWindow->consumeAnyMotionDown();
7640}
7641
7642TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
7643 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007644 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7645 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007646 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7647
7648 touch();
7649
7650 mTouchWindow->consumeAnyMotionDown();
7651}
7652
7653TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
7654 const sp<FakeWindowHandle>& w =
7655 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
7656 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7657
7658 touch();
7659
7660 mTouchWindow->consumeAnyMotionDown();
7661}
7662
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007663TEST_F(InputDispatcherUntrustedTouchesTest,
7664 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
7665 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7666 const sp<FakeWindowHandle>& w =
7667 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
7668 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7669
7670 touch();
7671
7672 mTouchWindow->assertNoEvents();
7673}
7674
7675TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
7676 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7677 const sp<FakeWindowHandle>& w =
7678 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
7679 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7680
7681 touch();
7682
7683 mTouchWindow->consumeAnyMotionDown();
7684}
7685
7686TEST_F(InputDispatcherUntrustedTouchesTest,
7687 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
7688 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
7689 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007690 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7691 OPACITY_ABOVE_THRESHOLD);
7692 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7693
7694 touch();
7695
7696 mTouchWindow->consumeAnyMotionDown();
7697}
7698
7699TEST_F(InputDispatcherUntrustedTouchesTest,
7700 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
7701 const sp<FakeWindowHandle>& w1 =
7702 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7703 OPACITY_BELOW_THRESHOLD);
7704 const sp<FakeWindowHandle>& w2 =
7705 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7706 OPACITY_BELOW_THRESHOLD);
7707 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7708
7709 touch();
7710
7711 mTouchWindow->assertNoEvents();
7712}
7713
7714/**
7715 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
7716 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
7717 * (which alone would result in allowing touches) does not affect the blocking behavior.
7718 */
7719TEST_F(InputDispatcherUntrustedTouchesTest,
7720 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
7721 const sp<FakeWindowHandle>& wB =
7722 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7723 OPACITY_BELOW_THRESHOLD);
7724 const sp<FakeWindowHandle>& wC =
7725 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7726 OPACITY_BELOW_THRESHOLD);
7727 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7728
7729 touch();
7730
7731 mTouchWindow->assertNoEvents();
7732}
7733
7734/**
7735 * This test is testing that a window from a different UID but with same application token doesn't
7736 * block the touch. Apps can share the application token for close UI collaboration for example.
7737 */
7738TEST_F(InputDispatcherUntrustedTouchesTest,
7739 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
7740 const sp<FakeWindowHandle>& w =
7741 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7742 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007743 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7744
7745 touch();
7746
7747 mTouchWindow->consumeAnyMotionDown();
7748}
7749
arthurhungb89ccb02020-12-30 16:19:01 +08007750class InputDispatcherDragTests : public InputDispatcherTest {
7751protected:
7752 std::shared_ptr<FakeApplicationHandle> mApp;
7753 sp<FakeWindowHandle> mWindow;
7754 sp<FakeWindowHandle> mSecondWindow;
7755 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007756 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007757 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
7758 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08007759
7760 void SetUp() override {
7761 InputDispatcherTest::SetUp();
7762 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007763 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007764 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007765
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007766 mSecondWindow =
7767 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007768 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007769
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007770 mSpyWindow =
7771 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007772 mSpyWindow->setSpy(true);
7773 mSpyWindow->setTrustedOverlay(true);
7774 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
7775
arthurhungb89ccb02020-12-30 16:19:01 +08007776 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007777 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007778 }
7779
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007780 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
7781 switch (fromSource) {
7782 case AINPUT_SOURCE_TOUCHSCREEN:
7783 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7784 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7785 ADISPLAY_ID_DEFAULT, {50, 50}))
7786 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7787 break;
7788 case AINPUT_SOURCE_STYLUS:
7789 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7790 injectMotionEvent(
7791 mDispatcher,
7792 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
7793 AINPUT_SOURCE_STYLUS)
7794 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007795 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007796 .x(50)
7797 .y(50))
7798 .build()));
7799 break;
7800 case AINPUT_SOURCE_MOUSE:
7801 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7802 injectMotionEvent(
7803 mDispatcher,
7804 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
7805 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7806 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007807 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007808 .x(50)
7809 .y(50))
7810 .build()));
7811 break;
7812 default:
7813 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
7814 }
arthurhungb89ccb02020-12-30 16:19:01 +08007815
7816 // Window should receive motion event.
7817 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007818 // Spy window should also receive motion event
7819 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00007820 }
7821
7822 // Start performing drag, we will create a drag window and transfer touch to it.
7823 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
7824 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007825 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00007826 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007827 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00007828 }
arthurhungb89ccb02020-12-30 16:19:01 +08007829
7830 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007831 mDragWindow =
7832 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007833 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08007834 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007835 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007836
7837 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00007838 bool transferred =
7839 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00007840 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00007841 if (transferred) {
7842 mWindow->consumeMotionCancel();
7843 mDragWindow->consumeMotionDown();
7844 }
7845 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08007846 }
7847};
7848
7849TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007850 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08007851
7852 // Move on window.
7853 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7854 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7855 ADISPLAY_ID_DEFAULT, {50, 50}))
7856 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7857 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7858 mWindow->consumeDragEvent(false, 50, 50);
7859 mSecondWindow->assertNoEvents();
7860
7861 // Move to another window.
7862 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7863 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7864 ADISPLAY_ID_DEFAULT, {150, 50}))
7865 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7866 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7867 mWindow->consumeDragEvent(true, 150, 50);
7868 mSecondWindow->consumeDragEvent(false, 50, 50);
7869
7870 // Move back to original window.
7871 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7872 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7873 ADISPLAY_ID_DEFAULT, {50, 50}))
7874 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7875 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7876 mWindow->consumeDragEvent(false, 50, 50);
7877 mSecondWindow->consumeDragEvent(true, -50, 50);
7878
7879 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7880 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
7881 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7882 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7883 mWindow->assertNoEvents();
7884 mSecondWindow->assertNoEvents();
7885}
7886
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007887TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007888 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007889
7890 // No cancel event after drag start
7891 mSpyWindow->assertNoEvents();
7892
7893 const MotionEvent secondFingerDownEvent =
7894 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7895 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007896 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
7897 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007898 .build();
7899 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7900 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7901 InputEventInjectionSync::WAIT_FOR_RESULT))
7902 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7903
7904 // Receives cancel for first pointer after next pointer down
7905 mSpyWindow->consumeMotionCancel();
7906 mSpyWindow->consumeMotionDown();
7907
7908 mSpyWindow->assertNoEvents();
7909}
7910
arthurhungf452d0b2021-01-06 00:19:52 +08007911TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007912 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08007913
7914 // Move on window.
7915 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7916 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7917 ADISPLAY_ID_DEFAULT, {50, 50}))
7918 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7919 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7920 mWindow->consumeDragEvent(false, 50, 50);
7921 mSecondWindow->assertNoEvents();
7922
7923 // Move to another window.
7924 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7925 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7926 ADISPLAY_ID_DEFAULT, {150, 50}))
7927 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7928 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7929 mWindow->consumeDragEvent(true, 150, 50);
7930 mSecondWindow->consumeDragEvent(false, 50, 50);
7931
7932 // drop to another window.
7933 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7934 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7935 {150, 50}))
7936 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7937 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7938 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7939 mWindow->assertNoEvents();
7940 mSecondWindow->assertNoEvents();
7941}
7942
arthurhung6d4bed92021-03-17 11:59:33 +08007943TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007944 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08007945
7946 // Move on window and keep button pressed.
7947 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7948 injectMotionEvent(mDispatcher,
7949 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7950 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007951 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08007952 .build()))
7953 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7954 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7955 mWindow->consumeDragEvent(false, 50, 50);
7956 mSecondWindow->assertNoEvents();
7957
7958 // Move to another window and release button, expect to drop item.
7959 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7960 injectMotionEvent(mDispatcher,
7961 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7962 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007963 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08007964 .build()))
7965 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7966 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7967 mWindow->assertNoEvents();
7968 mSecondWindow->assertNoEvents();
7969 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7970
7971 // nothing to the window.
7972 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7973 injectMotionEvent(mDispatcher,
7974 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
7975 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007976 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08007977 .build()))
7978 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7979 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7980 mWindow->assertNoEvents();
7981 mSecondWindow->assertNoEvents();
7982}
7983
Arthur Hung54745652022-04-20 07:17:41 +00007984TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007985 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08007986
7987 // Set second window invisible.
7988 mSecondWindow->setVisible(false);
7989 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
7990
7991 // Move on window.
7992 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7993 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7994 ADISPLAY_ID_DEFAULT, {50, 50}))
7995 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7996 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7997 mWindow->consumeDragEvent(false, 50, 50);
7998 mSecondWindow->assertNoEvents();
7999
8000 // Move to another window.
8001 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8002 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8003 ADISPLAY_ID_DEFAULT, {150, 50}))
8004 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8005 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8006 mWindow->consumeDragEvent(true, 150, 50);
8007 mSecondWindow->assertNoEvents();
8008
8009 // drop to another window.
8010 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8011 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8012 {150, 50}))
8013 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8014 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8015 mFakePolicy->assertDropTargetEquals(nullptr);
8016 mWindow->assertNoEvents();
8017 mSecondWindow->assertNoEvents();
8018}
8019
Arthur Hung54745652022-04-20 07:17:41 +00008020TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008021 // Ensure window could track pointerIds if it didn't support split touch.
8022 mWindow->setPreventSplitting(true);
8023
Arthur Hung54745652022-04-20 07:17:41 +00008024 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8025 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8026 {50, 50}))
8027 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8028 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8029
8030 const MotionEvent secondFingerDownEvent =
8031 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8032 .displayId(ADISPLAY_ID_DEFAULT)
8033 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008034 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8035 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008036 .build();
8037 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8038 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8039 InputEventInjectionSync::WAIT_FOR_RESULT))
8040 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008041 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008042
8043 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008044 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008045}
8046
8047TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8048 // First down on second window.
8049 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8050 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8051 {150, 50}))
8052 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8053
8054 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8055
8056 // Second down on first window.
8057 const MotionEvent secondFingerDownEvent =
8058 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8059 .displayId(ADISPLAY_ID_DEFAULT)
8060 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008061 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8062 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008063 .build();
8064 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8065 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8066 InputEventInjectionSync::WAIT_FOR_RESULT))
8067 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8068 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8069
8070 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008071 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008072
8073 // Move on window.
8074 const MotionEvent secondFingerMoveEvent =
8075 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8076 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008077 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8078 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008079 .build();
8080 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8081 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8082 InputEventInjectionSync::WAIT_FOR_RESULT));
8083 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8084 mWindow->consumeDragEvent(false, 50, 50);
8085 mSecondWindow->consumeMotionMove();
8086
8087 // Release the drag pointer should perform drop.
8088 const MotionEvent secondFingerUpEvent =
8089 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8090 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008091 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8092 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008093 .build();
8094 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8095 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8096 InputEventInjectionSync::WAIT_FOR_RESULT));
8097 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8098 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8099 mWindow->assertNoEvents();
8100 mSecondWindow->consumeMotionMove();
8101}
8102
Arthur Hung3915c1f2022-05-31 07:17:17 +00008103TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008104 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008105
8106 // Update window of second display.
8107 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008108 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008109 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8110
8111 // Let second display has a touch state.
8112 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8113 injectMotionEvent(mDispatcher,
8114 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8115 AINPUT_SOURCE_TOUCHSCREEN)
8116 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008117 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008118 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008119 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008120 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008121 // Update window again.
8122 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8123
8124 // Move on window.
8125 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8126 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8127 ADISPLAY_ID_DEFAULT, {50, 50}))
8128 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8129 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8130 mWindow->consumeDragEvent(false, 50, 50);
8131 mSecondWindow->assertNoEvents();
8132
8133 // Move to another window.
8134 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8135 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8136 ADISPLAY_ID_DEFAULT, {150, 50}))
8137 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8138 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8139 mWindow->consumeDragEvent(true, 150, 50);
8140 mSecondWindow->consumeDragEvent(false, 50, 50);
8141
8142 // drop to another window.
8143 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8144 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8145 {150, 50}))
8146 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8147 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8148 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8149 mWindow->assertNoEvents();
8150 mSecondWindow->assertNoEvents();
8151}
8152
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008153TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8154 startDrag(true, AINPUT_SOURCE_MOUSE);
8155 // Move on window.
8156 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8157 injectMotionEvent(mDispatcher,
8158 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8159 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8160 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008161 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008162 .x(50)
8163 .y(50))
8164 .build()))
8165 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8166 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8167 mWindow->consumeDragEvent(false, 50, 50);
8168 mSecondWindow->assertNoEvents();
8169
8170 // Move to another window.
8171 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8172 injectMotionEvent(mDispatcher,
8173 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8174 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8175 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008176 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008177 .x(150)
8178 .y(50))
8179 .build()))
8180 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8181 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8182 mWindow->consumeDragEvent(true, 150, 50);
8183 mSecondWindow->consumeDragEvent(false, 50, 50);
8184
8185 // drop to another window.
8186 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8187 injectMotionEvent(mDispatcher,
8188 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8189 .buttonState(0)
8190 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008191 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008192 .x(150)
8193 .y(50))
8194 .build()))
8195 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8196 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8197 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8198 mWindow->assertNoEvents();
8199 mSecondWindow->assertNoEvents();
8200}
8201
Vishnu Nair062a8672021-09-03 16:07:44 -07008202class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8203
8204TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8205 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008206 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8207 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008208 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008209 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8210 window->setFocusable(true);
8211 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8212 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008213 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008214
8215 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008216 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008217 window->assertNoEvents();
8218
Prabir Pradhan678438e2023-04-13 19:32:51 +00008219 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8220 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008221 window->assertNoEvents();
8222
8223 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008224 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008225 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8226
Prabir Pradhan678438e2023-04-13 19:32:51 +00008227 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008228 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8229
Prabir Pradhan678438e2023-04-13 19:32:51 +00008230 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8231 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008232 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8233 window->assertNoEvents();
8234}
8235
8236TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8237 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8238 std::make_shared<FakeApplicationHandle>();
8239 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008240 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8241 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008242 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8243 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008244 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008245 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008246 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8247 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008248 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008249 window->setOwnerInfo(222, 222);
8250 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8251 window->setFocusable(true);
8252 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8253 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008254 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008255
8256 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008257 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008258 window->assertNoEvents();
8259
Prabir Pradhan678438e2023-04-13 19:32:51 +00008260 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8261 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008262 window->assertNoEvents();
8263
8264 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008265 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008266 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8267
Prabir Pradhan678438e2023-04-13 19:32:51 +00008268 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008269 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8270
Prabir Pradhan678438e2023-04-13 19:32:51 +00008271 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8272 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008273 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8274 window->assertNoEvents();
8275}
8276
8277TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8278 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8279 std::make_shared<FakeApplicationHandle>();
8280 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008281 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8282 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008283 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8284 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008285 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008286 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008287 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8288 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008289 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008290 window->setOwnerInfo(222, 222);
8291 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8292 window->setFocusable(true);
8293 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8294 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008295 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008296
8297 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008298 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008299 window->assertNoEvents();
8300
Prabir Pradhan678438e2023-04-13 19:32:51 +00008301 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8302 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008303 window->assertNoEvents();
8304
8305 // When the window is no longer obscured because it went on top, it should get input
8306 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8307
Prabir Pradhan678438e2023-04-13 19:32:51 +00008308 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008309 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8310
Prabir Pradhan678438e2023-04-13 19:32:51 +00008311 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8312 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008313 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8314 window->assertNoEvents();
8315}
8316
Antonio Kantekf16f2832021-09-28 04:39:20 +00008317class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8318protected:
8319 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008320 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008321 sp<FakeWindowHandle> mWindow;
8322 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008323 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008324
8325 void SetUp() override {
8326 InputDispatcherTest::SetUp();
8327
8328 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008329 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008330 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008331 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008332 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008333 mSecondWindow =
8334 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008335 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008336 mThirdWindow =
8337 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8338 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8339 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008340
8341 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008342 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8343 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8344 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008345 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008346
Antonio Kantek15beb512022-06-13 22:35:41 +00008347 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008348 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008349 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008350 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8351 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008352 mThirdWindow->assertNoEvents();
8353 }
8354
8355 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8356 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008357 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008358 SECOND_DISPLAY_ID)) {
8359 mWindow->assertNoEvents();
8360 mSecondWindow->assertNoEvents();
8361 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008362 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008363 }
8364
Antonio Kantek15beb512022-06-13 22:35:41 +00008365 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8366 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008367 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8368 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008369 mWindow->consumeTouchModeEvent(inTouchMode);
8370 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008371 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008372 }
8373};
8374
Antonio Kantek26defcf2022-02-08 01:12:27 +00008375TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008376 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008377 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8378 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008379 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008380}
8381
Antonio Kantek26defcf2022-02-08 01:12:27 +00008382TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8383 const WindowInfo& windowInfo = *mWindow->getInfo();
8384 int32_t ownerPid = windowInfo.ownerPid;
8385 int32_t ownerUid = windowInfo.ownerUid;
8386 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8387 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008388 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008389 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008390 mWindow->assertNoEvents();
8391 mSecondWindow->assertNoEvents();
8392}
8393
8394TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8395 const WindowInfo& windowInfo = *mWindow->getInfo();
8396 int32_t ownerPid = windowInfo.ownerPid;
8397 int32_t ownerUid = windowInfo.ownerUid;
8398 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008399 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008400 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008401}
8402
Antonio Kantekf16f2832021-09-28 04:39:20 +00008403TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008404 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008405 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8406 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008407 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008408 mWindow->assertNoEvents();
8409 mSecondWindow->assertNoEvents();
8410}
8411
Antonio Kantek15beb512022-06-13 22:35:41 +00008412TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8413 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8414 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8415 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008416 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008417 mWindow->assertNoEvents();
8418 mSecondWindow->assertNoEvents();
8419 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8420}
8421
Antonio Kantek48710e42022-03-24 14:19:30 -07008422TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8423 // Interact with the window first.
8424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8425 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8426 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8427
8428 // Then remove focus.
8429 mWindow->setFocusable(false);
8430 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8431
8432 // Assert that caller can switch touch mode by owning one of the last interacted window.
8433 const WindowInfo& windowInfo = *mWindow->getInfo();
8434 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8435 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008436 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008437}
8438
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008439class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8440public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008441 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008442 std::shared_ptr<FakeApplicationHandle> application =
8443 std::make_shared<FakeApplicationHandle>();
8444 std::string name = "Fake Spy ";
8445 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008446 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8447 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008448 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008449 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008450 return spy;
8451 }
8452
8453 sp<FakeWindowHandle> createForeground() {
8454 std::shared_ptr<FakeApplicationHandle> application =
8455 std::make_shared<FakeApplicationHandle>();
8456 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008457 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8458 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008459 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008460 return window;
8461 }
8462
8463private:
8464 int mSpyCount{0};
8465};
8466
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008467using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008468/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008469 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8470 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008471TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8472 ScopedSilentDeath _silentDeath;
8473
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008474 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008475 spy->setTrustedOverlay(false);
8476 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8477 ".* not a trusted overlay");
8478}
8479
8480/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008481 * Input injection into a display with a spy window but no foreground windows should succeed.
8482 */
8483TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008484 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008485 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8486
8487 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8488 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8489 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8490 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8491}
8492
8493/**
8494 * Verify the order in which different input windows receive events. The touched foreground window
8495 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8496 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8497 * receive events before ones belows it.
8498 *
8499 * Here, we set up a scenario with four windows in the following Z order from the top:
8500 * spy1, spy2, window, spy3.
8501 * We then inject an event and verify that the foreground "window" receives it first, followed by
8502 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
8503 * window.
8504 */
8505TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
8506 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008507 auto spy1 = createSpy();
8508 auto spy2 = createSpy();
8509 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008510 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
8511 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
8512 const size_t numChannels = channels.size();
8513
Michael Wright8e9a8562022-02-09 13:44:29 +00008514 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008515 if (!epollFd.ok()) {
8516 FAIL() << "Failed to create epoll fd";
8517 }
8518
8519 for (size_t i = 0; i < numChannels; i++) {
8520 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
8521 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
8522 FAIL() << "Failed to add fd to epoll";
8523 }
8524 }
8525
8526 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8527 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8528 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8529
8530 std::vector<size_t> eventOrder;
8531 std::vector<struct epoll_event> events(numChannels);
8532 for (;;) {
8533 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
8534 (100ms).count());
8535 if (nFds < 0) {
8536 FAIL() << "Failed to call epoll_wait";
8537 }
8538 if (nFds == 0) {
8539 break; // epoll_wait timed out
8540 }
8541 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07008542 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07008543 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008544 channels[i]->consumeMotionDown();
8545 }
8546 }
8547
8548 // Verify the order in which the events were received.
8549 EXPECT_EQ(3u, eventOrder.size());
8550 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
8551 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
8552 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
8553}
8554
8555/**
8556 * A spy window using the NOT_TOUCHABLE flag does not receive events.
8557 */
8558TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
8559 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008560 auto spy = createSpy();
8561 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008562 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8563
8564 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8565 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8566 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8567 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8568 spy->assertNoEvents();
8569}
8570
8571/**
8572 * A spy window will only receive gestures that originate within its touchable region. Gestures that
8573 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
8574 * to the window.
8575 */
8576TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
8577 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008578 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008579 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
8580 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8581
8582 // Inject an event outside the spy window's touchable region.
8583 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8584 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8585 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8586 window->consumeMotionDown();
8587 spy->assertNoEvents();
8588 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8589 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8590 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8591 window->consumeMotionUp();
8592 spy->assertNoEvents();
8593
8594 // Inject an event inside the spy window's touchable region.
8595 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8596 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8597 {5, 10}))
8598 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8599 window->consumeMotionDown();
8600 spy->consumeMotionDown();
8601}
8602
8603/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008604 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008605 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008606 */
8607TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
8608 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008609 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008610 auto spy = createSpy();
8611 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008612 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008613 spy->setFrame(Rect{0, 0, 20, 20});
8614 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8615
8616 // Inject an event outside the spy window's frame and touchable region.
8617 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008618 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8619 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008620 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8621 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008622 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008623}
8624
8625/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008626 * Even when a spy window spans over multiple foreground windows, the spy should receive all
8627 * pointers that are down within its bounds.
8628 */
8629TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
8630 auto windowLeft = createForeground();
8631 windowLeft->setFrame({0, 0, 100, 200});
8632 auto windowRight = createForeground();
8633 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008634 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008635 spy->setFrame({0, 0, 200, 200});
8636 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
8637
8638 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8639 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8640 {50, 50}))
8641 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8642 windowLeft->consumeMotionDown();
8643 spy->consumeMotionDown();
8644
8645 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008646 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008647 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008648 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8649 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008650 .build();
8651 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8652 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8653 InputEventInjectionSync::WAIT_FOR_RESULT))
8654 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8655 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00008656 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008657}
8658
8659/**
8660 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
8661 * the spy should receive the second pointer with ACTION_DOWN.
8662 */
8663TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
8664 auto window = createForeground();
8665 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008666 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008667 spyRight->setFrame({100, 0, 200, 200});
8668 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
8669
8670 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8671 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8672 {50, 50}))
8673 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8674 window->consumeMotionDown();
8675 spyRight->assertNoEvents();
8676
8677 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008678 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008679 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008680 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8681 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008682 .build();
8683 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8684 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8685 InputEventInjectionSync::WAIT_FOR_RESULT))
8686 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008687 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008688 spyRight->consumeMotionDown();
8689}
8690
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008691/**
8692 * The spy window should not be able to affect whether or not touches are split. Only the foreground
8693 * windows should be allowed to control split touch.
8694 */
8695TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008696 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008697 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008698 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008699 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008700
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008701 auto window = createForeground();
8702 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008703
8704 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8705
8706 // First finger down, no window touched.
8707 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8708 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8709 {100, 200}))
8710 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8711 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8712 window->assertNoEvents();
8713
8714 // Second finger down on window, the window should receive touch down.
8715 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008716 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008717 .displayId(ADISPLAY_ID_DEFAULT)
8718 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008719 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8720 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008721 .build();
8722 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8723 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8724 InputEventInjectionSync::WAIT_FOR_RESULT))
8725 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8726
8727 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00008728 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008729}
8730
8731/**
8732 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
8733 * do not receive key events.
8734 */
8735TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008736 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008737 spy->setFocusable(false);
8738
8739 auto window = createForeground();
8740 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8741 setFocusedWindow(window);
8742 window->consumeFocusEvent(true);
8743
8744 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
8745 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8746 window->consumeKeyDown(ADISPLAY_ID_NONE);
8747
8748 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
8749 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8750 window->consumeKeyUp(ADISPLAY_ID_NONE);
8751
8752 spy->assertNoEvents();
8753}
8754
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008755using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
8756
8757/**
8758 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
8759 * are currently sent to any other windows - including other spy windows - will also be cancelled.
8760 */
8761TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
8762 auto window = createForeground();
8763 auto spy1 = createSpy();
8764 auto spy2 = createSpy();
8765 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
8766
8767 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8768 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8769 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8770 window->consumeMotionDown();
8771 spy1->consumeMotionDown();
8772 spy2->consumeMotionDown();
8773
8774 // Pilfer pointers from the second spy window.
8775 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
8776 spy2->assertNoEvents();
8777 spy1->consumeMotionCancel();
8778 window->consumeMotionCancel();
8779
8780 // The rest of the gesture should only be sent to the second spy window.
8781 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8782 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8783 ADISPLAY_ID_DEFAULT))
8784 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8785 spy2->consumeMotionMove();
8786 spy1->assertNoEvents();
8787 window->assertNoEvents();
8788}
8789
8790/**
8791 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
8792 * in the middle of the gesture.
8793 */
8794TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
8795 auto window = createForeground();
8796 auto spy = createSpy();
8797 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8798
8799 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8800 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8801 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8802 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8803 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8804
8805 window->releaseChannel();
8806
8807 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8808
8809 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8810 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8811 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8812 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8813}
8814
8815/**
8816 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
8817 * the spy, but not to any other windows.
8818 */
8819TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
8820 auto spy = createSpy();
8821 auto window = createForeground();
8822
8823 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8824
8825 // First finger down on the window and the spy.
8826 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8827 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8828 {100, 200}))
8829 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8830 spy->consumeMotionDown();
8831 window->consumeMotionDown();
8832
8833 // Spy window pilfers the pointers.
8834 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8835 window->consumeMotionCancel();
8836
8837 // Second finger down on the window and spy, but the window should not receive the pointer down.
8838 const MotionEvent secondFingerDownEvent =
8839 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8840 .displayId(ADISPLAY_ID_DEFAULT)
8841 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008842 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8843 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008844 .build();
8845 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8846 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8847 InputEventInjectionSync::WAIT_FOR_RESULT))
8848 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8849
Harry Cutts33476232023-01-30 19:57:29 +00008850 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008851
8852 // Third finger goes down outside all windows, so injection should fail.
8853 const MotionEvent thirdFingerDownEvent =
8854 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8855 .displayId(ADISPLAY_ID_DEFAULT)
8856 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008857 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8858 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
8859 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008860 .build();
8861 ASSERT_EQ(InputEventInjectionResult::FAILED,
8862 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8863 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008864 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008865
8866 spy->assertNoEvents();
8867 window->assertNoEvents();
8868}
8869
8870/**
8871 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
8872 */
8873TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
8874 auto spy = createSpy();
8875 spy->setFrame(Rect(0, 0, 100, 100));
8876 auto window = createForeground();
8877 window->setFrame(Rect(0, 0, 200, 200));
8878
8879 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8880
8881 // First finger down on the window only
8882 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8883 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8884 {150, 150}))
8885 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8886 window->consumeMotionDown();
8887
8888 // Second finger down on the spy and window
8889 const MotionEvent secondFingerDownEvent =
8890 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8891 .displayId(ADISPLAY_ID_DEFAULT)
8892 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008893 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
8894 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008895 .build();
8896 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8897 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8898 InputEventInjectionSync::WAIT_FOR_RESULT))
8899 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8900 spy->consumeMotionDown();
8901 window->consumeMotionPointerDown(1);
8902
8903 // Third finger down on the spy and window
8904 const MotionEvent thirdFingerDownEvent =
8905 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8906 .displayId(ADISPLAY_ID_DEFAULT)
8907 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008908 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
8909 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
8910 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008911 .build();
8912 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8913 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8914 InputEventInjectionSync::WAIT_FOR_RESULT))
8915 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8916 spy->consumeMotionPointerDown(1);
8917 window->consumeMotionPointerDown(2);
8918
8919 // Spy window pilfers the pointers.
8920 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8921 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8922 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8923
8924 spy->assertNoEvents();
8925 window->assertNoEvents();
8926}
8927
8928/**
8929 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
8930 * other windows should be canceled. If this results in the cancellation of all pointers for some
8931 * window, then that window should receive ACTION_CANCEL.
8932 */
8933TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
8934 auto spy = createSpy();
8935 spy->setFrame(Rect(0, 0, 100, 100));
8936 auto window = createForeground();
8937 window->setFrame(Rect(0, 0, 200, 200));
8938
8939 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8940
8941 // First finger down on both spy and window
8942 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8943 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8944 {10, 10}))
8945 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8946 window->consumeMotionDown();
8947 spy->consumeMotionDown();
8948
8949 // Second finger down on the spy and window
8950 const MotionEvent secondFingerDownEvent =
8951 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8952 .displayId(ADISPLAY_ID_DEFAULT)
8953 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008954 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
8955 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008956 .build();
8957 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8958 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8959 InputEventInjectionSync::WAIT_FOR_RESULT))
8960 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8961 spy->consumeMotionPointerDown(1);
8962 window->consumeMotionPointerDown(1);
8963
8964 // Spy window pilfers the pointers.
8965 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8966 window->consumeMotionCancel();
8967
8968 spy->assertNoEvents();
8969 window->assertNoEvents();
8970}
8971
8972/**
8973 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
8974 * be sent to other windows
8975 */
8976TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
8977 auto spy = createSpy();
8978 spy->setFrame(Rect(0, 0, 100, 100));
8979 auto window = createForeground();
8980 window->setFrame(Rect(0, 0, 200, 200));
8981
8982 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8983
8984 // First finger down on both window and spy
8985 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8986 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8987 {10, 10}))
8988 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8989 window->consumeMotionDown();
8990 spy->consumeMotionDown();
8991
8992 // Spy window pilfers the pointers.
8993 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8994 window->consumeMotionCancel();
8995
8996 // Second finger down on the window only
8997 const MotionEvent secondFingerDownEvent =
8998 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8999 .displayId(ADISPLAY_ID_DEFAULT)
9000 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009001 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9002 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009003 .build();
9004 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9005 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9006 InputEventInjectionSync::WAIT_FOR_RESULT))
9007 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9008 window->consumeMotionDown();
9009 window->assertNoEvents();
9010
9011 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9012 spy->consumeMotionMove();
9013 spy->assertNoEvents();
9014}
9015
Prabir Pradhand65552b2021-10-07 11:23:50 -07009016class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9017public:
9018 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9019 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9020 std::make_shared<FakeApplicationHandle>();
9021 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009022 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9023 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009024 overlay->setFocusable(false);
9025 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009026 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009027 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009028 overlay->setTrustedOverlay(true);
9029
9030 std::shared_ptr<FakeApplicationHandle> application =
9031 std::make_shared<FakeApplicationHandle>();
9032 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009033 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9034 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009035 window->setFocusable(true);
9036 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009037
9038 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9039 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9040 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009041 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009042 return {std::move(overlay), std::move(window)};
9043 }
9044
9045 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009046 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009047 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009048 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009049 }
9050
9051 void sendStylusEvent(int32_t action) {
9052 NotifyMotionArgs motionArgs =
9053 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9054 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009055 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009056 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009057 }
9058};
9059
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009060using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9061
9062TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9063 ScopedSilentDeath _silentDeath;
9064
Prabir Pradhand65552b2021-10-07 11:23:50 -07009065 auto [overlay, window] = setupStylusOverlayScenario();
9066 overlay->setTrustedOverlay(false);
9067 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9068 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9069 ".* not a trusted overlay");
9070}
9071
9072TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9073 auto [overlay, window] = setupStylusOverlayScenario();
9074 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9075
9076 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9077 overlay->consumeMotionDown();
9078 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9079 overlay->consumeMotionUp();
9080
9081 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9082 window->consumeMotionDown();
9083 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9084 window->consumeMotionUp();
9085
9086 overlay->assertNoEvents();
9087 window->assertNoEvents();
9088}
9089
9090TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9091 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009092 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009093 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9094
9095 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9096 overlay->consumeMotionDown();
9097 window->consumeMotionDown();
9098 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9099 overlay->consumeMotionUp();
9100 window->consumeMotionUp();
9101
9102 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9103 window->consumeMotionDown();
9104 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9105 window->consumeMotionUp();
9106
9107 overlay->assertNoEvents();
9108 window->assertNoEvents();
9109}
9110
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009111/**
9112 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9113 * The scenario is as follows:
9114 * - The stylus interceptor overlay is configured as a spy window.
9115 * - The stylus interceptor spy receives the start of a new stylus gesture.
9116 * - It pilfers pointers and then configures itself to no longer be a spy.
9117 * - The stylus interceptor continues to receive the rest of the gesture.
9118 */
9119TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9120 auto [overlay, window] = setupStylusOverlayScenario();
9121 overlay->setSpy(true);
9122 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9123
9124 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9125 overlay->consumeMotionDown();
9126 window->consumeMotionDown();
9127
9128 // The interceptor pilfers the pointers.
9129 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9130 window->consumeMotionCancel();
9131
9132 // The interceptor configures itself so that it is no longer a spy.
9133 overlay->setSpy(false);
9134 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9135
9136 // It continues to receive the rest of the stylus gesture.
9137 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9138 overlay->consumeMotionMove();
9139 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9140 overlay->consumeMotionUp();
9141
9142 window->assertNoEvents();
9143}
9144
Prabir Pradhan5735a322022-04-11 17:23:34 +00009145struct User {
9146 int32_t mPid;
9147 int32_t mUid;
9148 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9149 std::unique_ptr<InputDispatcher>& mDispatcher;
9150
9151 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9152 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9153
9154 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9155 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9156 ADISPLAY_ID_DEFAULT, {100, 200},
9157 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9158 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9159 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9160 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9161 }
9162
9163 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009164 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009165 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009166 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009167 mPolicyFlags);
9168 }
9169
9170 sp<FakeWindowHandle> createWindow() const {
9171 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9172 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009173 sp<FakeWindowHandle> window =
9174 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9175 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009176 window->setOwnerInfo(mPid, mUid);
9177 return window;
9178 }
9179};
9180
9181using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9182
9183TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9184 auto owner = User(mDispatcher, 10, 11);
9185 auto window = owner.createWindow();
9186 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9187
9188 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9189 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9190 window->consumeMotionDown();
9191
9192 setFocusedWindow(window);
9193 window->consumeFocusEvent(true);
9194
9195 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9196 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9197 window->consumeKeyDown(ADISPLAY_ID_NONE);
9198}
9199
9200TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9201 auto owner = User(mDispatcher, 10, 11);
9202 auto window = owner.createWindow();
9203 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9204
9205 auto rando = User(mDispatcher, 20, 21);
9206 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9207 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9208
9209 setFocusedWindow(window);
9210 window->consumeFocusEvent(true);
9211
9212 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9213 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9214 window->assertNoEvents();
9215}
9216
9217TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9218 auto owner = User(mDispatcher, 10, 11);
9219 auto window = owner.createWindow();
9220 auto spy = owner.createWindow();
9221 spy->setSpy(true);
9222 spy->setTrustedOverlay(true);
9223 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9224
9225 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9226 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9227 spy->consumeMotionDown();
9228 window->consumeMotionDown();
9229}
9230
9231TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9232 auto owner = User(mDispatcher, 10, 11);
9233 auto window = owner.createWindow();
9234
9235 auto rando = User(mDispatcher, 20, 21);
9236 auto randosSpy = rando.createWindow();
9237 randosSpy->setSpy(true);
9238 randosSpy->setTrustedOverlay(true);
9239 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9240
9241 // The event is targeted at owner's window, so injection should succeed, but the spy should
9242 // not receive the event.
9243 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9244 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9245 randosSpy->assertNoEvents();
9246 window->consumeMotionDown();
9247}
9248
9249TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9250 auto owner = User(mDispatcher, 10, 11);
9251 auto window = owner.createWindow();
9252
9253 auto rando = User(mDispatcher, 20, 21);
9254 auto randosSpy = rando.createWindow();
9255 randosSpy->setSpy(true);
9256 randosSpy->setTrustedOverlay(true);
9257 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9258
9259 // A user that has injection permission can inject into any window.
9260 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9261 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9262 ADISPLAY_ID_DEFAULT));
9263 randosSpy->consumeMotionDown();
9264 window->consumeMotionDown();
9265
9266 setFocusedWindow(randosSpy);
9267 randosSpy->consumeFocusEvent(true);
9268
9269 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9270 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9271 window->assertNoEvents();
9272}
9273
9274TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9275 auto owner = User(mDispatcher, 10, 11);
9276 auto window = owner.createWindow();
9277
9278 auto rando = User(mDispatcher, 20, 21);
9279 auto randosWindow = rando.createWindow();
9280 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9281 randosWindow->setWatchOutsideTouch(true);
9282 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9283
9284 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9285 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9286 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9287 window->consumeMotionDown();
9288 randosWindow->consumeMotionOutside();
9289}
9290
Garfield Tane84e6f92019-08-29 17:28:41 -07009291} // namespace android::inputdispatcher