blob: 4bd5f73730acfc0a28c57c95bc7a47f31b7ed527 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070019#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080020#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080021#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070022#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070023#include <binder/Binder.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000024#include <fcntl.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080025#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100027#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080029#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100030
Garfield Tan1c7bc862020-01-28 13:24:04 -080031#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080032#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070033#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080034#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080035#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036
Garfield Tan1c7bc862020-01-28 13:24:04 -080037using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050038using android::gui::FocusRequest;
39using android::gui::TouchOcclusionMode;
40using android::gui::WindowInfo;
41using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080042using android::os::InputEventInjectionResult;
43using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080044
Garfield Tane84e6f92019-08-29 17:28:41 -070045namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080046
Dominik Laskowski2f01d772022-03-23 16:01:29 -070047using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080048using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070049
Michael Wrightd02c5b62014-02-10 15:10:22 -080050// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000051static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080052
53// An arbitrary device id.
Prabir Pradhan5735a322022-04-11 17:23:34 +000054static constexpr int32_t DEVICE_ID = 1;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080055static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
Jeff Brownf086ddb2014-02-11 14:28:48 -080057// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000058static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
59static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080060
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080061static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
62static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
63static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
64static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
65static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080066static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080067static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080068/**
69 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
70 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
71 * index 0) is the new pointer going down. The same pointer could have been placed at a different
72 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
73 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
74 * pointer id=0 leaves but the pointer id=1 remains.
75 */
76static constexpr int32_t POINTER_0_DOWN =
77 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080078static constexpr int32_t POINTER_1_DOWN =
79 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000080static constexpr int32_t POINTER_2_DOWN =
81 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000082static constexpr int32_t POINTER_3_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000084static constexpr int32_t POINTER_0_UP =
85 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080086static constexpr int32_t POINTER_1_UP =
87 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88
Antonio Kantek15beb512022-06-13 22:35:41 +000089// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000090static constexpr int32_t WINDOW_PID = 999;
91static constexpr int32_t WINDOW_UID = 1001;
92
Antonio Kantek15beb512022-06-13 22:35:41 +000093// The default pid and uid for the windows created on the secondary display by the test.
94static constexpr int32_t SECONDARY_WINDOW_PID = 1010;
95static constexpr int32_t SECONDARY_WINDOW_UID = 1012;
96
Prabir Pradhan5735a322022-04-11 17:23:34 +000097// The default policy flags to use for event injection by tests.
98static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
Michael Wrightd02c5b62014-02-10 15:10:22 -080099
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000100// An arbitrary pid of the gesture monitor window
101static constexpr int32_t MONITOR_PID = 2001;
102
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800103static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
104
Arthur Hungc539dbb2022-12-08 07:45:36 +0000105static constexpr int expectedWallpaperFlags =
106 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
107
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800108using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
109
chaviwd1c23182019-12-20 18:44:56 -0800110struct PointF {
111 float x;
112 float y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800113 auto operator<=>(const PointF&) const = default;
chaviwd1c23182019-12-20 18:44:56 -0800114};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
Gang Wang342c9272020-01-13 13:15:04 -0500116/**
117 * Return a DOWN key event with KEYCODE_A.
118 */
119static KeyEvent getTestKeyEvent() {
120 KeyEvent event;
121
Garfield Tanfbe732e2020-01-24 11:26:14 -0800122 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
123 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
124 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500125 return event;
126}
127
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000128static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
129 ASSERT_EQ(expectedAction, receivedAction)
130 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
131 << MotionEvent::actionToString(receivedAction);
132}
133
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800134MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
135 bool matches = action == arg.getAction();
136 if (!matches) {
137 *result_listener << "expected action " << MotionEvent::actionToString(action)
138 << ", but got " << MotionEvent::actionToString(arg.getAction());
139 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800140 if (action == AMOTION_EVENT_ACTION_DOWN) {
141 if (!matches) {
142 *result_listener << "; ";
143 }
144 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
145 matches &= arg.getDownTime() == arg.getEventTime();
146 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800147 if (action == AMOTION_EVENT_ACTION_CANCEL) {
148 if (!matches) {
149 *result_listener << "; ";
150 }
151 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
152 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
153 }
154 return matches;
155}
156
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800157MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
158 return arg.getDownTime() == downTime;
159}
160
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800161MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
162 return arg.getDisplayId() == displayId;
163}
164
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800165MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") {
166 return arg.getDeviceId() == deviceId;
167}
168
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800169MATCHER_P(WithSource, source, "InputEvent with specified source") {
170 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
171 << inputEventSourceToString(arg.getSource());
172 return arg.getSource() == source;
173}
174
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800175MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
176 return arg.getFlags() == flags;
177}
178
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800179MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
180 if (arg.getPointerCount() != 1) {
181 *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
182 return false;
183 }
Harry Cutts33476232023-01-30 19:57:29 +0000184 return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800185}
186
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800187MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") {
188 return arg.getPointerCount() == pointerCount;
189}
190
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800191MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
192 // Build a map for the received pointers, by pointer id
193 std::map<int32_t /*pointerId*/, PointF> actualPointers;
194 for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
195 const int32_t pointerId = arg.getPointerId(pointerIndex);
196 actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
197 }
198 return pointers == actualPointers;
199}
200
Michael Wrightd02c5b62014-02-10 15:10:22 -0800201// --- FakeInputDispatcherPolicy ---
202
203class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
204 InputDispatcherConfiguration mConfig;
205
Prabir Pradhanedd96402022-02-15 01:46:16 -0800206 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
207
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208protected:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000209 virtual ~FakeInputDispatcherPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800210
211public:
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000212 FakeInputDispatcherPolicy() {}
Jackal Guof9696682018-10-05 12:23:23 +0800213
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800214 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700215 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700216 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700217 EXPECT_EQ(event.getDisplayId(), args.displayId);
218
219 const auto& keyEvent = static_cast<const KeyEvent&>(event);
220 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
221 EXPECT_EQ(keyEvent.getAction(), args.action);
222 });
Jackal Guof9696682018-10-05 12:23:23 +0800223 }
224
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700225 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
226 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700227 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700228 EXPECT_EQ(event.getDisplayId(), args.displayId);
229
230 const auto& motionEvent = static_cast<const MotionEvent&>(event);
231 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
232 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000233 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
234 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
235 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
236 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700237 });
Jackal Guof9696682018-10-05 12:23:23 +0800238 }
239
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700240 void assertFilterInputEventWasNotCalled() {
241 std::scoped_lock lock(mLock);
242 ASSERT_EQ(nullptr, mFilteredEvent);
243 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800245 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700246 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800247 ASSERT_TRUE(mConfigurationChangedTime)
248 << "Timed out waiting for configuration changed call";
249 ASSERT_EQ(*mConfigurationChangedTime, when);
250 mConfigurationChangedTime = std::nullopt;
251 }
252
253 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700254 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800255 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800256 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800257 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
258 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
259 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
260 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
261 mLastNotifySwitch = std::nullopt;
262 }
263
chaviwfd6d3512019-03-25 13:23:49 -0700264 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700265 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800266 ASSERT_EQ(touchedToken, mOnPointerDownToken);
267 mOnPointerDownToken.clear();
268 }
269
270 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700271 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800272 ASSERT_TRUE(mOnPointerDownToken == nullptr)
273 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700274 }
275
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700276 // This function must be called soon after the expected ANR timer starts,
277 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500278 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700279 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500280 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800281 std::unique_lock lock(mLock);
282 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500283 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800284 ASSERT_NO_FATAL_FAILURE(
285 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500286 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700287 }
288
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000289 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800290 const sp<WindowInfoHandle>& window) {
291 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
292 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
293 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500294 }
295
Prabir Pradhanedd96402022-02-15 01:46:16 -0800296 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
297 const sp<IBinder>& expectedToken,
298 int32_t expectedPid) {
299 std::unique_lock lock(mLock);
300 android::base::ScopedLockAssertion assumeLocked(mLock);
301 AnrResult result;
302 ASSERT_NO_FATAL_FAILURE(result =
303 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
304 const auto& [token, pid] = result;
305 ASSERT_EQ(expectedToken, token);
306 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500307 }
308
Prabir Pradhanedd96402022-02-15 01:46:16 -0800309 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000310 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500311 std::unique_lock lock(mLock);
312 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800313 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
314 const auto& [token, _] = result;
315 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000316 }
317
Prabir Pradhanedd96402022-02-15 01:46:16 -0800318 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
319 int32_t expectedPid) {
320 std::unique_lock lock(mLock);
321 android::base::ScopedLockAssertion assumeLocked(mLock);
322 AnrResult result;
323 ASSERT_NO_FATAL_FAILURE(
324 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
325 const auto& [token, pid] = result;
326 ASSERT_EQ(expectedToken, token);
327 ASSERT_EQ(expectedPid, pid);
328 }
329
330 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000331 sp<IBinder> getResponsiveWindowToken() {
332 std::unique_lock lock(mLock);
333 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800334 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
335 const auto& [token, _] = result;
336 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700337 }
338
339 void assertNotifyAnrWasNotCalled() {
340 std::scoped_lock lock(mLock);
341 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800342 ASSERT_TRUE(mAnrWindows.empty());
343 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500344 << "ANR was not called, but please also consume the 'connection is responsive' "
345 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700346 }
347
Garfield Tan1c7bc862020-01-28 13:24:04 -0800348 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
349 mConfig.keyRepeatTimeout = timeout;
350 mConfig.keyRepeatDelay = delay;
351 }
352
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000353 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800354 std::unique_lock lock(mLock);
355 base::ScopedLockAssertion assumeLocked(mLock);
356
357 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
358 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000359 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800360 enabled;
361 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000362 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
363 << ") to be called.";
364 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800365 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000366 auto request = *mPointerCaptureRequest;
367 mPointerCaptureRequest.reset();
368 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800369 }
370
371 void assertSetPointerCaptureNotCalled() {
372 std::unique_lock lock(mLock);
373 base::ScopedLockAssertion assumeLocked(mLock);
374
375 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000376 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800377 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000378 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800379 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000380 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800381 }
382
arthurhungf452d0b2021-01-06 00:19:52 +0800383 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
384 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800385 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800386 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800387 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800388 }
389
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800390 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
391 std::unique_lock lock(mLock);
392 base::ScopedLockAssertion assumeLocked(mLock);
393 std::optional<sp<IBinder>> receivedToken =
394 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
395 mNotifyInputChannelBroken);
396 ASSERT_TRUE(receivedToken.has_value());
397 ASSERT_EQ(token, *receivedToken);
398 }
399
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800400 /**
401 * Set policy timeout. A value of zero means next key will not be intercepted.
402 */
403 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
404 mInterceptKeyTimeout = timeout;
405 }
406
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700408 std::mutex mLock;
409 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
410 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
411 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
412 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800413
Prabir Pradhan99987712020-11-10 18:43:05 -0800414 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000415
416 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800417
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700418 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700419 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800420 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
421 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700422 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800423 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
424 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700425
arthurhungf452d0b2021-01-06 00:19:52 +0800426 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800427 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800428
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800429 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
430
Prabir Pradhanedd96402022-02-15 01:46:16 -0800431 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
432 // for a specific container to become non-empty. When the container is non-empty, return the
433 // first entry from the container and erase it.
434 template <class T>
435 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
436 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
437 // If there is an ANR, Dispatcher won't be idle because there are still events
438 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
439 // before checking if ANR was called.
440 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
441 // to provide it some time to act. 100ms seems reasonable.
442 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
443 const std::chrono::time_point start = std::chrono::steady_clock::now();
444 std::optional<T> token =
445 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
446 if (!token.has_value()) {
447 ADD_FAILURE() << "Did not receive the ANR callback";
448 return {};
449 }
450
451 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
452 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
453 // the dispatcher started counting before this function was called
454 if (std::chrono::abs(timeout - waited) > 100ms) {
455 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
456 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
457 << "ms, but waited "
458 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
459 << "ms instead";
460 }
461 return *token;
462 }
463
464 template <class T>
465 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
466 std::queue<T>& storage,
467 std::unique_lock<std::mutex>& lock,
468 std::condition_variable& condition)
469 REQUIRES(mLock) {
470 condition.wait_for(lock, timeout,
471 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
472 if (storage.empty()) {
473 ADD_FAILURE() << "Did not receive the expected callback";
474 return std::nullopt;
475 }
476 T item = storage.front();
477 storage.pop();
478 return std::make_optional(item);
479 }
480
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600481 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700482 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800483 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484 }
485
Prabir Pradhanedd96402022-02-15 01:46:16 -0800486 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
487 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700488 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800489 ASSERT_TRUE(pid.has_value());
490 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700491 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500492 }
493
Prabir Pradhanedd96402022-02-15 01:46:16 -0800494 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
495 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500496 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800497 ASSERT_TRUE(pid.has_value());
498 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500499 mNotifyAnr.notify_all();
500 }
501
502 void notifyNoFocusedWindowAnr(
503 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
504 std::scoped_lock lock(mLock);
505 mAnrApplications.push(applicationHandle);
506 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507 }
508
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800509 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
510 std::scoped_lock lock(mLock);
511 mBrokenInputChannels.push(connectionToken);
512 mNotifyInputChannelBroken.notify_all();
513 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600515 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700516
Chris Yef59a2f42020-10-16 12:55:26 -0700517 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
518 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
519 const std::vector<float>& values) override {}
520
521 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
522 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000523
Chris Yefb552902021-02-03 17:18:37 -0800524 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
525
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600526 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800527 *outConfig = mConfig;
528 }
529
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600530 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700531 std::scoped_lock lock(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800532 switch (inputEvent->getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700533 case InputEventType::KEY: {
Jackal Guof9696682018-10-05 12:23:23 +0800534 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800535 mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800536 break;
537 }
538
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700539 case InputEventType::MOTION: {
Jackal Guof9696682018-10-05 12:23:23 +0800540 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800541 mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800542 break;
543 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700544 default: {
545 ADD_FAILURE() << "Should only filter keys or motions";
546 break;
547 }
Jackal Guof9696682018-10-05 12:23:23 +0800548 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549 return true;
550 }
551
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800552 void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override {
553 if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) {
554 // Clear intercept state when we handled the event.
555 mInterceptKeyTimeout = 0ms;
556 }
557 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800558
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600559 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800560
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600561 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800562 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
563 // Clear intercept state so we could dispatch the event in next wake.
564 mInterceptKeyTimeout = 0ms;
565 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566 }
567
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600568 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800569 return false;
570 }
571
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600572 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
573 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700574 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800575 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
576 * essentially a passthrough for notifySwitch.
577 */
Harry Cutts33476232023-01-30 19:57:29 +0000578 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 }
580
Sean Stoutb4e0a592021-02-23 07:34:53 -0800581 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600583 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700584 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700585 mOnPointerDownToken = newToken;
586 }
587
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000588 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800589 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000590 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800591 mPointerCaptureChangedCondition.notify_all();
592 }
593
arthurhungf452d0b2021-01-06 00:19:52 +0800594 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
595 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800596 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800597 mDropTargetWindowToken = token;
598 }
599
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700600 void assertFilterInputEventWasCalledInternal(
601 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700602 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800603 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700604 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800605 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800606 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800607};
608
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609// --- InputDispatcherTest ---
610
611class InputDispatcherTest : public testing::Test {
612protected:
613 sp<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700614 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000616 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700617 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800618 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800619 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000620 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700621 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 }
623
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000624 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700625 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800626 mFakePolicy.clear();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700627 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700629
630 /**
631 * Used for debugging when writing the test
632 */
633 void dumpDispatcherState() {
634 std::string dump;
635 mDispatcher->dump(dump);
636 std::stringstream ss(dump);
637 std::string to;
638
639 while (std::getline(ss, to, '\n')) {
640 ALOGE("%s", to.c_str());
641 }
642 }
Vishnu Nair958da932020-08-21 17:12:37 -0700643
Chavi Weingarten847e8512023-03-29 00:26:09 +0000644 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700645 FocusRequest request;
646 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000647 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700648 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
649 request.displayId = window->getInfo()->displayId;
650 mDispatcher->setFocusedWindow(request);
651 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652};
653
Michael Wrightd02c5b62014-02-10 15:10:22 -0800654TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
655 KeyEvent event;
656
657 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800658 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
659 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600660 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
661 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800662 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000663 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000664 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800665 << "Should reject key events with undefined action.";
666
667 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800668 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
669 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600670 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800671 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000672 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000673 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674 << "Should reject key events with ACTION_MULTIPLE.";
675}
676
677TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
678 MotionEvent event;
679 PointerProperties pointerProperties[MAX_POINTERS + 1];
680 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800681 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 pointerProperties[i].clear();
683 pointerProperties[i].id = i;
684 pointerCoords[i].clear();
685 }
686
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800687 // Some constants commonly used below
688 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
689 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
690 constexpr int32_t metaState = AMETA_NONE;
691 constexpr MotionClassification classification = MotionClassification::NONE;
692
chaviw9eaa22c2020-07-01 16:21:27 -0700693 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800695 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700696 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
697 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700698 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
699 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700700 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800701 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000702 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000703 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800704 << "Should reject motion events with undefined action.";
705
706 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800707 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800708 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
709 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
710 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
711 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500712 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800713 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000714 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000715 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 << "Should reject motion events with pointer down index too large.";
717
Garfield Tanfbe732e2020-01-24 11:26:14 -0800718 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700719 AMOTION_EVENT_ACTION_POINTER_DOWN |
720 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700721 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
722 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700723 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500724 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800725 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000726 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000727 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728 << "Should reject motion events with pointer down index too small.";
729
730 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800731 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800732 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
733 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
734 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
735 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500736 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800737 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000738 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000739 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740 << "Should reject motion events with pointer up index too large.";
741
Garfield Tanfbe732e2020-01-24 11:26:14 -0800742 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700743 AMOTION_EVENT_ACTION_POINTER_UP |
744 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700745 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
746 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700747 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500748 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800749 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000750 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000751 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 << "Should reject motion events with pointer up index too small.";
753
754 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800755 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
756 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700757 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700758 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
759 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700760 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800761 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000762 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000763 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764 << "Should reject motion events with 0 pointers.";
765
Garfield Tanfbe732e2020-01-24 11:26:14 -0800766 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
767 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700768 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700769 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
770 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700771 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800772 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000773 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000774 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800775 << "Should reject motion events with more than MAX_POINTERS pointers.";
776
777 // Rejects motion events with invalid pointer ids.
778 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800779 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
780 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700781 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700782 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
783 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700784 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800785 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000786 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000787 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800788 << "Should reject motion events with pointer ids less than 0.";
789
790 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800791 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
792 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700793 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700794 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
795 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700796 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800797 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000798 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000799 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
801
802 // Rejects motion events with duplicate pointer ids.
803 pointerProperties[0].id = 1;
804 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800805 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
806 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700807 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700808 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
809 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700810 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800811 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000812 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000813 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814 << "Should reject motion events with duplicate pointer ids.";
815}
816
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800817/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
818
819TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
820 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000821 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800822 ASSERT_TRUE(mDispatcher->waitForIdle());
823
824 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
825}
826
827TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000828 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
829 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000830 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800831
832 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
833 args.policyFlags |= POLICY_FLAG_TRUSTED;
834 mFakePolicy->assertNotifySwitchWasCalled(args);
835}
836
Arthur Hungb92218b2018-08-14 12:00:21 +0800837// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700838static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700839// Default input dispatching timeout if there is no focused application or paused window
840// from which to determine an appropriate dispatching timeout.
841static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
842 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
843 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800844
845class FakeApplicationHandle : public InputApplicationHandle {
846public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700847 FakeApplicationHandle() {
848 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700849 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500850 mInfo.dispatchingTimeoutMillis =
851 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700852 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800853 virtual ~FakeApplicationHandle() {}
854
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000855 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700856
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500857 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
858 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700859 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800860};
861
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800862class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800863public:
Garfield Tan15601662020-09-22 15:32:38 -0700864 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800865 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700866 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800867 }
868
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800869 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700870 InputEvent* event;
871 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
872 if (!consumeSeq) {
873 return nullptr;
874 }
875 finishEvent(*consumeSeq);
876 return event;
877 }
878
879 /**
880 * Receive an event without acknowledging it.
881 * Return the sequence number that could later be used to send finished signal.
882 */
883 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800884 uint32_t consumeSeq;
885 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800886
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800887 std::chrono::time_point start = std::chrono::steady_clock::now();
888 status_t status = WOULD_BLOCK;
889 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000890 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800891 &event);
892 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
893 if (elapsed > 100ms) {
894 break;
895 }
896 }
897
898 if (status == WOULD_BLOCK) {
899 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700900 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800901 }
902
903 if (status != OK) {
904 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700905 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800906 }
907 if (event == nullptr) {
908 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700909 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800910 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700911 if (outEvent != nullptr) {
912 *outEvent = event;
913 }
914 return consumeSeq;
915 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800916
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700917 /**
918 * To be used together with "receiveEvent" to complete the consumption of an event.
919 */
920 void finishEvent(uint32_t consumeSeq) {
921 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
922 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800923 }
924
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000925 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
926 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
927 ASSERT_EQ(OK, status);
928 }
929
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700930 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000931 std::optional<int32_t> expectedDisplayId,
932 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800933 InputEvent* event = consume();
934
935 ASSERT_NE(nullptr, event) << mName.c_str()
936 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800937 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700938 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
939 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800940
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000941 if (expectedDisplayId.has_value()) {
942 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
943 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800944
Tiger Huang8664f8c2018-10-11 19:14:35 +0800945 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700946 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800947 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
948 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000949 if (expectedFlags.has_value()) {
950 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
951 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800952 break;
953 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700954 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800955 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000956 assertMotionAction(expectedAction, motionEvent.getAction());
957
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000958 if (expectedFlags.has_value()) {
959 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
960 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800961 break;
962 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700963 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100964 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
965 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700966 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -0800967 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
968 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700969 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +0000970 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
971 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700972 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +0800973 FAIL() << "Use 'consumeDragEvent' for DRAG events";
974 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800975 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800976 }
977
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800978 MotionEvent* consumeMotion() {
979 InputEvent* event = consume();
980
981 if (event == nullptr) {
982 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
983 return nullptr;
984 }
985
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700986 if (event->getType() != InputEventType::MOTION) {
987 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800988 return nullptr;
989 }
990 return static_cast<MotionEvent*>(event);
991 }
992
993 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
994 MotionEvent* motionEvent = consumeMotion();
995 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
996 ASSERT_THAT(*motionEvent, matcher);
997 }
998
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100999 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
1000 InputEvent* event = consume();
1001 ASSERT_NE(nullptr, event) << mName.c_str()
1002 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001003 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1004 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001005
1006 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1007 << mName.c_str() << ": event displayId should always be NONE.";
1008
1009 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1010 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001011 }
1012
Prabir Pradhan99987712020-11-10 18:43:05 -08001013 void consumeCaptureEvent(bool hasCapture) {
1014 const InputEvent* event = consume();
1015 ASSERT_NE(nullptr, event) << mName.c_str()
1016 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001017 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1018 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001019
1020 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1021 << mName.c_str() << ": event displayId should always be NONE.";
1022
1023 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1024 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1025 }
1026
arthurhungb89ccb02020-12-30 16:19:01 +08001027 void consumeDragEvent(bool isExiting, float x, float y) {
1028 const InputEvent* event = consume();
1029 ASSERT_NE(nullptr, event) << mName.c_str()
1030 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001031 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001032
1033 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1034 << mName.c_str() << ": event displayId should always be NONE.";
1035
1036 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1037 EXPECT_EQ(isExiting, dragEvent.isExiting());
1038 EXPECT_EQ(x, dragEvent.getX());
1039 EXPECT_EQ(y, dragEvent.getY());
1040 }
1041
Antonio Kantekf16f2832021-09-28 04:39:20 +00001042 void consumeTouchModeEvent(bool inTouchMode) {
1043 const InputEvent* event = consume();
1044 ASSERT_NE(nullptr, event) << mName.c_str()
1045 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001046 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1047 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001048
1049 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1050 << mName.c_str() << ": event displayId should always be NONE.";
1051 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1052 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1053 }
1054
chaviwd1c23182019-12-20 18:44:56 -08001055 void assertNoEvents() {
1056 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001057 if (event == nullptr) {
1058 return;
1059 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001060 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001061 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1062 ADD_FAILURE() << "Received key event "
1063 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001064 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001065 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1066 ADD_FAILURE() << "Received motion event "
1067 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001068 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001069 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1070 ADD_FAILURE() << "Received focus event, hasFocus = "
1071 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001072 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001073 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1074 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1075 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001076 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001077 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1078 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1079 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001080 }
1081 FAIL() << mName.c_str()
1082 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001083 }
1084
1085 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1086
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001087 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1088
chaviwd1c23182019-12-20 18:44:56 -08001089protected:
1090 std::unique_ptr<InputConsumer> mConsumer;
1091 PreallocatedInputEventFactory mEventFactory;
1092
1093 std::string mName;
1094};
1095
chaviw3277faf2021-05-19 16:45:23 -05001096class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001097public:
1098 static const int32_t WIDTH = 600;
1099 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001100
Chris Yea209fde2020-07-22 13:54:51 -07001101 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001102 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001103 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001104 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001105 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001106 base::Result<std::unique_ptr<InputChannel>> channel =
1107 dispatcher->createInputChannel(name);
1108 token = (*channel)->getConnectionToken();
1109 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001110 }
1111
1112 inputApplicationHandle->updateInfo();
1113 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1114
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001115 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001116 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001117 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001118 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001119 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001120 mInfo.frameLeft = 0;
1121 mInfo.frameTop = 0;
1122 mInfo.frameRight = WIDTH;
1123 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001124 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001125 mInfo.globalScaleFactor = 1.0;
1126 mInfo.touchableRegion.clear();
1127 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001128 mInfo.ownerPid = WINDOW_PID;
1129 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001130 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001131 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001132 }
1133
Arthur Hungabbb9d82021-09-01 14:52:30 +00001134 sp<FakeWindowHandle> clone(
1135 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001136 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001137 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001138 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1139 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001140 return handle;
1141 }
1142
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001143 void setTouchable(bool touchable) {
1144 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1145 }
chaviwd1c23182019-12-20 18:44:56 -08001146
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001147 void setFocusable(bool focusable) {
1148 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1149 }
1150
1151 void setVisible(bool visible) {
1152 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1153 }
Vishnu Nair958da932020-08-21 17:12:37 -07001154
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001155 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001156 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001157 }
1158
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001159 void setPaused(bool paused) {
1160 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1161 }
1162
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001163 void setPreventSplitting(bool preventSplitting) {
1164 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001165 }
1166
1167 void setSlippery(bool slippery) {
1168 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1169 }
1170
1171 void setWatchOutsideTouch(bool watchOutside) {
1172 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1173 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001174
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001175 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1176
1177 void setInterceptsStylus(bool interceptsStylus) {
1178 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1179 }
1180
1181 void setDropInput(bool dropInput) {
1182 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1183 }
1184
1185 void setDropInputIfObscured(bool dropInputIfObscured) {
1186 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1187 }
1188
1189 void setNoInputChannel(bool noInputChannel) {
1190 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1191 }
1192
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001193 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1194
chaviw3277faf2021-05-19 16:45:23 -05001195 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001196
Bernardo Rufino7393d172021-02-26 13:56:11 +00001197 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1198
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001199 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001200 mInfo.frameLeft = frame.left;
1201 mInfo.frameTop = frame.top;
1202 mInfo.frameRight = frame.right;
1203 mInfo.frameBottom = frame.bottom;
1204 mInfo.touchableRegion.clear();
1205 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001206
1207 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1208 ui::Transform translate;
1209 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1210 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001211 }
1212
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001213 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1214
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001215 void setIsWallpaper(bool isWallpaper) {
1216 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1217 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001218
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001219 void setDupTouchToWallpaper(bool hasWallpaper) {
1220 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1221 }
chaviwd1c23182019-12-20 18:44:56 -08001222
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001223 void setTrustedOverlay(bool trustedOverlay) {
1224 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1225 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001226
chaviw9eaa22c2020-07-01 16:21:27 -07001227 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1228 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1229 }
1230
1231 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001232
yunho.shinf4a80b82020-11-16 21:13:57 +09001233 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1234
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001235 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001236 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001237 }
1238
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001239 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001240 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001241 }
1242
Svet Ganov5d3bc372020-01-26 23:11:07 -08001243 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001244 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001245 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1246 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001247 }
1248
1249 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001250 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001251 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1252 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001253 }
1254
1255 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001256 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001257 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1258 }
1259
1260 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1261 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001262 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001263 expectedFlags);
1264 }
1265
Svet Ganov5d3bc372020-01-26 23:11:07 -08001266 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001267 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1268 int32_t expectedFlags = 0) {
1269 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1270 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001271 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001272 }
1273
1274 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001275 int32_t expectedFlags = 0) {
1276 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1277 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001278 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001279 }
1280
1281 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001282 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001283 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001284 expectedFlags);
1285 }
1286
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001287 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1288 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001289 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001290 expectedFlags);
1291 }
1292
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001293 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1294 int32_t expectedFlags = 0) {
1295 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001296 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001297 ASSERT_EQ(InputEventType::MOTION, event->getType());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001298 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1299 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1300 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1301 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1302 }
1303
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001304 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1305 ASSERT_NE(mInputReceiver, nullptr)
1306 << "Cannot consume events from a window with no receiver";
1307 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1308 }
1309
Prabir Pradhan99987712020-11-10 18:43:05 -08001310 void consumeCaptureEvent(bool hasCapture) {
1311 ASSERT_NE(mInputReceiver, nullptr)
1312 << "Cannot consume events from a window with no receiver";
1313 mInputReceiver->consumeCaptureEvent(hasCapture);
1314 }
1315
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001316 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1317 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001318 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001319 ASSERT_THAT(*motionEvent, matcher);
1320 }
1321
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001322 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001323 std::optional<int32_t> expectedDisplayId,
1324 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001325 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1326 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1327 expectedFlags);
1328 }
1329
arthurhungb89ccb02020-12-30 16:19:01 +08001330 void consumeDragEvent(bool isExiting, float x, float y) {
1331 mInputReceiver->consumeDragEvent(isExiting, x, y);
1332 }
1333
Antonio Kantekf16f2832021-09-28 04:39:20 +00001334 void consumeTouchModeEvent(bool inTouchMode) {
1335 ASSERT_NE(mInputReceiver, nullptr)
1336 << "Cannot consume events from a window with no receiver";
1337 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1338 }
1339
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001340 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001341 if (mInputReceiver == nullptr) {
1342 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1343 return std::nullopt;
1344 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001345 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001346 }
1347
1348 void finishEvent(uint32_t sequenceNum) {
1349 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1350 mInputReceiver->finishEvent(sequenceNum);
1351 }
1352
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001353 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1354 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1355 mInputReceiver->sendTimeline(inputEventId, timeline);
1356 }
1357
chaviwaf87b3e2019-10-01 16:59:28 -07001358 InputEvent* consume() {
1359 if (mInputReceiver == nullptr) {
1360 return nullptr;
1361 }
1362 return mInputReceiver->consume();
1363 }
1364
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001365 MotionEvent* consumeMotion() {
1366 InputEvent* event = consume();
1367 if (event == nullptr) {
1368 ADD_FAILURE() << "Consume failed : no event";
1369 return nullptr;
1370 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001371 if (event->getType() != InputEventType::MOTION) {
1372 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001373 return nullptr;
1374 }
1375 return static_cast<MotionEvent*>(event);
1376 }
1377
Arthur Hungb92218b2018-08-14 12:00:21 +08001378 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001379 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001380 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001381 return; // Can't receive events if the window does not have input channel
1382 }
1383 ASSERT_NE(nullptr, mInputReceiver)
1384 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001385 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001386 }
1387
chaviwaf87b3e2019-10-01 16:59:28 -07001388 sp<IBinder> getToken() { return mInfo.token; }
1389
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001390 const std::string& getName() { return mName; }
1391
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001392 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1393 mInfo.ownerPid = ownerPid;
1394 mInfo.ownerUid = ownerUid;
1395 }
1396
Prabir Pradhanedd96402022-02-15 01:46:16 -08001397 int32_t getPid() const { return mInfo.ownerPid; }
1398
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001399 void destroyReceiver() { mInputReceiver = nullptr; }
1400
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001401 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1402
chaviwd1c23182019-12-20 18:44:56 -08001403private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001404 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001405 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001406 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001407};
1408
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001409std::atomic<int32_t> FakeWindowHandle::sId{1};
1410
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001411static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001412 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001413 int32_t displayId = ADISPLAY_ID_NONE,
1414 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001415 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001416 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1417 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001418 KeyEvent event;
1419 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1420
1421 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001422 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001423 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1424 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001425
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001426 if (!allowKeyRepeat) {
1427 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1428 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001429 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001430 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001431}
1432
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001433static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001434 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001435 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001436}
1437
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001438// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1439// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1440// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001441static InputEventInjectionResult injectKeyDownNoRepeat(
1442 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001443 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001444 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001445 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001446}
1447
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001448static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001449 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001450 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001451}
1452
Garfield Tandf26e862020-07-01 20:18:19 -07001453class PointerBuilder {
1454public:
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001455 PointerBuilder(int32_t id, ToolType toolType) {
Garfield Tandf26e862020-07-01 20:18:19 -07001456 mProperties.clear();
1457 mProperties.id = id;
1458 mProperties.toolType = toolType;
1459 mCoords.clear();
1460 }
1461
1462 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1463
1464 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1465
1466 PointerBuilder& axis(int32_t axis, float value) {
1467 mCoords.setAxisValue(axis, value);
1468 return *this;
1469 }
1470
1471 PointerProperties buildProperties() const { return mProperties; }
1472
1473 PointerCoords buildCoords() const { return mCoords; }
1474
1475private:
1476 PointerProperties mProperties;
1477 PointerCoords mCoords;
1478};
1479
1480class MotionEventBuilder {
1481public:
1482 MotionEventBuilder(int32_t action, int32_t source) {
1483 mAction = action;
1484 mSource = source;
1485 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001486 mDownTime = mEventTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001487 }
1488
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001489 MotionEventBuilder& deviceId(int32_t deviceId) {
1490 mDeviceId = deviceId;
1491 return *this;
1492 }
1493
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001494 MotionEventBuilder& downTime(nsecs_t downTime) {
1495 mDownTime = downTime;
1496 return *this;
1497 }
1498
Garfield Tandf26e862020-07-01 20:18:19 -07001499 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1500 mEventTime = eventTime;
1501 return *this;
1502 }
1503
1504 MotionEventBuilder& displayId(int32_t displayId) {
1505 mDisplayId = displayId;
1506 return *this;
1507 }
1508
1509 MotionEventBuilder& actionButton(int32_t actionButton) {
1510 mActionButton = actionButton;
1511 return *this;
1512 }
1513
arthurhung6d4bed92021-03-17 11:59:33 +08001514 MotionEventBuilder& buttonState(int32_t buttonState) {
1515 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001516 return *this;
1517 }
1518
1519 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1520 mRawXCursorPosition = rawXCursorPosition;
1521 return *this;
1522 }
1523
1524 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1525 mRawYCursorPosition = rawYCursorPosition;
1526 return *this;
1527 }
1528
1529 MotionEventBuilder& pointer(PointerBuilder pointer) {
1530 mPointers.push_back(pointer);
1531 return *this;
1532 }
1533
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001534 MotionEventBuilder& addFlag(uint32_t flags) {
1535 mFlags |= flags;
1536 return *this;
1537 }
1538
Garfield Tandf26e862020-07-01 20:18:19 -07001539 MotionEvent build() {
1540 std::vector<PointerProperties> pointerProperties;
1541 std::vector<PointerCoords> pointerCoords;
1542 for (const PointerBuilder& pointer : mPointers) {
1543 pointerProperties.push_back(pointer.buildProperties());
1544 pointerCoords.push_back(pointer.buildCoords());
1545 }
1546
1547 // Set mouse cursor position for the most common cases to avoid boilerplate.
1548 if (mSource == AINPUT_SOURCE_MOUSE &&
1549 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1550 mPointers.size() == 1) {
1551 mRawXCursorPosition = pointerCoords[0].getX();
1552 mRawYCursorPosition = pointerCoords[0].getY();
1553 }
1554
1555 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001556 ui::Transform identityTransform;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001557 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001558 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001559 mButtonState, MotionClassification::NONE, identityTransform,
1560 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001561 mRawYCursorPosition, identityTransform, mDownTime, mEventTime,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001562 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001563
1564 return event;
1565 }
1566
1567private:
1568 int32_t mAction;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001569 int32_t mDeviceId = DEVICE_ID;
Garfield Tandf26e862020-07-01 20:18:19 -07001570 int32_t mSource;
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001571 nsecs_t mDownTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001572 nsecs_t mEventTime;
1573 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1574 int32_t mActionButton{0};
1575 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001576 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001577 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1578 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1579
1580 std::vector<PointerBuilder> mPointers;
1581};
1582
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001583class MotionArgsBuilder {
1584public:
1585 MotionArgsBuilder(int32_t action, int32_t source) {
1586 mAction = action;
1587 mSource = source;
1588 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1589 mDownTime = mEventTime;
1590 }
1591
1592 MotionArgsBuilder& deviceId(int32_t deviceId) {
1593 mDeviceId = deviceId;
1594 return *this;
1595 }
1596
1597 MotionArgsBuilder& downTime(nsecs_t downTime) {
1598 mDownTime = downTime;
1599 return *this;
1600 }
1601
1602 MotionArgsBuilder& eventTime(nsecs_t eventTime) {
1603 mEventTime = eventTime;
1604 return *this;
1605 }
1606
1607 MotionArgsBuilder& displayId(int32_t displayId) {
1608 mDisplayId = displayId;
1609 return *this;
1610 }
1611
1612 MotionArgsBuilder& policyFlags(int32_t policyFlags) {
1613 mPolicyFlags = policyFlags;
1614 return *this;
1615 }
1616
1617 MotionArgsBuilder& actionButton(int32_t actionButton) {
1618 mActionButton = actionButton;
1619 return *this;
1620 }
1621
1622 MotionArgsBuilder& buttonState(int32_t buttonState) {
1623 mButtonState = buttonState;
1624 return *this;
1625 }
1626
1627 MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) {
1628 mRawXCursorPosition = rawXCursorPosition;
1629 return *this;
1630 }
1631
1632 MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) {
1633 mRawYCursorPosition = rawYCursorPosition;
1634 return *this;
1635 }
1636
1637 MotionArgsBuilder& pointer(PointerBuilder pointer) {
1638 mPointers.push_back(pointer);
1639 return *this;
1640 }
1641
1642 MotionArgsBuilder& addFlag(uint32_t flags) {
1643 mFlags |= flags;
1644 return *this;
1645 }
1646
1647 NotifyMotionArgs build() {
1648 std::vector<PointerProperties> pointerProperties;
1649 std::vector<PointerCoords> pointerCoords;
1650 for (const PointerBuilder& pointer : mPointers) {
1651 pointerProperties.push_back(pointer.buildProperties());
1652 pointerCoords.push_back(pointer.buildCoords());
1653 }
1654
1655 // Set mouse cursor position for the most common cases to avoid boilerplate.
1656 if (mSource == AINPUT_SOURCE_MOUSE &&
1657 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1658 mPointers.size() == 1) {
1659 mRawXCursorPosition = pointerCoords[0].getX();
1660 mRawYCursorPosition = pointerCoords[0].getY();
1661 }
1662
1663 NotifyMotionArgs args(InputEvent::nextId(), mEventTime, /*readTime=*/mEventTime, mDeviceId,
1664 mSource, mDisplayId, mPolicyFlags, mAction, mActionButton, mFlags,
1665 AMETA_NONE, mButtonState, MotionClassification::NONE, /*edgeFlags=*/0,
1666 mPointers.size(), pointerProperties.data(), pointerCoords.data(),
1667 /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition,
1668 mRawYCursorPosition, mDownTime, /*videoFrames=*/{});
1669
1670 return args;
1671 }
1672
1673private:
1674 int32_t mAction;
1675 int32_t mDeviceId = DEVICE_ID;
1676 int32_t mSource;
1677 nsecs_t mDownTime;
1678 nsecs_t mEventTime;
1679 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1680 int32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
1681 int32_t mActionButton{0};
1682 int32_t mButtonState{0};
1683 int32_t mFlags{0};
1684 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1685 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1686
1687 std::vector<PointerBuilder> mPointers;
1688};
1689
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001690static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001691 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001692 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001693 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1694 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1695 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1696 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001697}
1698
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001699static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001700 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001701 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001702 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001703 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1704 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001705 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001706 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1707 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Garfield Tandf26e862020-07-01 20:18:19 -07001708 MotionEvent event = MotionEventBuilder(action, source)
1709 .displayId(displayId)
1710 .eventTime(eventTime)
1711 .rawXCursorPosition(cursorPosition.x)
1712 .rawYCursorPosition(cursorPosition.y)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001713 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Garfield Tandf26e862020-07-01 20:18:19 -07001714 .x(position.x)
1715 .y(position.y))
1716 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001717
1718 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001719 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid,
1720 policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001721}
1722
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001723static InputEventInjectionResult injectMotionDown(
1724 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1725 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001726 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001727}
1728
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001729static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001730 int32_t source, int32_t displayId,
1731 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001732 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001733}
1734
Jackal Guof9696682018-10-05 12:23:23 +08001735static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1736 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1737 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001738 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001739 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1740 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001741
1742 return args;
1743}
1744
Prabir Pradhan678438e2023-04-13 19:32:51 +00001745[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1746 int32_t displayId,
1747 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001748 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001749 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1750 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1751 }
1752
chaviwd1c23182019-12-20 18:44:56 -08001753 PointerProperties pointerProperties[pointerCount];
1754 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001755
chaviwd1c23182019-12-20 18:44:56 -08001756 for (size_t i = 0; i < pointerCount; i++) {
1757 pointerProperties[i].clear();
1758 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001759 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001760
chaviwd1c23182019-12-20 18:44:56 -08001761 pointerCoords[i].clear();
1762 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1763 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1764 }
Jackal Guof9696682018-10-05 12:23:23 +08001765
1766 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1767 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001768 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001769 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1770 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001771 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1772 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001773 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1774 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001775
1776 return args;
1777}
1778
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001779static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1780 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1781}
1782
chaviwd1c23182019-12-20 18:44:56 -08001783static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1784 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1785}
1786
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001787static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1788 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001789 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001790}
1791
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001792/**
1793 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1794 * broken channel.
1795 */
1796TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1797 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1798 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001799 sp<FakeWindowHandle>::make(application, mDispatcher,
1800 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001801
1802 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1803
1804 // Window closes its channel, but the window remains.
1805 window->destroyReceiver();
1806 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1807}
1808
Arthur Hungb92218b2018-08-14 12:00:21 +08001809TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001810 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001811 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1812 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001813
Arthur Hung72d8dc32020-03-28 00:48:39 +00001814 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001815 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1816 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1817 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001818
1819 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001820 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001821}
1822
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001823TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1824 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001825 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1826 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001827
1828 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1829 // Inject a MotionEvent to an unknown display.
1830 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1831 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1832 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1833
1834 // Window should receive motion event.
1835 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1836}
1837
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001838/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001839 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001840 * This test serves as a sanity check for the next test, where setInputWindows is
1841 * called twice.
1842 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001843TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001844 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001845 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1846 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001847 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001848
1849 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001850 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001851 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1852 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001853 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001854
1855 // Window should receive motion event.
1856 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1857}
1858
1859/**
1860 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001861 */
1862TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001863 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001864 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1865 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001866 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001867
1868 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1869 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001870 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001871 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1872 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001873 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001874
1875 // Window should receive motion event.
1876 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1877}
1878
Arthur Hungb92218b2018-08-14 12:00:21 +08001879// The foreground window should receive the first touch down event.
1880TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001881 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001882 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001883 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001884 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001885 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001886
Arthur Hung72d8dc32020-03-28 00:48:39 +00001887 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001888 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1889 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1890 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001891
1892 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001893 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001894 windowSecond->assertNoEvents();
1895}
1896
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001897/**
1898 * Two windows: A top window, and a wallpaper behind the window.
1899 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1900 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001901 * 1. foregroundWindow <-- dup touch to wallpaper
1902 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001903 */
1904TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1905 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1906 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001907 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001908 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001909 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001910 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001911 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001912
1913 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1914 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1915 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1916 {100, 200}))
1917 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1918
1919 // Both foreground window and its wallpaper should receive the touch down
1920 foregroundWindow->consumeMotionDown();
1921 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1922
1923 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1924 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1925 ADISPLAY_ID_DEFAULT, {110, 200}))
1926 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1927
1928 foregroundWindow->consumeMotionMove();
1929 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1930
1931 // Now the foreground window goes away, but the wallpaper stays
1932 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1933 foregroundWindow->consumeMotionCancel();
1934 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1935 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1936}
1937
1938/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001939 * Two fingers down on the window, and lift off the first finger.
1940 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1941 * contains a single pointer.
1942 */
1943TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1944 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1945 sp<FakeWindowHandle> window =
1946 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1947
1948 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001949 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001950 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1951 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1952 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001953 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001954 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1955 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1956 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1957 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001958 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001959 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1960 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1961 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1962 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001963 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1964 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1965 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1966
1967 // Remove the window. The gesture should be canceled
1968 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
1969 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1970 window->consumeMotionEvent(
1971 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1972}
1973
1974/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001975 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1976 * with the following differences:
1977 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1978 * clean up the connection.
1979 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1980 * Ensure that there's no crash in the dispatcher.
1981 */
1982TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1983 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1984 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001985 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001986 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001987 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001988 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001989 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001990
1991 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1992 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1993 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1994 {100, 200}))
1995 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1996
1997 // Both foreground window and its wallpaper should receive the touch down
1998 foregroundWindow->consumeMotionDown();
1999 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2000
2001 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2002 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2003 ADISPLAY_ID_DEFAULT, {110, 200}))
2004 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2005
2006 foregroundWindow->consumeMotionMove();
2007 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2008
2009 // Wallpaper closes its channel, but the window remains.
2010 wallpaperWindow->destroyReceiver();
2011 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
2012
2013 // Now the foreground window goes away, but the wallpaper stays, even though its channel
2014 // is no longer valid.
2015 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
2016 foregroundWindow->consumeMotionCancel();
2017}
2018
Arthur Hungc539dbb2022-12-08 07:45:36 +00002019class ShouldSplitTouchFixture : public InputDispatcherTest,
2020 public ::testing::WithParamInterface<bool> {};
2021INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
2022 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002023/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002024 * A single window that receives touch (on top), and a wallpaper window underneath it.
2025 * The top window gets a multitouch gesture.
2026 * Ensure that wallpaper gets the same gesture.
2027 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00002028TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002029 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002030 sp<FakeWindowHandle> foregroundWindow =
2031 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
2032 foregroundWindow->setDupTouchToWallpaper(true);
2033 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002034
2035 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002036 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002037 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002038
Arthur Hungc539dbb2022-12-08 07:45:36 +00002039 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002040
2041 // Touch down on top window
2042 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2043 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2044 {100, 100}))
2045 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2046
2047 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00002048 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002049 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2050
2051 // Second finger down on the top window
2052 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002053 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002054 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002055 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2056 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002057 .build();
2058 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2059 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2060 InputEventInjectionSync::WAIT_FOR_RESULT))
2061 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2062
Harry Cutts33476232023-01-30 19:57:29 +00002063 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
2064 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002065 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002066
2067 const MotionEvent secondFingerUpEvent =
2068 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2069 .displayId(ADISPLAY_ID_DEFAULT)
2070 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002071 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2072 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002073 .build();
2074 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2075 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
2076 InputEventInjectionSync::WAIT_FOR_RESULT))
2077 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2078 foregroundWindow->consumeMotionPointerUp(0);
2079 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2080
2081 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002082 injectMotionEvent(mDispatcher,
2083 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2084 AINPUT_SOURCE_TOUCHSCREEN)
2085 .displayId(ADISPLAY_ID_DEFAULT)
2086 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2087 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002088 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002089 .x(100)
2090 .y(100))
2091 .build(),
2092 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002093 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2094 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2095 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002096}
2097
2098/**
2099 * Two windows: a window on the left and window on the right.
2100 * A third window, wallpaper, is behind both windows, and spans both top windows.
2101 * The first touch down goes to the left window. A second pointer touches down on the right window.
2102 * The touch is split, so both left and right windows should receive ACTION_DOWN.
2103 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
2104 * ACTION_POINTER_DOWN(1).
2105 */
2106TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
2107 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2108 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002109 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002110 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002111 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002112
2113 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002114 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002115 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002116 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002117
2118 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002119 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002120 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002121 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002122
2123 mDispatcher->setInputWindows(
2124 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
2125
2126 // Touch down on left window
2127 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2128 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2129 {100, 100}))
2130 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2131
2132 // Both foreground window and its wallpaper should receive the touch down
2133 leftWindow->consumeMotionDown();
2134 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2135
2136 // Second finger down on the right window
2137 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002138 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002139 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002140 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2141 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002142 .build();
2143 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2144 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2145 InputEventInjectionSync::WAIT_FOR_RESULT))
2146 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2147
2148 leftWindow->consumeMotionMove();
2149 // Since the touch is split, right window gets ACTION_DOWN
2150 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00002151 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002152 expectedWallpaperFlags);
2153
2154 // Now, leftWindow, which received the first finger, disappears.
2155 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
2156 leftWindow->consumeMotionCancel();
2157 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2158 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2159
2160 // The pointer that's still down on the right window moves, and goes to the right window only.
2161 // As far as the dispatcher's concerned though, both pointers are still present.
2162 const MotionEvent secondFingerMoveEvent =
2163 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2164 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002165 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2166 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002167 .build();
2168 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2169 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
2170 InputEventInjectionSync::WAIT_FOR_RESULT));
2171 rightWindow->consumeMotionMove();
2172
2173 leftWindow->assertNoEvents();
2174 rightWindow->assertNoEvents();
2175 wallpaperWindow->assertNoEvents();
2176}
2177
Arthur Hungc539dbb2022-12-08 07:45:36 +00002178/**
2179 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2180 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2181 * The right window should receive ACTION_DOWN.
2182 */
2183TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002184 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002185 sp<FakeWindowHandle> leftWindow =
2186 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2187 leftWindow->setFrame(Rect(0, 0, 200, 200));
2188 leftWindow->setDupTouchToWallpaper(true);
2189 leftWindow->setSlippery(true);
2190
2191 sp<FakeWindowHandle> rightWindow =
2192 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2193 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002194
2195 sp<FakeWindowHandle> wallpaperWindow =
2196 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2197 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002198
Arthur Hungc539dbb2022-12-08 07:45:36 +00002199 mDispatcher->setInputWindows(
2200 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002201
Arthur Hungc539dbb2022-12-08 07:45:36 +00002202 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002203 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2204 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002205 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002206 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002207
2208 // Both foreground window and its wallpaper should receive the touch down
2209 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002210 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2211
Arthur Hungc539dbb2022-12-08 07:45:36 +00002212 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002214 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2215 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002216 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2217
Arthur Hungc539dbb2022-12-08 07:45:36 +00002218 leftWindow->consumeMotionCancel();
2219 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2220 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002221}
2222
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002223/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002224 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2225 * interactive, it might stop sending this flag.
2226 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2227 * to have a consistent input stream.
2228 *
2229 * Test procedure:
2230 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2231 * DOWN (new gesture).
2232 *
2233 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2234 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2235 *
2236 * We technically just need a single window here, but we are using two windows (spy on top and a
2237 * regular window below) to emulate the actual situation where it happens on the device.
2238 */
2239TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2240 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2241 sp<FakeWindowHandle> spyWindow =
2242 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2243 spyWindow->setFrame(Rect(0, 0, 200, 200));
2244 spyWindow->setTrustedOverlay(true);
2245 spyWindow->setSpy(true);
2246
2247 sp<FakeWindowHandle> window =
2248 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2249 window->setFrame(Rect(0, 0, 200, 200));
2250
2251 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2252 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002253
2254 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002255 mDispatcher->notifyMotion(
2256 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2257 .deviceId(touchDeviceId)
2258 .policyFlags(DEFAULT_POLICY_FLAGS)
2259 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2260 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002261
Prabir Pradhan678438e2023-04-13 19:32:51 +00002262 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2263 .deviceId(touchDeviceId)
2264 .policyFlags(DEFAULT_POLICY_FLAGS)
2265 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2266 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2267 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002268 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2269 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2270 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2271 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2272
2273 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002274 mDispatcher->notifyMotion(
2275 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2276 .deviceId(touchDeviceId)
2277 .policyFlags(0)
2278 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2279 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2280 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002281 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2282 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2283
2284 // We don't need to reset the device to reproduce the issue, but the reset event typically
2285 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002286 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002287
2288 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002289 mDispatcher->notifyMotion(
2290 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2291 .deviceId(touchDeviceId)
2292 .policyFlags(DEFAULT_POLICY_FLAGS)
2293 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2294 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002295 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2296 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2297
2298 // No more events
2299 spyWindow->assertNoEvents();
2300 window->assertNoEvents();
2301}
2302
2303/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002304 * Two windows: a window on the left and a window on the right.
2305 * Mouse is hovered from the right window into the left window.
2306 * Next, we tap on the left window, where the cursor was last seen.
2307 * The second tap is done onto the right window.
2308 * The mouse and tap are from two different devices.
2309 * We technically don't need to set the downtime / eventtime for these events, but setting these
2310 * explicitly helps during debugging.
2311 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2312 * In the buggy implementation, a tap on the right window would cause a crash.
2313 */
2314TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2315 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2316 sp<FakeWindowHandle> leftWindow =
2317 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2318 leftWindow->setFrame(Rect(0, 0, 200, 200));
2319
2320 sp<FakeWindowHandle> rightWindow =
2321 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2322 rightWindow->setFrame(Rect(200, 0, 400, 200));
2323
2324 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2325 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2326 // stale.
2327 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2328 const int32_t mouseDeviceId = 6;
2329 const int32_t touchDeviceId = 4;
2330 // Move the cursor from right
2331 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2332 injectMotionEvent(mDispatcher,
2333 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2334 AINPUT_SOURCE_MOUSE)
2335 .deviceId(mouseDeviceId)
2336 .downTime(baseTime + 10)
2337 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002338 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002339 .x(300)
2340 .y(100))
2341 .build()));
2342 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2343
2344 // .. to the left window
2345 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2346 injectMotionEvent(mDispatcher,
2347 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2348 AINPUT_SOURCE_MOUSE)
2349 .deviceId(mouseDeviceId)
2350 .downTime(baseTime + 10)
2351 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002352 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002353 .x(110)
2354 .y(100))
2355 .build()));
2356 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2357 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2358 // Now tap the left window
2359 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2360 injectMotionEvent(mDispatcher,
2361 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2362 AINPUT_SOURCE_TOUCHSCREEN)
2363 .deviceId(touchDeviceId)
2364 .downTime(baseTime + 40)
2365 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002366 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002367 .x(100)
2368 .y(100))
2369 .build()));
2370 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2371 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2372
2373 // release tap
2374 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2375 injectMotionEvent(mDispatcher,
2376 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2377 AINPUT_SOURCE_TOUCHSCREEN)
2378 .deviceId(touchDeviceId)
2379 .downTime(baseTime + 40)
2380 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002381 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002382 .x(100)
2383 .y(100))
2384 .build()));
2385 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2386
2387 // Tap the window on the right
2388 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2389 injectMotionEvent(mDispatcher,
2390 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2391 AINPUT_SOURCE_TOUCHSCREEN)
2392 .deviceId(touchDeviceId)
2393 .downTime(baseTime + 60)
2394 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002395 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002396 .x(300)
2397 .y(100))
2398 .build()));
2399 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2400
2401 // release tap
2402 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2403 injectMotionEvent(mDispatcher,
2404 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2405 AINPUT_SOURCE_TOUCHSCREEN)
2406 .deviceId(touchDeviceId)
2407 .downTime(baseTime + 60)
2408 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002409 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002410 .x(300)
2411 .y(100))
2412 .build()));
2413 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2414
2415 // No more events
2416 leftWindow->assertNoEvents();
2417 rightWindow->assertNoEvents();
2418}
2419
2420/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002421 * Two windows: a window on the left and a window on the right.
2422 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2423 * down. Then, on the left window, also place second touch pointer down.
2424 * This test tries to reproduce a crash.
2425 * In the buggy implementation, second pointer down on the left window would cause a crash.
2426 */
2427TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2428 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2429 sp<FakeWindowHandle> leftWindow =
2430 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2431 leftWindow->setFrame(Rect(0, 0, 200, 200));
2432
2433 sp<FakeWindowHandle> rightWindow =
2434 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2435 rightWindow->setFrame(Rect(200, 0, 400, 200));
2436
2437 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2438
2439 const int32_t touchDeviceId = 4;
2440 const int32_t mouseDeviceId = 6;
2441 NotifyMotionArgs args;
2442
2443 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002444 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2445 .deviceId(mouseDeviceId)
2446 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2447 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002448 leftWindow->consumeMotionEvent(
2449 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2450
2451 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002452 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2453 .deviceId(mouseDeviceId)
2454 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2455 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2456 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002457
2458 leftWindow->consumeMotionEvent(
2459 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2460 leftWindow->consumeMotionEvent(
2461 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2462
Prabir Pradhan678438e2023-04-13 19:32:51 +00002463 mDispatcher->notifyMotion(
2464 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2465 .deviceId(mouseDeviceId)
2466 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2467 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2468 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2469 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002470 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2471
2472 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002473 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2474 .deviceId(touchDeviceId)
2475 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2476 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002477 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2478
2479 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2480
2481 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002482 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2483 .deviceId(touchDeviceId)
2484 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2485 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2486 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002487 leftWindow->consumeMotionEvent(
2488 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2489 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2490 // current implementation.
2491 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2492 rightWindow->consumeMotionEvent(
2493 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2494
2495 leftWindow->assertNoEvents();
2496 rightWindow->assertNoEvents();
2497}
2498
2499/**
2500 * On a single window, use two different devices: mouse and touch.
2501 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2502 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2503 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2504 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2505 * represent a new gesture.
2506 */
2507TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2508 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2509 sp<FakeWindowHandle> window =
2510 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2511 window->setFrame(Rect(0, 0, 400, 400));
2512
2513 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2514
2515 const int32_t touchDeviceId = 4;
2516 const int32_t mouseDeviceId = 6;
2517 NotifyMotionArgs args;
2518
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002519 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002520 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2521 .deviceId(touchDeviceId)
2522 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2523 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002524 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002525 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2526 .deviceId(touchDeviceId)
2527 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2528 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2529 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002530 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002531 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2532 .deviceId(touchDeviceId)
2533 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2534 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2535 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002536 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2537 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2538 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2539
2540 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002541 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2542 .deviceId(mouseDeviceId)
2543 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2544 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2545 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002546
2547 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002548 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002549 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2550
Prabir Pradhan678438e2023-04-13 19:32:51 +00002551 mDispatcher->notifyMotion(
2552 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2553 .deviceId(mouseDeviceId)
2554 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2555 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2556 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2557 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002558 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2559
2560 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002561 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2562 .deviceId(touchDeviceId)
2563 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2564 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2565 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002566 // The pointer_down event should be ignored
2567 window->assertNoEvents();
2568}
2569
2570/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002571 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2572 * the injected event.
2573 */
2574TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2575 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2576 sp<FakeWindowHandle> window =
2577 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2578 window->setFrame(Rect(0, 0, 400, 400));
2579
2580 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2581
2582 const int32_t touchDeviceId = 4;
2583 NotifyMotionArgs args;
2584 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2585 // completion.
2586 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2587 injectMotionEvent(mDispatcher,
2588 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2589 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002590 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002591 .x(50)
2592 .y(50))
2593 .build()));
2594 window->consumeMotionEvent(
2595 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2596
2597 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2598 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002599 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2600 .deviceId(touchDeviceId)
2601 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2602 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002603
2604 window->consumeMotionEvent(
2605 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2606 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2607}
2608
2609/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002610 * This test is similar to the test above, but the sequence of injected events is different.
2611 *
2612 * Two windows: a window on the left and a window on the right.
2613 * Mouse is hovered over the left window.
2614 * Next, we tap on the left window, where the cursor was last seen.
2615 *
2616 * After that, we inject one finger down onto the right window, and then a second finger down onto
2617 * the left window.
2618 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2619 * window (first), and then another on the left window (second).
2620 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2621 * In the buggy implementation, second finger down on the left window would cause a crash.
2622 */
2623TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2624 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2625 sp<FakeWindowHandle> leftWindow =
2626 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2627 leftWindow->setFrame(Rect(0, 0, 200, 200));
2628
2629 sp<FakeWindowHandle> rightWindow =
2630 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2631 rightWindow->setFrame(Rect(200, 0, 400, 200));
2632
2633 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2634
2635 const int32_t mouseDeviceId = 6;
2636 const int32_t touchDeviceId = 4;
2637 // Hover over the left window. Keep the cursor there.
2638 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2639 injectMotionEvent(mDispatcher,
2640 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2641 AINPUT_SOURCE_MOUSE)
2642 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002643 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002644 .x(50)
2645 .y(50))
2646 .build()));
2647 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2648
2649 // Tap on left window
2650 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2651 injectMotionEvent(mDispatcher,
2652 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2653 AINPUT_SOURCE_TOUCHSCREEN)
2654 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002655 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002656 .x(100)
2657 .y(100))
2658 .build()));
2659
2660 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2661 injectMotionEvent(mDispatcher,
2662 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2663 AINPUT_SOURCE_TOUCHSCREEN)
2664 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002665 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002666 .x(100)
2667 .y(100))
2668 .build()));
2669 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2670 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2671 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2672
2673 // First finger down on right window
2674 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2675 injectMotionEvent(mDispatcher,
2676 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2677 AINPUT_SOURCE_TOUCHSCREEN)
2678 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002679 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002680 .x(300)
2681 .y(100))
2682 .build()));
2683 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2684
2685 // Second finger down on the left window
2686 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2687 injectMotionEvent(mDispatcher,
2688 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2689 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002690 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002691 .x(300)
2692 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002693 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002694 .x(100)
2695 .y(100))
2696 .build()));
2697 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2698 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2699
2700 // No more events
2701 leftWindow->assertNoEvents();
2702 rightWindow->assertNoEvents();
2703}
2704
2705/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002706 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2707 * While the touch is down, new hover events from the stylus device should be ignored. After the
2708 * touch is gone, stylus hovering should start working again.
2709 */
2710TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2711 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2712 sp<FakeWindowHandle> window =
2713 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2714 window->setFrame(Rect(0, 0, 200, 200));
2715
2716 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2717
2718 const int32_t stylusDeviceId = 5;
2719 const int32_t touchDeviceId = 4;
2720 // Start hovering with stylus
2721 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2722 injectMotionEvent(mDispatcher,
2723 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2724 AINPUT_SOURCE_STYLUS)
2725 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002726 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002727 .x(50)
2728 .y(50))
2729 .build()));
2730 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2731
2732 // Finger down on the window
2733 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2734 injectMotionEvent(mDispatcher,
2735 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2736 AINPUT_SOURCE_TOUCHSCREEN)
2737 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002738 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002739 .x(100)
2740 .y(100))
2741 .build()));
2742 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2743 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2744
2745 // Try to continue hovering with stylus. Since we are already down, injection should fail
2746 ASSERT_EQ(InputEventInjectionResult::FAILED,
2747 injectMotionEvent(mDispatcher,
2748 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2749 AINPUT_SOURCE_STYLUS)
2750 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002751 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002752 .x(50)
2753 .y(50))
2754 .build()));
2755 // No event should be sent. This event should be ignored because a pointer from another device
2756 // is already down.
2757
2758 // Lift up the finger
2759 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2760 injectMotionEvent(mDispatcher,
2761 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2762 AINPUT_SOURCE_TOUCHSCREEN)
2763 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002764 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002765 .x(100)
2766 .y(100))
2767 .build()));
2768 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2769
2770 // Now that the touch is gone, stylus hovering should start working again
2771 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2772 injectMotionEvent(mDispatcher,
2773 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2774 AINPUT_SOURCE_STYLUS)
2775 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002776 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002777 .x(50)
2778 .y(50))
2779 .build()));
2780 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2781 // No more events
2782 window->assertNoEvents();
2783}
2784
2785/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002786 * A spy window above a window with no input channel.
2787 * Start hovering with a stylus device, and then tap with it.
2788 * Ensure spy window receives the entire sequence.
2789 */
2790TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2791 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2792 sp<FakeWindowHandle> spyWindow =
2793 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2794 spyWindow->setFrame(Rect(0, 0, 200, 200));
2795 spyWindow->setTrustedOverlay(true);
2796 spyWindow->setSpy(true);
2797 sp<FakeWindowHandle> window =
2798 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2799 window->setNoInputChannel(true);
2800 window->setFrame(Rect(0, 0, 200, 200));
2801
2802 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2803
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002804 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002805 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2806 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2807 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002808 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2809 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002810 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2811 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2812 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002813 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2814
2815 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002816 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2817 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2818 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002819 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2820
2821 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002822 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
2823 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2824 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002825 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2826
2827 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002828 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2829 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2830 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002831 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2832 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002833 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2834 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2835 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002836 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2837
2838 // No more events
2839 spyWindow->assertNoEvents();
2840 window->assertNoEvents();
2841}
2842
2843/**
2844 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
2845 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
2846 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
2847 * While the mouse is down, new move events from the touch device should be ignored.
2848 */
2849TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
2850 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2851 sp<FakeWindowHandle> spyWindow =
2852 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2853 spyWindow->setFrame(Rect(0, 0, 200, 200));
2854 spyWindow->setTrustedOverlay(true);
2855 spyWindow->setSpy(true);
2856 sp<FakeWindowHandle> window =
2857 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2858 window->setFrame(Rect(0, 0, 200, 200));
2859
2860 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2861
2862 const int32_t mouseDeviceId = 7;
2863 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002864
2865 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00002866 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2867 .deviceId(mouseDeviceId)
2868 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2869 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002870 spyWindow->consumeMotionEvent(
2871 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2872 window->consumeMotionEvent(
2873 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2874
2875 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00002876 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2877 .deviceId(touchDeviceId)
2878 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
2879 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002880 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2881 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2882 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2883 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2884
Prabir Pradhan678438e2023-04-13 19:32:51 +00002885 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2886 .deviceId(touchDeviceId)
2887 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
2888 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002889 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2890 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2891
2892 // Pilfer the stream
2893 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2894 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2895
Prabir Pradhan678438e2023-04-13 19:32:51 +00002896 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2897 .deviceId(touchDeviceId)
2898 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
2899 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002900 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2901
2902 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002903 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2904 .deviceId(mouseDeviceId)
2905 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2906 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2907 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002908
2909 spyWindow->consumeMotionEvent(
2910 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2911 spyWindow->consumeMotionEvent(
2912 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2913 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2914
Prabir Pradhan678438e2023-04-13 19:32:51 +00002915 mDispatcher->notifyMotion(
2916 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2917 .deviceId(mouseDeviceId)
2918 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2919 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2920 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2921 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002922 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2923 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2924
2925 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002926 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
2927 .deviceId(mouseDeviceId)
2928 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2929 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
2930 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002931 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2932 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2933
2934 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002935 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2936 .deviceId(touchDeviceId)
2937 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
2938 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002939
2940 // No more events
2941 spyWindow->assertNoEvents();
2942 window->assertNoEvents();
2943}
2944
2945/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002946 * On the display, have a single window, and also an area where there's no window.
2947 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2948 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2949 */
2950TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2951 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2952 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002953 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002954
2955 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002956
2957 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00002958 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002959
2960 mDispatcher->waitForIdle();
2961 window->assertNoEvents();
2962
2963 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002964 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002965 mDispatcher->waitForIdle();
2966 window->consumeMotionDown();
2967}
2968
2969/**
2970 * Same test as above, but instead of touching the empty space, the first touch goes to
2971 * non-touchable window.
2972 */
2973TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2974 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2975 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002976 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002977 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2978 window1->setTouchable(false);
2979 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002980 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002981 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2982
2983 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2984
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002985 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002986 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002987
2988 mDispatcher->waitForIdle();
2989 window1->assertNoEvents();
2990 window2->assertNoEvents();
2991
2992 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002993 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002994 mDispatcher->waitForIdle();
2995 window2->consumeMotionDown();
2996}
2997
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002998/**
2999 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
3000 * to the event time of the first ACTION_DOWN sent to the particular window.
3001 */
3002TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
3003 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3004 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003005 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003006 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3007 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003008 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003009 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3010
3011 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
3012
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003013 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003014 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003015
3016 mDispatcher->waitForIdle();
3017 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003018 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003019 window2->assertNoEvents();
3020 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
3021 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
3022 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
3023
3024 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003025 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003026 mDispatcher->waitForIdle();
3027 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003028 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003029 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
3030 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
3031 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
3032 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
3033
3034 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003035 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003036 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003037 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003038
3039 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003040 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003041 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003042 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003043
3044 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
3045 window1->consumeMotionMove();
3046 window1->assertNoEvents();
3047
3048 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003049 mDispatcher->notifyMotion(
3050 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003051 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003052 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003053
Prabir Pradhan678438e2023-04-13 19:32:51 +00003054 mDispatcher->notifyMotion(
3055 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003056 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003057 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003058}
3059
Garfield Tandf26e862020-07-01 20:18:19 -07003060TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003061 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003062 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003063 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003064 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003065 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003066 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003067 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003068
3069 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3070
3071 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
3072
3073 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003074 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003075 injectMotionEvent(mDispatcher,
3076 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3077 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003078 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003079 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003080 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003081
3082 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003083 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003084 injectMotionEvent(mDispatcher,
3085 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3086 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003087 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003088 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003089 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3090 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003091
3092 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003093 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003094 injectMotionEvent(mDispatcher,
3095 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3096 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003097 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003098 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003099 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3100 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003101
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003102 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003103 injectMotionEvent(mDispatcher,
3104 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3105 AINPUT_SOURCE_MOUSE)
3106 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3107 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003108 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003109 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003110 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003111
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003112 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003113 injectMotionEvent(mDispatcher,
3114 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3115 AINPUT_SOURCE_MOUSE)
3116 .buttonState(0)
3117 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003118 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003119 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003120 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003121
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003122 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003123 injectMotionEvent(mDispatcher,
3124 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3125 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003126 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003127 .build()));
3128 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3129
3130 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003131 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003132 injectMotionEvent(mDispatcher,
3133 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3134 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003135 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003136 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003137 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003138
3139 // No more events
3140 windowLeft->assertNoEvents();
3141 windowRight->assertNoEvents();
3142}
3143
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003144/**
3145 * Put two fingers down (and don't release them) and click the mouse button.
3146 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3147 * currently active gesture should be canceled, and the new one should proceed.
3148 */
3149TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3150 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3151 sp<FakeWindowHandle> window =
3152 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3153 window->setFrame(Rect(0, 0, 600, 800));
3154
3155 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3156
3157 const int32_t touchDeviceId = 4;
3158 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003159
3160 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003161 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3162 .deviceId(touchDeviceId)
3163 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3164 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003165
Prabir Pradhan678438e2023-04-13 19:32:51 +00003166 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3167 .deviceId(touchDeviceId)
3168 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3169 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3170 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003171 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3172 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3173
3174 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003175 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3176 .deviceId(mouseDeviceId)
3177 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3178 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3179 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003180 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3181 WithPointerCount(2u)));
3182 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3183
Prabir Pradhan678438e2023-04-13 19:32:51 +00003184 mDispatcher->notifyMotion(
3185 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3186 .deviceId(mouseDeviceId)
3187 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3188 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3189 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3190 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003191 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3192
3193 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3194 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003195 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3196 .deviceId(touchDeviceId)
3197 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3198 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3199 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003200 window->assertNoEvents();
3201}
3202
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003203TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3204 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3205
3206 sp<FakeWindowHandle> spyWindow =
3207 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3208 spyWindow->setFrame(Rect(0, 0, 600, 800));
3209 spyWindow->setTrustedOverlay(true);
3210 spyWindow->setSpy(true);
3211 sp<FakeWindowHandle> window =
3212 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3213 window->setFrame(Rect(0, 0, 600, 800));
3214
3215 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3216 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3217
3218 // Send mouse cursor to the window
3219 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3220 injectMotionEvent(mDispatcher,
3221 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3222 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003223 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003224 .x(100)
3225 .y(100))
3226 .build()));
3227
3228 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3229 WithSource(AINPUT_SOURCE_MOUSE)));
3230 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3231 WithSource(AINPUT_SOURCE_MOUSE)));
3232
3233 window->assertNoEvents();
3234 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003235}
3236
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003237TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3238 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3239
3240 sp<FakeWindowHandle> spyWindow =
3241 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3242 spyWindow->setFrame(Rect(0, 0, 600, 800));
3243 spyWindow->setTrustedOverlay(true);
3244 spyWindow->setSpy(true);
3245 sp<FakeWindowHandle> window =
3246 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3247 window->setFrame(Rect(0, 0, 600, 800));
3248
3249 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3250 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3251
3252 // Send mouse cursor to the window
3253 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3254 injectMotionEvent(mDispatcher,
3255 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3256 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003257 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003258 .x(100)
3259 .y(100))
3260 .build()));
3261
3262 // Move mouse cursor
3263 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3264 injectMotionEvent(mDispatcher,
3265 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3266 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003267 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003268 .x(110)
3269 .y(110))
3270 .build()));
3271
3272 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3273 WithSource(AINPUT_SOURCE_MOUSE)));
3274 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3275 WithSource(AINPUT_SOURCE_MOUSE)));
3276 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3277 WithSource(AINPUT_SOURCE_MOUSE)));
3278 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3279 WithSource(AINPUT_SOURCE_MOUSE)));
3280 // Touch down on the window
3281 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3282 injectMotionEvent(mDispatcher,
3283 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3284 AINPUT_SOURCE_TOUCHSCREEN)
3285 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003286 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003287 .x(200)
3288 .y(200))
3289 .build()));
3290 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3291 WithSource(AINPUT_SOURCE_MOUSE)));
3292 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3293 WithSource(AINPUT_SOURCE_MOUSE)));
3294 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3295 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3296 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3297 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3298
3299 // pilfer the motion, retaining the gesture on the spy window.
3300 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3301 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3302 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3303
3304 // Touch UP on the window
3305 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3306 injectMotionEvent(mDispatcher,
3307 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3308 AINPUT_SOURCE_TOUCHSCREEN)
3309 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003310 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003311 .x(200)
3312 .y(200))
3313 .build()));
3314 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3315 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3316
3317 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3318 // to send a new gesture. It should again go to both windows (spy and the window below), just
3319 // like the first gesture did, before pilfering. The window configuration has not changed.
3320
3321 // One more tap - DOWN
3322 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3323 injectMotionEvent(mDispatcher,
3324 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3325 AINPUT_SOURCE_TOUCHSCREEN)
3326 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003327 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003328 .x(250)
3329 .y(250))
3330 .build()));
3331 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3332 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3333 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3334 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3335
3336 // Touch UP on the window
3337 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3338 injectMotionEvent(mDispatcher,
3339 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3340 AINPUT_SOURCE_TOUCHSCREEN)
3341 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003342 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003343 .x(250)
3344 .y(250))
3345 .build()));
3346 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3347 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3348 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3349 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3350
3351 window->assertNoEvents();
3352 spyWindow->assertNoEvents();
3353}
3354
Garfield Tandf26e862020-07-01 20:18:19 -07003355// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3356// directly in this test.
3357TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003358 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003359 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003360 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003361 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003362
3363 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3364
3365 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3366
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003367 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003368 injectMotionEvent(mDispatcher,
3369 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3370 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003371 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003372 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003373 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003374 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003375 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003376 injectMotionEvent(mDispatcher,
3377 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3378 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003379 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003380 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003381 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3382 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003383
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003384 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003385 injectMotionEvent(mDispatcher,
3386 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3387 AINPUT_SOURCE_MOUSE)
3388 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3389 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003390 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003391 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003392 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003393
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003394 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003395 injectMotionEvent(mDispatcher,
3396 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3397 AINPUT_SOURCE_MOUSE)
3398 .buttonState(0)
3399 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003400 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003401 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003402 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003403
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003404 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003405 injectMotionEvent(mDispatcher,
3406 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3407 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003408 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003409 .build()));
3410 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3411
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003412 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003413 injectMotionEvent(mDispatcher,
3414 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3415 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003416 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003417 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003418 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003419}
3420
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003421/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003422 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3423 * is generated.
3424 */
3425TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3426 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3427 sp<FakeWindowHandle> window =
3428 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3429 window->setFrame(Rect(0, 0, 1200, 800));
3430
3431 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3432
3433 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3434
3435 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3436 injectMotionEvent(mDispatcher,
3437 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3438 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003439 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003440 .x(300)
3441 .y(400))
3442 .build()));
3443 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3444
3445 // Remove the window, but keep the channel.
3446 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3447 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3448}
3449
3450/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003451 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3452 */
3453TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3454 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3455 sp<FakeWindowHandle> window =
3456 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3457 window->setFrame(Rect(0, 0, 100, 100));
3458
3459 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3460
3461 const int32_t mouseDeviceId = 7;
3462 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003463
3464 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003465 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3466 .deviceId(mouseDeviceId)
3467 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3468 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003469 window->consumeMotionEvent(
3470 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3471
3472 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003473 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3474 .deviceId(touchDeviceId)
3475 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3476 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003477
3478 window->consumeMotionEvent(
3479 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3480 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3481}
3482
3483/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003484 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003485 * The tap causes a HOVER_EXIT event to be generated because the current event
3486 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003487 */
3488TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3489 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3490 sp<FakeWindowHandle> window =
3491 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3492 window->setFrame(Rect(0, 0, 100, 100));
3493
3494 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3495
3496 // Inject a hover_move from mouse.
3497 NotifyMotionArgs motionArgs =
3498 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3499 ADISPLAY_ID_DEFAULT, {{50, 50}});
3500 motionArgs.xCursorPosition = 50;
3501 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003502 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003503 ASSERT_NO_FATAL_FAILURE(
3504 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3505 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003506
3507 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003508 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3509 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3510 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003511 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003512 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3513 WithSource(AINPUT_SOURCE_MOUSE))));
3514
3515 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003516 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3517 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3518
Prabir Pradhan678438e2023-04-13 19:32:51 +00003519 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3520 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003521 ASSERT_NO_FATAL_FAILURE(
3522 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3523 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3524}
3525
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003526TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3527 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3528 sp<FakeWindowHandle> windowDefaultDisplay =
3529 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3530 ADISPLAY_ID_DEFAULT);
3531 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3532 sp<FakeWindowHandle> windowSecondDisplay =
3533 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3534 SECOND_DISPLAY_ID);
3535 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3536
3537 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3538 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3539
3540 // Set cursor position in window in default display and check that hover enter and move
3541 // events are generated.
3542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3543 injectMotionEvent(mDispatcher,
3544 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3545 AINPUT_SOURCE_MOUSE)
3546 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003547 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003548 .x(300)
3549 .y(600))
3550 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003551 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003552
3553 // Remove all windows in secondary display and check that no event happens on window in
3554 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003555 mDispatcher->setInputWindows(
3556 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003557 windowDefaultDisplay->assertNoEvents();
3558
3559 // Move cursor position in window in default display and check that only hover move
3560 // event is generated and not hover enter event.
3561 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3562 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3563 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3564 injectMotionEvent(mDispatcher,
3565 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3566 AINPUT_SOURCE_MOUSE)
3567 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003568 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003569 .x(400)
3570 .y(700))
3571 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003572 windowDefaultDisplay->consumeMotionEvent(
3573 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3574 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003575 windowDefaultDisplay->assertNoEvents();
3576}
3577
Garfield Tan00f511d2019-06-12 16:55:40 -07003578TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003579 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003580
3581 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003582 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003583 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003584 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003585 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003586 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003587
3588 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3589
Arthur Hung72d8dc32020-03-28 00:48:39 +00003590 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003591
3592 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3593 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003594 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003595 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003596 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003597 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003598 windowRight->assertNoEvents();
3599}
3600
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003601TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003602 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003603 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3604 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003605 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003606
Arthur Hung72d8dc32020-03-28 00:48:39 +00003607 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003608 setFocusedWindow(window);
3609
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003610 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003611
Prabir Pradhan678438e2023-04-13 19:32:51 +00003612 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003613
3614 // Window should receive key down event.
3615 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3616
3617 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3618 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003619 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003620 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003621 AKEY_EVENT_FLAG_CANCELED);
3622}
3623
3624TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003625 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003626 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3627 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003628
Arthur Hung72d8dc32020-03-28 00:48:39 +00003629 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003630
Prabir Pradhan678438e2023-04-13 19:32:51 +00003631 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3632 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003633
3634 // Window should receive motion down event.
3635 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3636
3637 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3638 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003639 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003640 window->consumeMotionEvent(
3641 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003642}
3643
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003644TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3645 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003646 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3647 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003648 window->setFocusable(true);
3649
3650 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3651 setFocusedWindow(window);
3652
3653 window->consumeFocusEvent(true);
3654
Prabir Pradhan678438e2023-04-13 19:32:51 +00003655 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003656 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3657 const nsecs_t injectTime = keyArgs.eventTime;
3658 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003659 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003660 // The dispatching time should be always greater than or equal to intercept key timeout.
3661 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3662 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3663 std::chrono::nanoseconds(interceptKeyTimeout).count());
3664}
3665
3666TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3667 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003668 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3669 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003670 window->setFocusable(true);
3671
3672 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3673 setFocusedWindow(window);
3674
3675 window->consumeFocusEvent(true);
3676
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003677 mFakePolicy->setInterceptKeyTimeout(150ms);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003678 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
3679 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003680
3681 // Window should receive key event immediately when same key up.
3682 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3683 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3684}
3685
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003686/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003687 * Two windows. First is a regular window. Second does not overlap with the first, and has
3688 * WATCH_OUTSIDE_TOUCH.
3689 * Both windows are owned by the same UID.
3690 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3691 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3692 */
3693TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3694 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3695 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3696 "First Window", ADISPLAY_ID_DEFAULT);
3697 window->setFrame(Rect{0, 0, 100, 100});
3698
3699 sp<FakeWindowHandle> outsideWindow =
3700 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3701 ADISPLAY_ID_DEFAULT);
3702 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3703 outsideWindow->setWatchOutsideTouch(true);
3704 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3705 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3706
3707 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003708 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3709 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3710 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003711 window->consumeMotionDown();
3712 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3713 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3714 outsideWindow->consumeMotionEvent(
3715 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3716}
3717
3718/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003719 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3720 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3721 * ACTION_OUTSIDE event is sent per gesture.
3722 */
3723TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3724 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3725 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003726 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3727 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003728 window->setWatchOutsideTouch(true);
3729 window->setFrame(Rect{0, 0, 100, 100});
3730 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003731 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3732 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003733 secondWindow->setFrame(Rect{100, 100, 200, 200});
3734 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003735 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3736 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003737 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3738 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3739
3740 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003741 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3742 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3743 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003744 window->assertNoEvents();
3745 secondWindow->assertNoEvents();
3746
3747 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3748 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003749 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3750 ADISPLAY_ID_DEFAULT,
3751 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003752 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3753 window->consumeMotionEvent(
3754 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003755 secondWindow->consumeMotionDown();
3756 thirdWindow->assertNoEvents();
3757
3758 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3759 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003760 mDispatcher->notifyMotion(
3761 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3762 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003763 window->assertNoEvents();
3764 secondWindow->consumeMotionMove();
3765 thirdWindow->consumeMotionDown();
3766}
3767
Prabir Pradhan814fe082022-07-22 20:22:18 +00003768TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3769 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003770 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3771 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003772 window->setFocusable(true);
3773
3774 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
3775 setFocusedWindow(window);
3776
3777 window->consumeFocusEvent(true);
3778
Prabir Pradhan678438e2023-04-13 19:32:51 +00003779 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3780 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3781 mDispatcher->notifyKey(keyDown);
3782 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003783
3784 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3785 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3786
3787 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
3788 mDispatcher->onWindowInfosChanged({}, {});
3789
3790 window->consumeFocusEvent(false);
3791
Prabir Pradhan678438e2023-04-13 19:32:51 +00003792 mDispatcher->notifyKey(keyDown);
3793 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003794 window->assertNoEvents();
3795}
3796
Arthur Hung96483742022-11-15 03:30:48 +00003797TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3798 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3799 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3800 "Fake Window", ADISPLAY_ID_DEFAULT);
3801 // Ensure window is non-split and have some transform.
3802 window->setPreventSplitting(true);
3803 window->setWindowOffset(20, 40);
3804 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
3805
3806 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3807 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3808 {50, 50}))
3809 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3810 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3811
3812 const MotionEvent secondFingerDownEvent =
3813 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3814 .displayId(ADISPLAY_ID_DEFAULT)
3815 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003816 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3817 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003818 .build();
3819 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3820 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3821 InputEventInjectionSync::WAIT_FOR_RESULT))
3822 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3823
3824 const MotionEvent* event = window->consumeMotion();
3825 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3826 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3827 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3828 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3829 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3830}
3831
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003832/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003833 * Ensure the correct coordinate spaces are used by InputDispatcher.
3834 *
3835 * InputDispatcher works in the display space, so its coordinate system is relative to the display
3836 * panel. Windows get events in the window space, and get raw coordinates in the logical display
3837 * space.
3838 */
3839class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
3840public:
3841 void SetUp() override {
3842 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003843 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003844 }
3845
3846 void addDisplayInfo(int displayId, const ui::Transform& transform) {
3847 gui::DisplayInfo info;
3848 info.displayId = displayId;
3849 info.transform = transform;
3850 mDisplayInfos.push_back(std::move(info));
3851 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
3852 }
3853
3854 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
3855 mWindowInfos.push_back(*windowHandle->getInfo());
3856 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
3857 }
3858
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003859 void removeAllWindowsAndDisplays() {
3860 mDisplayInfos.clear();
3861 mWindowInfos.clear();
3862 }
3863
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003864 // Set up a test scenario where the display has a scaled projection and there are two windows
3865 // on the display.
3866 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
3867 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
3868 // respectively.
3869 ui::Transform displayTransform;
3870 displayTransform.set(2, 0, 0, 4);
3871 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3872
3873 std::shared_ptr<FakeApplicationHandle> application =
3874 std::make_shared<FakeApplicationHandle>();
3875
3876 // Add two windows to the display. Their frames are represented in the display space.
3877 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003878 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3879 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003880 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
3881 addWindow(firstWindow);
3882
3883 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003884 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3885 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003886 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
3887 addWindow(secondWindow);
3888 return {std::move(firstWindow), std::move(secondWindow)};
3889 }
3890
3891private:
3892 std::vector<gui::DisplayInfo> mDisplayInfos;
3893 std::vector<gui::WindowInfo> mWindowInfos;
3894};
3895
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003896TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003897 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3898 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003899 // selected so that if the hit test was performed with the point and the bounds being in
3900 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003901 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3902 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3903 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003904
3905 firstWindow->consumeMotionDown();
3906 secondWindow->assertNoEvents();
3907}
3908
3909// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
3910// the event should be treated as being in the logical display space.
3911TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
3912 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3913 // Send down to the first window. The point is represented in the logical display space. The
3914 // point is selected so that if the hit test was done in logical display space, then it would
3915 // end up in the incorrect window.
3916 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3917 PointF{75 * 2, 55 * 4});
3918
3919 firstWindow->consumeMotionDown();
3920 secondWindow->assertNoEvents();
3921}
3922
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003923// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
3924// event should be treated as being in the logical display space.
3925TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
3926 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3927
3928 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
3929 ui::Transform injectedEventTransform;
3930 injectedEventTransform.set(matrix);
3931 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
3932 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
3933
3934 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3935 .displayId(ADISPLAY_ID_DEFAULT)
3936 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003937 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003938 .x(untransformedPoint.x)
3939 .y(untransformedPoint.y))
3940 .build();
3941 event.transform(matrix);
3942
3943 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
3944 InputEventInjectionSync::WAIT_FOR_RESULT);
3945
3946 firstWindow->consumeMotionDown();
3947 secondWindow->assertNoEvents();
3948}
3949
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003950TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
3951 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3952
3953 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003954 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3955 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3956 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003957
3958 firstWindow->assertNoEvents();
3959 const MotionEvent* event = secondWindow->consumeMotion();
3960 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
3961
3962 // Ensure that the events from the "getRaw" API are in logical display coordinates.
3963 EXPECT_EQ(300, event->getRawX(0));
3964 EXPECT_EQ(880, event->getRawY(0));
3965
3966 // Ensure that the x and y values are in the window's coordinate space.
3967 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
3968 // the logical display space. This will be the origin of the window space.
3969 EXPECT_EQ(100, event->getX(0));
3970 EXPECT_EQ(80, event->getY(0));
3971}
3972
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003973/** Ensure consistent behavior of InputDispatcher in all orientations. */
3974class InputDispatcherDisplayOrientationFixture
3975 : public InputDispatcherDisplayProjectionTest,
3976 public ::testing::WithParamInterface<ui::Rotation> {};
3977
3978// This test verifies the touchable region of a window for all rotations of the display by tapping
3979// in different locations on the display, specifically points close to the four corners of a
3980// window.
3981TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
3982 constexpr static int32_t displayWidth = 400;
3983 constexpr static int32_t displayHeight = 800;
3984
3985 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3986
3987 const auto rotation = GetParam();
3988
3989 // Set up the display with the specified rotation.
3990 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
3991 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
3992 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
3993 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
3994 logicalDisplayWidth, logicalDisplayHeight);
3995 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3996
3997 // Create a window with its bounds determined in the logical display.
3998 const Rect frameInLogicalDisplay(100, 100, 200, 300);
3999 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4000 sp<FakeWindowHandle> window =
4001 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4002 window->setFrame(frameInDisplay, displayTransform);
4003 addWindow(window);
4004
4005 // The following points in logical display space should be inside the window.
4006 static const std::array<vec2, 4> insidePoints{
4007 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4008 for (const auto pointInsideWindow : insidePoints) {
4009 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4010 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004011 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4012 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4013 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004014 window->consumeMotionDown();
4015
Prabir Pradhan678438e2023-04-13 19:32:51 +00004016 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4017 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4018 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004019 window->consumeMotionUp();
4020 }
4021
4022 // The following points in logical display space should be outside the window.
4023 static const std::array<vec2, 5> outsidePoints{
4024 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4025 for (const auto pointOutsideWindow : outsidePoints) {
4026 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4027 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004028 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4029 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4030 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004031
Prabir Pradhan678438e2023-04-13 19:32:51 +00004032 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4033 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4034 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004035 }
4036 window->assertNoEvents();
4037}
4038
4039// Run the precision tests for all rotations.
4040INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4041 InputDispatcherDisplayOrientationFixture,
4042 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4043 ui::ROTATION_270),
4044 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4045 return ftl::enum_string(testParamInfo.param);
4046 });
4047
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004048using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4049 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004050
4051class TransferTouchFixture : public InputDispatcherTest,
4052 public ::testing::WithParamInterface<TransferFunction> {};
4053
4054TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004055 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004056
4057 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004058 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004059 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4060 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004061 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004062 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004063 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4064 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004065 sp<FakeWindowHandle> wallpaper =
4066 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4067 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004068 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004069 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004070
4071 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004072 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4073 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004074
Svet Ganov5d3bc372020-01-26 23:11:07 -08004075 // Only the first window should get the down event
4076 firstWindow->consumeMotionDown();
4077 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004078 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004079
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004080 // Transfer touch to the second window
4081 TransferFunction f = GetParam();
4082 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4083 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004084 // The first window gets cancel and the second gets down
4085 firstWindow->consumeMotionCancel();
4086 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004087 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004088
4089 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004090 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4091 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004092 // The first window gets no events and the second gets up
4093 firstWindow->assertNoEvents();
4094 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004095 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004096}
4097
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004098/**
4099 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4100 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4101 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4102 * natural to the user.
4103 * In this test, we are sending a pointer to both spy window and first window. We then try to
4104 * transfer touch to the second window. The dispatcher should identify the first window as the
4105 * one that should lose the gesture, and therefore the action should be to move the gesture from
4106 * the first window to the second.
4107 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4108 * the other API, as well.
4109 */
4110TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4111 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4112
4113 // Create a couple of windows + a spy window
4114 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004115 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004116 spyWindow->setTrustedOverlay(true);
4117 spyWindow->setSpy(true);
4118 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004119 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004120 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004121 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004122
4123 // Add the windows to the dispatcher
4124 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4125
4126 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004127 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4128 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004129 // Only the first window and spy should get the down event
4130 spyWindow->consumeMotionDown();
4131 firstWindow->consumeMotionDown();
4132
4133 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4134 // if f === 'transferTouch'.
4135 TransferFunction f = GetParam();
4136 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4137 ASSERT_TRUE(success);
4138 // The first window gets cancel and the second gets down
4139 firstWindow->consumeMotionCancel();
4140 secondWindow->consumeMotionDown();
4141
4142 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004143 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4144 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004145 // The first window gets no events and the second+spy get up
4146 firstWindow->assertNoEvents();
4147 spyWindow->consumeMotionUp();
4148 secondWindow->consumeMotionUp();
4149}
4150
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004151TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004152 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004153
4154 PointF touchPoint = {10, 10};
4155
4156 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004157 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004158 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4159 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004160 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004161 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004162 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4163 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004164 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004165
4166 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004167 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004168
4169 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004170 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4171 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4172 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004173 // Only the first window should get the down event
4174 firstWindow->consumeMotionDown();
4175 secondWindow->assertNoEvents();
4176
4177 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004178 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4179 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004180 // Only the first window should get the pointer down event
4181 firstWindow->consumeMotionPointerDown(1);
4182 secondWindow->assertNoEvents();
4183
4184 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004185 TransferFunction f = GetParam();
4186 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4187 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004188 // The first window gets cancel and the second gets down and pointer down
4189 firstWindow->consumeMotionCancel();
4190 secondWindow->consumeMotionDown();
4191 secondWindow->consumeMotionPointerDown(1);
4192
4193 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004194 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4195 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004196 // The first window gets nothing and the second gets pointer up
4197 firstWindow->assertNoEvents();
4198 secondWindow->consumeMotionPointerUp(1);
4199
4200 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004201 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4202 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004203 // The first window gets nothing and the second gets up
4204 firstWindow->assertNoEvents();
4205 secondWindow->consumeMotionUp();
4206}
4207
Arthur Hungc539dbb2022-12-08 07:45:36 +00004208TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4209 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4210
4211 // Create a couple of windows
4212 sp<FakeWindowHandle> firstWindow =
4213 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4214 ADISPLAY_ID_DEFAULT);
4215 firstWindow->setDupTouchToWallpaper(true);
4216 sp<FakeWindowHandle> secondWindow =
4217 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4218 ADISPLAY_ID_DEFAULT);
4219 secondWindow->setDupTouchToWallpaper(true);
4220
4221 sp<FakeWindowHandle> wallpaper1 =
4222 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4223 wallpaper1->setIsWallpaper(true);
4224
4225 sp<FakeWindowHandle> wallpaper2 =
4226 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4227 wallpaper2->setIsWallpaper(true);
4228 // Add the windows to the dispatcher
4229 mDispatcher->setInputWindows(
4230 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4231
4232 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004233 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4234 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004235
4236 // Only the first window should get the down event
4237 firstWindow->consumeMotionDown();
4238 secondWindow->assertNoEvents();
4239 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4240 wallpaper2->assertNoEvents();
4241
4242 // Transfer touch focus to the second window
4243 TransferFunction f = GetParam();
4244 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4245 ASSERT_TRUE(success);
4246
4247 // The first window gets cancel and the second gets down
4248 firstWindow->consumeMotionCancel();
4249 secondWindow->consumeMotionDown();
4250 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4251 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4252
4253 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004254 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4255 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004256 // The first window gets no events and the second gets up
4257 firstWindow->assertNoEvents();
4258 secondWindow->consumeMotionUp();
4259 wallpaper1->assertNoEvents();
4260 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4261}
4262
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004263// For the cases of single pointer touch and two pointers non-split touch, the api's
4264// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4265// for the case where there are multiple pointers split across several windows.
4266INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4267 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004268 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4269 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004270 return dispatcher->transferTouch(destChannelToken,
4271 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004272 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004273 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4274 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004275 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004276 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004277 }));
4278
Svet Ganov5d3bc372020-01-26 23:11:07 -08004279TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004280 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004281
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004282 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004283 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4284 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004285 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004286
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004287 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004288 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4289 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004290 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004291
4292 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004293 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004294
4295 PointF pointInFirst = {300, 200};
4296 PointF pointInSecond = {300, 600};
4297
4298 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004299 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4300 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4301 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004302 // Only the first window should get the down event
4303 firstWindow->consumeMotionDown();
4304 secondWindow->assertNoEvents();
4305
4306 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004307 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4308 ADISPLAY_ID_DEFAULT,
4309 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004310 // The first window gets a move and the second a down
4311 firstWindow->consumeMotionMove();
4312 secondWindow->consumeMotionDown();
4313
4314 // Transfer touch focus to the second window
4315 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4316 // The first window gets cancel and the new gets pointer down (it already saw down)
4317 firstWindow->consumeMotionCancel();
4318 secondWindow->consumeMotionPointerDown(1);
4319
4320 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004321 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4322 ADISPLAY_ID_DEFAULT,
4323 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004324 // The first window gets nothing and the second gets pointer up
4325 firstWindow->assertNoEvents();
4326 secondWindow->consumeMotionPointerUp(1);
4327
4328 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004329 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4330 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004331 // The first window gets nothing and the second gets up
4332 firstWindow->assertNoEvents();
4333 secondWindow->consumeMotionUp();
4334}
4335
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004336// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4337// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4338// touch is not supported, so the touch should continue on those windows and the transferred-to
4339// window should get nothing.
4340TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4341 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4342
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004343 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004344 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4345 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004346 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004347
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004348 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004349 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4350 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004351 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004352
4353 // Add the windows to the dispatcher
4354 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4355
4356 PointF pointInFirst = {300, 200};
4357 PointF pointInSecond = {300, 600};
4358
4359 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004360 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4361 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4362 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004363 // Only the first window should get the down event
4364 firstWindow->consumeMotionDown();
4365 secondWindow->assertNoEvents();
4366
4367 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004368 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4369 ADISPLAY_ID_DEFAULT,
4370 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004371 // The first window gets a move and the second a down
4372 firstWindow->consumeMotionMove();
4373 secondWindow->consumeMotionDown();
4374
4375 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004376 const bool transferred =
4377 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004378 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4379 ASSERT_FALSE(transferred);
4380 firstWindow->assertNoEvents();
4381 secondWindow->assertNoEvents();
4382
4383 // The rest of the dispatch should proceed as normal
4384 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004385 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4386 ADISPLAY_ID_DEFAULT,
4387 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004388 // The first window gets MOVE and the second gets pointer up
4389 firstWindow->consumeMotionMove();
4390 secondWindow->consumeMotionUp();
4391
4392 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004393 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4394 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004395 // The first window gets nothing and the second gets up
4396 firstWindow->consumeMotionUp();
4397 secondWindow->assertNoEvents();
4398}
4399
Arthur Hungabbb9d82021-09-01 14:52:30 +00004400// This case will create two windows and one mirrored window on the default display and mirror
4401// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4402// the windows info of second display before default display.
4403TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4404 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4405 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004406 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004407 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004408 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004409 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004410 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004411
4412 sp<FakeWindowHandle> mirrorWindowInPrimary =
4413 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4414 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004415
4416 sp<FakeWindowHandle> firstWindowInSecondary =
4417 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4418 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004419
4420 sp<FakeWindowHandle> secondWindowInSecondary =
4421 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4422 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004423
4424 // Update window info, let it find window handle of second display first.
4425 mDispatcher->setInputWindows(
4426 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4427 {ADISPLAY_ID_DEFAULT,
4428 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4429
4430 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4431 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4432 {50, 50}))
4433 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4434
4435 // Window should receive motion event.
4436 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4437
4438 // Transfer touch focus
4439 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4440 secondWindowInPrimary->getToken()));
4441 // The first window gets cancel.
4442 firstWindowInPrimary->consumeMotionCancel();
4443 secondWindowInPrimary->consumeMotionDown();
4444
4445 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4446 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4447 ADISPLAY_ID_DEFAULT, {150, 50}))
4448 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4449 firstWindowInPrimary->assertNoEvents();
4450 secondWindowInPrimary->consumeMotionMove();
4451
4452 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4453 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4454 {150, 50}))
4455 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4456 firstWindowInPrimary->assertNoEvents();
4457 secondWindowInPrimary->consumeMotionUp();
4458}
4459
4460// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4461// 'transferTouch' api.
4462TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4463 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4464 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004465 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004466 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004467 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004468 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004469 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004470
4471 sp<FakeWindowHandle> mirrorWindowInPrimary =
4472 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4473 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004474
4475 sp<FakeWindowHandle> firstWindowInSecondary =
4476 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4477 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004478
4479 sp<FakeWindowHandle> secondWindowInSecondary =
4480 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4481 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004482
4483 // Update window info, let it find window handle of second display first.
4484 mDispatcher->setInputWindows(
4485 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4486 {ADISPLAY_ID_DEFAULT,
4487 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4488
4489 // Touch on second display.
4490 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4491 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4492 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4493
4494 // Window should receive motion event.
4495 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4496
4497 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004498 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004499
4500 // The first window gets cancel.
4501 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4502 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4503
4504 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4505 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4506 SECOND_DISPLAY_ID, {150, 50}))
4507 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4508 firstWindowInPrimary->assertNoEvents();
4509 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4510
4511 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4512 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4513 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4514 firstWindowInPrimary->assertNoEvents();
4515 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4516}
4517
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004518TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004519 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004520 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4521 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004522
Vishnu Nair47074b82020-08-14 11:54:47 -07004523 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004524 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004525 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004526
4527 window->consumeFocusEvent(true);
4528
Prabir Pradhan678438e2023-04-13 19:32:51 +00004529 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004530
4531 // Window should receive key down event.
4532 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4533}
4534
4535TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004536 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004537 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4538 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004539
Arthur Hung72d8dc32020-03-28 00:48:39 +00004540 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004541
Prabir Pradhan678438e2023-04-13 19:32:51 +00004542 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004543 mDispatcher->waitForIdle();
4544
4545 window->assertNoEvents();
4546}
4547
4548// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4549TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004550 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004551 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4552 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004553
Arthur Hung72d8dc32020-03-28 00:48:39 +00004554 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004555
4556 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004557 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004558 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004559 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4560 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004561
4562 // Window should receive only the motion event
4563 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4564 window->assertNoEvents(); // Key event or focus event will not be received
4565}
4566
arthurhungea3f4fc2020-12-21 23:18:53 +08004567TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4568 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4569
arthurhungea3f4fc2020-12-21 23:18:53 +08004570 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004571 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4572 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004573 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004574
arthurhungea3f4fc2020-12-21 23:18:53 +08004575 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004576 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4577 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004578 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004579
4580 // Add the windows to the dispatcher
4581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4582
4583 PointF pointInFirst = {300, 200};
4584 PointF pointInSecond = {300, 600};
4585
4586 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004587 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4588 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4589 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004590 // Only the first window should get the down event
4591 firstWindow->consumeMotionDown();
4592 secondWindow->assertNoEvents();
4593
4594 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004595 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4596 ADISPLAY_ID_DEFAULT,
4597 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004598 // The first window gets a move and the second a down
4599 firstWindow->consumeMotionMove();
4600 secondWindow->consumeMotionDown();
4601
4602 // Send pointer cancel to the second window
4603 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004604 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004605 {pointInFirst, pointInSecond});
4606 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004607 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004608 // The first window gets move and the second gets cancel.
4609 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4610 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4611
4612 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004613 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4614 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08004615 // The first window gets up and the second gets nothing.
4616 firstWindow->consumeMotionUp();
4617 secondWindow->assertNoEvents();
4618}
4619
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004620TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
4621 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4622
4623 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004624 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004625 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4626 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
4627 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
4628 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
4629
Harry Cutts33476232023-01-30 19:57:29 +00004630 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004631 window->assertNoEvents();
4632 mDispatcher->waitForIdle();
4633}
4634
chaviwd1c23182019-12-20 18:44:56 -08004635class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00004636public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004637 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004638 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07004639 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004640 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07004641 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00004642 }
4643
chaviwd1c23182019-12-20 18:44:56 -08004644 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
4645
4646 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004647 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
4648 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08004649 }
4650
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004651 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
4652
4653 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
4654
chaviwd1c23182019-12-20 18:44:56 -08004655 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004656 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08004657 expectedDisplayId, expectedFlags);
4658 }
4659
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004660 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004661 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004662 expectedDisplayId, expectedFlags);
4663 }
4664
chaviwd1c23182019-12-20 18:44:56 -08004665 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004666 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08004667 expectedDisplayId, expectedFlags);
4668 }
4669
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004670 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004671 mInputReceiver->consumeMotionEvent(
4672 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4673 WithDisplayId(expectedDisplayId),
4674 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004675 }
4676
Arthur Hungfbfa5722021-11-16 02:45:54 +00004677 void consumeMotionPointerDown(int32_t pointerIdx) {
4678 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4679 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004680 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004681 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004682 }
4683
Evan Rosky84f07f02021-04-16 10:42:42 -07004684 MotionEvent* consumeMotion() {
4685 InputEvent* event = mInputReceiver->consume();
4686 if (!event) {
4687 ADD_FAILURE() << "No event was produced";
4688 return nullptr;
4689 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004690 if (event->getType() != InputEventType::MOTION) {
4691 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07004692 return nullptr;
4693 }
4694 return static_cast<MotionEvent*>(event);
4695 }
4696
chaviwd1c23182019-12-20 18:44:56 -08004697 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4698
4699private:
4700 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004701};
4702
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004703using InputDispatcherMonitorTest = InputDispatcherTest;
4704
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004705/**
4706 * Two entities that receive touch: A window, and a global monitor.
4707 * The touch goes to the window, and then the window disappears.
4708 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4709 * for the monitor, as well.
4710 * 1. foregroundWindow
4711 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4712 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004713TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004714 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4715 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004716 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004717
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004718 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004719
4720 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4721 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4722 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4723 {100, 200}))
4724 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4725
4726 // Both the foreground window and the global monitor should receive the touch down
4727 window->consumeMotionDown();
4728 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4729
4730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4731 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4732 ADISPLAY_ID_DEFAULT, {110, 200}))
4733 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4734
4735 window->consumeMotionMove();
4736 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4737
4738 // Now the foreground window goes away
4739 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4740 window->consumeMotionCancel();
4741 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
4742
4743 // If more events come in, there will be no more foreground window to send them to. This will
4744 // cause a cancel for the monitor, as well.
4745 ASSERT_EQ(InputEventInjectionResult::FAILED,
4746 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4747 ADISPLAY_ID_DEFAULT, {120, 200}))
4748 << "Injection should fail because the window was removed";
4749 window->assertNoEvents();
4750 // Global monitor now gets the cancel
4751 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4752}
4753
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004754TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07004755 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004756 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4757 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004758 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004759
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004760 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004761
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004762 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004763 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004764 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00004765 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004766 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004767}
4768
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004769TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
4770 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004771
Chris Yea209fde2020-07-22 13:54:51 -07004772 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004773 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4774 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004775 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004776
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004777 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004778 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004779 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08004780 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004781 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004782
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004783 // Pilfer pointers from the monitor.
4784 // This should not do anything and the window should continue to receive events.
4785 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00004786
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004787 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004788 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4789 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004790 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004791
4792 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4793 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004794}
4795
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004796TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07004797 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004798 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4799 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004800 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4801 window->setWindowOffset(20, 40);
4802 window->setWindowTransform(0, 1, -1, 0);
4803
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004804 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004805
4806 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4807 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4808 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4809 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4810 MotionEvent* event = monitor.consumeMotion();
4811 // Even though window has transform, gesture monitor must not.
4812 ASSERT_EQ(ui::Transform(), event->getTransform());
4813}
4814
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004815TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00004816 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004817 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00004818
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004819 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00004820 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004821 << "Injection should fail if there is a monitor, but no touchable window";
4822 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00004823}
4824
chaviw81e2bb92019-12-18 15:03:51 -08004825TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004826 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004827 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4828 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08004829
Arthur Hung72d8dc32020-03-28 00:48:39 +00004830 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08004831
4832 NotifyMotionArgs motionArgs =
4833 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4834 ADISPLAY_ID_DEFAULT);
4835
Prabir Pradhan678438e2023-04-13 19:32:51 +00004836 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08004837 // Window should receive motion down event.
4838 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4839
4840 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08004841 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08004842 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4843 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4844 motionArgs.pointerCoords[0].getX() - 10);
4845
Prabir Pradhan678438e2023-04-13 19:32:51 +00004846 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004847 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004848 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08004849}
4850
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004851/**
4852 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
4853 * the device default right away. In the test scenario, we check both the default value,
4854 * and the action of enabling / disabling.
4855 */
4856TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07004857 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004858 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4859 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08004860 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004861
4862 // Set focused application.
4863 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004864 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004865
4866 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00004867 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004868 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004869 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004870
4871 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004872 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004873 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00004874 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004875
4876 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004877 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00004878 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004879 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07004880 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004881 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004882 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004883 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004884
4885 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004886 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004887 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00004888 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004889
4890 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004891 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00004892 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004893 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07004894 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004895 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004896 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004897 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004898
4899 window->assertNoEvents();
4900}
4901
Gang Wange9087892020-01-07 12:17:14 -05004902TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004903 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004904 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4905 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05004906
4907 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004908 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05004909
Arthur Hung72d8dc32020-03-28 00:48:39 +00004910 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004911 setFocusedWindow(window);
4912
Harry Cutts33476232023-01-30 19:57:29 +00004913 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05004914
Prabir Pradhan678438e2023-04-13 19:32:51 +00004915 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
4916 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05004917
4918 InputEvent* event = window->consume();
4919 ASSERT_NE(event, nullptr);
4920
4921 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
4922 ASSERT_NE(verified, nullptr);
4923 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
4924
4925 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
4926 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
4927 ASSERT_EQ(keyArgs.source, verified->source);
4928 ASSERT_EQ(keyArgs.displayId, verified->displayId);
4929
4930 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
4931
4932 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05004933 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004934 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05004935 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
4936 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
4937 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
4938 ASSERT_EQ(0, verifiedKey.repeatCount);
4939}
4940
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004941TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004942 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004943 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4944 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004945
4946 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4947
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07004948 ui::Transform transform;
4949 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
4950
4951 gui::DisplayInfo displayInfo;
4952 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
4953 displayInfo.transform = transform;
4954
4955 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004956
Prabir Pradhan678438e2023-04-13 19:32:51 +00004957 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004958 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4959 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00004960 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004961
4962 InputEvent* event = window->consume();
4963 ASSERT_NE(event, nullptr);
4964
4965 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
4966 ASSERT_NE(verified, nullptr);
4967 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
4968
4969 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
4970 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
4971 EXPECT_EQ(motionArgs.source, verified->source);
4972 EXPECT_EQ(motionArgs.displayId, verified->displayId);
4973
4974 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
4975
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07004976 const vec2 rawXY =
4977 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
4978 motionArgs.pointerCoords[0].getXYValue());
4979 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
4980 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004981 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004982 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08004983 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08004984 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
4985 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
4986}
4987
chaviw09c8d2d2020-08-24 15:48:26 -07004988/**
4989 * Ensure that separate calls to sign the same data are generating the same key.
4990 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
4991 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
4992 * tests.
4993 */
4994TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
4995 KeyEvent event = getTestKeyEvent();
4996 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
4997
4998 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
4999 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5000 ASSERT_EQ(hmac1, hmac2);
5001}
5002
5003/**
5004 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5005 */
5006TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5007 KeyEvent event = getTestKeyEvent();
5008 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5009 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5010
5011 verifiedEvent.deviceId += 1;
5012 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5013
5014 verifiedEvent.source += 1;
5015 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5016
5017 verifiedEvent.eventTimeNanos += 1;
5018 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5019
5020 verifiedEvent.displayId += 1;
5021 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5022
5023 verifiedEvent.action += 1;
5024 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5025
5026 verifiedEvent.downTimeNanos += 1;
5027 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5028
5029 verifiedEvent.flags += 1;
5030 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5031
5032 verifiedEvent.keyCode += 1;
5033 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5034
5035 verifiedEvent.scanCode += 1;
5036 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5037
5038 verifiedEvent.metaState += 1;
5039 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5040
5041 verifiedEvent.repeatCount += 1;
5042 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5043}
5044
Vishnu Nair958da932020-08-21 17:12:37 -07005045TEST_F(InputDispatcherTest, SetFocusedWindow) {
5046 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5047 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005048 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005049 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005050 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005051 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5052
5053 // Top window is also focusable but is not granted focus.
5054 windowTop->setFocusable(true);
5055 windowSecond->setFocusable(true);
5056 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5057 setFocusedWindow(windowSecond);
5058
5059 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005060 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5061 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005062
5063 // Focused window should receive event.
5064 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5065 windowTop->assertNoEvents();
5066}
5067
5068TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5069 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5070 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005071 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005072 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5073
5074 window->setFocusable(true);
5075 // Release channel for window is no longer valid.
5076 window->releaseChannel();
5077 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5078 setFocusedWindow(window);
5079
5080 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005081 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5082 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005083
5084 // window channel is invalid, so it should not receive any input event.
5085 window->assertNoEvents();
5086}
5087
5088TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5089 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5090 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005091 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005092 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005093 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5094
Vishnu Nair958da932020-08-21 17:12:37 -07005095 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5096 setFocusedWindow(window);
5097
5098 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005099 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5100 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005101
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005102 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005103 window->assertNoEvents();
5104}
5105
5106TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5107 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5108 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005109 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005110 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005111 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005112 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5113
5114 windowTop->setFocusable(true);
5115 windowSecond->setFocusable(true);
5116 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5117 setFocusedWindow(windowTop);
5118 windowTop->consumeFocusEvent(true);
5119
Chavi Weingarten847e8512023-03-29 00:26:09 +00005120 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5121 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005122 windowSecond->consumeFocusEvent(true);
5123 windowTop->consumeFocusEvent(false);
5124
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005125 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5126 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005127
5128 // Focused window should receive event.
5129 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5130}
5131
Chavi Weingarten847e8512023-03-29 00:26:09 +00005132TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005133 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5134 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005135 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005136 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005137 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005138 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5139
5140 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005141 windowSecond->setFocusable(false);
5142 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005143 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005144 setFocusedWindow(windowTop);
5145 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005146
Chavi Weingarten847e8512023-03-29 00:26:09 +00005147 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5148 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005149
5150 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005151 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005152 windowSecond->assertNoEvents();
5153}
5154
5155TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5156 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5157 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005158 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005159 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005160 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5161 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005162 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5163
5164 window->setFocusable(true);
5165 previousFocusedWindow->setFocusable(true);
5166 window->setVisible(false);
5167 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5168 setFocusedWindow(previousFocusedWindow);
5169 previousFocusedWindow->consumeFocusEvent(true);
5170
5171 // Requesting focus on invisible window takes focus from currently focused window.
5172 setFocusedWindow(window);
5173 previousFocusedWindow->consumeFocusEvent(false);
5174
5175 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005176 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005177 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5178 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005179
5180 // Window does not get focus event or key down.
5181 window->assertNoEvents();
5182
5183 // Window becomes visible.
5184 window->setVisible(true);
5185 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5186
5187 // Window receives focus event.
5188 window->consumeFocusEvent(true);
5189 // Focused window receives key down.
5190 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5191}
5192
Vishnu Nair599f1412021-06-21 10:39:58 -07005193TEST_F(InputDispatcherTest, DisplayRemoved) {
5194 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5195 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005196 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005197 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5198
5199 // window is granted focus.
5200 window->setFocusable(true);
5201 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5202 setFocusedWindow(window);
5203 window->consumeFocusEvent(true);
5204
5205 // When a display is removed window loses focus.
5206 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5207 window->consumeFocusEvent(false);
5208}
5209
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005210/**
5211 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5212 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5213 * of the 'slipperyEnterWindow'.
5214 *
5215 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5216 * a way so that the touched location is no longer covered by the top window.
5217 *
5218 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5219 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5220 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5221 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5222 * with ACTION_DOWN).
5223 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5224 * window moved itself away from the touched location and had Flag::SLIPPERY.
5225 *
5226 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5227 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5228 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5229 *
5230 * In this test, we ensure that the event received by the bottom window has
5231 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5232 */
5233TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005234 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5235 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005236
5237 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5238 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5239
5240 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005241 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005242 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005243 // Make sure this one overlaps the bottom window
5244 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5245 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5246 // one. Windows with the same owner are not considered to be occluding each other.
5247 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5248
5249 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005250 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005251 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5252
5253 mDispatcher->setInputWindows(
5254 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5255
5256 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005257 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5258 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5259 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005260 slipperyExitWindow->consumeMotionDown();
5261 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5262 mDispatcher->setInputWindows(
5263 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5264
Prabir Pradhan678438e2023-04-13 19:32:51 +00005265 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5266 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5267 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005268
5269 slipperyExitWindow->consumeMotionCancel();
5270
5271 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5272 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5273}
5274
Garfield Tan1c7bc862020-01-28 13:24:04 -08005275class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5276protected:
5277 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5278 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5279
Chris Yea209fde2020-07-22 13:54:51 -07005280 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005281 sp<FakeWindowHandle> mWindow;
5282
5283 virtual void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005284 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005285 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005286 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005287 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005288 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5289 ASSERT_EQ(OK, mDispatcher->start());
5290
5291 setUpWindow();
5292 }
5293
5294 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005295 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005296 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005297
Vishnu Nair47074b82020-08-14 11:54:47 -07005298 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005299 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005300 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005301 mWindow->consumeFocusEvent(true);
5302 }
5303
Chris Ye2ad95392020-09-01 13:44:44 -07005304 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005305 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005306 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005307 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005308 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005309
5310 // Window should receive key down event.
5311 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5312 }
5313
5314 void expectKeyRepeatOnce(int32_t repeatCount) {
5315 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5316 InputEvent* repeatEvent = mWindow->consume();
5317 ASSERT_NE(nullptr, repeatEvent);
5318
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005319 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005320
5321 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5322 uint32_t eventAction = repeatKeyEvent->getAction();
5323 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5324 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5325 }
5326
Chris Ye2ad95392020-09-01 13:44:44 -07005327 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005328 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005329 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005330 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005331 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005332
5333 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005334 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005335 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005336 }
5337};
5338
5339TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005340 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005341 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5342 expectKeyRepeatOnce(repeatCount);
5343 }
5344}
5345
5346TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005347 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005348 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5349 expectKeyRepeatOnce(repeatCount);
5350 }
Harry Cutts33476232023-01-30 19:57:29 +00005351 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005352 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005353 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5354 expectKeyRepeatOnce(repeatCount);
5355 }
5356}
5357
5358TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005359 sendAndConsumeKeyDown(/*deviceId=*/1);
5360 expectKeyRepeatOnce(/*repeatCount=*/1);
5361 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005362 mWindow->assertNoEvents();
5363}
5364
5365TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005366 sendAndConsumeKeyDown(/*deviceId=*/1);
5367 expectKeyRepeatOnce(/*repeatCount=*/1);
5368 sendAndConsumeKeyDown(/*deviceId=*/2);
5369 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005370 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005371 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005372 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005373 expectKeyRepeatOnce(/*repeatCount=*/2);
5374 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005375 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005376 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005377 mWindow->assertNoEvents();
5378}
5379
5380TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005381 sendAndConsumeKeyDown(/*deviceId=*/1);
5382 expectKeyRepeatOnce(/*repeatCount=*/1);
5383 sendAndConsumeKeyDown(/*deviceId=*/2);
5384 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005385 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005386 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005387 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005388 mWindow->assertNoEvents();
5389}
5390
liushenxiang42232912021-05-21 20:24:09 +08005391TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5392 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005393 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005394 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005395 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5396 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5397 mWindow->assertNoEvents();
5398}
5399
Garfield Tan1c7bc862020-01-28 13:24:04 -08005400TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005401 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005402 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005403 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5404 InputEvent* repeatEvent = mWindow->consume();
5405 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5406 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5407 IdGenerator::getSource(repeatEvent->getId()));
5408 }
5409}
5410
5411TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005412 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005413 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005414
5415 std::unordered_set<int32_t> idSet;
5416 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5417 InputEvent* repeatEvent = mWindow->consume();
5418 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5419 int32_t id = repeatEvent->getId();
5420 EXPECT_EQ(idSet.end(), idSet.find(id));
5421 idSet.insert(id);
5422 }
5423}
5424
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005425/* Test InputDispatcher for MultiDisplay */
5426class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5427public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005428 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005429 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005430
Chris Yea209fde2020-07-22 13:54:51 -07005431 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005432 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005433 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005434
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005435 // Set focus window for primary display, but focused display would be second one.
5436 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005437 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005438 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005439 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005440 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005441
Chris Yea209fde2020-07-22 13:54:51 -07005442 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005443 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005444 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005445 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005446 // Set focus display to second one.
5447 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5448 // Set focus window for second display.
5449 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005450 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005451 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005452 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005453 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005454 }
5455
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005456 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005457 InputDispatcherTest::TearDown();
5458
Chris Yea209fde2020-07-22 13:54:51 -07005459 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005460 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005461 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005462 windowInSecondary.clear();
5463 }
5464
5465protected:
Chris Yea209fde2020-07-22 13:54:51 -07005466 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005467 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005468 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005469 sp<FakeWindowHandle> windowInSecondary;
5470};
5471
5472TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5473 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005474 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5475 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5476 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005477 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005478 windowInSecondary->assertNoEvents();
5479
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005480 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005481 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5482 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5483 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005484 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005485 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005486}
5487
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005488TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005489 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005490 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5491 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005492 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005493 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005494 windowInSecondary->assertNoEvents();
5495
5496 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005497 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005498 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005499 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005500 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005501
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005502 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005503 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005504
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005505 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005506 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005507 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005508
5509 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005510 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005511 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005512 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005513 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005514 windowInSecondary->assertNoEvents();
5515}
5516
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005517// Test per-display input monitors for motion event.
5518TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005519 FakeMonitorReceiver monitorInPrimary =
5520 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5521 FakeMonitorReceiver monitorInSecondary =
5522 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005523
5524 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005525 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5526 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5527 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005528 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005529 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005530 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005531 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005532
5533 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005534 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5535 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5536 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005537 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005538 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005539 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005540 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005541
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005542 // Lift up the touch from the second display
5543 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5544 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5545 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5546 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5547 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5548
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005549 // Test inject a non-pointer motion event.
5550 // If specific a display, it will dispatch to the focused window of particular display,
5551 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005552 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5553 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5554 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005555 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005556 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005557 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005558 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005559}
5560
5561// Test per-display input monitors for key event.
5562TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005563 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005564 FakeMonitorReceiver monitorInPrimary =
5565 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5566 FakeMonitorReceiver monitorInSecondary =
5567 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005568
5569 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005570 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5571 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005572 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005573 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005574 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005575 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005576}
5577
Vishnu Nair958da932020-08-21 17:12:37 -07005578TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
5579 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005580 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005581 secondWindowInPrimary->setFocusable(true);
5582 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
5583 setFocusedWindow(secondWindowInPrimary);
5584 windowInPrimary->consumeFocusEvent(false);
5585 secondWindowInPrimary->consumeFocusEvent(true);
5586
5587 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005588 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
5589 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005590 windowInPrimary->assertNoEvents();
5591 windowInSecondary->assertNoEvents();
5592 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5593}
5594
Arthur Hungdfd528e2021-12-08 13:23:04 +00005595TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
5596 FakeMonitorReceiver monitorInPrimary =
5597 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5598 FakeMonitorReceiver monitorInSecondary =
5599 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
5600
5601 // Test touch down on primary display.
5602 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5603 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5604 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5605 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5606 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5607
5608 // Test touch down on second display.
5609 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5610 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5611 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5612 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
5613 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
5614
5615 // Trigger cancel touch.
5616 mDispatcher->cancelCurrentTouch();
5617 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5618 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5619 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
5620 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
5621
5622 // Test inject a move motion event, no window/monitor should receive the event.
5623 ASSERT_EQ(InputEventInjectionResult::FAILED,
5624 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5625 ADISPLAY_ID_DEFAULT, {110, 200}))
5626 << "Inject motion event should return InputEventInjectionResult::FAILED";
5627 windowInPrimary->assertNoEvents();
5628 monitorInPrimary.assertNoEvents();
5629
5630 ASSERT_EQ(InputEventInjectionResult::FAILED,
5631 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5632 SECOND_DISPLAY_ID, {110, 200}))
5633 << "Inject motion event should return InputEventInjectionResult::FAILED";
5634 windowInSecondary->assertNoEvents();
5635 monitorInSecondary.assertNoEvents();
5636}
5637
Jackal Guof9696682018-10-05 12:23:23 +08005638class InputFilterTest : public InputDispatcherTest {
5639protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005640 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
5641 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08005642 NotifyMotionArgs motionArgs;
5643
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005644 motionArgs =
5645 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005646 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005647 motionArgs =
5648 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005649 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005650 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005651 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005652 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
5653 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08005654 } else {
5655 mFakePolicy->assertFilterInputEventWasNotCalled();
5656 }
5657 }
5658
5659 void testNotifyKey(bool expectToBeFiltered) {
5660 NotifyKeyArgs keyArgs;
5661
5662 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005663 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005664 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005665 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005666 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005667
5668 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08005669 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005670 } else {
5671 mFakePolicy->assertFilterInputEventWasNotCalled();
5672 }
5673 }
5674};
5675
5676// Test InputFilter for MotionEvent
5677TEST_F(InputFilterTest, MotionEvent_InputFilter) {
5678 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
5679 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5680 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5681
5682 // Enable InputFilter
5683 mDispatcher->setInputFilterEnabled(true);
5684 // Test touch on both primary and second display, and check if both events are filtered.
5685 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
5686 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
5687
5688 // Disable InputFilter
5689 mDispatcher->setInputFilterEnabled(false);
5690 // Test touch on both primary and second display, and check if both events aren't filtered.
5691 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5692 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5693}
5694
5695// Test InputFilter for KeyEvent
5696TEST_F(InputFilterTest, KeyEvent_InputFilter) {
5697 // Since the InputFilter is disabled by default, check if key event aren't filtered.
5698 testNotifyKey(/*expectToBeFiltered*/ false);
5699
5700 // Enable InputFilter
5701 mDispatcher->setInputFilterEnabled(true);
5702 // Send a key event, and check if it is filtered.
5703 testNotifyKey(/*expectToBeFiltered*/ true);
5704
5705 // Disable InputFilter
5706 mDispatcher->setInputFilterEnabled(false);
5707 // Send a key event, and check if it isn't filtered.
5708 testNotifyKey(/*expectToBeFiltered*/ false);
5709}
5710
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005711// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
5712// logical display coordinate space.
5713TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
5714 ui::Transform firstDisplayTransform;
5715 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5716 ui::Transform secondDisplayTransform;
5717 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
5718
5719 std::vector<gui::DisplayInfo> displayInfos(2);
5720 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
5721 displayInfos[0].transform = firstDisplayTransform;
5722 displayInfos[1].displayId = SECOND_DISPLAY_ID;
5723 displayInfos[1].transform = secondDisplayTransform;
5724
5725 mDispatcher->onWindowInfosChanged({}, displayInfos);
5726
5727 // Enable InputFilter
5728 mDispatcher->setInputFilterEnabled(true);
5729
5730 // Ensure the correct transforms are used for the displays.
5731 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
5732 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
5733}
5734
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005735class InputFilterInjectionPolicyTest : public InputDispatcherTest {
5736protected:
5737 virtual void SetUp() override {
5738 InputDispatcherTest::SetUp();
5739
5740 /**
5741 * We don't need to enable input filter to test the injected event policy, but we enabled it
5742 * here to make the tests more realistic, since this policy only matters when inputfilter is
5743 * on.
5744 */
5745 mDispatcher->setInputFilterEnabled(true);
5746
5747 std::shared_ptr<InputApplicationHandle> application =
5748 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005749 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
5750 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005751
5752 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5753 mWindow->setFocusable(true);
5754 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5755 setFocusedWindow(mWindow);
5756 mWindow->consumeFocusEvent(true);
5757 }
5758
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005759 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5760 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005761 KeyEvent event;
5762
5763 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5764 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
5765 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00005766 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005767 const int32_t additionalPolicyFlags =
5768 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
5769 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005770 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005771 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5772 policyFlags | additionalPolicyFlags));
5773
5774 InputEvent* received = mWindow->consume();
5775 ASSERT_NE(nullptr, received);
5776 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005777 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005778 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
5779 ASSERT_EQ(flags, keyEvent.getFlags());
5780 }
5781
5782 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5783 int32_t flags) {
5784 MotionEvent event;
5785 PointerProperties pointerProperties[1];
5786 PointerCoords pointerCoords[1];
5787 pointerProperties[0].clear();
5788 pointerProperties[0].id = 0;
5789 pointerCoords[0].clear();
5790 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
5791 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
5792
5793 ui::Transform identityTransform;
5794 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5795 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
5796 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
5797 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
5798 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07005799 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07005800 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005801 /*pointerCount*/ 1, pointerProperties, pointerCoords);
5802
5803 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
5804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005805 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005806 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5807 policyFlags | additionalPolicyFlags));
5808
5809 InputEvent* received = mWindow->consume();
5810 ASSERT_NE(nullptr, received);
5811 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005812 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005813 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
5814 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005815 }
5816
5817private:
5818 sp<FakeWindowHandle> mWindow;
5819};
5820
5821TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005822 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
5823 // filter. Without it, the event will no different from a regularly injected event, and the
5824 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00005825 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
5826 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005827}
5828
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005829TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005830 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005831 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005832 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
5833}
5834
5835TEST_F(InputFilterInjectionPolicyTest,
5836 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
5837 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005838 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005839 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005840}
5841
5842TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00005843 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
5844 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005845}
5846
chaviwfd6d3512019-03-25 13:23:49 -07005847class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005848 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07005849 InputDispatcherTest::SetUp();
5850
Chris Yea209fde2020-07-22 13:54:51 -07005851 std::shared_ptr<FakeApplicationHandle> application =
5852 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005853 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005854 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005855 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07005856
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005857 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005858 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005859 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07005860
5861 // Set focused application.
5862 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005863 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07005864
5865 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005866 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005867 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005868 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07005869 }
5870
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005871 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07005872 InputDispatcherTest::TearDown();
5873
5874 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005875 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07005876 }
5877
5878protected:
5879 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005880 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005881 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07005882};
5883
5884// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5885// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
5886// the onPointerDownOutsideFocus callback.
5887TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005888 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005889 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5890 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005891 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005892 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005893
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005894 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07005895 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
5896}
5897
5898// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
5899// DOWN on the window that doesn't have focus. Ensure no window received the
5900// onPointerDownOutsideFocus callback.
5901TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005902 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005903 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005904 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005905 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005906
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005907 ASSERT_TRUE(mDispatcher->waitForIdle());
5908 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005909}
5910
5911// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
5912// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
5913TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005914 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5915 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005916 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005917 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005918
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005919 ASSERT_TRUE(mDispatcher->waitForIdle());
5920 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005921}
5922
5923// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5924// DOWN on the window that already has focus. Ensure no window received the
5925// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005926TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005927 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005928 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005929 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005930 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07005931 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07005932
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005933 ASSERT_TRUE(mDispatcher->waitForIdle());
5934 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07005935}
5936
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005937// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
5938// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
5939TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
5940 const MotionEvent event =
5941 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
5942 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005943 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08005944 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
5945 .build();
5946 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
5947 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5948 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
5949
5950 ASSERT_TRUE(mDispatcher->waitForIdle());
5951 mFakePolicy->assertOnPointerDownWasNotCalled();
5952 // Ensure that the unfocused window did not receive any FOCUS events.
5953 mUnfocusedWindow->assertNoEvents();
5954}
5955
chaviwaf87b3e2019-10-01 16:59:28 -07005956// These tests ensures we can send touch events to a single client when there are multiple input
5957// windows that point to the same client token.
5958class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
5959 virtual void SetUp() override {
5960 InputDispatcherTest::SetUp();
5961
Chris Yea209fde2020-07-22 13:54:51 -07005962 std::shared_ptr<FakeApplicationHandle> application =
5963 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005964 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
5965 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07005966 mWindow1->setFrame(Rect(0, 0, 100, 100));
5967
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005968 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
5969 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07005970 mWindow2->setFrame(Rect(100, 100, 200, 200));
5971
Arthur Hung72d8dc32020-03-28 00:48:39 +00005972 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07005973 }
5974
5975protected:
5976 sp<FakeWindowHandle> mWindow1;
5977 sp<FakeWindowHandle> mWindow2;
5978
5979 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05005980 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07005981 vec2 vals = windowInfo->transform.transform(point.x, point.y);
5982 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07005983 }
5984
5985 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
5986 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005987 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07005988 InputEvent* event = window->consume();
5989
5990 ASSERT_NE(nullptr, event) << name.c_str()
5991 << ": consumer should have returned non-NULL event.";
5992
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005993 ASSERT_EQ(InputEventType::MOTION, event->getType())
5994 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07005995
5996 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005997 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08005998 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07005999
6000 for (size_t i = 0; i < points.size(); i++) {
6001 float expectedX = points[i].x;
6002 float expectedY = points[i].y;
6003
6004 EXPECT_EQ(expectedX, motionEvent.getX(i))
6005 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6006 << ", got " << motionEvent.getX(i);
6007 EXPECT_EQ(expectedY, motionEvent.getY(i))
6008 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6009 << ", got " << motionEvent.getY(i);
6010 }
6011 }
chaviw9eaa22c2020-07-01 16:21:27 -07006012
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006013 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006014 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006015 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6016 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006017
6018 // Always consume from window1 since it's the window that has the InputReceiver
6019 consumeMotionEvent(mWindow1, action, expectedPoints);
6020 }
chaviwaf87b3e2019-10-01 16:59:28 -07006021};
6022
6023TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6024 // Touch Window 1
6025 PointF touchedPoint = {10, 10};
6026 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006027 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006028
6029 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006030 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006031
6032 // Touch Window 2
6033 touchedPoint = {150, 150};
6034 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006035 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006036}
6037
chaviw9eaa22c2020-07-01 16:21:27 -07006038TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6039 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006040 mWindow2->setWindowScale(0.5f, 0.5f);
6041
6042 // Touch Window 1
6043 PointF touchedPoint = {10, 10};
6044 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006045 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006046 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006047 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006048
6049 // Touch Window 2
6050 touchedPoint = {150, 150};
6051 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006052 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6053 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006054
chaviw9eaa22c2020-07-01 16:21:27 -07006055 // Update the transform so rotation is set
6056 mWindow2->setWindowTransform(0, -1, 1, 0);
6057 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6058 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006059}
6060
chaviw9eaa22c2020-07-01 16:21:27 -07006061TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006062 mWindow2->setWindowScale(0.5f, 0.5f);
6063
6064 // Touch Window 1
6065 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6066 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006067 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006068
6069 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006070 touchedPoints.push_back(PointF{150, 150});
6071 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006072 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006073
chaviw9eaa22c2020-07-01 16:21:27 -07006074 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006075 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006076 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006077
chaviw9eaa22c2020-07-01 16:21:27 -07006078 // Update the transform so rotation is set for Window 2
6079 mWindow2->setWindowTransform(0, -1, 1, 0);
6080 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006081 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006082}
6083
chaviw9eaa22c2020-07-01 16:21:27 -07006084TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006085 mWindow2->setWindowScale(0.5f, 0.5f);
6086
6087 // Touch Window 1
6088 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6089 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006090 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006091
6092 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006093 touchedPoints.push_back(PointF{150, 150});
6094 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006095
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006096 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006097
6098 // Move both windows
6099 touchedPoints = {{20, 20}, {175, 175}};
6100 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6101 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6102
chaviw9eaa22c2020-07-01 16:21:27 -07006103 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006104
chaviw9eaa22c2020-07-01 16:21:27 -07006105 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006106 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006107 expectedPoints.pop_back();
6108
6109 // Touch Window 2
6110 mWindow2->setWindowTransform(0, -1, 1, 0);
6111 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006112 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006113
6114 // Move both windows
6115 touchedPoints = {{20, 20}, {175, 175}};
6116 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6117 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6118
6119 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006120}
6121
6122TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6123 mWindow1->setWindowScale(0.5f, 0.5f);
6124
6125 // Touch Window 1
6126 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6127 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006128 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006129
6130 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006131 touchedPoints.push_back(PointF{150, 150});
6132 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006133
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006134 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006135
6136 // Move both windows
6137 touchedPoints = {{20, 20}, {175, 175}};
6138 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6139 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6140
chaviw9eaa22c2020-07-01 16:21:27 -07006141 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006142}
6143
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006144class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6145 virtual void SetUp() override {
6146 InputDispatcherTest::SetUp();
6147
Chris Yea209fde2020-07-22 13:54:51 -07006148 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006149 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006150 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6151 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006152 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006153 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006154 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006155
6156 // Set focused application.
6157 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6158
6159 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006160 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006161 mWindow->consumeFocusEvent(true);
6162 }
6163
6164 virtual void TearDown() override {
6165 InputDispatcherTest::TearDown();
6166 mWindow.clear();
6167 }
6168
6169protected:
Chris Yea209fde2020-07-22 13:54:51 -07006170 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006171 sp<FakeWindowHandle> mWindow;
6172 static constexpr PointF WINDOW_LOCATION = {20, 20};
6173
6174 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006175 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006176 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6177 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006178 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006179 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6180 WINDOW_LOCATION));
6181 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006182
6183 sp<FakeWindowHandle> addSpyWindow() {
6184 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006185 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006186 spy->setTrustedOverlay(true);
6187 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006188 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006189 spy->setDispatchingTimeout(30ms);
6190 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6191 return spy;
6192 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006193};
6194
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006195// Send a tap and respond, which should not cause an ANR.
6196TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6197 tapOnWindow();
6198 mWindow->consumeMotionDown();
6199 mWindow->consumeMotionUp();
6200 ASSERT_TRUE(mDispatcher->waitForIdle());
6201 mFakePolicy->assertNotifyAnrWasNotCalled();
6202}
6203
6204// Send a regular key and respond, which should not cause an ANR.
6205TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006206 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006207 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6208 ASSERT_TRUE(mDispatcher->waitForIdle());
6209 mFakePolicy->assertNotifyAnrWasNotCalled();
6210}
6211
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006212TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6213 mWindow->setFocusable(false);
6214 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6215 mWindow->consumeFocusEvent(false);
6216
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006217 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006218 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6219 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6220 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006221 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006222 // Key will not go to window because we have no focused window.
6223 // The 'no focused window' ANR timer should start instead.
6224
6225 // Now, the focused application goes away.
6226 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6227 // The key should get dropped and there should be no ANR.
6228
6229 ASSERT_TRUE(mDispatcher->waitForIdle());
6230 mFakePolicy->assertNotifyAnrWasNotCalled();
6231}
6232
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006233// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006234// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6235// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006236TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006237 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006238 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6239 WINDOW_LOCATION));
6240
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006241 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6242 ASSERT_TRUE(sequenceNum);
6243 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006244 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006245
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006246 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006247 mWindow->consumeMotionEvent(
6248 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006249 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006250 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006251}
6252
6253// Send a key to the app and have the app not respond right away.
6254TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6255 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006256 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006257 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6258 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006259 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006260 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006261 ASSERT_TRUE(mDispatcher->waitForIdle());
6262}
6263
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006264// We have a focused application, but no focused window
6265TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006266 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006267 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6268 mWindow->consumeFocusEvent(false);
6269
6270 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006271 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006272 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6273 WINDOW_LOCATION));
6274 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6275 mDispatcher->waitForIdle();
6276 mFakePolicy->assertNotifyAnrWasNotCalled();
6277
6278 // Once a focused event arrives, we get an ANR for this application
6279 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6280 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006281 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006282 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6283 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006284 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006285 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006286 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006287 ASSERT_TRUE(mDispatcher->waitForIdle());
6288}
6289
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006290/**
6291 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6292 * there will not be an ANR.
6293 */
6294TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6295 mWindow->setFocusable(false);
6296 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6297 mWindow->consumeFocusEvent(false);
6298
6299 KeyEvent event;
6300 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6301 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6302
6303 // Define a valid key down event that is stale (too old).
6304 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6305 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006306 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006307
6308 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6309
6310 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006311 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006312 InputEventInjectionSync::WAIT_FOR_RESULT,
6313 INJECT_EVENT_TIMEOUT, policyFlags);
6314 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6315 << "Injection should fail because the event is stale";
6316
6317 ASSERT_TRUE(mDispatcher->waitForIdle());
6318 mFakePolicy->assertNotifyAnrWasNotCalled();
6319 mWindow->assertNoEvents();
6320}
6321
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006322// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006323// Make sure that we don't notify policy twice about the same ANR.
6324TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006325 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006326 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6327 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006328
6329 // Once a focused event arrives, we get an ANR for this application
6330 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6331 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006332 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006333 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6334 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006335 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006336 const std::chrono::duration appTimeout =
6337 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6338 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006339
Vishnu Naire4df8752022-09-08 09:17:55 -07006340 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006341 // ANR should not be raised again. It is up to policy to do that if it desires.
6342 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006343
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006344 // If we now get a focused window, the ANR should stop, but the policy handles that via
6345 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006346 ASSERT_TRUE(mDispatcher->waitForIdle());
6347}
6348
6349// We have a focused application, but no focused window
6350TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006351 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006352 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6353 mWindow->consumeFocusEvent(false);
6354
6355 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006356 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006357 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006358 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6359 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006360
Vishnu Naire4df8752022-09-08 09:17:55 -07006361 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6362 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006363
6364 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006365 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006366 ASSERT_TRUE(mDispatcher->waitForIdle());
6367 mWindow->assertNoEvents();
6368}
6369
6370/**
6371 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6372 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6373 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6374 * the ANR mechanism should still work.
6375 *
6376 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6377 * DOWN event, while not responding on the second one.
6378 */
6379TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6380 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6381 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6382 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6383 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6384 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006385 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006386
6387 // Now send ACTION_UP, with identical timestamp
6388 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6389 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6390 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6391 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006392 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006393
6394 // We have now sent down and up. Let's consume first event and then ANR on the second.
6395 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6396 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006397 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006398}
6399
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006400// A spy window can receive an ANR
6401TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6402 sp<FakeWindowHandle> spy = addSpyWindow();
6403
6404 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6405 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6406 WINDOW_LOCATION));
6407 mWindow->consumeMotionDown();
6408
6409 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6410 ASSERT_TRUE(sequenceNum);
6411 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006412 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006413
6414 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006415 spy->consumeMotionEvent(
6416 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006417 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006418 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006419}
6420
6421// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006422// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006423TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6424 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006425
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006426 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6427 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006428 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006429 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006430
6431 // Stuck on the ACTION_UP
6432 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006433 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006434
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006435 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006436 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006437 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6438 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006439
6440 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6441 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006442 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006443 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006444 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006445}
6446
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006447// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006448// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006449TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6450 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006451
6452 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006453 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6454 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006455
6456 mWindow->consumeMotionDown();
6457 // Stuck on the ACTION_UP
6458 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006459 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006460
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006461 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006462 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006463 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6464 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006465
6466 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6467 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006468 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006469 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006470 spy->assertNoEvents();
6471}
6472
6473TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6474 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6475
6476 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6477
6478 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6479 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6480 WINDOW_LOCATION));
6481
6482 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6483 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6484 ASSERT_TRUE(consumeSeq);
6485
Prabir Pradhanedd96402022-02-15 01:46:16 -08006486 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006487
6488 monitor.finishEvent(*consumeSeq);
6489 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6490
6491 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006492 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006493}
6494
6495// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6496// process events, you don't get an anr. When the window later becomes unresponsive again, you
6497// get an ANR again.
6498// 1. tap -> block on ACTION_UP -> receive ANR
6499// 2. consume all pending events (= queue becomes healthy again)
6500// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6501TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6502 tapOnWindow();
6503
6504 mWindow->consumeMotionDown();
6505 // Block on ACTION_UP
6506 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006507 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006508 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6509 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006510 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006511 mWindow->assertNoEvents();
6512
6513 tapOnWindow();
6514 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006515 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006516 mWindow->consumeMotionUp();
6517
6518 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006519 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006520 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006521 mWindow->assertNoEvents();
6522}
6523
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006524// If a connection remains unresponsive for a while, make sure policy is only notified once about
6525// it.
6526TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006527 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006528 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6529 WINDOW_LOCATION));
6530
6531 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006532 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006533 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006534 // 'notifyConnectionUnresponsive' should only be called once per connection
6535 mFakePolicy->assertNotifyAnrWasNotCalled();
6536 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006537 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006538 mWindow->consumeMotionEvent(
6539 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006540 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006541 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}
6545
6546/**
6547 * If a window is processing a motion event, and then a key event comes in, the key event should
6548 * not to to the focused window until the motion is processed.
6549 *
6550 * Warning!!!
6551 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6552 * and the injection timeout that we specify when injecting the key.
6553 * We must have the injection timeout (10ms) be smaller than
6554 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6555 *
6556 * If that value changes, this test should also change.
6557 */
6558TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
6559 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6560 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6561
6562 tapOnWindow();
6563 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6564 ASSERT_TRUE(downSequenceNum);
6565 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6566 ASSERT_TRUE(upSequenceNum);
6567 // Don't finish the events yet, and send a key
6568 // Injection will "succeed" because we will eventually give up and send the key to the focused
6569 // window even if motions are still being processed. But because the injection timeout is short,
6570 // we will receive INJECTION_TIMED_OUT as the result.
6571
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006572 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006573 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006574 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6575 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006576 // Key will not be sent to the window, yet, because the window is still processing events
6577 // and the key remains pending, waiting for the touch events to be processed
6578 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6579 ASSERT_FALSE(keySequenceNum);
6580
6581 std::this_thread::sleep_for(500ms);
6582 // if we wait long enough though, dispatcher will give up, and still send the key
6583 // to the focused window, even though we have not yet finished the motion event
6584 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6585 mWindow->finishEvent(*downSequenceNum);
6586 mWindow->finishEvent(*upSequenceNum);
6587}
6588
6589/**
6590 * If a window is processing a motion event, and then a key event comes in, the key event should
6591 * not go to the focused window until the motion is processed.
6592 * If then a new motion comes in, then the pending key event should be going to the currently
6593 * focused window right away.
6594 */
6595TEST_F(InputDispatcherSingleWindowAnr,
6596 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
6597 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6598 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6599
6600 tapOnWindow();
6601 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6602 ASSERT_TRUE(downSequenceNum);
6603 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6604 ASSERT_TRUE(upSequenceNum);
6605 // Don't finish the events yet, and send a key
6606 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006607 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006608 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6609 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006610 // At this point, key is still pending, and should not be sent to the application yet.
6611 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6612 ASSERT_FALSE(keySequenceNum);
6613
6614 // Now tap down again. It should cause the pending key to go to the focused window right away.
6615 tapOnWindow();
6616 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
6617 // the other events yet. We can finish events in any order.
6618 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
6619 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
6620 mWindow->consumeMotionDown();
6621 mWindow->consumeMotionUp();
6622 mWindow->assertNoEvents();
6623}
6624
6625class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
6626 virtual void SetUp() override {
6627 InputDispatcherTest::SetUp();
6628
Chris Yea209fde2020-07-22 13:54:51 -07006629 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006630 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006631 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
6632 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006633 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006634 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006635 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006636
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006637 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
6638 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006639 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006640 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006641
6642 // Set focused application.
6643 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07006644 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006645
6646 // Expect one focus window exist in display.
6647 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006648 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006649 mFocusedWindow->consumeFocusEvent(true);
6650 }
6651
6652 virtual void TearDown() override {
6653 InputDispatcherTest::TearDown();
6654
6655 mUnfocusedWindow.clear();
6656 mFocusedWindow.clear();
6657 }
6658
6659protected:
Chris Yea209fde2020-07-22 13:54:51 -07006660 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006661 sp<FakeWindowHandle> mUnfocusedWindow;
6662 sp<FakeWindowHandle> mFocusedWindow;
6663 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
6664 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
6665 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
6666
6667 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
6668
6669 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
6670
6671private:
6672 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006673 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006674 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6675 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006676 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006677 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6678 location));
6679 }
6680};
6681
6682// If we have 2 windows that are both unresponsive, the one with the shortest timeout
6683// should be ANR'd first.
6684TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006685 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006686 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6687 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006688 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006689 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006690 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006691 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006692 // We consumed all events, so no ANR
6693 ASSERT_TRUE(mDispatcher->waitForIdle());
6694 mFakePolicy->assertNotifyAnrWasNotCalled();
6695
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 FOCUSED_WINDOW_LOCATION));
6699 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
6700 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006701
6702 const std::chrono::duration timeout =
6703 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006704 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006705 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
6706 // sequence to make it consistent
6707 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006708 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006709 mFocusedWindow->consumeMotionDown();
6710 // This cancel is generated because the connection was unresponsive
6711 mFocusedWindow->consumeMotionCancel();
6712 mFocusedWindow->assertNoEvents();
6713 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006714 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006715 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6716 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006717 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006718}
6719
6720// If we have 2 windows with identical timeouts that are both unresponsive,
6721// it doesn't matter which order they should have ANR.
6722// But we should receive ANR for both.
6723TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
6724 // Set the timeout for unfocused window to match the focused window
6725 mUnfocusedWindow->setDispatchingTimeout(10ms);
6726 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6727
6728 tapOnFocusedWindow();
6729 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08006730 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
6731 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
6732 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006733
6734 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006735 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
6736 mFocusedWindow->getToken() == anrConnectionToken2);
6737 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
6738 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006739
6740 ASSERT_TRUE(mDispatcher->waitForIdle());
6741 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006742
6743 mFocusedWindow->consumeMotionDown();
6744 mFocusedWindow->consumeMotionUp();
6745 mUnfocusedWindow->consumeMotionOutside();
6746
Prabir Pradhanedd96402022-02-15 01:46:16 -08006747 sp<IBinder> responsiveToken1, responsiveToken2;
6748 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
6749 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006750
6751 // Both applications should be marked as responsive, in any order
6752 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
6753 mFocusedWindow->getToken() == responsiveToken2);
6754 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
6755 mUnfocusedWindow->getToken() == responsiveToken2);
6756 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006757}
6758
6759// If a window is already not responding, the second tap on the same window should be ignored.
6760// We should also log an error to account for the dropped event (not tested here).
6761// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
6762TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
6763 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006764 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006765 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006766 // Receive the events, but don't respond
6767 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
6768 ASSERT_TRUE(downEventSequenceNum);
6769 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
6770 ASSERT_TRUE(upEventSequenceNum);
6771 const std::chrono::duration timeout =
6772 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006773 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006774
6775 // Tap once again
6776 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006777 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006778 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6779 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006780 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006781 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6782 FOCUSED_WINDOW_LOCATION));
6783 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
6784 // valid touch target
6785 mUnfocusedWindow->assertNoEvents();
6786
6787 // Consume the first tap
6788 mFocusedWindow->finishEvent(*downEventSequenceNum);
6789 mFocusedWindow->finishEvent(*upEventSequenceNum);
6790 ASSERT_TRUE(mDispatcher->waitForIdle());
6791 // The second tap did not go to the focused window
6792 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006793 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08006794 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6795 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006796 mFakePolicy->assertNotifyAnrWasNotCalled();
6797}
6798
6799// If you tap outside of all windows, there will not be ANR
6800TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006801 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006802 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6803 LOCATION_OUTSIDE_ALL_WINDOWS));
6804 ASSERT_TRUE(mDispatcher->waitForIdle());
6805 mFakePolicy->assertNotifyAnrWasNotCalled();
6806}
6807
6808// Since the focused window is paused, tapping on it should not produce any events
6809TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
6810 mFocusedWindow->setPaused(true);
6811 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6812
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006813 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006814 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6815 FOCUSED_WINDOW_LOCATION));
6816
6817 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
6818 ASSERT_TRUE(mDispatcher->waitForIdle());
6819 // Should not ANR because the window is paused, and touches shouldn't go to it
6820 mFakePolicy->assertNotifyAnrWasNotCalled();
6821
6822 mFocusedWindow->assertNoEvents();
6823 mUnfocusedWindow->assertNoEvents();
6824}
6825
6826/**
6827 * If a window is processing a motion event, and then a key event comes in, the key event should
6828 * not to to the focused window until the motion is processed.
6829 * If a different window becomes focused at this time, the key should go to that window instead.
6830 *
6831 * Warning!!!
6832 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6833 * and the injection timeout that we specify when injecting the key.
6834 * We must have the injection timeout (10ms) be smaller than
6835 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6836 *
6837 * If that value changes, this test should also change.
6838 */
6839TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
6840 // Set a long ANR timeout to prevent it from triggering
6841 mFocusedWindow->setDispatchingTimeout(2s);
6842 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6843
6844 tapOnUnfocusedWindow();
6845 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
6846 ASSERT_TRUE(downSequenceNum);
6847 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
6848 ASSERT_TRUE(upSequenceNum);
6849 // Don't finish the events yet, and send a key
6850 // Injection will succeed because we will eventually give up and send the key to the focused
6851 // window even if motions are still being processed.
6852
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006853 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006854 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6855 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006856 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006857 // Key will not be sent to the window, yet, because the window is still processing events
6858 // and the key remains pending, waiting for the touch events to be processed
6859 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
6860 ASSERT_FALSE(keySequenceNum);
6861
6862 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07006863 mFocusedWindow->setFocusable(false);
6864 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006865 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006866 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006867
6868 // Focus events should precede the key events
6869 mUnfocusedWindow->consumeFocusEvent(true);
6870 mFocusedWindow->consumeFocusEvent(false);
6871
6872 // Finish the tap events, which should unblock dispatcher
6873 mUnfocusedWindow->finishEvent(*downSequenceNum);
6874 mUnfocusedWindow->finishEvent(*upSequenceNum);
6875
6876 // Now that all queues are cleared and no backlog in the connections, the key event
6877 // can finally go to the newly focused "mUnfocusedWindow".
6878 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6879 mFocusedWindow->assertNoEvents();
6880 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006881 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006882}
6883
6884// When the touch stream is split across 2 windows, and one of them does not respond,
6885// then ANR should be raised and the touch should be canceled for the unresponsive window.
6886// The other window should not be affected by that.
6887TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
6888 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00006889 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6890 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6891 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006892 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006893 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006894
6895 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00006896 mDispatcher->notifyMotion(
6897 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6898 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006899
6900 const std::chrono::duration timeout =
6901 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006902 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006903
6904 mUnfocusedWindow->consumeMotionDown();
6905 mFocusedWindow->consumeMotionDown();
6906 // Focused window may or may not receive ACTION_MOVE
6907 // But it should definitely receive ACTION_CANCEL due to the ANR
6908 InputEvent* event;
6909 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
6910 ASSERT_TRUE(moveOrCancelSequenceNum);
6911 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
6912 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006913 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006914 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
6915 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
6916 mFocusedWindow->consumeMotionCancel();
6917 } else {
6918 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
6919 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006920 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006921 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6922 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006923
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006924 mUnfocusedWindow->assertNoEvents();
6925 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006926 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006927}
6928
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006929/**
6930 * If we have no focused window, and a key comes in, we start the ANR timer.
6931 * The focused application should add a focused window before the timer runs out to prevent ANR.
6932 *
6933 * If the user touches another application during this time, the key should be dropped.
6934 * Next, if a new focused window comes in, without toggling the focused application,
6935 * then no ANR should occur.
6936 *
6937 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
6938 * but in some cases the policy may not update the focused application.
6939 */
6940TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
6941 std::shared_ptr<FakeApplicationHandle> focusedApplication =
6942 std::make_shared<FakeApplicationHandle>();
6943 focusedApplication->setDispatchingTimeout(60ms);
6944 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
6945 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
6946 mFocusedWindow->setFocusable(false);
6947
6948 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6949 mFocusedWindow->consumeFocusEvent(false);
6950
6951 // Send a key. The ANR timer should start because there is no focused window.
6952 // 'focusedApplication' will get blamed if this timer completes.
6953 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006954 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006955 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6956 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6957 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006958 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006959
6960 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
6961 // then the injected touches won't cause the focused event to get dropped.
6962 // The dispatcher only checks for whether the queue should be pruned upon queueing.
6963 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
6964 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
6965 // For this test, it means that the key would get delivered to the window once it becomes
6966 // focused.
6967 std::this_thread::sleep_for(10ms);
6968
6969 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00006970 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6971 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6972 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05006973
6974 // We do not consume the motion right away, because that would require dispatcher to first
6975 // process (== drop) the key event, and by that time, ANR will be raised.
6976 // Set the focused window first.
6977 mFocusedWindow->setFocusable(true);
6978 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6979 setFocusedWindow(mFocusedWindow);
6980 mFocusedWindow->consumeFocusEvent(true);
6981 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
6982 // to another application. This could be a bug / behaviour in the policy.
6983
6984 mUnfocusedWindow->consumeMotionDown();
6985
6986 ASSERT_TRUE(mDispatcher->waitForIdle());
6987 // Should not ANR because we actually have a focused window. It was just added too slowly.
6988 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
6989}
6990
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05006991// These tests ensure we cannot send touch events to a window that's positioned behind a window
6992// that has feature NO_INPUT_CHANNEL.
6993// Layout:
6994// Top (closest to user)
6995// mNoInputWindow (above all windows)
6996// mBottomWindow
6997// Bottom (furthest from user)
6998class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
6999 virtual void SetUp() override {
7000 InputDispatcherTest::SetUp();
7001
7002 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007003 mNoInputWindow =
7004 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7005 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007006 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007007 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007008 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7009 // It's perfectly valid for this window to not have an associated input channel
7010
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007011 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7012 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007013 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7014
7015 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7016 }
7017
7018protected:
7019 std::shared_ptr<FakeApplicationHandle> mApplication;
7020 sp<FakeWindowHandle> mNoInputWindow;
7021 sp<FakeWindowHandle> mBottomWindow;
7022};
7023
7024TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7025 PointF touchedPoint = {10, 10};
7026
Prabir Pradhan678438e2023-04-13 19:32:51 +00007027 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7028 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7029 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007030
7031 mNoInputWindow->assertNoEvents();
7032 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7033 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7034 // and therefore should prevent mBottomWindow from receiving touches
7035 mBottomWindow->assertNoEvents();
7036}
7037
7038/**
7039 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7040 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7041 */
7042TEST_F(InputDispatcherMultiWindowOcclusionTests,
7043 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007044 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7045 "Window with input channel and NO_INPUT_CHANNEL",
7046 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007047
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007048 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007049 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7050 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7051
7052 PointF touchedPoint = {10, 10};
7053
Prabir Pradhan678438e2023-04-13 19:32:51 +00007054 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7055 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7056 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007057
7058 mNoInputWindow->assertNoEvents();
7059 mBottomWindow->assertNoEvents();
7060}
7061
Vishnu Nair958da932020-08-21 17:12:37 -07007062class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7063protected:
7064 std::shared_ptr<FakeApplicationHandle> mApp;
7065 sp<FakeWindowHandle> mWindow;
7066 sp<FakeWindowHandle> mMirror;
7067
7068 virtual void SetUp() override {
7069 InputDispatcherTest::SetUp();
7070 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007071 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7072 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7073 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007074 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7075 mWindow->setFocusable(true);
7076 mMirror->setFocusable(true);
7077 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7078 }
7079};
7080
7081TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7082 // Request focus on a mirrored window
7083 setFocusedWindow(mMirror);
7084
7085 // window gets focused
7086 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007087 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7088 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007089 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7090}
7091
7092// A focused & mirrored window remains focused only if the window and its mirror are both
7093// focusable.
7094TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7095 setFocusedWindow(mMirror);
7096
7097 // window gets focused
7098 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007099 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7100 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007101 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007102 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7103 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007104 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7105
7106 mMirror->setFocusable(false);
7107 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7108
7109 // window loses focus since one of the windows associated with the token in not focusable
7110 mWindow->consumeFocusEvent(false);
7111
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007112 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7113 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007114 mWindow->assertNoEvents();
7115}
7116
7117// A focused & mirrored window remains focused until the window and its mirror both become
7118// invisible.
7119TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7120 setFocusedWindow(mMirror);
7121
7122 // window gets focused
7123 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007124 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7125 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007126 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007127 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7128 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007129 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7130
7131 mMirror->setVisible(false);
7132 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7133
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007134 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7135 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007136 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007137 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7138 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007139 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7140
7141 mWindow->setVisible(false);
7142 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7143
7144 // window loses focus only after all windows associated with the token become invisible.
7145 mWindow->consumeFocusEvent(false);
7146
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007147 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7148 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007149 mWindow->assertNoEvents();
7150}
7151
7152// A focused & mirrored window remains focused until both windows are removed.
7153TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7154 setFocusedWindow(mMirror);
7155
7156 // window gets focused
7157 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007158 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7159 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007160 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007161 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7162 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007163 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7164
7165 // single window is removed but the window token remains focused
7166 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7167
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007168 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7169 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007170 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007171 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7172 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007173 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7174
7175 // Both windows are removed
7176 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7177 mWindow->consumeFocusEvent(false);
7178
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007179 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7180 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007181 mWindow->assertNoEvents();
7182}
7183
7184// Focus request can be pending until one window becomes visible.
7185TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7186 // Request focus on an invisible mirror.
7187 mWindow->setVisible(false);
7188 mMirror->setVisible(false);
7189 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7190 setFocusedWindow(mMirror);
7191
7192 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007193 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007194 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7195 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007196
7197 mMirror->setVisible(true);
7198 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7199
7200 // window gets focused
7201 mWindow->consumeFocusEvent(true);
7202 // window gets the pending key event
7203 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7204}
Prabir Pradhan99987712020-11-10 18:43:05 -08007205
7206class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7207protected:
7208 std::shared_ptr<FakeApplicationHandle> mApp;
7209 sp<FakeWindowHandle> mWindow;
7210 sp<FakeWindowHandle> mSecondWindow;
7211
7212 void SetUp() override {
7213 InputDispatcherTest::SetUp();
7214 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007215 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007216 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007217 mSecondWindow =
7218 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007219 mSecondWindow->setFocusable(true);
7220
7221 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7222 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7223
7224 setFocusedWindow(mWindow);
7225 mWindow->consumeFocusEvent(true);
7226 }
7227
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007228 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007229 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007230 }
7231
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007232 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7233 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007234 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007235 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7236 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007237 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007238 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007239 }
7240};
7241
7242TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7243 // Ensure that capture cannot be obtained for unfocused windows.
7244 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7245 mFakePolicy->assertSetPointerCaptureNotCalled();
7246 mSecondWindow->assertNoEvents();
7247
7248 // Ensure that capture can be enabled from the focus window.
7249 requestAndVerifyPointerCapture(mWindow, true);
7250
7251 // Ensure that capture cannot be disabled from a window that does not have capture.
7252 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7253 mFakePolicy->assertSetPointerCaptureNotCalled();
7254
7255 // Ensure that capture can be disabled from the window with capture.
7256 requestAndVerifyPointerCapture(mWindow, false);
7257}
7258
7259TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007260 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007261
7262 setFocusedWindow(mSecondWindow);
7263
7264 // Ensure that the capture disabled event was sent first.
7265 mWindow->consumeCaptureEvent(false);
7266 mWindow->consumeFocusEvent(false);
7267 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007268 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007269
7270 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007271 notifyPointerCaptureChanged({});
7272 notifyPointerCaptureChanged(request);
7273 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007274 mWindow->assertNoEvents();
7275 mSecondWindow->assertNoEvents();
7276 mFakePolicy->assertSetPointerCaptureNotCalled();
7277}
7278
7279TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007280 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007281
7282 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007283 notifyPointerCaptureChanged({});
7284 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007285
7286 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007287 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007288 mWindow->consumeCaptureEvent(false);
7289 mWindow->assertNoEvents();
7290}
7291
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007292TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7293 requestAndVerifyPointerCapture(mWindow, true);
7294
7295 // The first window loses focus.
7296 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007297 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007298 mWindow->consumeCaptureEvent(false);
7299
7300 // Request Pointer Capture from the second window before the notification from InputReader
7301 // arrives.
7302 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007303 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007304
7305 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007306 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007307
7308 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007309 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007310
7311 mSecondWindow->consumeFocusEvent(true);
7312 mSecondWindow->consumeCaptureEvent(true);
7313}
7314
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007315TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7316 // App repeatedly enables and disables capture.
7317 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7318 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7319 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7320 mFakePolicy->assertSetPointerCaptureCalled(false);
7321 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7322 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7323
7324 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7325 // first request is now stale, this should do nothing.
7326 notifyPointerCaptureChanged(firstRequest);
7327 mWindow->assertNoEvents();
7328
7329 // InputReader notifies that the second request was enabled.
7330 notifyPointerCaptureChanged(secondRequest);
7331 mWindow->consumeCaptureEvent(true);
7332}
7333
Prabir Pradhan7092e262022-05-03 16:51:09 +00007334TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7335 requestAndVerifyPointerCapture(mWindow, true);
7336
7337 // App toggles pointer capture off and on.
7338 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7339 mFakePolicy->assertSetPointerCaptureCalled(false);
7340
7341 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7342 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7343
7344 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7345 // preceding "disable" request.
7346 notifyPointerCaptureChanged(enableRequest);
7347
7348 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7349 // any notifications.
7350 mWindow->assertNoEvents();
7351}
7352
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007353class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7354protected:
7355 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007356
7357 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7358 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7359
7360 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7361 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7362
7363 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7364 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7365 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7366 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7367 MAXIMUM_OBSCURING_OPACITY);
7368
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007369 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007370 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007371 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007372
7373 sp<FakeWindowHandle> mTouchWindow;
7374
7375 virtual void SetUp() override {
7376 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007377 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007378 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7379 }
7380
7381 virtual void TearDown() override {
7382 InputDispatcherTest::TearDown();
7383 mTouchWindow.clear();
7384 }
7385
chaviw3277faf2021-05-19 16:45:23 -05007386 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7387 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007388 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007389 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007390 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007391 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007392 return window;
7393 }
7394
7395 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7396 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7397 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007398 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007399 // Generate an arbitrary PID based on the UID
7400 window->setOwnerInfo(1777 + (uid % 10000), uid);
7401 return window;
7402 }
7403
7404 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007405 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7406 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7407 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007408 }
7409};
7410
7411TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007412 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007413 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007414 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007415
7416 touch();
7417
7418 mTouchWindow->assertNoEvents();
7419}
7420
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007421TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007422 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7423 const sp<FakeWindowHandle>& w =
7424 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7425 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7426
7427 touch();
7428
7429 mTouchWindow->assertNoEvents();
7430}
7431
7432TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007433 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7434 const sp<FakeWindowHandle>& w =
7435 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7436 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7437
7438 touch();
7439
7440 w->assertNoEvents();
7441}
7442
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007443TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007444 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7445 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007446
7447 touch();
7448
7449 mTouchWindow->consumeAnyMotionDown();
7450}
7451
7452TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007453 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007454 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007455 w->setFrame(Rect(0, 0, 50, 50));
7456 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007457
7458 touch({PointF{100, 100}});
7459
7460 mTouchWindow->consumeAnyMotionDown();
7461}
7462
7463TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007464 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007465 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007466 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7467
7468 touch();
7469
7470 mTouchWindow->consumeAnyMotionDown();
7471}
7472
7473TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7474 const sp<FakeWindowHandle>& w =
7475 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7476 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007477
7478 touch();
7479
7480 mTouchWindow->consumeAnyMotionDown();
7481}
7482
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007483TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7484 const sp<FakeWindowHandle>& w =
7485 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7486 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7487
7488 touch();
7489
7490 w->assertNoEvents();
7491}
7492
7493/**
7494 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7495 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7496 * window, the occluding window will still receive ACTION_OUTSIDE event.
7497 */
7498TEST_F(InputDispatcherUntrustedTouchesTest,
7499 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7500 const sp<FakeWindowHandle>& w =
7501 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007502 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007503 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7504
7505 touch();
7506
7507 w->consumeMotionOutside();
7508}
7509
7510TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
7511 const sp<FakeWindowHandle>& w =
7512 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007513 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007514 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7515
7516 touch();
7517
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007518 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007519}
7520
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007521TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007522 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007523 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7524 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007525 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7526
7527 touch();
7528
7529 mTouchWindow->consumeAnyMotionDown();
7530}
7531
7532TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
7533 const sp<FakeWindowHandle>& w =
7534 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7535 MAXIMUM_OBSCURING_OPACITY);
7536 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007537
7538 touch();
7539
7540 mTouchWindow->consumeAnyMotionDown();
7541}
7542
7543TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007544 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007545 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7546 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007547 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7548
7549 touch();
7550
7551 mTouchWindow->assertNoEvents();
7552}
7553
7554TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
7555 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
7556 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007557 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7558 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007559 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007560 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7561 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007562 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7563
7564 touch();
7565
7566 mTouchWindow->assertNoEvents();
7567}
7568
7569TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
7570 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
7571 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007572 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7573 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007574 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007575 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7576 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007577 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7578
7579 touch();
7580
7581 mTouchWindow->consumeAnyMotionDown();
7582}
7583
7584TEST_F(InputDispatcherUntrustedTouchesTest,
7585 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
7586 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007587 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7588 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007589 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007590 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7591 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007592 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7593
7594 touch();
7595
7596 mTouchWindow->consumeAnyMotionDown();
7597}
7598
7599TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
7600 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007601 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7602 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007603 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007604 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7605 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007606 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007607
7608 touch();
7609
7610 mTouchWindow->assertNoEvents();
7611}
7612
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007613TEST_F(InputDispatcherUntrustedTouchesTest,
7614 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
7615 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007616 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7617 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007618 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007619 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7620 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007621 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7622
7623 touch();
7624
7625 mTouchWindow->assertNoEvents();
7626}
7627
7628TEST_F(InputDispatcherUntrustedTouchesTest,
7629 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
7630 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007631 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7632 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007633 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007634 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7635 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007636 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7637
7638 touch();
7639
7640 mTouchWindow->consumeAnyMotionDown();
7641}
7642
7643TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
7644 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007645 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7646 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007647 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7648
7649 touch();
7650
7651 mTouchWindow->consumeAnyMotionDown();
7652}
7653
7654TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
7655 const sp<FakeWindowHandle>& w =
7656 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
7657 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7658
7659 touch();
7660
7661 mTouchWindow->consumeAnyMotionDown();
7662}
7663
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007664TEST_F(InputDispatcherUntrustedTouchesTest,
7665 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
7666 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7667 const sp<FakeWindowHandle>& w =
7668 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
7669 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7670
7671 touch();
7672
7673 mTouchWindow->assertNoEvents();
7674}
7675
7676TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
7677 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7678 const sp<FakeWindowHandle>& w =
7679 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
7680 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7681
7682 touch();
7683
7684 mTouchWindow->consumeAnyMotionDown();
7685}
7686
7687TEST_F(InputDispatcherUntrustedTouchesTest,
7688 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
7689 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
7690 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007691 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7692 OPACITY_ABOVE_THRESHOLD);
7693 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7694
7695 touch();
7696
7697 mTouchWindow->consumeAnyMotionDown();
7698}
7699
7700TEST_F(InputDispatcherUntrustedTouchesTest,
7701 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
7702 const sp<FakeWindowHandle>& w1 =
7703 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7704 OPACITY_BELOW_THRESHOLD);
7705 const sp<FakeWindowHandle>& w2 =
7706 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7707 OPACITY_BELOW_THRESHOLD);
7708 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7709
7710 touch();
7711
7712 mTouchWindow->assertNoEvents();
7713}
7714
7715/**
7716 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
7717 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
7718 * (which alone would result in allowing touches) does not affect the blocking behavior.
7719 */
7720TEST_F(InputDispatcherUntrustedTouchesTest,
7721 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
7722 const sp<FakeWindowHandle>& wB =
7723 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7724 OPACITY_BELOW_THRESHOLD);
7725 const sp<FakeWindowHandle>& wC =
7726 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7727 OPACITY_BELOW_THRESHOLD);
7728 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7729
7730 touch();
7731
7732 mTouchWindow->assertNoEvents();
7733}
7734
7735/**
7736 * This test is testing that a window from a different UID but with same application token doesn't
7737 * block the touch. Apps can share the application token for close UI collaboration for example.
7738 */
7739TEST_F(InputDispatcherUntrustedTouchesTest,
7740 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
7741 const sp<FakeWindowHandle>& w =
7742 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7743 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007744 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7745
7746 touch();
7747
7748 mTouchWindow->consumeAnyMotionDown();
7749}
7750
arthurhungb89ccb02020-12-30 16:19:01 +08007751class InputDispatcherDragTests : public InputDispatcherTest {
7752protected:
7753 std::shared_ptr<FakeApplicationHandle> mApp;
7754 sp<FakeWindowHandle> mWindow;
7755 sp<FakeWindowHandle> mSecondWindow;
7756 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007757 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007758 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
7759 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08007760
7761 void SetUp() override {
7762 InputDispatcherTest::SetUp();
7763 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007764 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007765 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007766
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007767 mSecondWindow =
7768 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007769 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007770
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007771 mSpyWindow =
7772 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007773 mSpyWindow->setSpy(true);
7774 mSpyWindow->setTrustedOverlay(true);
7775 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
7776
arthurhungb89ccb02020-12-30 16:19:01 +08007777 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007778 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007779 }
7780
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007781 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
7782 switch (fromSource) {
7783 case AINPUT_SOURCE_TOUCHSCREEN:
7784 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7785 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7786 ADISPLAY_ID_DEFAULT, {50, 50}))
7787 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7788 break;
7789 case AINPUT_SOURCE_STYLUS:
7790 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7791 injectMotionEvent(
7792 mDispatcher,
7793 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
7794 AINPUT_SOURCE_STYLUS)
7795 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007796 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007797 .x(50)
7798 .y(50))
7799 .build()));
7800 break;
7801 case AINPUT_SOURCE_MOUSE:
7802 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7803 injectMotionEvent(
7804 mDispatcher,
7805 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
7806 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7807 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007808 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007809 .x(50)
7810 .y(50))
7811 .build()));
7812 break;
7813 default:
7814 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
7815 }
arthurhungb89ccb02020-12-30 16:19:01 +08007816
7817 // Window should receive motion event.
7818 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007819 // Spy window should also receive motion event
7820 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00007821 }
7822
7823 // Start performing drag, we will create a drag window and transfer touch to it.
7824 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
7825 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007826 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00007827 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007828 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00007829 }
arthurhungb89ccb02020-12-30 16:19:01 +08007830
7831 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007832 mDragWindow =
7833 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007834 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08007835 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007836 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007837
7838 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00007839 bool transferred =
7840 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00007841 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00007842 if (transferred) {
7843 mWindow->consumeMotionCancel();
7844 mDragWindow->consumeMotionDown();
7845 }
7846 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08007847 }
7848};
7849
7850TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007851 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08007852
7853 // Move on window.
7854 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7855 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7856 ADISPLAY_ID_DEFAULT, {50, 50}))
7857 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7858 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7859 mWindow->consumeDragEvent(false, 50, 50);
7860 mSecondWindow->assertNoEvents();
7861
7862 // Move to another window.
7863 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7864 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7865 ADISPLAY_ID_DEFAULT, {150, 50}))
7866 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7867 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7868 mWindow->consumeDragEvent(true, 150, 50);
7869 mSecondWindow->consumeDragEvent(false, 50, 50);
7870
7871 // Move back to original window.
7872 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7873 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7874 ADISPLAY_ID_DEFAULT, {50, 50}))
7875 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7876 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7877 mWindow->consumeDragEvent(false, 50, 50);
7878 mSecondWindow->consumeDragEvent(true, -50, 50);
7879
7880 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7881 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
7882 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7883 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7884 mWindow->assertNoEvents();
7885 mSecondWindow->assertNoEvents();
7886}
7887
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007888TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007889 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007890
7891 // No cancel event after drag start
7892 mSpyWindow->assertNoEvents();
7893
7894 const MotionEvent secondFingerDownEvent =
7895 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7896 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007897 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
7898 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007899 .build();
7900 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7901 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
7902 InputEventInjectionSync::WAIT_FOR_RESULT))
7903 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7904
7905 // Receives cancel for first pointer after next pointer down
7906 mSpyWindow->consumeMotionCancel();
7907 mSpyWindow->consumeMotionDown();
7908
7909 mSpyWindow->assertNoEvents();
7910}
7911
arthurhungf452d0b2021-01-06 00:19:52 +08007912TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007913 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08007914
7915 // Move on window.
7916 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7917 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7918 ADISPLAY_ID_DEFAULT, {50, 50}))
7919 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7920 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7921 mWindow->consumeDragEvent(false, 50, 50);
7922 mSecondWindow->assertNoEvents();
7923
7924 // Move to another window.
7925 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7926 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7927 ADISPLAY_ID_DEFAULT, {150, 50}))
7928 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7929 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7930 mWindow->consumeDragEvent(true, 150, 50);
7931 mSecondWindow->consumeDragEvent(false, 50, 50);
7932
7933 // drop to another window.
7934 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7935 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7936 {150, 50}))
7937 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7938 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7939 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7940 mWindow->assertNoEvents();
7941 mSecondWindow->assertNoEvents();
7942}
7943
arthurhung6d4bed92021-03-17 11:59:33 +08007944TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007945 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08007946
7947 // Move on window and keep button pressed.
7948 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7949 injectMotionEvent(mDispatcher,
7950 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7951 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007952 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08007953 .build()))
7954 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7955 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7956 mWindow->consumeDragEvent(false, 50, 50);
7957 mSecondWindow->assertNoEvents();
7958
7959 // Move to another window and release button, expect to drop item.
7960 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7961 injectMotionEvent(mDispatcher,
7962 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
7963 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007964 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08007965 .build()))
7966 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7967 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7968 mWindow->assertNoEvents();
7969 mSecondWindow->assertNoEvents();
7970 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
7971
7972 // nothing to the window.
7973 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7974 injectMotionEvent(mDispatcher,
7975 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
7976 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007977 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08007978 .build()))
7979 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7980 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
7981 mWindow->assertNoEvents();
7982 mSecondWindow->assertNoEvents();
7983}
7984
Arthur Hung54745652022-04-20 07:17:41 +00007985TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007986 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08007987
7988 // Set second window invisible.
7989 mSecondWindow->setVisible(false);
7990 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
7991
7992 // Move on window.
7993 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7994 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7995 ADISPLAY_ID_DEFAULT, {50, 50}))
7996 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7997 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7998 mWindow->consumeDragEvent(false, 50, 50);
7999 mSecondWindow->assertNoEvents();
8000
8001 // Move to another window.
8002 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8003 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8004 ADISPLAY_ID_DEFAULT, {150, 50}))
8005 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8006 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8007 mWindow->consumeDragEvent(true, 150, 50);
8008 mSecondWindow->assertNoEvents();
8009
8010 // drop to another window.
8011 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8012 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8013 {150, 50}))
8014 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8015 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8016 mFakePolicy->assertDropTargetEquals(nullptr);
8017 mWindow->assertNoEvents();
8018 mSecondWindow->assertNoEvents();
8019}
8020
Arthur Hung54745652022-04-20 07:17:41 +00008021TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008022 // Ensure window could track pointerIds if it didn't support split touch.
8023 mWindow->setPreventSplitting(true);
8024
Arthur Hung54745652022-04-20 07:17:41 +00008025 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8026 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8027 {50, 50}))
8028 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8029 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8030
8031 const MotionEvent secondFingerDownEvent =
8032 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8033 .displayId(ADISPLAY_ID_DEFAULT)
8034 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008035 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8036 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008037 .build();
8038 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8039 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8040 InputEventInjectionSync::WAIT_FOR_RESULT))
8041 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008042 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008043
8044 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008045 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008046}
8047
8048TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8049 // First down on second window.
8050 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8051 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8052 {150, 50}))
8053 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8054
8055 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8056
8057 // Second down on first window.
8058 const MotionEvent secondFingerDownEvent =
8059 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8060 .displayId(ADISPLAY_ID_DEFAULT)
8061 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008062 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8063 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008064 .build();
8065 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8066 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8067 InputEventInjectionSync::WAIT_FOR_RESULT))
8068 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8069 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8070
8071 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008072 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008073
8074 // Move on window.
8075 const MotionEvent secondFingerMoveEvent =
8076 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8077 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008078 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8079 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008080 .build();
8081 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8082 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8083 InputEventInjectionSync::WAIT_FOR_RESULT));
8084 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8085 mWindow->consumeDragEvent(false, 50, 50);
8086 mSecondWindow->consumeMotionMove();
8087
8088 // Release the drag pointer should perform drop.
8089 const MotionEvent secondFingerUpEvent =
8090 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8091 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008092 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8093 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008094 .build();
8095 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8096 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8097 InputEventInjectionSync::WAIT_FOR_RESULT));
8098 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8099 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8100 mWindow->assertNoEvents();
8101 mSecondWindow->consumeMotionMove();
8102}
8103
Arthur Hung3915c1f2022-05-31 07:17:17 +00008104TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008105 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008106
8107 // Update window of second display.
8108 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008109 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008110 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8111
8112 // Let second display has a touch state.
8113 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8114 injectMotionEvent(mDispatcher,
8115 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8116 AINPUT_SOURCE_TOUCHSCREEN)
8117 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008118 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008119 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008120 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008121 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008122 // Update window again.
8123 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8124
8125 // Move on window.
8126 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8127 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8128 ADISPLAY_ID_DEFAULT, {50, 50}))
8129 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8130 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8131 mWindow->consumeDragEvent(false, 50, 50);
8132 mSecondWindow->assertNoEvents();
8133
8134 // Move to another window.
8135 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8136 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8137 ADISPLAY_ID_DEFAULT, {150, 50}))
8138 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8139 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8140 mWindow->consumeDragEvent(true, 150, 50);
8141 mSecondWindow->consumeDragEvent(false, 50, 50);
8142
8143 // drop to another window.
8144 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8145 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8146 {150, 50}))
8147 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8148 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8149 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8150 mWindow->assertNoEvents();
8151 mSecondWindow->assertNoEvents();
8152}
8153
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008154TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8155 startDrag(true, AINPUT_SOURCE_MOUSE);
8156 // Move on window.
8157 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8158 injectMotionEvent(mDispatcher,
8159 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8160 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8161 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008162 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008163 .x(50)
8164 .y(50))
8165 .build()))
8166 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8167 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8168 mWindow->consumeDragEvent(false, 50, 50);
8169 mSecondWindow->assertNoEvents();
8170
8171 // Move to another window.
8172 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8173 injectMotionEvent(mDispatcher,
8174 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8175 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8176 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008177 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008178 .x(150)
8179 .y(50))
8180 .build()))
8181 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8182 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8183 mWindow->consumeDragEvent(true, 150, 50);
8184 mSecondWindow->consumeDragEvent(false, 50, 50);
8185
8186 // drop to another window.
8187 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8188 injectMotionEvent(mDispatcher,
8189 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8190 .buttonState(0)
8191 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008192 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008193 .x(150)
8194 .y(50))
8195 .build()))
8196 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8197 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8198 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8199 mWindow->assertNoEvents();
8200 mSecondWindow->assertNoEvents();
8201}
8202
Vishnu Nair062a8672021-09-03 16:07:44 -07008203class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8204
8205TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8206 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008207 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8208 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008209 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008210 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8211 window->setFocusable(true);
8212 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8213 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008214 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008215
8216 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008217 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008218 window->assertNoEvents();
8219
Prabir Pradhan678438e2023-04-13 19:32:51 +00008220 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8221 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008222 window->assertNoEvents();
8223
8224 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008225 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008226 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8227
Prabir Pradhan678438e2023-04-13 19:32:51 +00008228 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008229 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8230
Prabir Pradhan678438e2023-04-13 19:32:51 +00008231 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8232 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008233 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8234 window->assertNoEvents();
8235}
8236
8237TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8238 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8239 std::make_shared<FakeApplicationHandle>();
8240 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008241 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8242 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008243 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8244 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008245 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008246 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008247 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8248 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008249 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008250 window->setOwnerInfo(222, 222);
8251 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8252 window->setFocusable(true);
8253 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8254 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008255 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008256
8257 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008258 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008259 window->assertNoEvents();
8260
Prabir Pradhan678438e2023-04-13 19:32:51 +00008261 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8262 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008263 window->assertNoEvents();
8264
8265 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008266 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008267 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8268
Prabir Pradhan678438e2023-04-13 19:32:51 +00008269 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008270 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8271
Prabir Pradhan678438e2023-04-13 19:32:51 +00008272 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8273 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008274 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8275 window->assertNoEvents();
8276}
8277
8278TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8279 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8280 std::make_shared<FakeApplicationHandle>();
8281 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008282 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8283 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008284 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8285 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008286 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008287 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008288 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8289 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008290 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008291 window->setOwnerInfo(222, 222);
8292 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8293 window->setFocusable(true);
8294 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8295 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008296 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008297
8298 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008299 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008300 window->assertNoEvents();
8301
Prabir Pradhan678438e2023-04-13 19:32:51 +00008302 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8303 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008304 window->assertNoEvents();
8305
8306 // When the window is no longer obscured because it went on top, it should get input
8307 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8308
Prabir Pradhan678438e2023-04-13 19:32:51 +00008309 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008310 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8311
Prabir Pradhan678438e2023-04-13 19:32:51 +00008312 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8313 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008314 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8315 window->assertNoEvents();
8316}
8317
Antonio Kantekf16f2832021-09-28 04:39:20 +00008318class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8319protected:
8320 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008321 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008322 sp<FakeWindowHandle> mWindow;
8323 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008324 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008325
8326 void SetUp() override {
8327 InputDispatcherTest::SetUp();
8328
8329 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008330 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008331 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008332 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008333 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008334 mSecondWindow =
8335 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008336 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008337 mThirdWindow =
8338 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8339 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8340 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008341
8342 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008343 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8344 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8345 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008346 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008347
Antonio Kantek15beb512022-06-13 22:35:41 +00008348 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008349 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008350 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008351 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8352 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008353 mThirdWindow->assertNoEvents();
8354 }
8355
8356 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8357 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008358 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008359 SECOND_DISPLAY_ID)) {
8360 mWindow->assertNoEvents();
8361 mSecondWindow->assertNoEvents();
8362 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008363 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008364 }
8365
Antonio Kantek15beb512022-06-13 22:35:41 +00008366 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8367 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008368 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8369 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008370 mWindow->consumeTouchModeEvent(inTouchMode);
8371 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008372 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008373 }
8374};
8375
Antonio Kantek26defcf2022-02-08 01:12:27 +00008376TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008377 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008378 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8379 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008380 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008381}
8382
Antonio Kantek26defcf2022-02-08 01:12:27 +00008383TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8384 const WindowInfo& windowInfo = *mWindow->getInfo();
8385 int32_t ownerPid = windowInfo.ownerPid;
8386 int32_t ownerUid = windowInfo.ownerUid;
8387 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8388 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008389 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008390 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008391 mWindow->assertNoEvents();
8392 mSecondWindow->assertNoEvents();
8393}
8394
8395TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8396 const WindowInfo& windowInfo = *mWindow->getInfo();
8397 int32_t ownerPid = windowInfo.ownerPid;
8398 int32_t ownerUid = windowInfo.ownerUid;
8399 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008400 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008401 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008402}
8403
Antonio Kantekf16f2832021-09-28 04:39:20 +00008404TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008405 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008406 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8407 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008408 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008409 mWindow->assertNoEvents();
8410 mSecondWindow->assertNoEvents();
8411}
8412
Antonio Kantek15beb512022-06-13 22:35:41 +00008413TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8414 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8415 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8416 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008417 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008418 mWindow->assertNoEvents();
8419 mSecondWindow->assertNoEvents();
8420 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8421}
8422
Antonio Kantek48710e42022-03-24 14:19:30 -07008423TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8424 // Interact with the window first.
8425 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8426 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8427 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8428
8429 // Then remove focus.
8430 mWindow->setFocusable(false);
8431 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8432
8433 // Assert that caller can switch touch mode by owning one of the last interacted window.
8434 const WindowInfo& windowInfo = *mWindow->getInfo();
8435 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8436 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008437 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008438}
8439
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008440class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8441public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008442 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008443 std::shared_ptr<FakeApplicationHandle> application =
8444 std::make_shared<FakeApplicationHandle>();
8445 std::string name = "Fake Spy ";
8446 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008447 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8448 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008449 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008450 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008451 return spy;
8452 }
8453
8454 sp<FakeWindowHandle> createForeground() {
8455 std::shared_ptr<FakeApplicationHandle> application =
8456 std::make_shared<FakeApplicationHandle>();
8457 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008458 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8459 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008460 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008461 return window;
8462 }
8463
8464private:
8465 int mSpyCount{0};
8466};
8467
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008468using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008469/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008470 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8471 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008472TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8473 ScopedSilentDeath _silentDeath;
8474
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008475 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008476 spy->setTrustedOverlay(false);
8477 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8478 ".* not a trusted overlay");
8479}
8480
8481/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008482 * Input injection into a display with a spy window but no foreground windows should succeed.
8483 */
8484TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008485 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008486 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8487
8488 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8489 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8490 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8491 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8492}
8493
8494/**
8495 * Verify the order in which different input windows receive events. The touched foreground window
8496 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8497 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8498 * receive events before ones belows it.
8499 *
8500 * Here, we set up a scenario with four windows in the following Z order from the top:
8501 * spy1, spy2, window, spy3.
8502 * We then inject an event and verify that the foreground "window" receives it first, followed by
8503 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
8504 * window.
8505 */
8506TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
8507 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008508 auto spy1 = createSpy();
8509 auto spy2 = createSpy();
8510 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008511 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
8512 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
8513 const size_t numChannels = channels.size();
8514
Michael Wright8e9a8562022-02-09 13:44:29 +00008515 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008516 if (!epollFd.ok()) {
8517 FAIL() << "Failed to create epoll fd";
8518 }
8519
8520 for (size_t i = 0; i < numChannels; i++) {
8521 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
8522 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
8523 FAIL() << "Failed to add fd to epoll";
8524 }
8525 }
8526
8527 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8528 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8529 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8530
8531 std::vector<size_t> eventOrder;
8532 std::vector<struct epoll_event> events(numChannels);
8533 for (;;) {
8534 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
8535 (100ms).count());
8536 if (nFds < 0) {
8537 FAIL() << "Failed to call epoll_wait";
8538 }
8539 if (nFds == 0) {
8540 break; // epoll_wait timed out
8541 }
8542 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07008543 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07008544 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008545 channels[i]->consumeMotionDown();
8546 }
8547 }
8548
8549 // Verify the order in which the events were received.
8550 EXPECT_EQ(3u, eventOrder.size());
8551 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
8552 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
8553 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
8554}
8555
8556/**
8557 * A spy window using the NOT_TOUCHABLE flag does not receive events.
8558 */
8559TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
8560 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008561 auto spy = createSpy();
8562 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008563 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8564
8565 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8566 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8567 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8568 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8569 spy->assertNoEvents();
8570}
8571
8572/**
8573 * A spy window will only receive gestures that originate within its touchable region. Gestures that
8574 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
8575 * to the window.
8576 */
8577TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
8578 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008579 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008580 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
8581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8582
8583 // Inject an event outside the spy window's touchable region.
8584 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8585 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8586 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8587 window->consumeMotionDown();
8588 spy->assertNoEvents();
8589 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8590 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8591 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8592 window->consumeMotionUp();
8593 spy->assertNoEvents();
8594
8595 // Inject an event inside the spy window's touchable region.
8596 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8597 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8598 {5, 10}))
8599 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8600 window->consumeMotionDown();
8601 spy->consumeMotionDown();
8602}
8603
8604/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008605 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008606 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008607 */
8608TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
8609 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008610 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008611 auto spy = createSpy();
8612 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008613 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008614 spy->setFrame(Rect{0, 0, 20, 20});
8615 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8616
8617 // Inject an event outside the spy window's frame and touchable region.
8618 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008619 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8620 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008621 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8622 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008623 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008624}
8625
8626/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008627 * Even when a spy window spans over multiple foreground windows, the spy should receive all
8628 * pointers that are down within its bounds.
8629 */
8630TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
8631 auto windowLeft = createForeground();
8632 windowLeft->setFrame({0, 0, 100, 200});
8633 auto windowRight = createForeground();
8634 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008635 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008636 spy->setFrame({0, 0, 200, 200});
8637 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
8638
8639 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8640 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8641 {50, 50}))
8642 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8643 windowLeft->consumeMotionDown();
8644 spy->consumeMotionDown();
8645
8646 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008647 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008648 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008649 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8650 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008651 .build();
8652 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8653 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8654 InputEventInjectionSync::WAIT_FOR_RESULT))
8655 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8656 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00008657 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008658}
8659
8660/**
8661 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
8662 * the spy should receive the second pointer with ACTION_DOWN.
8663 */
8664TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
8665 auto window = createForeground();
8666 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008667 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008668 spyRight->setFrame({100, 0, 200, 200});
8669 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
8670
8671 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8672 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8673 {50, 50}))
8674 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8675 window->consumeMotionDown();
8676 spyRight->assertNoEvents();
8677
8678 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008679 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008680 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008681 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8682 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008683 .build();
8684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8685 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8686 InputEventInjectionSync::WAIT_FOR_RESULT))
8687 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008688 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008689 spyRight->consumeMotionDown();
8690}
8691
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008692/**
8693 * The spy window should not be able to affect whether or not touches are split. Only the foreground
8694 * windows should be allowed to control split touch.
8695 */
8696TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008697 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008698 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008699 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008700 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008701
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008702 auto window = createForeground();
8703 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008704
8705 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8706
8707 // First finger down, no window touched.
8708 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8709 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8710 {100, 200}))
8711 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8712 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8713 window->assertNoEvents();
8714
8715 // Second finger down on window, the window should receive touch down.
8716 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008717 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008718 .displayId(ADISPLAY_ID_DEFAULT)
8719 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008720 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8721 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008722 .build();
8723 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8724 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8725 InputEventInjectionSync::WAIT_FOR_RESULT))
8726 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8727
8728 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00008729 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008730}
8731
8732/**
8733 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
8734 * do not receive key events.
8735 */
8736TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008737 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008738 spy->setFocusable(false);
8739
8740 auto window = createForeground();
8741 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8742 setFocusedWindow(window);
8743 window->consumeFocusEvent(true);
8744
8745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
8746 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8747 window->consumeKeyDown(ADISPLAY_ID_NONE);
8748
8749 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
8750 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8751 window->consumeKeyUp(ADISPLAY_ID_NONE);
8752
8753 spy->assertNoEvents();
8754}
8755
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008756using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
8757
8758/**
8759 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
8760 * are currently sent to any other windows - including other spy windows - will also be cancelled.
8761 */
8762TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
8763 auto window = createForeground();
8764 auto spy1 = createSpy();
8765 auto spy2 = createSpy();
8766 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
8767
8768 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8769 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8770 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8771 window->consumeMotionDown();
8772 spy1->consumeMotionDown();
8773 spy2->consumeMotionDown();
8774
8775 // Pilfer pointers from the second spy window.
8776 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
8777 spy2->assertNoEvents();
8778 spy1->consumeMotionCancel();
8779 window->consumeMotionCancel();
8780
8781 // The rest of the gesture should only be sent to the second spy window.
8782 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8783 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8784 ADISPLAY_ID_DEFAULT))
8785 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8786 spy2->consumeMotionMove();
8787 spy1->assertNoEvents();
8788 window->assertNoEvents();
8789}
8790
8791/**
8792 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
8793 * in the middle of the gesture.
8794 */
8795TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
8796 auto window = createForeground();
8797 auto spy = createSpy();
8798 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8799
8800 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8801 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8802 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8803 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8804 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8805
8806 window->releaseChannel();
8807
8808 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8809
8810 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8811 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8812 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8813 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8814}
8815
8816/**
8817 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
8818 * the spy, but not to any other windows.
8819 */
8820TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
8821 auto spy = createSpy();
8822 auto window = createForeground();
8823
8824 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8825
8826 // First finger down on the window and the spy.
8827 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8828 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8829 {100, 200}))
8830 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8831 spy->consumeMotionDown();
8832 window->consumeMotionDown();
8833
8834 // Spy window pilfers the pointers.
8835 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8836 window->consumeMotionCancel();
8837
8838 // Second finger down on the window and spy, but the window should not receive the pointer down.
8839 const MotionEvent secondFingerDownEvent =
8840 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8841 .displayId(ADISPLAY_ID_DEFAULT)
8842 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008843 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8844 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008845 .build();
8846 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8847 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8848 InputEventInjectionSync::WAIT_FOR_RESULT))
8849 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8850
Harry Cutts33476232023-01-30 19:57:29 +00008851 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008852
8853 // Third finger goes down outside all windows, so injection should fail.
8854 const MotionEvent thirdFingerDownEvent =
8855 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8856 .displayId(ADISPLAY_ID_DEFAULT)
8857 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008858 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8859 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
8860 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008861 .build();
8862 ASSERT_EQ(InputEventInjectionResult::FAILED,
8863 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8864 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008865 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008866
8867 spy->assertNoEvents();
8868 window->assertNoEvents();
8869}
8870
8871/**
8872 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
8873 */
8874TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
8875 auto spy = createSpy();
8876 spy->setFrame(Rect(0, 0, 100, 100));
8877 auto window = createForeground();
8878 window->setFrame(Rect(0, 0, 200, 200));
8879
8880 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8881
8882 // First finger down on the window only
8883 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8884 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8885 {150, 150}))
8886 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8887 window->consumeMotionDown();
8888
8889 // Second finger down on the spy and window
8890 const MotionEvent secondFingerDownEvent =
8891 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8892 .displayId(ADISPLAY_ID_DEFAULT)
8893 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008894 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
8895 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008896 .build();
8897 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8898 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8899 InputEventInjectionSync::WAIT_FOR_RESULT))
8900 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8901 spy->consumeMotionDown();
8902 window->consumeMotionPointerDown(1);
8903
8904 // Third finger down on the spy and window
8905 const MotionEvent thirdFingerDownEvent =
8906 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8907 .displayId(ADISPLAY_ID_DEFAULT)
8908 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008909 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
8910 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
8911 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008912 .build();
8913 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8914 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8915 InputEventInjectionSync::WAIT_FOR_RESULT))
8916 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8917 spy->consumeMotionPointerDown(1);
8918 window->consumeMotionPointerDown(2);
8919
8920 // Spy window pilfers the pointers.
8921 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8922 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8923 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
8924
8925 spy->assertNoEvents();
8926 window->assertNoEvents();
8927}
8928
8929/**
8930 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
8931 * other windows should be canceled. If this results in the cancellation of all pointers for some
8932 * window, then that window should receive ACTION_CANCEL.
8933 */
8934TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
8935 auto spy = createSpy();
8936 spy->setFrame(Rect(0, 0, 100, 100));
8937 auto window = createForeground();
8938 window->setFrame(Rect(0, 0, 200, 200));
8939
8940 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8941
8942 // First finger down on both spy and window
8943 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8944 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8945 {10, 10}))
8946 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8947 window->consumeMotionDown();
8948 spy->consumeMotionDown();
8949
8950 // Second finger down on the spy and window
8951 const MotionEvent secondFingerDownEvent =
8952 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8953 .displayId(ADISPLAY_ID_DEFAULT)
8954 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008955 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
8956 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008957 .build();
8958 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8959 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8960 InputEventInjectionSync::WAIT_FOR_RESULT))
8961 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8962 spy->consumeMotionPointerDown(1);
8963 window->consumeMotionPointerDown(1);
8964
8965 // Spy window pilfers the pointers.
8966 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8967 window->consumeMotionCancel();
8968
8969 spy->assertNoEvents();
8970 window->assertNoEvents();
8971}
8972
8973/**
8974 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
8975 * be sent to other windows
8976 */
8977TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
8978 auto spy = createSpy();
8979 spy->setFrame(Rect(0, 0, 100, 100));
8980 auto window = createForeground();
8981 window->setFrame(Rect(0, 0, 200, 200));
8982
8983 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8984
8985 // First finger down on both window and spy
8986 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8987 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8988 {10, 10}))
8989 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8990 window->consumeMotionDown();
8991 spy->consumeMotionDown();
8992
8993 // Spy window pilfers the pointers.
8994 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8995 window->consumeMotionCancel();
8996
8997 // Second finger down on the window only
8998 const MotionEvent secondFingerDownEvent =
8999 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9000 .displayId(ADISPLAY_ID_DEFAULT)
9001 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009002 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9003 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009004 .build();
9005 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9006 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9007 InputEventInjectionSync::WAIT_FOR_RESULT))
9008 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9009 window->consumeMotionDown();
9010 window->assertNoEvents();
9011
9012 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9013 spy->consumeMotionMove();
9014 spy->assertNoEvents();
9015}
9016
Prabir Pradhand65552b2021-10-07 11:23:50 -07009017class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9018public:
9019 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9020 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9021 std::make_shared<FakeApplicationHandle>();
9022 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009023 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9024 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009025 overlay->setFocusable(false);
9026 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009027 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009028 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009029 overlay->setTrustedOverlay(true);
9030
9031 std::shared_ptr<FakeApplicationHandle> application =
9032 std::make_shared<FakeApplicationHandle>();
9033 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009034 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9035 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009036 window->setFocusable(true);
9037 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009038
9039 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9040 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9041 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009042 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009043 return {std::move(overlay), std::move(window)};
9044 }
9045
9046 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009047 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009048 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009049 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009050 }
9051
9052 void sendStylusEvent(int32_t action) {
9053 NotifyMotionArgs motionArgs =
9054 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9055 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009056 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009057 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009058 }
9059};
9060
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009061using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9062
9063TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9064 ScopedSilentDeath _silentDeath;
9065
Prabir Pradhand65552b2021-10-07 11:23:50 -07009066 auto [overlay, window] = setupStylusOverlayScenario();
9067 overlay->setTrustedOverlay(false);
9068 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9069 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9070 ".* not a trusted overlay");
9071}
9072
9073TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9074 auto [overlay, window] = setupStylusOverlayScenario();
9075 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9076
9077 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9078 overlay->consumeMotionDown();
9079 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9080 overlay->consumeMotionUp();
9081
9082 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9083 window->consumeMotionDown();
9084 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9085 window->consumeMotionUp();
9086
9087 overlay->assertNoEvents();
9088 window->assertNoEvents();
9089}
9090
9091TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9092 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009093 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009094 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9095
9096 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9097 overlay->consumeMotionDown();
9098 window->consumeMotionDown();
9099 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9100 overlay->consumeMotionUp();
9101 window->consumeMotionUp();
9102
9103 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9104 window->consumeMotionDown();
9105 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9106 window->consumeMotionUp();
9107
9108 overlay->assertNoEvents();
9109 window->assertNoEvents();
9110}
9111
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009112/**
9113 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9114 * The scenario is as follows:
9115 * - The stylus interceptor overlay is configured as a spy window.
9116 * - The stylus interceptor spy receives the start of a new stylus gesture.
9117 * - It pilfers pointers and then configures itself to no longer be a spy.
9118 * - The stylus interceptor continues to receive the rest of the gesture.
9119 */
9120TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9121 auto [overlay, window] = setupStylusOverlayScenario();
9122 overlay->setSpy(true);
9123 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9124
9125 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9126 overlay->consumeMotionDown();
9127 window->consumeMotionDown();
9128
9129 // The interceptor pilfers the pointers.
9130 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9131 window->consumeMotionCancel();
9132
9133 // The interceptor configures itself so that it is no longer a spy.
9134 overlay->setSpy(false);
9135 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9136
9137 // It continues to receive the rest of the stylus gesture.
9138 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9139 overlay->consumeMotionMove();
9140 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9141 overlay->consumeMotionUp();
9142
9143 window->assertNoEvents();
9144}
9145
Prabir Pradhan5735a322022-04-11 17:23:34 +00009146struct User {
9147 int32_t mPid;
9148 int32_t mUid;
9149 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9150 std::unique_ptr<InputDispatcher>& mDispatcher;
9151
9152 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9153 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9154
9155 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9156 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9157 ADISPLAY_ID_DEFAULT, {100, 200},
9158 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9159 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9160 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9161 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9162 }
9163
9164 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009165 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009166 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009167 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009168 mPolicyFlags);
9169 }
9170
9171 sp<FakeWindowHandle> createWindow() const {
9172 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9173 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009174 sp<FakeWindowHandle> window =
9175 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9176 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009177 window->setOwnerInfo(mPid, mUid);
9178 return window;
9179 }
9180};
9181
9182using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9183
9184TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9185 auto owner = User(mDispatcher, 10, 11);
9186 auto window = owner.createWindow();
9187 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9188
9189 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9190 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9191 window->consumeMotionDown();
9192
9193 setFocusedWindow(window);
9194 window->consumeFocusEvent(true);
9195
9196 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9197 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9198 window->consumeKeyDown(ADISPLAY_ID_NONE);
9199}
9200
9201TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9202 auto owner = User(mDispatcher, 10, 11);
9203 auto window = owner.createWindow();
9204 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9205
9206 auto rando = User(mDispatcher, 20, 21);
9207 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9208 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9209
9210 setFocusedWindow(window);
9211 window->consumeFocusEvent(true);
9212
9213 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9214 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9215 window->assertNoEvents();
9216}
9217
9218TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9219 auto owner = User(mDispatcher, 10, 11);
9220 auto window = owner.createWindow();
9221 auto spy = owner.createWindow();
9222 spy->setSpy(true);
9223 spy->setTrustedOverlay(true);
9224 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9225
9226 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9227 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9228 spy->consumeMotionDown();
9229 window->consumeMotionDown();
9230}
9231
9232TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9233 auto owner = User(mDispatcher, 10, 11);
9234 auto window = owner.createWindow();
9235
9236 auto rando = User(mDispatcher, 20, 21);
9237 auto randosSpy = rando.createWindow();
9238 randosSpy->setSpy(true);
9239 randosSpy->setTrustedOverlay(true);
9240 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9241
9242 // The event is targeted at owner's window, so injection should succeed, but the spy should
9243 // not receive the event.
9244 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9245 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9246 randosSpy->assertNoEvents();
9247 window->consumeMotionDown();
9248}
9249
9250TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9251 auto owner = User(mDispatcher, 10, 11);
9252 auto window = owner.createWindow();
9253
9254 auto rando = User(mDispatcher, 20, 21);
9255 auto randosSpy = rando.createWindow();
9256 randosSpy->setSpy(true);
9257 randosSpy->setTrustedOverlay(true);
9258 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9259
9260 // A user that has injection permission can inject into any window.
9261 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9262 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9263 ADISPLAY_ID_DEFAULT));
9264 randosSpy->consumeMotionDown();
9265 window->consumeMotionDown();
9266
9267 setFocusedWindow(randosSpy);
9268 randosSpy->consumeFocusEvent(true);
9269
9270 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9271 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9272 window->assertNoEvents();
9273}
9274
9275TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9276 auto owner = User(mDispatcher, 10, 11);
9277 auto window = owner.createWindow();
9278
9279 auto rando = User(mDispatcher, 20, 21);
9280 auto randosWindow = rando.createWindow();
9281 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9282 randosWindow->setWatchOutsideTouch(true);
9283 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9284
9285 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9286 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9287 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9288 window->consumeMotionDown();
9289 randosWindow->consumeMotionOutside();
9290}
9291
Garfield Tane84e6f92019-08-29 17:28:41 -07009292} // namespace android::inputdispatcher