blob: f92b3a3c05d67054b5e3d9ec2dc9c53cb34e67d5 [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();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07003960 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003961 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
3962
3963 // Ensure that the events from the "getRaw" API are in logical display coordinates.
3964 EXPECT_EQ(300, event->getRawX(0));
3965 EXPECT_EQ(880, event->getRawY(0));
3966
3967 // Ensure that the x and y values are in the window's coordinate space.
3968 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
3969 // the logical display space. This will be the origin of the window space.
3970 EXPECT_EQ(100, event->getX(0));
3971 EXPECT_EQ(80, event->getY(0));
3972}
3973
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003974/** Ensure consistent behavior of InputDispatcher in all orientations. */
3975class InputDispatcherDisplayOrientationFixture
3976 : public InputDispatcherDisplayProjectionTest,
3977 public ::testing::WithParamInterface<ui::Rotation> {};
3978
3979// This test verifies the touchable region of a window for all rotations of the display by tapping
3980// in different locations on the display, specifically points close to the four corners of a
3981// window.
3982TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
3983 constexpr static int32_t displayWidth = 400;
3984 constexpr static int32_t displayHeight = 800;
3985
3986 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3987
3988 const auto rotation = GetParam();
3989
3990 // Set up the display with the specified rotation.
3991 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
3992 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
3993 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
3994 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
3995 logicalDisplayWidth, logicalDisplayHeight);
3996 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3997
3998 // Create a window with its bounds determined in the logical display.
3999 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4000 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4001 sp<FakeWindowHandle> window =
4002 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4003 window->setFrame(frameInDisplay, displayTransform);
4004 addWindow(window);
4005
4006 // The following points in logical display space should be inside the window.
4007 static const std::array<vec2, 4> insidePoints{
4008 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4009 for (const auto pointInsideWindow : insidePoints) {
4010 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4011 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004012 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4013 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4014 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004015 window->consumeMotionDown();
4016
Prabir Pradhan678438e2023-04-13 19:32:51 +00004017 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4018 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4019 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004020 window->consumeMotionUp();
4021 }
4022
4023 // The following points in logical display space should be outside the window.
4024 static const std::array<vec2, 5> outsidePoints{
4025 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4026 for (const auto pointOutsideWindow : outsidePoints) {
4027 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4028 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004029 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4030 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4031 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004032
Prabir Pradhan678438e2023-04-13 19:32:51 +00004033 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4034 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4035 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004036 }
4037 window->assertNoEvents();
4038}
4039
4040// Run the precision tests for all rotations.
4041INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4042 InputDispatcherDisplayOrientationFixture,
4043 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4044 ui::ROTATION_270),
4045 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4046 return ftl::enum_string(testParamInfo.param);
4047 });
4048
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004049using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4050 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004051
4052class TransferTouchFixture : public InputDispatcherTest,
4053 public ::testing::WithParamInterface<TransferFunction> {};
4054
4055TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004056 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004057
4058 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004059 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004060 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4061 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004062 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004063 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004064 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4065 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004066 sp<FakeWindowHandle> wallpaper =
4067 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4068 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004069 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004070 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004071
4072 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004073 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4074 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004075
Svet Ganov5d3bc372020-01-26 23:11:07 -08004076 // Only the first window should get the down event
4077 firstWindow->consumeMotionDown();
4078 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004079 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004080
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004081 // Transfer touch to the second window
4082 TransferFunction f = GetParam();
4083 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4084 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004085 // The first window gets cancel and the second gets down
4086 firstWindow->consumeMotionCancel();
4087 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004088 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004089
4090 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004091 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4092 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004093 // The first window gets no events and the second gets up
4094 firstWindow->assertNoEvents();
4095 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004096 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004097}
4098
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004099/**
4100 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4101 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4102 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4103 * natural to the user.
4104 * In this test, we are sending a pointer to both spy window and first window. We then try to
4105 * transfer touch to the second window. The dispatcher should identify the first window as the
4106 * one that should lose the gesture, and therefore the action should be to move the gesture from
4107 * the first window to the second.
4108 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4109 * the other API, as well.
4110 */
4111TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4112 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4113
4114 // Create a couple of windows + a spy window
4115 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004116 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004117 spyWindow->setTrustedOverlay(true);
4118 spyWindow->setSpy(true);
4119 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004120 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004121 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004122 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004123
4124 // Add the windows to the dispatcher
4125 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4126
4127 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004128 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4129 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004130 // Only the first window and spy should get the down event
4131 spyWindow->consumeMotionDown();
4132 firstWindow->consumeMotionDown();
4133
4134 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4135 // if f === 'transferTouch'.
4136 TransferFunction f = GetParam();
4137 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4138 ASSERT_TRUE(success);
4139 // The first window gets cancel and the second gets down
4140 firstWindow->consumeMotionCancel();
4141 secondWindow->consumeMotionDown();
4142
4143 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004144 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4145 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004146 // The first window gets no events and the second+spy get up
4147 firstWindow->assertNoEvents();
4148 spyWindow->consumeMotionUp();
4149 secondWindow->consumeMotionUp();
4150}
4151
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004152TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004153 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004154
4155 PointF touchPoint = {10, 10};
4156
4157 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004158 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004159 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4160 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004161 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004162 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004163 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4164 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004165 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004166
4167 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004168 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004169
4170 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004171 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4172 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4173 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004174 // Only the first window should get the down event
4175 firstWindow->consumeMotionDown();
4176 secondWindow->assertNoEvents();
4177
4178 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004179 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4180 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004181 // Only the first window should get the pointer down event
4182 firstWindow->consumeMotionPointerDown(1);
4183 secondWindow->assertNoEvents();
4184
4185 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004186 TransferFunction f = GetParam();
4187 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4188 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004189 // The first window gets cancel and the second gets down and pointer down
4190 firstWindow->consumeMotionCancel();
4191 secondWindow->consumeMotionDown();
4192 secondWindow->consumeMotionPointerDown(1);
4193
4194 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004195 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4196 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004197 // The first window gets nothing and the second gets pointer up
4198 firstWindow->assertNoEvents();
4199 secondWindow->consumeMotionPointerUp(1);
4200
4201 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004202 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4203 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004204 // The first window gets nothing and the second gets up
4205 firstWindow->assertNoEvents();
4206 secondWindow->consumeMotionUp();
4207}
4208
Arthur Hungc539dbb2022-12-08 07:45:36 +00004209TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4210 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4211
4212 // Create a couple of windows
4213 sp<FakeWindowHandle> firstWindow =
4214 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4215 ADISPLAY_ID_DEFAULT);
4216 firstWindow->setDupTouchToWallpaper(true);
4217 sp<FakeWindowHandle> secondWindow =
4218 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4219 ADISPLAY_ID_DEFAULT);
4220 secondWindow->setDupTouchToWallpaper(true);
4221
4222 sp<FakeWindowHandle> wallpaper1 =
4223 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4224 wallpaper1->setIsWallpaper(true);
4225
4226 sp<FakeWindowHandle> wallpaper2 =
4227 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4228 wallpaper2->setIsWallpaper(true);
4229 // Add the windows to the dispatcher
4230 mDispatcher->setInputWindows(
4231 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4232
4233 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004234 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4235 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004236
4237 // Only the first window should get the down event
4238 firstWindow->consumeMotionDown();
4239 secondWindow->assertNoEvents();
4240 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4241 wallpaper2->assertNoEvents();
4242
4243 // Transfer touch focus to the second window
4244 TransferFunction f = GetParam();
4245 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4246 ASSERT_TRUE(success);
4247
4248 // The first window gets cancel and the second gets down
4249 firstWindow->consumeMotionCancel();
4250 secondWindow->consumeMotionDown();
4251 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4252 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4253
4254 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004255 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4256 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004257 // The first window gets no events and the second gets up
4258 firstWindow->assertNoEvents();
4259 secondWindow->consumeMotionUp();
4260 wallpaper1->assertNoEvents();
4261 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4262}
4263
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004264// For the cases of single pointer touch and two pointers non-split touch, the api's
4265// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4266// for the case where there are multiple pointers split across several windows.
4267INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4268 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004269 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4270 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004271 return dispatcher->transferTouch(destChannelToken,
4272 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004273 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004274 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4275 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004276 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004277 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004278 }));
4279
Svet Ganov5d3bc372020-01-26 23:11:07 -08004280TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004281 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004282
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004283 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004284 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4285 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004286 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004287
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004288 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004289 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4290 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004291 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004292
4293 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004294 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004295
4296 PointF pointInFirst = {300, 200};
4297 PointF pointInSecond = {300, 600};
4298
4299 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004300 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4301 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4302 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004303 // Only the first window should get the down event
4304 firstWindow->consumeMotionDown();
4305 secondWindow->assertNoEvents();
4306
4307 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004308 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4309 ADISPLAY_ID_DEFAULT,
4310 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004311 // The first window gets a move and the second a down
4312 firstWindow->consumeMotionMove();
4313 secondWindow->consumeMotionDown();
4314
4315 // Transfer touch focus to the second window
4316 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4317 // The first window gets cancel and the new gets pointer down (it already saw down)
4318 firstWindow->consumeMotionCancel();
4319 secondWindow->consumeMotionPointerDown(1);
4320
4321 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004322 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4323 ADISPLAY_ID_DEFAULT,
4324 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004325 // The first window gets nothing and the second gets pointer up
4326 firstWindow->assertNoEvents();
4327 secondWindow->consumeMotionPointerUp(1);
4328
4329 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004330 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4331 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004332 // The first window gets nothing and the second gets up
4333 firstWindow->assertNoEvents();
4334 secondWindow->consumeMotionUp();
4335}
4336
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004337// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4338// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4339// touch is not supported, so the touch should continue on those windows and the transferred-to
4340// window should get nothing.
4341TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4342 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4343
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004344 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004345 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4346 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004347 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004348
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004349 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004350 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4351 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004352 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004353
4354 // Add the windows to the dispatcher
4355 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4356
4357 PointF pointInFirst = {300, 200};
4358 PointF pointInSecond = {300, 600};
4359
4360 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004361 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4362 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4363 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004364 // Only the first window should get the down event
4365 firstWindow->consumeMotionDown();
4366 secondWindow->assertNoEvents();
4367
4368 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004369 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4370 ADISPLAY_ID_DEFAULT,
4371 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004372 // The first window gets a move and the second a down
4373 firstWindow->consumeMotionMove();
4374 secondWindow->consumeMotionDown();
4375
4376 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004377 const bool transferred =
4378 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004379 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4380 ASSERT_FALSE(transferred);
4381 firstWindow->assertNoEvents();
4382 secondWindow->assertNoEvents();
4383
4384 // The rest of the dispatch should proceed as normal
4385 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004386 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4387 ADISPLAY_ID_DEFAULT,
4388 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004389 // The first window gets MOVE and the second gets pointer up
4390 firstWindow->consumeMotionMove();
4391 secondWindow->consumeMotionUp();
4392
4393 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004394 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4395 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004396 // The first window gets nothing and the second gets up
4397 firstWindow->consumeMotionUp();
4398 secondWindow->assertNoEvents();
4399}
4400
Arthur Hungabbb9d82021-09-01 14:52:30 +00004401// This case will create two windows and one mirrored window on the default display and mirror
4402// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4403// the windows info of second display before default display.
4404TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4405 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4406 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004407 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004408 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004409 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004410 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004411 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004412
4413 sp<FakeWindowHandle> mirrorWindowInPrimary =
4414 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4415 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004416
4417 sp<FakeWindowHandle> firstWindowInSecondary =
4418 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4419 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004420
4421 sp<FakeWindowHandle> secondWindowInSecondary =
4422 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4423 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004424
4425 // Update window info, let it find window handle of second display first.
4426 mDispatcher->setInputWindows(
4427 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4428 {ADISPLAY_ID_DEFAULT,
4429 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4430
4431 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4432 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4433 {50, 50}))
4434 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4435
4436 // Window should receive motion event.
4437 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4438
4439 // Transfer touch focus
4440 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4441 secondWindowInPrimary->getToken()));
4442 // The first window gets cancel.
4443 firstWindowInPrimary->consumeMotionCancel();
4444 secondWindowInPrimary->consumeMotionDown();
4445
4446 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4447 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4448 ADISPLAY_ID_DEFAULT, {150, 50}))
4449 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4450 firstWindowInPrimary->assertNoEvents();
4451 secondWindowInPrimary->consumeMotionMove();
4452
4453 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4454 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4455 {150, 50}))
4456 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4457 firstWindowInPrimary->assertNoEvents();
4458 secondWindowInPrimary->consumeMotionUp();
4459}
4460
4461// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4462// 'transferTouch' api.
4463TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4464 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4465 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004466 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004467 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004468 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004469 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004470 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004471
4472 sp<FakeWindowHandle> mirrorWindowInPrimary =
4473 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4474 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004475
4476 sp<FakeWindowHandle> firstWindowInSecondary =
4477 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4478 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004479
4480 sp<FakeWindowHandle> secondWindowInSecondary =
4481 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4482 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004483
4484 // Update window info, let it find window handle of second display first.
4485 mDispatcher->setInputWindows(
4486 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4487 {ADISPLAY_ID_DEFAULT,
4488 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4489
4490 // Touch on second display.
4491 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4492 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4493 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4494
4495 // Window should receive motion event.
4496 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4497
4498 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004499 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004500
4501 // The first window gets cancel.
4502 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4503 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4504
4505 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4506 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4507 SECOND_DISPLAY_ID, {150, 50}))
4508 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4509 firstWindowInPrimary->assertNoEvents();
4510 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4511
4512 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4513 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4514 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4515 firstWindowInPrimary->assertNoEvents();
4516 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4517}
4518
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004519TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004520 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004521 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4522 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004523
Vishnu Nair47074b82020-08-14 11:54:47 -07004524 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004525 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004526 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004527
4528 window->consumeFocusEvent(true);
4529
Prabir Pradhan678438e2023-04-13 19:32:51 +00004530 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004531
4532 // Window should receive key down event.
4533 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4534}
4535
4536TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004537 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004538 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4539 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004540
Arthur Hung72d8dc32020-03-28 00:48:39 +00004541 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004542
Prabir Pradhan678438e2023-04-13 19:32:51 +00004543 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004544 mDispatcher->waitForIdle();
4545
4546 window->assertNoEvents();
4547}
4548
4549// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4550TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004551 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004552 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4553 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004554
Arthur Hung72d8dc32020-03-28 00:48:39 +00004555 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004556
4557 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004558 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004559 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004560 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4561 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004562
4563 // Window should receive only the motion event
4564 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4565 window->assertNoEvents(); // Key event or focus event will not be received
4566}
4567
arthurhungea3f4fc2020-12-21 23:18:53 +08004568TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4569 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4570
arthurhungea3f4fc2020-12-21 23:18:53 +08004571 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004572 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4573 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004574 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004575
arthurhungea3f4fc2020-12-21 23:18:53 +08004576 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004577 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4578 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004579 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004580
4581 // Add the windows to the dispatcher
4582 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4583
4584 PointF pointInFirst = {300, 200};
4585 PointF pointInSecond = {300, 600};
4586
4587 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004588 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4589 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4590 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004591 // Only the first window should get the down event
4592 firstWindow->consumeMotionDown();
4593 secondWindow->assertNoEvents();
4594
4595 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004596 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4597 ADISPLAY_ID_DEFAULT,
4598 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004599 // The first window gets a move and the second a down
4600 firstWindow->consumeMotionMove();
4601 secondWindow->consumeMotionDown();
4602
4603 // Send pointer cancel to the second window
4604 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004605 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004606 {pointInFirst, pointInSecond});
4607 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004608 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004609 // The first window gets move and the second gets cancel.
4610 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4611 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4612
4613 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004614 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4615 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08004616 // The first window gets up and the second gets nothing.
4617 firstWindow->consumeMotionUp();
4618 secondWindow->assertNoEvents();
4619}
4620
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004621TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
4622 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4623
4624 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004625 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004626 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4627 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
4628 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
4629 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
4630
Harry Cutts33476232023-01-30 19:57:29 +00004631 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004632 window->assertNoEvents();
4633 mDispatcher->waitForIdle();
4634}
4635
chaviwd1c23182019-12-20 18:44:56 -08004636class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00004637public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004638 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004639 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07004640 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004641 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07004642 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00004643 }
4644
chaviwd1c23182019-12-20 18:44:56 -08004645 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
4646
4647 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004648 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
4649 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08004650 }
4651
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004652 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
4653
4654 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
4655
chaviwd1c23182019-12-20 18:44:56 -08004656 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004657 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08004658 expectedDisplayId, expectedFlags);
4659 }
4660
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004661 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004662 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004663 expectedDisplayId, expectedFlags);
4664 }
4665
chaviwd1c23182019-12-20 18:44:56 -08004666 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004667 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08004668 expectedDisplayId, expectedFlags);
4669 }
4670
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004671 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004672 mInputReceiver->consumeMotionEvent(
4673 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4674 WithDisplayId(expectedDisplayId),
4675 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004676 }
4677
Arthur Hungfbfa5722021-11-16 02:45:54 +00004678 void consumeMotionPointerDown(int32_t pointerIdx) {
4679 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4680 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004681 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004682 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004683 }
4684
Evan Rosky84f07f02021-04-16 10:42:42 -07004685 MotionEvent* consumeMotion() {
4686 InputEvent* event = mInputReceiver->consume();
4687 if (!event) {
4688 ADD_FAILURE() << "No event was produced";
4689 return nullptr;
4690 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004691 if (event->getType() != InputEventType::MOTION) {
4692 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07004693 return nullptr;
4694 }
4695 return static_cast<MotionEvent*>(event);
4696 }
4697
chaviwd1c23182019-12-20 18:44:56 -08004698 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4699
4700private:
4701 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004702};
4703
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004704using InputDispatcherMonitorTest = InputDispatcherTest;
4705
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004706/**
4707 * Two entities that receive touch: A window, and a global monitor.
4708 * The touch goes to the window, and then the window disappears.
4709 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4710 * for the monitor, as well.
4711 * 1. foregroundWindow
4712 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4713 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004714TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004715 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4716 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004717 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004718
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004719 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004720
4721 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4722 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4723 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4724 {100, 200}))
4725 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4726
4727 // Both the foreground window and the global monitor should receive the touch down
4728 window->consumeMotionDown();
4729 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4730
4731 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4732 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4733 ADISPLAY_ID_DEFAULT, {110, 200}))
4734 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4735
4736 window->consumeMotionMove();
4737 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4738
4739 // Now the foreground window goes away
4740 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4741 window->consumeMotionCancel();
4742 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
4743
4744 // If more events come in, there will be no more foreground window to send them to. This will
4745 // cause a cancel for the monitor, as well.
4746 ASSERT_EQ(InputEventInjectionResult::FAILED,
4747 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4748 ADISPLAY_ID_DEFAULT, {120, 200}))
4749 << "Injection should fail because the window was removed";
4750 window->assertNoEvents();
4751 // Global monitor now gets the cancel
4752 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4753}
4754
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004755TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07004756 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004757 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4758 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004759 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004760
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004761 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004762
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004763 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004764 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004765 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00004766 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004767 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004768}
4769
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004770TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
4771 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004772
Chris Yea209fde2020-07-22 13:54:51 -07004773 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004774 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4775 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004776 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004777
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004778 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004779 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004780 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08004781 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004782 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004783
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004784 // Pilfer pointers from the monitor.
4785 // This should not do anything and the window should continue to receive events.
4786 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00004787
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004788 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004789 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4790 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004791 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004792
4793 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4794 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004795}
4796
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004797TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07004798 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004799 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4800 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004801 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4802 window->setWindowOffset(20, 40);
4803 window->setWindowTransform(0, 1, -1, 0);
4804
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004805 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004806
4807 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4808 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4809 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4810 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4811 MotionEvent* event = monitor.consumeMotion();
4812 // Even though window has transform, gesture monitor must not.
4813 ASSERT_EQ(ui::Transform(), event->getTransform());
4814}
4815
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004816TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00004817 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004818 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00004819
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004820 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00004821 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004822 << "Injection should fail if there is a monitor, but no touchable window";
4823 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00004824}
4825
chaviw81e2bb92019-12-18 15:03:51 -08004826TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004827 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004828 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4829 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08004830
Arthur Hung72d8dc32020-03-28 00:48:39 +00004831 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08004832
4833 NotifyMotionArgs motionArgs =
4834 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4835 ADISPLAY_ID_DEFAULT);
4836
Prabir Pradhan678438e2023-04-13 19:32:51 +00004837 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08004838 // Window should receive motion down event.
4839 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4840
4841 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08004842 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08004843 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4844 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4845 motionArgs.pointerCoords[0].getX() - 10);
4846
Prabir Pradhan678438e2023-04-13 19:32:51 +00004847 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004848 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004849 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08004850}
4851
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004852/**
4853 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
4854 * the device default right away. In the test scenario, we check both the default value,
4855 * and the action of enabling / disabling.
4856 */
4857TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07004858 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004859 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4860 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08004861 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004862
4863 // Set focused application.
4864 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004865 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004866
4867 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00004868 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004869 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004870 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004871
4872 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004873 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004874 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00004875 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004876
4877 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004878 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00004879 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004880 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07004881 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004882 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004883 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004884 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004885
4886 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004887 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00004889 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004890
4891 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004892 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00004893 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004894 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07004895 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004896 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004897 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004898 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004899
4900 window->assertNoEvents();
4901}
4902
Gang Wange9087892020-01-07 12:17:14 -05004903TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004904 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004905 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4906 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05004907
4908 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004909 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05004910
Arthur Hung72d8dc32020-03-28 00:48:39 +00004911 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004912 setFocusedWindow(window);
4913
Harry Cutts33476232023-01-30 19:57:29 +00004914 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05004915
Prabir Pradhan678438e2023-04-13 19:32:51 +00004916 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4917 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05004918
4919 InputEvent* event = window->consume();
4920 ASSERT_NE(event, nullptr);
4921
4922 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
4923 ASSERT_NE(verified, nullptr);
4924 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
4925
4926 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
4927 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
4928 ASSERT_EQ(keyArgs.source, verified->source);
4929 ASSERT_EQ(keyArgs.displayId, verified->displayId);
4930
4931 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
4932
4933 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05004934 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004935 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05004936 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
4937 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
4938 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
4939 ASSERT_EQ(0, verifiedKey.repeatCount);
4940}
4941
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004942TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004943 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004944 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4945 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004946
4947 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4948
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07004949 ui::Transform transform;
4950 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
4951
4952 gui::DisplayInfo displayInfo;
4953 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
4954 displayInfo.transform = transform;
4955
4956 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004957
Prabir Pradhan678438e2023-04-13 19:32:51 +00004958 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004959 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4960 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00004961 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004962
4963 InputEvent* event = window->consume();
4964 ASSERT_NE(event, nullptr);
4965
4966 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
4967 ASSERT_NE(verified, nullptr);
4968 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
4969
4970 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
4971 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
4972 EXPECT_EQ(motionArgs.source, verified->source);
4973 EXPECT_EQ(motionArgs.displayId, verified->displayId);
4974
4975 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
4976
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07004977 const vec2 rawXY =
4978 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
4979 motionArgs.pointerCoords[0].getXYValue());
4980 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
4981 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004982 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004983 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004984 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004985 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
4986 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
4987}
4988
chaviw09c8d2d2020-08-24 15:48:26 -07004989/**
4990 * Ensure that separate calls to sign the same data are generating the same key.
4991 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
4992 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
4993 * tests.
4994 */
4995TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
4996 KeyEvent event = getTestKeyEvent();
4997 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
4998
4999 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5000 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5001 ASSERT_EQ(hmac1, hmac2);
5002}
5003
5004/**
5005 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5006 */
5007TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5008 KeyEvent event = getTestKeyEvent();
5009 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5010 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5011
5012 verifiedEvent.deviceId += 1;
5013 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5014
5015 verifiedEvent.source += 1;
5016 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5017
5018 verifiedEvent.eventTimeNanos += 1;
5019 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5020
5021 verifiedEvent.displayId += 1;
5022 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5023
5024 verifiedEvent.action += 1;
5025 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5026
5027 verifiedEvent.downTimeNanos += 1;
5028 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5029
5030 verifiedEvent.flags += 1;
5031 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5032
5033 verifiedEvent.keyCode += 1;
5034 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5035
5036 verifiedEvent.scanCode += 1;
5037 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5038
5039 verifiedEvent.metaState += 1;
5040 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5041
5042 verifiedEvent.repeatCount += 1;
5043 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5044}
5045
Vishnu Nair958da932020-08-21 17:12:37 -07005046TEST_F(InputDispatcherTest, SetFocusedWindow) {
5047 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5048 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005049 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005050 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005051 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005052 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5053
5054 // Top window is also focusable but is not granted focus.
5055 windowTop->setFocusable(true);
5056 windowSecond->setFocusable(true);
5057 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5058 setFocusedWindow(windowSecond);
5059
5060 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005061 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5062 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005063
5064 // Focused window should receive event.
5065 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5066 windowTop->assertNoEvents();
5067}
5068
5069TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5070 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5071 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005072 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005073 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5074
5075 window->setFocusable(true);
5076 // Release channel for window is no longer valid.
5077 window->releaseChannel();
5078 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5079 setFocusedWindow(window);
5080
5081 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005082 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5083 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005084
5085 // window channel is invalid, so it should not receive any input event.
5086 window->assertNoEvents();
5087}
5088
5089TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5090 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5091 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005092 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005093 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005094 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5095
Vishnu Nair958da932020-08-21 17:12:37 -07005096 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5097 setFocusedWindow(window);
5098
5099 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005100 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5101 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005102
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005103 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005104 window->assertNoEvents();
5105}
5106
5107TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5108 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5109 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005110 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005111 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005112 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005113 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5114
5115 windowTop->setFocusable(true);
5116 windowSecond->setFocusable(true);
5117 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5118 setFocusedWindow(windowTop);
5119 windowTop->consumeFocusEvent(true);
5120
Chavi Weingarten847e8512023-03-29 00:26:09 +00005121 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5122 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005123 windowSecond->consumeFocusEvent(true);
5124 windowTop->consumeFocusEvent(false);
5125
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005126 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5127 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005128
5129 // Focused window should receive event.
5130 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5131}
5132
Chavi Weingarten847e8512023-03-29 00:26:09 +00005133TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005134 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5135 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005136 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005137 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005138 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005139 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5140
5141 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005142 windowSecond->setFocusable(false);
5143 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005144 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005145 setFocusedWindow(windowTop);
5146 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005147
Chavi Weingarten847e8512023-03-29 00:26:09 +00005148 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5149 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005150
5151 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005152 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005153 windowSecond->assertNoEvents();
5154}
5155
5156TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5157 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5158 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005159 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005160 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005161 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5162 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005163 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5164
5165 window->setFocusable(true);
5166 previousFocusedWindow->setFocusable(true);
5167 window->setVisible(false);
5168 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5169 setFocusedWindow(previousFocusedWindow);
5170 previousFocusedWindow->consumeFocusEvent(true);
5171
5172 // Requesting focus on invisible window takes focus from currently focused window.
5173 setFocusedWindow(window);
5174 previousFocusedWindow->consumeFocusEvent(false);
5175
5176 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005178 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5179 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005180
5181 // Window does not get focus event or key down.
5182 window->assertNoEvents();
5183
5184 // Window becomes visible.
5185 window->setVisible(true);
5186 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5187
5188 // Window receives focus event.
5189 window->consumeFocusEvent(true);
5190 // Focused window receives key down.
5191 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5192}
5193
Vishnu Nair599f1412021-06-21 10:39:58 -07005194TEST_F(InputDispatcherTest, DisplayRemoved) {
5195 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5196 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005197 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005198 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5199
5200 // window is granted focus.
5201 window->setFocusable(true);
5202 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5203 setFocusedWindow(window);
5204 window->consumeFocusEvent(true);
5205
5206 // When a display is removed window loses focus.
5207 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5208 window->consumeFocusEvent(false);
5209}
5210
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005211/**
5212 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5213 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5214 * of the 'slipperyEnterWindow'.
5215 *
5216 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5217 * a way so that the touched location is no longer covered by the top window.
5218 *
5219 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5220 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5221 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5222 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5223 * with ACTION_DOWN).
5224 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5225 * window moved itself away from the touched location and had Flag::SLIPPERY.
5226 *
5227 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5228 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5229 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5230 *
5231 * In this test, we ensure that the event received by the bottom window has
5232 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5233 */
5234TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005235 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5236 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005237
5238 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5239 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5240
5241 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005242 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005243 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005244 // Make sure this one overlaps the bottom window
5245 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5246 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5247 // one. Windows with the same owner are not considered to be occluding each other.
5248 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5249
5250 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005251 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005252 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5253
5254 mDispatcher->setInputWindows(
5255 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5256
5257 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005258 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5259 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5260 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005261 slipperyExitWindow->consumeMotionDown();
5262 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5263 mDispatcher->setInputWindows(
5264 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5265
Prabir Pradhan678438e2023-04-13 19:32:51 +00005266 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5267 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5268 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005269
5270 slipperyExitWindow->consumeMotionCancel();
5271
5272 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5273 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5274}
5275
Garfield Tan1c7bc862020-01-28 13:24:04 -08005276class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5277protected:
5278 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5279 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5280
Chris Yea209fde2020-07-22 13:54:51 -07005281 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005282 sp<FakeWindowHandle> mWindow;
5283
5284 virtual void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005285 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005286 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005287 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005288 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005289 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5290 ASSERT_EQ(OK, mDispatcher->start());
5291
5292 setUpWindow();
5293 }
5294
5295 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005296 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005297 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005298
Vishnu Nair47074b82020-08-14 11:54:47 -07005299 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005300 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005301 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005302 mWindow->consumeFocusEvent(true);
5303 }
5304
Chris Ye2ad95392020-09-01 13:44:44 -07005305 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005306 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005307 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005308 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005309 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005310
5311 // Window should receive key down event.
5312 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5313 }
5314
5315 void expectKeyRepeatOnce(int32_t repeatCount) {
5316 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5317 InputEvent* repeatEvent = mWindow->consume();
5318 ASSERT_NE(nullptr, repeatEvent);
5319
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005320 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005321
5322 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5323 uint32_t eventAction = repeatKeyEvent->getAction();
5324 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5325 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5326 }
5327
Chris Ye2ad95392020-09-01 13:44:44 -07005328 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005329 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005330 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005331 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005332 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005333
5334 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005335 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005336 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005337 }
5338};
5339
5340TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005341 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005342 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5343 expectKeyRepeatOnce(repeatCount);
5344 }
5345}
5346
5347TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005348 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005349 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5350 expectKeyRepeatOnce(repeatCount);
5351 }
Harry Cutts33476232023-01-30 19:57:29 +00005352 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005353 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005354 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5355 expectKeyRepeatOnce(repeatCount);
5356 }
5357}
5358
5359TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005360 sendAndConsumeKeyDown(/*deviceId=*/1);
5361 expectKeyRepeatOnce(/*repeatCount=*/1);
5362 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005363 mWindow->assertNoEvents();
5364}
5365
5366TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005367 sendAndConsumeKeyDown(/*deviceId=*/1);
5368 expectKeyRepeatOnce(/*repeatCount=*/1);
5369 sendAndConsumeKeyDown(/*deviceId=*/2);
5370 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005371 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005372 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005373 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005374 expectKeyRepeatOnce(/*repeatCount=*/2);
5375 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005376 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005377 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005378 mWindow->assertNoEvents();
5379}
5380
5381TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005382 sendAndConsumeKeyDown(/*deviceId=*/1);
5383 expectKeyRepeatOnce(/*repeatCount=*/1);
5384 sendAndConsumeKeyDown(/*deviceId=*/2);
5385 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005386 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005387 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005388 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005389 mWindow->assertNoEvents();
5390}
5391
liushenxiang42232912021-05-21 20:24:09 +08005392TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5393 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005394 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005395 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005396 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5397 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5398 mWindow->assertNoEvents();
5399}
5400
Garfield Tan1c7bc862020-01-28 13:24:04 -08005401TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005402 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005403 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005404 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5405 InputEvent* repeatEvent = mWindow->consume();
5406 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5407 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5408 IdGenerator::getSource(repeatEvent->getId()));
5409 }
5410}
5411
5412TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005413 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005414 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005415
5416 std::unordered_set<int32_t> idSet;
5417 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5418 InputEvent* repeatEvent = mWindow->consume();
5419 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5420 int32_t id = repeatEvent->getId();
5421 EXPECT_EQ(idSet.end(), idSet.find(id));
5422 idSet.insert(id);
5423 }
5424}
5425
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005426/* Test InputDispatcher for MultiDisplay */
5427class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5428public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005429 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005430 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005431
Chris Yea209fde2020-07-22 13:54:51 -07005432 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005433 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005434 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005435
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005436 // Set focus window for primary display, but focused display would be second one.
5437 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005438 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005439 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005440 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005441 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005442
Chris Yea209fde2020-07-22 13:54:51 -07005443 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005444 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005445 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005446 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005447 // Set focus display to second one.
5448 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5449 // Set focus window for second display.
5450 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005451 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005452 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005453 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005454 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005455 }
5456
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005457 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005458 InputDispatcherTest::TearDown();
5459
Chris Yea209fde2020-07-22 13:54:51 -07005460 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005461 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005462 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005463 windowInSecondary.clear();
5464 }
5465
5466protected:
Chris Yea209fde2020-07-22 13:54:51 -07005467 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005468 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005469 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005470 sp<FakeWindowHandle> windowInSecondary;
5471};
5472
5473TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5474 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005475 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5476 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5477 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005478 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005479 windowInSecondary->assertNoEvents();
5480
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005481 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005482 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5483 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5484 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005485 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005486 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005487}
5488
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005489TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005490 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005491 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5492 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005493 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005494 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005495 windowInSecondary->assertNoEvents();
5496
5497 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005498 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005499 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005500 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005501 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005502
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005503 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005504 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005505
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005506 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005507 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005508 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005509
5510 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005511 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005512 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005513 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005514 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005515 windowInSecondary->assertNoEvents();
5516}
5517
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005518// Test per-display input monitors for motion event.
5519TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005520 FakeMonitorReceiver monitorInPrimary =
5521 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5522 FakeMonitorReceiver monitorInSecondary =
5523 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005524
5525 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005526 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5527 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5528 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005529 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005530 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005531 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005532 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005533
5534 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005535 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5536 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5537 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005538 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005539 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005540 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005541 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005542
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005543 // Lift up the touch from the second display
5544 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5545 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5546 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5547 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5548 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5549
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005550 // Test inject a non-pointer motion event.
5551 // If specific a display, it will dispatch to the focused window of particular display,
5552 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005553 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5554 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5555 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005556 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005557 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005558 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005559 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005560}
5561
5562// Test per-display input monitors for key event.
5563TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005564 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005565 FakeMonitorReceiver monitorInPrimary =
5566 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5567 FakeMonitorReceiver monitorInSecondary =
5568 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005569
5570 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005571 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5572 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005573 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005574 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005575 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005576 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005577}
5578
Vishnu Nair958da932020-08-21 17:12:37 -07005579TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
5580 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005581 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005582 secondWindowInPrimary->setFocusable(true);
5583 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
5584 setFocusedWindow(secondWindowInPrimary);
5585 windowInPrimary->consumeFocusEvent(false);
5586 secondWindowInPrimary->consumeFocusEvent(true);
5587
5588 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005589 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
5590 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005591 windowInPrimary->assertNoEvents();
5592 windowInSecondary->assertNoEvents();
5593 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5594}
5595
Arthur Hungdfd528e2021-12-08 13:23:04 +00005596TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
5597 FakeMonitorReceiver monitorInPrimary =
5598 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5599 FakeMonitorReceiver monitorInSecondary =
5600 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
5601
5602 // Test touch down on primary display.
5603 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5604 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5605 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5606 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5607 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5608
5609 // Test touch down on second display.
5610 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5611 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5612 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5613 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
5614 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
5615
5616 // Trigger cancel touch.
5617 mDispatcher->cancelCurrentTouch();
5618 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5619 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5620 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
5621 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
5622
5623 // Test inject a move motion event, no window/monitor should receive the event.
5624 ASSERT_EQ(InputEventInjectionResult::FAILED,
5625 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5626 ADISPLAY_ID_DEFAULT, {110, 200}))
5627 << "Inject motion event should return InputEventInjectionResult::FAILED";
5628 windowInPrimary->assertNoEvents();
5629 monitorInPrimary.assertNoEvents();
5630
5631 ASSERT_EQ(InputEventInjectionResult::FAILED,
5632 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5633 SECOND_DISPLAY_ID, {110, 200}))
5634 << "Inject motion event should return InputEventInjectionResult::FAILED";
5635 windowInSecondary->assertNoEvents();
5636 monitorInSecondary.assertNoEvents();
5637}
5638
Jackal Guof9696682018-10-05 12:23:23 +08005639class InputFilterTest : public InputDispatcherTest {
5640protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005641 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
5642 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08005643 NotifyMotionArgs motionArgs;
5644
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005645 motionArgs =
5646 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005647 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005648 motionArgs =
5649 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005650 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005651 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005652 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005653 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
5654 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08005655 } else {
5656 mFakePolicy->assertFilterInputEventWasNotCalled();
5657 }
5658 }
5659
5660 void testNotifyKey(bool expectToBeFiltered) {
5661 NotifyKeyArgs keyArgs;
5662
5663 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005664 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005665 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005666 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005667 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005668
5669 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08005670 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005671 } else {
5672 mFakePolicy->assertFilterInputEventWasNotCalled();
5673 }
5674 }
5675};
5676
5677// Test InputFilter for MotionEvent
5678TEST_F(InputFilterTest, MotionEvent_InputFilter) {
5679 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
5680 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5681 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5682
5683 // Enable InputFilter
5684 mDispatcher->setInputFilterEnabled(true);
5685 // Test touch on both primary and second display, and check if both events are filtered.
5686 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
5687 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
5688
5689 // Disable InputFilter
5690 mDispatcher->setInputFilterEnabled(false);
5691 // Test touch on both primary and second display, and check if both events aren't filtered.
5692 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5693 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5694}
5695
5696// Test InputFilter for KeyEvent
5697TEST_F(InputFilterTest, KeyEvent_InputFilter) {
5698 // Since the InputFilter is disabled by default, check if key event aren't filtered.
5699 testNotifyKey(/*expectToBeFiltered*/ false);
5700
5701 // Enable InputFilter
5702 mDispatcher->setInputFilterEnabled(true);
5703 // Send a key event, and check if it is filtered.
5704 testNotifyKey(/*expectToBeFiltered*/ true);
5705
5706 // Disable InputFilter
5707 mDispatcher->setInputFilterEnabled(false);
5708 // Send a key event, and check if it isn't filtered.
5709 testNotifyKey(/*expectToBeFiltered*/ false);
5710}
5711
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005712// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
5713// logical display coordinate space.
5714TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
5715 ui::Transform firstDisplayTransform;
5716 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5717 ui::Transform secondDisplayTransform;
5718 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
5719
5720 std::vector<gui::DisplayInfo> displayInfos(2);
5721 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
5722 displayInfos[0].transform = firstDisplayTransform;
5723 displayInfos[1].displayId = SECOND_DISPLAY_ID;
5724 displayInfos[1].transform = secondDisplayTransform;
5725
5726 mDispatcher->onWindowInfosChanged({}, displayInfos);
5727
5728 // Enable InputFilter
5729 mDispatcher->setInputFilterEnabled(true);
5730
5731 // Ensure the correct transforms are used for the displays.
5732 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
5733 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
5734}
5735
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005736class InputFilterInjectionPolicyTest : public InputDispatcherTest {
5737protected:
5738 virtual void SetUp() override {
5739 InputDispatcherTest::SetUp();
5740
5741 /**
5742 * We don't need to enable input filter to test the injected event policy, but we enabled it
5743 * here to make the tests more realistic, since this policy only matters when inputfilter is
5744 * on.
5745 */
5746 mDispatcher->setInputFilterEnabled(true);
5747
5748 std::shared_ptr<InputApplicationHandle> application =
5749 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005750 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
5751 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005752
5753 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5754 mWindow->setFocusable(true);
5755 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5756 setFocusedWindow(mWindow);
5757 mWindow->consumeFocusEvent(true);
5758 }
5759
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005760 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5761 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005762 KeyEvent event;
5763
5764 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5765 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
5766 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00005767 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005768 const int32_t additionalPolicyFlags =
5769 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
5770 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005771 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005772 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5773 policyFlags | additionalPolicyFlags));
5774
5775 InputEvent* received = mWindow->consume();
5776 ASSERT_NE(nullptr, received);
5777 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005778 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005779 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
5780 ASSERT_EQ(flags, keyEvent.getFlags());
5781 }
5782
5783 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5784 int32_t flags) {
5785 MotionEvent event;
5786 PointerProperties pointerProperties[1];
5787 PointerCoords pointerCoords[1];
5788 pointerProperties[0].clear();
5789 pointerProperties[0].id = 0;
5790 pointerCoords[0].clear();
5791 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
5792 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
5793
5794 ui::Transform identityTransform;
5795 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5796 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
5797 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
5798 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
5799 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07005800 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07005801 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005802 /*pointerCount*/ 1, pointerProperties, pointerCoords);
5803
5804 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
5805 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005806 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005807 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5808 policyFlags | additionalPolicyFlags));
5809
5810 InputEvent* received = mWindow->consume();
5811 ASSERT_NE(nullptr, received);
5812 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005813 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005814 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
5815 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005816 }
5817
5818private:
5819 sp<FakeWindowHandle> mWindow;
5820};
5821
5822TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005823 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
5824 // filter. Without it, the event will no different from a regularly injected event, and the
5825 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00005826 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
5827 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005828}
5829
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005830TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005831 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005832 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005833 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
5834}
5835
5836TEST_F(InputFilterInjectionPolicyTest,
5837 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
5838 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005839 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005840 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005841}
5842
5843TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00005844 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
5845 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005846}
5847
chaviwfd6d3512019-03-25 13:23:49 -07005848class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005849 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07005850 InputDispatcherTest::SetUp();
5851
Chris Yea209fde2020-07-22 13:54:51 -07005852 std::shared_ptr<FakeApplicationHandle> application =
5853 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005854 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005855 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005856 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07005857
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005858 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005859 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005860 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07005861
5862 // Set focused application.
5863 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005864 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07005865
5866 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005867 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005868 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005869 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07005870 }
5871
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005872 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07005873 InputDispatcherTest::TearDown();
5874
5875 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005876 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07005877 }
5878
5879protected:
5880 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005881 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005882 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07005883};
5884
5885// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5886// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
5887// the onPointerDownOutsideFocus callback.
5888TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005889 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005890 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5891 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005892 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005893 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005894
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005895 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07005896 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
5897}
5898
5899// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
5900// DOWN on the window that doesn't have focus. Ensure no window received the
5901// onPointerDownOutsideFocus callback.
5902TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005903 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005904 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005905 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005906 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005907
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005908 ASSERT_TRUE(mDispatcher->waitForIdle());
5909 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005910}
5911
5912// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
5913// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
5914TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005915 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5916 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005917 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005918 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005919
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005920 ASSERT_TRUE(mDispatcher->waitForIdle());
5921 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005922}
5923
5924// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5925// DOWN on the window that already has focus. Ensure no window received the
5926// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005927TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005928 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005929 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005930 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005931 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005932 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005933
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005934 ASSERT_TRUE(mDispatcher->waitForIdle());
5935 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005936}
5937
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005938// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
5939// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
5940TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
5941 const MotionEvent event =
5942 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
5943 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005944 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005945 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
5946 .build();
5947 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
5948 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5949 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
5950
5951 ASSERT_TRUE(mDispatcher->waitForIdle());
5952 mFakePolicy->assertOnPointerDownWasNotCalled();
5953 // Ensure that the unfocused window did not receive any FOCUS events.
5954 mUnfocusedWindow->assertNoEvents();
5955}
5956
chaviwaf87b3e2019-10-01 16:59:28 -07005957// These tests ensures we can send touch events to a single client when there are multiple input
5958// windows that point to the same client token.
5959class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
5960 virtual void SetUp() override {
5961 InputDispatcherTest::SetUp();
5962
Chris Yea209fde2020-07-22 13:54:51 -07005963 std::shared_ptr<FakeApplicationHandle> application =
5964 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005965 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
5966 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07005967 mWindow1->setFrame(Rect(0, 0, 100, 100));
5968
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005969 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
5970 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07005971 mWindow2->setFrame(Rect(100, 100, 200, 200));
5972
Arthur Hung72d8dc32020-03-28 00:48:39 +00005973 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07005974 }
5975
5976protected:
5977 sp<FakeWindowHandle> mWindow1;
5978 sp<FakeWindowHandle> mWindow2;
5979
5980 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05005981 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07005982 vec2 vals = windowInfo->transform.transform(point.x, point.y);
5983 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07005984 }
5985
5986 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
5987 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005988 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07005989 InputEvent* event = window->consume();
5990
5991 ASSERT_NE(nullptr, event) << name.c_str()
5992 << ": consumer should have returned non-NULL event.";
5993
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005994 ASSERT_EQ(InputEventType::MOTION, event->getType())
5995 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07005996
5997 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005998 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08005999 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006000
6001 for (size_t i = 0; i < points.size(); i++) {
6002 float expectedX = points[i].x;
6003 float expectedY = points[i].y;
6004
6005 EXPECT_EQ(expectedX, motionEvent.getX(i))
6006 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6007 << ", got " << motionEvent.getX(i);
6008 EXPECT_EQ(expectedY, motionEvent.getY(i))
6009 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6010 << ", got " << motionEvent.getY(i);
6011 }
6012 }
chaviw9eaa22c2020-07-01 16:21:27 -07006013
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006014 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006015 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006016 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6017 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006018
6019 // Always consume from window1 since it's the window that has the InputReceiver
6020 consumeMotionEvent(mWindow1, action, expectedPoints);
6021 }
chaviwaf87b3e2019-10-01 16:59:28 -07006022};
6023
6024TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6025 // Touch Window 1
6026 PointF touchedPoint = {10, 10};
6027 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006028 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006029
6030 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006031 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006032
6033 // Touch Window 2
6034 touchedPoint = {150, 150};
6035 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006036 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006037}
6038
chaviw9eaa22c2020-07-01 16:21:27 -07006039TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6040 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006041 mWindow2->setWindowScale(0.5f, 0.5f);
6042
6043 // Touch Window 1
6044 PointF touchedPoint = {10, 10};
6045 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006046 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006047 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006048 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006049
6050 // Touch Window 2
6051 touchedPoint = {150, 150};
6052 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006053 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6054 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006055
chaviw9eaa22c2020-07-01 16:21:27 -07006056 // Update the transform so rotation is set
6057 mWindow2->setWindowTransform(0, -1, 1, 0);
6058 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6059 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006060}
6061
chaviw9eaa22c2020-07-01 16:21:27 -07006062TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006063 mWindow2->setWindowScale(0.5f, 0.5f);
6064
6065 // Touch Window 1
6066 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6067 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006068 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006069
6070 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006071 touchedPoints.push_back(PointF{150, 150});
6072 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006073 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006074
chaviw9eaa22c2020-07-01 16:21:27 -07006075 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006076 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006077 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006078
chaviw9eaa22c2020-07-01 16:21:27 -07006079 // Update the transform so rotation is set for Window 2
6080 mWindow2->setWindowTransform(0, -1, 1, 0);
6081 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006082 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006083}
6084
chaviw9eaa22c2020-07-01 16:21:27 -07006085TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006086 mWindow2->setWindowScale(0.5f, 0.5f);
6087
6088 // Touch Window 1
6089 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6090 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006091 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006092
6093 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006094 touchedPoints.push_back(PointF{150, 150});
6095 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006096
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006097 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006098
6099 // Move both windows
6100 touchedPoints = {{20, 20}, {175, 175}};
6101 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6102 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6103
chaviw9eaa22c2020-07-01 16:21:27 -07006104 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006105
chaviw9eaa22c2020-07-01 16:21:27 -07006106 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006107 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006108 expectedPoints.pop_back();
6109
6110 // Touch Window 2
6111 mWindow2->setWindowTransform(0, -1, 1, 0);
6112 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006113 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006114
6115 // Move both windows
6116 touchedPoints = {{20, 20}, {175, 175}};
6117 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6118 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6119
6120 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006121}
6122
6123TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6124 mWindow1->setWindowScale(0.5f, 0.5f);
6125
6126 // Touch Window 1
6127 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6128 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006129 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006130
6131 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006132 touchedPoints.push_back(PointF{150, 150});
6133 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006134
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006135 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006136
6137 // Move both windows
6138 touchedPoints = {{20, 20}, {175, 175}};
6139 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6140 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6141
chaviw9eaa22c2020-07-01 16:21:27 -07006142 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006143}
6144
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006145/**
6146 * When one of the windows is slippery, the touch should not slip into the other window with the
6147 * same input channel.
6148 */
6149TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6150 mWindow1->setSlippery(true);
6151 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6152
6153 // Touch down in window 1
6154 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6155 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6156 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6157
6158 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6159 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6160 // getting generated.
6161 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6162 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6163
6164 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6165}
6166
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006167class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6168 virtual void SetUp() override {
6169 InputDispatcherTest::SetUp();
6170
Chris Yea209fde2020-07-22 13:54:51 -07006171 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006172 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006173 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6174 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006175 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006176 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006177 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006178
6179 // Set focused application.
6180 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6181
6182 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006183 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006184 mWindow->consumeFocusEvent(true);
6185 }
6186
6187 virtual void TearDown() override {
6188 InputDispatcherTest::TearDown();
6189 mWindow.clear();
6190 }
6191
6192protected:
Chris Yea209fde2020-07-22 13:54:51 -07006193 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006194 sp<FakeWindowHandle> mWindow;
6195 static constexpr PointF WINDOW_LOCATION = {20, 20};
6196
6197 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006198 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006199 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6200 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006201 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006202 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6203 WINDOW_LOCATION));
6204 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006205
6206 sp<FakeWindowHandle> addSpyWindow() {
6207 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006208 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006209 spy->setTrustedOverlay(true);
6210 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006211 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006212 spy->setDispatchingTimeout(30ms);
6213 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6214 return spy;
6215 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006216};
6217
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006218// Send a tap and respond, which should not cause an ANR.
6219TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6220 tapOnWindow();
6221 mWindow->consumeMotionDown();
6222 mWindow->consumeMotionUp();
6223 ASSERT_TRUE(mDispatcher->waitForIdle());
6224 mFakePolicy->assertNotifyAnrWasNotCalled();
6225}
6226
6227// Send a regular key and respond, which should not cause an ANR.
6228TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006229 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006230 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6231 ASSERT_TRUE(mDispatcher->waitForIdle());
6232 mFakePolicy->assertNotifyAnrWasNotCalled();
6233}
6234
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006235TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6236 mWindow->setFocusable(false);
6237 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6238 mWindow->consumeFocusEvent(false);
6239
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006240 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006241 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6242 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6243 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006244 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006245 // Key will not go to window because we have no focused window.
6246 // The 'no focused window' ANR timer should start instead.
6247
6248 // Now, the focused application goes away.
6249 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6250 // The key should get dropped and there should be no ANR.
6251
6252 ASSERT_TRUE(mDispatcher->waitForIdle());
6253 mFakePolicy->assertNotifyAnrWasNotCalled();
6254}
6255
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006256// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006257// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6258// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006259TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006260 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006261 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6262 WINDOW_LOCATION));
6263
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006264 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6265 ASSERT_TRUE(sequenceNum);
6266 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006267 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006268
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006269 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006270 mWindow->consumeMotionEvent(
6271 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006272 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006273 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006274}
6275
6276// Send a key to the app and have the app not respond right away.
6277TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6278 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006279 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006280 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6281 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006282 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006283 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006284 ASSERT_TRUE(mDispatcher->waitForIdle());
6285}
6286
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006287// We have a focused application, but no focused window
6288TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006289 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006290 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6291 mWindow->consumeFocusEvent(false);
6292
6293 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006294 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006295 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6296 WINDOW_LOCATION));
6297 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6298 mDispatcher->waitForIdle();
6299 mFakePolicy->assertNotifyAnrWasNotCalled();
6300
6301 // Once a focused event arrives, we get an ANR for this application
6302 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6303 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006304 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006305 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6306 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006307 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006308 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006309 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006310 ASSERT_TRUE(mDispatcher->waitForIdle());
6311}
6312
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006313/**
6314 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6315 * there will not be an ANR.
6316 */
6317TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6318 mWindow->setFocusable(false);
6319 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6320 mWindow->consumeFocusEvent(false);
6321
6322 KeyEvent event;
6323 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6324 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6325
6326 // Define a valid key down event that is stale (too old).
6327 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6328 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006329 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006330
6331 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6332
6333 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006334 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006335 InputEventInjectionSync::WAIT_FOR_RESULT,
6336 INJECT_EVENT_TIMEOUT, policyFlags);
6337 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6338 << "Injection should fail because the event is stale";
6339
6340 ASSERT_TRUE(mDispatcher->waitForIdle());
6341 mFakePolicy->assertNotifyAnrWasNotCalled();
6342 mWindow->assertNoEvents();
6343}
6344
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006345// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006346// Make sure that we don't notify policy twice about the same ANR.
6347TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006348 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006349 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6350 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006351
6352 // Once a focused event arrives, we get an ANR for this application
6353 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6354 // injection times out (instead of failing).
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,
6357 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006358 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006359 const std::chrono::duration appTimeout =
6360 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6361 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006362
Vishnu Naire4df8752022-09-08 09:17:55 -07006363 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006364 // ANR should not be raised again. It is up to policy to do that if it desires.
6365 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006366
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006367 // If we now get a focused window, the ANR should stop, but the policy handles that via
6368 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006369 ASSERT_TRUE(mDispatcher->waitForIdle());
6370}
6371
6372// We have a focused application, but no focused window
6373TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006374 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006375 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6376 mWindow->consumeFocusEvent(false);
6377
6378 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006379 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006380 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006381 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6382 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006383
Vishnu Naire4df8752022-09-08 09:17:55 -07006384 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6385 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006386
6387 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006388 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006389 ASSERT_TRUE(mDispatcher->waitForIdle());
6390 mWindow->assertNoEvents();
6391}
6392
6393/**
6394 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6395 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6396 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6397 * the ANR mechanism should still work.
6398 *
6399 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6400 * DOWN event, while not responding on the second one.
6401 */
6402TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6403 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6404 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6405 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6406 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6407 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006408 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006409
6410 // Now send ACTION_UP, with identical timestamp
6411 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6412 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6413 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6414 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006415 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006416
6417 // We have now sent down and up. Let's consume first event and then ANR on the second.
6418 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6419 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006420 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006421}
6422
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006423// A spy window can receive an ANR
6424TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6425 sp<FakeWindowHandle> spy = addSpyWindow();
6426
6427 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6428 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6429 WINDOW_LOCATION));
6430 mWindow->consumeMotionDown();
6431
6432 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6433 ASSERT_TRUE(sequenceNum);
6434 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006435 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006436
6437 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006438 spy->consumeMotionEvent(
6439 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006440 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006441 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006442}
6443
6444// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006445// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006446TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6447 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006448
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006449 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6450 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006451 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006452 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006453
6454 // Stuck on the ACTION_UP
6455 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006456 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006457
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006458 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006459 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006460 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6461 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006462
6463 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6464 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006465 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006466 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006467 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006468}
6469
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006470// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006471// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006472TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6473 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006474
6475 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006476 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6477 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006478
6479 mWindow->consumeMotionDown();
6480 // Stuck on the ACTION_UP
6481 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006482 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006483
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006484 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006485 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006486 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6487 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006488
6489 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6490 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006491 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006492 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006493 spy->assertNoEvents();
6494}
6495
6496TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6497 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6498
6499 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6500
6501 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6502 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6503 WINDOW_LOCATION));
6504
6505 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6506 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6507 ASSERT_TRUE(consumeSeq);
6508
Prabir Pradhanedd96402022-02-15 01:46:16 -08006509 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006510
6511 monitor.finishEvent(*consumeSeq);
6512 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6513
6514 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006515 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006516}
6517
6518// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6519// process events, you don't get an anr. When the window later becomes unresponsive again, you
6520// get an ANR again.
6521// 1. tap -> block on ACTION_UP -> receive ANR
6522// 2. consume all pending events (= queue becomes healthy again)
6523// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6524TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6525 tapOnWindow();
6526
6527 mWindow->consumeMotionDown();
6528 // Block on ACTION_UP
6529 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006530 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006531 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6532 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006533 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006534 mWindow->assertNoEvents();
6535
6536 tapOnWindow();
6537 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006538 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006539 mWindow->consumeMotionUp();
6540
6541 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006542 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006543 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006544 mWindow->assertNoEvents();
6545}
6546
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006547// If a connection remains unresponsive for a while, make sure policy is only notified once about
6548// it.
6549TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006550 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006551 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6552 WINDOW_LOCATION));
6553
6554 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006555 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006556 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006557 // 'notifyConnectionUnresponsive' should only be called once per connection
6558 mFakePolicy->assertNotifyAnrWasNotCalled();
6559 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006560 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006561 mWindow->consumeMotionEvent(
6562 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006563 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006564 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006565 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006566 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006567}
6568
6569/**
6570 * If a window is processing a motion event, and then a key event comes in, the key event should
6571 * not to to the focused window until the motion is processed.
6572 *
6573 * Warning!!!
6574 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6575 * and the injection timeout that we specify when injecting the key.
6576 * We must have the injection timeout (10ms) be smaller than
6577 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6578 *
6579 * If that value changes, this test should also change.
6580 */
6581TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
6582 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6583 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6584
6585 tapOnWindow();
6586 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6587 ASSERT_TRUE(downSequenceNum);
6588 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6589 ASSERT_TRUE(upSequenceNum);
6590 // Don't finish the events yet, and send a key
6591 // Injection will "succeed" because we will eventually give up and send the key to the focused
6592 // window even if motions are still being processed. But because the injection timeout is short,
6593 // we will receive INJECTION_TIMED_OUT as the result.
6594
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006595 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006596 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006597 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6598 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006599 // Key will not be sent to the window, yet, because the window is still processing events
6600 // and the key remains pending, waiting for the touch events to be processed
6601 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6602 ASSERT_FALSE(keySequenceNum);
6603
6604 std::this_thread::sleep_for(500ms);
6605 // if we wait long enough though, dispatcher will give up, and still send the key
6606 // to the focused window, even though we have not yet finished the motion event
6607 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6608 mWindow->finishEvent(*downSequenceNum);
6609 mWindow->finishEvent(*upSequenceNum);
6610}
6611
6612/**
6613 * If a window is processing a motion event, and then a key event comes in, the key event should
6614 * not go to the focused window until the motion is processed.
6615 * If then a new motion comes in, then the pending key event should be going to the currently
6616 * focused window right away.
6617 */
6618TEST_F(InputDispatcherSingleWindowAnr,
6619 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
6620 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6621 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6622
6623 tapOnWindow();
6624 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6625 ASSERT_TRUE(downSequenceNum);
6626 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6627 ASSERT_TRUE(upSequenceNum);
6628 // Don't finish the events yet, and send a key
6629 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006630 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006631 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6632 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006633 // At this point, key is still pending, and should not be sent to the application yet.
6634 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6635 ASSERT_FALSE(keySequenceNum);
6636
6637 // Now tap down again. It should cause the pending key to go to the focused window right away.
6638 tapOnWindow();
6639 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
6640 // the other events yet. We can finish events in any order.
6641 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
6642 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
6643 mWindow->consumeMotionDown();
6644 mWindow->consumeMotionUp();
6645 mWindow->assertNoEvents();
6646}
6647
6648class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
6649 virtual void SetUp() override {
6650 InputDispatcherTest::SetUp();
6651
Chris Yea209fde2020-07-22 13:54:51 -07006652 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006653 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006654 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
6655 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006656 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006657 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006658 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006659
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006660 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
6661 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006662 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006663 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006664
6665 // Set focused application.
6666 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07006667 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006668
6669 // Expect one focus window exist in display.
6670 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006671 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006672 mFocusedWindow->consumeFocusEvent(true);
6673 }
6674
6675 virtual void TearDown() override {
6676 InputDispatcherTest::TearDown();
6677
6678 mUnfocusedWindow.clear();
6679 mFocusedWindow.clear();
6680 }
6681
6682protected:
Chris Yea209fde2020-07-22 13:54:51 -07006683 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006684 sp<FakeWindowHandle> mUnfocusedWindow;
6685 sp<FakeWindowHandle> mFocusedWindow;
6686 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
6687 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
6688 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
6689
6690 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
6691
6692 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
6693
6694private:
6695 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006696 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006697 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6698 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006699 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006700 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6701 location));
6702 }
6703};
6704
6705// If we have 2 windows that are both unresponsive, the one with the shortest timeout
6706// should be ANR'd first.
6707TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006708 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006709 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6710 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006711 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006712 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006713 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006714 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006715 // We consumed all events, so no ANR
6716 ASSERT_TRUE(mDispatcher->waitForIdle());
6717 mFakePolicy->assertNotifyAnrWasNotCalled();
6718
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006719 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006720 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6721 FOCUSED_WINDOW_LOCATION));
6722 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
6723 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006724
6725 const std::chrono::duration timeout =
6726 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006727 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006728 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
6729 // sequence to make it consistent
6730 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006731 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006732 mFocusedWindow->consumeMotionDown();
6733 // This cancel is generated because the connection was unresponsive
6734 mFocusedWindow->consumeMotionCancel();
6735 mFocusedWindow->assertNoEvents();
6736 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006737 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006738 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6739 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006740 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006741}
6742
6743// If we have 2 windows with identical timeouts that are both unresponsive,
6744// it doesn't matter which order they should have ANR.
6745// But we should receive ANR for both.
6746TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
6747 // Set the timeout for unfocused window to match the focused window
6748 mUnfocusedWindow->setDispatchingTimeout(10ms);
6749 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6750
6751 tapOnFocusedWindow();
6752 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08006753 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
6754 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
6755 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006756
6757 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006758 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
6759 mFocusedWindow->getToken() == anrConnectionToken2);
6760 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
6761 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006762
6763 ASSERT_TRUE(mDispatcher->waitForIdle());
6764 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006765
6766 mFocusedWindow->consumeMotionDown();
6767 mFocusedWindow->consumeMotionUp();
6768 mUnfocusedWindow->consumeMotionOutside();
6769
Prabir Pradhanedd96402022-02-15 01:46:16 -08006770 sp<IBinder> responsiveToken1, responsiveToken2;
6771 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
6772 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006773
6774 // Both applications should be marked as responsive, in any order
6775 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
6776 mFocusedWindow->getToken() == responsiveToken2);
6777 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
6778 mUnfocusedWindow->getToken() == responsiveToken2);
6779 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006780}
6781
6782// If a window is already not responding, the second tap on the same window should be ignored.
6783// We should also log an error to account for the dropped event (not tested here).
6784// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
6785TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
6786 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006787 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006788 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006789 // Receive the events, but don't respond
6790 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
6791 ASSERT_TRUE(downEventSequenceNum);
6792 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
6793 ASSERT_TRUE(upEventSequenceNum);
6794 const std::chrono::duration timeout =
6795 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006796 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006797
6798 // Tap once again
6799 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
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 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006803 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006804 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6805 FOCUSED_WINDOW_LOCATION));
6806 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
6807 // valid touch target
6808 mUnfocusedWindow->assertNoEvents();
6809
6810 // Consume the first tap
6811 mFocusedWindow->finishEvent(*downEventSequenceNum);
6812 mFocusedWindow->finishEvent(*upEventSequenceNum);
6813 ASSERT_TRUE(mDispatcher->waitForIdle());
6814 // The second tap did not go to the focused window
6815 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006816 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08006817 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6818 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006819 mFakePolicy->assertNotifyAnrWasNotCalled();
6820}
6821
6822// If you tap outside of all windows, there will not be ANR
6823TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006824 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006825 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6826 LOCATION_OUTSIDE_ALL_WINDOWS));
6827 ASSERT_TRUE(mDispatcher->waitForIdle());
6828 mFakePolicy->assertNotifyAnrWasNotCalled();
6829}
6830
6831// Since the focused window is paused, tapping on it should not produce any events
6832TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
6833 mFocusedWindow->setPaused(true);
6834 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6835
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006836 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006837 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6838 FOCUSED_WINDOW_LOCATION));
6839
6840 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
6841 ASSERT_TRUE(mDispatcher->waitForIdle());
6842 // Should not ANR because the window is paused, and touches shouldn't go to it
6843 mFakePolicy->assertNotifyAnrWasNotCalled();
6844
6845 mFocusedWindow->assertNoEvents();
6846 mUnfocusedWindow->assertNoEvents();
6847}
6848
6849/**
6850 * If a window is processing a motion event, and then a key event comes in, the key event should
6851 * not to to the focused window until the motion is processed.
6852 * If a different window becomes focused at this time, the key should go to that window instead.
6853 *
6854 * Warning!!!
6855 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6856 * and the injection timeout that we specify when injecting the key.
6857 * We must have the injection timeout (10ms) be smaller than
6858 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6859 *
6860 * If that value changes, this test should also change.
6861 */
6862TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
6863 // Set a long ANR timeout to prevent it from triggering
6864 mFocusedWindow->setDispatchingTimeout(2s);
6865 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6866
6867 tapOnUnfocusedWindow();
6868 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
6869 ASSERT_TRUE(downSequenceNum);
6870 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
6871 ASSERT_TRUE(upSequenceNum);
6872 // Don't finish the events yet, and send a key
6873 // Injection will succeed because we will eventually give up and send the key to the focused
6874 // window even if motions are still being processed.
6875
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006876 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006877 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6878 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006879 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006880 // Key will not be sent to the window, yet, because the window is still processing events
6881 // and the key remains pending, waiting for the touch events to be processed
6882 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
6883 ASSERT_FALSE(keySequenceNum);
6884
6885 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07006886 mFocusedWindow->setFocusable(false);
6887 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006889 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006890
6891 // Focus events should precede the key events
6892 mUnfocusedWindow->consumeFocusEvent(true);
6893 mFocusedWindow->consumeFocusEvent(false);
6894
6895 // Finish the tap events, which should unblock dispatcher
6896 mUnfocusedWindow->finishEvent(*downSequenceNum);
6897 mUnfocusedWindow->finishEvent(*upSequenceNum);
6898
6899 // Now that all queues are cleared and no backlog in the connections, the key event
6900 // can finally go to the newly focused "mUnfocusedWindow".
6901 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6902 mFocusedWindow->assertNoEvents();
6903 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006904 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006905}
6906
6907// When the touch stream is split across 2 windows, and one of them does not respond,
6908// then ANR should be raised and the touch should be canceled for the unresponsive window.
6909// The other window should not be affected by that.
6910TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
6911 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00006912 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6913 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6914 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006915 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006916 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006917
6918 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00006919 mDispatcher->notifyMotion(
6920 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6921 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006922
6923 const std::chrono::duration timeout =
6924 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006925 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006926
6927 mUnfocusedWindow->consumeMotionDown();
6928 mFocusedWindow->consumeMotionDown();
6929 // Focused window may or may not receive ACTION_MOVE
6930 // But it should definitely receive ACTION_CANCEL due to the ANR
6931 InputEvent* event;
6932 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
6933 ASSERT_TRUE(moveOrCancelSequenceNum);
6934 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
6935 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006936 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006937 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
6938 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
6939 mFocusedWindow->consumeMotionCancel();
6940 } else {
6941 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
6942 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006943 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006944 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6945 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006946
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006947 mUnfocusedWindow->assertNoEvents();
6948 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006949 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006950}
6951
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006952/**
6953 * If we have no focused window, and a key comes in, we start the ANR timer.
6954 * The focused application should add a focused window before the timer runs out to prevent ANR.
6955 *
6956 * If the user touches another application during this time, the key should be dropped.
6957 * Next, if a new focused window comes in, without toggling the focused application,
6958 * then no ANR should occur.
6959 *
6960 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
6961 * but in some cases the policy may not update the focused application.
6962 */
6963TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
6964 std::shared_ptr<FakeApplicationHandle> focusedApplication =
6965 std::make_shared<FakeApplicationHandle>();
6966 focusedApplication->setDispatchingTimeout(60ms);
6967 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
6968 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
6969 mFocusedWindow->setFocusable(false);
6970
6971 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6972 mFocusedWindow->consumeFocusEvent(false);
6973
6974 // Send a key. The ANR timer should start because there is no focused window.
6975 // 'focusedApplication' will get blamed if this timer completes.
6976 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006977 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006978 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6979 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6980 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006981 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006982
6983 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
6984 // then the injected touches won't cause the focused event to get dropped.
6985 // The dispatcher only checks for whether the queue should be pruned upon queueing.
6986 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
6987 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
6988 // For this test, it means that the key would get delivered to the window once it becomes
6989 // focused.
6990 std::this_thread::sleep_for(10ms);
6991
6992 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00006993 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6994 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6995 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006996
6997 // We do not consume the motion right away, because that would require dispatcher to first
6998 // process (== drop) the key event, and by that time, ANR will be raised.
6999 // Set the focused window first.
7000 mFocusedWindow->setFocusable(true);
7001 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7002 setFocusedWindow(mFocusedWindow);
7003 mFocusedWindow->consumeFocusEvent(true);
7004 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7005 // to another application. This could be a bug / behaviour in the policy.
7006
7007 mUnfocusedWindow->consumeMotionDown();
7008
7009 ASSERT_TRUE(mDispatcher->waitForIdle());
7010 // Should not ANR because we actually have a focused window. It was just added too slowly.
7011 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7012}
7013
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007014// These tests ensure we cannot send touch events to a window that's positioned behind a window
7015// that has feature NO_INPUT_CHANNEL.
7016// Layout:
7017// Top (closest to user)
7018// mNoInputWindow (above all windows)
7019// mBottomWindow
7020// Bottom (furthest from user)
7021class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7022 virtual void SetUp() override {
7023 InputDispatcherTest::SetUp();
7024
7025 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007026 mNoInputWindow =
7027 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7028 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007029 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007030 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007031 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7032 // It's perfectly valid for this window to not have an associated input channel
7033
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007034 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7035 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007036 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7037
7038 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7039 }
7040
7041protected:
7042 std::shared_ptr<FakeApplicationHandle> mApplication;
7043 sp<FakeWindowHandle> mNoInputWindow;
7044 sp<FakeWindowHandle> mBottomWindow;
7045};
7046
7047TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7048 PointF touchedPoint = {10, 10};
7049
Prabir Pradhan678438e2023-04-13 19:32:51 +00007050 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7051 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7052 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007053
7054 mNoInputWindow->assertNoEvents();
7055 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7056 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7057 // and therefore should prevent mBottomWindow from receiving touches
7058 mBottomWindow->assertNoEvents();
7059}
7060
7061/**
7062 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7063 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7064 */
7065TEST_F(InputDispatcherMultiWindowOcclusionTests,
7066 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007067 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7068 "Window with input channel and NO_INPUT_CHANNEL",
7069 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007070
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007071 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007072 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7073 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7074
7075 PointF touchedPoint = {10, 10};
7076
Prabir Pradhan678438e2023-04-13 19:32:51 +00007077 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7078 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7079 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007080
7081 mNoInputWindow->assertNoEvents();
7082 mBottomWindow->assertNoEvents();
7083}
7084
Vishnu Nair958da932020-08-21 17:12:37 -07007085class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7086protected:
7087 std::shared_ptr<FakeApplicationHandle> mApp;
7088 sp<FakeWindowHandle> mWindow;
7089 sp<FakeWindowHandle> mMirror;
7090
7091 virtual void SetUp() override {
7092 InputDispatcherTest::SetUp();
7093 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007094 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7095 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7096 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007097 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7098 mWindow->setFocusable(true);
7099 mMirror->setFocusable(true);
7100 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7101 }
7102};
7103
7104TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7105 // Request focus on a mirrored window
7106 setFocusedWindow(mMirror);
7107
7108 // window gets focused
7109 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007110 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7111 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007112 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7113}
7114
7115// A focused & mirrored window remains focused only if the window and its mirror are both
7116// focusable.
7117TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7118 setFocusedWindow(mMirror);
7119
7120 // window gets focused
7121 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007122 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7123 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007124 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007125 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7126 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007127 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7128
7129 mMirror->setFocusable(false);
7130 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7131
7132 // window loses focus since one of the windows associated with the token in not focusable
7133 mWindow->consumeFocusEvent(false);
7134
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007135 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7136 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007137 mWindow->assertNoEvents();
7138}
7139
7140// A focused & mirrored window remains focused until the window and its mirror both become
7141// invisible.
7142TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7143 setFocusedWindow(mMirror);
7144
7145 // window gets focused
7146 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007147 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7148 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007149 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007150 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7151 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007152 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7153
7154 mMirror->setVisible(false);
7155 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7156
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 mWindow->setVisible(false);
7165 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7166
7167 // window loses focus only after all windows associated with the token become invisible.
7168 mWindow->consumeFocusEvent(false);
7169
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007170 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7171 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007172 mWindow->assertNoEvents();
7173}
7174
7175// A focused & mirrored window remains focused until both windows are removed.
7176TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7177 setFocusedWindow(mMirror);
7178
7179 // window gets focused
7180 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007181 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7182 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007183 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007184 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7185 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007186 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7187
7188 // single window is removed but the window token remains focused
7189 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7190
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007191 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7192 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007193 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007194 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7195 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007196 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7197
7198 // Both windows are removed
7199 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7200 mWindow->consumeFocusEvent(false);
7201
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007202 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7203 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007204 mWindow->assertNoEvents();
7205}
7206
7207// Focus request can be pending until one window becomes visible.
7208TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7209 // Request focus on an invisible mirror.
7210 mWindow->setVisible(false);
7211 mMirror->setVisible(false);
7212 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7213 setFocusedWindow(mMirror);
7214
7215 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007216 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007217 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7218 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007219
7220 mMirror->setVisible(true);
7221 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7222
7223 // window gets focused
7224 mWindow->consumeFocusEvent(true);
7225 // window gets the pending key event
7226 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7227}
Prabir Pradhan99987712020-11-10 18:43:05 -08007228
7229class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7230protected:
7231 std::shared_ptr<FakeApplicationHandle> mApp;
7232 sp<FakeWindowHandle> mWindow;
7233 sp<FakeWindowHandle> mSecondWindow;
7234
7235 void SetUp() override {
7236 InputDispatcherTest::SetUp();
7237 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007238 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007239 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007240 mSecondWindow =
7241 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007242 mSecondWindow->setFocusable(true);
7243
7244 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7245 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7246
7247 setFocusedWindow(mWindow);
7248 mWindow->consumeFocusEvent(true);
7249 }
7250
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007251 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007252 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007253 }
7254
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007255 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7256 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007257 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007258 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7259 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007260 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007261 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007262 }
7263};
7264
7265TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7266 // Ensure that capture cannot be obtained for unfocused windows.
7267 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7268 mFakePolicy->assertSetPointerCaptureNotCalled();
7269 mSecondWindow->assertNoEvents();
7270
7271 // Ensure that capture can be enabled from the focus window.
7272 requestAndVerifyPointerCapture(mWindow, true);
7273
7274 // Ensure that capture cannot be disabled from a window that does not have capture.
7275 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7276 mFakePolicy->assertSetPointerCaptureNotCalled();
7277
7278 // Ensure that capture can be disabled from the window with capture.
7279 requestAndVerifyPointerCapture(mWindow, false);
7280}
7281
7282TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007283 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007284
7285 setFocusedWindow(mSecondWindow);
7286
7287 // Ensure that the capture disabled event was sent first.
7288 mWindow->consumeCaptureEvent(false);
7289 mWindow->consumeFocusEvent(false);
7290 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007291 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007292
7293 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007294 notifyPointerCaptureChanged({});
7295 notifyPointerCaptureChanged(request);
7296 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007297 mWindow->assertNoEvents();
7298 mSecondWindow->assertNoEvents();
7299 mFakePolicy->assertSetPointerCaptureNotCalled();
7300}
7301
7302TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007303 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007304
7305 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007306 notifyPointerCaptureChanged({});
7307 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007308
7309 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007310 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007311 mWindow->consumeCaptureEvent(false);
7312 mWindow->assertNoEvents();
7313}
7314
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007315TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7316 requestAndVerifyPointerCapture(mWindow, true);
7317
7318 // The first window loses focus.
7319 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007320 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007321 mWindow->consumeCaptureEvent(false);
7322
7323 // Request Pointer Capture from the second window before the notification from InputReader
7324 // arrives.
7325 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007326 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007327
7328 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007329 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007330
7331 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007332 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007333
7334 mSecondWindow->consumeFocusEvent(true);
7335 mSecondWindow->consumeCaptureEvent(true);
7336}
7337
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007338TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7339 // App repeatedly enables and disables capture.
7340 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7341 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7342 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7343 mFakePolicy->assertSetPointerCaptureCalled(false);
7344 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7345 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7346
7347 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7348 // first request is now stale, this should do nothing.
7349 notifyPointerCaptureChanged(firstRequest);
7350 mWindow->assertNoEvents();
7351
7352 // InputReader notifies that the second request was enabled.
7353 notifyPointerCaptureChanged(secondRequest);
7354 mWindow->consumeCaptureEvent(true);
7355}
7356
Prabir Pradhan7092e262022-05-03 16:51:09 +00007357TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7358 requestAndVerifyPointerCapture(mWindow, true);
7359
7360 // App toggles pointer capture off and on.
7361 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7362 mFakePolicy->assertSetPointerCaptureCalled(false);
7363
7364 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7365 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7366
7367 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7368 // preceding "disable" request.
7369 notifyPointerCaptureChanged(enableRequest);
7370
7371 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7372 // any notifications.
7373 mWindow->assertNoEvents();
7374}
7375
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007376class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7377protected:
7378 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007379
7380 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7381 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7382
7383 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7384 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7385
7386 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7387 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7388 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7389 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7390 MAXIMUM_OBSCURING_OPACITY);
7391
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007392 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007393 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007394 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007395
7396 sp<FakeWindowHandle> mTouchWindow;
7397
7398 virtual void SetUp() override {
7399 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007400 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007401 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7402 }
7403
7404 virtual void TearDown() override {
7405 InputDispatcherTest::TearDown();
7406 mTouchWindow.clear();
7407 }
7408
chaviw3277faf2021-05-19 16:45:23 -05007409 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7410 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007411 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007412 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007413 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007414 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007415 return window;
7416 }
7417
7418 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7419 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7420 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007421 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007422 // Generate an arbitrary PID based on the UID
7423 window->setOwnerInfo(1777 + (uid % 10000), uid);
7424 return window;
7425 }
7426
7427 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007428 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7429 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7430 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007431 }
7432};
7433
7434TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007435 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007436 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007437 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007438
7439 touch();
7440
7441 mTouchWindow->assertNoEvents();
7442}
7443
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007444TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007445 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7446 const sp<FakeWindowHandle>& w =
7447 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7448 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7449
7450 touch();
7451
7452 mTouchWindow->assertNoEvents();
7453}
7454
7455TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007456 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7457 const sp<FakeWindowHandle>& w =
7458 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7459 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7460
7461 touch();
7462
7463 w->assertNoEvents();
7464}
7465
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007466TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007467 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7468 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007469
7470 touch();
7471
7472 mTouchWindow->consumeAnyMotionDown();
7473}
7474
7475TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007476 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007477 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007478 w->setFrame(Rect(0, 0, 50, 50));
7479 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007480
7481 touch({PointF{100, 100}});
7482
7483 mTouchWindow->consumeAnyMotionDown();
7484}
7485
7486TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007487 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007488 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007489 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7490
7491 touch();
7492
7493 mTouchWindow->consumeAnyMotionDown();
7494}
7495
7496TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7497 const sp<FakeWindowHandle>& w =
7498 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7499 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007500
7501 touch();
7502
7503 mTouchWindow->consumeAnyMotionDown();
7504}
7505
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007506TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7507 const sp<FakeWindowHandle>& w =
7508 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7509 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7510
7511 touch();
7512
7513 w->assertNoEvents();
7514}
7515
7516/**
7517 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7518 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7519 * window, the occluding window will still receive ACTION_OUTSIDE event.
7520 */
7521TEST_F(InputDispatcherUntrustedTouchesTest,
7522 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7523 const sp<FakeWindowHandle>& w =
7524 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007525 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007526 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7527
7528 touch();
7529
7530 w->consumeMotionOutside();
7531}
7532
7533TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
7534 const sp<FakeWindowHandle>& w =
7535 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007536 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007537 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7538
7539 touch();
7540
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007541 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007542}
7543
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007544TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007545 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007546 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7547 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007548 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7549
7550 touch();
7551
7552 mTouchWindow->consumeAnyMotionDown();
7553}
7554
7555TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
7556 const sp<FakeWindowHandle>& w =
7557 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7558 MAXIMUM_OBSCURING_OPACITY);
7559 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007560
7561 touch();
7562
7563 mTouchWindow->consumeAnyMotionDown();
7564}
7565
7566TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007567 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007568 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7569 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007570 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7571
7572 touch();
7573
7574 mTouchWindow->assertNoEvents();
7575}
7576
7577TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
7578 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
7579 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007580 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7581 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007582 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007583 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7584 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007585 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7586
7587 touch();
7588
7589 mTouchWindow->assertNoEvents();
7590}
7591
7592TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
7593 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
7594 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007595 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7596 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007597 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007598 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7599 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007600 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7601
7602 touch();
7603
7604 mTouchWindow->consumeAnyMotionDown();
7605}
7606
7607TEST_F(InputDispatcherUntrustedTouchesTest,
7608 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
7609 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007610 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7611 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007612 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007613 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7614 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007615 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7616
7617 touch();
7618
7619 mTouchWindow->consumeAnyMotionDown();
7620}
7621
7622TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
7623 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007624 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7625 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007626 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007627 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7628 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007629 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007630
7631 touch();
7632
7633 mTouchWindow->assertNoEvents();
7634}
7635
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007636TEST_F(InputDispatcherUntrustedTouchesTest,
7637 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
7638 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007639 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7640 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007641 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007642 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7643 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007644 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7645
7646 touch();
7647
7648 mTouchWindow->assertNoEvents();
7649}
7650
7651TEST_F(InputDispatcherUntrustedTouchesTest,
7652 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
7653 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007654 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7655 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007656 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007657 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7658 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007659 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7660
7661 touch();
7662
7663 mTouchWindow->consumeAnyMotionDown();
7664}
7665
7666TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
7667 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007668 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7669 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007670 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7671
7672 touch();
7673
7674 mTouchWindow->consumeAnyMotionDown();
7675}
7676
7677TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
7678 const sp<FakeWindowHandle>& w =
7679 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
7680 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7681
7682 touch();
7683
7684 mTouchWindow->consumeAnyMotionDown();
7685}
7686
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007687TEST_F(InputDispatcherUntrustedTouchesTest,
7688 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
7689 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7690 const sp<FakeWindowHandle>& w =
7691 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
7692 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7693
7694 touch();
7695
7696 mTouchWindow->assertNoEvents();
7697}
7698
7699TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
7700 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7701 const sp<FakeWindowHandle>& w =
7702 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
7703 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7704
7705 touch();
7706
7707 mTouchWindow->consumeAnyMotionDown();
7708}
7709
7710TEST_F(InputDispatcherUntrustedTouchesTest,
7711 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
7712 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
7713 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007714 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7715 OPACITY_ABOVE_THRESHOLD);
7716 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7717
7718 touch();
7719
7720 mTouchWindow->consumeAnyMotionDown();
7721}
7722
7723TEST_F(InputDispatcherUntrustedTouchesTest,
7724 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
7725 const sp<FakeWindowHandle>& w1 =
7726 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7727 OPACITY_BELOW_THRESHOLD);
7728 const sp<FakeWindowHandle>& w2 =
7729 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7730 OPACITY_BELOW_THRESHOLD);
7731 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7732
7733 touch();
7734
7735 mTouchWindow->assertNoEvents();
7736}
7737
7738/**
7739 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
7740 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
7741 * (which alone would result in allowing touches) does not affect the blocking behavior.
7742 */
7743TEST_F(InputDispatcherUntrustedTouchesTest,
7744 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
7745 const sp<FakeWindowHandle>& wB =
7746 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7747 OPACITY_BELOW_THRESHOLD);
7748 const sp<FakeWindowHandle>& wC =
7749 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7750 OPACITY_BELOW_THRESHOLD);
7751 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7752
7753 touch();
7754
7755 mTouchWindow->assertNoEvents();
7756}
7757
7758/**
7759 * This test is testing that a window from a different UID but with same application token doesn't
7760 * block the touch. Apps can share the application token for close UI collaboration for example.
7761 */
7762TEST_F(InputDispatcherUntrustedTouchesTest,
7763 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
7764 const sp<FakeWindowHandle>& w =
7765 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7766 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007767 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7768
7769 touch();
7770
7771 mTouchWindow->consumeAnyMotionDown();
7772}
7773
arthurhungb89ccb02020-12-30 16:19:01 +08007774class InputDispatcherDragTests : public InputDispatcherTest {
7775protected:
7776 std::shared_ptr<FakeApplicationHandle> mApp;
7777 sp<FakeWindowHandle> mWindow;
7778 sp<FakeWindowHandle> mSecondWindow;
7779 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007780 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007781 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
7782 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08007783
7784 void SetUp() override {
7785 InputDispatcherTest::SetUp();
7786 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007787 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007788 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007789
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007790 mSecondWindow =
7791 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007792 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007793
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007794 mSpyWindow =
7795 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007796 mSpyWindow->setSpy(true);
7797 mSpyWindow->setTrustedOverlay(true);
7798 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
7799
arthurhungb89ccb02020-12-30 16:19:01 +08007800 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007801 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007802 }
7803
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007804 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
7805 switch (fromSource) {
7806 case AINPUT_SOURCE_TOUCHSCREEN:
7807 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7808 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7809 ADISPLAY_ID_DEFAULT, {50, 50}))
7810 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7811 break;
7812 case AINPUT_SOURCE_STYLUS:
7813 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7814 injectMotionEvent(
7815 mDispatcher,
7816 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
7817 AINPUT_SOURCE_STYLUS)
7818 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007819 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007820 .x(50)
7821 .y(50))
7822 .build()));
7823 break;
7824 case AINPUT_SOURCE_MOUSE:
7825 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7826 injectMotionEvent(
7827 mDispatcher,
7828 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
7829 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7830 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007831 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007832 .x(50)
7833 .y(50))
7834 .build()));
7835 break;
7836 default:
7837 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
7838 }
arthurhungb89ccb02020-12-30 16:19:01 +08007839
7840 // Window should receive motion event.
7841 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007842 // Spy window should also receive motion event
7843 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00007844 }
7845
7846 // Start performing drag, we will create a drag window and transfer touch to it.
7847 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
7848 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007849 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00007850 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007851 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00007852 }
arthurhungb89ccb02020-12-30 16:19:01 +08007853
7854 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007855 mDragWindow =
7856 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007857 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08007858 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007859 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007860
7861 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00007862 bool transferred =
7863 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00007864 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00007865 if (transferred) {
7866 mWindow->consumeMotionCancel();
7867 mDragWindow->consumeMotionDown();
7868 }
7869 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08007870 }
7871};
7872
7873TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007874 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08007875
7876 // Move on window.
7877 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7878 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7879 ADISPLAY_ID_DEFAULT, {50, 50}))
7880 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7881 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7882 mWindow->consumeDragEvent(false, 50, 50);
7883 mSecondWindow->assertNoEvents();
7884
7885 // Move to another window.
7886 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7887 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7888 ADISPLAY_ID_DEFAULT, {150, 50}))
7889 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7890 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7891 mWindow->consumeDragEvent(true, 150, 50);
7892 mSecondWindow->consumeDragEvent(false, 50, 50);
7893
7894 // Move back to original window.
7895 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7896 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7897 ADISPLAY_ID_DEFAULT, {50, 50}))
7898 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7899 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7900 mWindow->consumeDragEvent(false, 50, 50);
7901 mSecondWindow->consumeDragEvent(true, -50, 50);
7902
7903 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7904 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
7905 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7906 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7907 mWindow->assertNoEvents();
7908 mSecondWindow->assertNoEvents();
7909}
7910
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007911TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007912 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007913
7914 // No cancel event after drag start
7915 mSpyWindow->assertNoEvents();
7916
7917 const MotionEvent secondFingerDownEvent =
7918 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7919 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007920 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
7921 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007922 .build();
7923 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7924 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7925 InputEventInjectionSync::WAIT_FOR_RESULT))
7926 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7927
7928 // Receives cancel for first pointer after next pointer down
7929 mSpyWindow->consumeMotionCancel();
7930 mSpyWindow->consumeMotionDown();
7931
7932 mSpyWindow->assertNoEvents();
7933}
7934
arthurhungf452d0b2021-01-06 00:19:52 +08007935TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007936 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08007937
7938 // Move on window.
7939 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7940 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7941 ADISPLAY_ID_DEFAULT, {50, 50}))
7942 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7943 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7944 mWindow->consumeDragEvent(false, 50, 50);
7945 mSecondWindow->assertNoEvents();
7946
7947 // Move to another window.
7948 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7949 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7950 ADISPLAY_ID_DEFAULT, {150, 50}))
7951 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7952 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7953 mWindow->consumeDragEvent(true, 150, 50);
7954 mSecondWindow->consumeDragEvent(false, 50, 50);
7955
7956 // drop to another window.
7957 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7958 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7959 {150, 50}))
7960 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7961 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7962 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7963 mWindow->assertNoEvents();
7964 mSecondWindow->assertNoEvents();
7965}
7966
arthurhung6d4bed92021-03-17 11:59:33 +08007967TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007968 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08007969
7970 // Move on window and keep button pressed.
7971 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7972 injectMotionEvent(mDispatcher,
7973 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7974 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007975 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08007976 .build()))
7977 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7978 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7979 mWindow->consumeDragEvent(false, 50, 50);
7980 mSecondWindow->assertNoEvents();
7981
7982 // Move to another window and release button, expect to drop item.
7983 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7984 injectMotionEvent(mDispatcher,
7985 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7986 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007987 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08007988 .build()))
7989 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7990 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7991 mWindow->assertNoEvents();
7992 mSecondWindow->assertNoEvents();
7993 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7994
7995 // nothing to the window.
7996 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7997 injectMotionEvent(mDispatcher,
7998 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
7999 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008000 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008001 .build()))
8002 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8003 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8004 mWindow->assertNoEvents();
8005 mSecondWindow->assertNoEvents();
8006}
8007
Arthur Hung54745652022-04-20 07:17:41 +00008008TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008009 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008010
8011 // Set second window invisible.
8012 mSecondWindow->setVisible(false);
8013 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8014
8015 // Move on window.
8016 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8017 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8018 ADISPLAY_ID_DEFAULT, {50, 50}))
8019 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8020 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8021 mWindow->consumeDragEvent(false, 50, 50);
8022 mSecondWindow->assertNoEvents();
8023
8024 // Move to another window.
8025 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8026 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8027 ADISPLAY_ID_DEFAULT, {150, 50}))
8028 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8029 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8030 mWindow->consumeDragEvent(true, 150, 50);
8031 mSecondWindow->assertNoEvents();
8032
8033 // drop to another window.
8034 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8035 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8036 {150, 50}))
8037 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8038 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8039 mFakePolicy->assertDropTargetEquals(nullptr);
8040 mWindow->assertNoEvents();
8041 mSecondWindow->assertNoEvents();
8042}
8043
Arthur Hung54745652022-04-20 07:17:41 +00008044TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008045 // Ensure window could track pointerIds if it didn't support split touch.
8046 mWindow->setPreventSplitting(true);
8047
Arthur Hung54745652022-04-20 07:17:41 +00008048 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8049 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8050 {50, 50}))
8051 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8052 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8053
8054 const MotionEvent secondFingerDownEvent =
8055 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8056 .displayId(ADISPLAY_ID_DEFAULT)
8057 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008058 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8059 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008060 .build();
8061 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8062 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8063 InputEventInjectionSync::WAIT_FOR_RESULT))
8064 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008065 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008066
8067 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008068 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008069}
8070
8071TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8072 // First down on second window.
8073 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8074 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8075 {150, 50}))
8076 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8077
8078 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8079
8080 // Second down on first window.
8081 const MotionEvent secondFingerDownEvent =
8082 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8083 .displayId(ADISPLAY_ID_DEFAULT)
8084 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008085 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8086 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008087 .build();
8088 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8089 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8090 InputEventInjectionSync::WAIT_FOR_RESULT))
8091 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8092 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8093
8094 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008095 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008096
8097 // Move on window.
8098 const MotionEvent secondFingerMoveEvent =
8099 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8100 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008101 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8102 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008103 .build();
8104 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8105 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8106 InputEventInjectionSync::WAIT_FOR_RESULT));
8107 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8108 mWindow->consumeDragEvent(false, 50, 50);
8109 mSecondWindow->consumeMotionMove();
8110
8111 // Release the drag pointer should perform drop.
8112 const MotionEvent secondFingerUpEvent =
8113 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8114 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008115 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8116 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008117 .build();
8118 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8119 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8120 InputEventInjectionSync::WAIT_FOR_RESULT));
8121 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8122 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8123 mWindow->assertNoEvents();
8124 mSecondWindow->consumeMotionMove();
8125}
8126
Arthur Hung3915c1f2022-05-31 07:17:17 +00008127TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008128 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008129
8130 // Update window of second display.
8131 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008132 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008133 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8134
8135 // Let second display has a touch state.
8136 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8137 injectMotionEvent(mDispatcher,
8138 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8139 AINPUT_SOURCE_TOUCHSCREEN)
8140 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008141 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008142 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008143 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008144 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008145 // Update window again.
8146 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8147
8148 // Move on window.
8149 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8150 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8151 ADISPLAY_ID_DEFAULT, {50, 50}))
8152 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8153 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8154 mWindow->consumeDragEvent(false, 50, 50);
8155 mSecondWindow->assertNoEvents();
8156
8157 // Move to another window.
8158 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8159 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8160 ADISPLAY_ID_DEFAULT, {150, 50}))
8161 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8162 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8163 mWindow->consumeDragEvent(true, 150, 50);
8164 mSecondWindow->consumeDragEvent(false, 50, 50);
8165
8166 // drop to another window.
8167 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8168 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8169 {150, 50}))
8170 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8171 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8172 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8173 mWindow->assertNoEvents();
8174 mSecondWindow->assertNoEvents();
8175}
8176
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008177TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8178 startDrag(true, AINPUT_SOURCE_MOUSE);
8179 // Move on window.
8180 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8181 injectMotionEvent(mDispatcher,
8182 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8183 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8184 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008185 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008186 .x(50)
8187 .y(50))
8188 .build()))
8189 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8190 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8191 mWindow->consumeDragEvent(false, 50, 50);
8192 mSecondWindow->assertNoEvents();
8193
8194 // Move to another window.
8195 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8196 injectMotionEvent(mDispatcher,
8197 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8198 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8199 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008200 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008201 .x(150)
8202 .y(50))
8203 .build()))
8204 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8205 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8206 mWindow->consumeDragEvent(true, 150, 50);
8207 mSecondWindow->consumeDragEvent(false, 50, 50);
8208
8209 // drop to another window.
8210 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8211 injectMotionEvent(mDispatcher,
8212 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8213 .buttonState(0)
8214 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008215 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008216 .x(150)
8217 .y(50))
8218 .build()))
8219 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8220 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8221 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8222 mWindow->assertNoEvents();
8223 mSecondWindow->assertNoEvents();
8224}
8225
Vishnu Nair062a8672021-09-03 16:07:44 -07008226class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8227
8228TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8229 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008230 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8231 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008232 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008233 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8234 window->setFocusable(true);
8235 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8236 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008237 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008238
8239 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008240 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008241 window->assertNoEvents();
8242
Prabir Pradhan678438e2023-04-13 19:32:51 +00008243 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8244 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008245 window->assertNoEvents();
8246
8247 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008248 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008249 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8250
Prabir Pradhan678438e2023-04-13 19:32:51 +00008251 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008252 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8253
Prabir Pradhan678438e2023-04-13 19:32:51 +00008254 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8255 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008256 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8257 window->assertNoEvents();
8258}
8259
8260TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8261 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8262 std::make_shared<FakeApplicationHandle>();
8263 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008264 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8265 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008266 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8267 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008268 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008269 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008270 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8271 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008272 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008273 window->setOwnerInfo(222, 222);
8274 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8275 window->setFocusable(true);
8276 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8277 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008278 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008279
8280 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008281 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008282 window->assertNoEvents();
8283
Prabir Pradhan678438e2023-04-13 19:32:51 +00008284 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8285 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008286 window->assertNoEvents();
8287
8288 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008289 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008290 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8291
Prabir Pradhan678438e2023-04-13 19:32:51 +00008292 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008293 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8294
Prabir Pradhan678438e2023-04-13 19:32:51 +00008295 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8296 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008297 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8298 window->assertNoEvents();
8299}
8300
8301TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8302 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8303 std::make_shared<FakeApplicationHandle>();
8304 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008305 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8306 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008307 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8308 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008309 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008310 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008311 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8312 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008313 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008314 window->setOwnerInfo(222, 222);
8315 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8316 window->setFocusable(true);
8317 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8318 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008319 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008320
8321 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008322 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008323 window->assertNoEvents();
8324
Prabir Pradhan678438e2023-04-13 19:32:51 +00008325 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8326 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008327 window->assertNoEvents();
8328
8329 // When the window is no longer obscured because it went on top, it should get input
8330 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8331
Prabir Pradhan678438e2023-04-13 19:32:51 +00008332 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008333 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8334
Prabir Pradhan678438e2023-04-13 19:32:51 +00008335 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8336 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008337 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8338 window->assertNoEvents();
8339}
8340
Antonio Kantekf16f2832021-09-28 04:39:20 +00008341class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8342protected:
8343 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008344 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008345 sp<FakeWindowHandle> mWindow;
8346 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008347 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008348
8349 void SetUp() override {
8350 InputDispatcherTest::SetUp();
8351
8352 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008353 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008354 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008355 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008356 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008357 mSecondWindow =
8358 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008359 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008360 mThirdWindow =
8361 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8362 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8363 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008364
8365 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008366 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8367 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8368 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008369 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008370
Antonio Kantek15beb512022-06-13 22:35:41 +00008371 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008372 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008373 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008374 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8375 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008376 mThirdWindow->assertNoEvents();
8377 }
8378
8379 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8380 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008381 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008382 SECOND_DISPLAY_ID)) {
8383 mWindow->assertNoEvents();
8384 mSecondWindow->assertNoEvents();
8385 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008386 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008387 }
8388
Antonio Kantek15beb512022-06-13 22:35:41 +00008389 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8390 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008391 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8392 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008393 mWindow->consumeTouchModeEvent(inTouchMode);
8394 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008395 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008396 }
8397};
8398
Antonio Kantek26defcf2022-02-08 01:12:27 +00008399TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008400 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008401 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8402 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008403 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008404}
8405
Antonio Kantek26defcf2022-02-08 01:12:27 +00008406TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8407 const WindowInfo& windowInfo = *mWindow->getInfo();
8408 int32_t ownerPid = windowInfo.ownerPid;
8409 int32_t ownerUid = windowInfo.ownerUid;
8410 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8411 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008412 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008413 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008414 mWindow->assertNoEvents();
8415 mSecondWindow->assertNoEvents();
8416}
8417
8418TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8419 const WindowInfo& windowInfo = *mWindow->getInfo();
8420 int32_t ownerPid = windowInfo.ownerPid;
8421 int32_t ownerUid = windowInfo.ownerUid;
8422 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008423 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008424 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008425}
8426
Antonio Kantekf16f2832021-09-28 04:39:20 +00008427TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008428 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008429 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8430 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008431 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008432 mWindow->assertNoEvents();
8433 mSecondWindow->assertNoEvents();
8434}
8435
Antonio Kantek15beb512022-06-13 22:35:41 +00008436TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8437 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8438 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8439 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008440 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008441 mWindow->assertNoEvents();
8442 mSecondWindow->assertNoEvents();
8443 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8444}
8445
Antonio Kantek48710e42022-03-24 14:19:30 -07008446TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8447 // Interact with the window first.
8448 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8449 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8450 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8451
8452 // Then remove focus.
8453 mWindow->setFocusable(false);
8454 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8455
8456 // Assert that caller can switch touch mode by owning one of the last interacted window.
8457 const WindowInfo& windowInfo = *mWindow->getInfo();
8458 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8459 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008460 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008461}
8462
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008463class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8464public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008465 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008466 std::shared_ptr<FakeApplicationHandle> application =
8467 std::make_shared<FakeApplicationHandle>();
8468 std::string name = "Fake Spy ";
8469 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008470 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8471 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008472 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008473 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008474 return spy;
8475 }
8476
8477 sp<FakeWindowHandle> createForeground() {
8478 std::shared_ptr<FakeApplicationHandle> application =
8479 std::make_shared<FakeApplicationHandle>();
8480 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008481 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8482 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008483 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008484 return window;
8485 }
8486
8487private:
8488 int mSpyCount{0};
8489};
8490
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008491using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008492/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008493 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8494 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008495TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8496 ScopedSilentDeath _silentDeath;
8497
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008498 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008499 spy->setTrustedOverlay(false);
8500 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8501 ".* not a trusted overlay");
8502}
8503
8504/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008505 * Input injection into a display with a spy window but no foreground windows should succeed.
8506 */
8507TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008508 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008509 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8510
8511 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8512 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8513 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8514 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8515}
8516
8517/**
8518 * Verify the order in which different input windows receive events. The touched foreground window
8519 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8520 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8521 * receive events before ones belows it.
8522 *
8523 * Here, we set up a scenario with four windows in the following Z order from the top:
8524 * spy1, spy2, window, spy3.
8525 * We then inject an event and verify that the foreground "window" receives it first, followed by
8526 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
8527 * window.
8528 */
8529TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
8530 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008531 auto spy1 = createSpy();
8532 auto spy2 = createSpy();
8533 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008534 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
8535 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
8536 const size_t numChannels = channels.size();
8537
Michael Wright8e9a8562022-02-09 13:44:29 +00008538 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008539 if (!epollFd.ok()) {
8540 FAIL() << "Failed to create epoll fd";
8541 }
8542
8543 for (size_t i = 0; i < numChannels; i++) {
8544 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
8545 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
8546 FAIL() << "Failed to add fd to epoll";
8547 }
8548 }
8549
8550 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8551 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8552 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8553
8554 std::vector<size_t> eventOrder;
8555 std::vector<struct epoll_event> events(numChannels);
8556 for (;;) {
8557 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
8558 (100ms).count());
8559 if (nFds < 0) {
8560 FAIL() << "Failed to call epoll_wait";
8561 }
8562 if (nFds == 0) {
8563 break; // epoll_wait timed out
8564 }
8565 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07008566 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07008567 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008568 channels[i]->consumeMotionDown();
8569 }
8570 }
8571
8572 // Verify the order in which the events were received.
8573 EXPECT_EQ(3u, eventOrder.size());
8574 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
8575 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
8576 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
8577}
8578
8579/**
8580 * A spy window using the NOT_TOUCHABLE flag does not receive events.
8581 */
8582TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
8583 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008584 auto spy = createSpy();
8585 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008586 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8587
8588 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8589 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8590 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8591 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8592 spy->assertNoEvents();
8593}
8594
8595/**
8596 * A spy window will only receive gestures that originate within its touchable region. Gestures that
8597 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
8598 * to the window.
8599 */
8600TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
8601 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008602 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008603 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
8604 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8605
8606 // Inject an event outside the spy window's touchable region.
8607 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8608 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8609 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8610 window->consumeMotionDown();
8611 spy->assertNoEvents();
8612 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8613 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8614 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8615 window->consumeMotionUp();
8616 spy->assertNoEvents();
8617
8618 // Inject an event inside the spy window's touchable region.
8619 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8620 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8621 {5, 10}))
8622 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8623 window->consumeMotionDown();
8624 spy->consumeMotionDown();
8625}
8626
8627/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008628 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008629 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008630 */
8631TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
8632 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008633 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008634 auto spy = createSpy();
8635 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008636 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008637 spy->setFrame(Rect{0, 0, 20, 20});
8638 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8639
8640 // Inject an event outside the spy window's frame and touchable region.
8641 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008642 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8643 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008644 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8645 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008646 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008647}
8648
8649/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008650 * Even when a spy window spans over multiple foreground windows, the spy should receive all
8651 * pointers that are down within its bounds.
8652 */
8653TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
8654 auto windowLeft = createForeground();
8655 windowLeft->setFrame({0, 0, 100, 200});
8656 auto windowRight = createForeground();
8657 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008658 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008659 spy->setFrame({0, 0, 200, 200});
8660 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
8661
8662 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8663 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8664 {50, 50}))
8665 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8666 windowLeft->consumeMotionDown();
8667 spy->consumeMotionDown();
8668
8669 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008670 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008671 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008672 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8673 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008674 .build();
8675 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8676 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8677 InputEventInjectionSync::WAIT_FOR_RESULT))
8678 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8679 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00008680 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008681}
8682
8683/**
8684 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
8685 * the spy should receive the second pointer with ACTION_DOWN.
8686 */
8687TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
8688 auto window = createForeground();
8689 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008690 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008691 spyRight->setFrame({100, 0, 200, 200});
8692 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
8693
8694 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8695 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8696 {50, 50}))
8697 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8698 window->consumeMotionDown();
8699 spyRight->assertNoEvents();
8700
8701 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008702 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008703 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008704 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8705 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008706 .build();
8707 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8708 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8709 InputEventInjectionSync::WAIT_FOR_RESULT))
8710 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008711 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008712 spyRight->consumeMotionDown();
8713}
8714
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008715/**
8716 * The spy window should not be able to affect whether or not touches are split. Only the foreground
8717 * windows should be allowed to control split touch.
8718 */
8719TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008720 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008721 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008722 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008723 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008724
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008725 auto window = createForeground();
8726 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008727
8728 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8729
8730 // First finger down, no window touched.
8731 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8732 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8733 {100, 200}))
8734 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8735 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8736 window->assertNoEvents();
8737
8738 // Second finger down on window, the window should receive touch down.
8739 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008740 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008741 .displayId(ADISPLAY_ID_DEFAULT)
8742 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008743 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8744 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008745 .build();
8746 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8747 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8748 InputEventInjectionSync::WAIT_FOR_RESULT))
8749 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8750
8751 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00008752 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008753}
8754
8755/**
8756 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
8757 * do not receive key events.
8758 */
8759TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008760 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008761 spy->setFocusable(false);
8762
8763 auto window = createForeground();
8764 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8765 setFocusedWindow(window);
8766 window->consumeFocusEvent(true);
8767
8768 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
8769 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8770 window->consumeKeyDown(ADISPLAY_ID_NONE);
8771
8772 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
8773 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8774 window->consumeKeyUp(ADISPLAY_ID_NONE);
8775
8776 spy->assertNoEvents();
8777}
8778
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008779using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
8780
8781/**
8782 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
8783 * are currently sent to any other windows - including other spy windows - will also be cancelled.
8784 */
8785TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
8786 auto window = createForeground();
8787 auto spy1 = createSpy();
8788 auto spy2 = createSpy();
8789 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
8790
8791 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8792 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8793 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8794 window->consumeMotionDown();
8795 spy1->consumeMotionDown();
8796 spy2->consumeMotionDown();
8797
8798 // Pilfer pointers from the second spy window.
8799 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
8800 spy2->assertNoEvents();
8801 spy1->consumeMotionCancel();
8802 window->consumeMotionCancel();
8803
8804 // The rest of the gesture should only be sent to the second spy window.
8805 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8806 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8807 ADISPLAY_ID_DEFAULT))
8808 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8809 spy2->consumeMotionMove();
8810 spy1->assertNoEvents();
8811 window->assertNoEvents();
8812}
8813
8814/**
8815 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
8816 * in the middle of the gesture.
8817 */
8818TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
8819 auto window = createForeground();
8820 auto spy = createSpy();
8821 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8822
8823 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8824 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8825 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8826 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8827 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8828
8829 window->releaseChannel();
8830
8831 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8832
8833 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8834 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8835 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8836 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8837}
8838
8839/**
8840 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
8841 * the spy, but not to any other windows.
8842 */
8843TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
8844 auto spy = createSpy();
8845 auto window = createForeground();
8846
8847 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8848
8849 // First finger down on the window and the spy.
8850 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8851 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8852 {100, 200}))
8853 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8854 spy->consumeMotionDown();
8855 window->consumeMotionDown();
8856
8857 // Spy window pilfers the pointers.
8858 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8859 window->consumeMotionCancel();
8860
8861 // Second finger down on the window and spy, but the window should not receive the pointer down.
8862 const MotionEvent secondFingerDownEvent =
8863 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8864 .displayId(ADISPLAY_ID_DEFAULT)
8865 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008866 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8867 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008868 .build();
8869 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8870 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8871 InputEventInjectionSync::WAIT_FOR_RESULT))
8872 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8873
Harry Cutts33476232023-01-30 19:57:29 +00008874 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008875
8876 // Third finger goes down outside all windows, so injection should fail.
8877 const MotionEvent thirdFingerDownEvent =
8878 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8879 .displayId(ADISPLAY_ID_DEFAULT)
8880 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008881 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8882 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
8883 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008884 .build();
8885 ASSERT_EQ(InputEventInjectionResult::FAILED,
8886 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8887 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008888 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008889
8890 spy->assertNoEvents();
8891 window->assertNoEvents();
8892}
8893
8894/**
8895 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
8896 */
8897TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
8898 auto spy = createSpy();
8899 spy->setFrame(Rect(0, 0, 100, 100));
8900 auto window = createForeground();
8901 window->setFrame(Rect(0, 0, 200, 200));
8902
8903 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8904
8905 // First finger down on the window only
8906 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8907 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8908 {150, 150}))
8909 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8910 window->consumeMotionDown();
8911
8912 // Second finger down on the spy and window
8913 const MotionEvent secondFingerDownEvent =
8914 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8915 .displayId(ADISPLAY_ID_DEFAULT)
8916 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008917 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
8918 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008919 .build();
8920 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8921 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8922 InputEventInjectionSync::WAIT_FOR_RESULT))
8923 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8924 spy->consumeMotionDown();
8925 window->consumeMotionPointerDown(1);
8926
8927 // Third finger down on the spy and window
8928 const MotionEvent thirdFingerDownEvent =
8929 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8930 .displayId(ADISPLAY_ID_DEFAULT)
8931 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008932 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
8933 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
8934 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008935 .build();
8936 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8937 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8938 InputEventInjectionSync::WAIT_FOR_RESULT))
8939 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8940 spy->consumeMotionPointerDown(1);
8941 window->consumeMotionPointerDown(2);
8942
8943 // Spy window pilfers the pointers.
8944 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8945 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8946 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8947
8948 spy->assertNoEvents();
8949 window->assertNoEvents();
8950}
8951
8952/**
8953 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
8954 * other windows should be canceled. If this results in the cancellation of all pointers for some
8955 * window, then that window should receive ACTION_CANCEL.
8956 */
8957TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
8958 auto spy = createSpy();
8959 spy->setFrame(Rect(0, 0, 100, 100));
8960 auto window = createForeground();
8961 window->setFrame(Rect(0, 0, 200, 200));
8962
8963 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8964
8965 // First finger down on both spy and window
8966 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8967 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8968 {10, 10}))
8969 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8970 window->consumeMotionDown();
8971 spy->consumeMotionDown();
8972
8973 // Second finger down on the spy and window
8974 const MotionEvent secondFingerDownEvent =
8975 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8976 .displayId(ADISPLAY_ID_DEFAULT)
8977 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008978 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
8979 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008980 .build();
8981 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8982 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8983 InputEventInjectionSync::WAIT_FOR_RESULT))
8984 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8985 spy->consumeMotionPointerDown(1);
8986 window->consumeMotionPointerDown(1);
8987
8988 // Spy window pilfers the pointers.
8989 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8990 window->consumeMotionCancel();
8991
8992 spy->assertNoEvents();
8993 window->assertNoEvents();
8994}
8995
8996/**
8997 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
8998 * be sent to other windows
8999 */
9000TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9001 auto spy = createSpy();
9002 spy->setFrame(Rect(0, 0, 100, 100));
9003 auto window = createForeground();
9004 window->setFrame(Rect(0, 0, 200, 200));
9005
9006 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9007
9008 // First finger down on both window and spy
9009 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9010 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9011 {10, 10}))
9012 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9013 window->consumeMotionDown();
9014 spy->consumeMotionDown();
9015
9016 // Spy window pilfers the pointers.
9017 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9018 window->consumeMotionCancel();
9019
9020 // Second finger down on the window only
9021 const MotionEvent secondFingerDownEvent =
9022 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9023 .displayId(ADISPLAY_ID_DEFAULT)
9024 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009025 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9026 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009027 .build();
9028 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9029 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9030 InputEventInjectionSync::WAIT_FOR_RESULT))
9031 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9032 window->consumeMotionDown();
9033 window->assertNoEvents();
9034
9035 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9036 spy->consumeMotionMove();
9037 spy->assertNoEvents();
9038}
9039
Prabir Pradhand65552b2021-10-07 11:23:50 -07009040class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9041public:
9042 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9043 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9044 std::make_shared<FakeApplicationHandle>();
9045 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009046 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9047 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009048 overlay->setFocusable(false);
9049 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009050 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009051 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009052 overlay->setTrustedOverlay(true);
9053
9054 std::shared_ptr<FakeApplicationHandle> application =
9055 std::make_shared<FakeApplicationHandle>();
9056 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009057 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9058 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009059 window->setFocusable(true);
9060 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009061
9062 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9063 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9064 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009065 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009066 return {std::move(overlay), std::move(window)};
9067 }
9068
9069 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009070 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009071 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009072 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009073 }
9074
9075 void sendStylusEvent(int32_t action) {
9076 NotifyMotionArgs motionArgs =
9077 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9078 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009079 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009080 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009081 }
9082};
9083
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009084using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9085
9086TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9087 ScopedSilentDeath _silentDeath;
9088
Prabir Pradhand65552b2021-10-07 11:23:50 -07009089 auto [overlay, window] = setupStylusOverlayScenario();
9090 overlay->setTrustedOverlay(false);
9091 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9092 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9093 ".* not a trusted overlay");
9094}
9095
9096TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9097 auto [overlay, window] = setupStylusOverlayScenario();
9098 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9099
9100 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9101 overlay->consumeMotionDown();
9102 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9103 overlay->consumeMotionUp();
9104
9105 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9106 window->consumeMotionDown();
9107 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9108 window->consumeMotionUp();
9109
9110 overlay->assertNoEvents();
9111 window->assertNoEvents();
9112}
9113
9114TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9115 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009116 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009117 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9118
9119 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9120 overlay->consumeMotionDown();
9121 window->consumeMotionDown();
9122 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9123 overlay->consumeMotionUp();
9124 window->consumeMotionUp();
9125
9126 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9127 window->consumeMotionDown();
9128 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9129 window->consumeMotionUp();
9130
9131 overlay->assertNoEvents();
9132 window->assertNoEvents();
9133}
9134
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009135/**
9136 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9137 * The scenario is as follows:
9138 * - The stylus interceptor overlay is configured as a spy window.
9139 * - The stylus interceptor spy receives the start of a new stylus gesture.
9140 * - It pilfers pointers and then configures itself to no longer be a spy.
9141 * - The stylus interceptor continues to receive the rest of the gesture.
9142 */
9143TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9144 auto [overlay, window] = setupStylusOverlayScenario();
9145 overlay->setSpy(true);
9146 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9147
9148 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9149 overlay->consumeMotionDown();
9150 window->consumeMotionDown();
9151
9152 // The interceptor pilfers the pointers.
9153 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9154 window->consumeMotionCancel();
9155
9156 // The interceptor configures itself so that it is no longer a spy.
9157 overlay->setSpy(false);
9158 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9159
9160 // It continues to receive the rest of the stylus gesture.
9161 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9162 overlay->consumeMotionMove();
9163 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9164 overlay->consumeMotionUp();
9165
9166 window->assertNoEvents();
9167}
9168
Prabir Pradhan5735a322022-04-11 17:23:34 +00009169struct User {
9170 int32_t mPid;
9171 int32_t mUid;
9172 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9173 std::unique_ptr<InputDispatcher>& mDispatcher;
9174
9175 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9176 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9177
9178 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9179 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9180 ADISPLAY_ID_DEFAULT, {100, 200},
9181 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9182 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9183 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9184 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9185 }
9186
9187 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009188 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009189 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009190 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009191 mPolicyFlags);
9192 }
9193
9194 sp<FakeWindowHandle> createWindow() const {
9195 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9196 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009197 sp<FakeWindowHandle> window =
9198 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9199 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009200 window->setOwnerInfo(mPid, mUid);
9201 return window;
9202 }
9203};
9204
9205using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9206
9207TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9208 auto owner = User(mDispatcher, 10, 11);
9209 auto window = owner.createWindow();
9210 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9211
9212 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9213 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9214 window->consumeMotionDown();
9215
9216 setFocusedWindow(window);
9217 window->consumeFocusEvent(true);
9218
9219 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9220 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9221 window->consumeKeyDown(ADISPLAY_ID_NONE);
9222}
9223
9224TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9225 auto owner = User(mDispatcher, 10, 11);
9226 auto window = owner.createWindow();
9227 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9228
9229 auto rando = User(mDispatcher, 20, 21);
9230 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9231 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9232
9233 setFocusedWindow(window);
9234 window->consumeFocusEvent(true);
9235
9236 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9237 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9238 window->assertNoEvents();
9239}
9240
9241TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9242 auto owner = User(mDispatcher, 10, 11);
9243 auto window = owner.createWindow();
9244 auto spy = owner.createWindow();
9245 spy->setSpy(true);
9246 spy->setTrustedOverlay(true);
9247 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9248
9249 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9250 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9251 spy->consumeMotionDown();
9252 window->consumeMotionDown();
9253}
9254
9255TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9256 auto owner = User(mDispatcher, 10, 11);
9257 auto window = owner.createWindow();
9258
9259 auto rando = User(mDispatcher, 20, 21);
9260 auto randosSpy = rando.createWindow();
9261 randosSpy->setSpy(true);
9262 randosSpy->setTrustedOverlay(true);
9263 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9264
9265 // The event is targeted at owner's window, so injection should succeed, but the spy should
9266 // not receive the event.
9267 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9268 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9269 randosSpy->assertNoEvents();
9270 window->consumeMotionDown();
9271}
9272
9273TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9274 auto owner = User(mDispatcher, 10, 11);
9275 auto window = owner.createWindow();
9276
9277 auto rando = User(mDispatcher, 20, 21);
9278 auto randosSpy = rando.createWindow();
9279 randosSpy->setSpy(true);
9280 randosSpy->setTrustedOverlay(true);
9281 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9282
9283 // A user that has injection permission can inject into any window.
9284 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9285 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9286 ADISPLAY_ID_DEFAULT));
9287 randosSpy->consumeMotionDown();
9288 window->consumeMotionDown();
9289
9290 setFocusedWindow(randosSpy);
9291 randosSpy->consumeFocusEvent(true);
9292
9293 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9294 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9295 window->assertNoEvents();
9296}
9297
9298TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9299 auto owner = User(mDispatcher, 10, 11);
9300 auto window = owner.createWindow();
9301
9302 auto rando = User(mDispatcher, 20, 21);
9303 auto randosWindow = rando.createWindow();
9304 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9305 randosWindow->setWatchOutsideTouch(true);
9306 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9307
9308 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9309 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9310 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9311 window->consumeMotionDown();
9312 randosWindow->consumeMotionOutside();
9313}
9314
Garfield Tane84e6f92019-08-29 17:28:41 -07009315} // namespace android::inputdispatcher