blob: a58ad84e90edf5cbcc1a5499ec5fb9f99d92d2f5 [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) {
216 ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY);
217 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) {
227 ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION);
228 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()) {
533 case AINPUT_EVENT_TYPE_KEY: {
534 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
539 case AINPUT_EVENT_TYPE_MOTION: {
540 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 }
544 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 return true;
546 }
547
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800548 void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override {
549 if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) {
550 // Clear intercept state when we handled the event.
551 mInterceptKeyTimeout = 0ms;
552 }
553 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600555 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600557 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800558 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
559 // Clear intercept state so we could dispatch the event in next wake.
560 mInterceptKeyTimeout = 0ms;
561 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 }
563
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600564 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565 return false;
566 }
567
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600568 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
569 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700570 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800571 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
572 * essentially a passthrough for notifySwitch.
573 */
Harry Cutts33476232023-01-30 19:57:29 +0000574 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800575 }
576
Sean Stoutb4e0a592021-02-23 07:34:53 -0800577 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600579 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700580 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700581 mOnPointerDownToken = newToken;
582 }
583
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000584 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800585 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000586 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800587 mPointerCaptureChangedCondition.notify_all();
588 }
589
arthurhungf452d0b2021-01-06 00:19:52 +0800590 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
591 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800592 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800593 mDropTargetWindowToken = token;
594 }
595
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700596 void assertFilterInputEventWasCalledInternal(
597 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700598 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800599 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700600 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800601 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800602 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800603};
604
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605// --- InputDispatcherTest ---
606
607class InputDispatcherTest : public testing::Test {
608protected:
609 sp<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700610 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000612 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700613 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800614 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800615 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000616 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700617 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 }
619
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000620 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700621 ASSERT_EQ(OK, mDispatcher->stop());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 mFakePolicy.clear();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700623 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800624 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700625
626 /**
627 * Used for debugging when writing the test
628 */
629 void dumpDispatcherState() {
630 std::string dump;
631 mDispatcher->dump(dump);
632 std::stringstream ss(dump);
633 std::string to;
634
635 while (std::getline(ss, to, '\n')) {
636 ALOGE("%s", to.c_str());
637 }
638 }
Vishnu Nair958da932020-08-21 17:12:37 -0700639
chaviw3277faf2021-05-19 16:45:23 -0500640 void setFocusedWindow(const sp<WindowInfoHandle>& window,
641 const sp<WindowInfoHandle>& focusedWindow = nullptr) {
Vishnu Nair958da932020-08-21 17:12:37 -0700642 FocusRequest request;
643 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000644 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700645 if (focusedWindow) {
646 request.focusedToken = focusedWindow->getToken();
647 }
648 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;
Harry Cutts33476232023-01-30 19:57:29 +0000821 NotifyConfigurationChangedArgs args(/*id=*/10, eventTime);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800822 mDispatcher->notifyConfigurationChanged(&args);
823 ASSERT_TRUE(mDispatcher->waitForIdle());
824
825 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
826}
827
828TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000829 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
830 /*switchMask=*/2);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800831 mDispatcher->notifySwitch(&args);
832
833 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
834 args.policyFlags |= POLICY_FLAG_TRUSTED;
835 mFakePolicy->assertNotifySwitchWasCalled(args);
836}
837
Arthur Hungb92218b2018-08-14 12:00:21 +0800838// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700839static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700840// Default input dispatching timeout if there is no focused application or paused window
841// from which to determine an appropriate dispatching timeout.
842static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
843 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
844 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800845
846class FakeApplicationHandle : public InputApplicationHandle {
847public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700848 FakeApplicationHandle() {
849 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700850 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500851 mInfo.dispatchingTimeoutMillis =
852 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700853 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800854 virtual ~FakeApplicationHandle() {}
855
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000856 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700857
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500858 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
859 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700860 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800861};
862
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800863class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800864public:
Garfield Tan15601662020-09-22 15:32:38 -0700865 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800866 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700867 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800868 }
869
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800870 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700871 InputEvent* event;
872 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
873 if (!consumeSeq) {
874 return nullptr;
875 }
876 finishEvent(*consumeSeq);
877 return event;
878 }
879
880 /**
881 * Receive an event without acknowledging it.
882 * Return the sequence number that could later be used to send finished signal.
883 */
884 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800885 uint32_t consumeSeq;
886 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800887
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800888 std::chrono::time_point start = std::chrono::steady_clock::now();
889 status_t status = WOULD_BLOCK;
890 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000891 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800892 &event);
893 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
894 if (elapsed > 100ms) {
895 break;
896 }
897 }
898
899 if (status == WOULD_BLOCK) {
900 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700901 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800902 }
903
904 if (status != OK) {
905 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700906 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800907 }
908 if (event == nullptr) {
909 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700910 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800911 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700912 if (outEvent != nullptr) {
913 *outEvent = event;
914 }
915 return consumeSeq;
916 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800917
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700918 /**
919 * To be used together with "receiveEvent" to complete the consumption of an event.
920 */
921 void finishEvent(uint32_t consumeSeq) {
922 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
923 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800924 }
925
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000926 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
927 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
928 ASSERT_EQ(OK, status);
929 }
930
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000931 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
932 std::optional<int32_t> expectedDisplayId,
933 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800934 InputEvent* event = consume();
935
936 ASSERT_NE(nullptr, event) << mName.c_str()
937 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800938 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700939 << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType)
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800940 << " event, got " << inputEventTypeToString(event->getType()) << " event";
Arthur Hungb92218b2018-08-14 12:00:21 +0800941
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000942 if (expectedDisplayId.has_value()) {
943 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
944 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800945
Tiger Huang8664f8c2018-10-11 19:14:35 +0800946 switch (expectedEventType) {
947 case AINPUT_EVENT_TYPE_KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800948 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
949 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000950 if (expectedFlags.has_value()) {
951 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
952 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800953 break;
954 }
955 case AINPUT_EVENT_TYPE_MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800956 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000957 assertMotionAction(expectedAction, motionEvent.getAction());
958
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000959 if (expectedFlags.has_value()) {
960 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
961 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800962 break;
963 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100964 case AINPUT_EVENT_TYPE_FOCUS: {
965 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
966 }
Prabir Pradhan99987712020-11-10 18:43:05 -0800967 case AINPUT_EVENT_TYPE_CAPTURE: {
968 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
969 }
Antonio Kantekf16f2832021-09-28 04:39:20 +0000970 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
971 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
972 }
arthurhungb89ccb02020-12-30 16:19:01 +0800973 case AINPUT_EVENT_TYPE_DRAG: {
974 FAIL() << "Use 'consumeDragEvent' for DRAG events";
975 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800976 default: {
977 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
978 }
979 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800980 }
981
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800982 MotionEvent* consumeMotion() {
983 InputEvent* event = consume();
984
985 if (event == nullptr) {
986 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
987 return nullptr;
988 }
989
990 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
991 ADD_FAILURE() << mName << " expected a MotionEvent, got "
992 << inputEventTypeToString(event->getType()) << " event";
993 return nullptr;
994 }
995 return static_cast<MotionEvent*>(event);
996 }
997
998 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
999 MotionEvent* motionEvent = consumeMotion();
1000 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1001 ASSERT_THAT(*motionEvent, matcher);
1002 }
1003
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001004 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
1005 InputEvent* event = consume();
1006 ASSERT_NE(nullptr, event) << mName.c_str()
1007 << ": consumer should have returned non-NULL event.";
1008 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
1009 << "Got " << inputEventTypeToString(event->getType())
1010 << " event instead of FOCUS event";
1011
1012 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1013 << mName.c_str() << ": event displayId should always be NONE.";
1014
1015 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1016 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001017 }
1018
Prabir Pradhan99987712020-11-10 18:43:05 -08001019 void consumeCaptureEvent(bool hasCapture) {
1020 const InputEvent* event = consume();
1021 ASSERT_NE(nullptr, event) << mName.c_str()
1022 << ": consumer should have returned non-NULL event.";
1023 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
1024 << "Got " << inputEventTypeToString(event->getType())
1025 << " event instead of CAPTURE event";
1026
1027 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1028 << mName.c_str() << ": event displayId should always be NONE.";
1029
1030 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1031 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1032 }
1033
arthurhungb89ccb02020-12-30 16:19:01 +08001034 void consumeDragEvent(bool isExiting, float x, float y) {
1035 const InputEvent* event = consume();
1036 ASSERT_NE(nullptr, event) << mName.c_str()
1037 << ": consumer should have returned non-NULL event.";
1038 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
1039 << "Got " << inputEventTypeToString(event->getType())
1040 << " event instead of DRAG event";
1041
1042 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1043 << mName.c_str() << ": event displayId should always be NONE.";
1044
1045 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1046 EXPECT_EQ(isExiting, dragEvent.isExiting());
1047 EXPECT_EQ(x, dragEvent.getX());
1048 EXPECT_EQ(y, dragEvent.getY());
1049 }
1050
Antonio Kantekf16f2832021-09-28 04:39:20 +00001051 void consumeTouchModeEvent(bool inTouchMode) {
1052 const InputEvent* event = consume();
1053 ASSERT_NE(nullptr, event) << mName.c_str()
1054 << ": consumer should have returned non-NULL event.";
1055 ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType())
1056 << "Got " << inputEventTypeToString(event->getType())
1057 << " event instead of TOUCH_MODE event";
1058
1059 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1060 << mName.c_str() << ": event displayId should always be NONE.";
1061 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1062 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1063 }
1064
chaviwd1c23182019-12-20 18:44:56 -08001065 void assertNoEvents() {
1066 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001067 if (event == nullptr) {
1068 return;
1069 }
1070 if (event->getType() == AINPUT_EVENT_TYPE_KEY) {
1071 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1072 ADD_FAILURE() << "Received key event "
1073 << KeyEvent::actionToString(keyEvent.getAction());
1074 } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) {
1075 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1076 ADD_FAILURE() << "Received motion event "
1077 << MotionEvent::actionToString(motionEvent.getAction());
1078 } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) {
1079 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1080 ADD_FAILURE() << "Received focus event, hasFocus = "
1081 << (focusEvent.getHasFocus() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -08001082 } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) {
1083 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1084 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1085 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Antonio Kantekf16f2832021-09-28 04:39:20 +00001086 } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) {
1087 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1088 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1089 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001090 }
1091 FAIL() << mName.c_str()
1092 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001093 }
1094
1095 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1096
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001097 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1098
chaviwd1c23182019-12-20 18:44:56 -08001099protected:
1100 std::unique_ptr<InputConsumer> mConsumer;
1101 PreallocatedInputEventFactory mEventFactory;
1102
1103 std::string mName;
1104};
1105
chaviw3277faf2021-05-19 16:45:23 -05001106class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001107public:
1108 static const int32_t WIDTH = 600;
1109 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001110
Chris Yea209fde2020-07-22 13:54:51 -07001111 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001112 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001113 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001114 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001115 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001116 base::Result<std::unique_ptr<InputChannel>> channel =
1117 dispatcher->createInputChannel(name);
1118 token = (*channel)->getConnectionToken();
1119 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001120 }
1121
1122 inputApplicationHandle->updateInfo();
1123 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1124
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001125 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001126 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001127 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001128 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001129 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001130 mInfo.frameLeft = 0;
1131 mInfo.frameTop = 0;
1132 mInfo.frameRight = WIDTH;
1133 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001134 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001135 mInfo.globalScaleFactor = 1.0;
1136 mInfo.touchableRegion.clear();
1137 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001138 mInfo.ownerPid = WINDOW_PID;
1139 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001140 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001141 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001142 }
1143
Arthur Hungabbb9d82021-09-01 14:52:30 +00001144 sp<FakeWindowHandle> clone(
1145 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001146 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001147 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001148 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1149 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001150 return handle;
1151 }
1152
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001153 void setTouchable(bool touchable) {
1154 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1155 }
chaviwd1c23182019-12-20 18:44:56 -08001156
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001157 void setFocusable(bool focusable) {
1158 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1159 }
1160
1161 void setVisible(bool visible) {
1162 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1163 }
Vishnu Nair958da932020-08-21 17:12:37 -07001164
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001165 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001166 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001167 }
1168
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001169 void setPaused(bool paused) {
1170 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1171 }
1172
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001173 void setPreventSplitting(bool preventSplitting) {
1174 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001175 }
1176
1177 void setSlippery(bool slippery) {
1178 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1179 }
1180
1181 void setWatchOutsideTouch(bool watchOutside) {
1182 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1183 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001184
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001185 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1186
1187 void setInterceptsStylus(bool interceptsStylus) {
1188 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1189 }
1190
1191 void setDropInput(bool dropInput) {
1192 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1193 }
1194
1195 void setDropInputIfObscured(bool dropInputIfObscured) {
1196 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1197 }
1198
1199 void setNoInputChannel(bool noInputChannel) {
1200 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1201 }
1202
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001203 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1204
chaviw3277faf2021-05-19 16:45:23 -05001205 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001206
Bernardo Rufino7393d172021-02-26 13:56:11 +00001207 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1208
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001209 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001210 mInfo.frameLeft = frame.left;
1211 mInfo.frameTop = frame.top;
1212 mInfo.frameRight = frame.right;
1213 mInfo.frameBottom = frame.bottom;
1214 mInfo.touchableRegion.clear();
1215 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001216
1217 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1218 ui::Transform translate;
1219 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1220 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001221 }
1222
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001223 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1224
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001225 void setIsWallpaper(bool isWallpaper) {
1226 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1227 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001228
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001229 void setDupTouchToWallpaper(bool hasWallpaper) {
1230 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1231 }
chaviwd1c23182019-12-20 18:44:56 -08001232
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001233 void setTrustedOverlay(bool trustedOverlay) {
1234 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1235 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001236
chaviw9eaa22c2020-07-01 16:21:27 -07001237 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1238 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1239 }
1240
1241 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001242
yunho.shinf4a80b82020-11-16 21:13:57 +09001243 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1244
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001245 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1246 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
1247 expectedFlags);
1248 }
1249
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001250 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
1251 consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
1252 }
1253
Svet Ganov5d3bc372020-01-26 23:11:07 -08001254 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001255 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001256 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1257 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001258 }
1259
1260 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001261 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001262 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1263 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001264 }
1265
1266 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001267 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001268 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1269 }
1270
1271 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1272 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001273 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
1274 expectedFlags);
1275 }
1276
Svet Ganov5d3bc372020-01-26 23:11:07 -08001277 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001278 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1279 int32_t expectedFlags = 0) {
1280 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1281 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001282 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1283 }
1284
1285 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001286 int32_t expectedFlags = 0) {
1287 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1288 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001289 consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags);
1290 }
1291
1292 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001293 int32_t expectedFlags = 0) {
Michael Wright3a240c42019-12-10 20:53:41 +00001294 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
1295 expectedFlags);
1296 }
1297
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001298 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1299 int32_t expectedFlags = 0) {
1300 consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
1301 expectedFlags);
1302 }
1303
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001304 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1305 int32_t expectedFlags = 0) {
1306 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001307 ASSERT_NE(nullptr, event);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001308 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
1309 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1310 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1311 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1312 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1313 }
1314
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001315 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1316 ASSERT_NE(mInputReceiver, nullptr)
1317 << "Cannot consume events from a window with no receiver";
1318 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1319 }
1320
Prabir Pradhan99987712020-11-10 18:43:05 -08001321 void consumeCaptureEvent(bool hasCapture) {
1322 ASSERT_NE(mInputReceiver, nullptr)
1323 << "Cannot consume events from a window with no receiver";
1324 mInputReceiver->consumeCaptureEvent(hasCapture);
1325 }
1326
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001327 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1328 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001329 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001330 ASSERT_THAT(*motionEvent, matcher);
1331 }
1332
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001333 void consumeEvent(int32_t expectedEventType, int32_t expectedAction,
1334 std::optional<int32_t> expectedDisplayId,
1335 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001336 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1337 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1338 expectedFlags);
1339 }
1340
arthurhungb89ccb02020-12-30 16:19:01 +08001341 void consumeDragEvent(bool isExiting, float x, float y) {
1342 mInputReceiver->consumeDragEvent(isExiting, x, y);
1343 }
1344
Antonio Kantekf16f2832021-09-28 04:39:20 +00001345 void consumeTouchModeEvent(bool inTouchMode) {
1346 ASSERT_NE(mInputReceiver, nullptr)
1347 << "Cannot consume events from a window with no receiver";
1348 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1349 }
1350
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001351 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001352 if (mInputReceiver == nullptr) {
1353 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1354 return std::nullopt;
1355 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001356 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001357 }
1358
1359 void finishEvent(uint32_t sequenceNum) {
1360 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1361 mInputReceiver->finishEvent(sequenceNum);
1362 }
1363
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001364 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1365 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1366 mInputReceiver->sendTimeline(inputEventId, timeline);
1367 }
1368
chaviwaf87b3e2019-10-01 16:59:28 -07001369 InputEvent* consume() {
1370 if (mInputReceiver == nullptr) {
1371 return nullptr;
1372 }
1373 return mInputReceiver->consume();
1374 }
1375
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001376 MotionEvent* consumeMotion() {
1377 InputEvent* event = consume();
1378 if (event == nullptr) {
1379 ADD_FAILURE() << "Consume failed : no event";
1380 return nullptr;
1381 }
1382 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
1383 ADD_FAILURE() << "Instead of motion event, got "
1384 << inputEventTypeToString(event->getType());
1385 return nullptr;
1386 }
1387 return static_cast<MotionEvent*>(event);
1388 }
1389
Arthur Hungb92218b2018-08-14 12:00:21 +08001390 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001391 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001392 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001393 return; // Can't receive events if the window does not have input channel
1394 }
1395 ASSERT_NE(nullptr, mInputReceiver)
1396 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001397 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001398 }
1399
chaviwaf87b3e2019-10-01 16:59:28 -07001400 sp<IBinder> getToken() { return mInfo.token; }
1401
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001402 const std::string& getName() { return mName; }
1403
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001404 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1405 mInfo.ownerPid = ownerPid;
1406 mInfo.ownerUid = ownerUid;
1407 }
1408
Prabir Pradhanedd96402022-02-15 01:46:16 -08001409 int32_t getPid() const { return mInfo.ownerPid; }
1410
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001411 void destroyReceiver() { mInputReceiver = nullptr; }
1412
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001413 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1414
chaviwd1c23182019-12-20 18:44:56 -08001415private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001416 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001417 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001418 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001419};
1420
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001421std::atomic<int32_t> FakeWindowHandle::sId{1};
1422
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001423static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001424 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001425 int32_t displayId = ADISPLAY_ID_NONE,
1426 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001427 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001428 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1429 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001430 KeyEvent event;
1431 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1432
1433 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001434 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001435 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1436 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001437
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001438 if (!allowKeyRepeat) {
1439 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1440 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001441 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001442 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001443}
1444
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001445static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001446 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001447 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001448}
1449
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001450// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1451// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1452// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001453static InputEventInjectionResult injectKeyDownNoRepeat(
1454 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001455 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001456 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001457 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001458}
1459
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001460static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001461 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001462 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001463}
1464
Garfield Tandf26e862020-07-01 20:18:19 -07001465class PointerBuilder {
1466public:
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001467 PointerBuilder(int32_t id, ToolType toolType) {
Garfield Tandf26e862020-07-01 20:18:19 -07001468 mProperties.clear();
1469 mProperties.id = id;
1470 mProperties.toolType = toolType;
1471 mCoords.clear();
1472 }
1473
1474 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1475
1476 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1477
1478 PointerBuilder& axis(int32_t axis, float value) {
1479 mCoords.setAxisValue(axis, value);
1480 return *this;
1481 }
1482
1483 PointerProperties buildProperties() const { return mProperties; }
1484
1485 PointerCoords buildCoords() const { return mCoords; }
1486
1487private:
1488 PointerProperties mProperties;
1489 PointerCoords mCoords;
1490};
1491
1492class MotionEventBuilder {
1493public:
1494 MotionEventBuilder(int32_t action, int32_t source) {
1495 mAction = action;
1496 mSource = source;
1497 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001498 mDownTime = mEventTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001499 }
1500
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001501 MotionEventBuilder& deviceId(int32_t deviceId) {
1502 mDeviceId = deviceId;
1503 return *this;
1504 }
1505
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001506 MotionEventBuilder& downTime(nsecs_t downTime) {
1507 mDownTime = downTime;
1508 return *this;
1509 }
1510
Garfield Tandf26e862020-07-01 20:18:19 -07001511 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1512 mEventTime = eventTime;
1513 return *this;
1514 }
1515
1516 MotionEventBuilder& displayId(int32_t displayId) {
1517 mDisplayId = displayId;
1518 return *this;
1519 }
1520
1521 MotionEventBuilder& actionButton(int32_t actionButton) {
1522 mActionButton = actionButton;
1523 return *this;
1524 }
1525
arthurhung6d4bed92021-03-17 11:59:33 +08001526 MotionEventBuilder& buttonState(int32_t buttonState) {
1527 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001528 return *this;
1529 }
1530
1531 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1532 mRawXCursorPosition = rawXCursorPosition;
1533 return *this;
1534 }
1535
1536 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1537 mRawYCursorPosition = rawYCursorPosition;
1538 return *this;
1539 }
1540
1541 MotionEventBuilder& pointer(PointerBuilder pointer) {
1542 mPointers.push_back(pointer);
1543 return *this;
1544 }
1545
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001546 MotionEventBuilder& addFlag(uint32_t flags) {
1547 mFlags |= flags;
1548 return *this;
1549 }
1550
Garfield Tandf26e862020-07-01 20:18:19 -07001551 MotionEvent build() {
1552 std::vector<PointerProperties> pointerProperties;
1553 std::vector<PointerCoords> pointerCoords;
1554 for (const PointerBuilder& pointer : mPointers) {
1555 pointerProperties.push_back(pointer.buildProperties());
1556 pointerCoords.push_back(pointer.buildCoords());
1557 }
1558
1559 // Set mouse cursor position for the most common cases to avoid boilerplate.
1560 if (mSource == AINPUT_SOURCE_MOUSE &&
1561 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1562 mPointers.size() == 1) {
1563 mRawXCursorPosition = pointerCoords[0].getX();
1564 mRawYCursorPosition = pointerCoords[0].getY();
1565 }
1566
1567 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001568 ui::Transform identityTransform;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001569 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001570 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001571 mButtonState, MotionClassification::NONE, identityTransform,
1572 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001573 mRawYCursorPosition, identityTransform, mDownTime, mEventTime,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001574 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001575
1576 return event;
1577 }
1578
1579private:
1580 int32_t mAction;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001581 int32_t mDeviceId = DEVICE_ID;
Garfield Tandf26e862020-07-01 20:18:19 -07001582 int32_t mSource;
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001583 nsecs_t mDownTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001584 nsecs_t mEventTime;
1585 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1586 int32_t mActionButton{0};
1587 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001588 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001589 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1590 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1591
1592 std::vector<PointerBuilder> mPointers;
1593};
1594
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001595class MotionArgsBuilder {
1596public:
1597 MotionArgsBuilder(int32_t action, int32_t source) {
1598 mAction = action;
1599 mSource = source;
1600 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1601 mDownTime = mEventTime;
1602 }
1603
1604 MotionArgsBuilder& deviceId(int32_t deviceId) {
1605 mDeviceId = deviceId;
1606 return *this;
1607 }
1608
1609 MotionArgsBuilder& downTime(nsecs_t downTime) {
1610 mDownTime = downTime;
1611 return *this;
1612 }
1613
1614 MotionArgsBuilder& eventTime(nsecs_t eventTime) {
1615 mEventTime = eventTime;
1616 return *this;
1617 }
1618
1619 MotionArgsBuilder& displayId(int32_t displayId) {
1620 mDisplayId = displayId;
1621 return *this;
1622 }
1623
1624 MotionArgsBuilder& policyFlags(int32_t policyFlags) {
1625 mPolicyFlags = policyFlags;
1626 return *this;
1627 }
1628
1629 MotionArgsBuilder& actionButton(int32_t actionButton) {
1630 mActionButton = actionButton;
1631 return *this;
1632 }
1633
1634 MotionArgsBuilder& buttonState(int32_t buttonState) {
1635 mButtonState = buttonState;
1636 return *this;
1637 }
1638
1639 MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) {
1640 mRawXCursorPosition = rawXCursorPosition;
1641 return *this;
1642 }
1643
1644 MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) {
1645 mRawYCursorPosition = rawYCursorPosition;
1646 return *this;
1647 }
1648
1649 MotionArgsBuilder& pointer(PointerBuilder pointer) {
1650 mPointers.push_back(pointer);
1651 return *this;
1652 }
1653
1654 MotionArgsBuilder& addFlag(uint32_t flags) {
1655 mFlags |= flags;
1656 return *this;
1657 }
1658
1659 NotifyMotionArgs build() {
1660 std::vector<PointerProperties> pointerProperties;
1661 std::vector<PointerCoords> pointerCoords;
1662 for (const PointerBuilder& pointer : mPointers) {
1663 pointerProperties.push_back(pointer.buildProperties());
1664 pointerCoords.push_back(pointer.buildCoords());
1665 }
1666
1667 // Set mouse cursor position for the most common cases to avoid boilerplate.
1668 if (mSource == AINPUT_SOURCE_MOUSE &&
1669 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1670 mPointers.size() == 1) {
1671 mRawXCursorPosition = pointerCoords[0].getX();
1672 mRawYCursorPosition = pointerCoords[0].getY();
1673 }
1674
1675 NotifyMotionArgs args(InputEvent::nextId(), mEventTime, /*readTime=*/mEventTime, mDeviceId,
1676 mSource, mDisplayId, mPolicyFlags, mAction, mActionButton, mFlags,
1677 AMETA_NONE, mButtonState, MotionClassification::NONE, /*edgeFlags=*/0,
1678 mPointers.size(), pointerProperties.data(), pointerCoords.data(),
1679 /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition,
1680 mRawYCursorPosition, mDownTime, /*videoFrames=*/{});
1681
1682 return args;
1683 }
1684
1685private:
1686 int32_t mAction;
1687 int32_t mDeviceId = DEVICE_ID;
1688 int32_t mSource;
1689 nsecs_t mDownTime;
1690 nsecs_t mEventTime;
1691 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1692 int32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
1693 int32_t mActionButton{0};
1694 int32_t mButtonState{0};
1695 int32_t mFlags{0};
1696 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1697 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1698
1699 std::vector<PointerBuilder> mPointers;
1700};
1701
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001702static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001703 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001704 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001705 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1706 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1707 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1708 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001709}
1710
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001711static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001712 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001713 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001714 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001715 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1716 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001717 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001718 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1719 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Garfield Tandf26e862020-07-01 20:18:19 -07001720 MotionEvent event = MotionEventBuilder(action, source)
1721 .displayId(displayId)
1722 .eventTime(eventTime)
1723 .rawXCursorPosition(cursorPosition.x)
1724 .rawYCursorPosition(cursorPosition.y)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001725 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Garfield Tandf26e862020-07-01 20:18:19 -07001726 .x(position.x)
1727 .y(position.y))
1728 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001729
1730 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001731 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid,
1732 policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001733}
1734
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001735static InputEventInjectionResult injectMotionDown(
1736 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1737 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001738 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001739}
1740
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001741static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001742 int32_t source, int32_t displayId,
1743 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001744 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001745}
1746
Jackal Guof9696682018-10-05 12:23:23 +08001747static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1748 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1749 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001750 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001751 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1752 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001753
1754 return args;
1755}
1756
chaviwd1c23182019-12-20 18:44:56 -08001757static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId,
1758 const std::vector<PointF>& points) {
1759 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001760 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1761 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1762 }
1763
chaviwd1c23182019-12-20 18:44:56 -08001764 PointerProperties pointerProperties[pointerCount];
1765 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001766
chaviwd1c23182019-12-20 18:44:56 -08001767 for (size_t i = 0; i < pointerCount; i++) {
1768 pointerProperties[i].clear();
1769 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001770 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001771
chaviwd1c23182019-12-20 18:44:56 -08001772 pointerCoords[i].clear();
1773 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1774 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1775 }
Jackal Guof9696682018-10-05 12:23:23 +08001776
1777 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1778 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001779 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001780 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1781 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001782 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1783 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001784 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1785 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001786
1787 return args;
1788}
1789
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001790static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1791 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1792}
1793
chaviwd1c23182019-12-20 18:44:56 -08001794static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1795 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1796}
1797
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001798static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1799 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001800 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001801}
1802
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001803/**
1804 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1805 * broken channel.
1806 */
1807TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1808 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1809 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001810 sp<FakeWindowHandle>::make(application, mDispatcher,
1811 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001812
1813 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1814
1815 // Window closes its channel, but the window remains.
1816 window->destroyReceiver();
1817 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1818}
1819
Arthur Hungb92218b2018-08-14 12:00:21 +08001820TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001821 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001822 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1823 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001824
Arthur Hung72d8dc32020-03-28 00:48:39 +00001825 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001826 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1827 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1828 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001829
1830 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001831 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001832}
1833
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001834TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1835 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001836 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1837 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001838
1839 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1840 // Inject a MotionEvent to an unknown display.
1841 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1842 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1843 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1844
1845 // Window should receive motion event.
1846 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1847}
1848
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001849/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001850 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001851 * This test serves as a sanity check for the next test, where setInputWindows is
1852 * called twice.
1853 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001854TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001855 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001856 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1857 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001858 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001859
1860 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001861 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001862 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1863 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001864 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001865
1866 // Window should receive motion event.
1867 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1868}
1869
1870/**
1871 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001872 */
1873TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001874 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001875 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1876 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001877 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001878
1879 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1880 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001882 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1883 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001884 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001885
1886 // Window should receive motion event.
1887 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1888}
1889
Arthur Hungb92218b2018-08-14 12:00:21 +08001890// The foreground window should receive the first touch down event.
1891TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001892 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001893 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001894 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001895 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001896 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001897
Arthur Hung72d8dc32020-03-28 00:48:39 +00001898 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001899 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1900 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1901 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001902
1903 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001904 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001905 windowSecond->assertNoEvents();
1906}
1907
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001908/**
1909 * Two windows: A top window, and a wallpaper behind the window.
1910 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1911 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001912 * 1. foregroundWindow <-- dup touch to wallpaper
1913 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001914 */
1915TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1916 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1917 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001918 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001919 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001920 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001921 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001922 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001923
1924 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1925 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1926 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1927 {100, 200}))
1928 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1929
1930 // Both foreground window and its wallpaper should receive the touch down
1931 foregroundWindow->consumeMotionDown();
1932 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1933
1934 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1935 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1936 ADISPLAY_ID_DEFAULT, {110, 200}))
1937 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1938
1939 foregroundWindow->consumeMotionMove();
1940 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1941
1942 // Now the foreground window goes away, but the wallpaper stays
1943 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1944 foregroundWindow->consumeMotionCancel();
1945 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1946 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1947}
1948
1949/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001950 * Two fingers down on the window, and lift off the first finger.
1951 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1952 * contains a single pointer.
1953 */
1954TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1955 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1956 sp<FakeWindowHandle> window =
1957 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1958
1959 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1960 NotifyMotionArgs args;
1961 // First touch pointer down on right window
1962 mDispatcher->notifyMotion(&(
1963 args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001964 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001965 .build()));
1966 // Second touch pointer down
1967 mDispatcher->notifyMotion(&(
1968 args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1969
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001970 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1971 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001972 .build()));
1973 // First touch pointer lifts. The second one remains down
1974 mDispatcher->notifyMotion(&(
1975 args = MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1976
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001977 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1978 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001979 .build()));
1980 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1981 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1982 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1983
1984 // Remove the window. The gesture should be canceled
1985 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
1986 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1987 window->consumeMotionEvent(
1988 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1989}
1990
1991/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001992 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1993 * with the following differences:
1994 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1995 * clean up the connection.
1996 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1997 * Ensure that there's no crash in the dispatcher.
1998 */
1999TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
2000 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2001 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002002 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002003 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002004 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002005 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002006 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002007
2008 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
2009 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2010 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2011 {100, 200}))
2012 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2013
2014 // Both foreground window and its wallpaper should receive the touch down
2015 foregroundWindow->consumeMotionDown();
2016 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2017
2018 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2019 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2020 ADISPLAY_ID_DEFAULT, {110, 200}))
2021 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2022
2023 foregroundWindow->consumeMotionMove();
2024 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2025
2026 // Wallpaper closes its channel, but the window remains.
2027 wallpaperWindow->destroyReceiver();
2028 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
2029
2030 // Now the foreground window goes away, but the wallpaper stays, even though its channel
2031 // is no longer valid.
2032 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
2033 foregroundWindow->consumeMotionCancel();
2034}
2035
Arthur Hungc539dbb2022-12-08 07:45:36 +00002036class ShouldSplitTouchFixture : public InputDispatcherTest,
2037 public ::testing::WithParamInterface<bool> {};
2038INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
2039 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002040/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002041 * A single window that receives touch (on top), and a wallpaper window underneath it.
2042 * The top window gets a multitouch gesture.
2043 * Ensure that wallpaper gets the same gesture.
2044 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00002045TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002046 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002047 sp<FakeWindowHandle> foregroundWindow =
2048 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
2049 foregroundWindow->setDupTouchToWallpaper(true);
2050 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002051
2052 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002053 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002054 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002055
Arthur Hungc539dbb2022-12-08 07:45:36 +00002056 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002057
2058 // Touch down on top window
2059 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2060 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2061 {100, 100}))
2062 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2063
2064 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00002065 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002066 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2067
2068 // Second finger down on the top window
2069 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002070 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002071 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002072 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2073 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002074 .build();
2075 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2076 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2077 InputEventInjectionSync::WAIT_FOR_RESULT))
2078 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2079
Harry Cutts33476232023-01-30 19:57:29 +00002080 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
2081 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002082 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002083
2084 const MotionEvent secondFingerUpEvent =
2085 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2086 .displayId(ADISPLAY_ID_DEFAULT)
2087 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002088 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2089 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002090 .build();
2091 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2092 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
2093 InputEventInjectionSync::WAIT_FOR_RESULT))
2094 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2095 foregroundWindow->consumeMotionPointerUp(0);
2096 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2097
2098 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002099 injectMotionEvent(mDispatcher,
2100 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2101 AINPUT_SOURCE_TOUCHSCREEN)
2102 .displayId(ADISPLAY_ID_DEFAULT)
2103 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2104 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002105 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002106 .x(100)
2107 .y(100))
2108 .build(),
2109 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002110 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2111 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2112 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002113}
2114
2115/**
2116 * Two windows: a window on the left and window on the right.
2117 * A third window, wallpaper, is behind both windows, and spans both top windows.
2118 * The first touch down goes to the left window. A second pointer touches down on the right window.
2119 * The touch is split, so both left and right windows should receive ACTION_DOWN.
2120 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
2121 * ACTION_POINTER_DOWN(1).
2122 */
2123TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
2124 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2125 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002126 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002127 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002128 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002129
2130 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002131 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002132 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002133 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002134
2135 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002136 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002137 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002138 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002139
2140 mDispatcher->setInputWindows(
2141 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
2142
2143 // Touch down on left window
2144 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2145 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2146 {100, 100}))
2147 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2148
2149 // Both foreground window and its wallpaper should receive the touch down
2150 leftWindow->consumeMotionDown();
2151 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2152
2153 // Second finger down on the right window
2154 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002155 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002156 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002157 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2158 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002159 .build();
2160 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2161 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2162 InputEventInjectionSync::WAIT_FOR_RESULT))
2163 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2164
2165 leftWindow->consumeMotionMove();
2166 // Since the touch is split, right window gets ACTION_DOWN
2167 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00002168 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002169 expectedWallpaperFlags);
2170
2171 // Now, leftWindow, which received the first finger, disappears.
2172 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
2173 leftWindow->consumeMotionCancel();
2174 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2175 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2176
2177 // The pointer that's still down on the right window moves, and goes to the right window only.
2178 // As far as the dispatcher's concerned though, both pointers are still present.
2179 const MotionEvent secondFingerMoveEvent =
2180 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2181 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002182 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2183 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002184 .build();
2185 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2186 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
2187 InputEventInjectionSync::WAIT_FOR_RESULT));
2188 rightWindow->consumeMotionMove();
2189
2190 leftWindow->assertNoEvents();
2191 rightWindow->assertNoEvents();
2192 wallpaperWindow->assertNoEvents();
2193}
2194
Arthur Hungc539dbb2022-12-08 07:45:36 +00002195/**
2196 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2197 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2198 * The right window should receive ACTION_DOWN.
2199 */
2200TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002201 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002202 sp<FakeWindowHandle> leftWindow =
2203 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2204 leftWindow->setFrame(Rect(0, 0, 200, 200));
2205 leftWindow->setDupTouchToWallpaper(true);
2206 leftWindow->setSlippery(true);
2207
2208 sp<FakeWindowHandle> rightWindow =
2209 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2210 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002211
2212 sp<FakeWindowHandle> wallpaperWindow =
2213 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2214 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002215
Arthur Hungc539dbb2022-12-08 07:45:36 +00002216 mDispatcher->setInputWindows(
2217 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002218
Arthur Hungc539dbb2022-12-08 07:45:36 +00002219 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002220 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2221 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002222 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002223 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002224
2225 // Both foreground window and its wallpaper should receive the touch down
2226 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002227 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2228
Arthur Hungc539dbb2022-12-08 07:45:36 +00002229 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002230 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002231 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2232 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002233 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2234
Arthur Hungc539dbb2022-12-08 07:45:36 +00002235 leftWindow->consumeMotionCancel();
2236 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2237 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002238}
2239
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002240/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002241 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2242 * interactive, it might stop sending this flag.
2243 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2244 * to have a consistent input stream.
2245 *
2246 * Test procedure:
2247 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2248 * DOWN (new gesture).
2249 *
2250 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2251 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2252 *
2253 * We technically just need a single window here, but we are using two windows (spy on top and a
2254 * regular window below) to emulate the actual situation where it happens on the device.
2255 */
2256TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2257 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2258 sp<FakeWindowHandle> spyWindow =
2259 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2260 spyWindow->setFrame(Rect(0, 0, 200, 200));
2261 spyWindow->setTrustedOverlay(true);
2262 spyWindow->setSpy(true);
2263
2264 sp<FakeWindowHandle> window =
2265 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2266 window->setFrame(Rect(0, 0, 200, 200));
2267
2268 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2269 const int32_t touchDeviceId = 4;
2270 NotifyMotionArgs args;
2271
2272 // Two pointers down
2273 mDispatcher->notifyMotion(&(
2274 args = MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2275 .deviceId(touchDeviceId)
2276 .policyFlags(DEFAULT_POLICY_FLAGS)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002277 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002278 .build()));
2279
2280 mDispatcher->notifyMotion(&(
2281 args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2282 .deviceId(touchDeviceId)
2283 .policyFlags(DEFAULT_POLICY_FLAGS)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002284 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2285 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002286 .build()));
2287 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2288 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2289 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2290 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2291
2292 // Cancel the current gesture. Send the cancel without the default policy flags.
2293 mDispatcher->notifyMotion(&(
2294 args = MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2295 .deviceId(touchDeviceId)
2296 .policyFlags(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002297 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2298 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002299 .build()));
2300 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2301 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2302
2303 // We don't need to reset the device to reproduce the issue, but the reset event typically
2304 // follows, so we keep it here to model the actual listener behaviour more closely.
2305 NotifyDeviceResetArgs resetArgs;
2306 resetArgs.id = 1; // arbitrary id
2307 resetArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
2308 resetArgs.deviceId = touchDeviceId;
2309 mDispatcher->notifyDeviceReset(&resetArgs);
2310
2311 // Start new gesture
2312 mDispatcher->notifyMotion(&(
2313 args = MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2314 .deviceId(touchDeviceId)
2315 .policyFlags(DEFAULT_POLICY_FLAGS)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002316 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002317 .build()));
2318 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2319 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2320
2321 // No more events
2322 spyWindow->assertNoEvents();
2323 window->assertNoEvents();
2324}
2325
2326/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002327 * Two windows: a window on the left and a window on the right.
2328 * Mouse is hovered from the right window into the left window.
2329 * Next, we tap on the left window, where the cursor was last seen.
2330 * The second tap is done onto the right window.
2331 * The mouse and tap are from two different devices.
2332 * We technically don't need to set the downtime / eventtime for these events, but setting these
2333 * explicitly helps during debugging.
2334 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2335 * In the buggy implementation, a tap on the right window would cause a crash.
2336 */
2337TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2338 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2339 sp<FakeWindowHandle> leftWindow =
2340 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2341 leftWindow->setFrame(Rect(0, 0, 200, 200));
2342
2343 sp<FakeWindowHandle> rightWindow =
2344 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2345 rightWindow->setFrame(Rect(200, 0, 400, 200));
2346
2347 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2348 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2349 // stale.
2350 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2351 const int32_t mouseDeviceId = 6;
2352 const int32_t touchDeviceId = 4;
2353 // Move the cursor from right
2354 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2355 injectMotionEvent(mDispatcher,
2356 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2357 AINPUT_SOURCE_MOUSE)
2358 .deviceId(mouseDeviceId)
2359 .downTime(baseTime + 10)
2360 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002361 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002362 .x(300)
2363 .y(100))
2364 .build()));
2365 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2366
2367 // .. to the left window
2368 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2369 injectMotionEvent(mDispatcher,
2370 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2371 AINPUT_SOURCE_MOUSE)
2372 .deviceId(mouseDeviceId)
2373 .downTime(baseTime + 10)
2374 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002375 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002376 .x(110)
2377 .y(100))
2378 .build()));
2379 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2380 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2381 // Now tap the left window
2382 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2383 injectMotionEvent(mDispatcher,
2384 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2385 AINPUT_SOURCE_TOUCHSCREEN)
2386 .deviceId(touchDeviceId)
2387 .downTime(baseTime + 40)
2388 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002389 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002390 .x(100)
2391 .y(100))
2392 .build()));
2393 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2394 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2395
2396 // release tap
2397 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2398 injectMotionEvent(mDispatcher,
2399 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2400 AINPUT_SOURCE_TOUCHSCREEN)
2401 .deviceId(touchDeviceId)
2402 .downTime(baseTime + 40)
2403 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002404 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002405 .x(100)
2406 .y(100))
2407 .build()));
2408 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2409
2410 // Tap the window on the right
2411 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2412 injectMotionEvent(mDispatcher,
2413 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2414 AINPUT_SOURCE_TOUCHSCREEN)
2415 .deviceId(touchDeviceId)
2416 .downTime(baseTime + 60)
2417 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002418 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002419 .x(300)
2420 .y(100))
2421 .build()));
2422 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2423
2424 // release tap
2425 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2426 injectMotionEvent(mDispatcher,
2427 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2428 AINPUT_SOURCE_TOUCHSCREEN)
2429 .deviceId(touchDeviceId)
2430 .downTime(baseTime + 60)
2431 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002432 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002433 .x(300)
2434 .y(100))
2435 .build()));
2436 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2437
2438 // No more events
2439 leftWindow->assertNoEvents();
2440 rightWindow->assertNoEvents();
2441}
2442
2443/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002444 * Two windows: a window on the left and a window on the right.
2445 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2446 * down. Then, on the left window, also place second touch pointer down.
2447 * This test tries to reproduce a crash.
2448 * In the buggy implementation, second pointer down on the left window would cause a crash.
2449 */
2450TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2451 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2452 sp<FakeWindowHandle> leftWindow =
2453 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2454 leftWindow->setFrame(Rect(0, 0, 200, 200));
2455
2456 sp<FakeWindowHandle> rightWindow =
2457 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2458 rightWindow->setFrame(Rect(200, 0, 400, 200));
2459
2460 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2461
2462 const int32_t touchDeviceId = 4;
2463 const int32_t mouseDeviceId = 6;
2464 NotifyMotionArgs args;
2465
2466 // Start hovering over the left window
2467 mDispatcher->notifyMotion(&(
2468 args = MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2469 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002470 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002471 .build()));
2472 leftWindow->consumeMotionEvent(
2473 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2474
2475 // Mouse down on left window
2476 mDispatcher->notifyMotion(&(
2477 args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2478 .deviceId(mouseDeviceId)
2479 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002480 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002481 .build()));
2482
2483 leftWindow->consumeMotionEvent(
2484 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2485 leftWindow->consumeMotionEvent(
2486 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2487
2488 mDispatcher->notifyMotion(&(
2489 args = MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2490 .deviceId(mouseDeviceId)
2491 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2492 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002493 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002494 .build()));
2495 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2496
2497 // First touch pointer down on right window
2498 mDispatcher->notifyMotion(&(
2499 args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2500 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002501 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002502 .build()));
2503 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2504
2505 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2506
2507 // Second touch pointer down on left window
2508 mDispatcher->notifyMotion(&(
2509 args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2510 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002511 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2512 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002513 .build()));
2514 leftWindow->consumeMotionEvent(
2515 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2516 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2517 // current implementation.
2518 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2519 rightWindow->consumeMotionEvent(
2520 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2521
2522 leftWindow->assertNoEvents();
2523 rightWindow->assertNoEvents();
2524}
2525
2526/**
2527 * On a single window, use two different devices: mouse and touch.
2528 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2529 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2530 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2531 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2532 * represent a new gesture.
2533 */
2534TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2535 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2536 sp<FakeWindowHandle> window =
2537 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2538 window->setFrame(Rect(0, 0, 400, 400));
2539
2540 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2541
2542 const int32_t touchDeviceId = 4;
2543 const int32_t mouseDeviceId = 6;
2544 NotifyMotionArgs args;
2545
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002546 // First touch pointer down
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002547 mDispatcher->notifyMotion(&(
2548 args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2549 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002550 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002551 .build()));
2552 // Second touch pointer down
2553 mDispatcher->notifyMotion(&(
2554 args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2555 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002556 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2557 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002558 .build()));
2559 // First touch pointer lifts. The second one remains down
2560 mDispatcher->notifyMotion(&(
2561 args = MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2562 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002563 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2564 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002565 .build()));
2566 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2567 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2568 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2569
2570 // Mouse down. The touch should be canceled
2571 mDispatcher->notifyMotion(&(
2572 args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2573 .deviceId(mouseDeviceId)
2574 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002575 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002576 .build()));
2577
2578 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002579 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002580 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2581
2582 mDispatcher->notifyMotion(&(
2583 args = MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2584 .deviceId(mouseDeviceId)
2585 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2586 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002587 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002588 .build()));
2589 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2590
2591 // Second touch pointer down.
2592 mDispatcher->notifyMotion(&(
2593 args = MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2594 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002595 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2596 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002597 .build()));
2598 // The pointer_down event should be ignored
2599 window->assertNoEvents();
2600}
2601
2602/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002603 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2604 * the injected event.
2605 */
2606TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2607 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2608 sp<FakeWindowHandle> window =
2609 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2610 window->setFrame(Rect(0, 0, 400, 400));
2611
2612 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2613
2614 const int32_t touchDeviceId = 4;
2615 NotifyMotionArgs args;
2616 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2617 // completion.
2618 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2619 injectMotionEvent(mDispatcher,
2620 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2621 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002622 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002623 .x(50)
2624 .y(50))
2625 .build()));
2626 window->consumeMotionEvent(
2627 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2628
2629 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2630 // should be canceled and the new gesture should take over.
2631 mDispatcher->notifyMotion(&(
2632 args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2633 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002634 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002635 .build()));
2636
2637 window->consumeMotionEvent(
2638 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2639 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2640}
2641
2642/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002643 * This test is similar to the test above, but the sequence of injected events is different.
2644 *
2645 * Two windows: a window on the left and a window on the right.
2646 * Mouse is hovered over the left window.
2647 * Next, we tap on the left window, where the cursor was last seen.
2648 *
2649 * After that, we inject one finger down onto the right window, and then a second finger down onto
2650 * the left window.
2651 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2652 * window (first), and then another on the left window (second).
2653 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2654 * In the buggy implementation, second finger down on the left window would cause a crash.
2655 */
2656TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2657 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2658 sp<FakeWindowHandle> leftWindow =
2659 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2660 leftWindow->setFrame(Rect(0, 0, 200, 200));
2661
2662 sp<FakeWindowHandle> rightWindow =
2663 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2664 rightWindow->setFrame(Rect(200, 0, 400, 200));
2665
2666 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2667
2668 const int32_t mouseDeviceId = 6;
2669 const int32_t touchDeviceId = 4;
2670 // Hover over the left window. Keep the cursor there.
2671 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2672 injectMotionEvent(mDispatcher,
2673 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2674 AINPUT_SOURCE_MOUSE)
2675 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002676 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002677 .x(50)
2678 .y(50))
2679 .build()));
2680 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2681
2682 // Tap on left window
2683 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2684 injectMotionEvent(mDispatcher,
2685 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2686 AINPUT_SOURCE_TOUCHSCREEN)
2687 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002688 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002689 .x(100)
2690 .y(100))
2691 .build()));
2692
2693 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2694 injectMotionEvent(mDispatcher,
2695 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2696 AINPUT_SOURCE_TOUCHSCREEN)
2697 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002698 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002699 .x(100)
2700 .y(100))
2701 .build()));
2702 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2703 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2704 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2705
2706 // First finger down on right window
2707 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2708 injectMotionEvent(mDispatcher,
2709 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2710 AINPUT_SOURCE_TOUCHSCREEN)
2711 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002712 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002713 .x(300)
2714 .y(100))
2715 .build()));
2716 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2717
2718 // Second finger down on the left window
2719 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2720 injectMotionEvent(mDispatcher,
2721 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2722 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002723 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002724 .x(300)
2725 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002726 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002727 .x(100)
2728 .y(100))
2729 .build()));
2730 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2731 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2732
2733 // No more events
2734 leftWindow->assertNoEvents();
2735 rightWindow->assertNoEvents();
2736}
2737
2738/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002739 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2740 * While the touch is down, new hover events from the stylus device should be ignored. After the
2741 * touch is gone, stylus hovering should start working again.
2742 */
2743TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2744 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2745 sp<FakeWindowHandle> window =
2746 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2747 window->setFrame(Rect(0, 0, 200, 200));
2748
2749 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2750
2751 const int32_t stylusDeviceId = 5;
2752 const int32_t touchDeviceId = 4;
2753 // Start hovering with stylus
2754 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2755 injectMotionEvent(mDispatcher,
2756 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2757 AINPUT_SOURCE_STYLUS)
2758 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002759 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002760 .x(50)
2761 .y(50))
2762 .build()));
2763 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2764
2765 // Finger down on the window
2766 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2767 injectMotionEvent(mDispatcher,
2768 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2769 AINPUT_SOURCE_TOUCHSCREEN)
2770 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002771 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002772 .x(100)
2773 .y(100))
2774 .build()));
2775 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2776 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2777
2778 // Try to continue hovering with stylus. Since we are already down, injection should fail
2779 ASSERT_EQ(InputEventInjectionResult::FAILED,
2780 injectMotionEvent(mDispatcher,
2781 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2782 AINPUT_SOURCE_STYLUS)
2783 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002784 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002785 .x(50)
2786 .y(50))
2787 .build()));
2788 // No event should be sent. This event should be ignored because a pointer from another device
2789 // is already down.
2790
2791 // Lift up the finger
2792 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2793 injectMotionEvent(mDispatcher,
2794 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2795 AINPUT_SOURCE_TOUCHSCREEN)
2796 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002797 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002798 .x(100)
2799 .y(100))
2800 .build()));
2801 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2802
2803 // Now that the touch is gone, stylus hovering should start working again
2804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2805 injectMotionEvent(mDispatcher,
2806 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2807 AINPUT_SOURCE_STYLUS)
2808 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002809 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002810 .x(50)
2811 .y(50))
2812 .build()));
2813 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2814 // No more events
2815 window->assertNoEvents();
2816}
2817
2818/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002819 * A spy window above a window with no input channel.
2820 * Start hovering with a stylus device, and then tap with it.
2821 * Ensure spy window receives the entire sequence.
2822 */
2823TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2824 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2825 sp<FakeWindowHandle> spyWindow =
2826 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2827 spyWindow->setFrame(Rect(0, 0, 200, 200));
2828 spyWindow->setTrustedOverlay(true);
2829 spyWindow->setSpy(true);
2830 sp<FakeWindowHandle> window =
2831 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2832 window->setNoInputChannel(true);
2833 window->setFrame(Rect(0, 0, 200, 200));
2834
2835 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2836
2837 NotifyMotionArgs args;
2838
2839 // Start hovering with stylus
2840 mDispatcher->notifyMotion(
2841 &(args = MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002842 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002843 .build()));
2844 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2845 // Stop hovering
2846 mDispatcher->notifyMotion(
2847 &(args = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002848 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002849 .build()));
2850 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2851
2852 // Stylus touches down
2853 mDispatcher->notifyMotion(
2854 &(args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002855 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002856 .build()));
2857 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2858
2859 // Stylus goes up
2860 mDispatcher->notifyMotion(
2861 &(args = MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002862 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002863 .build()));
2864 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2865
2866 // Again hover
2867 mDispatcher->notifyMotion(
2868 &(args = MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002869 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002870 .build()));
2871 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2872 // Stop hovering
2873 mDispatcher->notifyMotion(
2874 &(args = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002875 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002876 .build()));
2877 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2878
2879 // No more events
2880 spyWindow->assertNoEvents();
2881 window->assertNoEvents();
2882}
2883
2884/**
2885 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
2886 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
2887 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
2888 * While the mouse is down, new move events from the touch device should be ignored.
2889 */
2890TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
2891 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2892 sp<FakeWindowHandle> spyWindow =
2893 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2894 spyWindow->setFrame(Rect(0, 0, 200, 200));
2895 spyWindow->setTrustedOverlay(true);
2896 spyWindow->setSpy(true);
2897 sp<FakeWindowHandle> window =
2898 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2899 window->setFrame(Rect(0, 0, 200, 200));
2900
2901 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2902
2903 const int32_t mouseDeviceId = 7;
2904 const int32_t touchDeviceId = 4;
2905 NotifyMotionArgs args;
2906
2907 // Hover a bit with mouse first
2908 mDispatcher->notifyMotion(&(
2909 args = MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2910 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002911 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002912 .build()));
2913 spyWindow->consumeMotionEvent(
2914 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2915 window->consumeMotionEvent(
2916 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2917
2918 // Start touching
2919 mDispatcher->notifyMotion(
2920 &(args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2921 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002922 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002923 .build()));
2924 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2925 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2926 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2927 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2928
2929 mDispatcher->notifyMotion(
2930 &(args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2931 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002932 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002933 .build()));
2934 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2935 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2936
2937 // Pilfer the stream
2938 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2939 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2940
2941 mDispatcher->notifyMotion(
2942 &(args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2943 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002944 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002945 .build()));
2946 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2947
2948 // Mouse down
2949 mDispatcher->notifyMotion(&(
2950 args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2951 .deviceId(mouseDeviceId)
2952 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002953 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002954 .build()));
2955
2956 spyWindow->consumeMotionEvent(
2957 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2958 spyWindow->consumeMotionEvent(
2959 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2960 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2961
2962 mDispatcher->notifyMotion(&(
2963 args = MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2964 .deviceId(mouseDeviceId)
2965 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2966 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002967 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002968 .build()));
2969 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2970 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2971
2972 // Mouse move!
2973 mDispatcher->notifyMotion(&(
2974 args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
2975 .deviceId(mouseDeviceId)
2976 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002977 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002978 .build()));
2979 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2980 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2981
2982 // Touch move!
2983 mDispatcher->notifyMotion(
2984 &(args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2985 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002986 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002987 .build()));
2988
2989 // No more events
2990 spyWindow->assertNoEvents();
2991 window->assertNoEvents();
2992}
2993
2994/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002995 * On the display, have a single window, and also an area where there's no window.
2996 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2997 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2998 */
2999TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
3000 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3001 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003002 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003003
3004 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
3005 NotifyMotionArgs args;
3006
3007 // Touch down on the empty space
3008 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})));
3009
3010 mDispatcher->waitForIdle();
3011 window->assertNoEvents();
3012
3013 // Now touch down on the window with another pointer
3014 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})));
3015 mDispatcher->waitForIdle();
3016 window->consumeMotionDown();
3017}
3018
3019/**
3020 * Same test as above, but instead of touching the empty space, the first touch goes to
3021 * non-touchable window.
3022 */
3023TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
3024 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3025 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003026 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003027 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3028 window1->setTouchable(false);
3029 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003030 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003031 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3032
3033 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
3034
3035 NotifyMotionArgs args;
3036 // Touch down on the non-touchable window
3037 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
3038
3039 mDispatcher->waitForIdle();
3040 window1->assertNoEvents();
3041 window2->assertNoEvents();
3042
3043 // Now touch down on the window with another pointer
3044 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
3045 mDispatcher->waitForIdle();
3046 window2->consumeMotionDown();
3047}
3048
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003049/**
3050 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
3051 * to the event time of the first ACTION_DOWN sent to the particular window.
3052 */
3053TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
3054 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3055 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003056 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003057 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3058 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003059 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003060 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3061
3062 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
3063
3064 NotifyMotionArgs args;
3065 // Touch down on the first window
3066 mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})));
3067
3068 mDispatcher->waitForIdle();
3069 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003070 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003071 window2->assertNoEvents();
3072 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
3073 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
3074 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
3075
3076 // Now touch down on the window with another pointer
3077 mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
3078 mDispatcher->waitForIdle();
3079 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003080 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003081 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
3082 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
3083 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
3084 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
3085
3086 // Now move the pointer on the second window
3087 mDispatcher->notifyMotion(
3088 &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})));
3089 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003090 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003091
3092 // Now add new touch down on the second window
3093 mDispatcher->notifyMotion(
3094 &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})));
3095 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003096 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003097
3098 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
3099 window1->consumeMotionMove();
3100 window1->assertNoEvents();
3101
3102 // Now move the pointer on the first window
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08003103 mDispatcher->notifyMotion(&(
3104 args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003105 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003106 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003107
3108 mDispatcher->notifyMotion(&(
3109 args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})));
3110 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003111 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003112}
3113
Garfield Tandf26e862020-07-01 20:18:19 -07003114TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003115 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003116 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003117 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003118 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003119 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003120 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003121 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003122
3123 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3124
3125 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
3126
3127 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003128 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003129 injectMotionEvent(mDispatcher,
3130 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3131 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003132 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003133 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003134 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003135
3136 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003137 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003138 injectMotionEvent(mDispatcher,
3139 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3140 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003141 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003142 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003143 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3144 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003145
3146 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003147 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003148 injectMotionEvent(mDispatcher,
3149 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3150 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003151 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003152 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003153 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3154 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003155
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003156 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003157 injectMotionEvent(mDispatcher,
3158 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3159 AINPUT_SOURCE_MOUSE)
3160 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3161 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003162 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003163 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003164 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003165
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003166 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003167 injectMotionEvent(mDispatcher,
3168 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3169 AINPUT_SOURCE_MOUSE)
3170 .buttonState(0)
3171 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003172 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003173 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003174 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003175
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003176 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003177 injectMotionEvent(mDispatcher,
3178 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3179 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003180 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003181 .build()));
3182 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3183
3184 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003185 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003186 injectMotionEvent(mDispatcher,
3187 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3188 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003189 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003190 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003191 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003192
3193 // No more events
3194 windowLeft->assertNoEvents();
3195 windowRight->assertNoEvents();
3196}
3197
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003198/**
3199 * Put two fingers down (and don't release them) and click the mouse button.
3200 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3201 * currently active gesture should be canceled, and the new one should proceed.
3202 */
3203TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3204 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3205 sp<FakeWindowHandle> window =
3206 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3207 window->setFrame(Rect(0, 0, 600, 800));
3208
3209 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3210
3211 const int32_t touchDeviceId = 4;
3212 const int32_t mouseDeviceId = 6;
3213 NotifyMotionArgs args;
3214
3215 // Two pointers down
3216 mDispatcher->notifyMotion(&(
3217 args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3218 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003219 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003220 .build()));
3221
3222 mDispatcher->notifyMotion(&(
3223 args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3224 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003225 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3226 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003227 .build()));
3228 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3229 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3230
3231 // Inject a series of mouse events for a mouse click
3232 mDispatcher->notifyMotion(&(
3233 args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3234 .deviceId(mouseDeviceId)
3235 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003236 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003237 .build()));
3238 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3239 WithPointerCount(2u)));
3240 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3241
3242 mDispatcher->notifyMotion(&(
3243 args = MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3244 .deviceId(mouseDeviceId)
3245 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3246 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003247 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003248 .build()));
3249 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3250
3251 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3252 // already canceled gesture, it should be ignored.
3253 mDispatcher->notifyMotion(&(
3254 args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3255 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003256 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3257 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003258 .build()));
3259 window->assertNoEvents();
3260}
3261
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003262TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3263 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3264
3265 sp<FakeWindowHandle> spyWindow =
3266 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3267 spyWindow->setFrame(Rect(0, 0, 600, 800));
3268 spyWindow->setTrustedOverlay(true);
3269 spyWindow->setSpy(true);
3270 sp<FakeWindowHandle> window =
3271 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3272 window->setFrame(Rect(0, 0, 600, 800));
3273
3274 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3275 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3276
3277 // Send mouse cursor to the window
3278 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3279 injectMotionEvent(mDispatcher,
3280 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3281 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003282 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003283 .x(100)
3284 .y(100))
3285 .build()));
3286
3287 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3288 WithSource(AINPUT_SOURCE_MOUSE)));
3289 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3290 WithSource(AINPUT_SOURCE_MOUSE)));
3291
3292 window->assertNoEvents();
3293 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003294}
3295
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003296TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3297 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3298
3299 sp<FakeWindowHandle> spyWindow =
3300 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3301 spyWindow->setFrame(Rect(0, 0, 600, 800));
3302 spyWindow->setTrustedOverlay(true);
3303 spyWindow->setSpy(true);
3304 sp<FakeWindowHandle> window =
3305 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3306 window->setFrame(Rect(0, 0, 600, 800));
3307
3308 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3309 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3310
3311 // Send mouse cursor to the window
3312 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3313 injectMotionEvent(mDispatcher,
3314 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3315 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003316 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003317 .x(100)
3318 .y(100))
3319 .build()));
3320
3321 // Move mouse cursor
3322 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3323 injectMotionEvent(mDispatcher,
3324 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3325 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003326 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003327 .x(110)
3328 .y(110))
3329 .build()));
3330
3331 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3332 WithSource(AINPUT_SOURCE_MOUSE)));
3333 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3334 WithSource(AINPUT_SOURCE_MOUSE)));
3335 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3336 WithSource(AINPUT_SOURCE_MOUSE)));
3337 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3338 WithSource(AINPUT_SOURCE_MOUSE)));
3339 // Touch down on the window
3340 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3341 injectMotionEvent(mDispatcher,
3342 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3343 AINPUT_SOURCE_TOUCHSCREEN)
3344 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003345 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003346 .x(200)
3347 .y(200))
3348 .build()));
3349 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3350 WithSource(AINPUT_SOURCE_MOUSE)));
3351 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3352 WithSource(AINPUT_SOURCE_MOUSE)));
3353 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3354 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3355 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3356 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3357
3358 // pilfer the motion, retaining the gesture on the spy window.
3359 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3360 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3361 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3362
3363 // Touch UP on the window
3364 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3365 injectMotionEvent(mDispatcher,
3366 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3367 AINPUT_SOURCE_TOUCHSCREEN)
3368 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003369 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003370 .x(200)
3371 .y(200))
3372 .build()));
3373 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3374 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3375
3376 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3377 // to send a new gesture. It should again go to both windows (spy and the window below), just
3378 // like the first gesture did, before pilfering. The window configuration has not changed.
3379
3380 // One more tap - DOWN
3381 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3382 injectMotionEvent(mDispatcher,
3383 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3384 AINPUT_SOURCE_TOUCHSCREEN)
3385 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003386 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003387 .x(250)
3388 .y(250))
3389 .build()));
3390 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3391 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3392 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3393 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3394
3395 // Touch UP on the window
3396 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3397 injectMotionEvent(mDispatcher,
3398 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3399 AINPUT_SOURCE_TOUCHSCREEN)
3400 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003401 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003402 .x(250)
3403 .y(250))
3404 .build()));
3405 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3406 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3407 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3408 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3409
3410 window->assertNoEvents();
3411 spyWindow->assertNoEvents();
3412}
3413
Garfield Tandf26e862020-07-01 20:18:19 -07003414// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3415// directly in this test.
3416TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003417 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003418 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003419 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003420 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003421
3422 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3423
3424 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3425
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003426 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003427 injectMotionEvent(mDispatcher,
3428 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3429 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003430 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003431 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003432 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003433 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003434 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003435 injectMotionEvent(mDispatcher,
3436 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3437 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003438 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003439 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003440 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3441 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003442
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003443 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003444 injectMotionEvent(mDispatcher,
3445 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3446 AINPUT_SOURCE_MOUSE)
3447 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3448 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003449 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003450 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003451 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003452
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003453 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003454 injectMotionEvent(mDispatcher,
3455 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3456 AINPUT_SOURCE_MOUSE)
3457 .buttonState(0)
3458 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003459 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003460 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003461 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003462
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003463 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003464 injectMotionEvent(mDispatcher,
3465 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3466 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003467 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003468 .build()));
3469 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3470
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003471 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003472 injectMotionEvent(mDispatcher,
3473 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3474 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003475 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003476 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003477 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003478}
3479
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003480/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003481 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3482 * is generated.
3483 */
3484TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3485 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3486 sp<FakeWindowHandle> window =
3487 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3488 window->setFrame(Rect(0, 0, 1200, 800));
3489
3490 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3491
3492 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3493
3494 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3495 injectMotionEvent(mDispatcher,
3496 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3497 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003498 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003499 .x(300)
3500 .y(400))
3501 .build()));
3502 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3503
3504 // Remove the window, but keep the channel.
3505 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3506 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3507}
3508
3509/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003510 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3511 */
3512TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3513 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3514 sp<FakeWindowHandle> window =
3515 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3516 window->setFrame(Rect(0, 0, 100, 100));
3517
3518 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3519
3520 const int32_t mouseDeviceId = 7;
3521 const int32_t touchDeviceId = 4;
3522 NotifyMotionArgs args;
3523
3524 // Start hovering with the mouse
3525 mDispatcher->notifyMotion(
3526 &(args = MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3527 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003528 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003529 .build()));
3530 window->consumeMotionEvent(
3531 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3532
3533 // Touch goes down
3534 mDispatcher->notifyMotion(
3535 &(args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3536 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003537 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003538 .build()));
3539
3540 window->consumeMotionEvent(
3541 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3542 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3543}
3544
3545/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003546 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003547 * The tap causes a HOVER_EXIT event to be generated because the current event
3548 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003549 */
3550TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3551 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3552 sp<FakeWindowHandle> window =
3553 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3554 window->setFrame(Rect(0, 0, 100, 100));
3555
3556 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3557
3558 // Inject a hover_move from mouse.
3559 NotifyMotionArgs motionArgs =
3560 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3561 ADISPLAY_ID_DEFAULT, {{50, 50}});
3562 motionArgs.xCursorPosition = 50;
3563 motionArgs.yCursorPosition = 50;
3564 mDispatcher->notifyMotion(&motionArgs);
3565 ASSERT_NO_FATAL_FAILURE(
3566 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3567 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003568
3569 // Tap on the window
3570 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3571 ADISPLAY_ID_DEFAULT, {{10, 10}});
3572 mDispatcher->notifyMotion(&motionArgs);
3573 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003574 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3575 WithSource(AINPUT_SOURCE_MOUSE))));
3576
3577 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003578 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3579 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3580
3581 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3582 ADISPLAY_ID_DEFAULT, {{10, 10}});
3583 mDispatcher->notifyMotion(&motionArgs);
3584 ASSERT_NO_FATAL_FAILURE(
3585 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3586 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3587}
3588
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003589TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3590 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3591 sp<FakeWindowHandle> windowDefaultDisplay =
3592 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3593 ADISPLAY_ID_DEFAULT);
3594 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3595 sp<FakeWindowHandle> windowSecondDisplay =
3596 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3597 SECOND_DISPLAY_ID);
3598 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3599
3600 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3601 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3602
3603 // Set cursor position in window in default display and check that hover enter and move
3604 // events are generated.
3605 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3606 injectMotionEvent(mDispatcher,
3607 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3608 AINPUT_SOURCE_MOUSE)
3609 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003610 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003611 .x(300)
3612 .y(600))
3613 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003614 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003615
3616 // Remove all windows in secondary display and check that no event happens on window in
3617 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003618 mDispatcher->setInputWindows(
3619 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003620 windowDefaultDisplay->assertNoEvents();
3621
3622 // Move cursor position in window in default display and check that only hover move
3623 // event is generated and not hover enter event.
3624 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3625 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3626 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3627 injectMotionEvent(mDispatcher,
3628 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3629 AINPUT_SOURCE_MOUSE)
3630 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003631 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003632 .x(400)
3633 .y(700))
3634 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003635 windowDefaultDisplay->consumeMotionEvent(
3636 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3637 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003638 windowDefaultDisplay->assertNoEvents();
3639}
3640
Garfield Tan00f511d2019-06-12 16:55:40 -07003641TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003642 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003643
3644 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003645 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003646 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003647 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003648 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003649 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003650
3651 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3652
Arthur Hung72d8dc32020-03-28 00:48:39 +00003653 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003654
3655 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3656 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003657 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003658 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003659 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003660 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003661 windowRight->assertNoEvents();
3662}
3663
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003664TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003665 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003666 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3667 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003668 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003669
Arthur Hung72d8dc32020-03-28 00:48:39 +00003670 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003671 setFocusedWindow(window);
3672
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003673 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003674
3675 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3676 mDispatcher->notifyKey(&keyArgs);
3677
3678 // Window should receive key down event.
3679 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3680
3681 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3682 // on the app side.
Harry Cutts33476232023-01-30 19:57:29 +00003683 NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003684 mDispatcher->notifyDeviceReset(&args);
3685 window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
3686 AKEY_EVENT_FLAG_CANCELED);
3687}
3688
3689TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003690 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003691 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3692 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003693
Arthur Hung72d8dc32020-03-28 00:48:39 +00003694 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003695
3696 NotifyMotionArgs motionArgs =
3697 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3698 ADISPLAY_ID_DEFAULT);
3699 mDispatcher->notifyMotion(&motionArgs);
3700
3701 // Window should receive motion down event.
3702 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3703
3704 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3705 // on the app side.
Harry Cutts33476232023-01-30 19:57:29 +00003706 NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003707 mDispatcher->notifyDeviceReset(&args);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003708 window->consumeMotionEvent(
3709 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003710}
3711
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003712TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3713 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003714 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3715 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003716 window->setFocusable(true);
3717
3718 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3719 setFocusedWindow(window);
3720
3721 window->consumeFocusEvent(true);
3722
3723 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3724 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3725 const nsecs_t injectTime = keyArgs.eventTime;
3726 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
3727 mDispatcher->notifyKey(&keyArgs);
3728 // The dispatching time should be always greater than or equal to intercept key timeout.
3729 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3730 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3731 std::chrono::nanoseconds(interceptKeyTimeout).count());
3732}
3733
3734TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3735 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003736 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3737 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003738 window->setFocusable(true);
3739
3740 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3741 setFocusedWindow(window);
3742
3743 window->consumeFocusEvent(true);
3744
3745 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3746 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3747 mFakePolicy->setInterceptKeyTimeout(150ms);
3748 mDispatcher->notifyKey(&keyDown);
3749 mDispatcher->notifyKey(&keyUp);
3750
3751 // Window should receive key event immediately when same key up.
3752 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3753 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3754}
3755
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003756/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003757 * Two windows. First is a regular window. Second does not overlap with the first, and has
3758 * WATCH_OUTSIDE_TOUCH.
3759 * Both windows are owned by the same UID.
3760 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3761 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3762 */
3763TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3764 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3765 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3766 "First Window", ADISPLAY_ID_DEFAULT);
3767 window->setFrame(Rect{0, 0, 100, 100});
3768
3769 sp<FakeWindowHandle> outsideWindow =
3770 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3771 ADISPLAY_ID_DEFAULT);
3772 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3773 outsideWindow->setWatchOutsideTouch(true);
3774 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3775 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3776
3777 // Tap on first window.
3778 NotifyMotionArgs motionArgs =
3779 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3780 ADISPLAY_ID_DEFAULT, {PointF{50, 50}});
3781 mDispatcher->notifyMotion(&motionArgs);
3782 window->consumeMotionDown();
3783 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3784 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3785 outsideWindow->consumeMotionEvent(
3786 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3787}
3788
3789/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003790 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3791 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3792 * ACTION_OUTSIDE event is sent per gesture.
3793 */
3794TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3795 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3796 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003797 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3798 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003799 window->setWatchOutsideTouch(true);
3800 window->setFrame(Rect{0, 0, 100, 100});
3801 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003802 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3803 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003804 secondWindow->setFrame(Rect{100, 100, 200, 200});
3805 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003806 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3807 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003808 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3809 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3810
3811 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
3812 NotifyMotionArgs motionArgs =
3813 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3814 ADISPLAY_ID_DEFAULT, {PointF{-10, -10}});
3815 mDispatcher->notifyMotion(&motionArgs);
3816 window->assertNoEvents();
3817 secondWindow->assertNoEvents();
3818
3819 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3820 // Now, `window` should get ACTION_OUTSIDE.
3821 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3822 {PointF{-10, -10}, PointF{105, 105}});
3823 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003824 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3825 window->consumeMotionEvent(
3826 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003827 secondWindow->consumeMotionDown();
3828 thirdWindow->assertNoEvents();
3829
3830 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3831 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
3832 motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3833 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}});
3834 mDispatcher->notifyMotion(&motionArgs);
3835 window->assertNoEvents();
3836 secondWindow->consumeMotionMove();
3837 thirdWindow->consumeMotionDown();
3838}
3839
Prabir Pradhan814fe082022-07-22 20:22:18 +00003840TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3841 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003842 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3843 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003844 window->setFocusable(true);
3845
3846 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
3847 setFocusedWindow(window);
3848
3849 window->consumeFocusEvent(true);
3850
3851 NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3852 NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3853 mDispatcher->notifyKey(&keyDown);
3854 mDispatcher->notifyKey(&keyUp);
3855
3856 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3857 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3858
3859 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
3860 mDispatcher->onWindowInfosChanged({}, {});
3861
3862 window->consumeFocusEvent(false);
3863
3864 mDispatcher->notifyKey(&keyDown);
3865 mDispatcher->notifyKey(&keyUp);
3866 window->assertNoEvents();
3867}
3868
Arthur Hung96483742022-11-15 03:30:48 +00003869TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3870 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3871 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3872 "Fake Window", ADISPLAY_ID_DEFAULT);
3873 // Ensure window is non-split and have some transform.
3874 window->setPreventSplitting(true);
3875 window->setWindowOffset(20, 40);
3876 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {});
3877
3878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3879 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3880 {50, 50}))
3881 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3882 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3883
3884 const MotionEvent secondFingerDownEvent =
3885 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3886 .displayId(ADISPLAY_ID_DEFAULT)
3887 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003888 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3889 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003890 .build();
3891 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3892 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3893 InputEventInjectionSync::WAIT_FOR_RESULT))
3894 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3895
3896 const MotionEvent* event = window->consumeMotion();
3897 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3898 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3899 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3900 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3901 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3902}
3903
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003904/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003905 * Ensure the correct coordinate spaces are used by InputDispatcher.
3906 *
3907 * InputDispatcher works in the display space, so its coordinate system is relative to the display
3908 * panel. Windows get events in the window space, and get raw coordinates in the logical display
3909 * space.
3910 */
3911class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
3912public:
3913 void SetUp() override {
3914 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003915 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003916 }
3917
3918 void addDisplayInfo(int displayId, const ui::Transform& transform) {
3919 gui::DisplayInfo info;
3920 info.displayId = displayId;
3921 info.transform = transform;
3922 mDisplayInfos.push_back(std::move(info));
3923 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
3924 }
3925
3926 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
3927 mWindowInfos.push_back(*windowHandle->getInfo());
3928 mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos);
3929 }
3930
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003931 void removeAllWindowsAndDisplays() {
3932 mDisplayInfos.clear();
3933 mWindowInfos.clear();
3934 }
3935
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003936 // Set up a test scenario where the display has a scaled projection and there are two windows
3937 // on the display.
3938 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
3939 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
3940 // respectively.
3941 ui::Transform displayTransform;
3942 displayTransform.set(2, 0, 0, 4);
3943 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3944
3945 std::shared_ptr<FakeApplicationHandle> application =
3946 std::make_shared<FakeApplicationHandle>();
3947
3948 // Add two windows to the display. Their frames are represented in the display space.
3949 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003950 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3951 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003952 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
3953 addWindow(firstWindow);
3954
3955 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003956 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3957 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003958 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
3959 addWindow(secondWindow);
3960 return {std::move(firstWindow), std::move(secondWindow)};
3961 }
3962
3963private:
3964 std::vector<gui::DisplayInfo> mDisplayInfos;
3965 std::vector<gui::WindowInfo> mWindowInfos;
3966};
3967
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003968TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003969 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3970 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003971 // selected so that if the hit test was performed with the point and the bounds being in
3972 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003973 NotifyMotionArgs downMotionArgs =
3974 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3975 ADISPLAY_ID_DEFAULT, {PointF{75, 55}});
3976 mDispatcher->notifyMotion(&downMotionArgs);
3977
3978 firstWindow->consumeMotionDown();
3979 secondWindow->assertNoEvents();
3980}
3981
3982// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
3983// the event should be treated as being in the logical display space.
3984TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
3985 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3986 // Send down to the first window. The point is represented in the logical display space. The
3987 // point is selected so that if the hit test was done in logical display space, then it would
3988 // end up in the incorrect window.
3989 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3990 PointF{75 * 2, 55 * 4});
3991
3992 firstWindow->consumeMotionDown();
3993 secondWindow->assertNoEvents();
3994}
3995
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003996// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
3997// event should be treated as being in the logical display space.
3998TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
3999 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4000
4001 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
4002 ui::Transform injectedEventTransform;
4003 injectedEventTransform.set(matrix);
4004 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
4005 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
4006
4007 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4008 .displayId(ADISPLAY_ID_DEFAULT)
4009 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004010 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004011 .x(untransformedPoint.x)
4012 .y(untransformedPoint.y))
4013 .build();
4014 event.transform(matrix);
4015
4016 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4017 InputEventInjectionSync::WAIT_FOR_RESULT);
4018
4019 firstWindow->consumeMotionDown();
4020 secondWindow->assertNoEvents();
4021}
4022
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004023TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4024 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4025
4026 // Send down to the second window.
4027 NotifyMotionArgs downMotionArgs =
4028 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4029 ADISPLAY_ID_DEFAULT, {PointF{150, 220}});
4030 mDispatcher->notifyMotion(&downMotionArgs);
4031
4032 firstWindow->assertNoEvents();
4033 const MotionEvent* event = secondWindow->consumeMotion();
4034 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4035
4036 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4037 EXPECT_EQ(300, event->getRawX(0));
4038 EXPECT_EQ(880, event->getRawY(0));
4039
4040 // Ensure that the x and y values are in the window's coordinate space.
4041 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4042 // the logical display space. This will be the origin of the window space.
4043 EXPECT_EQ(100, event->getX(0));
4044 EXPECT_EQ(80, event->getY(0));
4045}
4046
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004047/** Ensure consistent behavior of InputDispatcher in all orientations. */
4048class InputDispatcherDisplayOrientationFixture
4049 : public InputDispatcherDisplayProjectionTest,
4050 public ::testing::WithParamInterface<ui::Rotation> {};
4051
4052// This test verifies the touchable region of a window for all rotations of the display by tapping
4053// in different locations on the display, specifically points close to the four corners of a
4054// window.
4055TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4056 constexpr static int32_t displayWidth = 400;
4057 constexpr static int32_t displayHeight = 800;
4058
4059 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4060
4061 const auto rotation = GetParam();
4062
4063 // Set up the display with the specified rotation.
4064 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4065 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4066 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4067 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4068 logicalDisplayWidth, logicalDisplayHeight);
4069 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4070
4071 // Create a window with its bounds determined in the logical display.
4072 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4073 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4074 sp<FakeWindowHandle> window =
4075 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4076 window->setFrame(frameInDisplay, displayTransform);
4077 addWindow(window);
4078
4079 // The following points in logical display space should be inside the window.
4080 static const std::array<vec2, 4> insidePoints{
4081 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4082 for (const auto pointInsideWindow : insidePoints) {
4083 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4084 const PointF pointInDisplaySpace{p.x, p.y};
4085 const auto down = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4086 ADISPLAY_ID_DEFAULT, {pointInDisplaySpace});
4087 mDispatcher->notifyMotion(&down);
4088 window->consumeMotionDown();
4089
4090 const auto up = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4091 ADISPLAY_ID_DEFAULT, {pointInDisplaySpace});
4092 mDispatcher->notifyMotion(&up);
4093 window->consumeMotionUp();
4094 }
4095
4096 // The following points in logical display space should be outside the window.
4097 static const std::array<vec2, 5> outsidePoints{
4098 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4099 for (const auto pointOutsideWindow : outsidePoints) {
4100 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4101 const PointF pointInDisplaySpace{p.x, p.y};
4102 const auto down = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4103 ADISPLAY_ID_DEFAULT, {pointInDisplaySpace});
4104 mDispatcher->notifyMotion(&down);
4105
4106 const auto up = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4107 ADISPLAY_ID_DEFAULT, {pointInDisplaySpace});
4108 mDispatcher->notifyMotion(&up);
4109 }
4110 window->assertNoEvents();
4111}
4112
4113// Run the precision tests for all rotations.
4114INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4115 InputDispatcherDisplayOrientationFixture,
4116 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4117 ui::ROTATION_270),
4118 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4119 return ftl::enum_string(testParamInfo.param);
4120 });
4121
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004122using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4123 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004124
4125class TransferTouchFixture : public InputDispatcherTest,
4126 public ::testing::WithParamInterface<TransferFunction> {};
4127
4128TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004129 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004130
4131 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004132 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004133 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4134 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004135 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004136 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004137 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4138 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004139 sp<FakeWindowHandle> wallpaper =
4140 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4141 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004142 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004143 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004144
4145 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004146 NotifyMotionArgs downMotionArgs =
4147 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4148 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004149 mDispatcher->notifyMotion(&downMotionArgs);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004150
Svet Ganov5d3bc372020-01-26 23:11:07 -08004151 // Only the first window should get the down event
4152 firstWindow->consumeMotionDown();
4153 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004154 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004155
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004156 // Transfer touch to the second window
4157 TransferFunction f = GetParam();
4158 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4159 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004160 // The first window gets cancel and the second gets down
4161 firstWindow->consumeMotionCancel();
4162 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004163 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004164
4165 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004166 NotifyMotionArgs upMotionArgs =
4167 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4168 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004169 mDispatcher->notifyMotion(&upMotionArgs);
4170 // The first window gets no events and the second gets up
4171 firstWindow->assertNoEvents();
4172 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004173 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004174}
4175
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004176/**
4177 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4178 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4179 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4180 * natural to the user.
4181 * In this test, we are sending a pointer to both spy window and first window. We then try to
4182 * transfer touch to the second window. The dispatcher should identify the first window as the
4183 * one that should lose the gesture, and therefore the action should be to move the gesture from
4184 * the first window to the second.
4185 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4186 * the other API, as well.
4187 */
4188TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4189 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4190
4191 // Create a couple of windows + a spy window
4192 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004193 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004194 spyWindow->setTrustedOverlay(true);
4195 spyWindow->setSpy(true);
4196 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004197 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004198 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004199 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004200
4201 // Add the windows to the dispatcher
4202 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4203
4204 // Send down to the first window
4205 NotifyMotionArgs downMotionArgs =
4206 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4207 ADISPLAY_ID_DEFAULT);
4208 mDispatcher->notifyMotion(&downMotionArgs);
4209 // Only the first window and spy should get the down event
4210 spyWindow->consumeMotionDown();
4211 firstWindow->consumeMotionDown();
4212
4213 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4214 // if f === 'transferTouch'.
4215 TransferFunction f = GetParam();
4216 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4217 ASSERT_TRUE(success);
4218 // The first window gets cancel and the second gets down
4219 firstWindow->consumeMotionCancel();
4220 secondWindow->consumeMotionDown();
4221
4222 // Send up event to the second window
4223 NotifyMotionArgs upMotionArgs =
4224 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4225 ADISPLAY_ID_DEFAULT);
4226 mDispatcher->notifyMotion(&upMotionArgs);
4227 // The first window gets no events and the second+spy get up
4228 firstWindow->assertNoEvents();
4229 spyWindow->consumeMotionUp();
4230 secondWindow->consumeMotionUp();
4231}
4232
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004233TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004234 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004235
4236 PointF touchPoint = {10, 10};
4237
4238 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004239 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004240 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4241 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004242 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004243 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004244 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4245 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004246 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004247
4248 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004249 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004250
4251 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004252 NotifyMotionArgs downMotionArgs =
4253 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4254 ADISPLAY_ID_DEFAULT, {touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004255 mDispatcher->notifyMotion(&downMotionArgs);
4256 // Only the first window should get the down event
4257 firstWindow->consumeMotionDown();
4258 secondWindow->assertNoEvents();
4259
4260 // Send pointer down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004261 NotifyMotionArgs pointerDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004262 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004263 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004264 mDispatcher->notifyMotion(&pointerDownMotionArgs);
4265 // Only the first window should get the pointer down event
4266 firstWindow->consumeMotionPointerDown(1);
4267 secondWindow->assertNoEvents();
4268
4269 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004270 TransferFunction f = GetParam();
4271 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4272 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004273 // The first window gets cancel and the second gets down and pointer down
4274 firstWindow->consumeMotionCancel();
4275 secondWindow->consumeMotionDown();
4276 secondWindow->consumeMotionPointerDown(1);
4277
4278 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004279 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004280 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004281 {touchPoint, touchPoint});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004282 mDispatcher->notifyMotion(&pointerUpMotionArgs);
4283 // The first window gets nothing and the second gets pointer up
4284 firstWindow->assertNoEvents();
4285 secondWindow->consumeMotionPointerUp(1);
4286
4287 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004288 NotifyMotionArgs upMotionArgs =
4289 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4290 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004291 mDispatcher->notifyMotion(&upMotionArgs);
4292 // The first window gets nothing and the second gets up
4293 firstWindow->assertNoEvents();
4294 secondWindow->consumeMotionUp();
4295}
4296
Arthur Hungc539dbb2022-12-08 07:45:36 +00004297TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4298 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4299
4300 // Create a couple of windows
4301 sp<FakeWindowHandle> firstWindow =
4302 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4303 ADISPLAY_ID_DEFAULT);
4304 firstWindow->setDupTouchToWallpaper(true);
4305 sp<FakeWindowHandle> secondWindow =
4306 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4307 ADISPLAY_ID_DEFAULT);
4308 secondWindow->setDupTouchToWallpaper(true);
4309
4310 sp<FakeWindowHandle> wallpaper1 =
4311 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4312 wallpaper1->setIsWallpaper(true);
4313
4314 sp<FakeWindowHandle> wallpaper2 =
4315 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4316 wallpaper2->setIsWallpaper(true);
4317 // Add the windows to the dispatcher
4318 mDispatcher->setInputWindows(
4319 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4320
4321 // Send down to the first window
4322 NotifyMotionArgs downMotionArgs =
4323 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4324 ADISPLAY_ID_DEFAULT);
4325 mDispatcher->notifyMotion(&downMotionArgs);
4326
4327 // Only the first window should get the down event
4328 firstWindow->consumeMotionDown();
4329 secondWindow->assertNoEvents();
4330 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4331 wallpaper2->assertNoEvents();
4332
4333 // Transfer touch focus to the second window
4334 TransferFunction f = GetParam();
4335 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4336 ASSERT_TRUE(success);
4337
4338 // The first window gets cancel and the second gets down
4339 firstWindow->consumeMotionCancel();
4340 secondWindow->consumeMotionDown();
4341 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4342 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4343
4344 // Send up event to the second window
4345 NotifyMotionArgs upMotionArgs =
4346 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4347 ADISPLAY_ID_DEFAULT);
4348 mDispatcher->notifyMotion(&upMotionArgs);
4349 // The first window gets no events and the second gets up
4350 firstWindow->assertNoEvents();
4351 secondWindow->consumeMotionUp();
4352 wallpaper1->assertNoEvents();
4353 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4354}
4355
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004356// For the cases of single pointer touch and two pointers non-split touch, the api's
4357// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4358// for the case where there are multiple pointers split across several windows.
4359INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4360 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004361 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4362 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004363 return dispatcher->transferTouch(destChannelToken,
4364 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004365 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004366 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4367 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004368 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004369 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004370 }));
4371
Svet Ganov5d3bc372020-01-26 23:11:07 -08004372TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004373 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004374
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004375 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004376 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4377 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004378 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004379
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004380 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004381 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4382 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004383 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004384
4385 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004386 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004387
4388 PointF pointInFirst = {300, 200};
4389 PointF pointInSecond = {300, 600};
4390
4391 // Send down to the first window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004392 NotifyMotionArgs firstDownMotionArgs =
4393 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4394 ADISPLAY_ID_DEFAULT, {pointInFirst});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004395 mDispatcher->notifyMotion(&firstDownMotionArgs);
4396 // Only the first window should get the down event
4397 firstWindow->consumeMotionDown();
4398 secondWindow->assertNoEvents();
4399
4400 // Send down to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004401 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004402 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004403 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004404 mDispatcher->notifyMotion(&secondDownMotionArgs);
4405 // The first window gets a move and the second a down
4406 firstWindow->consumeMotionMove();
4407 secondWindow->consumeMotionDown();
4408
4409 // Transfer touch focus to the second window
4410 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4411 // The first window gets cancel and the new gets pointer down (it already saw down)
4412 firstWindow->consumeMotionCancel();
4413 secondWindow->consumeMotionPointerDown(1);
4414
4415 // Send pointer up to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004416 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004417 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004418 {pointInFirst, pointInSecond});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004419 mDispatcher->notifyMotion(&pointerUpMotionArgs);
4420 // The first window gets nothing and the second gets pointer up
4421 firstWindow->assertNoEvents();
4422 secondWindow->consumeMotionPointerUp(1);
4423
4424 // Send up event to the second window
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004425 NotifyMotionArgs upMotionArgs =
4426 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4427 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004428 mDispatcher->notifyMotion(&upMotionArgs);
4429 // The first window gets nothing and the second gets up
4430 firstWindow->assertNoEvents();
4431 secondWindow->consumeMotionUp();
4432}
4433
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004434// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4435// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4436// touch is not supported, so the touch should continue on those windows and the transferred-to
4437// window should get nothing.
4438TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4439 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4440
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004441 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004442 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4443 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004444 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004445
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004446 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004447 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4448 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004449 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004450
4451 // Add the windows to the dispatcher
4452 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4453
4454 PointF pointInFirst = {300, 200};
4455 PointF pointInSecond = {300, 600};
4456
4457 // Send down to the first window
4458 NotifyMotionArgs firstDownMotionArgs =
4459 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4460 ADISPLAY_ID_DEFAULT, {pointInFirst});
4461 mDispatcher->notifyMotion(&firstDownMotionArgs);
4462 // Only the first window should get the down event
4463 firstWindow->consumeMotionDown();
4464 secondWindow->assertNoEvents();
4465
4466 // Send down to the second window
4467 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004468 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004469 {pointInFirst, pointInSecond});
4470 mDispatcher->notifyMotion(&secondDownMotionArgs);
4471 // The first window gets a move and the second a down
4472 firstWindow->consumeMotionMove();
4473 secondWindow->consumeMotionDown();
4474
4475 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004476 const bool transferred =
4477 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004478 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4479 ASSERT_FALSE(transferred);
4480 firstWindow->assertNoEvents();
4481 secondWindow->assertNoEvents();
4482
4483 // The rest of the dispatch should proceed as normal
4484 // Send pointer up to the second window
4485 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004486 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004487 {pointInFirst, pointInSecond});
4488 mDispatcher->notifyMotion(&pointerUpMotionArgs);
4489 // The first window gets MOVE and the second gets pointer up
4490 firstWindow->consumeMotionMove();
4491 secondWindow->consumeMotionUp();
4492
4493 // Send up event to the first window
4494 NotifyMotionArgs upMotionArgs =
4495 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4496 ADISPLAY_ID_DEFAULT);
4497 mDispatcher->notifyMotion(&upMotionArgs);
4498 // The first window gets nothing and the second gets up
4499 firstWindow->consumeMotionUp();
4500 secondWindow->assertNoEvents();
4501}
4502
Arthur Hungabbb9d82021-09-01 14:52:30 +00004503// This case will create two windows and one mirrored window on the default display and mirror
4504// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4505// the windows info of second display before default display.
4506TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4507 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4508 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004509 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004510 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004511 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004512 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004513 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004514
4515 sp<FakeWindowHandle> mirrorWindowInPrimary =
4516 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4517 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004518
4519 sp<FakeWindowHandle> firstWindowInSecondary =
4520 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4521 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004522
4523 sp<FakeWindowHandle> secondWindowInSecondary =
4524 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4525 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004526
4527 // Update window info, let it find window handle of second display first.
4528 mDispatcher->setInputWindows(
4529 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4530 {ADISPLAY_ID_DEFAULT,
4531 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4532
4533 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4534 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4535 {50, 50}))
4536 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4537
4538 // Window should receive motion event.
4539 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4540
4541 // Transfer touch focus
4542 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4543 secondWindowInPrimary->getToken()));
4544 // The first window gets cancel.
4545 firstWindowInPrimary->consumeMotionCancel();
4546 secondWindowInPrimary->consumeMotionDown();
4547
4548 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4549 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4550 ADISPLAY_ID_DEFAULT, {150, 50}))
4551 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4552 firstWindowInPrimary->assertNoEvents();
4553 secondWindowInPrimary->consumeMotionMove();
4554
4555 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4556 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4557 {150, 50}))
4558 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4559 firstWindowInPrimary->assertNoEvents();
4560 secondWindowInPrimary->consumeMotionUp();
4561}
4562
4563// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4564// 'transferTouch' api.
4565TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4566 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4567 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004568 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004569 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004570 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004571 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004572 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004573
4574 sp<FakeWindowHandle> mirrorWindowInPrimary =
4575 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4576 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004577
4578 sp<FakeWindowHandle> firstWindowInSecondary =
4579 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4580 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004581
4582 sp<FakeWindowHandle> secondWindowInSecondary =
4583 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4584 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004585
4586 // Update window info, let it find window handle of second display first.
4587 mDispatcher->setInputWindows(
4588 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4589 {ADISPLAY_ID_DEFAULT,
4590 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4591
4592 // Touch on second display.
4593 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4594 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4595 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4596
4597 // Window should receive motion event.
4598 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4599
4600 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004601 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004602
4603 // The first window gets cancel.
4604 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4605 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4606
4607 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4608 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4609 SECOND_DISPLAY_ID, {150, 50}))
4610 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4611 firstWindowInPrimary->assertNoEvents();
4612 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4613
4614 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4615 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4616 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4617 firstWindowInPrimary->assertNoEvents();
4618 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4619}
4620
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004621TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004622 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004623 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4624 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004625
Vishnu Nair47074b82020-08-14 11:54:47 -07004626 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004627 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004628 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004629
4630 window->consumeFocusEvent(true);
4631
4632 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
4633 mDispatcher->notifyKey(&keyArgs);
4634
4635 // Window should receive key down event.
4636 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4637}
4638
4639TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004640 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004641 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4642 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004643
Arthur Hung72d8dc32020-03-28 00:48:39 +00004644 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004645
4646 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
4647 mDispatcher->notifyKey(&keyArgs);
4648 mDispatcher->waitForIdle();
4649
4650 window->assertNoEvents();
4651}
4652
4653// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4654TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004655 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004656 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4657 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004658
Arthur Hung72d8dc32020-03-28 00:48:39 +00004659 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004660
4661 // Send key
4662 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
4663 mDispatcher->notifyKey(&keyArgs);
4664 // Send motion
4665 NotifyMotionArgs motionArgs =
4666 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4667 ADISPLAY_ID_DEFAULT);
4668 mDispatcher->notifyMotion(&motionArgs);
4669
4670 // Window should receive only the motion event
4671 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4672 window->assertNoEvents(); // Key event or focus event will not be received
4673}
4674
arthurhungea3f4fc2020-12-21 23:18:53 +08004675TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4676 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4677
arthurhungea3f4fc2020-12-21 23:18:53 +08004678 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004679 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4680 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004681 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004682
arthurhungea3f4fc2020-12-21 23:18:53 +08004683 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004684 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4685 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004686 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004687
4688 // Add the windows to the dispatcher
4689 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4690
4691 PointF pointInFirst = {300, 200};
4692 PointF pointInSecond = {300, 600};
4693
4694 // Send down to the first window
4695 NotifyMotionArgs firstDownMotionArgs =
4696 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4697 ADISPLAY_ID_DEFAULT, {pointInFirst});
4698 mDispatcher->notifyMotion(&firstDownMotionArgs);
4699 // Only the first window should get the down event
4700 firstWindow->consumeMotionDown();
4701 secondWindow->assertNoEvents();
4702
4703 // Send down to the second window
4704 NotifyMotionArgs secondDownMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004705 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004706 {pointInFirst, pointInSecond});
4707 mDispatcher->notifyMotion(&secondDownMotionArgs);
4708 // The first window gets a move and the second a down
4709 firstWindow->consumeMotionMove();
4710 secondWindow->consumeMotionDown();
4711
4712 // Send pointer cancel to the second window
4713 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004714 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004715 {pointInFirst, pointInSecond});
4716 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
4717 mDispatcher->notifyMotion(&pointerUpMotionArgs);
4718 // The first window gets move and the second gets cancel.
4719 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4720 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4721
4722 // Send up event.
4723 NotifyMotionArgs upMotionArgs =
4724 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4725 ADISPLAY_ID_DEFAULT);
4726 mDispatcher->notifyMotion(&upMotionArgs);
4727 // The first window gets up and the second gets nothing.
4728 firstWindow->consumeMotionUp();
4729 secondWindow->assertNoEvents();
4730}
4731
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004732TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
4733 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4734
4735 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004736 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004737 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4738 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
4739 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
4740 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
4741
Harry Cutts33476232023-01-30 19:57:29 +00004742 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004743 window->assertNoEvents();
4744 mDispatcher->waitForIdle();
4745}
4746
chaviwd1c23182019-12-20 18:44:56 -08004747class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00004748public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004749 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004750 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07004751 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004752 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07004753 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00004754 }
4755
chaviwd1c23182019-12-20 18:44:56 -08004756 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
4757
4758 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
4759 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN,
4760 expectedDisplayId, expectedFlags);
4761 }
4762
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004763 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
4764
4765 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
4766
chaviwd1c23182019-12-20 18:44:56 -08004767 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
4768 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
4769 expectedDisplayId, expectedFlags);
4770 }
4771
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004772 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
4773 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE,
4774 expectedDisplayId, expectedFlags);
4775 }
4776
chaviwd1c23182019-12-20 18:44:56 -08004777 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
4778 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP,
4779 expectedDisplayId, expectedFlags);
4780 }
4781
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004782 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004783 mInputReceiver->consumeMotionEvent(
4784 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4785 WithDisplayId(expectedDisplayId),
4786 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004787 }
4788
Arthur Hungfbfa5722021-11-16 02:45:54 +00004789 void consumeMotionPointerDown(int32_t pointerIdx) {
4790 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4791 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
4792 mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004793 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004794 }
4795
Evan Rosky84f07f02021-04-16 10:42:42 -07004796 MotionEvent* consumeMotion() {
4797 InputEvent* event = mInputReceiver->consume();
4798 if (!event) {
4799 ADD_FAILURE() << "No event was produced";
4800 return nullptr;
4801 }
4802 if (event->getType() != AINPUT_EVENT_TYPE_MOTION) {
4803 ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion";
4804 return nullptr;
4805 }
4806 return static_cast<MotionEvent*>(event);
4807 }
4808
chaviwd1c23182019-12-20 18:44:56 -08004809 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4810
4811private:
4812 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004813};
4814
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004815using InputDispatcherMonitorTest = InputDispatcherTest;
4816
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004817/**
4818 * Two entities that receive touch: A window, and a global monitor.
4819 * The touch goes to the window, and then the window disappears.
4820 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4821 * for the monitor, as well.
4822 * 1. foregroundWindow
4823 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4824 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004825TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004826 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4827 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004828 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004829
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004830 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004831
4832 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4833 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4834 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4835 {100, 200}))
4836 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4837
4838 // Both the foreground window and the global monitor should receive the touch down
4839 window->consumeMotionDown();
4840 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4841
4842 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4843 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4844 ADISPLAY_ID_DEFAULT, {110, 200}))
4845 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4846
4847 window->consumeMotionMove();
4848 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4849
4850 // Now the foreground window goes away
4851 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4852 window->consumeMotionCancel();
4853 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
4854
4855 // If more events come in, there will be no more foreground window to send them to. This will
4856 // cause a cancel for the monitor, as well.
4857 ASSERT_EQ(InputEventInjectionResult::FAILED,
4858 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4859 ADISPLAY_ID_DEFAULT, {120, 200}))
4860 << "Injection should fail because the window was removed";
4861 window->assertNoEvents();
4862 // Global monitor now gets the cancel
4863 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4864}
4865
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004866TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07004867 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004868 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4869 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004870 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004871
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004872 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004873
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004874 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004875 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004876 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00004877 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004878 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004879}
4880
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004881TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
4882 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004883
Chris Yea209fde2020-07-22 13:54:51 -07004884 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004885 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4886 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004887 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004888
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004889 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004890 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004891 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08004892 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004893 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004894
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004895 // Pilfer pointers from the monitor.
4896 // This should not do anything and the window should continue to receive events.
4897 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00004898
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004899 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004900 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4901 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004902 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004903
4904 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4905 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004906}
4907
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004908TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07004909 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004910 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4911 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004912 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4913 window->setWindowOffset(20, 40);
4914 window->setWindowTransform(0, 1, -1, 0);
4915
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004916 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004917
4918 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4919 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4920 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4921 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4922 MotionEvent* event = monitor.consumeMotion();
4923 // Even though window has transform, gesture monitor must not.
4924 ASSERT_EQ(ui::Transform(), event->getTransform());
4925}
4926
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004927TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00004928 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004929 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00004930
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004931 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00004932 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004933 << "Injection should fail if there is a monitor, but no touchable window";
4934 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00004935}
4936
chaviw81e2bb92019-12-18 15:03:51 -08004937TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004938 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004939 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4940 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08004941
Arthur Hung72d8dc32020-03-28 00:48:39 +00004942 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08004943
4944 NotifyMotionArgs motionArgs =
4945 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4946 ADISPLAY_ID_DEFAULT);
4947
4948 mDispatcher->notifyMotion(&motionArgs);
4949 // Window should receive motion down event.
4950 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4951
4952 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08004953 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08004954 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
4955 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4956 motionArgs.pointerCoords[0].getX() - 10);
4957
4958 mDispatcher->notifyMotion(&motionArgs);
4959 window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004960 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08004961}
4962
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004963/**
4964 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
4965 * the device default right away. In the test scenario, we check both the default value,
4966 * and the action of enabling / disabling.
4967 */
4968TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07004969 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004970 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4971 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08004972 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004973
4974 // Set focused application.
4975 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07004976 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004977
4978 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00004979 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004980 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004981 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004982
4983 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004984 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004985 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00004986 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004987
4988 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08004989 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00004990 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00004991 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07004992 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004993 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004994 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00004995 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004996
4997 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07004998 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004999 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005000 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005001
5002 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005003 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005004 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005005 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005006 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005007 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005008 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005009 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005010
5011 window->assertNoEvents();
5012}
5013
Gang Wange9087892020-01-07 12:17:14 -05005014TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005015 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005016 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5017 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005018
5019 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005020 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005021
Arthur Hung72d8dc32020-03-28 00:48:39 +00005022 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005023 setFocusedWindow(window);
5024
Harry Cutts33476232023-01-30 19:57:29 +00005025 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005026
5027 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5028 mDispatcher->notifyKey(&keyArgs);
5029
5030 InputEvent* event = window->consume();
5031 ASSERT_NE(event, nullptr);
5032
5033 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5034 ASSERT_NE(verified, nullptr);
5035 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5036
5037 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5038 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5039 ASSERT_EQ(keyArgs.source, verified->source);
5040 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5041
5042 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5043
5044 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005045 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005046 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005047 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5048 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5049 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5050 ASSERT_EQ(0, verifiedKey.repeatCount);
5051}
5052
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005053TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005054 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005055 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5056 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005057
5058 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5059
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005060 ui::Transform transform;
5061 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5062
5063 gui::DisplayInfo displayInfo;
5064 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5065 displayInfo.transform = transform;
5066
5067 mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005068
5069 NotifyMotionArgs motionArgs =
5070 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5071 ADISPLAY_ID_DEFAULT);
5072 mDispatcher->notifyMotion(&motionArgs);
5073
5074 InputEvent* event = window->consume();
5075 ASSERT_NE(event, nullptr);
5076
5077 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5078 ASSERT_NE(verified, nullptr);
5079 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5080
5081 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5082 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5083 EXPECT_EQ(motionArgs.source, verified->source);
5084 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5085
5086 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5087
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005088 const vec2 rawXY =
5089 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5090 motionArgs.pointerCoords[0].getXYValue());
5091 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5092 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005093 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005094 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005095 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005096 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5097 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5098}
5099
chaviw09c8d2d2020-08-24 15:48:26 -07005100/**
5101 * Ensure that separate calls to sign the same data are generating the same key.
5102 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5103 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5104 * tests.
5105 */
5106TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5107 KeyEvent event = getTestKeyEvent();
5108 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5109
5110 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5111 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5112 ASSERT_EQ(hmac1, hmac2);
5113}
5114
5115/**
5116 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5117 */
5118TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5119 KeyEvent event = getTestKeyEvent();
5120 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5121 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5122
5123 verifiedEvent.deviceId += 1;
5124 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5125
5126 verifiedEvent.source += 1;
5127 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5128
5129 verifiedEvent.eventTimeNanos += 1;
5130 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5131
5132 verifiedEvent.displayId += 1;
5133 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5134
5135 verifiedEvent.action += 1;
5136 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5137
5138 verifiedEvent.downTimeNanos += 1;
5139 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5140
5141 verifiedEvent.flags += 1;
5142 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5143
5144 verifiedEvent.keyCode += 1;
5145 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5146
5147 verifiedEvent.scanCode += 1;
5148 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5149
5150 verifiedEvent.metaState += 1;
5151 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5152
5153 verifiedEvent.repeatCount += 1;
5154 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5155}
5156
Vishnu Nair958da932020-08-21 17:12:37 -07005157TEST_F(InputDispatcherTest, SetFocusedWindow) {
5158 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5159 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005160 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005161 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005162 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005163 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5164
5165 // Top window is also focusable but is not granted focus.
5166 windowTop->setFocusable(true);
5167 windowSecond->setFocusable(true);
5168 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5169 setFocusedWindow(windowSecond);
5170
5171 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005172 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5173 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005174
5175 // Focused window should receive event.
5176 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5177 windowTop->assertNoEvents();
5178}
5179
5180TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5181 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5182 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005183 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005184 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5185
5186 window->setFocusable(true);
5187 // Release channel for window is no longer valid.
5188 window->releaseChannel();
5189 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5190 setFocusedWindow(window);
5191
5192 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005193 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5194 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005195
5196 // window channel is invalid, so it should not receive any input event.
5197 window->assertNoEvents();
5198}
5199
5200TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5201 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5202 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005203 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005204 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005205 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5206
Vishnu Nair958da932020-08-21 17:12:37 -07005207 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5208 setFocusedWindow(window);
5209
5210 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005211 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5212 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005213
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005214 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005215 window->assertNoEvents();
5216}
5217
5218TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5219 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5220 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005221 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005222 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005223 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005224 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5225
5226 windowTop->setFocusable(true);
5227 windowSecond->setFocusable(true);
5228 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5229 setFocusedWindow(windowTop);
5230 windowTop->consumeFocusEvent(true);
5231
5232 setFocusedWindow(windowSecond, windowTop);
5233 windowSecond->consumeFocusEvent(true);
5234 windowTop->consumeFocusEvent(false);
5235
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005236 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5237 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005238
5239 // Focused window should receive event.
5240 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5241}
5242
5243TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) {
5244 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5245 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005246 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005247 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005248 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005249 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5250
5251 windowTop->setFocusable(true);
5252 windowSecond->setFocusable(true);
5253 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5254 setFocusedWindow(windowSecond, windowTop);
5255
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005256 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5257 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005258
5259 // Event should be dropped.
5260 windowTop->assertNoEvents();
5261 windowSecond->assertNoEvents();
5262}
5263
5264TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5265 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5266 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005267 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005268 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005269 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5270 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005271 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5272
5273 window->setFocusable(true);
5274 previousFocusedWindow->setFocusable(true);
5275 window->setVisible(false);
5276 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5277 setFocusedWindow(previousFocusedWindow);
5278 previousFocusedWindow->consumeFocusEvent(true);
5279
5280 // Requesting focus on invisible window takes focus from currently focused window.
5281 setFocusedWindow(window);
5282 previousFocusedWindow->consumeFocusEvent(false);
5283
5284 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005286 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5287 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005288
5289 // Window does not get focus event or key down.
5290 window->assertNoEvents();
5291
5292 // Window becomes visible.
5293 window->setVisible(true);
5294 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5295
5296 // Window receives focus event.
5297 window->consumeFocusEvent(true);
5298 // Focused window receives key down.
5299 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5300}
5301
Vishnu Nair599f1412021-06-21 10:39:58 -07005302TEST_F(InputDispatcherTest, DisplayRemoved) {
5303 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5304 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005305 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005306 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5307
5308 // window is granted focus.
5309 window->setFocusable(true);
5310 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5311 setFocusedWindow(window);
5312 window->consumeFocusEvent(true);
5313
5314 // When a display is removed window loses focus.
5315 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5316 window->consumeFocusEvent(false);
5317}
5318
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005319/**
5320 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5321 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5322 * of the 'slipperyEnterWindow'.
5323 *
5324 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5325 * a way so that the touched location is no longer covered by the top window.
5326 *
5327 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5328 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5329 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5330 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5331 * with ACTION_DOWN).
5332 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5333 * window moved itself away from the touched location and had Flag::SLIPPERY.
5334 *
5335 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5336 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5337 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5338 *
5339 * In this test, we ensure that the event received by the bottom window has
5340 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5341 */
5342TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005343 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5344 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005345
5346 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5347 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5348
5349 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005350 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005351 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005352 // Make sure this one overlaps the bottom window
5353 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5354 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5355 // one. Windows with the same owner are not considered to be occluding each other.
5356 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5357
5358 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005359 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005360 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5361
5362 mDispatcher->setInputWindows(
5363 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5364
5365 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
5366 NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5367 ADISPLAY_ID_DEFAULT, {{50, 50}});
5368 mDispatcher->notifyMotion(&args);
5369 slipperyExitWindow->consumeMotionDown();
5370 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5371 mDispatcher->setInputWindows(
5372 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5373
5374 args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5375 ADISPLAY_ID_DEFAULT, {{51, 51}});
5376 mDispatcher->notifyMotion(&args);
5377
5378 slipperyExitWindow->consumeMotionCancel();
5379
5380 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5381 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5382}
5383
Garfield Tan1c7bc862020-01-28 13:24:04 -08005384class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5385protected:
5386 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5387 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5388
Chris Yea209fde2020-07-22 13:54:51 -07005389 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005390 sp<FakeWindowHandle> mWindow;
5391
5392 virtual void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005393 mFakePolicy = sp<FakeInputDispatcherPolicy>::make();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005394 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005395 mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005396 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5397 ASSERT_EQ(OK, mDispatcher->start());
5398
5399 setUpWindow();
5400 }
5401
5402 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005403 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005404 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005405
Vishnu Nair47074b82020-08-14 11:54:47 -07005406 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005407 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005408 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005409 mWindow->consumeFocusEvent(true);
5410 }
5411
Chris Ye2ad95392020-09-01 13:44:44 -07005412 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005413 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005414 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005415 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
5416 mDispatcher->notifyKey(&keyArgs);
5417
5418 // Window should receive key down event.
5419 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5420 }
5421
5422 void expectKeyRepeatOnce(int32_t repeatCount) {
5423 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5424 InputEvent* repeatEvent = mWindow->consume();
5425 ASSERT_NE(nullptr, repeatEvent);
5426
5427 uint32_t eventType = repeatEvent->getType();
5428 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType);
5429
5430 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5431 uint32_t eventAction = repeatKeyEvent->getAction();
5432 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5433 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5434 }
5435
Chris Ye2ad95392020-09-01 13:44:44 -07005436 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005437 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005438 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005439 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
5440 mDispatcher->notifyKey(&keyArgs);
5441
5442 // Window should receive key down event.
5443 mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005444 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005445 }
5446};
5447
5448TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005449 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005450 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5451 expectKeyRepeatOnce(repeatCount);
5452 }
5453}
5454
5455TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005456 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005457 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5458 expectKeyRepeatOnce(repeatCount);
5459 }
Harry Cutts33476232023-01-30 19:57:29 +00005460 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005461 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005462 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5463 expectKeyRepeatOnce(repeatCount);
5464 }
5465}
5466
5467TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005468 sendAndConsumeKeyDown(/*deviceId=*/1);
5469 expectKeyRepeatOnce(/*repeatCount=*/1);
5470 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005471 mWindow->assertNoEvents();
5472}
5473
5474TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005475 sendAndConsumeKeyDown(/*deviceId=*/1);
5476 expectKeyRepeatOnce(/*repeatCount=*/1);
5477 sendAndConsumeKeyDown(/*deviceId=*/2);
5478 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005479 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005480 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005481 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005482 expectKeyRepeatOnce(/*repeatCount=*/2);
5483 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005484 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005485 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005486 mWindow->assertNoEvents();
5487}
5488
5489TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005490 sendAndConsumeKeyDown(/*deviceId=*/1);
5491 expectKeyRepeatOnce(/*repeatCount=*/1);
5492 sendAndConsumeKeyDown(/*deviceId=*/2);
5493 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005494 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005495 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005496 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005497 mWindow->assertNoEvents();
5498}
5499
liushenxiang42232912021-05-21 20:24:09 +08005500TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5501 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005502 expectKeyRepeatOnce(/*repeatCount=*/1);
5503 NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID);
liushenxiang42232912021-05-21 20:24:09 +08005504 mDispatcher->notifyDeviceReset(&args);
5505 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5506 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5507 mWindow->assertNoEvents();
5508}
5509
Garfield Tan1c7bc862020-01-28 13:24:04 -08005510TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005511 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005512 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005513 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5514 InputEvent* repeatEvent = mWindow->consume();
5515 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5516 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5517 IdGenerator::getSource(repeatEvent->getId()));
5518 }
5519}
5520
5521TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005522 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005523 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005524
5525 std::unordered_set<int32_t> idSet;
5526 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5527 InputEvent* repeatEvent = mWindow->consume();
5528 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5529 int32_t id = repeatEvent->getId();
5530 EXPECT_EQ(idSet.end(), idSet.find(id));
5531 idSet.insert(id);
5532 }
5533}
5534
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005535/* Test InputDispatcher for MultiDisplay */
5536class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5537public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005538 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005539 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005540
Chris Yea209fde2020-07-22 13:54:51 -07005541 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005542 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005543 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005544
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005545 // Set focus window for primary display, but focused display would be second one.
5546 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005547 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005548 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005549 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005550 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005551
Chris Yea209fde2020-07-22 13:54:51 -07005552 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005553 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005554 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005555 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005556 // Set focus display to second one.
5557 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5558 // Set focus window for second display.
5559 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005560 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005561 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005562 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005563 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005564 }
5565
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005566 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005567 InputDispatcherTest::TearDown();
5568
Chris Yea209fde2020-07-22 13:54:51 -07005569 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005570 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005571 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005572 windowInSecondary.clear();
5573 }
5574
5575protected:
Chris Yea209fde2020-07-22 13:54:51 -07005576 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005577 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005578 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005579 sp<FakeWindowHandle> windowInSecondary;
5580};
5581
5582TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5583 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005584 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5585 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5586 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005587 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005588 windowInSecondary->assertNoEvents();
5589
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005590 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005591 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5592 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5593 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005594 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005595 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005596}
5597
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005598TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005599 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005600 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5601 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005602 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005603 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005604 windowInSecondary->assertNoEvents();
5605
5606 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005607 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005608 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005609 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005610 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005611
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005612 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005613 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005614
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005615 // Old focus should receive a cancel event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005616 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
5617 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005618
5619 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005620 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005621 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005622 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005623 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005624 windowInSecondary->assertNoEvents();
5625}
5626
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005627// Test per-display input monitors for motion event.
5628TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005629 FakeMonitorReceiver monitorInPrimary =
5630 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5631 FakeMonitorReceiver monitorInSecondary =
5632 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005633
5634 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005635 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5636 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5637 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005638 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005639 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005640 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005641 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005642
5643 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005644 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5645 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5646 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005647 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005648 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005649 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005650 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005651
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005652 // Lift up the touch from the second display
5653 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5654 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5655 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5656 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5657 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5658
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005659 // Test inject a non-pointer motion event.
5660 // If specific a display, it will dispatch to the focused window of particular display,
5661 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005662 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5663 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5664 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005665 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005666 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005667 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005668 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005669}
5670
5671// Test per-display input monitors for key event.
5672TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005673 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005674 FakeMonitorReceiver monitorInPrimary =
5675 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5676 FakeMonitorReceiver monitorInSecondary =
5677 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005678
5679 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005680 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5681 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005682 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005683 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005684 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005685 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005686}
5687
Vishnu Nair958da932020-08-21 17:12:37 -07005688TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
5689 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005690 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005691 secondWindowInPrimary->setFocusable(true);
5692 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
5693 setFocusedWindow(secondWindowInPrimary);
5694 windowInPrimary->consumeFocusEvent(false);
5695 secondWindowInPrimary->consumeFocusEvent(true);
5696
5697 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005698 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
5699 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005700 windowInPrimary->assertNoEvents();
5701 windowInSecondary->assertNoEvents();
5702 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5703}
5704
Arthur Hungdfd528e2021-12-08 13:23:04 +00005705TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
5706 FakeMonitorReceiver monitorInPrimary =
5707 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5708 FakeMonitorReceiver monitorInSecondary =
5709 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
5710
5711 // Test touch down on primary display.
5712 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5713 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5714 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5715 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5716 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5717
5718 // Test touch down on second display.
5719 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5720 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5721 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5722 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
5723 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
5724
5725 // Trigger cancel touch.
5726 mDispatcher->cancelCurrentTouch();
5727 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5728 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5729 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
5730 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
5731
5732 // Test inject a move motion event, no window/monitor should receive the event.
5733 ASSERT_EQ(InputEventInjectionResult::FAILED,
5734 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5735 ADISPLAY_ID_DEFAULT, {110, 200}))
5736 << "Inject motion event should return InputEventInjectionResult::FAILED";
5737 windowInPrimary->assertNoEvents();
5738 monitorInPrimary.assertNoEvents();
5739
5740 ASSERT_EQ(InputEventInjectionResult::FAILED,
5741 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5742 SECOND_DISPLAY_ID, {110, 200}))
5743 << "Inject motion event should return InputEventInjectionResult::FAILED";
5744 windowInSecondary->assertNoEvents();
5745 monitorInSecondary.assertNoEvents();
5746}
5747
Jackal Guof9696682018-10-05 12:23:23 +08005748class InputFilterTest : public InputDispatcherTest {
5749protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005750 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
5751 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08005752 NotifyMotionArgs motionArgs;
5753
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005754 motionArgs =
5755 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08005756 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005757 motionArgs =
5758 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Jackal Guof9696682018-10-05 12:23:23 +08005759 mDispatcher->notifyMotion(&motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005760 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005761 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005762 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
5763 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08005764 } else {
5765 mFakePolicy->assertFilterInputEventWasNotCalled();
5766 }
5767 }
5768
5769 void testNotifyKey(bool expectToBeFiltered) {
5770 NotifyKeyArgs keyArgs;
5771
5772 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5773 mDispatcher->notifyKey(&keyArgs);
5774 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
5775 mDispatcher->notifyKey(&keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005776 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005777
5778 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08005779 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005780 } else {
5781 mFakePolicy->assertFilterInputEventWasNotCalled();
5782 }
5783 }
5784};
5785
5786// Test InputFilter for MotionEvent
5787TEST_F(InputFilterTest, MotionEvent_InputFilter) {
5788 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
5789 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5790 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5791
5792 // Enable InputFilter
5793 mDispatcher->setInputFilterEnabled(true);
5794 // Test touch on both primary and second display, and check if both events are filtered.
5795 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
5796 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
5797
5798 // Disable InputFilter
5799 mDispatcher->setInputFilterEnabled(false);
5800 // Test touch on both primary and second display, and check if both events aren't filtered.
5801 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5802 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5803}
5804
5805// Test InputFilter for KeyEvent
5806TEST_F(InputFilterTest, KeyEvent_InputFilter) {
5807 // Since the InputFilter is disabled by default, check if key event aren't filtered.
5808 testNotifyKey(/*expectToBeFiltered*/ false);
5809
5810 // Enable InputFilter
5811 mDispatcher->setInputFilterEnabled(true);
5812 // Send a key event, and check if it is filtered.
5813 testNotifyKey(/*expectToBeFiltered*/ true);
5814
5815 // Disable InputFilter
5816 mDispatcher->setInputFilterEnabled(false);
5817 // Send a key event, and check if it isn't filtered.
5818 testNotifyKey(/*expectToBeFiltered*/ false);
5819}
5820
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005821// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
5822// logical display coordinate space.
5823TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
5824 ui::Transform firstDisplayTransform;
5825 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5826 ui::Transform secondDisplayTransform;
5827 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
5828
5829 std::vector<gui::DisplayInfo> displayInfos(2);
5830 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
5831 displayInfos[0].transform = firstDisplayTransform;
5832 displayInfos[1].displayId = SECOND_DISPLAY_ID;
5833 displayInfos[1].transform = secondDisplayTransform;
5834
5835 mDispatcher->onWindowInfosChanged({}, displayInfos);
5836
5837 // Enable InputFilter
5838 mDispatcher->setInputFilterEnabled(true);
5839
5840 // Ensure the correct transforms are used for the displays.
5841 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
5842 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
5843}
5844
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005845class InputFilterInjectionPolicyTest : public InputDispatcherTest {
5846protected:
5847 virtual void SetUp() override {
5848 InputDispatcherTest::SetUp();
5849
5850 /**
5851 * We don't need to enable input filter to test the injected event policy, but we enabled it
5852 * here to make the tests more realistic, since this policy only matters when inputfilter is
5853 * on.
5854 */
5855 mDispatcher->setInputFilterEnabled(true);
5856
5857 std::shared_ptr<InputApplicationHandle> application =
5858 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005859 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
5860 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005861
5862 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5863 mWindow->setFocusable(true);
5864 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5865 setFocusedWindow(mWindow);
5866 mWindow->consumeFocusEvent(true);
5867 }
5868
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005869 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5870 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005871 KeyEvent event;
5872
5873 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5874 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
5875 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00005876 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005877 const int32_t additionalPolicyFlags =
5878 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
5879 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005880 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005881 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5882 policyFlags | additionalPolicyFlags));
5883
5884 InputEvent* received = mWindow->consume();
5885 ASSERT_NE(nullptr, received);
5886 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005887 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY);
5888 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
5889 ASSERT_EQ(flags, keyEvent.getFlags());
5890 }
5891
5892 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5893 int32_t flags) {
5894 MotionEvent event;
5895 PointerProperties pointerProperties[1];
5896 PointerCoords pointerCoords[1];
5897 pointerProperties[0].clear();
5898 pointerProperties[0].id = 0;
5899 pointerCoords[0].clear();
5900 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
5901 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
5902
5903 ui::Transform identityTransform;
5904 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5905 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
5906 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
5907 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
5908 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07005909 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07005910 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005911 /*pointerCount*/ 1, pointerProperties, pointerCoords);
5912
5913 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
5914 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005915 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005916 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5917 policyFlags | additionalPolicyFlags));
5918
5919 InputEvent* received = mWindow->consume();
5920 ASSERT_NE(nullptr, received);
5921 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
5922 ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION);
5923 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
5924 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005925 }
5926
5927private:
5928 sp<FakeWindowHandle> mWindow;
5929};
5930
5931TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005932 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
5933 // filter. Without it, the event will no different from a regularly injected event, and the
5934 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00005935 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
5936 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005937}
5938
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005939TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005940 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005941 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005942 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
5943}
5944
5945TEST_F(InputFilterInjectionPolicyTest,
5946 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
5947 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00005948 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005949 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005950}
5951
5952TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00005953 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
5954 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005955}
5956
chaviwfd6d3512019-03-25 13:23:49 -07005957class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005958 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07005959 InputDispatcherTest::SetUp();
5960
Chris Yea209fde2020-07-22 13:54:51 -07005961 std::shared_ptr<FakeApplicationHandle> application =
5962 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005963 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005964 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07005965 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07005966
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005967 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005968 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005969 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07005970
5971 // Set focused application.
5972 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005973 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07005974
5975 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005976 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005977 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005978 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07005979 }
5980
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005981 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07005982 InputDispatcherTest::TearDown();
5983
5984 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005985 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07005986 }
5987
5988protected:
5989 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005990 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005991 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07005992};
5993
5994// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
5995// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
5996// the onPointerDownOutsideFocus callback.
5997TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005998 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07005999 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6000 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006001 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006002 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006003
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006004 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006005 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6006}
6007
6008// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6009// DOWN on the window that doesn't have focus. Ensure no window received the
6010// onPointerDownOutsideFocus callback.
6011TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006012 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006013 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006014 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006015 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006016
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006017 ASSERT_TRUE(mDispatcher->waitForIdle());
6018 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006019}
6020
6021// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6022// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6023TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006024 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6025 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006026 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006027 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006028
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006029 ASSERT_TRUE(mDispatcher->waitForIdle());
6030 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006031}
6032
6033// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6034// DOWN on the window that already has focus. Ensure no window received the
6035// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006036TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006037 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006038 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006039 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006040 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006041 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006042
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006043 ASSERT_TRUE(mDispatcher->waitForIdle());
6044 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006045}
6046
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006047// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6048// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6049TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6050 const MotionEvent event =
6051 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6052 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006053 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006054 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6055 .build();
6056 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6057 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6058 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6059
6060 ASSERT_TRUE(mDispatcher->waitForIdle());
6061 mFakePolicy->assertOnPointerDownWasNotCalled();
6062 // Ensure that the unfocused window did not receive any FOCUS events.
6063 mUnfocusedWindow->assertNoEvents();
6064}
6065
chaviwaf87b3e2019-10-01 16:59:28 -07006066// These tests ensures we can send touch events to a single client when there are multiple input
6067// windows that point to the same client token.
6068class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6069 virtual void SetUp() override {
6070 InputDispatcherTest::SetUp();
6071
Chris Yea209fde2020-07-22 13:54:51 -07006072 std::shared_ptr<FakeApplicationHandle> application =
6073 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006074 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6075 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006076 mWindow1->setFrame(Rect(0, 0, 100, 100));
6077
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006078 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6079 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006080 mWindow2->setFrame(Rect(100, 100, 200, 200));
6081
Arthur Hung72d8dc32020-03-28 00:48:39 +00006082 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006083 }
6084
6085protected:
6086 sp<FakeWindowHandle> mWindow1;
6087 sp<FakeWindowHandle> mWindow2;
6088
6089 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006090 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006091 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6092 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006093 }
6094
6095 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6096 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006097 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006098 InputEvent* event = window->consume();
6099
6100 ASSERT_NE(nullptr, event) << name.c_str()
6101 << ": consumer should have returned non-NULL event.";
6102
6103 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
6104 << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION)
6105 << " event, got " << inputEventTypeToString(event->getType()) << " event";
6106
6107 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006108 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006109 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006110
6111 for (size_t i = 0; i < points.size(); i++) {
6112 float expectedX = points[i].x;
6113 float expectedY = points[i].y;
6114
6115 EXPECT_EQ(expectedX, motionEvent.getX(i))
6116 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6117 << ", got " << motionEvent.getX(i);
6118 EXPECT_EQ(expectedY, motionEvent.getY(i))
6119 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6120 << ", got " << motionEvent.getY(i);
6121 }
6122 }
chaviw9eaa22c2020-07-01 16:21:27 -07006123
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006124 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006125 std::vector<PointF> expectedPoints) {
6126 NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6127 ADISPLAY_ID_DEFAULT, touchedPoints);
6128 mDispatcher->notifyMotion(&motionArgs);
6129
6130 // Always consume from window1 since it's the window that has the InputReceiver
6131 consumeMotionEvent(mWindow1, action, expectedPoints);
6132 }
chaviwaf87b3e2019-10-01 16:59:28 -07006133};
6134
6135TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6136 // Touch Window 1
6137 PointF touchedPoint = {10, 10};
6138 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006139 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006140
6141 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006142 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006143
6144 // Touch Window 2
6145 touchedPoint = {150, 150};
6146 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006147 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006148}
6149
chaviw9eaa22c2020-07-01 16:21:27 -07006150TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6151 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006152 mWindow2->setWindowScale(0.5f, 0.5f);
6153
6154 // Touch Window 1
6155 PointF touchedPoint = {10, 10};
6156 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006157 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006158 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006159 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006160
6161 // Touch Window 2
6162 touchedPoint = {150, 150};
6163 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006164 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6165 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006166
chaviw9eaa22c2020-07-01 16:21:27 -07006167 // Update the transform so rotation is set
6168 mWindow2->setWindowTransform(0, -1, 1, 0);
6169 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6170 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006171}
6172
chaviw9eaa22c2020-07-01 16:21:27 -07006173TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006174 mWindow2->setWindowScale(0.5f, 0.5f);
6175
6176 // Touch Window 1
6177 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6178 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006179 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006180
6181 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006182 touchedPoints.push_back(PointF{150, 150});
6183 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006184 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006185
chaviw9eaa22c2020-07-01 16:21:27 -07006186 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006187 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006188 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006189
chaviw9eaa22c2020-07-01 16:21:27 -07006190 // Update the transform so rotation is set for Window 2
6191 mWindow2->setWindowTransform(0, -1, 1, 0);
6192 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006193 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006194}
6195
chaviw9eaa22c2020-07-01 16:21:27 -07006196TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006197 mWindow2->setWindowScale(0.5f, 0.5f);
6198
6199 // Touch Window 1
6200 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6201 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006202 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006203
6204 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006205 touchedPoints.push_back(PointF{150, 150});
6206 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006207
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006208 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006209
6210 // Move both windows
6211 touchedPoints = {{20, 20}, {175, 175}};
6212 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6213 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6214
chaviw9eaa22c2020-07-01 16:21:27 -07006215 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006216
chaviw9eaa22c2020-07-01 16:21:27 -07006217 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006218 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006219 expectedPoints.pop_back();
6220
6221 // Touch Window 2
6222 mWindow2->setWindowTransform(0, -1, 1, 0);
6223 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006224 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006225
6226 // Move both windows
6227 touchedPoints = {{20, 20}, {175, 175}};
6228 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6229 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6230
6231 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006232}
6233
6234TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6235 mWindow1->setWindowScale(0.5f, 0.5f);
6236
6237 // Touch Window 1
6238 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6239 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006240 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006241
6242 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006243 touchedPoints.push_back(PointF{150, 150});
6244 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006245
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006246 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006247
6248 // Move both windows
6249 touchedPoints = {{20, 20}, {175, 175}};
6250 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6251 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6252
chaviw9eaa22c2020-07-01 16:21:27 -07006253 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006254}
6255
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006256class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6257 virtual void SetUp() override {
6258 InputDispatcherTest::SetUp();
6259
Chris Yea209fde2020-07-22 13:54:51 -07006260 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006261 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006262 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6263 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006264 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006265 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006266 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006267
6268 // Set focused application.
6269 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6270
6271 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006272 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006273 mWindow->consumeFocusEvent(true);
6274 }
6275
6276 virtual void TearDown() override {
6277 InputDispatcherTest::TearDown();
6278 mWindow.clear();
6279 }
6280
6281protected:
Chris Yea209fde2020-07-22 13:54:51 -07006282 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006283 sp<FakeWindowHandle> mWindow;
6284 static constexpr PointF WINDOW_LOCATION = {20, 20};
6285
6286 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006287 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006288 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6289 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006290 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006291 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6292 WINDOW_LOCATION));
6293 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006294
6295 sp<FakeWindowHandle> addSpyWindow() {
6296 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006297 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006298 spy->setTrustedOverlay(true);
6299 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006300 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006301 spy->setDispatchingTimeout(30ms);
6302 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6303 return spy;
6304 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006305};
6306
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006307// Send a tap and respond, which should not cause an ANR.
6308TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6309 tapOnWindow();
6310 mWindow->consumeMotionDown();
6311 mWindow->consumeMotionUp();
6312 ASSERT_TRUE(mDispatcher->waitForIdle());
6313 mFakePolicy->assertNotifyAnrWasNotCalled();
6314}
6315
6316// Send a regular key and respond, which should not cause an ANR.
6317TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006318 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006319 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6320 ASSERT_TRUE(mDispatcher->waitForIdle());
6321 mFakePolicy->assertNotifyAnrWasNotCalled();
6322}
6323
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006324TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6325 mWindow->setFocusable(false);
6326 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6327 mWindow->consumeFocusEvent(false);
6328
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006329 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006330 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6331 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6332 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006333 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006334 // Key will not go to window because we have no focused window.
6335 // The 'no focused window' ANR timer should start instead.
6336
6337 // Now, the focused application goes away.
6338 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6339 // The key should get dropped and there should be no ANR.
6340
6341 ASSERT_TRUE(mDispatcher->waitForIdle());
6342 mFakePolicy->assertNotifyAnrWasNotCalled();
6343}
6344
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006345// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006346// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6347// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006348TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006349 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006350 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6351 WINDOW_LOCATION));
6352
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006353 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6354 ASSERT_TRUE(sequenceNum);
6355 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006356 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006357
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006358 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006359 mWindow->consumeMotionEvent(
6360 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006361 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006362 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006363}
6364
6365// Send a key to the app and have the app not respond right away.
6366TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6367 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006368 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006369 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6370 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006371 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006372 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006373 ASSERT_TRUE(mDispatcher->waitForIdle());
6374}
6375
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006376// We have a focused application, but no focused window
6377TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006378 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006379 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6380 mWindow->consumeFocusEvent(false);
6381
6382 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006383 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006384 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6385 WINDOW_LOCATION));
6386 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6387 mDispatcher->waitForIdle();
6388 mFakePolicy->assertNotifyAnrWasNotCalled();
6389
6390 // Once a focused event arrives, we get an ANR for this application
6391 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6392 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006393 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006394 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6395 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006396 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006397 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006398 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006399 ASSERT_TRUE(mDispatcher->waitForIdle());
6400}
6401
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006402/**
6403 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6404 * there will not be an ANR.
6405 */
6406TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6407 mWindow->setFocusable(false);
6408 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6409 mWindow->consumeFocusEvent(false);
6410
6411 KeyEvent event;
6412 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6413 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6414
6415 // Define a valid key down event that is stale (too old).
6416 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6417 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006418 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006419
6420 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6421
6422 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006423 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006424 InputEventInjectionSync::WAIT_FOR_RESULT,
6425 INJECT_EVENT_TIMEOUT, policyFlags);
6426 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6427 << "Injection should fail because the event is stale";
6428
6429 ASSERT_TRUE(mDispatcher->waitForIdle());
6430 mFakePolicy->assertNotifyAnrWasNotCalled();
6431 mWindow->assertNoEvents();
6432}
6433
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006434// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006435// Make sure that we don't notify policy twice about the same ANR.
6436TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006437 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006438 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6439 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006440
6441 // Once a focused event arrives, we get an ANR for this application
6442 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6443 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006444 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006445 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6446 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006447 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006448 const std::chrono::duration appTimeout =
6449 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6450 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006451
Vishnu Naire4df8752022-09-08 09:17:55 -07006452 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006453 // ANR should not be raised again. It is up to policy to do that if it desires.
6454 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006455
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006456 // If we now get a focused window, the ANR should stop, but the policy handles that via
6457 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006458 ASSERT_TRUE(mDispatcher->waitForIdle());
6459}
6460
6461// We have a focused application, but no focused window
6462TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006463 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006464 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6465 mWindow->consumeFocusEvent(false);
6466
6467 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006468 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006469 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006470 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6471 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006472
Vishnu Naire4df8752022-09-08 09:17:55 -07006473 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6474 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006475
6476 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006477 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006478 ASSERT_TRUE(mDispatcher->waitForIdle());
6479 mWindow->assertNoEvents();
6480}
6481
6482/**
6483 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6484 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6485 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6486 * the ANR mechanism should still work.
6487 *
6488 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6489 * DOWN event, while not responding on the second one.
6490 */
6491TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6492 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6493 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6494 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6495 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6496 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006497 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006498
6499 // Now send ACTION_UP, with identical timestamp
6500 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6501 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6502 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6503 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006504 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006505
6506 // We have now sent down and up. Let's consume first event and then ANR on the second.
6507 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6508 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006509 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006510}
6511
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006512// A spy window can receive an ANR
6513TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6514 sp<FakeWindowHandle> spy = addSpyWindow();
6515
6516 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6517 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6518 WINDOW_LOCATION));
6519 mWindow->consumeMotionDown();
6520
6521 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6522 ASSERT_TRUE(sequenceNum);
6523 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006524 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006525
6526 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006527 spy->consumeMotionEvent(
6528 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006529 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006530 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006531}
6532
6533// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006534// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006535TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6536 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006537
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006538 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6539 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006540 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006541 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006542
6543 // Stuck on the ACTION_UP
6544 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006545 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006546
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006547 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006548 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006549 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6550 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006551
6552 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6553 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006554 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006555 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006556 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006557}
6558
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006559// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006560// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006561TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6562 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006563
6564 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006565 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6566 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006567
6568 mWindow->consumeMotionDown();
6569 // Stuck on the ACTION_UP
6570 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006571 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006572
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006573 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006574 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006575 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6576 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006577
6578 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6579 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006580 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006581 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006582 spy->assertNoEvents();
6583}
6584
6585TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6586 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6587
6588 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6589
6590 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6591 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6592 WINDOW_LOCATION));
6593
6594 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6595 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6596 ASSERT_TRUE(consumeSeq);
6597
Prabir Pradhanedd96402022-02-15 01:46:16 -08006598 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006599
6600 monitor.finishEvent(*consumeSeq);
6601 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6602
6603 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006604 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006605}
6606
6607// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6608// process events, you don't get an anr. When the window later becomes unresponsive again, you
6609// get an ANR again.
6610// 1. tap -> block on ACTION_UP -> receive ANR
6611// 2. consume all pending events (= queue becomes healthy again)
6612// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6613TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6614 tapOnWindow();
6615
6616 mWindow->consumeMotionDown();
6617 // Block on ACTION_UP
6618 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006619 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006620 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6621 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006622 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006623 mWindow->assertNoEvents();
6624
6625 tapOnWindow();
6626 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006627 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006628 mWindow->consumeMotionUp();
6629
6630 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006631 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006632 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006633 mWindow->assertNoEvents();
6634}
6635
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006636// If a connection remains unresponsive for a while, make sure policy is only notified once about
6637// it.
6638TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006639 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006640 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6641 WINDOW_LOCATION));
6642
6643 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006644 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006645 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006646 // 'notifyConnectionUnresponsive' should only be called once per connection
6647 mFakePolicy->assertNotifyAnrWasNotCalled();
6648 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006649 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006650 mWindow->consumeMotionEvent(
6651 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006652 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006653 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006654 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006655 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006656}
6657
6658/**
6659 * If a window is processing a motion event, and then a key event comes in, the key event should
6660 * not to to the focused window until the motion is processed.
6661 *
6662 * Warning!!!
6663 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6664 * and the injection timeout that we specify when injecting the key.
6665 * We must have the injection timeout (10ms) be smaller than
6666 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6667 *
6668 * If that value changes, this test should also change.
6669 */
6670TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
6671 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6672 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6673
6674 tapOnWindow();
6675 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6676 ASSERT_TRUE(downSequenceNum);
6677 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6678 ASSERT_TRUE(upSequenceNum);
6679 // Don't finish the events yet, and send a key
6680 // Injection will "succeed" because we will eventually give up and send the key to the focused
6681 // window even if motions are still being processed. But because the injection timeout is short,
6682 // we will receive INJECTION_TIMED_OUT as the result.
6683
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006684 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006685 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006686 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6687 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006688 // Key will not be sent to the window, yet, because the window is still processing events
6689 // and the key remains pending, waiting for the touch events to be processed
6690 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6691 ASSERT_FALSE(keySequenceNum);
6692
6693 std::this_thread::sleep_for(500ms);
6694 // if we wait long enough though, dispatcher will give up, and still send the key
6695 // to the focused window, even though we have not yet finished the motion event
6696 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6697 mWindow->finishEvent(*downSequenceNum);
6698 mWindow->finishEvent(*upSequenceNum);
6699}
6700
6701/**
6702 * If a window is processing a motion event, and then a key event comes in, the key event should
6703 * not go to the focused window until the motion is processed.
6704 * If then a new motion comes in, then the pending key event should be going to the currently
6705 * focused window right away.
6706 */
6707TEST_F(InputDispatcherSingleWindowAnr,
6708 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
6709 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6710 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6711
6712 tapOnWindow();
6713 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6714 ASSERT_TRUE(downSequenceNum);
6715 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6716 ASSERT_TRUE(upSequenceNum);
6717 // Don't finish the events yet, and send a key
6718 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006719 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006720 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6721 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006722 // At this point, key is still pending, and should not be sent to the application yet.
6723 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6724 ASSERT_FALSE(keySequenceNum);
6725
6726 // Now tap down again. It should cause the pending key to go to the focused window right away.
6727 tapOnWindow();
6728 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
6729 // the other events yet. We can finish events in any order.
6730 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
6731 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
6732 mWindow->consumeMotionDown();
6733 mWindow->consumeMotionUp();
6734 mWindow->assertNoEvents();
6735}
6736
6737class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
6738 virtual void SetUp() override {
6739 InputDispatcherTest::SetUp();
6740
Chris Yea209fde2020-07-22 13:54:51 -07006741 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006742 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006743 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
6744 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006745 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006746 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006747 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006748
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006749 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
6750 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006751 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006752 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006753
6754 // Set focused application.
6755 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07006756 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006757
6758 // Expect one focus window exist in display.
6759 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006760 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006761 mFocusedWindow->consumeFocusEvent(true);
6762 }
6763
6764 virtual void TearDown() override {
6765 InputDispatcherTest::TearDown();
6766
6767 mUnfocusedWindow.clear();
6768 mFocusedWindow.clear();
6769 }
6770
6771protected:
Chris Yea209fde2020-07-22 13:54:51 -07006772 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006773 sp<FakeWindowHandle> mUnfocusedWindow;
6774 sp<FakeWindowHandle> mFocusedWindow;
6775 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
6776 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
6777 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
6778
6779 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
6780
6781 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
6782
6783private:
6784 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006785 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006786 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6787 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006788 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006789 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6790 location));
6791 }
6792};
6793
6794// If we have 2 windows that are both unresponsive, the one with the shortest timeout
6795// should be ANR'd first.
6796TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006797 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006798 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6799 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006800 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006801 mFocusedWindow->consumeMotionDown();
6802 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006803 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006804 // We consumed all events, so no ANR
6805 ASSERT_TRUE(mDispatcher->waitForIdle());
6806 mFakePolicy->assertNotifyAnrWasNotCalled();
6807
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006808 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006809 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6810 FOCUSED_WINDOW_LOCATION));
6811 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
6812 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006813
6814 const std::chrono::duration timeout =
6815 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006816 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006817 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
6818 // sequence to make it consistent
6819 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006820 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006821 mFocusedWindow->consumeMotionDown();
6822 // This cancel is generated because the connection was unresponsive
6823 mFocusedWindow->consumeMotionCancel();
6824 mFocusedWindow->assertNoEvents();
6825 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006826 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006827 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6828 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006829 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006830}
6831
6832// If we have 2 windows with identical timeouts that are both unresponsive,
6833// it doesn't matter which order they should have ANR.
6834// But we should receive ANR for both.
6835TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
6836 // Set the timeout for unfocused window to match the focused window
6837 mUnfocusedWindow->setDispatchingTimeout(10ms);
6838 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6839
6840 tapOnFocusedWindow();
6841 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08006842 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
6843 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
6844 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006845
6846 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006847 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
6848 mFocusedWindow->getToken() == anrConnectionToken2);
6849 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
6850 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006851
6852 ASSERT_TRUE(mDispatcher->waitForIdle());
6853 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006854
6855 mFocusedWindow->consumeMotionDown();
6856 mFocusedWindow->consumeMotionUp();
6857 mUnfocusedWindow->consumeMotionOutside();
6858
Prabir Pradhanedd96402022-02-15 01:46:16 -08006859 sp<IBinder> responsiveToken1, responsiveToken2;
6860 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
6861 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006862
6863 // Both applications should be marked as responsive, in any order
6864 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
6865 mFocusedWindow->getToken() == responsiveToken2);
6866 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
6867 mUnfocusedWindow->getToken() == responsiveToken2);
6868 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006869}
6870
6871// If a window is already not responding, the second tap on the same window should be ignored.
6872// We should also log an error to account for the dropped event (not tested here).
6873// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
6874TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
6875 tapOnFocusedWindow();
6876 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006877 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006878 // Receive the events, but don't respond
6879 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
6880 ASSERT_TRUE(downEventSequenceNum);
6881 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
6882 ASSERT_TRUE(upEventSequenceNum);
6883 const std::chrono::duration timeout =
6884 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006885 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006886
6887 // Tap once again
6888 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006889 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006890 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6891 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006892 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006893 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6894 FOCUSED_WINDOW_LOCATION));
6895 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
6896 // valid touch target
6897 mUnfocusedWindow->assertNoEvents();
6898
6899 // Consume the first tap
6900 mFocusedWindow->finishEvent(*downEventSequenceNum);
6901 mFocusedWindow->finishEvent(*upEventSequenceNum);
6902 ASSERT_TRUE(mDispatcher->waitForIdle());
6903 // The second tap did not go to the focused window
6904 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006905 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08006906 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6907 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006908 mFakePolicy->assertNotifyAnrWasNotCalled();
6909}
6910
6911// If you tap outside of all windows, there will not be ANR
6912TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006913 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006914 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6915 LOCATION_OUTSIDE_ALL_WINDOWS));
6916 ASSERT_TRUE(mDispatcher->waitForIdle());
6917 mFakePolicy->assertNotifyAnrWasNotCalled();
6918}
6919
6920// Since the focused window is paused, tapping on it should not produce any events
6921TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
6922 mFocusedWindow->setPaused(true);
6923 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6924
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006925 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006926 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6927 FOCUSED_WINDOW_LOCATION));
6928
6929 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
6930 ASSERT_TRUE(mDispatcher->waitForIdle());
6931 // Should not ANR because the window is paused, and touches shouldn't go to it
6932 mFakePolicy->assertNotifyAnrWasNotCalled();
6933
6934 mFocusedWindow->assertNoEvents();
6935 mUnfocusedWindow->assertNoEvents();
6936}
6937
6938/**
6939 * If a window is processing a motion event, and then a key event comes in, the key event should
6940 * not to to the focused window until the motion is processed.
6941 * If a different window becomes focused at this time, the key should go to that window instead.
6942 *
6943 * Warning!!!
6944 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6945 * and the injection timeout that we specify when injecting the key.
6946 * We must have the injection timeout (10ms) be smaller than
6947 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6948 *
6949 * If that value changes, this test should also change.
6950 */
6951TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
6952 // Set a long ANR timeout to prevent it from triggering
6953 mFocusedWindow->setDispatchingTimeout(2s);
6954 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
6955
6956 tapOnUnfocusedWindow();
6957 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
6958 ASSERT_TRUE(downSequenceNum);
6959 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
6960 ASSERT_TRUE(upSequenceNum);
6961 // Don't finish the events yet, and send a key
6962 // Injection will succeed because we will eventually give up and send the key to the focused
6963 // window even if motions are still being processed.
6964
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006965 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006966 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6967 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006968 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006969 // Key will not be sent to the window, yet, because the window is still processing events
6970 // and the key remains pending, waiting for the touch events to be processed
6971 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
6972 ASSERT_FALSE(keySequenceNum);
6973
6974 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07006975 mFocusedWindow->setFocusable(false);
6976 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006977 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006978 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006979
6980 // Focus events should precede the key events
6981 mUnfocusedWindow->consumeFocusEvent(true);
6982 mFocusedWindow->consumeFocusEvent(false);
6983
6984 // Finish the tap events, which should unblock dispatcher
6985 mUnfocusedWindow->finishEvent(*downSequenceNum);
6986 mUnfocusedWindow->finishEvent(*upSequenceNum);
6987
6988 // Now that all queues are cleared and no backlog in the connections, the key event
6989 // can finally go to the newly focused "mUnfocusedWindow".
6990 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6991 mFocusedWindow->assertNoEvents();
6992 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006993 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006994}
6995
6996// When the touch stream is split across 2 windows, and one of them does not respond,
6997// then ANR should be raised and the touch should be canceled for the unresponsive window.
6998// The other window should not be affected by that.
6999TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7000 // Touch Window 1
7001 NotifyMotionArgs motionArgs =
7002 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7003 ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION});
7004 mDispatcher->notifyMotion(&motionArgs);
7005 mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007006 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007007
7008 // Touch Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007009 motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7010 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007011 mDispatcher->notifyMotion(&motionArgs);
7012
7013 const std::chrono::duration timeout =
7014 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007015 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007016
7017 mUnfocusedWindow->consumeMotionDown();
7018 mFocusedWindow->consumeMotionDown();
7019 // Focused window may or may not receive ACTION_MOVE
7020 // But it should definitely receive ACTION_CANCEL due to the ANR
7021 InputEvent* event;
7022 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7023 ASSERT_TRUE(moveOrCancelSequenceNum);
7024 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7025 ASSERT_NE(nullptr, event);
7026 ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION);
7027 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7028 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7029 mFocusedWindow->consumeMotionCancel();
7030 } else {
7031 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7032 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007033 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007034 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7035 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007036
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007037 mUnfocusedWindow->assertNoEvents();
7038 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007039 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007040}
7041
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007042/**
7043 * If we have no focused window, and a key comes in, we start the ANR timer.
7044 * The focused application should add a focused window before the timer runs out to prevent ANR.
7045 *
7046 * If the user touches another application during this time, the key should be dropped.
7047 * Next, if a new focused window comes in, without toggling the focused application,
7048 * then no ANR should occur.
7049 *
7050 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7051 * but in some cases the policy may not update the focused application.
7052 */
7053TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7054 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7055 std::make_shared<FakeApplicationHandle>();
7056 focusedApplication->setDispatchingTimeout(60ms);
7057 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7058 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7059 mFocusedWindow->setFocusable(false);
7060
7061 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7062 mFocusedWindow->consumeFocusEvent(false);
7063
7064 // Send a key. The ANR timer should start because there is no focused window.
7065 // 'focusedApplication' will get blamed if this timer completes.
7066 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007067 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007068 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7069 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7070 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007071 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007072
7073 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7074 // then the injected touches won't cause the focused event to get dropped.
7075 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7076 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7077 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7078 // For this test, it means that the key would get delivered to the window once it becomes
7079 // focused.
7080 std::this_thread::sleep_for(10ms);
7081
7082 // Touch unfocused window. This should force the pending key to get dropped.
7083 NotifyMotionArgs motionArgs =
7084 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7085 ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION});
7086 mDispatcher->notifyMotion(&motionArgs);
7087
7088 // We do not consume the motion right away, because that would require dispatcher to first
7089 // process (== drop) the key event, and by that time, ANR will be raised.
7090 // Set the focused window first.
7091 mFocusedWindow->setFocusable(true);
7092 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7093 setFocusedWindow(mFocusedWindow);
7094 mFocusedWindow->consumeFocusEvent(true);
7095 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7096 // to another application. This could be a bug / behaviour in the policy.
7097
7098 mUnfocusedWindow->consumeMotionDown();
7099
7100 ASSERT_TRUE(mDispatcher->waitForIdle());
7101 // Should not ANR because we actually have a focused window. It was just added too slowly.
7102 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7103}
7104
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007105// These tests ensure we cannot send touch events to a window that's positioned behind a window
7106// that has feature NO_INPUT_CHANNEL.
7107// Layout:
7108// Top (closest to user)
7109// mNoInputWindow (above all windows)
7110// mBottomWindow
7111// Bottom (furthest from user)
7112class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7113 virtual void SetUp() override {
7114 InputDispatcherTest::SetUp();
7115
7116 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007117 mNoInputWindow =
7118 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7119 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007120 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007121 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007122 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7123 // It's perfectly valid for this window to not have an associated input channel
7124
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007125 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7126 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007127 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7128
7129 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7130 }
7131
7132protected:
7133 std::shared_ptr<FakeApplicationHandle> mApplication;
7134 sp<FakeWindowHandle> mNoInputWindow;
7135 sp<FakeWindowHandle> mBottomWindow;
7136};
7137
7138TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7139 PointF touchedPoint = {10, 10};
7140
7141 NotifyMotionArgs motionArgs =
7142 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7143 ADISPLAY_ID_DEFAULT, {touchedPoint});
7144 mDispatcher->notifyMotion(&motionArgs);
7145
7146 mNoInputWindow->assertNoEvents();
7147 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7148 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7149 // and therefore should prevent mBottomWindow from receiving touches
7150 mBottomWindow->assertNoEvents();
7151}
7152
7153/**
7154 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7155 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7156 */
7157TEST_F(InputDispatcherMultiWindowOcclusionTests,
7158 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007159 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7160 "Window with input channel and NO_INPUT_CHANNEL",
7161 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007162
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007163 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007164 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7165 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7166
7167 PointF touchedPoint = {10, 10};
7168
7169 NotifyMotionArgs motionArgs =
7170 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7171 ADISPLAY_ID_DEFAULT, {touchedPoint});
7172 mDispatcher->notifyMotion(&motionArgs);
7173
7174 mNoInputWindow->assertNoEvents();
7175 mBottomWindow->assertNoEvents();
7176}
7177
Vishnu Nair958da932020-08-21 17:12:37 -07007178class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7179protected:
7180 std::shared_ptr<FakeApplicationHandle> mApp;
7181 sp<FakeWindowHandle> mWindow;
7182 sp<FakeWindowHandle> mMirror;
7183
7184 virtual void SetUp() override {
7185 InputDispatcherTest::SetUp();
7186 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007187 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7188 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7189 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007190 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7191 mWindow->setFocusable(true);
7192 mMirror->setFocusable(true);
7193 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7194 }
7195};
7196
7197TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7198 // Request focus on a mirrored window
7199 setFocusedWindow(mMirror);
7200
7201 // window gets focused
7202 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007203 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7204 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007205 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7206}
7207
7208// A focused & mirrored window remains focused only if the window and its mirror are both
7209// focusable.
7210TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7211 setFocusedWindow(mMirror);
7212
7213 // window gets focused
7214 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007215 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7216 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007217 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7219 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007220 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7221
7222 mMirror->setFocusable(false);
7223 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7224
7225 // window loses focus since one of the windows associated with the token in not focusable
7226 mWindow->consumeFocusEvent(false);
7227
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007228 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7229 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007230 mWindow->assertNoEvents();
7231}
7232
7233// A focused & mirrored window remains focused until the window and its mirror both become
7234// invisible.
7235TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7236 setFocusedWindow(mMirror);
7237
7238 // window gets focused
7239 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007240 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7241 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007242 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007243 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7244 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007245 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7246
7247 mMirror->setVisible(false);
7248 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7249
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007250 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7251 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007252 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007253 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7254 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007255 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7256
7257 mWindow->setVisible(false);
7258 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7259
7260 // window loses focus only after all windows associated with the token become invisible.
7261 mWindow->consumeFocusEvent(false);
7262
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007263 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7264 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007265 mWindow->assertNoEvents();
7266}
7267
7268// A focused & mirrored window remains focused until both windows are removed.
7269TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7270 setFocusedWindow(mMirror);
7271
7272 // window gets focused
7273 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007274 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7275 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007276 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007277 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7278 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007279 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7280
7281 // single window is removed but the window token remains focused
7282 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7283
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007284 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7285 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007286 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007287 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7288 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007289 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7290
7291 // Both windows are removed
7292 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7293 mWindow->consumeFocusEvent(false);
7294
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007295 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7296 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007297 mWindow->assertNoEvents();
7298}
7299
7300// Focus request can be pending until one window becomes visible.
7301TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7302 // Request focus on an invisible mirror.
7303 mWindow->setVisible(false);
7304 mMirror->setVisible(false);
7305 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7306 setFocusedWindow(mMirror);
7307
7308 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007309 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007310 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7311 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007312
7313 mMirror->setVisible(true);
7314 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7315
7316 // window gets focused
7317 mWindow->consumeFocusEvent(true);
7318 // window gets the pending key event
7319 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7320}
Prabir Pradhan99987712020-11-10 18:43:05 -08007321
7322class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7323protected:
7324 std::shared_ptr<FakeApplicationHandle> mApp;
7325 sp<FakeWindowHandle> mWindow;
7326 sp<FakeWindowHandle> mSecondWindow;
7327
7328 void SetUp() override {
7329 InputDispatcherTest::SetUp();
7330 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007331 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007332 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007333 mSecondWindow =
7334 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007335 mSecondWindow->setFocusable(true);
7336
7337 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7338 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7339
7340 setFocusedWindow(mWindow);
7341 mWindow->consumeFocusEvent(true);
7342 }
7343
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007344 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
7345 const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007346 mDispatcher->notifyPointerCaptureChanged(&args);
7347 }
7348
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007349 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7350 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007351 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007352 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7353 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007354 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007355 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007356 }
7357};
7358
7359TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7360 // Ensure that capture cannot be obtained for unfocused windows.
7361 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7362 mFakePolicy->assertSetPointerCaptureNotCalled();
7363 mSecondWindow->assertNoEvents();
7364
7365 // Ensure that capture can be enabled from the focus window.
7366 requestAndVerifyPointerCapture(mWindow, true);
7367
7368 // Ensure that capture cannot be disabled from a window that does not have capture.
7369 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7370 mFakePolicy->assertSetPointerCaptureNotCalled();
7371
7372 // Ensure that capture can be disabled from the window with capture.
7373 requestAndVerifyPointerCapture(mWindow, false);
7374}
7375
7376TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007377 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007378
7379 setFocusedWindow(mSecondWindow);
7380
7381 // Ensure that the capture disabled event was sent first.
7382 mWindow->consumeCaptureEvent(false);
7383 mWindow->consumeFocusEvent(false);
7384 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007385 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007386
7387 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007388 notifyPointerCaptureChanged({});
7389 notifyPointerCaptureChanged(request);
7390 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007391 mWindow->assertNoEvents();
7392 mSecondWindow->assertNoEvents();
7393 mFakePolicy->assertSetPointerCaptureNotCalled();
7394}
7395
7396TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007397 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007398
7399 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007400 notifyPointerCaptureChanged({});
7401 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007402
7403 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007404 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007405 mWindow->consumeCaptureEvent(false);
7406 mWindow->assertNoEvents();
7407}
7408
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007409TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7410 requestAndVerifyPointerCapture(mWindow, true);
7411
7412 // The first window loses focus.
7413 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007414 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007415 mWindow->consumeCaptureEvent(false);
7416
7417 // Request Pointer Capture from the second window before the notification from InputReader
7418 // arrives.
7419 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007420 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007421
7422 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007423 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007424
7425 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007426 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007427
7428 mSecondWindow->consumeFocusEvent(true);
7429 mSecondWindow->consumeCaptureEvent(true);
7430}
7431
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007432TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7433 // App repeatedly enables and disables capture.
7434 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7435 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7436 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7437 mFakePolicy->assertSetPointerCaptureCalled(false);
7438 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7439 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7440
7441 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7442 // first request is now stale, this should do nothing.
7443 notifyPointerCaptureChanged(firstRequest);
7444 mWindow->assertNoEvents();
7445
7446 // InputReader notifies that the second request was enabled.
7447 notifyPointerCaptureChanged(secondRequest);
7448 mWindow->consumeCaptureEvent(true);
7449}
7450
Prabir Pradhan7092e262022-05-03 16:51:09 +00007451TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7452 requestAndVerifyPointerCapture(mWindow, true);
7453
7454 // App toggles pointer capture off and on.
7455 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7456 mFakePolicy->assertSetPointerCaptureCalled(false);
7457
7458 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7459 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7460
7461 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7462 // preceding "disable" request.
7463 notifyPointerCaptureChanged(enableRequest);
7464
7465 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7466 // any notifications.
7467 mWindow->assertNoEvents();
7468}
7469
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007470class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7471protected:
7472 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007473
7474 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7475 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7476
7477 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7478 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7479
7480 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7481 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7482 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7483 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7484 MAXIMUM_OBSCURING_OPACITY);
7485
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007486 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007487 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007488 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007489
7490 sp<FakeWindowHandle> mTouchWindow;
7491
7492 virtual void SetUp() override {
7493 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007494 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007495 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7496 }
7497
7498 virtual void TearDown() override {
7499 InputDispatcherTest::TearDown();
7500 mTouchWindow.clear();
7501 }
7502
chaviw3277faf2021-05-19 16:45:23 -05007503 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7504 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007505 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007506 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007507 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007508 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007509 return window;
7510 }
7511
7512 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7513 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7514 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007515 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007516 // Generate an arbitrary PID based on the UID
7517 window->setOwnerInfo(1777 + (uid % 10000), uid);
7518 return window;
7519 }
7520
7521 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
7522 NotifyMotionArgs args =
7523 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7524 ADISPLAY_ID_DEFAULT, points);
7525 mDispatcher->notifyMotion(&args);
7526 }
7527};
7528
7529TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007530 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007531 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007532 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007533
7534 touch();
7535
7536 mTouchWindow->assertNoEvents();
7537}
7538
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007539TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007540 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7541 const sp<FakeWindowHandle>& w =
7542 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7543 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7544
7545 touch();
7546
7547 mTouchWindow->assertNoEvents();
7548}
7549
7550TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007551 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7552 const sp<FakeWindowHandle>& w =
7553 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7554 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7555
7556 touch();
7557
7558 w->assertNoEvents();
7559}
7560
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007561TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007562 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7563 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007564
7565 touch();
7566
7567 mTouchWindow->consumeAnyMotionDown();
7568}
7569
7570TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007571 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007572 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007573 w->setFrame(Rect(0, 0, 50, 50));
7574 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007575
7576 touch({PointF{100, 100}});
7577
7578 mTouchWindow->consumeAnyMotionDown();
7579}
7580
7581TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007582 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007583 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007584 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7585
7586 touch();
7587
7588 mTouchWindow->consumeAnyMotionDown();
7589}
7590
7591TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7592 const sp<FakeWindowHandle>& w =
7593 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7594 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007595
7596 touch();
7597
7598 mTouchWindow->consumeAnyMotionDown();
7599}
7600
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007601TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7602 const sp<FakeWindowHandle>& w =
7603 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7604 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7605
7606 touch();
7607
7608 w->assertNoEvents();
7609}
7610
7611/**
7612 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7613 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7614 * window, the occluding window will still receive ACTION_OUTSIDE event.
7615 */
7616TEST_F(InputDispatcherUntrustedTouchesTest,
7617 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7618 const sp<FakeWindowHandle>& w =
7619 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007620 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007621 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7622
7623 touch();
7624
7625 w->consumeMotionOutside();
7626}
7627
7628TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
7629 const sp<FakeWindowHandle>& w =
7630 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007631 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007632 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7633
7634 touch();
7635
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007636 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007637}
7638
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007639TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007640 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007641 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7642 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007643 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7644
7645 touch();
7646
7647 mTouchWindow->consumeAnyMotionDown();
7648}
7649
7650TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
7651 const sp<FakeWindowHandle>& w =
7652 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7653 MAXIMUM_OBSCURING_OPACITY);
7654 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007655
7656 touch();
7657
7658 mTouchWindow->consumeAnyMotionDown();
7659}
7660
7661TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007662 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007663 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7664 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007665 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7666
7667 touch();
7668
7669 mTouchWindow->assertNoEvents();
7670}
7671
7672TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
7673 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
7674 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007675 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7676 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007677 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007678 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7679 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007680 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7681
7682 touch();
7683
7684 mTouchWindow->assertNoEvents();
7685}
7686
7687TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
7688 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
7689 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007690 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7691 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007692 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007693 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7694 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007695 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7696
7697 touch();
7698
7699 mTouchWindow->consumeAnyMotionDown();
7700}
7701
7702TEST_F(InputDispatcherUntrustedTouchesTest,
7703 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
7704 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007705 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7706 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007707 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007708 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7709 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007710 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7711
7712 touch();
7713
7714 mTouchWindow->consumeAnyMotionDown();
7715}
7716
7717TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
7718 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007719 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7720 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007721 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007722 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7723 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007724 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007725
7726 touch();
7727
7728 mTouchWindow->assertNoEvents();
7729}
7730
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007731TEST_F(InputDispatcherUntrustedTouchesTest,
7732 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
7733 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007734 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7735 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007736 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007737 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7738 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007739 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7740
7741 touch();
7742
7743 mTouchWindow->assertNoEvents();
7744}
7745
7746TEST_F(InputDispatcherUntrustedTouchesTest,
7747 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
7748 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007749 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7750 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007751 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007752 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7753 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007754 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7755
7756 touch();
7757
7758 mTouchWindow->consumeAnyMotionDown();
7759}
7760
7761TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
7762 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007763 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7764 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007765 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7766
7767 touch();
7768
7769 mTouchWindow->consumeAnyMotionDown();
7770}
7771
7772TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
7773 const sp<FakeWindowHandle>& w =
7774 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
7775 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7776
7777 touch();
7778
7779 mTouchWindow->consumeAnyMotionDown();
7780}
7781
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007782TEST_F(InputDispatcherUntrustedTouchesTest,
7783 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
7784 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7785 const sp<FakeWindowHandle>& w =
7786 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
7787 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7788
7789 touch();
7790
7791 mTouchWindow->assertNoEvents();
7792}
7793
7794TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
7795 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7796 const sp<FakeWindowHandle>& w =
7797 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
7798 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7799
7800 touch();
7801
7802 mTouchWindow->consumeAnyMotionDown();
7803}
7804
7805TEST_F(InputDispatcherUntrustedTouchesTest,
7806 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
7807 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
7808 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007809 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7810 OPACITY_ABOVE_THRESHOLD);
7811 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7812
7813 touch();
7814
7815 mTouchWindow->consumeAnyMotionDown();
7816}
7817
7818TEST_F(InputDispatcherUntrustedTouchesTest,
7819 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
7820 const sp<FakeWindowHandle>& w1 =
7821 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7822 OPACITY_BELOW_THRESHOLD);
7823 const sp<FakeWindowHandle>& w2 =
7824 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7825 OPACITY_BELOW_THRESHOLD);
7826 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7827
7828 touch();
7829
7830 mTouchWindow->assertNoEvents();
7831}
7832
7833/**
7834 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
7835 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
7836 * (which alone would result in allowing touches) does not affect the blocking behavior.
7837 */
7838TEST_F(InputDispatcherUntrustedTouchesTest,
7839 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
7840 const sp<FakeWindowHandle>& wB =
7841 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7842 OPACITY_BELOW_THRESHOLD);
7843 const sp<FakeWindowHandle>& wC =
7844 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7845 OPACITY_BELOW_THRESHOLD);
7846 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7847
7848 touch();
7849
7850 mTouchWindow->assertNoEvents();
7851}
7852
7853/**
7854 * This test is testing that a window from a different UID but with same application token doesn't
7855 * block the touch. Apps can share the application token for close UI collaboration for example.
7856 */
7857TEST_F(InputDispatcherUntrustedTouchesTest,
7858 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
7859 const sp<FakeWindowHandle>& w =
7860 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7861 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007862 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7863
7864 touch();
7865
7866 mTouchWindow->consumeAnyMotionDown();
7867}
7868
arthurhungb89ccb02020-12-30 16:19:01 +08007869class InputDispatcherDragTests : public InputDispatcherTest {
7870protected:
7871 std::shared_ptr<FakeApplicationHandle> mApp;
7872 sp<FakeWindowHandle> mWindow;
7873 sp<FakeWindowHandle> mSecondWindow;
7874 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007875 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007876 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
7877 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08007878
7879 void SetUp() override {
7880 InputDispatcherTest::SetUp();
7881 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007882 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007883 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007884
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007885 mSecondWindow =
7886 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007887 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007888
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007889 mSpyWindow =
7890 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007891 mSpyWindow->setSpy(true);
7892 mSpyWindow->setTrustedOverlay(true);
7893 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
7894
arthurhungb89ccb02020-12-30 16:19:01 +08007895 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007896 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007897 }
7898
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007899 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
7900 switch (fromSource) {
7901 case AINPUT_SOURCE_TOUCHSCREEN:
7902 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7903 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7904 ADISPLAY_ID_DEFAULT, {50, 50}))
7905 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7906 break;
7907 case AINPUT_SOURCE_STYLUS:
7908 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7909 injectMotionEvent(
7910 mDispatcher,
7911 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
7912 AINPUT_SOURCE_STYLUS)
7913 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007914 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007915 .x(50)
7916 .y(50))
7917 .build()));
7918 break;
7919 case AINPUT_SOURCE_MOUSE:
7920 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7921 injectMotionEvent(
7922 mDispatcher,
7923 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
7924 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
7925 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007926 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007927 .x(50)
7928 .y(50))
7929 .build()));
7930 break;
7931 default:
7932 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
7933 }
arthurhungb89ccb02020-12-30 16:19:01 +08007934
7935 // Window should receive motion event.
7936 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007937 // Spy window should also receive motion event
7938 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00007939 }
7940
7941 // Start performing drag, we will create a drag window and transfer touch to it.
7942 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
7943 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007944 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00007945 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007946 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00007947 }
arthurhungb89ccb02020-12-30 16:19:01 +08007948
7949 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007950 mDragWindow =
7951 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007952 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08007953 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007954 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08007955
7956 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00007957 bool transferred =
7958 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00007959 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00007960 if (transferred) {
7961 mWindow->consumeMotionCancel();
7962 mDragWindow->consumeMotionDown();
7963 }
7964 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08007965 }
7966};
7967
7968TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007969 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08007970
7971 // Move on window.
7972 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7973 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7974 ADISPLAY_ID_DEFAULT, {50, 50}))
7975 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7976 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7977 mWindow->consumeDragEvent(false, 50, 50);
7978 mSecondWindow->assertNoEvents();
7979
7980 // Move to another window.
7981 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7982 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7983 ADISPLAY_ID_DEFAULT, {150, 50}))
7984 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7985 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7986 mWindow->consumeDragEvent(true, 150, 50);
7987 mSecondWindow->consumeDragEvent(false, 50, 50);
7988
7989 // Move back to original window.
7990 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7991 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7992 ADISPLAY_ID_DEFAULT, {50, 50}))
7993 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7994 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
7995 mWindow->consumeDragEvent(false, 50, 50);
7996 mSecondWindow->consumeDragEvent(true, -50, 50);
7997
7998 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7999 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8000 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8001 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8002 mWindow->assertNoEvents();
8003 mSecondWindow->assertNoEvents();
8004}
8005
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008006TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008007 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008008
8009 // No cancel event after drag start
8010 mSpyWindow->assertNoEvents();
8011
8012 const MotionEvent secondFingerDownEvent =
8013 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8014 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008015 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8016 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008017 .build();
8018 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8019 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8020 InputEventInjectionSync::WAIT_FOR_RESULT))
8021 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8022
8023 // Receives cancel for first pointer after next pointer down
8024 mSpyWindow->consumeMotionCancel();
8025 mSpyWindow->consumeMotionDown();
8026
8027 mSpyWindow->assertNoEvents();
8028}
8029
arthurhungf452d0b2021-01-06 00:19:52 +08008030TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008031 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008032
8033 // Move on window.
8034 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8035 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8036 ADISPLAY_ID_DEFAULT, {50, 50}))
8037 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8038 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8039 mWindow->consumeDragEvent(false, 50, 50);
8040 mSecondWindow->assertNoEvents();
8041
8042 // Move to another window.
8043 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8044 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8045 ADISPLAY_ID_DEFAULT, {150, 50}))
8046 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8047 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8048 mWindow->consumeDragEvent(true, 150, 50);
8049 mSecondWindow->consumeDragEvent(false, 50, 50);
8050
8051 // drop to another window.
8052 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8053 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8054 {150, 50}))
8055 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8056 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8057 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8058 mWindow->assertNoEvents();
8059 mSecondWindow->assertNoEvents();
8060}
8061
arthurhung6d4bed92021-03-17 11:59:33 +08008062TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008063 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008064
8065 // Move on window and keep button pressed.
8066 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8067 injectMotionEvent(mDispatcher,
8068 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8069 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008070 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008071 .build()))
8072 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8073 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8074 mWindow->consumeDragEvent(false, 50, 50);
8075 mSecondWindow->assertNoEvents();
8076
8077 // Move to another window and release button, expect to drop item.
8078 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8079 injectMotionEvent(mDispatcher,
8080 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8081 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008082 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008083 .build()))
8084 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8085 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8086 mWindow->assertNoEvents();
8087 mSecondWindow->assertNoEvents();
8088 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8089
8090 // nothing to the window.
8091 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8092 injectMotionEvent(mDispatcher,
8093 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8094 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008095 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008096 .build()))
8097 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8098 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8099 mWindow->assertNoEvents();
8100 mSecondWindow->assertNoEvents();
8101}
8102
Arthur Hung54745652022-04-20 07:17:41 +00008103TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008104 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008105
8106 // Set second window invisible.
8107 mSecondWindow->setVisible(false);
8108 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8109
8110 // Move on window.
8111 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8112 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8113 ADISPLAY_ID_DEFAULT, {50, 50}))
8114 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8115 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8116 mWindow->consumeDragEvent(false, 50, 50);
8117 mSecondWindow->assertNoEvents();
8118
8119 // Move to another window.
8120 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8121 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8122 ADISPLAY_ID_DEFAULT, {150, 50}))
8123 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8124 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8125 mWindow->consumeDragEvent(true, 150, 50);
8126 mSecondWindow->assertNoEvents();
8127
8128 // drop to another window.
8129 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8130 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8131 {150, 50}))
8132 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8133 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8134 mFakePolicy->assertDropTargetEquals(nullptr);
8135 mWindow->assertNoEvents();
8136 mSecondWindow->assertNoEvents();
8137}
8138
Arthur Hung54745652022-04-20 07:17:41 +00008139TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008140 // Ensure window could track pointerIds if it didn't support split touch.
8141 mWindow->setPreventSplitting(true);
8142
Arthur Hung54745652022-04-20 07:17:41 +00008143 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8144 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8145 {50, 50}))
8146 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8147 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8148
8149 const MotionEvent secondFingerDownEvent =
8150 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8151 .displayId(ADISPLAY_ID_DEFAULT)
8152 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008153 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8154 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008155 .build();
8156 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8157 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8158 InputEventInjectionSync::WAIT_FOR_RESULT))
8159 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008160 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008161
8162 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008163 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008164}
8165
8166TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8167 // First down on second window.
8168 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8169 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8170 {150, 50}))
8171 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8172
8173 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8174
8175 // Second down on first window.
8176 const MotionEvent secondFingerDownEvent =
8177 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8178 .displayId(ADISPLAY_ID_DEFAULT)
8179 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008180 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8181 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008182 .build();
8183 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8184 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8185 InputEventInjectionSync::WAIT_FOR_RESULT))
8186 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8187 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8188
8189 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008190 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008191
8192 // Move on window.
8193 const MotionEvent secondFingerMoveEvent =
8194 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8195 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008196 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8197 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008198 .build();
8199 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8200 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8201 InputEventInjectionSync::WAIT_FOR_RESULT));
8202 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8203 mWindow->consumeDragEvent(false, 50, 50);
8204 mSecondWindow->consumeMotionMove();
8205
8206 // Release the drag pointer should perform drop.
8207 const MotionEvent secondFingerUpEvent =
8208 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8209 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008210 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8211 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008212 .build();
8213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8214 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8215 InputEventInjectionSync::WAIT_FOR_RESULT));
8216 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8217 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8218 mWindow->assertNoEvents();
8219 mSecondWindow->consumeMotionMove();
8220}
8221
Arthur Hung3915c1f2022-05-31 07:17:17 +00008222TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008223 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008224
8225 // Update window of second display.
8226 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008227 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008228 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8229
8230 // Let second display has a touch state.
8231 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8232 injectMotionEvent(mDispatcher,
8233 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8234 AINPUT_SOURCE_TOUCHSCREEN)
8235 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008236 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008237 .build()));
8238 windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008239 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008240 // Update window again.
8241 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8242
8243 // Move on window.
8244 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8245 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8246 ADISPLAY_ID_DEFAULT, {50, 50}))
8247 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8248 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8249 mWindow->consumeDragEvent(false, 50, 50);
8250 mSecondWindow->assertNoEvents();
8251
8252 // Move to another window.
8253 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8254 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8255 ADISPLAY_ID_DEFAULT, {150, 50}))
8256 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8257 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8258 mWindow->consumeDragEvent(true, 150, 50);
8259 mSecondWindow->consumeDragEvent(false, 50, 50);
8260
8261 // drop to another window.
8262 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8263 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8264 {150, 50}))
8265 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8266 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8267 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8268 mWindow->assertNoEvents();
8269 mSecondWindow->assertNoEvents();
8270}
8271
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008272TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8273 startDrag(true, AINPUT_SOURCE_MOUSE);
8274 // Move on window.
8275 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8276 injectMotionEvent(mDispatcher,
8277 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8278 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8279 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008280 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008281 .x(50)
8282 .y(50))
8283 .build()))
8284 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8285 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8286 mWindow->consumeDragEvent(false, 50, 50);
8287 mSecondWindow->assertNoEvents();
8288
8289 // Move to another window.
8290 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8291 injectMotionEvent(mDispatcher,
8292 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8293 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8294 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008295 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008296 .x(150)
8297 .y(50))
8298 .build()))
8299 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8300 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8301 mWindow->consumeDragEvent(true, 150, 50);
8302 mSecondWindow->consumeDragEvent(false, 50, 50);
8303
8304 // drop to another window.
8305 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8306 injectMotionEvent(mDispatcher,
8307 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8308 .buttonState(0)
8309 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008310 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008311 .x(150)
8312 .y(50))
8313 .build()))
8314 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8315 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8316 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8317 mWindow->assertNoEvents();
8318 mSecondWindow->assertNoEvents();
8319}
8320
Vishnu Nair062a8672021-09-03 16:07:44 -07008321class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8322
8323TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8324 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008325 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8326 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008327 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008328 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8329 window->setFocusable(true);
8330 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8331 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008332 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008333
8334 // With the flag set, window should not get any input
8335 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
8336 mDispatcher->notifyKey(&keyArgs);
8337 window->assertNoEvents();
8338
8339 NotifyMotionArgs motionArgs =
8340 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8341 ADISPLAY_ID_DEFAULT);
8342 mDispatcher->notifyMotion(&motionArgs);
8343 window->assertNoEvents();
8344
8345 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008346 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008347 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8348
8349 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
8350 mDispatcher->notifyKey(&keyArgs);
8351 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8352
8353 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8354 ADISPLAY_ID_DEFAULT);
8355 mDispatcher->notifyMotion(&motionArgs);
8356 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8357 window->assertNoEvents();
8358}
8359
8360TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8361 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8362 std::make_shared<FakeApplicationHandle>();
8363 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008364 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8365 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008366 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8367 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008368 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008369 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008370 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8371 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008372 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008373 window->setOwnerInfo(222, 222);
8374 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8375 window->setFocusable(true);
8376 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8377 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008378 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008379
8380 // With the flag set, window should not get any input
8381 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
8382 mDispatcher->notifyKey(&keyArgs);
8383 window->assertNoEvents();
8384
8385 NotifyMotionArgs motionArgs =
8386 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8387 ADISPLAY_ID_DEFAULT);
8388 mDispatcher->notifyMotion(&motionArgs);
8389 window->assertNoEvents();
8390
8391 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008392 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008393 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8394
8395 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
8396 mDispatcher->notifyKey(&keyArgs);
8397 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8398
8399 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8400 ADISPLAY_ID_DEFAULT);
8401 mDispatcher->notifyMotion(&motionArgs);
8402 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8403 window->assertNoEvents();
8404}
8405
8406TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8407 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8408 std::make_shared<FakeApplicationHandle>();
8409 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008410 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8411 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008412 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8413 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008414 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008415 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008416 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8417 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008418 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008419 window->setOwnerInfo(222, 222);
8420 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8421 window->setFocusable(true);
8422 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8423 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008424 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008425
8426 // With the flag set, window should not get any input
8427 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
8428 mDispatcher->notifyKey(&keyArgs);
8429 window->assertNoEvents();
8430
8431 NotifyMotionArgs motionArgs =
8432 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8433 ADISPLAY_ID_DEFAULT);
8434 mDispatcher->notifyMotion(&motionArgs);
8435 window->assertNoEvents();
8436
8437 // When the window is no longer obscured because it went on top, it should get input
8438 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8439
8440 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
8441 mDispatcher->notifyKey(&keyArgs);
8442 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8443
8444 motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8445 ADISPLAY_ID_DEFAULT);
8446 mDispatcher->notifyMotion(&motionArgs);
8447 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8448 window->assertNoEvents();
8449}
8450
Antonio Kantekf16f2832021-09-28 04:39:20 +00008451class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8452protected:
8453 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008454 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008455 sp<FakeWindowHandle> mWindow;
8456 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008457 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008458
8459 void SetUp() override {
8460 InputDispatcherTest::SetUp();
8461
8462 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008463 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008464 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008465 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008466 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008467 mSecondWindow =
8468 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008469 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008470 mThirdWindow =
8471 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8472 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8473 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008474
8475 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008476 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8477 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8478 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008479 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008480
Antonio Kantek15beb512022-06-13 22:35:41 +00008481 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008482 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008483 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008484 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8485 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008486 mThirdWindow->assertNoEvents();
8487 }
8488
8489 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8490 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008491 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008492 SECOND_DISPLAY_ID)) {
8493 mWindow->assertNoEvents();
8494 mSecondWindow->assertNoEvents();
8495 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008496 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008497 }
8498
Antonio Kantek15beb512022-06-13 22:35:41 +00008499 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8500 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008501 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8502 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008503 mWindow->consumeTouchModeEvent(inTouchMode);
8504 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008505 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008506 }
8507};
8508
Antonio Kantek26defcf2022-02-08 01:12:27 +00008509TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008510 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008511 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8512 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008513 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008514}
8515
Antonio Kantek26defcf2022-02-08 01:12:27 +00008516TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8517 const WindowInfo& windowInfo = *mWindow->getInfo();
8518 int32_t ownerPid = windowInfo.ownerPid;
8519 int32_t ownerUid = windowInfo.ownerUid;
8520 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8521 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008522 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008523 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008524 mWindow->assertNoEvents();
8525 mSecondWindow->assertNoEvents();
8526}
8527
8528TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8529 const WindowInfo& windowInfo = *mWindow->getInfo();
8530 int32_t ownerPid = windowInfo.ownerPid;
8531 int32_t ownerUid = windowInfo.ownerUid;
8532 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008533 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008534 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008535}
8536
Antonio Kantekf16f2832021-09-28 04:39:20 +00008537TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008538 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008539 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8540 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008541 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008542 mWindow->assertNoEvents();
8543 mSecondWindow->assertNoEvents();
8544}
8545
Antonio Kantek15beb512022-06-13 22:35:41 +00008546TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8547 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8548 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8549 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008550 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008551 mWindow->assertNoEvents();
8552 mSecondWindow->assertNoEvents();
8553 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8554}
8555
Antonio Kantek48710e42022-03-24 14:19:30 -07008556TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8557 // Interact with the window first.
8558 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8559 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8560 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8561
8562 // Then remove focus.
8563 mWindow->setFocusable(false);
8564 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8565
8566 // Assert that caller can switch touch mode by owning one of the last interacted window.
8567 const WindowInfo& windowInfo = *mWindow->getInfo();
8568 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8569 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008570 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008571}
8572
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008573class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8574public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008575 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008576 std::shared_ptr<FakeApplicationHandle> application =
8577 std::make_shared<FakeApplicationHandle>();
8578 std::string name = "Fake Spy ";
8579 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008580 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8581 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008582 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008583 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008584 return spy;
8585 }
8586
8587 sp<FakeWindowHandle> createForeground() {
8588 std::shared_ptr<FakeApplicationHandle> application =
8589 std::make_shared<FakeApplicationHandle>();
8590 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008591 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8592 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008593 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008594 return window;
8595 }
8596
8597private:
8598 int mSpyCount{0};
8599};
8600
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008601using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008602/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008603 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8604 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008605TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8606 ScopedSilentDeath _silentDeath;
8607
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008608 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008609 spy->setTrustedOverlay(false);
8610 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8611 ".* not a trusted overlay");
8612}
8613
8614/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008615 * Input injection into a display with a spy window but no foreground windows should succeed.
8616 */
8617TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008618 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008619 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8620
8621 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8622 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8623 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8624 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8625}
8626
8627/**
8628 * Verify the order in which different input windows receive events. The touched foreground window
8629 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8630 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8631 * receive events before ones belows it.
8632 *
8633 * Here, we set up a scenario with four windows in the following Z order from the top:
8634 * spy1, spy2, window, spy3.
8635 * We then inject an event and verify that the foreground "window" receives it first, followed by
8636 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
8637 * window.
8638 */
8639TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
8640 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008641 auto spy1 = createSpy();
8642 auto spy2 = createSpy();
8643 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008644 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
8645 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
8646 const size_t numChannels = channels.size();
8647
Michael Wright8e9a8562022-02-09 13:44:29 +00008648 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008649 if (!epollFd.ok()) {
8650 FAIL() << "Failed to create epoll fd";
8651 }
8652
8653 for (size_t i = 0; i < numChannels; i++) {
8654 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
8655 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
8656 FAIL() << "Failed to add fd to epoll";
8657 }
8658 }
8659
8660 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8661 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8662 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8663
8664 std::vector<size_t> eventOrder;
8665 std::vector<struct epoll_event> events(numChannels);
8666 for (;;) {
8667 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
8668 (100ms).count());
8669 if (nFds < 0) {
8670 FAIL() << "Failed to call epoll_wait";
8671 }
8672 if (nFds == 0) {
8673 break; // epoll_wait timed out
8674 }
8675 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07008676 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07008677 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008678 channels[i]->consumeMotionDown();
8679 }
8680 }
8681
8682 // Verify the order in which the events were received.
8683 EXPECT_EQ(3u, eventOrder.size());
8684 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
8685 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
8686 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
8687}
8688
8689/**
8690 * A spy window using the NOT_TOUCHABLE flag does not receive events.
8691 */
8692TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
8693 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008694 auto spy = createSpy();
8695 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008696 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8697
8698 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8699 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8700 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8701 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8702 spy->assertNoEvents();
8703}
8704
8705/**
8706 * A spy window will only receive gestures that originate within its touchable region. Gestures that
8707 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
8708 * to the window.
8709 */
8710TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
8711 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008712 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008713 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
8714 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8715
8716 // Inject an event outside the spy window's touchable region.
8717 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8718 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8719 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8720 window->consumeMotionDown();
8721 spy->assertNoEvents();
8722 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8723 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8724 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8725 window->consumeMotionUp();
8726 spy->assertNoEvents();
8727
8728 // Inject an event inside the spy window's touchable region.
8729 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8730 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8731 {5, 10}))
8732 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8733 window->consumeMotionDown();
8734 spy->consumeMotionDown();
8735}
8736
8737/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008738 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008739 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008740 */
8741TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
8742 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008743 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008744 auto spy = createSpy();
8745 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008746 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008747 spy->setFrame(Rect{0, 0, 20, 20});
8748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8749
8750 // Inject an event outside the spy window's frame and touchable region.
8751 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008752 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8753 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008754 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8755 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008756 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008757}
8758
8759/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008760 * Even when a spy window spans over multiple foreground windows, the spy should receive all
8761 * pointers that are down within its bounds.
8762 */
8763TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
8764 auto windowLeft = createForeground();
8765 windowLeft->setFrame({0, 0, 100, 200});
8766 auto windowRight = createForeground();
8767 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008768 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008769 spy->setFrame({0, 0, 200, 200});
8770 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
8771
8772 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8773 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8774 {50, 50}))
8775 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8776 windowLeft->consumeMotionDown();
8777 spy->consumeMotionDown();
8778
8779 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008780 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008781 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008782 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8783 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008784 .build();
8785 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8786 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8787 InputEventInjectionSync::WAIT_FOR_RESULT))
8788 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8789 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00008790 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008791}
8792
8793/**
8794 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
8795 * the spy should receive the second pointer with ACTION_DOWN.
8796 */
8797TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
8798 auto window = createForeground();
8799 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008800 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008801 spyRight->setFrame({100, 0, 200, 200});
8802 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
8803
8804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8805 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8806 {50, 50}))
8807 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8808 window->consumeMotionDown();
8809 spyRight->assertNoEvents();
8810
8811 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008812 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008813 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008814 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8815 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008816 .build();
8817 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8818 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8819 InputEventInjectionSync::WAIT_FOR_RESULT))
8820 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008821 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008822 spyRight->consumeMotionDown();
8823}
8824
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008825/**
8826 * The spy window should not be able to affect whether or not touches are split. Only the foreground
8827 * windows should be allowed to control split touch.
8828 */
8829TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008830 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008831 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008832 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008833 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008834
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008835 auto window = createForeground();
8836 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008837
8838 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8839
8840 // First finger down, no window touched.
8841 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8842 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8843 {100, 200}))
8844 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8845 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8846 window->assertNoEvents();
8847
8848 // Second finger down on window, the window should receive touch down.
8849 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008850 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008851 .displayId(ADISPLAY_ID_DEFAULT)
8852 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008853 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8854 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008855 .build();
8856 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8857 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8858 InputEventInjectionSync::WAIT_FOR_RESULT))
8859 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8860
8861 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00008862 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008863}
8864
8865/**
8866 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
8867 * do not receive key events.
8868 */
8869TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008870 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008871 spy->setFocusable(false);
8872
8873 auto window = createForeground();
8874 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8875 setFocusedWindow(window);
8876 window->consumeFocusEvent(true);
8877
8878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
8879 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8880 window->consumeKeyDown(ADISPLAY_ID_NONE);
8881
8882 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
8883 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8884 window->consumeKeyUp(ADISPLAY_ID_NONE);
8885
8886 spy->assertNoEvents();
8887}
8888
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008889using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
8890
8891/**
8892 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
8893 * are currently sent to any other windows - including other spy windows - will also be cancelled.
8894 */
8895TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
8896 auto window = createForeground();
8897 auto spy1 = createSpy();
8898 auto spy2 = createSpy();
8899 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
8900
8901 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8902 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8903 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8904 window->consumeMotionDown();
8905 spy1->consumeMotionDown();
8906 spy2->consumeMotionDown();
8907
8908 // Pilfer pointers from the second spy window.
8909 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
8910 spy2->assertNoEvents();
8911 spy1->consumeMotionCancel();
8912 window->consumeMotionCancel();
8913
8914 // The rest of the gesture should only be sent to the second spy window.
8915 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8916 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8917 ADISPLAY_ID_DEFAULT))
8918 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8919 spy2->consumeMotionMove();
8920 spy1->assertNoEvents();
8921 window->assertNoEvents();
8922}
8923
8924/**
8925 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
8926 * in the middle of the gesture.
8927 */
8928TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
8929 auto window = createForeground();
8930 auto spy = createSpy();
8931 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8932
8933 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8934 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8935 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8936 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8937 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8938
8939 window->releaseChannel();
8940
8941 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8942
8943 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8944 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8945 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8946 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8947}
8948
8949/**
8950 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
8951 * the spy, but not to any other windows.
8952 */
8953TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
8954 auto spy = createSpy();
8955 auto window = createForeground();
8956
8957 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8958
8959 // First finger down on the window and the spy.
8960 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8961 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8962 {100, 200}))
8963 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8964 spy->consumeMotionDown();
8965 window->consumeMotionDown();
8966
8967 // Spy window pilfers the pointers.
8968 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
8969 window->consumeMotionCancel();
8970
8971 // Second finger down on the window and spy, but the window should not receive the pointer down.
8972 const MotionEvent secondFingerDownEvent =
8973 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8974 .displayId(ADISPLAY_ID_DEFAULT)
8975 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008976 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8977 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008978 .build();
8979 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8980 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8981 InputEventInjectionSync::WAIT_FOR_RESULT))
8982 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8983
Harry Cutts33476232023-01-30 19:57:29 +00008984 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008985
8986 // Third finger goes down outside all windows, so injection should fail.
8987 const MotionEvent thirdFingerDownEvent =
8988 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8989 .displayId(ADISPLAY_ID_DEFAULT)
8990 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008991 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8992 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
8993 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008994 .build();
8995 ASSERT_EQ(InputEventInjectionResult::FAILED,
8996 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
8997 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008998 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008999
9000 spy->assertNoEvents();
9001 window->assertNoEvents();
9002}
9003
9004/**
9005 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9006 */
9007TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9008 auto spy = createSpy();
9009 spy->setFrame(Rect(0, 0, 100, 100));
9010 auto window = createForeground();
9011 window->setFrame(Rect(0, 0, 200, 200));
9012
9013 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9014
9015 // First finger down on the window only
9016 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9017 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9018 {150, 150}))
9019 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9020 window->consumeMotionDown();
9021
9022 // Second finger down on the spy and window
9023 const MotionEvent secondFingerDownEvent =
9024 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9025 .displayId(ADISPLAY_ID_DEFAULT)
9026 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009027 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9028 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009029 .build();
9030 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9031 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9032 InputEventInjectionSync::WAIT_FOR_RESULT))
9033 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9034 spy->consumeMotionDown();
9035 window->consumeMotionPointerDown(1);
9036
9037 // Third finger down on the spy and window
9038 const MotionEvent thirdFingerDownEvent =
9039 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9040 .displayId(ADISPLAY_ID_DEFAULT)
9041 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009042 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9043 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9044 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009045 .build();
9046 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9047 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9048 InputEventInjectionSync::WAIT_FOR_RESULT))
9049 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9050 spy->consumeMotionPointerDown(1);
9051 window->consumeMotionPointerDown(2);
9052
9053 // Spy window pilfers the pointers.
9054 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9055 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9056 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9057
9058 spy->assertNoEvents();
9059 window->assertNoEvents();
9060}
9061
9062/**
9063 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9064 * other windows should be canceled. If this results in the cancellation of all pointers for some
9065 * window, then that window should receive ACTION_CANCEL.
9066 */
9067TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9068 auto spy = createSpy();
9069 spy->setFrame(Rect(0, 0, 100, 100));
9070 auto window = createForeground();
9071 window->setFrame(Rect(0, 0, 200, 200));
9072
9073 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9074
9075 // First finger down on both spy and window
9076 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9077 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9078 {10, 10}))
9079 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9080 window->consumeMotionDown();
9081 spy->consumeMotionDown();
9082
9083 // Second finger down on the spy and window
9084 const MotionEvent secondFingerDownEvent =
9085 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9086 .displayId(ADISPLAY_ID_DEFAULT)
9087 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009088 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9089 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009090 .build();
9091 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9092 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9093 InputEventInjectionSync::WAIT_FOR_RESULT))
9094 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9095 spy->consumeMotionPointerDown(1);
9096 window->consumeMotionPointerDown(1);
9097
9098 // Spy window pilfers the pointers.
9099 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9100 window->consumeMotionCancel();
9101
9102 spy->assertNoEvents();
9103 window->assertNoEvents();
9104}
9105
9106/**
9107 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9108 * be sent to other windows
9109 */
9110TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9111 auto spy = createSpy();
9112 spy->setFrame(Rect(0, 0, 100, 100));
9113 auto window = createForeground();
9114 window->setFrame(Rect(0, 0, 200, 200));
9115
9116 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9117
9118 // First finger down on both window and spy
9119 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9120 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9121 {10, 10}))
9122 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9123 window->consumeMotionDown();
9124 spy->consumeMotionDown();
9125
9126 // Spy window pilfers the pointers.
9127 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9128 window->consumeMotionCancel();
9129
9130 // Second finger down on the window only
9131 const MotionEvent secondFingerDownEvent =
9132 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9133 .displayId(ADISPLAY_ID_DEFAULT)
9134 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009135 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9136 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009137 .build();
9138 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9139 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9140 InputEventInjectionSync::WAIT_FOR_RESULT))
9141 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9142 window->consumeMotionDown();
9143 window->assertNoEvents();
9144
9145 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9146 spy->consumeMotionMove();
9147 spy->assertNoEvents();
9148}
9149
Prabir Pradhand65552b2021-10-07 11:23:50 -07009150class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9151public:
9152 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9153 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9154 std::make_shared<FakeApplicationHandle>();
9155 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009156 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9157 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009158 overlay->setFocusable(false);
9159 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009160 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009161 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009162 overlay->setTrustedOverlay(true);
9163
9164 std::shared_ptr<FakeApplicationHandle> application =
9165 std::make_shared<FakeApplicationHandle>();
9166 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009167 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9168 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009169 window->setFocusable(true);
9170 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009171
9172 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9173 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9174 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009175 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009176 return {std::move(overlay), std::move(window)};
9177 }
9178
9179 void sendFingerEvent(int32_t action) {
9180 NotifyMotionArgs motionArgs =
9181 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9182 ADISPLAY_ID_DEFAULT, {PointF{20, 20}});
9183 mDispatcher->notifyMotion(&motionArgs);
9184 }
9185
9186 void sendStylusEvent(int32_t action) {
9187 NotifyMotionArgs motionArgs =
9188 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9189 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009190 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhand65552b2021-10-07 11:23:50 -07009191 mDispatcher->notifyMotion(&motionArgs);
9192 }
9193};
9194
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009195using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9196
9197TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9198 ScopedSilentDeath _silentDeath;
9199
Prabir Pradhand65552b2021-10-07 11:23:50 -07009200 auto [overlay, window] = setupStylusOverlayScenario();
9201 overlay->setTrustedOverlay(false);
9202 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9203 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9204 ".* not a trusted overlay");
9205}
9206
9207TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9208 auto [overlay, window] = setupStylusOverlayScenario();
9209 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9210
9211 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9212 overlay->consumeMotionDown();
9213 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9214 overlay->consumeMotionUp();
9215
9216 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9217 window->consumeMotionDown();
9218 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9219 window->consumeMotionUp();
9220
9221 overlay->assertNoEvents();
9222 window->assertNoEvents();
9223}
9224
9225TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9226 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009227 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009228 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9229
9230 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9231 overlay->consumeMotionDown();
9232 window->consumeMotionDown();
9233 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9234 overlay->consumeMotionUp();
9235 window->consumeMotionUp();
9236
9237 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9238 window->consumeMotionDown();
9239 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9240 window->consumeMotionUp();
9241
9242 overlay->assertNoEvents();
9243 window->assertNoEvents();
9244}
9245
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009246/**
9247 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9248 * The scenario is as follows:
9249 * - The stylus interceptor overlay is configured as a spy window.
9250 * - The stylus interceptor spy receives the start of a new stylus gesture.
9251 * - It pilfers pointers and then configures itself to no longer be a spy.
9252 * - The stylus interceptor continues to receive the rest of the gesture.
9253 */
9254TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9255 auto [overlay, window] = setupStylusOverlayScenario();
9256 overlay->setSpy(true);
9257 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9258
9259 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9260 overlay->consumeMotionDown();
9261 window->consumeMotionDown();
9262
9263 // The interceptor pilfers the pointers.
9264 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9265 window->consumeMotionCancel();
9266
9267 // The interceptor configures itself so that it is no longer a spy.
9268 overlay->setSpy(false);
9269 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9270
9271 // It continues to receive the rest of the stylus gesture.
9272 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9273 overlay->consumeMotionMove();
9274 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9275 overlay->consumeMotionUp();
9276
9277 window->assertNoEvents();
9278}
9279
Prabir Pradhan5735a322022-04-11 17:23:34 +00009280struct User {
9281 int32_t mPid;
9282 int32_t mUid;
9283 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9284 std::unique_ptr<InputDispatcher>& mDispatcher;
9285
9286 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9287 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9288
9289 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9290 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9291 ADISPLAY_ID_DEFAULT, {100, 200},
9292 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9293 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9294 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9295 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9296 }
9297
9298 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009299 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009300 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009301 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009302 mPolicyFlags);
9303 }
9304
9305 sp<FakeWindowHandle> createWindow() const {
9306 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9307 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009308 sp<FakeWindowHandle> window =
9309 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9310 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009311 window->setOwnerInfo(mPid, mUid);
9312 return window;
9313 }
9314};
9315
9316using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9317
9318TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9319 auto owner = User(mDispatcher, 10, 11);
9320 auto window = owner.createWindow();
9321 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9322
9323 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9324 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9325 window->consumeMotionDown();
9326
9327 setFocusedWindow(window);
9328 window->consumeFocusEvent(true);
9329
9330 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9331 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9332 window->consumeKeyDown(ADISPLAY_ID_NONE);
9333}
9334
9335TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9336 auto owner = User(mDispatcher, 10, 11);
9337 auto window = owner.createWindow();
9338 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9339
9340 auto rando = User(mDispatcher, 20, 21);
9341 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9342 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9343
9344 setFocusedWindow(window);
9345 window->consumeFocusEvent(true);
9346
9347 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9348 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9349 window->assertNoEvents();
9350}
9351
9352TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9353 auto owner = User(mDispatcher, 10, 11);
9354 auto window = owner.createWindow();
9355 auto spy = owner.createWindow();
9356 spy->setSpy(true);
9357 spy->setTrustedOverlay(true);
9358 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9359
9360 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9361 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9362 spy->consumeMotionDown();
9363 window->consumeMotionDown();
9364}
9365
9366TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9367 auto owner = User(mDispatcher, 10, 11);
9368 auto window = owner.createWindow();
9369
9370 auto rando = User(mDispatcher, 20, 21);
9371 auto randosSpy = rando.createWindow();
9372 randosSpy->setSpy(true);
9373 randosSpy->setTrustedOverlay(true);
9374 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9375
9376 // The event is targeted at owner's window, so injection should succeed, but the spy should
9377 // not receive the event.
9378 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9379 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9380 randosSpy->assertNoEvents();
9381 window->consumeMotionDown();
9382}
9383
9384TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9385 auto owner = User(mDispatcher, 10, 11);
9386 auto window = owner.createWindow();
9387
9388 auto rando = User(mDispatcher, 20, 21);
9389 auto randosSpy = rando.createWindow();
9390 randosSpy->setSpy(true);
9391 randosSpy->setTrustedOverlay(true);
9392 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9393
9394 // A user that has injection permission can inject into any window.
9395 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9396 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9397 ADISPLAY_ID_DEFAULT));
9398 randosSpy->consumeMotionDown();
9399 window->consumeMotionDown();
9400
9401 setFocusedWindow(randosSpy);
9402 randosSpy->consumeFocusEvent(true);
9403
9404 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9405 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9406 window->assertNoEvents();
9407}
9408
9409TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9410 auto owner = User(mDispatcher, 10, 11);
9411 auto window = owner.createWindow();
9412
9413 auto rando = User(mDispatcher, 20, 21);
9414 auto randosWindow = rando.createWindow();
9415 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9416 randosWindow->setWatchOutsideTouch(true);
9417 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9418
9419 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9420 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9421 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9422 window->consumeMotionDown();
9423 randosWindow->consumeMotionOutside();
9424}
9425
Garfield Tane84e6f92019-08-29 17:28:41 -07009426} // namespace android::inputdispatcher