blob: 749d4dfd67790327437a981345d0ecf553d92548 [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"
Prabir Pradhan9205e422023-05-16 20:06:13 +000018#include "EventBuilders.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080019
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070020#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080021#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080022#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070023#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070024#include <binder/Binder.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000025#include <fcntl.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080026#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080027#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100028#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080030#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100031
Garfield Tan1c7bc862020-01-28 13:24:04 -080032#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080033#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070034#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080035#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080036#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037
Garfield Tan1c7bc862020-01-28 13:24:04 -080038using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050039using android::gui::FocusRequest;
40using android::gui::TouchOcclusionMode;
41using android::gui::WindowInfo;
42using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080043using android::os::InputEventInjectionResult;
44using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080045
Garfield Tane84e6f92019-08-29 17:28:41 -070046namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080047
Dominik Laskowski2f01d772022-03-23 16:01:29 -070048using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080049using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070050
Michael Wrightd02c5b62014-02-10 15:10:22 -080051// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000052static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080053
54// An arbitrary device id.
Prabir Pradhan9205e422023-05-16 20:06:13 +000055static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080056static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080057
Jeff Brownf086ddb2014-02-11 14:28:48 -080058// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000059static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
60static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080061
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080062static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
63static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
64static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
65static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -070066static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080067static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080068static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080069static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080070/**
71 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
72 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
73 * index 0) is the new pointer going down. The same pointer could have been placed at a different
74 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
75 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
76 * pointer id=0 leaves but the pointer id=1 remains.
77 */
78static constexpr int32_t POINTER_0_DOWN =
79 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080080static constexpr int32_t POINTER_1_DOWN =
81 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000082static constexpr int32_t POINTER_2_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000084static constexpr int32_t POINTER_3_DOWN =
85 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000086static constexpr int32_t POINTER_0_UP =
87 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080088static constexpr int32_t POINTER_1_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Harry Cuttsb166c002023-05-09 13:06:05 +000090static constexpr int32_t POINTER_2_UP =
91 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080092
Antonio Kantek15beb512022-06-13 22:35:41 +000093// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000094static constexpr int32_t WINDOW_PID = 999;
95static constexpr int32_t WINDOW_UID = 1001;
96
Antonio Kantek15beb512022-06-13 22:35:41 +000097// The default pid and uid for the windows created on the secondary display by the test.
98static constexpr int32_t SECONDARY_WINDOW_PID = 1010;
99static constexpr int32_t SECONDARY_WINDOW_UID = 1012;
100
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000101// An arbitrary pid of the gesture monitor window
102static constexpr int32_t MONITOR_PID = 2001;
103
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800104static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
105
Arthur Hungc539dbb2022-12-08 07:45:36 +0000106static constexpr int expectedWallpaperFlags =
107 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
108
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800109using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
110
chaviwd1c23182019-12-20 18:44:56 -0800111struct PointF {
112 float x;
113 float y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800114 auto operator<=>(const PointF&) const = default;
chaviwd1c23182019-12-20 18:44:56 -0800115};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
Gang Wang342c9272020-01-13 13:15:04 -0500117/**
118 * Return a DOWN key event with KEYCODE_A.
119 */
120static KeyEvent getTestKeyEvent() {
121 KeyEvent event;
122
Garfield Tanfbe732e2020-01-24 11:26:14 -0800123 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
124 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
125 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500126 return event;
127}
128
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000129static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
130 ASSERT_EQ(expectedAction, receivedAction)
131 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
132 << MotionEvent::actionToString(receivedAction);
133}
134
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800135MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
136 bool matches = action == arg.getAction();
137 if (!matches) {
138 *result_listener << "expected action " << MotionEvent::actionToString(action)
139 << ", but got " << MotionEvent::actionToString(arg.getAction());
140 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800141 if (action == AMOTION_EVENT_ACTION_DOWN) {
142 if (!matches) {
143 *result_listener << "; ";
144 }
145 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
146 matches &= arg.getDownTime() == arg.getEventTime();
147 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800148 if (action == AMOTION_EVENT_ACTION_CANCEL) {
149 if (!matches) {
150 *result_listener << "; ";
151 }
152 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
153 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
154 }
155 return matches;
156}
157
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800158MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
159 return arg.getDownTime() == downTime;
160}
161
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800162MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
163 return arg.getDisplayId() == displayId;
164}
165
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800166MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") {
167 return arg.getDeviceId() == deviceId;
168}
169
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800170MATCHER_P(WithSource, source, "InputEvent with specified source") {
171 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
172 << inputEventSourceToString(arg.getSource());
173 return arg.getSource() == source;
174}
175
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800176MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
177 return arg.getFlags() == flags;
178}
179
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800180MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
181 if (arg.getPointerCount() != 1) {
182 *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
183 return false;
184 }
Harry Cutts33476232023-01-30 19:57:29 +0000185 return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800186}
187
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800188MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") {
189 return arg.getPointerCount() == pointerCount;
190}
191
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800192MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
193 // Build a map for the received pointers, by pointer id
194 std::map<int32_t /*pointerId*/, PointF> actualPointers;
195 for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
196 const int32_t pointerId = arg.getPointerId(pointerIndex);
197 actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
198 }
199 return pointers == actualPointers;
200}
201
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202// --- FakeInputDispatcherPolicy ---
203
204class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
205 InputDispatcherConfiguration mConfig;
206
Prabir Pradhanedd96402022-02-15 01:46:16 -0800207 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
208
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209public:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000210 FakeInputDispatcherPolicy() = default;
211 virtual ~FakeInputDispatcherPolicy() = default;
Jackal Guof9696682018-10-05 12:23:23 +0800212
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800213 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700214 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700215 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700216 EXPECT_EQ(event.getDisplayId(), args.displayId);
217
218 const auto& keyEvent = static_cast<const KeyEvent&>(event);
219 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
220 EXPECT_EQ(keyEvent.getAction(), args.action);
221 });
Jackal Guof9696682018-10-05 12:23:23 +0800222 }
223
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700224 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
225 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700226 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700227 EXPECT_EQ(event.getDisplayId(), args.displayId);
228
229 const auto& motionEvent = static_cast<const MotionEvent&>(event);
230 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
231 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000232 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
233 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
234 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
235 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700236 });
Jackal Guof9696682018-10-05 12:23:23 +0800237 }
238
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700239 void assertFilterInputEventWasNotCalled() {
240 std::scoped_lock lock(mLock);
241 ASSERT_EQ(nullptr, mFilteredEvent);
242 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800244 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700245 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800246 ASSERT_TRUE(mConfigurationChangedTime)
247 << "Timed out waiting for configuration changed call";
248 ASSERT_EQ(*mConfigurationChangedTime, when);
249 mConfigurationChangedTime = std::nullopt;
250 }
251
252 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700253 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800254 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800255 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800256 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
257 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
258 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
259 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
260 mLastNotifySwitch = std::nullopt;
261 }
262
chaviwfd6d3512019-03-25 13:23:49 -0700263 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700264 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800265 ASSERT_EQ(touchedToken, mOnPointerDownToken);
266 mOnPointerDownToken.clear();
267 }
268
269 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700270 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800271 ASSERT_TRUE(mOnPointerDownToken == nullptr)
272 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700273 }
274
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700275 // This function must be called soon after the expected ANR timer starts,
276 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500277 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700278 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500279 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800280 std::unique_lock lock(mLock);
281 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500282 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800283 ASSERT_NO_FATAL_FAILURE(
284 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500285 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700286 }
287
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000288 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800289 const sp<WindowInfoHandle>& window) {
290 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
291 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
292 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500293 }
294
Prabir Pradhanedd96402022-02-15 01:46:16 -0800295 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
296 const sp<IBinder>& expectedToken,
297 int32_t expectedPid) {
298 std::unique_lock lock(mLock);
299 android::base::ScopedLockAssertion assumeLocked(mLock);
300 AnrResult result;
301 ASSERT_NO_FATAL_FAILURE(result =
302 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
303 const auto& [token, pid] = result;
304 ASSERT_EQ(expectedToken, token);
305 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500306 }
307
Prabir Pradhanedd96402022-02-15 01:46:16 -0800308 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000309 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500310 std::unique_lock lock(mLock);
311 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800312 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
313 const auto& [token, _] = result;
314 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000315 }
316
Prabir Pradhanedd96402022-02-15 01:46:16 -0800317 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
318 int32_t expectedPid) {
319 std::unique_lock lock(mLock);
320 android::base::ScopedLockAssertion assumeLocked(mLock);
321 AnrResult result;
322 ASSERT_NO_FATAL_FAILURE(
323 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
324 const auto& [token, pid] = result;
325 ASSERT_EQ(expectedToken, token);
326 ASSERT_EQ(expectedPid, pid);
327 }
328
329 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000330 sp<IBinder> getResponsiveWindowToken() {
331 std::unique_lock lock(mLock);
332 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800333 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
334 const auto& [token, _] = result;
335 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700336 }
337
338 void assertNotifyAnrWasNotCalled() {
339 std::scoped_lock lock(mLock);
340 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800341 ASSERT_TRUE(mAnrWindows.empty());
342 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500343 << "ANR was not called, but please also consume the 'connection is responsive' "
344 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700345 }
346
Garfield Tan1c7bc862020-01-28 13:24:04 -0800347 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
348 mConfig.keyRepeatTimeout = timeout;
349 mConfig.keyRepeatDelay = delay;
350 }
351
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000352 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800353 std::unique_lock lock(mLock);
354 base::ScopedLockAssertion assumeLocked(mLock);
355
356 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
357 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000358 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800359 enabled;
360 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000361 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
362 << ") to be called.";
363 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800364 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000365 auto request = *mPointerCaptureRequest;
366 mPointerCaptureRequest.reset();
367 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800368 }
369
370 void assertSetPointerCaptureNotCalled() {
371 std::unique_lock lock(mLock);
372 base::ScopedLockAssertion assumeLocked(mLock);
373
374 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000375 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800376 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000377 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800378 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000379 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800380 }
381
arthurhungf452d0b2021-01-06 00:19:52 +0800382 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
383 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800384 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800385 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800386 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800387 }
388
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800389 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
390 std::unique_lock lock(mLock);
391 base::ScopedLockAssertion assumeLocked(mLock);
392 std::optional<sp<IBinder>> receivedToken =
393 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
394 mNotifyInputChannelBroken);
395 ASSERT_TRUE(receivedToken.has_value());
396 ASSERT_EQ(token, *receivedToken);
397 }
398
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800399 /**
400 * Set policy timeout. A value of zero means next key will not be intercepted.
401 */
402 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
403 mInterceptKeyTimeout = timeout;
404 }
405
Josep del Riob3981622023-04-18 15:49:45 +0000406 void assertUserActivityPoked() {
407 std::scoped_lock lock(mLock);
408 ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked";
409 }
410
411 void assertUserActivityNotPoked() {
412 std::scoped_lock lock(mLock);
413 ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked";
414 }
415
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700417 std::mutex mLock;
418 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
419 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
420 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
421 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800422
Prabir Pradhan99987712020-11-10 18:43:05 -0800423 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000424
425 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800426
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700427 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700428 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800429 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
430 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700431 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800432 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
433 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700434
arthurhungf452d0b2021-01-06 00:19:52 +0800435 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800436 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Josep del Riob3981622023-04-18 15:49:45 +0000437 bool mPokedUserActivity GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800438
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800439 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
440
Prabir Pradhanedd96402022-02-15 01:46:16 -0800441 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
442 // for a specific container to become non-empty. When the container is non-empty, return the
443 // first entry from the container and erase it.
444 template <class T>
445 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
446 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
447 // If there is an ANR, Dispatcher won't be idle because there are still events
448 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
449 // before checking if ANR was called.
450 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
451 // to provide it some time to act. 100ms seems reasonable.
452 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
453 const std::chrono::time_point start = std::chrono::steady_clock::now();
454 std::optional<T> token =
455 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
456 if (!token.has_value()) {
457 ADD_FAILURE() << "Did not receive the ANR callback";
458 return {};
459 }
460
461 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
462 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
463 // the dispatcher started counting before this function was called
464 if (std::chrono::abs(timeout - waited) > 100ms) {
465 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
466 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
467 << "ms, but waited "
468 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
469 << "ms instead";
470 }
471 return *token;
472 }
473
474 template <class T>
475 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
476 std::queue<T>& storage,
477 std::unique_lock<std::mutex>& lock,
478 std::condition_variable& condition)
479 REQUIRES(mLock) {
480 condition.wait_for(lock, timeout,
481 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
482 if (storage.empty()) {
483 ADD_FAILURE() << "Did not receive the expected callback";
484 return std::nullopt;
485 }
486 T item = storage.front();
487 storage.pop();
488 return std::make_optional(item);
489 }
490
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600491 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700492 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800493 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 }
495
Prabir Pradhanedd96402022-02-15 01:46:16 -0800496 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
497 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700498 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800499 ASSERT_TRUE(pid.has_value());
500 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700501 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500502 }
503
Prabir Pradhanedd96402022-02-15 01:46:16 -0800504 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
505 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500506 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800507 ASSERT_TRUE(pid.has_value());
508 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500509 mNotifyAnr.notify_all();
510 }
511
512 void notifyNoFocusedWindowAnr(
513 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
514 std::scoped_lock lock(mLock);
515 mAnrApplications.push(applicationHandle);
516 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517 }
518
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800519 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
520 std::scoped_lock lock(mLock);
521 mBrokenInputChannels.push(connectionToken);
522 mNotifyInputChannelBroken.notify_all();
523 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600525 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700526
Chris Yef59a2f42020-10-16 12:55:26 -0700527 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
528 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
529 const std::vector<float>& values) override {}
530
531 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
532 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000533
Chris Yefb552902021-02-03 17:18:37 -0800534 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
535
Prabir Pradhana41d2442023-04-20 21:30:40 +0000536 InputDispatcherConfiguration getDispatcherConfiguration() override { return mConfig; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537
Prabir Pradhana41d2442023-04-20 21:30:40 +0000538 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700539 std::scoped_lock lock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +0000540 switch (inputEvent.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700541 case InputEventType::KEY: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000542 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent);
543 mFilteredEvent = std::make_unique<KeyEvent>(keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800544 break;
545 }
546
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700547 case InputEventType::MOTION: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000548 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent);
549 mFilteredEvent = std::make_unique<MotionEvent>(motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800550 break;
551 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700552 default: {
553 ADD_FAILURE() << "Should only filter keys or motions";
554 break;
555 }
Jackal Guof9696682018-10-05 12:23:23 +0800556 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 return true;
558 }
559
Prabir Pradhana41d2442023-04-20 21:30:40 +0000560 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override {
561 if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800562 // Clear intercept state when we handled the event.
563 mInterceptKeyTimeout = 0ms;
564 }
565 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600567 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568
Prabir Pradhana41d2442023-04-20 21:30:40 +0000569 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800570 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
571 // Clear intercept state so we could dispatch the event in next wake.
572 mInterceptKeyTimeout = 0ms;
573 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 }
575
Prabir Pradhana41d2442023-04-20 21:30:40 +0000576 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&,
577 uint32_t) override {
578 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 }
580
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600581 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
582 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700583 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800584 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
585 * essentially a passthrough for notifySwitch.
586 */
Harry Cutts33476232023-01-30 19:57:29 +0000587 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 }
589
Josep del Riob3981622023-04-18 15:49:45 +0000590 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {
591 std::scoped_lock lock(mLock);
592 mPokedUserActivity = true;
593 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600595 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700596 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700597 mOnPointerDownToken = newToken;
598 }
599
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000600 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800601 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000602 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800603 mPointerCaptureChangedCondition.notify_all();
604 }
605
arthurhungf452d0b2021-01-06 00:19:52 +0800606 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
607 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800608 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800609 mDropTargetWindowToken = token;
610 }
611
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700612 void assertFilterInputEventWasCalledInternal(
613 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700614 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800615 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700616 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800617 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800618 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619};
620
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621// --- InputDispatcherTest ---
622
623class InputDispatcherTest : public testing::Test {
624protected:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000625 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700626 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000628 void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000629 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
630 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800631 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000632 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700633 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634 }
635
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000636 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700637 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000638 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700639 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700641
642 /**
643 * Used for debugging when writing the test
644 */
645 void dumpDispatcherState() {
646 std::string dump;
647 mDispatcher->dump(dump);
648 std::stringstream ss(dump);
649 std::string to;
650
651 while (std::getline(ss, to, '\n')) {
652 ALOGE("%s", to.c_str());
653 }
654 }
Vishnu Nair958da932020-08-21 17:12:37 -0700655
Chavi Weingarten847e8512023-03-29 00:26:09 +0000656 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700657 FocusRequest request;
658 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000659 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700660 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
661 request.displayId = window->getInfo()->displayId;
662 mDispatcher->setFocusedWindow(request);
663 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664};
665
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
667 KeyEvent event;
668
669 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800670 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
671 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600672 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
673 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800674 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000675 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000676 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677 << "Should reject key events with undefined action.";
678
679 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800680 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
681 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600682 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800683 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000684 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000685 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686 << "Should reject key events with ACTION_MULTIPLE.";
687}
688
689TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
690 MotionEvent event;
691 PointerProperties pointerProperties[MAX_POINTERS + 1];
692 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800693 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694 pointerProperties[i].clear();
695 pointerProperties[i].id = i;
696 pointerCoords[i].clear();
697 }
698
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800699 // Some constants commonly used below
700 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
701 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
702 constexpr int32_t metaState = AMETA_NONE;
703 constexpr MotionClassification classification = MotionClassification::NONE;
704
chaviw9eaa22c2020-07-01 16:21:27 -0700705 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800707 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700708 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
709 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700710 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
711 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700712 /*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 undefined action.";
717
718 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800719 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800720 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
721 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
722 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
723 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 large.";
729
Garfield Tanfbe732e2020-01-24 11:26:14 -0800730 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700731 AMOTION_EVENT_ACTION_POINTER_DOWN |
732 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700733 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
734 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700735 identityTransform, ARBITRARY_TIME, 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 down index too small.";
741
742 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800743 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800744 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
745 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
746 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
747 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 large.";
753
Garfield Tanfbe732e2020-01-24 11:26:14 -0800754 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700755 AMOTION_EVENT_ACTION_POINTER_UP |
756 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700757 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
758 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700759 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500760 /*pointerCount*/ 1, 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 pointer up index too small.";
765
766 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800767 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
768 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700769 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700770 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
771 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700772 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800773 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000774 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000775 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800776 << "Should reject motion events with 0 pointers.";
777
Garfield Tanfbe732e2020-01-24 11:26:14 -0800778 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
779 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700780 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700781 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
782 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700783 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800784 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000785 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000786 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787 << "Should reject motion events with more than MAX_POINTERS pointers.";
788
789 // Rejects motion events with invalid pointer ids.
790 pointerProperties[0].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 less than 0.";
801
802 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800803 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
804 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700805 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700806 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
807 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700808 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800809 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000810 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000811 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800812 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
813
814 // Rejects motion events with duplicate pointer ids.
815 pointerProperties[0].id = 1;
816 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800817 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
818 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700819 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700820 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
821 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700822 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800823 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000824 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000825 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826 << "Should reject motion events with duplicate pointer ids.";
827}
828
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800829/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
830
831TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
832 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000833 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800834 ASSERT_TRUE(mDispatcher->waitForIdle());
835
836 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
837}
838
839TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000840 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
841 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000842 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800843
844 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
845 args.policyFlags |= POLICY_FLAG_TRUSTED;
846 mFakePolicy->assertNotifySwitchWasCalled(args);
847}
848
Arthur Hungb92218b2018-08-14 12:00:21 +0800849// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700850static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700851// Default input dispatching timeout if there is no focused application or paused window
852// from which to determine an appropriate dispatching timeout.
853static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
854 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
855 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800856
857class FakeApplicationHandle : public InputApplicationHandle {
858public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700859 FakeApplicationHandle() {
860 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700861 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500862 mInfo.dispatchingTimeoutMillis =
863 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700864 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800865 virtual ~FakeApplicationHandle() {}
866
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000867 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700868
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500869 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
870 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700871 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800872};
873
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800874class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800875public:
Garfield Tan15601662020-09-22 15:32:38 -0700876 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800877 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700878 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800879 }
880
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800881 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700882 InputEvent* event;
883 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
884 if (!consumeSeq) {
885 return nullptr;
886 }
887 finishEvent(*consumeSeq);
888 return event;
889 }
890
891 /**
892 * Receive an event without acknowledging it.
893 * Return the sequence number that could later be used to send finished signal.
894 */
895 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800896 uint32_t consumeSeq;
897 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800898
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800899 std::chrono::time_point start = std::chrono::steady_clock::now();
900 status_t status = WOULD_BLOCK;
901 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000902 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800903 &event);
904 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
905 if (elapsed > 100ms) {
906 break;
907 }
908 }
909
910 if (status == WOULD_BLOCK) {
911 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700912 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800913 }
914
915 if (status != OK) {
916 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700917 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800918 }
919 if (event == nullptr) {
920 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700921 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800922 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700923 if (outEvent != nullptr) {
924 *outEvent = event;
925 }
926 return consumeSeq;
927 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800928
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700929 /**
930 * To be used together with "receiveEvent" to complete the consumption of an event.
931 */
932 void finishEvent(uint32_t consumeSeq) {
933 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
934 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800935 }
936
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000937 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
938 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
939 ASSERT_EQ(OK, status);
940 }
941
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700942 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000943 std::optional<int32_t> expectedDisplayId,
944 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800945 InputEvent* event = consume();
946
947 ASSERT_NE(nullptr, event) << mName.c_str()
948 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800949 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700950 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
951 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800952
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000953 if (expectedDisplayId.has_value()) {
954 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
955 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800956
Tiger Huang8664f8c2018-10-11 19:14:35 +0800957 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700958 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800959 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
960 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000961 if (expectedFlags.has_value()) {
962 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
963 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800964 break;
965 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700966 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800967 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000968 assertMotionAction(expectedAction, motionEvent.getAction());
969
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000970 if (expectedFlags.has_value()) {
971 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
972 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800973 break;
974 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700975 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100976 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
977 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700978 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -0800979 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
980 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700981 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +0000982 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
983 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700984 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +0800985 FAIL() << "Use 'consumeDragEvent' for DRAG events";
986 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800987 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800988 }
989
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800990 MotionEvent* consumeMotion() {
991 InputEvent* event = consume();
992
993 if (event == nullptr) {
994 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
995 return nullptr;
996 }
997
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700998 if (event->getType() != InputEventType::MOTION) {
999 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001000 return nullptr;
1001 }
1002 return static_cast<MotionEvent*>(event);
1003 }
1004
1005 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1006 MotionEvent* motionEvent = consumeMotion();
1007 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1008 ASSERT_THAT(*motionEvent, matcher);
1009 }
1010
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001011 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
1012 InputEvent* event = consume();
1013 ASSERT_NE(nullptr, event) << mName.c_str()
1014 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001015 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1016 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001017
1018 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1019 << mName.c_str() << ": event displayId should always be NONE.";
1020
1021 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1022 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001023 }
1024
Prabir Pradhan99987712020-11-10 18:43:05 -08001025 void consumeCaptureEvent(bool hasCapture) {
1026 const InputEvent* event = consume();
1027 ASSERT_NE(nullptr, event) << mName.c_str()
1028 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001029 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1030 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001031
1032 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1033 << mName.c_str() << ": event displayId should always be NONE.";
1034
1035 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1036 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1037 }
1038
arthurhungb89ccb02020-12-30 16:19:01 +08001039 void consumeDragEvent(bool isExiting, float x, float y) {
1040 const InputEvent* event = consume();
1041 ASSERT_NE(nullptr, event) << mName.c_str()
1042 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001043 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001044
1045 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1046 << mName.c_str() << ": event displayId should always be NONE.";
1047
1048 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1049 EXPECT_EQ(isExiting, dragEvent.isExiting());
1050 EXPECT_EQ(x, dragEvent.getX());
1051 EXPECT_EQ(y, dragEvent.getY());
1052 }
1053
Antonio Kantekf16f2832021-09-28 04:39:20 +00001054 void consumeTouchModeEvent(bool inTouchMode) {
1055 const InputEvent* event = consume();
1056 ASSERT_NE(nullptr, event) << mName.c_str()
1057 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001058 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1059 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001060
1061 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1062 << mName.c_str() << ": event displayId should always be NONE.";
1063 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1064 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1065 }
1066
chaviwd1c23182019-12-20 18:44:56 -08001067 void assertNoEvents() {
1068 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001069 if (event == nullptr) {
1070 return;
1071 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001072 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001073 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1074 ADD_FAILURE() << "Received key event "
1075 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001076 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001077 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1078 ADD_FAILURE() << "Received motion event "
1079 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001080 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001081 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1082 ADD_FAILURE() << "Received focus event, hasFocus = "
1083 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001084 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001085 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1086 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1087 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001088 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001089 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1090 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1091 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001092 }
1093 FAIL() << mName.c_str()
1094 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001095 }
1096
1097 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1098
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001099 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1100
chaviwd1c23182019-12-20 18:44:56 -08001101protected:
1102 std::unique_ptr<InputConsumer> mConsumer;
1103 PreallocatedInputEventFactory mEventFactory;
1104
1105 std::string mName;
1106};
1107
chaviw3277faf2021-05-19 16:45:23 -05001108class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001109public:
1110 static const int32_t WIDTH = 600;
1111 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001112
Chris Yea209fde2020-07-22 13:54:51 -07001113 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001114 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001115 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001116 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001117 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001118 base::Result<std::unique_ptr<InputChannel>> channel =
1119 dispatcher->createInputChannel(name);
1120 token = (*channel)->getConnectionToken();
1121 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001122 }
1123
1124 inputApplicationHandle->updateInfo();
1125 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1126
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001127 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001128 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001129 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001130 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001131 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001132 mInfo.frameLeft = 0;
1133 mInfo.frameTop = 0;
1134 mInfo.frameRight = WIDTH;
1135 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001136 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001137 mInfo.globalScaleFactor = 1.0;
1138 mInfo.touchableRegion.clear();
1139 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001140 mInfo.ownerPid = WINDOW_PID;
1141 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001142 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001143 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001144 }
1145
Arthur Hungabbb9d82021-09-01 14:52:30 +00001146 sp<FakeWindowHandle> clone(
1147 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001148 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001149 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001150 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1151 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001152 return handle;
1153 }
1154
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001155 void setTouchable(bool touchable) {
1156 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1157 }
chaviwd1c23182019-12-20 18:44:56 -08001158
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001159 void setFocusable(bool focusable) {
1160 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1161 }
1162
1163 void setVisible(bool visible) {
1164 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1165 }
Vishnu Nair958da932020-08-21 17:12:37 -07001166
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001167 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001168 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001169 }
1170
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001171 void setPaused(bool paused) {
1172 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1173 }
1174
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001175 void setPreventSplitting(bool preventSplitting) {
1176 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001177 }
1178
1179 void setSlippery(bool slippery) {
1180 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1181 }
1182
1183 void setWatchOutsideTouch(bool watchOutside) {
1184 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1185 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001186
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001187 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1188
1189 void setInterceptsStylus(bool interceptsStylus) {
1190 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1191 }
1192
1193 void setDropInput(bool dropInput) {
1194 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1195 }
1196
1197 void setDropInputIfObscured(bool dropInputIfObscured) {
1198 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1199 }
1200
1201 void setNoInputChannel(bool noInputChannel) {
1202 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1203 }
1204
Josep del Riob3981622023-04-18 15:49:45 +00001205 void setDisableUserActivity(bool disableUserActivity) {
1206 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1207 }
1208
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001209 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1210
chaviw3277faf2021-05-19 16:45:23 -05001211 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001212
Bernardo Rufino7393d172021-02-26 13:56:11 +00001213 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1214
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001215 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001216 mInfo.frameLeft = frame.left;
1217 mInfo.frameTop = frame.top;
1218 mInfo.frameRight = frame.right;
1219 mInfo.frameBottom = frame.bottom;
1220 mInfo.touchableRegion.clear();
1221 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001222
1223 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1224 ui::Transform translate;
1225 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1226 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001227 }
1228
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001229 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1230
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001231 void setIsWallpaper(bool isWallpaper) {
1232 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1233 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001234
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001235 void setDupTouchToWallpaper(bool hasWallpaper) {
1236 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1237 }
chaviwd1c23182019-12-20 18:44:56 -08001238
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001239 void setTrustedOverlay(bool trustedOverlay) {
1240 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1241 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001242
chaviw9eaa22c2020-07-01 16:21:27 -07001243 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1244 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1245 }
1246
1247 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001248
yunho.shinf4a80b82020-11-16 21:13:57 +09001249 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1250
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001251 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001252 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001253 }
1254
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001255 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001256 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001257 }
1258
Svet Ganov5d3bc372020-01-26 23:11:07 -08001259 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001260 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001261 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1262 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001263 }
1264
1265 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001266 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001267 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1268 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001269 }
1270
1271 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001272 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001273 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1274 }
1275
1276 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1277 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001278 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001279 expectedFlags);
1280 }
1281
Svet Ganov5d3bc372020-01-26 23:11:07 -08001282 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001283 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1284 int32_t expectedFlags = 0) {
1285 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1286 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001287 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001288 }
1289
1290 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001291 int32_t expectedFlags = 0) {
1292 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1293 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001294 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001295 }
1296
1297 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001298 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001299 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001300 expectedFlags);
1301 }
1302
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001303 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1304 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001305 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001306 expectedFlags);
1307 }
1308
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001309 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1310 int32_t expectedFlags = 0) {
1311 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001312 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001313 ASSERT_EQ(InputEventType::MOTION, event->getType());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001314 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1315 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1316 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1317 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1318 }
1319
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001320 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1321 ASSERT_NE(mInputReceiver, nullptr)
1322 << "Cannot consume events from a window with no receiver";
1323 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1324 }
1325
Prabir Pradhan99987712020-11-10 18:43:05 -08001326 void consumeCaptureEvent(bool hasCapture) {
1327 ASSERT_NE(mInputReceiver, nullptr)
1328 << "Cannot consume events from a window with no receiver";
1329 mInputReceiver->consumeCaptureEvent(hasCapture);
1330 }
1331
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001332 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1333 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001334 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001335 ASSERT_THAT(*motionEvent, matcher);
1336 }
1337
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001338 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001339 std::optional<int32_t> expectedDisplayId,
1340 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001341 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1342 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1343 expectedFlags);
1344 }
1345
arthurhungb89ccb02020-12-30 16:19:01 +08001346 void consumeDragEvent(bool isExiting, float x, float y) {
1347 mInputReceiver->consumeDragEvent(isExiting, x, y);
1348 }
1349
Antonio Kantekf16f2832021-09-28 04:39:20 +00001350 void consumeTouchModeEvent(bool inTouchMode) {
1351 ASSERT_NE(mInputReceiver, nullptr)
1352 << "Cannot consume events from a window with no receiver";
1353 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1354 }
1355
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001356 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001357 if (mInputReceiver == nullptr) {
1358 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1359 return std::nullopt;
1360 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001361 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001362 }
1363
1364 void finishEvent(uint32_t sequenceNum) {
1365 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1366 mInputReceiver->finishEvent(sequenceNum);
1367 }
1368
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001369 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1370 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1371 mInputReceiver->sendTimeline(inputEventId, timeline);
1372 }
1373
chaviwaf87b3e2019-10-01 16:59:28 -07001374 InputEvent* consume() {
1375 if (mInputReceiver == nullptr) {
1376 return nullptr;
1377 }
1378 return mInputReceiver->consume();
1379 }
1380
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001381 MotionEvent* consumeMotion() {
1382 InputEvent* event = consume();
1383 if (event == nullptr) {
1384 ADD_FAILURE() << "Consume failed : no event";
1385 return nullptr;
1386 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001387 if (event->getType() != InputEventType::MOTION) {
1388 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001389 return nullptr;
1390 }
1391 return static_cast<MotionEvent*>(event);
1392 }
1393
Arthur Hungb92218b2018-08-14 12:00:21 +08001394 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001395 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001396 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001397 return; // Can't receive events if the window does not have input channel
1398 }
1399 ASSERT_NE(nullptr, mInputReceiver)
1400 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001401 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001402 }
1403
chaviwaf87b3e2019-10-01 16:59:28 -07001404 sp<IBinder> getToken() { return mInfo.token; }
1405
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001406 const std::string& getName() { return mName; }
1407
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001408 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1409 mInfo.ownerPid = ownerPid;
1410 mInfo.ownerUid = ownerUid;
1411 }
1412
Prabir Pradhanedd96402022-02-15 01:46:16 -08001413 int32_t getPid() const { return mInfo.ownerPid; }
1414
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001415 void destroyReceiver() { mInputReceiver = nullptr; }
1416
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001417 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1418
chaviwd1c23182019-12-20 18:44:56 -08001419private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001420 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001421 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001422 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001423};
1424
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001425std::atomic<int32_t> FakeWindowHandle::sId{1};
1426
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001427static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001428 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001429 int32_t displayId = ADISPLAY_ID_NONE,
1430 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001431 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001432 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1433 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001434 KeyEvent event;
1435 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1436
1437 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001438 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001439 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1440 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001441
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001442 if (!allowKeyRepeat) {
1443 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1444 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001445 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001446 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001447}
1448
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001449static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001450 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001451 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001452}
1453
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001454// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1455// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1456// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001457static InputEventInjectionResult injectKeyDownNoRepeat(
1458 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001459 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001460 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001461 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001462}
1463
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001464static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001465 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001466 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001467}
1468
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001469static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001470 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001471 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001472 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1473 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1474 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1475 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001476}
1477
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001478static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001479 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001480 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001481 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001482 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1483 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001484 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001485 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1486 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001487 MotionEventBuilder motionBuilder =
1488 MotionEventBuilder(action, source)
1489 .displayId(displayId)
1490 .eventTime(eventTime)
1491 .rawXCursorPosition(cursorPosition.x)
1492 .rawYCursorPosition(cursorPosition.y)
1493 .pointer(
1494 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1495 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1496 motionBuilder.downTime(eventTime);
1497 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001498
1499 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001500 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1501 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001502}
1503
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001504static InputEventInjectionResult injectMotionDown(
1505 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1506 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001507 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001508}
1509
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001510static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001511 int32_t source, int32_t displayId,
1512 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001513 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001514}
1515
Jackal Guof9696682018-10-05 12:23:23 +08001516static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1517 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1518 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001519 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001520 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1521 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001522
1523 return args;
1524}
1525
Josep del Riob3981622023-04-18 15:49:45 +00001526static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1527 int32_t displayId = ADISPLAY_ID_NONE) {
1528 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1529 // Define a valid key event.
1530 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1531 displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON,
1532 currentTime);
1533
1534 return args;
1535}
1536
1537static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1538 int32_t displayId = ADISPLAY_ID_NONE) {
1539 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1540 // Define a valid key event.
1541 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1542 displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT,
1543 AMETA_NONE, currentTime);
1544
1545 return args;
1546}
1547
Prabir Pradhan678438e2023-04-13 19:32:51 +00001548[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1549 int32_t displayId,
1550 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001551 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001552 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1553 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1554 }
1555
chaviwd1c23182019-12-20 18:44:56 -08001556 PointerProperties pointerProperties[pointerCount];
1557 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001558
chaviwd1c23182019-12-20 18:44:56 -08001559 for (size_t i = 0; i < pointerCount; i++) {
1560 pointerProperties[i].clear();
1561 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001562 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001563
chaviwd1c23182019-12-20 18:44:56 -08001564 pointerCoords[i].clear();
1565 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1566 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1567 }
Jackal Guof9696682018-10-05 12:23:23 +08001568
1569 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1570 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001571 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001572 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1573 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001574 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1575 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001576 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1577 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001578
1579 return args;
1580}
1581
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001582static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1583 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1584}
1585
chaviwd1c23182019-12-20 18:44:56 -08001586static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1587 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1588}
1589
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001590static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1591 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001592 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001593}
1594
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001595/**
1596 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1597 * broken channel.
1598 */
1599TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1600 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1601 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001602 sp<FakeWindowHandle>::make(application, mDispatcher,
1603 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001604
1605 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1606
1607 // Window closes its channel, but the window remains.
1608 window->destroyReceiver();
1609 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1610}
1611
Arthur Hungb92218b2018-08-14 12:00:21 +08001612TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001613 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001614 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1615 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001616
Arthur Hung72d8dc32020-03-28 00:48:39 +00001617 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001618 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1619 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1620 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001621
1622 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001623 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001624}
1625
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001626TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1627 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001628 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1629 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001630
1631 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1632 // Inject a MotionEvent to an unknown display.
1633 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1634 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1635 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1636
1637 // Window should receive motion event.
1638 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1639}
1640
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001641/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001642 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001643 * This test serves as a sanity check for the next test, where setInputWindows is
1644 * called twice.
1645 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001646TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001647 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001648 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1649 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001650 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001651
1652 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001653 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001654 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1655 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001656 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001657
1658 // Window should receive motion event.
1659 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1660}
1661
1662/**
1663 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001664 */
1665TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001666 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001667 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1668 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001669 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001670
1671 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1672 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001673 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001674 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1675 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001676 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001677
1678 // Window should receive motion event.
1679 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1680}
1681
Arthur Hungb92218b2018-08-14 12:00:21 +08001682// The foreground window should receive the first touch down event.
1683TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001684 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001685 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001686 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001687 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001688 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001689
Arthur Hung72d8dc32020-03-28 00:48:39 +00001690 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001691 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1692 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1693 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001694
1695 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001696 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001697 windowSecond->assertNoEvents();
1698}
1699
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001700/**
1701 * Two windows: A top window, and a wallpaper behind the window.
1702 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1703 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001704 * 1. foregroundWindow <-- dup touch to wallpaper
1705 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001706 */
1707TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1708 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1709 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001710 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001711 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001712 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001713 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001714 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001715
1716 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1717 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1718 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1719 {100, 200}))
1720 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1721
1722 // Both foreground window and its wallpaper should receive the touch down
1723 foregroundWindow->consumeMotionDown();
1724 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1725
1726 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1727 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1728 ADISPLAY_ID_DEFAULT, {110, 200}))
1729 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1730
1731 foregroundWindow->consumeMotionMove();
1732 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1733
1734 // Now the foreground window goes away, but the wallpaper stays
1735 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1736 foregroundWindow->consumeMotionCancel();
1737 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1738 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1739}
1740
1741/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001742 * Two fingers down on the window, and lift off the first finger.
1743 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1744 * contains a single pointer.
1745 */
1746TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1747 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1748 sp<FakeWindowHandle> window =
1749 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1750
1751 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001752 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001753 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1754 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1755 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001756 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001757 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1758 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1759 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1760 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001761 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001762 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1763 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1764 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1765 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001766 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1767 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1768 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1769
1770 // Remove the window. The gesture should be canceled
1771 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
1772 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1773 window->consumeMotionEvent(
1774 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1775}
1776
1777/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001778 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1779 * with the following differences:
1780 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1781 * clean up the connection.
1782 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1783 * Ensure that there's no crash in the dispatcher.
1784 */
1785TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1786 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1787 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001788 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001789 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001790 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001791 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001792 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001793
1794 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1795 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1796 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1797 {100, 200}))
1798 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1799
1800 // Both foreground window and its wallpaper should receive the touch down
1801 foregroundWindow->consumeMotionDown();
1802 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1803
1804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1805 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1806 ADISPLAY_ID_DEFAULT, {110, 200}))
1807 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1808
1809 foregroundWindow->consumeMotionMove();
1810 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1811
1812 // Wallpaper closes its channel, but the window remains.
1813 wallpaperWindow->destroyReceiver();
1814 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1815
1816 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1817 // is no longer valid.
1818 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1819 foregroundWindow->consumeMotionCancel();
1820}
1821
Arthur Hungc539dbb2022-12-08 07:45:36 +00001822class ShouldSplitTouchFixture : public InputDispatcherTest,
1823 public ::testing::WithParamInterface<bool> {};
1824INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1825 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001826/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001827 * A single window that receives touch (on top), and a wallpaper window underneath it.
1828 * The top window gets a multitouch gesture.
1829 * Ensure that wallpaper gets the same gesture.
1830 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00001831TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001832 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001833 sp<FakeWindowHandle> foregroundWindow =
1834 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1835 foregroundWindow->setDupTouchToWallpaper(true);
1836 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001837
1838 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001839 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001840 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001841
Arthur Hungc539dbb2022-12-08 07:45:36 +00001842 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001843
1844 // Touch down on top window
1845 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1846 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1847 {100, 100}))
1848 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1849
1850 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00001851 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001852 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1853
1854 // Second finger down on the top window
1855 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001856 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001857 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001858 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1859 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001860 .build();
1861 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1862 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1863 InputEventInjectionSync::WAIT_FOR_RESULT))
1864 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1865
Harry Cutts33476232023-01-30 19:57:29 +00001866 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
1867 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001868 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001869
1870 const MotionEvent secondFingerUpEvent =
1871 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1872 .displayId(ADISPLAY_ID_DEFAULT)
1873 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001874 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1875 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001876 .build();
1877 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1878 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
1879 InputEventInjectionSync::WAIT_FOR_RESULT))
1880 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1881 foregroundWindow->consumeMotionPointerUp(0);
1882 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1883
1884 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001885 injectMotionEvent(mDispatcher,
1886 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
1887 AINPUT_SOURCE_TOUCHSCREEN)
1888 .displayId(ADISPLAY_ID_DEFAULT)
1889 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1890 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001891 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001892 .x(100)
1893 .y(100))
1894 .build(),
1895 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001896 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1897 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1898 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001899}
1900
1901/**
1902 * Two windows: a window on the left and window on the right.
1903 * A third window, wallpaper, is behind both windows, and spans both top windows.
1904 * The first touch down goes to the left window. A second pointer touches down on the right window.
1905 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1906 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1907 * ACTION_POINTER_DOWN(1).
1908 */
1909TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1910 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1911 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001912 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001913 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001914 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001915
1916 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001917 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001918 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001919 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001920
1921 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001922 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001923 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001924 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001925
1926 mDispatcher->setInputWindows(
1927 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
1928
1929 // Touch down on left window
1930 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1931 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1932 {100, 100}))
1933 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1934
1935 // Both foreground window and its wallpaper should receive the touch down
1936 leftWindow->consumeMotionDown();
1937 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1938
1939 // Second finger down on the right window
1940 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001941 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001942 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001943 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1944 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001945 .build();
1946 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1947 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1948 InputEventInjectionSync::WAIT_FOR_RESULT))
1949 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1950
1951 leftWindow->consumeMotionMove();
1952 // Since the touch is split, right window gets ACTION_DOWN
1953 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00001954 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001955 expectedWallpaperFlags);
1956
1957 // Now, leftWindow, which received the first finger, disappears.
1958 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
1959 leftWindow->consumeMotionCancel();
1960 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1961 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1962
1963 // The pointer that's still down on the right window moves, and goes to the right window only.
1964 // As far as the dispatcher's concerned though, both pointers are still present.
1965 const MotionEvent secondFingerMoveEvent =
1966 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1967 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001968 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1969 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001970 .build();
1971 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1972 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
1973 InputEventInjectionSync::WAIT_FOR_RESULT));
1974 rightWindow->consumeMotionMove();
1975
1976 leftWindow->assertNoEvents();
1977 rightWindow->assertNoEvents();
1978 wallpaperWindow->assertNoEvents();
1979}
1980
Arthur Hungc539dbb2022-12-08 07:45:36 +00001981/**
1982 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
1983 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
1984 * The right window should receive ACTION_DOWN.
1985 */
1986TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00001987 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001988 sp<FakeWindowHandle> leftWindow =
1989 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1990 leftWindow->setFrame(Rect(0, 0, 200, 200));
1991 leftWindow->setDupTouchToWallpaper(true);
1992 leftWindow->setSlippery(true);
1993
1994 sp<FakeWindowHandle> rightWindow =
1995 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1996 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00001997
1998 sp<FakeWindowHandle> wallpaperWindow =
1999 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2000 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002001
Arthur Hungc539dbb2022-12-08 07:45:36 +00002002 mDispatcher->setInputWindows(
2003 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002004
Arthur Hungc539dbb2022-12-08 07:45:36 +00002005 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002006 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2007 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002008 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002009 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002010
2011 // Both foreground window and its wallpaper should receive the touch down
2012 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002013 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2014
Arthur Hungc539dbb2022-12-08 07:45:36 +00002015 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002016 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002017 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2018 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002019 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2020
Arthur Hungc539dbb2022-12-08 07:45:36 +00002021 leftWindow->consumeMotionCancel();
2022 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2023 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002024}
2025
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002026/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002027 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2028 * interactive, it might stop sending this flag.
2029 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2030 * to have a consistent input stream.
2031 *
2032 * Test procedure:
2033 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2034 * DOWN (new gesture).
2035 *
2036 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2037 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2038 *
2039 * We technically just need a single window here, but we are using two windows (spy on top and a
2040 * regular window below) to emulate the actual situation where it happens on the device.
2041 */
2042TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2043 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2044 sp<FakeWindowHandle> spyWindow =
2045 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2046 spyWindow->setFrame(Rect(0, 0, 200, 200));
2047 spyWindow->setTrustedOverlay(true);
2048 spyWindow->setSpy(true);
2049
2050 sp<FakeWindowHandle> window =
2051 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2052 window->setFrame(Rect(0, 0, 200, 200));
2053
2054 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2055 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002056
2057 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002058 mDispatcher->notifyMotion(
2059 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2060 .deviceId(touchDeviceId)
2061 .policyFlags(DEFAULT_POLICY_FLAGS)
2062 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2063 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002064
Prabir Pradhan678438e2023-04-13 19:32:51 +00002065 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2066 .deviceId(touchDeviceId)
2067 .policyFlags(DEFAULT_POLICY_FLAGS)
2068 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2069 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2070 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002071 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2072 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2073 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2074 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2075
2076 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002077 mDispatcher->notifyMotion(
2078 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2079 .deviceId(touchDeviceId)
2080 .policyFlags(0)
2081 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2082 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2083 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002084 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2085 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2086
2087 // We don't need to reset the device to reproduce the issue, but the reset event typically
2088 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002089 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002090
2091 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002092 mDispatcher->notifyMotion(
2093 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2094 .deviceId(touchDeviceId)
2095 .policyFlags(DEFAULT_POLICY_FLAGS)
2096 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2097 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002098 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2099 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2100
2101 // No more events
2102 spyWindow->assertNoEvents();
2103 window->assertNoEvents();
2104}
2105
2106/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002107 * Two windows: a window on the left and a window on the right.
2108 * Mouse is hovered from the right window into the left window.
2109 * Next, we tap on the left window, where the cursor was last seen.
2110 * The second tap is done onto the right window.
2111 * The mouse and tap are from two different devices.
2112 * We technically don't need to set the downtime / eventtime for these events, but setting these
2113 * explicitly helps during debugging.
2114 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2115 * In the buggy implementation, a tap on the right window would cause a crash.
2116 */
2117TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2118 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2119 sp<FakeWindowHandle> leftWindow =
2120 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2121 leftWindow->setFrame(Rect(0, 0, 200, 200));
2122
2123 sp<FakeWindowHandle> rightWindow =
2124 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2125 rightWindow->setFrame(Rect(200, 0, 400, 200));
2126
2127 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2128 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2129 // stale.
2130 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2131 const int32_t mouseDeviceId = 6;
2132 const int32_t touchDeviceId = 4;
2133 // Move the cursor from right
2134 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2135 injectMotionEvent(mDispatcher,
2136 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2137 AINPUT_SOURCE_MOUSE)
2138 .deviceId(mouseDeviceId)
2139 .downTime(baseTime + 10)
2140 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002141 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002142 .x(300)
2143 .y(100))
2144 .build()));
2145 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2146
2147 // .. to the left window
2148 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2149 injectMotionEvent(mDispatcher,
2150 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2151 AINPUT_SOURCE_MOUSE)
2152 .deviceId(mouseDeviceId)
2153 .downTime(baseTime + 10)
2154 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002155 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002156 .x(110)
2157 .y(100))
2158 .build()));
2159 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2160 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2161 // Now tap the left window
2162 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2163 injectMotionEvent(mDispatcher,
2164 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2165 AINPUT_SOURCE_TOUCHSCREEN)
2166 .deviceId(touchDeviceId)
2167 .downTime(baseTime + 40)
2168 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002169 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002170 .x(100)
2171 .y(100))
2172 .build()));
2173 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2174 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2175
2176 // release tap
2177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2178 injectMotionEvent(mDispatcher,
2179 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2180 AINPUT_SOURCE_TOUCHSCREEN)
2181 .deviceId(touchDeviceId)
2182 .downTime(baseTime + 40)
2183 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002184 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002185 .x(100)
2186 .y(100))
2187 .build()));
2188 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2189
2190 // Tap the window on the right
2191 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2192 injectMotionEvent(mDispatcher,
2193 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2194 AINPUT_SOURCE_TOUCHSCREEN)
2195 .deviceId(touchDeviceId)
2196 .downTime(baseTime + 60)
2197 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002198 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002199 .x(300)
2200 .y(100))
2201 .build()));
2202 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2203
2204 // release tap
2205 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2206 injectMotionEvent(mDispatcher,
2207 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2208 AINPUT_SOURCE_TOUCHSCREEN)
2209 .deviceId(touchDeviceId)
2210 .downTime(baseTime + 60)
2211 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002212 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002213 .x(300)
2214 .y(100))
2215 .build()));
2216 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2217
2218 // No more events
2219 leftWindow->assertNoEvents();
2220 rightWindow->assertNoEvents();
2221}
2222
2223/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002224 * Start hovering in a window. While this hover is still active, make another window appear on top.
2225 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2226 * While the top window is present, the hovering is stopped.
2227 * Later, hovering gets resumed again.
2228 * Ensure that new hover gesture is handled correctly.
2229 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2230 * to the window that's currently being hovered over.
2231 */
2232TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2233 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2234 sp<FakeWindowHandle> window =
2235 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2236 window->setFrame(Rect(0, 0, 200, 200));
2237
2238 // Only a single window is present at first
2239 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2240
2241 // Start hovering in the window
2242 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2243 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2244 .build());
2245 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2246
2247 // Now, an obscuring window appears!
2248 sp<FakeWindowHandle> obscuringWindow =
2249 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2250 ADISPLAY_ID_DEFAULT,
2251 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2252 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2253 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2254 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2255 obscuringWindow->setNoInputChannel(true);
2256 obscuringWindow->setFocusable(false);
2257 obscuringWindow->setAlpha(1.0);
2258 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2259
2260 // While this new obscuring window is present, the hovering is stopped
2261 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2262 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2263 .build());
2264 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2265
2266 // Now the obscuring window goes away.
2267 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2268
2269 // And a new hover gesture starts.
2270 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2271 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2272 .build());
2273 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2274}
2275
2276/**
2277 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2278 * the obscuring window.
2279 */
2280TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2281 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2282 sp<FakeWindowHandle> window =
2283 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2284 window->setFrame(Rect(0, 0, 200, 200));
2285
2286 // Only a single window is present at first
2287 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2288
2289 // Start hovering in the window
2290 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2291 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2292 .build());
2293 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2294
2295 // Now, an obscuring window appears!
2296 sp<FakeWindowHandle> obscuringWindow =
2297 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2298 ADISPLAY_ID_DEFAULT,
2299 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2300 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2301 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2302 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2303 obscuringWindow->setNoInputChannel(true);
2304 obscuringWindow->setFocusable(false);
2305 obscuringWindow->setAlpha(1.0);
2306 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2307
2308 // While this new obscuring window is present, the hovering continues. The event can't go to the
2309 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2310 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2311 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2312 .build());
2313 obscuringWindow->assertNoEvents();
2314 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2315
2316 // Now the obscuring window goes away.
2317 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2318
2319 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2320 // so it should generate a HOVER_ENTER
2321 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2322 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2323 .build());
2324 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2325
2326 // Now the MOVE should be getting dispatched normally
2327 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2328 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2329 .build());
2330 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2331}
2332
2333/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002334 * Two windows: a window on the left and a window on the right.
2335 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2336 * down. Then, on the left window, also place second touch pointer down.
2337 * This test tries to reproduce a crash.
2338 * In the buggy implementation, second pointer down on the left window would cause a crash.
2339 */
2340TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2341 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2342 sp<FakeWindowHandle> leftWindow =
2343 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2344 leftWindow->setFrame(Rect(0, 0, 200, 200));
2345
2346 sp<FakeWindowHandle> rightWindow =
2347 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2348 rightWindow->setFrame(Rect(200, 0, 400, 200));
2349
2350 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2351
2352 const int32_t touchDeviceId = 4;
2353 const int32_t mouseDeviceId = 6;
2354 NotifyMotionArgs args;
2355
2356 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002357 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2358 .deviceId(mouseDeviceId)
2359 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2360 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002361 leftWindow->consumeMotionEvent(
2362 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2363
2364 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002365 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2366 .deviceId(mouseDeviceId)
2367 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2368 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2369 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002370
2371 leftWindow->consumeMotionEvent(
2372 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2373 leftWindow->consumeMotionEvent(
2374 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2375
Prabir Pradhan678438e2023-04-13 19:32:51 +00002376 mDispatcher->notifyMotion(
2377 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2378 .deviceId(mouseDeviceId)
2379 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2380 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2381 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2382 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002383 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2384
2385 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002386 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2387 .deviceId(touchDeviceId)
2388 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2389 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002390 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2391
2392 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2393
2394 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002395 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2396 .deviceId(touchDeviceId)
2397 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2398 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2399 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002400 leftWindow->consumeMotionEvent(
2401 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2402 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2403 // current implementation.
2404 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2405 rightWindow->consumeMotionEvent(
2406 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2407
2408 leftWindow->assertNoEvents();
2409 rightWindow->assertNoEvents();
2410}
2411
2412/**
2413 * On a single window, use two different devices: mouse and touch.
2414 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2415 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2416 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2417 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2418 * represent a new gesture.
2419 */
2420TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2421 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2422 sp<FakeWindowHandle> window =
2423 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2424 window->setFrame(Rect(0, 0, 400, 400));
2425
2426 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2427
2428 const int32_t touchDeviceId = 4;
2429 const int32_t mouseDeviceId = 6;
2430 NotifyMotionArgs args;
2431
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002432 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002433 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2434 .deviceId(touchDeviceId)
2435 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2436 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002437 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002438 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2439 .deviceId(touchDeviceId)
2440 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2441 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2442 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002443 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002444 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2445 .deviceId(touchDeviceId)
2446 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2447 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2448 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002449 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2450 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2451 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2452
2453 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002454 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2455 .deviceId(mouseDeviceId)
2456 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2457 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2458 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002459
2460 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002461 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002462 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2463
Prabir Pradhan678438e2023-04-13 19:32:51 +00002464 mDispatcher->notifyMotion(
2465 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2466 .deviceId(mouseDeviceId)
2467 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2468 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2469 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2470 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002471 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2472
2473 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002474 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2475 .deviceId(touchDeviceId)
2476 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2477 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2478 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002479 // The pointer_down event should be ignored
2480 window->assertNoEvents();
2481}
2482
2483/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002484 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2485 * the injected event.
2486 */
2487TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2488 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2489 sp<FakeWindowHandle> window =
2490 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2491 window->setFrame(Rect(0, 0, 400, 400));
2492
2493 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2494
2495 const int32_t touchDeviceId = 4;
2496 NotifyMotionArgs args;
2497 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2498 // completion.
2499 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2500 injectMotionEvent(mDispatcher,
2501 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2502 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002503 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002504 .x(50)
2505 .y(50))
2506 .build()));
2507 window->consumeMotionEvent(
2508 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2509
2510 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2511 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002512 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2513 .deviceId(touchDeviceId)
2514 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2515 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002516
2517 window->consumeMotionEvent(
2518 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2519 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2520}
2521
2522/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002523 * This test is similar to the test above, but the sequence of injected events is different.
2524 *
2525 * Two windows: a window on the left and a window on the right.
2526 * Mouse is hovered over the left window.
2527 * Next, we tap on the left window, where the cursor was last seen.
2528 *
2529 * After that, we inject one finger down onto the right window, and then a second finger down onto
2530 * the left window.
2531 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2532 * window (first), and then another on the left window (second).
2533 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2534 * In the buggy implementation, second finger down on the left window would cause a crash.
2535 */
2536TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2537 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2538 sp<FakeWindowHandle> leftWindow =
2539 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2540 leftWindow->setFrame(Rect(0, 0, 200, 200));
2541
2542 sp<FakeWindowHandle> rightWindow =
2543 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2544 rightWindow->setFrame(Rect(200, 0, 400, 200));
2545
2546 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2547
2548 const int32_t mouseDeviceId = 6;
2549 const int32_t touchDeviceId = 4;
2550 // Hover over the left window. Keep the cursor there.
2551 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2552 injectMotionEvent(mDispatcher,
2553 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2554 AINPUT_SOURCE_MOUSE)
2555 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002556 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002557 .x(50)
2558 .y(50))
2559 .build()));
2560 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2561
2562 // Tap on left window
2563 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2564 injectMotionEvent(mDispatcher,
2565 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2566 AINPUT_SOURCE_TOUCHSCREEN)
2567 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002568 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002569 .x(100)
2570 .y(100))
2571 .build()));
2572
2573 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2574 injectMotionEvent(mDispatcher,
2575 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2576 AINPUT_SOURCE_TOUCHSCREEN)
2577 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002578 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002579 .x(100)
2580 .y(100))
2581 .build()));
2582 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2583 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2584 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2585
2586 // First finger down on right window
2587 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2588 injectMotionEvent(mDispatcher,
2589 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2590 AINPUT_SOURCE_TOUCHSCREEN)
2591 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002592 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002593 .x(300)
2594 .y(100))
2595 .build()));
2596 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2597
2598 // Second finger down on the left window
2599 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2600 injectMotionEvent(mDispatcher,
2601 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2602 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002603 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002604 .x(300)
2605 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002606 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002607 .x(100)
2608 .y(100))
2609 .build()));
2610 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2611 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2612
2613 // No more events
2614 leftWindow->assertNoEvents();
2615 rightWindow->assertNoEvents();
2616}
2617
2618/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002619 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2620 * While the touch is down, new hover events from the stylus device should be ignored. After the
2621 * touch is gone, stylus hovering should start working again.
2622 */
2623TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2624 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2625 sp<FakeWindowHandle> window =
2626 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2627 window->setFrame(Rect(0, 0, 200, 200));
2628
2629 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2630
2631 const int32_t stylusDeviceId = 5;
2632 const int32_t touchDeviceId = 4;
2633 // Start hovering with stylus
2634 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2635 injectMotionEvent(mDispatcher,
2636 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2637 AINPUT_SOURCE_STYLUS)
2638 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002639 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002640 .x(50)
2641 .y(50))
2642 .build()));
2643 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2644
2645 // Finger down on the window
2646 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2647 injectMotionEvent(mDispatcher,
2648 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2649 AINPUT_SOURCE_TOUCHSCREEN)
2650 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002651 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002652 .x(100)
2653 .y(100))
2654 .build()));
2655 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2656 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2657
2658 // Try to continue hovering with stylus. Since we are already down, injection should fail
2659 ASSERT_EQ(InputEventInjectionResult::FAILED,
2660 injectMotionEvent(mDispatcher,
2661 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2662 AINPUT_SOURCE_STYLUS)
2663 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002664 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002665 .x(50)
2666 .y(50))
2667 .build()));
2668 // No event should be sent. This event should be ignored because a pointer from another device
2669 // is already down.
2670
2671 // Lift up the finger
2672 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2673 injectMotionEvent(mDispatcher,
2674 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2675 AINPUT_SOURCE_TOUCHSCREEN)
2676 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002677 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002678 .x(100)
2679 .y(100))
2680 .build()));
2681 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2682
2683 // Now that the touch is gone, stylus hovering should start working again
2684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2685 injectMotionEvent(mDispatcher,
2686 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2687 AINPUT_SOURCE_STYLUS)
2688 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002689 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002690 .x(50)
2691 .y(50))
2692 .build()));
2693 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2694 // No more events
2695 window->assertNoEvents();
2696}
2697
2698/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002699 * A spy window above a window with no input channel.
2700 * Start hovering with a stylus device, and then tap with it.
2701 * Ensure spy window receives the entire sequence.
2702 */
2703TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2704 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2705 sp<FakeWindowHandle> spyWindow =
2706 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2707 spyWindow->setFrame(Rect(0, 0, 200, 200));
2708 spyWindow->setTrustedOverlay(true);
2709 spyWindow->setSpy(true);
2710 sp<FakeWindowHandle> window =
2711 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2712 window->setNoInputChannel(true);
2713 window->setFrame(Rect(0, 0, 200, 200));
2714
2715 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2716
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002717 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002718 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2719 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2720 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002721 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2722 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002723 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2724 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2725 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002726 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2727
2728 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002729 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2730 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2731 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002732 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2733
2734 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002735 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
2736 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2737 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002738 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2739
2740 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002741 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2742 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2743 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002744 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2745 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002746 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2747 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2748 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002749 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2750
2751 // No more events
2752 spyWindow->assertNoEvents();
2753 window->assertNoEvents();
2754}
2755
2756/**
2757 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
2758 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
2759 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
2760 * While the mouse is down, new move events from the touch device should be ignored.
2761 */
2762TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
2763 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2764 sp<FakeWindowHandle> spyWindow =
2765 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2766 spyWindow->setFrame(Rect(0, 0, 200, 200));
2767 spyWindow->setTrustedOverlay(true);
2768 spyWindow->setSpy(true);
2769 sp<FakeWindowHandle> window =
2770 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2771 window->setFrame(Rect(0, 0, 200, 200));
2772
2773 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2774
2775 const int32_t mouseDeviceId = 7;
2776 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002777
2778 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00002779 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2780 .deviceId(mouseDeviceId)
2781 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2782 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002783 spyWindow->consumeMotionEvent(
2784 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2785 window->consumeMotionEvent(
2786 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2787
2788 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00002789 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2790 .deviceId(touchDeviceId)
2791 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
2792 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002793 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2794 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2795 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2796 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2797
Prabir Pradhan678438e2023-04-13 19:32:51 +00002798 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2799 .deviceId(touchDeviceId)
2800 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
2801 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002802 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2803 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2804
2805 // Pilfer the stream
2806 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2807 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2808
Prabir Pradhan678438e2023-04-13 19:32:51 +00002809 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2810 .deviceId(touchDeviceId)
2811 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
2812 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002813 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2814
2815 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002816 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2817 .deviceId(mouseDeviceId)
2818 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2819 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2820 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002821
2822 spyWindow->consumeMotionEvent(
2823 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2824 spyWindow->consumeMotionEvent(
2825 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2826 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2827
Prabir Pradhan678438e2023-04-13 19:32:51 +00002828 mDispatcher->notifyMotion(
2829 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2830 .deviceId(mouseDeviceId)
2831 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2832 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2833 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2834 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002835 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2836 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2837
2838 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002839 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
2840 .deviceId(mouseDeviceId)
2841 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2842 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
2843 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002844 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2845 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2846
2847 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002848 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2849 .deviceId(touchDeviceId)
2850 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
2851 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002852
2853 // No more events
2854 spyWindow->assertNoEvents();
2855 window->assertNoEvents();
2856}
2857
2858/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002859 * On the display, have a single window, and also an area where there's no window.
2860 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2861 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2862 */
2863TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2864 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2865 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002866 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002867
2868 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002869
2870 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00002871 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002872
2873 mDispatcher->waitForIdle();
2874 window->assertNoEvents();
2875
2876 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002877 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002878 mDispatcher->waitForIdle();
2879 window->consumeMotionDown();
2880}
2881
2882/**
2883 * Same test as above, but instead of touching the empty space, the first touch goes to
2884 * non-touchable window.
2885 */
2886TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2887 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2888 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002889 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002890 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2891 window1->setTouchable(false);
2892 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002893 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002894 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2895
2896 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2897
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002898 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002899 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002900
2901 mDispatcher->waitForIdle();
2902 window1->assertNoEvents();
2903 window2->assertNoEvents();
2904
2905 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002906 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002907 mDispatcher->waitForIdle();
2908 window2->consumeMotionDown();
2909}
2910
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002911/**
2912 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2913 * to the event time of the first ACTION_DOWN sent to the particular window.
2914 */
2915TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2916 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2917 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002918 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002919 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2920 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002921 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002922 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2923
2924 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2925
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002926 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002927 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002928
2929 mDispatcher->waitForIdle();
2930 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002931 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002932 window2->assertNoEvents();
2933 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2934 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2935 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2936
2937 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002938 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002939 mDispatcher->waitForIdle();
2940 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002941 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002942 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2943 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2944 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2945 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2946
2947 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002948 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002949 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002950 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002951
2952 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002953 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002954 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002955 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002956
2957 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
2958 window1->consumeMotionMove();
2959 window1->assertNoEvents();
2960
2961 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002962 mDispatcher->notifyMotion(
2963 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002964 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002965 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002966
Prabir Pradhan678438e2023-04-13 19:32:51 +00002967 mDispatcher->notifyMotion(
2968 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002969 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002970 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002971}
2972
Garfield Tandf26e862020-07-01 20:18:19 -07002973TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002974 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002975 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002976 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002977 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002978 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002979 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002980 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002981
2982 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2983
2984 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
2985
2986 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002987 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002988 injectMotionEvent(mDispatcher,
2989 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2990 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002991 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07002992 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002993 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002994
2995 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002996 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002997 injectMotionEvent(mDispatcher,
2998 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2999 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003000 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003001 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003002 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3003 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003004
3005 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003006 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003007 injectMotionEvent(mDispatcher,
3008 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3009 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003010 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003011 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003012 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3013 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003014
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003015 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003016 injectMotionEvent(mDispatcher,
3017 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3018 AINPUT_SOURCE_MOUSE)
3019 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3020 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003021 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003022 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003023 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003024
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003025 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003026 injectMotionEvent(mDispatcher,
3027 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3028 AINPUT_SOURCE_MOUSE)
3029 .buttonState(0)
3030 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003031 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003032 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003033 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003034
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003035 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003036 injectMotionEvent(mDispatcher,
3037 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3038 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003039 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003040 .build()));
3041 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3042
3043 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003044 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003045 injectMotionEvent(mDispatcher,
3046 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3047 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003048 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003049 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003050 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003051
3052 // No more events
3053 windowLeft->assertNoEvents();
3054 windowRight->assertNoEvents();
3055}
3056
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003057/**
3058 * Put two fingers down (and don't release them) and click the mouse button.
3059 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3060 * currently active gesture should be canceled, and the new one should proceed.
3061 */
3062TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3063 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3064 sp<FakeWindowHandle> window =
3065 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3066 window->setFrame(Rect(0, 0, 600, 800));
3067
3068 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3069
3070 const int32_t touchDeviceId = 4;
3071 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003072
3073 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003074 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3075 .deviceId(touchDeviceId)
3076 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3077 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003078
Prabir Pradhan678438e2023-04-13 19:32:51 +00003079 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3080 .deviceId(touchDeviceId)
3081 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3082 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3083 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003084 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3085 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3086
3087 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003088 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3089 .deviceId(mouseDeviceId)
3090 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3091 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3092 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003093 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3094 WithPointerCount(2u)));
3095 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3096
Prabir Pradhan678438e2023-04-13 19:32:51 +00003097 mDispatcher->notifyMotion(
3098 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3099 .deviceId(mouseDeviceId)
3100 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3101 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3102 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3103 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003104 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3105
3106 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3107 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003108 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3109 .deviceId(touchDeviceId)
3110 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3111 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3112 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003113 window->assertNoEvents();
3114}
3115
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003116TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3117 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3118
3119 sp<FakeWindowHandle> spyWindow =
3120 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3121 spyWindow->setFrame(Rect(0, 0, 600, 800));
3122 spyWindow->setTrustedOverlay(true);
3123 spyWindow->setSpy(true);
3124 sp<FakeWindowHandle> window =
3125 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3126 window->setFrame(Rect(0, 0, 600, 800));
3127
3128 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3129 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3130
3131 // Send mouse cursor to the window
3132 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3133 injectMotionEvent(mDispatcher,
3134 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3135 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003136 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003137 .x(100)
3138 .y(100))
3139 .build()));
3140
3141 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3142 WithSource(AINPUT_SOURCE_MOUSE)));
3143 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3144 WithSource(AINPUT_SOURCE_MOUSE)));
3145
3146 window->assertNoEvents();
3147 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003148}
3149
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003150TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3151 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3152
3153 sp<FakeWindowHandle> spyWindow =
3154 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3155 spyWindow->setFrame(Rect(0, 0, 600, 800));
3156 spyWindow->setTrustedOverlay(true);
3157 spyWindow->setSpy(true);
3158 sp<FakeWindowHandle> window =
3159 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3160 window->setFrame(Rect(0, 0, 600, 800));
3161
3162 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3163 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3164
3165 // Send mouse cursor to the window
3166 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3167 injectMotionEvent(mDispatcher,
3168 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3169 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003170 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003171 .x(100)
3172 .y(100))
3173 .build()));
3174
3175 // Move mouse cursor
3176 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3177 injectMotionEvent(mDispatcher,
3178 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3179 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003180 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003181 .x(110)
3182 .y(110))
3183 .build()));
3184
3185 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3186 WithSource(AINPUT_SOURCE_MOUSE)));
3187 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3188 WithSource(AINPUT_SOURCE_MOUSE)));
3189 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3190 WithSource(AINPUT_SOURCE_MOUSE)));
3191 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3192 WithSource(AINPUT_SOURCE_MOUSE)));
3193 // Touch down on the window
3194 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3195 injectMotionEvent(mDispatcher,
3196 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3197 AINPUT_SOURCE_TOUCHSCREEN)
3198 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003199 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003200 .x(200)
3201 .y(200))
3202 .build()));
3203 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3204 WithSource(AINPUT_SOURCE_MOUSE)));
3205 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3206 WithSource(AINPUT_SOURCE_MOUSE)));
3207 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3208 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3209 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3210 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3211
3212 // pilfer the motion, retaining the gesture on the spy window.
3213 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3214 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3215 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3216
3217 // Touch UP on the window
3218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3219 injectMotionEvent(mDispatcher,
3220 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3221 AINPUT_SOURCE_TOUCHSCREEN)
3222 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003223 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003224 .x(200)
3225 .y(200))
3226 .build()));
3227 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3228 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3229
3230 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3231 // to send a new gesture. It should again go to both windows (spy and the window below), just
3232 // like the first gesture did, before pilfering. The window configuration has not changed.
3233
3234 // One more tap - DOWN
3235 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3236 injectMotionEvent(mDispatcher,
3237 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3238 AINPUT_SOURCE_TOUCHSCREEN)
3239 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003240 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003241 .x(250)
3242 .y(250))
3243 .build()));
3244 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3245 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3246 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3247 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3248
3249 // Touch UP on the window
3250 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3251 injectMotionEvent(mDispatcher,
3252 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3253 AINPUT_SOURCE_TOUCHSCREEN)
3254 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003255 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003256 .x(250)
3257 .y(250))
3258 .build()));
3259 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3260 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3261 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3262 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3263
3264 window->assertNoEvents();
3265 spyWindow->assertNoEvents();
3266}
3267
Garfield Tandf26e862020-07-01 20:18:19 -07003268// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3269// directly in this test.
3270TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003271 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003272 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003273 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003274 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003275
3276 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3277
3278 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3279
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003280 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003281 injectMotionEvent(mDispatcher,
3282 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3283 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003284 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003285 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003286 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003287 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003288 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003289 injectMotionEvent(mDispatcher,
3290 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3291 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003292 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003293 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003294 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3295 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003296
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003297 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003298 injectMotionEvent(mDispatcher,
3299 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3300 AINPUT_SOURCE_MOUSE)
3301 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3302 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003303 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003304 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003305 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003306
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003307 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003308 injectMotionEvent(mDispatcher,
3309 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3310 AINPUT_SOURCE_MOUSE)
3311 .buttonState(0)
3312 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003313 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003314 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003315 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003316
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003317 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003318 injectMotionEvent(mDispatcher,
3319 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3320 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003321 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003322 .build()));
3323 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3324
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07003325 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
3326 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
3327 ASSERT_EQ(InputEventInjectionResult::FAILED,
Garfield Tandf26e862020-07-01 20:18:19 -07003328 injectMotionEvent(mDispatcher,
3329 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3330 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003331 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003332 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003333 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003334}
3335
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003336/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003337 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3338 * is generated.
3339 */
3340TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3341 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3342 sp<FakeWindowHandle> window =
3343 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3344 window->setFrame(Rect(0, 0, 1200, 800));
3345
3346 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3347
3348 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3349
3350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3351 injectMotionEvent(mDispatcher,
3352 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3353 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003354 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003355 .x(300)
3356 .y(400))
3357 .build()));
3358 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3359
3360 // Remove the window, but keep the channel.
3361 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3362 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3363}
3364
3365/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003366 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3367 */
3368TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3369 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3370 sp<FakeWindowHandle> window =
3371 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3372 window->setFrame(Rect(0, 0, 100, 100));
3373
3374 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3375
3376 const int32_t mouseDeviceId = 7;
3377 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003378
3379 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003380 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3381 .deviceId(mouseDeviceId)
3382 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3383 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003384 window->consumeMotionEvent(
3385 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3386
3387 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003388 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3389 .deviceId(touchDeviceId)
3390 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3391 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003392
3393 window->consumeMotionEvent(
3394 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3395 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3396}
3397
3398/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003399 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003400 * The tap causes a HOVER_EXIT event to be generated because the current event
3401 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003402 */
3403TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3404 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3405 sp<FakeWindowHandle> window =
3406 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3407 window->setFrame(Rect(0, 0, 100, 100));
3408
3409 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3410
3411 // Inject a hover_move from mouse.
3412 NotifyMotionArgs motionArgs =
3413 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3414 ADISPLAY_ID_DEFAULT, {{50, 50}});
3415 motionArgs.xCursorPosition = 50;
3416 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003417 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003418 ASSERT_NO_FATAL_FAILURE(
3419 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3420 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003421
3422 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003423 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3424 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3425 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003426 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003427 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3428 WithSource(AINPUT_SOURCE_MOUSE))));
3429
3430 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003431 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3432 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3433
Prabir Pradhan678438e2023-04-13 19:32:51 +00003434 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3435 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003436 ASSERT_NO_FATAL_FAILURE(
3437 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3438 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3439}
3440
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003441TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3442 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3443 sp<FakeWindowHandle> windowDefaultDisplay =
3444 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3445 ADISPLAY_ID_DEFAULT);
3446 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3447 sp<FakeWindowHandle> windowSecondDisplay =
3448 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3449 SECOND_DISPLAY_ID);
3450 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3451
3452 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3453 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3454
3455 // Set cursor position in window in default display and check that hover enter and move
3456 // events are generated.
3457 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3458 injectMotionEvent(mDispatcher,
3459 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3460 AINPUT_SOURCE_MOUSE)
3461 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003462 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003463 .x(300)
3464 .y(600))
3465 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003466 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003467
3468 // Remove all windows in secondary display and check that no event happens on window in
3469 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003470 mDispatcher->setInputWindows(
3471 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003472 windowDefaultDisplay->assertNoEvents();
3473
3474 // Move cursor position in window in default display and check that only hover move
3475 // event is generated and not hover enter event.
3476 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3477 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3478 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3479 injectMotionEvent(mDispatcher,
3480 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3481 AINPUT_SOURCE_MOUSE)
3482 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003483 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003484 .x(400)
3485 .y(700))
3486 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003487 windowDefaultDisplay->consumeMotionEvent(
3488 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3489 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003490 windowDefaultDisplay->assertNoEvents();
3491}
3492
Garfield Tan00f511d2019-06-12 16:55:40 -07003493TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003494 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003495
3496 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003497 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003498 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003499 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003500 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003501 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003502
3503 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3504
Arthur Hung72d8dc32020-03-28 00:48:39 +00003505 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003506
3507 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3508 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003509 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003510 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003511 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003512 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003513 windowRight->assertNoEvents();
3514}
3515
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003516TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003517 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003518 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3519 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003520 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003521
Arthur Hung72d8dc32020-03-28 00:48:39 +00003522 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003523 setFocusedWindow(window);
3524
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003525 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003526
Prabir Pradhan678438e2023-04-13 19:32:51 +00003527 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003528
3529 // Window should receive key down event.
3530 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3531
3532 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3533 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003534 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003535 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003536 AKEY_EVENT_FLAG_CANCELED);
3537}
3538
3539TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003540 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003541 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3542 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003543
Arthur Hung72d8dc32020-03-28 00:48:39 +00003544 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003545
Prabir Pradhan678438e2023-04-13 19:32:51 +00003546 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3547 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003548
3549 // Window should receive motion down event.
3550 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3551
3552 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3553 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003554 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003555 window->consumeMotionEvent(
3556 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003557}
3558
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003559TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
3560 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3561 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3562 "Fake Window", ADISPLAY_ID_DEFAULT);
3563
3564 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3565
3566 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3567 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
3568 .build());
3569
3570 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3571
3572 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
3573 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
3574 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3575
3576 // After the device has been reset, a new hovering stream can be sent to the window
3577 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3578 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
3579 .build());
3580 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3581}
3582
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003583TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3584 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003585 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3586 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003587 window->setFocusable(true);
3588
3589 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3590 setFocusedWindow(window);
3591
3592 window->consumeFocusEvent(true);
3593
Prabir Pradhan678438e2023-04-13 19:32:51 +00003594 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003595 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3596 const nsecs_t injectTime = keyArgs.eventTime;
3597 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003598 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003599 // The dispatching time should be always greater than or equal to intercept key timeout.
3600 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3601 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3602 std::chrono::nanoseconds(interceptKeyTimeout).count());
3603}
3604
3605TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3606 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003607 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3608 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003609 window->setFocusable(true);
3610
3611 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3612 setFocusedWindow(window);
3613
3614 window->consumeFocusEvent(true);
3615
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003616 mFakePolicy->setInterceptKeyTimeout(150ms);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003617 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
3618 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003619
3620 // Window should receive key event immediately when same key up.
3621 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3622 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3623}
3624
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003625/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003626 * Two windows. First is a regular window. Second does not overlap with the first, and has
3627 * WATCH_OUTSIDE_TOUCH.
3628 * Both windows are owned by the same UID.
3629 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3630 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3631 */
3632TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3633 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3634 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3635 "First Window", ADISPLAY_ID_DEFAULT);
3636 window->setFrame(Rect{0, 0, 100, 100});
3637
3638 sp<FakeWindowHandle> outsideWindow =
3639 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3640 ADISPLAY_ID_DEFAULT);
3641 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3642 outsideWindow->setWatchOutsideTouch(true);
3643 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3644 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3645
3646 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003647 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3648 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3649 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003650 window->consumeMotionDown();
3651 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3652 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3653 outsideWindow->consumeMotionEvent(
3654 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3655}
3656
3657/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003658 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3659 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3660 * ACTION_OUTSIDE event is sent per gesture.
3661 */
3662TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3663 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3664 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003665 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3666 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003667 window->setWatchOutsideTouch(true);
3668 window->setFrame(Rect{0, 0, 100, 100});
3669 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003670 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3671 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003672 secondWindow->setFrame(Rect{100, 100, 200, 200});
3673 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003674 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3675 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003676 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3677 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3678
3679 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003680 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3681 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3682 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003683 window->assertNoEvents();
3684 secondWindow->assertNoEvents();
3685
3686 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3687 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003688 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3689 ADISPLAY_ID_DEFAULT,
3690 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003691 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3692 window->consumeMotionEvent(
3693 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003694 secondWindow->consumeMotionDown();
3695 thirdWindow->assertNoEvents();
3696
3697 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3698 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003699 mDispatcher->notifyMotion(
3700 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3701 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003702 window->assertNoEvents();
3703 secondWindow->consumeMotionMove();
3704 thirdWindow->consumeMotionDown();
3705}
3706
Prabir Pradhan814fe082022-07-22 20:22:18 +00003707TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3708 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003709 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3710 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003711 window->setFocusable(true);
3712
Patrick Williamsd828f302023-04-28 17:52:08 -05003713 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003714 setFocusedWindow(window);
3715
3716 window->consumeFocusEvent(true);
3717
Prabir Pradhan678438e2023-04-13 19:32:51 +00003718 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3719 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3720 mDispatcher->notifyKey(keyDown);
3721 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003722
3723 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3724 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3725
3726 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003727 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003728
3729 window->consumeFocusEvent(false);
3730
Prabir Pradhan678438e2023-04-13 19:32:51 +00003731 mDispatcher->notifyKey(keyDown);
3732 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003733 window->assertNoEvents();
3734}
3735
Arthur Hung96483742022-11-15 03:30:48 +00003736TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3737 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3738 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3739 "Fake Window", ADISPLAY_ID_DEFAULT);
3740 // Ensure window is non-split and have some transform.
3741 window->setPreventSplitting(true);
3742 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05003743 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00003744
3745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3746 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3747 {50, 50}))
3748 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3749 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3750
3751 const MotionEvent secondFingerDownEvent =
3752 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3753 .displayId(ADISPLAY_ID_DEFAULT)
3754 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003755 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3756 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003757 .build();
3758 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3759 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3760 InputEventInjectionSync::WAIT_FOR_RESULT))
3761 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3762
3763 const MotionEvent* event = window->consumeMotion();
3764 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3765 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3766 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3767 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3768 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3769}
3770
Harry Cuttsb166c002023-05-09 13:06:05 +00003771TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
3772 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3773 sp<FakeWindowHandle> window =
3774 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3775 window->setFrame(Rect(0, 0, 400, 400));
3776 sp<FakeWindowHandle> trustedOverlay =
3777 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
3778 ADISPLAY_ID_DEFAULT);
3779 trustedOverlay->setSpy(true);
3780 trustedOverlay->setTrustedOverlay(true);
3781
3782 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}});
3783
3784 // Start a three-finger touchpad swipe
3785 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3786 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3787 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3788 .build());
3789 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3790 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3791 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3792 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3793 .build());
3794 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3795 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3796 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3797 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3798 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3799 .build());
3800
3801 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3802 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3803 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
3804
3805 // Move the swipe a bit
3806 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3807 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3808 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3809 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3810 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3811 .build());
3812
3813 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3814
3815 // End the swipe
3816 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3817 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3818 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3819 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3820 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3821 .build());
3822 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3823 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3824 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3825 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3826 .build());
3827 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3828 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3829 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3830 .build());
3831
3832 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
3833 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
3834 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
3835
3836 window->assertNoEvents();
3837}
3838
3839TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
3840 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3841 sp<FakeWindowHandle> window =
3842 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3843 window->setFrame(Rect(0, 0, 400, 400));
3844 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3845
3846 // Start a three-finger touchpad swipe
3847 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3848 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3849 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3850 .build());
3851 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3852 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3853 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3854 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3855 .build());
3856 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3857 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3858 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3859 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3860 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3861 .build());
3862
3863 // Move the swipe a bit
3864 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3865 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3866 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3867 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3868 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3869 .build());
3870
3871 // End the swipe
3872 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3873 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3874 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3875 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3876 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3877 .build());
3878 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3879 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3880 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3881 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3882 .build());
3883 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3884 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3885 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3886 .build());
3887
3888 window->assertNoEvents();
3889}
3890
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003891/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003892 * Ensure the correct coordinate spaces are used by InputDispatcher.
3893 *
3894 * InputDispatcher works in the display space, so its coordinate system is relative to the display
3895 * panel. Windows get events in the window space, and get raw coordinates in the logical display
3896 * space.
3897 */
3898class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
3899public:
3900 void SetUp() override {
3901 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003902 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003903 }
3904
3905 void addDisplayInfo(int displayId, const ui::Transform& transform) {
3906 gui::DisplayInfo info;
3907 info.displayId = displayId;
3908 info.transform = transform;
3909 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05003910 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003911 }
3912
3913 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
3914 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05003915 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003916 }
3917
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003918 void removeAllWindowsAndDisplays() {
3919 mDisplayInfos.clear();
3920 mWindowInfos.clear();
3921 }
3922
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003923 // Set up a test scenario where the display has a scaled projection and there are two windows
3924 // on the display.
3925 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
3926 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
3927 // respectively.
3928 ui::Transform displayTransform;
3929 displayTransform.set(2, 0, 0, 4);
3930 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3931
3932 std::shared_ptr<FakeApplicationHandle> application =
3933 std::make_shared<FakeApplicationHandle>();
3934
3935 // Add two windows to the display. Their frames are represented in the display space.
3936 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003937 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3938 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003939 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
3940 addWindow(firstWindow);
3941
3942 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003943 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3944 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003945 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
3946 addWindow(secondWindow);
3947 return {std::move(firstWindow), std::move(secondWindow)};
3948 }
3949
3950private:
3951 std::vector<gui::DisplayInfo> mDisplayInfos;
3952 std::vector<gui::WindowInfo> mWindowInfos;
3953};
3954
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003955TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003956 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3957 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003958 // selected so that if the hit test was performed with the point and the bounds being in
3959 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003960 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3961 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3962 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003963
3964 firstWindow->consumeMotionDown();
3965 secondWindow->assertNoEvents();
3966}
3967
3968// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
3969// the event should be treated as being in the logical display space.
3970TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
3971 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3972 // Send down to the first window. The point is represented in the logical display space. The
3973 // point is selected so that if the hit test was done in logical display space, then it would
3974 // end up in the incorrect window.
3975 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3976 PointF{75 * 2, 55 * 4});
3977
3978 firstWindow->consumeMotionDown();
3979 secondWindow->assertNoEvents();
3980}
3981
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003982// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
3983// event should be treated as being in the logical display space.
3984TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
3985 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3986
3987 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
3988 ui::Transform injectedEventTransform;
3989 injectedEventTransform.set(matrix);
3990 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
3991 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
3992
3993 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3994 .displayId(ADISPLAY_ID_DEFAULT)
3995 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003996 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003997 .x(untransformedPoint.x)
3998 .y(untransformedPoint.y))
3999 .build();
4000 event.transform(matrix);
4001
4002 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4003 InputEventInjectionSync::WAIT_FOR_RESULT);
4004
4005 firstWindow->consumeMotionDown();
4006 secondWindow->assertNoEvents();
4007}
4008
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004009TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4010 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4011
4012 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004013 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4014 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4015 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004016
4017 firstWindow->assertNoEvents();
4018 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004019 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004020 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4021
4022 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4023 EXPECT_EQ(300, event->getRawX(0));
4024 EXPECT_EQ(880, event->getRawY(0));
4025
4026 // Ensure that the x and y values are in the window's coordinate space.
4027 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4028 // the logical display space. This will be the origin of the window space.
4029 EXPECT_EQ(100, event->getX(0));
4030 EXPECT_EQ(80, event->getY(0));
4031}
4032
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004033/** Ensure consistent behavior of InputDispatcher in all orientations. */
4034class InputDispatcherDisplayOrientationFixture
4035 : public InputDispatcherDisplayProjectionTest,
4036 public ::testing::WithParamInterface<ui::Rotation> {};
4037
4038// This test verifies the touchable region of a window for all rotations of the display by tapping
4039// in different locations on the display, specifically points close to the four corners of a
4040// window.
4041TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4042 constexpr static int32_t displayWidth = 400;
4043 constexpr static int32_t displayHeight = 800;
4044
4045 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4046
4047 const auto rotation = GetParam();
4048
4049 // Set up the display with the specified rotation.
4050 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4051 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4052 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4053 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4054 logicalDisplayWidth, logicalDisplayHeight);
4055 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4056
4057 // Create a window with its bounds determined in the logical display.
4058 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4059 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4060 sp<FakeWindowHandle> window =
4061 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4062 window->setFrame(frameInDisplay, displayTransform);
4063 addWindow(window);
4064
4065 // The following points in logical display space should be inside the window.
4066 static const std::array<vec2, 4> insidePoints{
4067 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4068 for (const auto pointInsideWindow : insidePoints) {
4069 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4070 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004071 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4072 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4073 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004074 window->consumeMotionDown();
4075
Prabir Pradhan678438e2023-04-13 19:32:51 +00004076 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4077 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4078 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004079 window->consumeMotionUp();
4080 }
4081
4082 // The following points in logical display space should be outside the window.
4083 static const std::array<vec2, 5> outsidePoints{
4084 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4085 for (const auto pointOutsideWindow : outsidePoints) {
4086 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4087 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004088 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4089 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4090 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004091
Prabir Pradhan678438e2023-04-13 19:32:51 +00004092 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4093 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4094 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004095 }
4096 window->assertNoEvents();
4097}
4098
4099// Run the precision tests for all rotations.
4100INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4101 InputDispatcherDisplayOrientationFixture,
4102 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4103 ui::ROTATION_270),
4104 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4105 return ftl::enum_string(testParamInfo.param);
4106 });
4107
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004108using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4109 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004110
4111class TransferTouchFixture : public InputDispatcherTest,
4112 public ::testing::WithParamInterface<TransferFunction> {};
4113
4114TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004115 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004116
4117 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004118 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004119 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4120 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004121 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004122 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004123 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4124 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004125 sp<FakeWindowHandle> wallpaper =
4126 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4127 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004128 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004129 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004130
4131 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004132 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4133 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004134
Svet Ganov5d3bc372020-01-26 23:11:07 -08004135 // Only the first window should get the down event
4136 firstWindow->consumeMotionDown();
4137 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004138 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004139
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004140 // Transfer touch to the second window
4141 TransferFunction f = GetParam();
4142 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4143 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004144 // The first window gets cancel and the second gets down
4145 firstWindow->consumeMotionCancel();
4146 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004147 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004148
4149 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004150 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4151 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004152 // The first window gets no events and the second gets up
4153 firstWindow->assertNoEvents();
4154 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004155 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004156}
4157
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004158/**
4159 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4160 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4161 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4162 * natural to the user.
4163 * In this test, we are sending a pointer to both spy window and first window. We then try to
4164 * transfer touch to the second window. The dispatcher should identify the first window as the
4165 * one that should lose the gesture, and therefore the action should be to move the gesture from
4166 * the first window to the second.
4167 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4168 * the other API, as well.
4169 */
4170TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4171 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4172
4173 // Create a couple of windows + a spy window
4174 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004175 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004176 spyWindow->setTrustedOverlay(true);
4177 spyWindow->setSpy(true);
4178 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004179 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004180 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004181 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004182
4183 // Add the windows to the dispatcher
4184 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4185
4186 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004187 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4188 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004189 // Only the first window and spy should get the down event
4190 spyWindow->consumeMotionDown();
4191 firstWindow->consumeMotionDown();
4192
4193 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4194 // if f === 'transferTouch'.
4195 TransferFunction f = GetParam();
4196 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4197 ASSERT_TRUE(success);
4198 // The first window gets cancel and the second gets down
4199 firstWindow->consumeMotionCancel();
4200 secondWindow->consumeMotionDown();
4201
4202 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004203 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4204 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004205 // The first window gets no events and the second+spy get up
4206 firstWindow->assertNoEvents();
4207 spyWindow->consumeMotionUp();
4208 secondWindow->consumeMotionUp();
4209}
4210
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004211TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004212 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004213
4214 PointF touchPoint = {10, 10};
4215
4216 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004217 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004218 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4219 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004220 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004221 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004222 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4223 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004224 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004225
4226 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004227 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004228
4229 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004230 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4231 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4232 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004233 // Only the first window should get the down event
4234 firstWindow->consumeMotionDown();
4235 secondWindow->assertNoEvents();
4236
4237 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004238 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4239 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004240 // Only the first window should get the pointer down event
4241 firstWindow->consumeMotionPointerDown(1);
4242 secondWindow->assertNoEvents();
4243
4244 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004245 TransferFunction f = GetParam();
4246 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4247 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004248 // The first window gets cancel and the second gets down and pointer down
4249 firstWindow->consumeMotionCancel();
4250 secondWindow->consumeMotionDown();
4251 secondWindow->consumeMotionPointerDown(1);
4252
4253 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004254 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4255 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004256 // The first window gets nothing and the second gets pointer up
4257 firstWindow->assertNoEvents();
4258 secondWindow->consumeMotionPointerUp(1);
4259
4260 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004261 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4262 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004263 // The first window gets nothing and the second gets up
4264 firstWindow->assertNoEvents();
4265 secondWindow->consumeMotionUp();
4266}
4267
Arthur Hungc539dbb2022-12-08 07:45:36 +00004268TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4269 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4270
4271 // Create a couple of windows
4272 sp<FakeWindowHandle> firstWindow =
4273 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4274 ADISPLAY_ID_DEFAULT);
4275 firstWindow->setDupTouchToWallpaper(true);
4276 sp<FakeWindowHandle> secondWindow =
4277 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4278 ADISPLAY_ID_DEFAULT);
4279 secondWindow->setDupTouchToWallpaper(true);
4280
4281 sp<FakeWindowHandle> wallpaper1 =
4282 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4283 wallpaper1->setIsWallpaper(true);
4284
4285 sp<FakeWindowHandle> wallpaper2 =
4286 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4287 wallpaper2->setIsWallpaper(true);
4288 // Add the windows to the dispatcher
4289 mDispatcher->setInputWindows(
4290 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4291
4292 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004293 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4294 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004295
4296 // Only the first window should get the down event
4297 firstWindow->consumeMotionDown();
4298 secondWindow->assertNoEvents();
4299 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4300 wallpaper2->assertNoEvents();
4301
4302 // Transfer touch focus to the second window
4303 TransferFunction f = GetParam();
4304 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4305 ASSERT_TRUE(success);
4306
4307 // The first window gets cancel and the second gets down
4308 firstWindow->consumeMotionCancel();
4309 secondWindow->consumeMotionDown();
4310 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4311 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4312
4313 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004314 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4315 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004316 // The first window gets no events and the second gets up
4317 firstWindow->assertNoEvents();
4318 secondWindow->consumeMotionUp();
4319 wallpaper1->assertNoEvents();
4320 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4321}
4322
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004323// For the cases of single pointer touch and two pointers non-split touch, the api's
4324// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4325// for the case where there are multiple pointers split across several windows.
4326INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4327 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004328 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4329 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004330 return dispatcher->transferTouch(destChannelToken,
4331 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004332 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004333 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4334 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004335 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004336 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004337 }));
4338
Svet Ganov5d3bc372020-01-26 23:11:07 -08004339TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004340 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004341
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004342 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004343 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4344 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004345 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004346
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004347 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004348 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4349 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004350 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004351
4352 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004353 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004354
4355 PointF pointInFirst = {300, 200};
4356 PointF pointInSecond = {300, 600};
4357
4358 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004359 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4360 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4361 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004362 // Only the first window should get the down event
4363 firstWindow->consumeMotionDown();
4364 secondWindow->assertNoEvents();
4365
4366 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004367 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4368 ADISPLAY_ID_DEFAULT,
4369 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004370 // The first window gets a move and the second a down
4371 firstWindow->consumeMotionMove();
4372 secondWindow->consumeMotionDown();
4373
4374 // Transfer touch focus to the second window
4375 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4376 // The first window gets cancel and the new gets pointer down (it already saw down)
4377 firstWindow->consumeMotionCancel();
4378 secondWindow->consumeMotionPointerDown(1);
4379
4380 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004381 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4382 ADISPLAY_ID_DEFAULT,
4383 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004384 // The first window gets nothing and the second gets pointer up
4385 firstWindow->assertNoEvents();
4386 secondWindow->consumeMotionPointerUp(1);
4387
4388 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004389 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4390 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004391 // The first window gets nothing and the second gets up
4392 firstWindow->assertNoEvents();
4393 secondWindow->consumeMotionUp();
4394}
4395
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004396// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4397// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4398// touch is not supported, so the touch should continue on those windows and the transferred-to
4399// window should get nothing.
4400TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4401 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4402
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004403 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004404 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4405 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004406 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004407
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004408 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004409 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4410 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004411 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004412
4413 // Add the windows to the dispatcher
4414 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4415
4416 PointF pointInFirst = {300, 200};
4417 PointF pointInSecond = {300, 600};
4418
4419 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004420 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4421 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4422 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004423 // Only the first window should get the down event
4424 firstWindow->consumeMotionDown();
4425 secondWindow->assertNoEvents();
4426
4427 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004428 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4429 ADISPLAY_ID_DEFAULT,
4430 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004431 // The first window gets a move and the second a down
4432 firstWindow->consumeMotionMove();
4433 secondWindow->consumeMotionDown();
4434
4435 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004436 const bool transferred =
4437 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004438 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4439 ASSERT_FALSE(transferred);
4440 firstWindow->assertNoEvents();
4441 secondWindow->assertNoEvents();
4442
4443 // The rest of the dispatch should proceed as normal
4444 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004445 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4446 ADISPLAY_ID_DEFAULT,
4447 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004448 // The first window gets MOVE and the second gets pointer up
4449 firstWindow->consumeMotionMove();
4450 secondWindow->consumeMotionUp();
4451
4452 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004453 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4454 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004455 // The first window gets nothing and the second gets up
4456 firstWindow->consumeMotionUp();
4457 secondWindow->assertNoEvents();
4458}
4459
Arthur Hungabbb9d82021-09-01 14:52:30 +00004460// This case will create two windows and one mirrored window on the default display and mirror
4461// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4462// the windows info of second display before default display.
4463TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4464 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4465 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004466 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004467 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004468 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004469 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004470 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004471
4472 sp<FakeWindowHandle> mirrorWindowInPrimary =
4473 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4474 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004475
4476 sp<FakeWindowHandle> firstWindowInSecondary =
4477 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4478 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004479
4480 sp<FakeWindowHandle> secondWindowInSecondary =
4481 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4482 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004483
4484 // Update window info, let it find window handle of second display first.
4485 mDispatcher->setInputWindows(
4486 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4487 {ADISPLAY_ID_DEFAULT,
4488 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4489
4490 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4491 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4492 {50, 50}))
4493 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4494
4495 // Window should receive motion event.
4496 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4497
4498 // Transfer touch focus
4499 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4500 secondWindowInPrimary->getToken()));
4501 // The first window gets cancel.
4502 firstWindowInPrimary->consumeMotionCancel();
4503 secondWindowInPrimary->consumeMotionDown();
4504
4505 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4506 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4507 ADISPLAY_ID_DEFAULT, {150, 50}))
4508 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4509 firstWindowInPrimary->assertNoEvents();
4510 secondWindowInPrimary->consumeMotionMove();
4511
4512 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4513 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4514 {150, 50}))
4515 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4516 firstWindowInPrimary->assertNoEvents();
4517 secondWindowInPrimary->consumeMotionUp();
4518}
4519
4520// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4521// 'transferTouch' api.
4522TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4523 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4524 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004525 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004526 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004527 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004528 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004529 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004530
4531 sp<FakeWindowHandle> mirrorWindowInPrimary =
4532 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4533 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004534
4535 sp<FakeWindowHandle> firstWindowInSecondary =
4536 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4537 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004538
4539 sp<FakeWindowHandle> secondWindowInSecondary =
4540 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4541 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004542
4543 // Update window info, let it find window handle of second display first.
4544 mDispatcher->setInputWindows(
4545 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4546 {ADISPLAY_ID_DEFAULT,
4547 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4548
4549 // Touch on second display.
4550 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4551 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4552 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4553
4554 // Window should receive motion event.
4555 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4556
4557 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004558 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004559
4560 // The first window gets cancel.
4561 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4562 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4563
4564 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4565 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4566 SECOND_DISPLAY_ID, {150, 50}))
4567 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4568 firstWindowInPrimary->assertNoEvents();
4569 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4570
4571 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4572 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4573 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4574 firstWindowInPrimary->assertNoEvents();
4575 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4576}
4577
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004578TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004579 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004580 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4581 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004582
Vishnu Nair47074b82020-08-14 11:54:47 -07004583 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004584 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004585 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004586
4587 window->consumeFocusEvent(true);
4588
Prabir Pradhan678438e2023-04-13 19:32:51 +00004589 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004590
4591 // Window should receive key down event.
4592 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004593
4594 // Should have poked user activity
4595 mFakePolicy->assertUserActivityPoked();
4596}
4597
4598TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4599 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4600 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4601 "Fake Window", ADISPLAY_ID_DEFAULT);
4602
4603 window->setDisableUserActivity(true);
4604 window->setFocusable(true);
4605 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4606 setFocusedWindow(window);
4607
4608 window->consumeFocusEvent(true);
4609
4610 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4611
4612 // Window should receive key down event.
4613 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4614
4615 // Should have poked user activity
4616 mFakePolicy->assertUserActivityNotPoked();
4617}
4618
4619TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4620 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4621 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4622 "Fake Window", ADISPLAY_ID_DEFAULT);
4623
4624 window->setFocusable(true);
4625 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4626 setFocusedWindow(window);
4627
4628 window->consumeFocusEvent(true);
4629
4630 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4631 mDispatcher->waitForIdle();
4632
4633 // System key is not passed down
4634 window->assertNoEvents();
4635
4636 // Should have poked user activity
4637 mFakePolicy->assertUserActivityPoked();
4638}
4639
4640TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4641 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4642 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4643 "Fake Window", ADISPLAY_ID_DEFAULT);
4644
4645 window->setFocusable(true);
4646 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4647 setFocusedWindow(window);
4648
4649 window->consumeFocusEvent(true);
4650
4651 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4652 mDispatcher->waitForIdle();
4653
4654 // System key is not passed down
4655 window->assertNoEvents();
4656
4657 // Should have poked user activity
4658 mFakePolicy->assertUserActivityPoked();
4659}
4660
4661TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4662 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4663 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4664 "Fake Window", ADISPLAY_ID_DEFAULT);
4665
4666 window->setDisableUserActivity(true);
4667 window->setFocusable(true);
4668 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4669 setFocusedWindow(window);
4670
4671 window->consumeFocusEvent(true);
4672
4673 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4674 mDispatcher->waitForIdle();
4675
4676 // System key is not passed down
4677 window->assertNoEvents();
4678
4679 // Should have poked user activity
4680 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004681}
4682
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004683TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
4684 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4685 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4686 "Fake Window", ADISPLAY_ID_DEFAULT);
4687
4688 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4689
4690 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4691 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4692 ADISPLAY_ID_DEFAULT, {100, 100}))
4693 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4694
4695 window->consumeMotionEvent(
4696 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
4697
4698 // Should have poked user activity
4699 mFakePolicy->assertUserActivityPoked();
4700}
4701
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004702TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004703 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004704 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4705 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004706
Arthur Hung72d8dc32020-03-28 00:48:39 +00004707 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004708
Prabir Pradhan678438e2023-04-13 19:32:51 +00004709 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004710 mDispatcher->waitForIdle();
4711
4712 window->assertNoEvents();
4713}
4714
4715// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4716TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004717 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004718 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4719 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004720
Arthur Hung72d8dc32020-03-28 00:48:39 +00004721 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004722
4723 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004724 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004725 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004726 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4727 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004728
4729 // Window should receive only the motion event
4730 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4731 window->assertNoEvents(); // Key event or focus event will not be received
4732}
4733
arthurhungea3f4fc2020-12-21 23:18:53 +08004734TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4735 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4736
arthurhungea3f4fc2020-12-21 23:18:53 +08004737 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004738 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4739 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004740 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004741
arthurhungea3f4fc2020-12-21 23:18:53 +08004742 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004743 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4744 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004745 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004746
4747 // Add the windows to the dispatcher
4748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4749
4750 PointF pointInFirst = {300, 200};
4751 PointF pointInSecond = {300, 600};
4752
4753 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004754 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4755 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4756 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004757 // Only the first window should get the down event
4758 firstWindow->consumeMotionDown();
4759 secondWindow->assertNoEvents();
4760
4761 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004762 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4763 ADISPLAY_ID_DEFAULT,
4764 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004765 // The first window gets a move and the second a down
4766 firstWindow->consumeMotionMove();
4767 secondWindow->consumeMotionDown();
4768
4769 // Send pointer cancel to the second window
4770 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004771 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004772 {pointInFirst, pointInSecond});
4773 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004774 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004775 // The first window gets move and the second gets cancel.
4776 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4777 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4778
4779 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004780 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4781 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08004782 // The first window gets up and the second gets nothing.
4783 firstWindow->consumeMotionUp();
4784 secondWindow->assertNoEvents();
4785}
4786
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004787TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
4788 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4789
4790 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004791 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004792 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4793 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
4794 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
4795 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
4796
Harry Cutts33476232023-01-30 19:57:29 +00004797 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004798 window->assertNoEvents();
4799 mDispatcher->waitForIdle();
4800}
4801
chaviwd1c23182019-12-20 18:44:56 -08004802class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00004803public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004804 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004805 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07004806 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004807 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07004808 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00004809 }
4810
chaviwd1c23182019-12-20 18:44:56 -08004811 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
4812
4813 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004814 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
4815 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08004816 }
4817
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004818 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
4819
4820 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
4821
chaviwd1c23182019-12-20 18:44:56 -08004822 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004823 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08004824 expectedDisplayId, expectedFlags);
4825 }
4826
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004827 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004828 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004829 expectedDisplayId, expectedFlags);
4830 }
4831
chaviwd1c23182019-12-20 18:44:56 -08004832 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004833 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08004834 expectedDisplayId, expectedFlags);
4835 }
4836
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004837 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004838 mInputReceiver->consumeMotionEvent(
4839 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4840 WithDisplayId(expectedDisplayId),
4841 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004842 }
4843
Arthur Hungfbfa5722021-11-16 02:45:54 +00004844 void consumeMotionPointerDown(int32_t pointerIdx) {
4845 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4846 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004847 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004848 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004849 }
4850
Evan Rosky84f07f02021-04-16 10:42:42 -07004851 MotionEvent* consumeMotion() {
4852 InputEvent* event = mInputReceiver->consume();
4853 if (!event) {
4854 ADD_FAILURE() << "No event was produced";
4855 return nullptr;
4856 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004857 if (event->getType() != InputEventType::MOTION) {
4858 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07004859 return nullptr;
4860 }
4861 return static_cast<MotionEvent*>(event);
4862 }
4863
chaviwd1c23182019-12-20 18:44:56 -08004864 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4865
4866private:
4867 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004868};
4869
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004870using InputDispatcherMonitorTest = InputDispatcherTest;
4871
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004872/**
4873 * Two entities that receive touch: A window, and a global monitor.
4874 * The touch goes to the window, and then the window disappears.
4875 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4876 * for the monitor, as well.
4877 * 1. foregroundWindow
4878 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4879 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004880TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004881 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4882 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004883 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004884
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004885 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004886
4887 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4888 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4889 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4890 {100, 200}))
4891 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4892
4893 // Both the foreground window and the global monitor should receive the touch down
4894 window->consumeMotionDown();
4895 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4896
4897 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4898 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4899 ADISPLAY_ID_DEFAULT, {110, 200}))
4900 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4901
4902 window->consumeMotionMove();
4903 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4904
4905 // Now the foreground window goes away
4906 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4907 window->consumeMotionCancel();
4908 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
4909
4910 // If more events come in, there will be no more foreground window to send them to. This will
4911 // cause a cancel for the monitor, as well.
4912 ASSERT_EQ(InputEventInjectionResult::FAILED,
4913 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4914 ADISPLAY_ID_DEFAULT, {120, 200}))
4915 << "Injection should fail because the window was removed";
4916 window->assertNoEvents();
4917 // Global monitor now gets the cancel
4918 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4919}
4920
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004921TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07004922 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004923 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4924 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004925 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004926
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004927 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004928
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004929 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004930 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004931 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00004932 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004933 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004934}
4935
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004936TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
4937 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004938
Chris Yea209fde2020-07-22 13:54:51 -07004939 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004940 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4941 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004942 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004943
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004944 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004945 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004946 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08004947 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004948 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004949
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004950 // Pilfer pointers from the monitor.
4951 // This should not do anything and the window should continue to receive events.
4952 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00004953
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004954 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004955 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4956 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004957 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004958
4959 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4960 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004961}
4962
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004963TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07004964 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004965 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4966 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004967 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4968 window->setWindowOffset(20, 40);
4969 window->setWindowTransform(0, 1, -1, 0);
4970
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004971 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004972
4973 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4974 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4975 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4976 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4977 MotionEvent* event = monitor.consumeMotion();
4978 // Even though window has transform, gesture monitor must not.
4979 ASSERT_EQ(ui::Transform(), event->getTransform());
4980}
4981
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004982TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00004983 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004984 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00004985
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004986 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00004987 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004988 << "Injection should fail if there is a monitor, but no touchable window";
4989 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00004990}
4991
chaviw81e2bb92019-12-18 15:03:51 -08004992TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004993 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004994 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4995 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08004996
Arthur Hung72d8dc32020-03-28 00:48:39 +00004997 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08004998
4999 NotifyMotionArgs motionArgs =
5000 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5001 ADISPLAY_ID_DEFAULT);
5002
Prabir Pradhan678438e2023-04-13 19:32:51 +00005003 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005004 // Window should receive motion down event.
5005 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5006
5007 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005008 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005009 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5010 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5011 motionArgs.pointerCoords[0].getX() - 10);
5012
Prabir Pradhan678438e2023-04-13 19:32:51 +00005013 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005014 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005015 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005016}
5017
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005018/**
5019 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5020 * the device default right away. In the test scenario, we check both the default value,
5021 * and the action of enabling / disabling.
5022 */
5023TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005024 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005025 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5026 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005027 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005028
5029 // Set focused application.
5030 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005031 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005032
5033 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005034 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005035 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005036 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005037
5038 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005039 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005040 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005041 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005042
5043 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005044 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005045 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005046 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005047 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005048 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005049 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005050 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005051
5052 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005053 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005054 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005055 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005056
5057 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005058 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005059 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005060 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005061 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005062 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005063 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005064 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005065
5066 window->assertNoEvents();
5067}
5068
Gang Wange9087892020-01-07 12:17:14 -05005069TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005070 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005071 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5072 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005073
5074 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005075 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005076
Arthur Hung72d8dc32020-03-28 00:48:39 +00005077 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005078 setFocusedWindow(window);
5079
Harry Cutts33476232023-01-30 19:57:29 +00005080 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005081
Prabir Pradhan678438e2023-04-13 19:32:51 +00005082 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5083 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005084
5085 InputEvent* event = window->consume();
5086 ASSERT_NE(event, nullptr);
5087
5088 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5089 ASSERT_NE(verified, nullptr);
5090 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5091
5092 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5093 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5094 ASSERT_EQ(keyArgs.source, verified->source);
5095 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5096
5097 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5098
5099 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005100 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005101 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005102 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5103 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5104 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5105 ASSERT_EQ(0, verifiedKey.repeatCount);
5106}
5107
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005108TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005109 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005110 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5111 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005112
5113 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5114
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005115 ui::Transform transform;
5116 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5117
5118 gui::DisplayInfo displayInfo;
5119 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5120 displayInfo.transform = transform;
5121
Patrick Williamsd828f302023-04-28 17:52:08 -05005122 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005123
Prabir Pradhan678438e2023-04-13 19:32:51 +00005124 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005125 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5126 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005127 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005128
5129 InputEvent* event = window->consume();
5130 ASSERT_NE(event, nullptr);
5131
5132 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5133 ASSERT_NE(verified, nullptr);
5134 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5135
5136 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5137 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5138 EXPECT_EQ(motionArgs.source, verified->source);
5139 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5140
5141 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5142
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005143 const vec2 rawXY =
5144 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5145 motionArgs.pointerCoords[0].getXYValue());
5146 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5147 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005148 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005149 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005150 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005151 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5152 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5153}
5154
chaviw09c8d2d2020-08-24 15:48:26 -07005155/**
5156 * Ensure that separate calls to sign the same data are generating the same key.
5157 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5158 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5159 * tests.
5160 */
5161TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5162 KeyEvent event = getTestKeyEvent();
5163 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5164
5165 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5166 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5167 ASSERT_EQ(hmac1, hmac2);
5168}
5169
5170/**
5171 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5172 */
5173TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5174 KeyEvent event = getTestKeyEvent();
5175 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5176 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5177
5178 verifiedEvent.deviceId += 1;
5179 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5180
5181 verifiedEvent.source += 1;
5182 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5183
5184 verifiedEvent.eventTimeNanos += 1;
5185 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5186
5187 verifiedEvent.displayId += 1;
5188 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5189
5190 verifiedEvent.action += 1;
5191 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5192
5193 verifiedEvent.downTimeNanos += 1;
5194 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5195
5196 verifiedEvent.flags += 1;
5197 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5198
5199 verifiedEvent.keyCode += 1;
5200 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5201
5202 verifiedEvent.scanCode += 1;
5203 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5204
5205 verifiedEvent.metaState += 1;
5206 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5207
5208 verifiedEvent.repeatCount += 1;
5209 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5210}
5211
Vishnu Nair958da932020-08-21 17:12:37 -07005212TEST_F(InputDispatcherTest, SetFocusedWindow) {
5213 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5214 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005215 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005216 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005217 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005218 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5219
5220 // Top window is also focusable but is not granted focus.
5221 windowTop->setFocusable(true);
5222 windowSecond->setFocusable(true);
5223 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5224 setFocusedWindow(windowSecond);
5225
5226 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005227 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5228 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005229
5230 // Focused window should receive event.
5231 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5232 windowTop->assertNoEvents();
5233}
5234
5235TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5236 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5237 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005238 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005239 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5240
5241 window->setFocusable(true);
5242 // Release channel for window is no longer valid.
5243 window->releaseChannel();
5244 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5245 setFocusedWindow(window);
5246
5247 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005248 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5249 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005250
5251 // window channel is invalid, so it should not receive any input event.
5252 window->assertNoEvents();
5253}
5254
5255TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5256 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5257 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005258 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005259 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005260 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5261
Vishnu Nair958da932020-08-21 17:12:37 -07005262 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5263 setFocusedWindow(window);
5264
5265 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005266 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5267 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005268
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005269 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005270 window->assertNoEvents();
5271}
5272
5273TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5274 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5275 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005276 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005277 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005278 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005279 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5280
5281 windowTop->setFocusable(true);
5282 windowSecond->setFocusable(true);
5283 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5284 setFocusedWindow(windowTop);
5285 windowTop->consumeFocusEvent(true);
5286
Chavi Weingarten847e8512023-03-29 00:26:09 +00005287 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5288 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005289 windowSecond->consumeFocusEvent(true);
5290 windowTop->consumeFocusEvent(false);
5291
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005292 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5293 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005294
5295 // Focused window should receive event.
5296 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5297}
5298
Chavi Weingarten847e8512023-03-29 00:26:09 +00005299TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005300 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5301 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005302 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005303 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005304 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005305 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5306
5307 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005308 windowSecond->setFocusable(false);
5309 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005310 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005311 setFocusedWindow(windowTop);
5312 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005313
Chavi Weingarten847e8512023-03-29 00:26:09 +00005314 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5315 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005316
5317 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005318 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005319 windowSecond->assertNoEvents();
5320}
5321
5322TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5323 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5324 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005325 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005326 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005327 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5328 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005329 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5330
5331 window->setFocusable(true);
5332 previousFocusedWindow->setFocusable(true);
5333 window->setVisible(false);
5334 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5335 setFocusedWindow(previousFocusedWindow);
5336 previousFocusedWindow->consumeFocusEvent(true);
5337
5338 // Requesting focus on invisible window takes focus from currently focused window.
5339 setFocusedWindow(window);
5340 previousFocusedWindow->consumeFocusEvent(false);
5341
5342 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005343 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005344 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5345 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005346
5347 // Window does not get focus event or key down.
5348 window->assertNoEvents();
5349
5350 // Window becomes visible.
5351 window->setVisible(true);
5352 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5353
5354 // Window receives focus event.
5355 window->consumeFocusEvent(true);
5356 // Focused window receives key down.
5357 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5358}
5359
Vishnu Nair599f1412021-06-21 10:39:58 -07005360TEST_F(InputDispatcherTest, DisplayRemoved) {
5361 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5362 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005363 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005364 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5365
5366 // window is granted focus.
5367 window->setFocusable(true);
5368 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5369 setFocusedWindow(window);
5370 window->consumeFocusEvent(true);
5371
5372 // When a display is removed window loses focus.
5373 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5374 window->consumeFocusEvent(false);
5375}
5376
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005377/**
5378 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5379 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5380 * of the 'slipperyEnterWindow'.
5381 *
5382 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5383 * a way so that the touched location is no longer covered by the top window.
5384 *
5385 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5386 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5387 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5388 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5389 * with ACTION_DOWN).
5390 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5391 * window moved itself away from the touched location and had Flag::SLIPPERY.
5392 *
5393 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5394 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5395 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5396 *
5397 * In this test, we ensure that the event received by the bottom window has
5398 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5399 */
5400TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005401 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5402 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005403
5404 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5405 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5406
5407 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005408 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005409 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005410 // Make sure this one overlaps the bottom window
5411 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5412 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5413 // one. Windows with the same owner are not considered to be occluding each other.
5414 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5415
5416 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005417 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005418 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5419
5420 mDispatcher->setInputWindows(
5421 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5422
5423 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005424 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5425 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5426 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005427 slipperyExitWindow->consumeMotionDown();
5428 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5429 mDispatcher->setInputWindows(
5430 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5431
Prabir Pradhan678438e2023-04-13 19:32:51 +00005432 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5433 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5434 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005435
5436 slipperyExitWindow->consumeMotionCancel();
5437
5438 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5439 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5440}
5441
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005442/**
5443 * Two windows, one on the left and another on the right. The left window is slippery. The right
5444 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5445 * touch moves from the left window into the right window, the gesture should continue to go to the
5446 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5447 * reproduces a crash.
5448 */
5449TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5450 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5451
5452 sp<FakeWindowHandle> leftSlipperyWindow =
5453 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5454 leftSlipperyWindow->setSlippery(true);
5455 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5456
5457 sp<FakeWindowHandle> rightDropTouchesWindow =
5458 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5459 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5460 rightDropTouchesWindow->setDropInput(true);
5461
5462 mDispatcher->setInputWindows(
5463 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5464
5465 // Start touch in the left window
5466 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5467 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5468 .build());
5469 leftSlipperyWindow->consumeMotionDown();
5470
5471 // And move it into the right window
5472 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5473 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5474 .build());
5475
5476 // Since the right window isn't eligible to receive input, touch does not slip.
5477 // The left window continues to receive the gesture.
5478 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5479 rightDropTouchesWindow->assertNoEvents();
5480}
5481
Garfield Tan1c7bc862020-01-28 13:24:04 -08005482class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5483protected:
5484 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5485 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5486
Chris Yea209fde2020-07-22 13:54:51 -07005487 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005488 sp<FakeWindowHandle> mWindow;
5489
5490 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005491 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005492 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005493 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005494 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005495 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5496 ASSERT_EQ(OK, mDispatcher->start());
5497
5498 setUpWindow();
5499 }
5500
5501 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005502 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005503 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005504
Vishnu Nair47074b82020-08-14 11:54:47 -07005505 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005506 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005507 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005508 mWindow->consumeFocusEvent(true);
5509 }
5510
Chris Ye2ad95392020-09-01 13:44:44 -07005511 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005512 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005513 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005514 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005515 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005516
5517 // Window should receive key down event.
5518 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5519 }
5520
5521 void expectKeyRepeatOnce(int32_t repeatCount) {
5522 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5523 InputEvent* repeatEvent = mWindow->consume();
5524 ASSERT_NE(nullptr, repeatEvent);
5525
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005526 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005527
5528 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5529 uint32_t eventAction = repeatKeyEvent->getAction();
5530 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5531 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5532 }
5533
Chris Ye2ad95392020-09-01 13:44:44 -07005534 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005535 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005536 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005537 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005538 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005539
5540 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005541 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005542 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005543 }
5544};
5545
5546TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005547 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005548 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5549 expectKeyRepeatOnce(repeatCount);
5550 }
5551}
5552
5553TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005554 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005555 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5556 expectKeyRepeatOnce(repeatCount);
5557 }
Harry Cutts33476232023-01-30 19:57:29 +00005558 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005559 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005560 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5561 expectKeyRepeatOnce(repeatCount);
5562 }
5563}
5564
5565TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005566 sendAndConsumeKeyDown(/*deviceId=*/1);
5567 expectKeyRepeatOnce(/*repeatCount=*/1);
5568 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005569 mWindow->assertNoEvents();
5570}
5571
5572TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005573 sendAndConsumeKeyDown(/*deviceId=*/1);
5574 expectKeyRepeatOnce(/*repeatCount=*/1);
5575 sendAndConsumeKeyDown(/*deviceId=*/2);
5576 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005577 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005578 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005579 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005580 expectKeyRepeatOnce(/*repeatCount=*/2);
5581 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005582 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005583 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005584 mWindow->assertNoEvents();
5585}
5586
5587TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005588 sendAndConsumeKeyDown(/*deviceId=*/1);
5589 expectKeyRepeatOnce(/*repeatCount=*/1);
5590 sendAndConsumeKeyDown(/*deviceId=*/2);
5591 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005592 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005593 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005594 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005595 mWindow->assertNoEvents();
5596}
5597
liushenxiang42232912021-05-21 20:24:09 +08005598TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5599 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005600 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005601 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005602 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5603 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5604 mWindow->assertNoEvents();
5605}
5606
Garfield Tan1c7bc862020-01-28 13:24:04 -08005607TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005608 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005609 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005610 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5611 InputEvent* repeatEvent = mWindow->consume();
5612 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5613 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5614 IdGenerator::getSource(repeatEvent->getId()));
5615 }
5616}
5617
5618TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005619 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005620 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005621
5622 std::unordered_set<int32_t> idSet;
5623 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5624 InputEvent* repeatEvent = mWindow->consume();
5625 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5626 int32_t id = repeatEvent->getId();
5627 EXPECT_EQ(idSet.end(), idSet.find(id));
5628 idSet.insert(id);
5629 }
5630}
5631
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005632/* Test InputDispatcher for MultiDisplay */
5633class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5634public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005635 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005636 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005637
Chris Yea209fde2020-07-22 13:54:51 -07005638 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005639 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005640 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005641
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005642 // Set focus window for primary display, but focused display would be second one.
5643 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005644 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005645 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005646 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005647 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005648
Chris Yea209fde2020-07-22 13:54:51 -07005649 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005650 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005651 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005652 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005653 // Set focus display to second one.
5654 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5655 // Set focus window for second display.
5656 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005657 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005658 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005659 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005660 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005661 }
5662
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005663 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005664 InputDispatcherTest::TearDown();
5665
Chris Yea209fde2020-07-22 13:54:51 -07005666 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005667 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005668 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005669 windowInSecondary.clear();
5670 }
5671
5672protected:
Chris Yea209fde2020-07-22 13:54:51 -07005673 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005674 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005675 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005676 sp<FakeWindowHandle> windowInSecondary;
5677};
5678
5679TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5680 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005681 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5682 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5683 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005684 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005685 windowInSecondary->assertNoEvents();
5686
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005687 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5689 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5690 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005691 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005692 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005693}
5694
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005695TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005696 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005697 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5698 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005699 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005700 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005701 windowInSecondary->assertNoEvents();
5702
5703 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005704 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005705 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005706 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005707 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005708
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005709 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005710 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005711
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005712 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005713 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005714 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005715
5716 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005717 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005718 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005719 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005720 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005721 windowInSecondary->assertNoEvents();
5722}
5723
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005724// Test per-display input monitors for motion event.
5725TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005726 FakeMonitorReceiver monitorInPrimary =
5727 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5728 FakeMonitorReceiver monitorInSecondary =
5729 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005730
5731 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005732 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5733 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5734 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005735 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005736 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005737 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005738 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005739
5740 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005741 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5742 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5743 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005744 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005745 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005746 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005747 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005748
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005749 // Lift up the touch from the second display
5750 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5751 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5752 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5753 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5754 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5755
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005756 // Test inject a non-pointer motion event.
5757 // If specific a display, it will dispatch to the focused window of particular display,
5758 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005759 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5760 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5761 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005762 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005763 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005764 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005765 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005766}
5767
5768// Test per-display input monitors for key event.
5769TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005770 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005771 FakeMonitorReceiver monitorInPrimary =
5772 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5773 FakeMonitorReceiver monitorInSecondary =
5774 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005775
5776 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005777 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5778 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005779 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005780 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005781 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005782 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005783}
5784
Vishnu Nair958da932020-08-21 17:12:37 -07005785TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
5786 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005787 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005788 secondWindowInPrimary->setFocusable(true);
5789 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
5790 setFocusedWindow(secondWindowInPrimary);
5791 windowInPrimary->consumeFocusEvent(false);
5792 secondWindowInPrimary->consumeFocusEvent(true);
5793
5794 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005795 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
5796 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005797 windowInPrimary->assertNoEvents();
5798 windowInSecondary->assertNoEvents();
5799 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5800}
5801
Arthur Hungdfd528e2021-12-08 13:23:04 +00005802TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
5803 FakeMonitorReceiver monitorInPrimary =
5804 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5805 FakeMonitorReceiver monitorInSecondary =
5806 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
5807
5808 // Test touch down on primary display.
5809 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5810 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5811 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5812 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5813 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5814
5815 // Test touch down on second display.
5816 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5817 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5818 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5819 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
5820 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
5821
5822 // Trigger cancel touch.
5823 mDispatcher->cancelCurrentTouch();
5824 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5825 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5826 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
5827 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
5828
5829 // Test inject a move motion event, no window/monitor should receive the event.
5830 ASSERT_EQ(InputEventInjectionResult::FAILED,
5831 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5832 ADISPLAY_ID_DEFAULT, {110, 200}))
5833 << "Inject motion event should return InputEventInjectionResult::FAILED";
5834 windowInPrimary->assertNoEvents();
5835 monitorInPrimary.assertNoEvents();
5836
5837 ASSERT_EQ(InputEventInjectionResult::FAILED,
5838 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5839 SECOND_DISPLAY_ID, {110, 200}))
5840 << "Inject motion event should return InputEventInjectionResult::FAILED";
5841 windowInSecondary->assertNoEvents();
5842 monitorInSecondary.assertNoEvents();
5843}
5844
Jackal Guof9696682018-10-05 12:23:23 +08005845class InputFilterTest : public InputDispatcherTest {
5846protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005847 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
5848 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08005849 NotifyMotionArgs motionArgs;
5850
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005851 motionArgs =
5852 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005853 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005854 motionArgs =
5855 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005856 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005857 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005858 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005859 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
5860 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08005861 } else {
5862 mFakePolicy->assertFilterInputEventWasNotCalled();
5863 }
5864 }
5865
5866 void testNotifyKey(bool expectToBeFiltered) {
5867 NotifyKeyArgs keyArgs;
5868
5869 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005870 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005871 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005872 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005873 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005874
5875 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08005876 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005877 } else {
5878 mFakePolicy->assertFilterInputEventWasNotCalled();
5879 }
5880 }
5881};
5882
5883// Test InputFilter for MotionEvent
5884TEST_F(InputFilterTest, MotionEvent_InputFilter) {
5885 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
5886 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5887 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5888
5889 // Enable InputFilter
5890 mDispatcher->setInputFilterEnabled(true);
5891 // Test touch on both primary and second display, and check if both events are filtered.
5892 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
5893 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
5894
5895 // Disable InputFilter
5896 mDispatcher->setInputFilterEnabled(false);
5897 // Test touch on both primary and second display, and check if both events aren't filtered.
5898 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5899 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5900}
5901
5902// Test InputFilter for KeyEvent
5903TEST_F(InputFilterTest, KeyEvent_InputFilter) {
5904 // Since the InputFilter is disabled by default, check if key event aren't filtered.
5905 testNotifyKey(/*expectToBeFiltered*/ false);
5906
5907 // Enable InputFilter
5908 mDispatcher->setInputFilterEnabled(true);
5909 // Send a key event, and check if it is filtered.
5910 testNotifyKey(/*expectToBeFiltered*/ true);
5911
5912 // Disable InputFilter
5913 mDispatcher->setInputFilterEnabled(false);
5914 // Send a key event, and check if it isn't filtered.
5915 testNotifyKey(/*expectToBeFiltered*/ false);
5916}
5917
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005918// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
5919// logical display coordinate space.
5920TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
5921 ui::Transform firstDisplayTransform;
5922 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5923 ui::Transform secondDisplayTransform;
5924 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
5925
5926 std::vector<gui::DisplayInfo> displayInfos(2);
5927 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
5928 displayInfos[0].transform = firstDisplayTransform;
5929 displayInfos[1].displayId = SECOND_DISPLAY_ID;
5930 displayInfos[1].transform = secondDisplayTransform;
5931
Patrick Williamsd828f302023-04-28 17:52:08 -05005932 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005933
5934 // Enable InputFilter
5935 mDispatcher->setInputFilterEnabled(true);
5936
5937 // Ensure the correct transforms are used for the displays.
5938 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
5939 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
5940}
5941
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005942class InputFilterInjectionPolicyTest : public InputDispatcherTest {
5943protected:
5944 virtual void SetUp() override {
5945 InputDispatcherTest::SetUp();
5946
5947 /**
5948 * We don't need to enable input filter to test the injected event policy, but we enabled it
5949 * here to make the tests more realistic, since this policy only matters when inputfilter is
5950 * on.
5951 */
5952 mDispatcher->setInputFilterEnabled(true);
5953
5954 std::shared_ptr<InputApplicationHandle> application =
5955 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005956 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
5957 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005958
5959 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5960 mWindow->setFocusable(true);
5961 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5962 setFocusedWindow(mWindow);
5963 mWindow->consumeFocusEvent(true);
5964 }
5965
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005966 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5967 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005968 KeyEvent event;
5969
5970 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5971 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
5972 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00005973 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005974 const int32_t additionalPolicyFlags =
5975 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
5976 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005977 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005978 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5979 policyFlags | additionalPolicyFlags));
5980
5981 InputEvent* received = mWindow->consume();
5982 ASSERT_NE(nullptr, received);
5983 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005984 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005985 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
5986 ASSERT_EQ(flags, keyEvent.getFlags());
5987 }
5988
5989 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5990 int32_t flags) {
5991 MotionEvent event;
5992 PointerProperties pointerProperties[1];
5993 PointerCoords pointerCoords[1];
5994 pointerProperties[0].clear();
5995 pointerProperties[0].id = 0;
5996 pointerCoords[0].clear();
5997 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
5998 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
5999
6000 ui::Transform identityTransform;
6001 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6002 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6003 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6004 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6005 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006006 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006007 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006008 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6009
6010 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6011 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006012 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006013 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6014 policyFlags | additionalPolicyFlags));
6015
6016 InputEvent* received = mWindow->consume();
6017 ASSERT_NE(nullptr, received);
6018 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006019 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006020 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6021 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006022 }
6023
6024private:
6025 sp<FakeWindowHandle> mWindow;
6026};
6027
6028TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006029 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6030 // filter. Without it, the event will no different from a regularly injected event, and the
6031 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006032 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6033 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006034}
6035
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006036TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006037 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006038 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006039 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6040}
6041
6042TEST_F(InputFilterInjectionPolicyTest,
6043 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6044 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006045 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006046 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006047}
6048
6049TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006050 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6051 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006052}
6053
chaviwfd6d3512019-03-25 13:23:49 -07006054class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006055 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006056 InputDispatcherTest::SetUp();
6057
Chris Yea209fde2020-07-22 13:54:51 -07006058 std::shared_ptr<FakeApplicationHandle> application =
6059 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006060 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006061 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006062 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006063
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006064 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006065 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006066 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006067
6068 // Set focused application.
6069 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006070 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006071
6072 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006073 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006074 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006075 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006076 }
6077
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006078 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006079 InputDispatcherTest::TearDown();
6080
6081 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006082 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006083 }
6084
6085protected:
6086 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006087 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006088 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006089};
6090
6091// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6092// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6093// the onPointerDownOutsideFocus callback.
6094TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006095 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006096 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6097 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006098 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006099 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006100
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006101 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006102 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6103}
6104
6105// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6106// DOWN on the window that doesn't have focus. Ensure no window received the
6107// onPointerDownOutsideFocus callback.
6108TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006109 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006110 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006111 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006112 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006113
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006114 ASSERT_TRUE(mDispatcher->waitForIdle());
6115 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006116}
6117
6118// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6119// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6120TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006121 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6122 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006123 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006124 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006125
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006126 ASSERT_TRUE(mDispatcher->waitForIdle());
6127 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006128}
6129
6130// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6131// DOWN on the window that already has focus. Ensure no window received the
6132// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006133TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006134 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006135 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006136 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006137 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006138 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006139
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006140 ASSERT_TRUE(mDispatcher->waitForIdle());
6141 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006142}
6143
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006144// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6145// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6146TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6147 const MotionEvent event =
6148 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6149 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006150 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006151 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6152 .build();
6153 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6154 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6155 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6156
6157 ASSERT_TRUE(mDispatcher->waitForIdle());
6158 mFakePolicy->assertOnPointerDownWasNotCalled();
6159 // Ensure that the unfocused window did not receive any FOCUS events.
6160 mUnfocusedWindow->assertNoEvents();
6161}
6162
chaviwaf87b3e2019-10-01 16:59:28 -07006163// These tests ensures we can send touch events to a single client when there are multiple input
6164// windows that point to the same client token.
6165class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6166 virtual void SetUp() override {
6167 InputDispatcherTest::SetUp();
6168
Chris Yea209fde2020-07-22 13:54:51 -07006169 std::shared_ptr<FakeApplicationHandle> application =
6170 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006171 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6172 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006173 mWindow1->setFrame(Rect(0, 0, 100, 100));
6174
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006175 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6176 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006177 mWindow2->setFrame(Rect(100, 100, 200, 200));
6178
Arthur Hung72d8dc32020-03-28 00:48:39 +00006179 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006180 }
6181
6182protected:
6183 sp<FakeWindowHandle> mWindow1;
6184 sp<FakeWindowHandle> mWindow2;
6185
6186 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006187 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006188 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6189 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006190 }
6191
6192 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6193 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006194 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006195 InputEvent* event = window->consume();
6196
6197 ASSERT_NE(nullptr, event) << name.c_str()
6198 << ": consumer should have returned non-NULL event.";
6199
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006200 ASSERT_EQ(InputEventType::MOTION, event->getType())
6201 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006202
6203 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006204 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006205 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006206
6207 for (size_t i = 0; i < points.size(); i++) {
6208 float expectedX = points[i].x;
6209 float expectedY = points[i].y;
6210
6211 EXPECT_EQ(expectedX, motionEvent.getX(i))
6212 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6213 << ", got " << motionEvent.getX(i);
6214 EXPECT_EQ(expectedY, motionEvent.getY(i))
6215 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6216 << ", got " << motionEvent.getY(i);
6217 }
6218 }
chaviw9eaa22c2020-07-01 16:21:27 -07006219
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006220 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006221 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006222 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6223 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006224
6225 // Always consume from window1 since it's the window that has the InputReceiver
6226 consumeMotionEvent(mWindow1, action, expectedPoints);
6227 }
chaviwaf87b3e2019-10-01 16:59:28 -07006228};
6229
6230TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6231 // Touch Window 1
6232 PointF touchedPoint = {10, 10};
6233 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006234 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006235
6236 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006237 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006238
6239 // Touch Window 2
6240 touchedPoint = {150, 150};
6241 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006242 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006243}
6244
chaviw9eaa22c2020-07-01 16:21:27 -07006245TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6246 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006247 mWindow2->setWindowScale(0.5f, 0.5f);
6248
6249 // Touch Window 1
6250 PointF touchedPoint = {10, 10};
6251 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006252 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006253 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006254 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006255
6256 // Touch Window 2
6257 touchedPoint = {150, 150};
6258 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006259 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6260 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006261
chaviw9eaa22c2020-07-01 16:21:27 -07006262 // Update the transform so rotation is set
6263 mWindow2->setWindowTransform(0, -1, 1, 0);
6264 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6265 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006266}
6267
chaviw9eaa22c2020-07-01 16:21:27 -07006268TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006269 mWindow2->setWindowScale(0.5f, 0.5f);
6270
6271 // Touch Window 1
6272 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6273 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006274 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006275
6276 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006277 touchedPoints.push_back(PointF{150, 150});
6278 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006279 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006280
chaviw9eaa22c2020-07-01 16:21:27 -07006281 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006282 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006283 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006284
chaviw9eaa22c2020-07-01 16:21:27 -07006285 // Update the transform so rotation is set for Window 2
6286 mWindow2->setWindowTransform(0, -1, 1, 0);
6287 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006288 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006289}
6290
chaviw9eaa22c2020-07-01 16:21:27 -07006291TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006292 mWindow2->setWindowScale(0.5f, 0.5f);
6293
6294 // Touch Window 1
6295 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6296 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006297 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006298
6299 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006300 touchedPoints.push_back(PointF{150, 150});
6301 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006302
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006303 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006304
6305 // Move both windows
6306 touchedPoints = {{20, 20}, {175, 175}};
6307 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6308 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6309
chaviw9eaa22c2020-07-01 16:21:27 -07006310 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006311
chaviw9eaa22c2020-07-01 16:21:27 -07006312 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006313 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006314 expectedPoints.pop_back();
6315
6316 // Touch Window 2
6317 mWindow2->setWindowTransform(0, -1, 1, 0);
6318 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006319 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006320
6321 // Move both windows
6322 touchedPoints = {{20, 20}, {175, 175}};
6323 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6324 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6325
6326 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006327}
6328
6329TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6330 mWindow1->setWindowScale(0.5f, 0.5f);
6331
6332 // Touch Window 1
6333 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6334 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006335 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006336
6337 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006338 touchedPoints.push_back(PointF{150, 150});
6339 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006340
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006341 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006342
6343 // Move both windows
6344 touchedPoints = {{20, 20}, {175, 175}};
6345 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6346 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6347
chaviw9eaa22c2020-07-01 16:21:27 -07006348 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006349}
6350
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006351/**
6352 * When one of the windows is slippery, the touch should not slip into the other window with the
6353 * same input channel.
6354 */
6355TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6356 mWindow1->setSlippery(true);
6357 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6358
6359 // Touch down in window 1
6360 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6361 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6362 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6363
6364 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6365 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6366 // getting generated.
6367 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6368 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6369
6370 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6371}
6372
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006373class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6374 virtual void SetUp() override {
6375 InputDispatcherTest::SetUp();
6376
Chris Yea209fde2020-07-22 13:54:51 -07006377 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006378 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006379 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6380 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006381 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006382 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006383 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006384
6385 // Set focused application.
6386 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6387
6388 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006389 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006390 mWindow->consumeFocusEvent(true);
6391 }
6392
6393 virtual void TearDown() override {
6394 InputDispatcherTest::TearDown();
6395 mWindow.clear();
6396 }
6397
6398protected:
Chris Yea209fde2020-07-22 13:54:51 -07006399 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006400 sp<FakeWindowHandle> mWindow;
6401 static constexpr PointF WINDOW_LOCATION = {20, 20};
6402
6403 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006404 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006405 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6406 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006407 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006408 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6409 WINDOW_LOCATION));
6410 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006411
6412 sp<FakeWindowHandle> addSpyWindow() {
6413 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006414 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006415 spy->setTrustedOverlay(true);
6416 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006417 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006418 spy->setDispatchingTimeout(30ms);
6419 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6420 return spy;
6421 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006422};
6423
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006424// Send a tap and respond, which should not cause an ANR.
6425TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6426 tapOnWindow();
6427 mWindow->consumeMotionDown();
6428 mWindow->consumeMotionUp();
6429 ASSERT_TRUE(mDispatcher->waitForIdle());
6430 mFakePolicy->assertNotifyAnrWasNotCalled();
6431}
6432
6433// Send a regular key and respond, which should not cause an ANR.
6434TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006435 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006436 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6437 ASSERT_TRUE(mDispatcher->waitForIdle());
6438 mFakePolicy->assertNotifyAnrWasNotCalled();
6439}
6440
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006441TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6442 mWindow->setFocusable(false);
6443 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6444 mWindow->consumeFocusEvent(false);
6445
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006446 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006447 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6448 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6449 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006450 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006451 // Key will not go to window because we have no focused window.
6452 // The 'no focused window' ANR timer should start instead.
6453
6454 // Now, the focused application goes away.
6455 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6456 // The key should get dropped and there should be no ANR.
6457
6458 ASSERT_TRUE(mDispatcher->waitForIdle());
6459 mFakePolicy->assertNotifyAnrWasNotCalled();
6460}
6461
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006462// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006463// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6464// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006465TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006466 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006467 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6468 WINDOW_LOCATION));
6469
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006470 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6471 ASSERT_TRUE(sequenceNum);
6472 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006473 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006474
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006475 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006476 mWindow->consumeMotionEvent(
6477 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006478 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006479 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006480}
6481
6482// Send a key to the app and have the app not respond right away.
6483TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6484 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006485 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006486 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6487 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006488 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006489 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006490 ASSERT_TRUE(mDispatcher->waitForIdle());
6491}
6492
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006493// We have a focused application, but no focused window
6494TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006495 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006496 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6497 mWindow->consumeFocusEvent(false);
6498
6499 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006500 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006501 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6502 WINDOW_LOCATION));
6503 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6504 mDispatcher->waitForIdle();
6505 mFakePolicy->assertNotifyAnrWasNotCalled();
6506
6507 // Once a focused event arrives, we get an ANR for this application
6508 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6509 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006510 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006511 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6512 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006513 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006514 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006515 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006516 ASSERT_TRUE(mDispatcher->waitForIdle());
6517}
6518
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006519/**
6520 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6521 * there will not be an ANR.
6522 */
6523TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6524 mWindow->setFocusable(false);
6525 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6526 mWindow->consumeFocusEvent(false);
6527
6528 KeyEvent event;
6529 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6530 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6531
6532 // Define a valid key down event that is stale (too old).
6533 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6534 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006535 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006536
6537 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6538
6539 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006540 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006541 InputEventInjectionSync::WAIT_FOR_RESULT,
6542 INJECT_EVENT_TIMEOUT, policyFlags);
6543 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6544 << "Injection should fail because the event is stale";
6545
6546 ASSERT_TRUE(mDispatcher->waitForIdle());
6547 mFakePolicy->assertNotifyAnrWasNotCalled();
6548 mWindow->assertNoEvents();
6549}
6550
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006551// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006552// Make sure that we don't notify policy twice about the same ANR.
6553TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006554 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006555 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6556 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006557
6558 // Once a focused event arrives, we get an ANR for this application
6559 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6560 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006561 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006562 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6563 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006564 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006565 const std::chrono::duration appTimeout =
6566 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6567 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006568
Vishnu Naire4df8752022-09-08 09:17:55 -07006569 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006570 // ANR should not be raised again. It is up to policy to do that if it desires.
6571 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006572
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006573 // If we now get a focused window, the ANR should stop, but the policy handles that via
6574 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006575 ASSERT_TRUE(mDispatcher->waitForIdle());
6576}
6577
6578// We have a focused application, but no focused window
6579TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006580 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6582 mWindow->consumeFocusEvent(false);
6583
6584 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006585 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006586 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006587 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6588 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006589
Vishnu Naire4df8752022-09-08 09:17:55 -07006590 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6591 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006592
6593 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006594 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006595 ASSERT_TRUE(mDispatcher->waitForIdle());
6596 mWindow->assertNoEvents();
6597}
6598
6599/**
6600 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6601 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6602 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6603 * the ANR mechanism should still work.
6604 *
6605 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6606 * DOWN event, while not responding on the second one.
6607 */
6608TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6609 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6610 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6611 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6612 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6613 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006614 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006615
6616 // Now send ACTION_UP, with identical timestamp
6617 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6618 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6619 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6620 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006621 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006622
6623 // We have now sent down and up. Let's consume first event and then ANR on the second.
6624 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6625 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006626 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006627}
6628
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006629// A spy window can receive an ANR
6630TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6631 sp<FakeWindowHandle> spy = addSpyWindow();
6632
6633 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6634 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6635 WINDOW_LOCATION));
6636 mWindow->consumeMotionDown();
6637
6638 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6639 ASSERT_TRUE(sequenceNum);
6640 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006641 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006642
6643 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006644 spy->consumeMotionEvent(
6645 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006646 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006647 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006648}
6649
6650// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006651// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006652TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6653 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006654
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006655 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6656 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006657 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006658 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006659
6660 // Stuck on the ACTION_UP
6661 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006662 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006663
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006664 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006665 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006666 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6667 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006668
6669 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6670 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006671 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006672 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006673 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006674}
6675
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006676// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006677// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006678TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6679 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006680
6681 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006682 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6683 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006684
6685 mWindow->consumeMotionDown();
6686 // Stuck on the ACTION_UP
6687 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006688 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006689
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006690 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006691 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006692 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6693 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006694
6695 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6696 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006697 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006698 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006699 spy->assertNoEvents();
6700}
6701
6702TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6703 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6704
6705 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6706
6707 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6708 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6709 WINDOW_LOCATION));
6710
6711 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6712 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6713 ASSERT_TRUE(consumeSeq);
6714
Prabir Pradhanedd96402022-02-15 01:46:16 -08006715 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006716
6717 monitor.finishEvent(*consumeSeq);
6718 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6719
6720 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006721 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006722}
6723
6724// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6725// process events, you don't get an anr. When the window later becomes unresponsive again, you
6726// get an ANR again.
6727// 1. tap -> block on ACTION_UP -> receive ANR
6728// 2. consume all pending events (= queue becomes healthy again)
6729// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6730TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6731 tapOnWindow();
6732
6733 mWindow->consumeMotionDown();
6734 // Block on ACTION_UP
6735 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006736 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006737 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6738 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006739 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006740 mWindow->assertNoEvents();
6741
6742 tapOnWindow();
6743 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006744 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006745 mWindow->consumeMotionUp();
6746
6747 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006748 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006749 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006750 mWindow->assertNoEvents();
6751}
6752
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006753// If a connection remains unresponsive for a while, make sure policy is only notified once about
6754// it.
6755TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006756 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006757 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6758 WINDOW_LOCATION));
6759
6760 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006761 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006762 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006763 // 'notifyConnectionUnresponsive' should only be called once per connection
6764 mFakePolicy->assertNotifyAnrWasNotCalled();
6765 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006766 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006767 mWindow->consumeMotionEvent(
6768 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006769 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006770 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006771 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006772 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006773}
6774
6775/**
6776 * If a window is processing a motion event, and then a key event comes in, the key event should
6777 * not to to the focused window until the motion is processed.
6778 *
6779 * Warning!!!
6780 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6781 * and the injection timeout that we specify when injecting the key.
6782 * We must have the injection timeout (10ms) be smaller than
6783 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6784 *
6785 * If that value changes, this test should also change.
6786 */
6787TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
6788 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6789 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6790
6791 tapOnWindow();
6792 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6793 ASSERT_TRUE(downSequenceNum);
6794 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6795 ASSERT_TRUE(upSequenceNum);
6796 // Don't finish the events yet, and send a key
6797 // Injection will "succeed" because we will eventually give up and send the key to the focused
6798 // window even if motions are still being processed. But because the injection timeout is short,
6799 // we will receive INJECTION_TIMED_OUT as the result.
6800
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006801 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006802 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006803 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6804 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006805 // Key will not be sent to the window, yet, because the window is still processing events
6806 // and the key remains pending, waiting for the touch events to be processed
6807 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6808 ASSERT_FALSE(keySequenceNum);
6809
6810 std::this_thread::sleep_for(500ms);
6811 // if we wait long enough though, dispatcher will give up, and still send the key
6812 // to the focused window, even though we have not yet finished the motion event
6813 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6814 mWindow->finishEvent(*downSequenceNum);
6815 mWindow->finishEvent(*upSequenceNum);
6816}
6817
6818/**
6819 * If a window is processing a motion event, and then a key event comes in, the key event should
6820 * not go to the focused window until the motion is processed.
6821 * If then a new motion comes in, then the pending key event should be going to the currently
6822 * focused window right away.
6823 */
6824TEST_F(InputDispatcherSingleWindowAnr,
6825 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
6826 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6827 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6828
6829 tapOnWindow();
6830 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6831 ASSERT_TRUE(downSequenceNum);
6832 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6833 ASSERT_TRUE(upSequenceNum);
6834 // Don't finish the events yet, and send a key
6835 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006836 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006837 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6838 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006839 // At this point, key is still pending, and should not be sent to the application yet.
6840 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6841 ASSERT_FALSE(keySequenceNum);
6842
6843 // Now tap down again. It should cause the pending key to go to the focused window right away.
6844 tapOnWindow();
6845 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
6846 // the other events yet. We can finish events in any order.
6847 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
6848 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
6849 mWindow->consumeMotionDown();
6850 mWindow->consumeMotionUp();
6851 mWindow->assertNoEvents();
6852}
6853
6854class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
6855 virtual void SetUp() override {
6856 InputDispatcherTest::SetUp();
6857
Chris Yea209fde2020-07-22 13:54:51 -07006858 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006859 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006860 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
6861 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006862 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006863 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006864 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006865
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006866 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
6867 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006868 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006869 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006870
6871 // Set focused application.
6872 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07006873 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006874
6875 // Expect one focus window exist in display.
6876 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006877 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006878 mFocusedWindow->consumeFocusEvent(true);
6879 }
6880
6881 virtual void TearDown() override {
6882 InputDispatcherTest::TearDown();
6883
6884 mUnfocusedWindow.clear();
6885 mFocusedWindow.clear();
6886 }
6887
6888protected:
Chris Yea209fde2020-07-22 13:54:51 -07006889 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006890 sp<FakeWindowHandle> mUnfocusedWindow;
6891 sp<FakeWindowHandle> mFocusedWindow;
6892 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
6893 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
6894 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
6895
6896 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
6897
6898 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
6899
6900private:
6901 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006902 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006903 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6904 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006905 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006906 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6907 location));
6908 }
6909};
6910
6911// If we have 2 windows that are both unresponsive, the one with the shortest timeout
6912// should be ANR'd first.
6913TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006914 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006915 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6916 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006917 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006918 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006919 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006920 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006921 // We consumed all events, so no ANR
6922 ASSERT_TRUE(mDispatcher->waitForIdle());
6923 mFakePolicy->assertNotifyAnrWasNotCalled();
6924
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006925 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006926 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6927 FOCUSED_WINDOW_LOCATION));
6928 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
6929 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006930
6931 const std::chrono::duration timeout =
6932 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006933 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006934 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
6935 // sequence to make it consistent
6936 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006937 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006938 mFocusedWindow->consumeMotionDown();
6939 // This cancel is generated because the connection was unresponsive
6940 mFocusedWindow->consumeMotionCancel();
6941 mFocusedWindow->assertNoEvents();
6942 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006943 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006944 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6945 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006946 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006947}
6948
6949// If we have 2 windows with identical timeouts that are both unresponsive,
6950// it doesn't matter which order they should have ANR.
6951// But we should receive ANR for both.
6952TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
6953 // Set the timeout for unfocused window to match the focused window
6954 mUnfocusedWindow->setDispatchingTimeout(10ms);
6955 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6956
6957 tapOnFocusedWindow();
6958 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08006959 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
6960 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
6961 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006962
6963 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006964 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
6965 mFocusedWindow->getToken() == anrConnectionToken2);
6966 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
6967 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006968
6969 ASSERT_TRUE(mDispatcher->waitForIdle());
6970 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006971
6972 mFocusedWindow->consumeMotionDown();
6973 mFocusedWindow->consumeMotionUp();
6974 mUnfocusedWindow->consumeMotionOutside();
6975
Prabir Pradhanedd96402022-02-15 01:46:16 -08006976 sp<IBinder> responsiveToken1, responsiveToken2;
6977 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
6978 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006979
6980 // Both applications should be marked as responsive, in any order
6981 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
6982 mFocusedWindow->getToken() == responsiveToken2);
6983 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
6984 mUnfocusedWindow->getToken() == responsiveToken2);
6985 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006986}
6987
6988// If a window is already not responding, the second tap on the same window should be ignored.
6989// We should also log an error to account for the dropped event (not tested here).
6990// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
6991TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
6992 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006993 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006994 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006995 // Receive the events, but don't respond
6996 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
6997 ASSERT_TRUE(downEventSequenceNum);
6998 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
6999 ASSERT_TRUE(upEventSequenceNum);
7000 const std::chrono::duration timeout =
7001 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007002 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007003
7004 // Tap once again
7005 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007006 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007007 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7008 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007009 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007010 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7011 FOCUSED_WINDOW_LOCATION));
7012 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7013 // valid touch target
7014 mUnfocusedWindow->assertNoEvents();
7015
7016 // Consume the first tap
7017 mFocusedWindow->finishEvent(*downEventSequenceNum);
7018 mFocusedWindow->finishEvent(*upEventSequenceNum);
7019 ASSERT_TRUE(mDispatcher->waitForIdle());
7020 // The second tap did not go to the focused window
7021 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007022 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007023 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7024 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007025 mFakePolicy->assertNotifyAnrWasNotCalled();
7026}
7027
7028// If you tap outside of all windows, there will not be ANR
7029TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007030 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007031 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7032 LOCATION_OUTSIDE_ALL_WINDOWS));
7033 ASSERT_TRUE(mDispatcher->waitForIdle());
7034 mFakePolicy->assertNotifyAnrWasNotCalled();
7035}
7036
7037// Since the focused window is paused, tapping on it should not produce any events
7038TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7039 mFocusedWindow->setPaused(true);
7040 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7041
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007042 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007043 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7044 FOCUSED_WINDOW_LOCATION));
7045
7046 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7047 ASSERT_TRUE(mDispatcher->waitForIdle());
7048 // Should not ANR because the window is paused, and touches shouldn't go to it
7049 mFakePolicy->assertNotifyAnrWasNotCalled();
7050
7051 mFocusedWindow->assertNoEvents();
7052 mUnfocusedWindow->assertNoEvents();
7053}
7054
7055/**
7056 * If a window is processing a motion event, and then a key event comes in, the key event should
7057 * not to to the focused window until the motion is processed.
7058 * If a different window becomes focused at this time, the key should go to that window instead.
7059 *
7060 * Warning!!!
7061 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7062 * and the injection timeout that we specify when injecting the key.
7063 * We must have the injection timeout (10ms) be smaller than
7064 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7065 *
7066 * If that value changes, this test should also change.
7067 */
7068TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7069 // Set a long ANR timeout to prevent it from triggering
7070 mFocusedWindow->setDispatchingTimeout(2s);
7071 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7072
7073 tapOnUnfocusedWindow();
7074 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7075 ASSERT_TRUE(downSequenceNum);
7076 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7077 ASSERT_TRUE(upSequenceNum);
7078 // Don't finish the events yet, and send a key
7079 // Injection will succeed because we will eventually give up and send the key to the focused
7080 // window even if motions are still being processed.
7081
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007082 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007083 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7084 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007085 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007086 // Key will not be sent to the window, yet, because the window is still processing events
7087 // and the key remains pending, waiting for the touch events to be processed
7088 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7089 ASSERT_FALSE(keySequenceNum);
7090
7091 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007092 mFocusedWindow->setFocusable(false);
7093 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007094 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007095 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007096
7097 // Focus events should precede the key events
7098 mUnfocusedWindow->consumeFocusEvent(true);
7099 mFocusedWindow->consumeFocusEvent(false);
7100
7101 // Finish the tap events, which should unblock dispatcher
7102 mUnfocusedWindow->finishEvent(*downSequenceNum);
7103 mUnfocusedWindow->finishEvent(*upSequenceNum);
7104
7105 // Now that all queues are cleared and no backlog in the connections, the key event
7106 // can finally go to the newly focused "mUnfocusedWindow".
7107 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7108 mFocusedWindow->assertNoEvents();
7109 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007110 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007111}
7112
7113// When the touch stream is split across 2 windows, and one of them does not respond,
7114// then ANR should be raised and the touch should be canceled for the unresponsive window.
7115// The other window should not be affected by that.
7116TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7117 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007118 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7119 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7120 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007121 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007122 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007123
7124 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007125 mDispatcher->notifyMotion(
7126 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7127 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007128
7129 const std::chrono::duration timeout =
7130 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007131 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007132
7133 mUnfocusedWindow->consumeMotionDown();
7134 mFocusedWindow->consumeMotionDown();
7135 // Focused window may or may not receive ACTION_MOVE
7136 // But it should definitely receive ACTION_CANCEL due to the ANR
7137 InputEvent* event;
7138 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7139 ASSERT_TRUE(moveOrCancelSequenceNum);
7140 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7141 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007142 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007143 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7144 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7145 mFocusedWindow->consumeMotionCancel();
7146 } else {
7147 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7148 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007149 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007150 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7151 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007152
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007153 mUnfocusedWindow->assertNoEvents();
7154 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007155 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007156}
7157
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007158/**
7159 * If we have no focused window, and a key comes in, we start the ANR timer.
7160 * The focused application should add a focused window before the timer runs out to prevent ANR.
7161 *
7162 * If the user touches another application during this time, the key should be dropped.
7163 * Next, if a new focused window comes in, without toggling the focused application,
7164 * then no ANR should occur.
7165 *
7166 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7167 * but in some cases the policy may not update the focused application.
7168 */
7169TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7170 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7171 std::make_shared<FakeApplicationHandle>();
7172 focusedApplication->setDispatchingTimeout(60ms);
7173 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7174 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7175 mFocusedWindow->setFocusable(false);
7176
7177 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7178 mFocusedWindow->consumeFocusEvent(false);
7179
7180 // Send a key. The ANR timer should start because there is no focused window.
7181 // 'focusedApplication' will get blamed if this timer completes.
7182 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007183 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007184 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7185 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7186 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007187 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007188
7189 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7190 // then the injected touches won't cause the focused event to get dropped.
7191 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7192 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7193 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7194 // For this test, it means that the key would get delivered to the window once it becomes
7195 // focused.
7196 std::this_thread::sleep_for(10ms);
7197
7198 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007199 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7200 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7201 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007202
7203 // We do not consume the motion right away, because that would require dispatcher to first
7204 // process (== drop) the key event, and by that time, ANR will be raised.
7205 // Set the focused window first.
7206 mFocusedWindow->setFocusable(true);
7207 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7208 setFocusedWindow(mFocusedWindow);
7209 mFocusedWindow->consumeFocusEvent(true);
7210 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7211 // to another application. This could be a bug / behaviour in the policy.
7212
7213 mUnfocusedWindow->consumeMotionDown();
7214
7215 ASSERT_TRUE(mDispatcher->waitForIdle());
7216 // Should not ANR because we actually have a focused window. It was just added too slowly.
7217 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7218}
7219
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007220// These tests ensure we cannot send touch events to a window that's positioned behind a window
7221// that has feature NO_INPUT_CHANNEL.
7222// Layout:
7223// Top (closest to user)
7224// mNoInputWindow (above all windows)
7225// mBottomWindow
7226// Bottom (furthest from user)
7227class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7228 virtual void SetUp() override {
7229 InputDispatcherTest::SetUp();
7230
7231 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007232 mNoInputWindow =
7233 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7234 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007235 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007236 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007237 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7238 // It's perfectly valid for this window to not have an associated input channel
7239
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007240 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7241 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007242 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7243
7244 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7245 }
7246
7247protected:
7248 std::shared_ptr<FakeApplicationHandle> mApplication;
7249 sp<FakeWindowHandle> mNoInputWindow;
7250 sp<FakeWindowHandle> mBottomWindow;
7251};
7252
7253TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7254 PointF touchedPoint = {10, 10};
7255
Prabir Pradhan678438e2023-04-13 19:32:51 +00007256 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7257 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7258 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007259
7260 mNoInputWindow->assertNoEvents();
7261 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7262 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7263 // and therefore should prevent mBottomWindow from receiving touches
7264 mBottomWindow->assertNoEvents();
7265}
7266
7267/**
7268 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7269 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7270 */
7271TEST_F(InputDispatcherMultiWindowOcclusionTests,
7272 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007273 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7274 "Window with input channel and NO_INPUT_CHANNEL",
7275 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007276
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007277 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007278 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7279 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7280
7281 PointF touchedPoint = {10, 10};
7282
Prabir Pradhan678438e2023-04-13 19:32:51 +00007283 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7284 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7285 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007286
7287 mNoInputWindow->assertNoEvents();
7288 mBottomWindow->assertNoEvents();
7289}
7290
Vishnu Nair958da932020-08-21 17:12:37 -07007291class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7292protected:
7293 std::shared_ptr<FakeApplicationHandle> mApp;
7294 sp<FakeWindowHandle> mWindow;
7295 sp<FakeWindowHandle> mMirror;
7296
7297 virtual void SetUp() override {
7298 InputDispatcherTest::SetUp();
7299 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007300 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7301 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7302 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007303 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7304 mWindow->setFocusable(true);
7305 mMirror->setFocusable(true);
7306 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7307 }
7308};
7309
7310TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7311 // Request focus on a mirrored window
7312 setFocusedWindow(mMirror);
7313
7314 // window gets focused
7315 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007316 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7317 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007318 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7319}
7320
7321// A focused & mirrored window remains focused only if the window and its mirror are both
7322// focusable.
7323TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7324 setFocusedWindow(mMirror);
7325
7326 // window gets focused
7327 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007328 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7329 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007330 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007331 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7332 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007333 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7334
7335 mMirror->setFocusable(false);
7336 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7337
7338 // window loses focus since one of the windows associated with the token in not focusable
7339 mWindow->consumeFocusEvent(false);
7340
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007341 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7342 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007343 mWindow->assertNoEvents();
7344}
7345
7346// A focused & mirrored window remains focused until the window and its mirror both become
7347// invisible.
7348TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7349 setFocusedWindow(mMirror);
7350
7351 // window gets focused
7352 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007353 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7354 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007355 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7357 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007358 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7359
7360 mMirror->setVisible(false);
7361 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7362
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007363 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7364 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007365 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007366 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7367 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007368 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7369
7370 mWindow->setVisible(false);
7371 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7372
7373 // window loses focus only after all windows associated with the token become invisible.
7374 mWindow->consumeFocusEvent(false);
7375
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007376 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7377 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007378 mWindow->assertNoEvents();
7379}
7380
7381// A focused & mirrored window remains focused until both windows are removed.
7382TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7383 setFocusedWindow(mMirror);
7384
7385 // window gets focused
7386 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007387 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7388 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007389 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007390 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7391 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007392 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7393
7394 // single window is removed but the window token remains focused
7395 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7396
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007397 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7398 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007399 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007400 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7401 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007402 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7403
7404 // Both windows are removed
7405 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7406 mWindow->consumeFocusEvent(false);
7407
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007408 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7409 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007410 mWindow->assertNoEvents();
7411}
7412
7413// Focus request can be pending until one window becomes visible.
7414TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7415 // Request focus on an invisible mirror.
7416 mWindow->setVisible(false);
7417 mMirror->setVisible(false);
7418 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7419 setFocusedWindow(mMirror);
7420
7421 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007422 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007423 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7424 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007425
7426 mMirror->setVisible(true);
7427 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7428
7429 // window gets focused
7430 mWindow->consumeFocusEvent(true);
7431 // window gets the pending key event
7432 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7433}
Prabir Pradhan99987712020-11-10 18:43:05 -08007434
7435class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7436protected:
7437 std::shared_ptr<FakeApplicationHandle> mApp;
7438 sp<FakeWindowHandle> mWindow;
7439 sp<FakeWindowHandle> mSecondWindow;
7440
7441 void SetUp() override {
7442 InputDispatcherTest::SetUp();
7443 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007444 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007445 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007446 mSecondWindow =
7447 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007448 mSecondWindow->setFocusable(true);
7449
7450 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7451 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7452
7453 setFocusedWindow(mWindow);
7454 mWindow->consumeFocusEvent(true);
7455 }
7456
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007457 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007458 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007459 }
7460
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007461 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7462 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007463 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007464 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7465 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007466 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007467 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007468 }
7469};
7470
7471TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7472 // Ensure that capture cannot be obtained for unfocused windows.
7473 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7474 mFakePolicy->assertSetPointerCaptureNotCalled();
7475 mSecondWindow->assertNoEvents();
7476
7477 // Ensure that capture can be enabled from the focus window.
7478 requestAndVerifyPointerCapture(mWindow, true);
7479
7480 // Ensure that capture cannot be disabled from a window that does not have capture.
7481 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7482 mFakePolicy->assertSetPointerCaptureNotCalled();
7483
7484 // Ensure that capture can be disabled from the window with capture.
7485 requestAndVerifyPointerCapture(mWindow, false);
7486}
7487
7488TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007489 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007490
7491 setFocusedWindow(mSecondWindow);
7492
7493 // Ensure that the capture disabled event was sent first.
7494 mWindow->consumeCaptureEvent(false);
7495 mWindow->consumeFocusEvent(false);
7496 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007497 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007498
7499 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007500 notifyPointerCaptureChanged({});
7501 notifyPointerCaptureChanged(request);
7502 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007503 mWindow->assertNoEvents();
7504 mSecondWindow->assertNoEvents();
7505 mFakePolicy->assertSetPointerCaptureNotCalled();
7506}
7507
7508TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007509 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007510
7511 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007512 notifyPointerCaptureChanged({});
7513 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007514
7515 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007516 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007517 mWindow->consumeCaptureEvent(false);
7518 mWindow->assertNoEvents();
7519}
7520
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007521TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7522 requestAndVerifyPointerCapture(mWindow, true);
7523
7524 // The first window loses focus.
7525 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007526 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007527 mWindow->consumeCaptureEvent(false);
7528
7529 // Request Pointer Capture from the second window before the notification from InputReader
7530 // arrives.
7531 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007532 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007533
7534 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007535 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007536
7537 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007538 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007539
7540 mSecondWindow->consumeFocusEvent(true);
7541 mSecondWindow->consumeCaptureEvent(true);
7542}
7543
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007544TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7545 // App repeatedly enables and disables capture.
7546 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7547 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7548 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7549 mFakePolicy->assertSetPointerCaptureCalled(false);
7550 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7551 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7552
7553 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7554 // first request is now stale, this should do nothing.
7555 notifyPointerCaptureChanged(firstRequest);
7556 mWindow->assertNoEvents();
7557
7558 // InputReader notifies that the second request was enabled.
7559 notifyPointerCaptureChanged(secondRequest);
7560 mWindow->consumeCaptureEvent(true);
7561}
7562
Prabir Pradhan7092e262022-05-03 16:51:09 +00007563TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7564 requestAndVerifyPointerCapture(mWindow, true);
7565
7566 // App toggles pointer capture off and on.
7567 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7568 mFakePolicy->assertSetPointerCaptureCalled(false);
7569
7570 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7571 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7572
7573 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7574 // preceding "disable" request.
7575 notifyPointerCaptureChanged(enableRequest);
7576
7577 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7578 // any notifications.
7579 mWindow->assertNoEvents();
7580}
7581
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007582class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7583protected:
7584 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007585
7586 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7587 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7588
7589 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7590 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7591
7592 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7593 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7594 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7595 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7596 MAXIMUM_OBSCURING_OPACITY);
7597
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007598 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007599 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007600 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007601
7602 sp<FakeWindowHandle> mTouchWindow;
7603
7604 virtual void SetUp() override {
7605 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007606 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007607 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7608 }
7609
7610 virtual void TearDown() override {
7611 InputDispatcherTest::TearDown();
7612 mTouchWindow.clear();
7613 }
7614
chaviw3277faf2021-05-19 16:45:23 -05007615 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7616 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007617 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007618 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007619 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007620 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007621 return window;
7622 }
7623
7624 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7625 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7626 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007627 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007628 // Generate an arbitrary PID based on the UID
7629 window->setOwnerInfo(1777 + (uid % 10000), uid);
7630 return window;
7631 }
7632
7633 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007634 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7635 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7636 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007637 }
7638};
7639
7640TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007641 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007642 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007643 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007644
7645 touch();
7646
7647 mTouchWindow->assertNoEvents();
7648}
7649
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007650TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007651 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7652 const sp<FakeWindowHandle>& w =
7653 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7654 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7655
7656 touch();
7657
7658 mTouchWindow->assertNoEvents();
7659}
7660
7661TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007662 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7663 const sp<FakeWindowHandle>& w =
7664 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7665 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7666
7667 touch();
7668
7669 w->assertNoEvents();
7670}
7671
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007672TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007673 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7674 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007675
7676 touch();
7677
7678 mTouchWindow->consumeAnyMotionDown();
7679}
7680
7681TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007682 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007683 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007684 w->setFrame(Rect(0, 0, 50, 50));
7685 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007686
7687 touch({PointF{100, 100}});
7688
7689 mTouchWindow->consumeAnyMotionDown();
7690}
7691
7692TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007693 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007694 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007695 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7696
7697 touch();
7698
7699 mTouchWindow->consumeAnyMotionDown();
7700}
7701
7702TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7703 const sp<FakeWindowHandle>& w =
7704 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7705 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007706
7707 touch();
7708
7709 mTouchWindow->consumeAnyMotionDown();
7710}
7711
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007712TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7713 const sp<FakeWindowHandle>& w =
7714 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7715 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7716
7717 touch();
7718
7719 w->assertNoEvents();
7720}
7721
7722/**
7723 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7724 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7725 * window, the occluding window will still receive ACTION_OUTSIDE event.
7726 */
7727TEST_F(InputDispatcherUntrustedTouchesTest,
7728 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7729 const sp<FakeWindowHandle>& w =
7730 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007731 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007732 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7733
7734 touch();
7735
7736 w->consumeMotionOutside();
7737}
7738
7739TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
7740 const sp<FakeWindowHandle>& w =
7741 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007742 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007743 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7744
7745 touch();
7746
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007747 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007748}
7749
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007750TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007751 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007752 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7753 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007754 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7755
7756 touch();
7757
7758 mTouchWindow->consumeAnyMotionDown();
7759}
7760
7761TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
7762 const sp<FakeWindowHandle>& w =
7763 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7764 MAXIMUM_OBSCURING_OPACITY);
7765 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007766
7767 touch();
7768
7769 mTouchWindow->consumeAnyMotionDown();
7770}
7771
7772TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007773 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007774 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7775 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007776 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7777
7778 touch();
7779
7780 mTouchWindow->assertNoEvents();
7781}
7782
7783TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
7784 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
7785 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007786 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7787 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007788 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007789 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7790 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007791 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7792
7793 touch();
7794
7795 mTouchWindow->assertNoEvents();
7796}
7797
7798TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
7799 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
7800 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007801 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7802 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007803 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007804 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7805 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007806 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7807
7808 touch();
7809
7810 mTouchWindow->consumeAnyMotionDown();
7811}
7812
7813TEST_F(InputDispatcherUntrustedTouchesTest,
7814 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
7815 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007816 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7817 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007818 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007819 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7820 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007821 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7822
7823 touch();
7824
7825 mTouchWindow->consumeAnyMotionDown();
7826}
7827
7828TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
7829 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007830 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7831 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007832 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007833 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7834 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007835 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007836
7837 touch();
7838
7839 mTouchWindow->assertNoEvents();
7840}
7841
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007842TEST_F(InputDispatcherUntrustedTouchesTest,
7843 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
7844 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007845 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7846 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007847 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007848 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7849 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007850 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7851
7852 touch();
7853
7854 mTouchWindow->assertNoEvents();
7855}
7856
7857TEST_F(InputDispatcherUntrustedTouchesTest,
7858 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
7859 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007860 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7861 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007862 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007863 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7864 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007865 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7866
7867 touch();
7868
7869 mTouchWindow->consumeAnyMotionDown();
7870}
7871
7872TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
7873 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007874 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7875 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007876 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7877
7878 touch();
7879
7880 mTouchWindow->consumeAnyMotionDown();
7881}
7882
7883TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
7884 const sp<FakeWindowHandle>& w =
7885 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
7886 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7887
7888 touch();
7889
7890 mTouchWindow->consumeAnyMotionDown();
7891}
7892
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007893TEST_F(InputDispatcherUntrustedTouchesTest,
7894 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
7895 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7896 const sp<FakeWindowHandle>& w =
7897 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
7898 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7899
7900 touch();
7901
7902 mTouchWindow->assertNoEvents();
7903}
7904
7905TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
7906 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7907 const sp<FakeWindowHandle>& w =
7908 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
7909 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7910
7911 touch();
7912
7913 mTouchWindow->consumeAnyMotionDown();
7914}
7915
7916TEST_F(InputDispatcherUntrustedTouchesTest,
7917 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
7918 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
7919 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007920 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7921 OPACITY_ABOVE_THRESHOLD);
7922 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7923
7924 touch();
7925
7926 mTouchWindow->consumeAnyMotionDown();
7927}
7928
7929TEST_F(InputDispatcherUntrustedTouchesTest,
7930 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
7931 const sp<FakeWindowHandle>& w1 =
7932 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7933 OPACITY_BELOW_THRESHOLD);
7934 const sp<FakeWindowHandle>& w2 =
7935 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7936 OPACITY_BELOW_THRESHOLD);
7937 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7938
7939 touch();
7940
7941 mTouchWindow->assertNoEvents();
7942}
7943
7944/**
7945 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
7946 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
7947 * (which alone would result in allowing touches) does not affect the blocking behavior.
7948 */
7949TEST_F(InputDispatcherUntrustedTouchesTest,
7950 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
7951 const sp<FakeWindowHandle>& wB =
7952 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7953 OPACITY_BELOW_THRESHOLD);
7954 const sp<FakeWindowHandle>& wC =
7955 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7956 OPACITY_BELOW_THRESHOLD);
7957 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7958
7959 touch();
7960
7961 mTouchWindow->assertNoEvents();
7962}
7963
7964/**
7965 * This test is testing that a window from a different UID but with same application token doesn't
7966 * block the touch. Apps can share the application token for close UI collaboration for example.
7967 */
7968TEST_F(InputDispatcherUntrustedTouchesTest,
7969 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
7970 const sp<FakeWindowHandle>& w =
7971 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7972 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007973 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7974
7975 touch();
7976
7977 mTouchWindow->consumeAnyMotionDown();
7978}
7979
arthurhungb89ccb02020-12-30 16:19:01 +08007980class InputDispatcherDragTests : public InputDispatcherTest {
7981protected:
7982 std::shared_ptr<FakeApplicationHandle> mApp;
7983 sp<FakeWindowHandle> mWindow;
7984 sp<FakeWindowHandle> mSecondWindow;
7985 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00007986 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00007987 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
7988 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08007989
7990 void SetUp() override {
7991 InputDispatcherTest::SetUp();
7992 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007993 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007994 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007995
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007996 mSecondWindow =
7997 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08007998 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08007999
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008000 mSpyWindow =
8001 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008002 mSpyWindow->setSpy(true);
8003 mSpyWindow->setTrustedOverlay(true);
8004 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8005
arthurhungb89ccb02020-12-30 16:19:01 +08008006 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008007 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008008 }
8009
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008010 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8011 switch (fromSource) {
8012 case AINPUT_SOURCE_TOUCHSCREEN:
8013 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8014 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8015 ADISPLAY_ID_DEFAULT, {50, 50}))
8016 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8017 break;
8018 case AINPUT_SOURCE_STYLUS:
8019 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8020 injectMotionEvent(
8021 mDispatcher,
8022 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8023 AINPUT_SOURCE_STYLUS)
8024 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008025 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008026 .x(50)
8027 .y(50))
8028 .build()));
8029 break;
8030 case AINPUT_SOURCE_MOUSE:
8031 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8032 injectMotionEvent(
8033 mDispatcher,
8034 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8035 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8036 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008037 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008038 .x(50)
8039 .y(50))
8040 .build()));
8041 break;
8042 default:
8043 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8044 }
arthurhungb89ccb02020-12-30 16:19:01 +08008045
8046 // Window should receive motion event.
8047 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008048 // Spy window should also receive motion event
8049 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008050 }
8051
8052 // Start performing drag, we will create a drag window and transfer touch to it.
8053 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8054 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008055 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008056 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008057 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008058 }
arthurhungb89ccb02020-12-30 16:19:01 +08008059
8060 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008061 mDragWindow =
8062 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008063 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008064 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008065 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008066
8067 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008068 bool transferred =
8069 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008070 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008071 if (transferred) {
8072 mWindow->consumeMotionCancel();
8073 mDragWindow->consumeMotionDown();
8074 }
8075 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008076 }
8077};
8078
8079TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008080 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008081
8082 // Move on window.
8083 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8084 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8085 ADISPLAY_ID_DEFAULT, {50, 50}))
8086 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8087 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8088 mWindow->consumeDragEvent(false, 50, 50);
8089 mSecondWindow->assertNoEvents();
8090
8091 // Move to another window.
8092 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8093 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8094 ADISPLAY_ID_DEFAULT, {150, 50}))
8095 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8096 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8097 mWindow->consumeDragEvent(true, 150, 50);
8098 mSecondWindow->consumeDragEvent(false, 50, 50);
8099
8100 // Move back to original window.
8101 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8102 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8103 ADISPLAY_ID_DEFAULT, {50, 50}))
8104 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8105 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8106 mWindow->consumeDragEvent(false, 50, 50);
8107 mSecondWindow->consumeDragEvent(true, -50, 50);
8108
8109 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8110 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8111 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8112 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8113 mWindow->assertNoEvents();
8114 mSecondWindow->assertNoEvents();
8115}
8116
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008117TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008118 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008119
8120 // No cancel event after drag start
8121 mSpyWindow->assertNoEvents();
8122
8123 const MotionEvent secondFingerDownEvent =
8124 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8125 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008126 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8127 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008128 .build();
8129 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8130 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8131 InputEventInjectionSync::WAIT_FOR_RESULT))
8132 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8133
8134 // Receives cancel for first pointer after next pointer down
8135 mSpyWindow->consumeMotionCancel();
8136 mSpyWindow->consumeMotionDown();
8137
8138 mSpyWindow->assertNoEvents();
8139}
8140
arthurhungf452d0b2021-01-06 00:19:52 +08008141TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008142 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008143
8144 // Move on window.
8145 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8146 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8147 ADISPLAY_ID_DEFAULT, {50, 50}))
8148 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8149 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8150 mWindow->consumeDragEvent(false, 50, 50);
8151 mSecondWindow->assertNoEvents();
8152
8153 // Move to another window.
8154 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8155 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8156 ADISPLAY_ID_DEFAULT, {150, 50}))
8157 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8158 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8159 mWindow->consumeDragEvent(true, 150, 50);
8160 mSecondWindow->consumeDragEvent(false, 50, 50);
8161
8162 // drop to another window.
8163 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8164 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8165 {150, 50}))
8166 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8167 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8168 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8169 mWindow->assertNoEvents();
8170 mSecondWindow->assertNoEvents();
8171}
8172
arthurhung6d4bed92021-03-17 11:59:33 +08008173TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008174 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008175
8176 // Move on window and keep button pressed.
8177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8178 injectMotionEvent(mDispatcher,
8179 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8180 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008181 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008182 .build()))
8183 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8184 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8185 mWindow->consumeDragEvent(false, 50, 50);
8186 mSecondWindow->assertNoEvents();
8187
8188 // Move to another window and release button, expect to drop item.
8189 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8190 injectMotionEvent(mDispatcher,
8191 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8192 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008193 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008194 .build()))
8195 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8196 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8197 mWindow->assertNoEvents();
8198 mSecondWindow->assertNoEvents();
8199 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8200
8201 // nothing to the window.
8202 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8203 injectMotionEvent(mDispatcher,
8204 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8205 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008206 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008207 .build()))
8208 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8209 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8210 mWindow->assertNoEvents();
8211 mSecondWindow->assertNoEvents();
8212}
8213
Arthur Hung54745652022-04-20 07:17:41 +00008214TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008215 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008216
8217 // Set second window invisible.
8218 mSecondWindow->setVisible(false);
8219 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8220
8221 // Move on window.
8222 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8223 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8224 ADISPLAY_ID_DEFAULT, {50, 50}))
8225 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8226 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8227 mWindow->consumeDragEvent(false, 50, 50);
8228 mSecondWindow->assertNoEvents();
8229
8230 // Move to another window.
8231 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8232 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8233 ADISPLAY_ID_DEFAULT, {150, 50}))
8234 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8235 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8236 mWindow->consumeDragEvent(true, 150, 50);
8237 mSecondWindow->assertNoEvents();
8238
8239 // drop to another window.
8240 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8241 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8242 {150, 50}))
8243 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8244 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8245 mFakePolicy->assertDropTargetEquals(nullptr);
8246 mWindow->assertNoEvents();
8247 mSecondWindow->assertNoEvents();
8248}
8249
Arthur Hung54745652022-04-20 07:17:41 +00008250TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008251 // Ensure window could track pointerIds if it didn't support split touch.
8252 mWindow->setPreventSplitting(true);
8253
Arthur Hung54745652022-04-20 07:17:41 +00008254 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8255 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8256 {50, 50}))
8257 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8258 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8259
8260 const MotionEvent secondFingerDownEvent =
8261 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8262 .displayId(ADISPLAY_ID_DEFAULT)
8263 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008264 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8265 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008266 .build();
8267 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8268 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8269 InputEventInjectionSync::WAIT_FOR_RESULT))
8270 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008271 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008272
8273 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008274 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008275}
8276
8277TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8278 // First down on second window.
8279 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8280 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8281 {150, 50}))
8282 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8283
8284 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8285
8286 // Second down on first window.
8287 const MotionEvent secondFingerDownEvent =
8288 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8289 .displayId(ADISPLAY_ID_DEFAULT)
8290 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008291 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8292 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008293 .build();
8294 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8295 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8296 InputEventInjectionSync::WAIT_FOR_RESULT))
8297 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8298 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8299
8300 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008301 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008302
8303 // Move on window.
8304 const MotionEvent secondFingerMoveEvent =
8305 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8306 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008307 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8308 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008309 .build();
8310 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8311 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8312 InputEventInjectionSync::WAIT_FOR_RESULT));
8313 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8314 mWindow->consumeDragEvent(false, 50, 50);
8315 mSecondWindow->consumeMotionMove();
8316
8317 // Release the drag pointer should perform drop.
8318 const MotionEvent secondFingerUpEvent =
8319 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8320 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008321 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8322 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008323 .build();
8324 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8325 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8326 InputEventInjectionSync::WAIT_FOR_RESULT));
8327 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8328 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8329 mWindow->assertNoEvents();
8330 mSecondWindow->consumeMotionMove();
8331}
8332
Arthur Hung3915c1f2022-05-31 07:17:17 +00008333TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008334 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008335
8336 // Update window of second display.
8337 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008338 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008339 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8340
8341 // Let second display has a touch state.
8342 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8343 injectMotionEvent(mDispatcher,
8344 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8345 AINPUT_SOURCE_TOUCHSCREEN)
8346 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008347 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008348 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008349 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008350 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008351 // Update window again.
8352 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8353
8354 // Move on window.
8355 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8356 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8357 ADISPLAY_ID_DEFAULT, {50, 50}))
8358 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8359 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8360 mWindow->consumeDragEvent(false, 50, 50);
8361 mSecondWindow->assertNoEvents();
8362
8363 // Move to another window.
8364 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8365 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8366 ADISPLAY_ID_DEFAULT, {150, 50}))
8367 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8368 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8369 mWindow->consumeDragEvent(true, 150, 50);
8370 mSecondWindow->consumeDragEvent(false, 50, 50);
8371
8372 // drop to another window.
8373 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8374 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8375 {150, 50}))
8376 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8377 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8378 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8379 mWindow->assertNoEvents();
8380 mSecondWindow->assertNoEvents();
8381}
8382
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008383TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8384 startDrag(true, AINPUT_SOURCE_MOUSE);
8385 // Move on window.
8386 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8387 injectMotionEvent(mDispatcher,
8388 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8389 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8390 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008391 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008392 .x(50)
8393 .y(50))
8394 .build()))
8395 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8396 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8397 mWindow->consumeDragEvent(false, 50, 50);
8398 mSecondWindow->assertNoEvents();
8399
8400 // Move to another window.
8401 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8402 injectMotionEvent(mDispatcher,
8403 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8404 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8405 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008406 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008407 .x(150)
8408 .y(50))
8409 .build()))
8410 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8411 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8412 mWindow->consumeDragEvent(true, 150, 50);
8413 mSecondWindow->consumeDragEvent(false, 50, 50);
8414
8415 // drop to another window.
8416 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8417 injectMotionEvent(mDispatcher,
8418 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8419 .buttonState(0)
8420 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008421 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008422 .x(150)
8423 .y(50))
8424 .build()))
8425 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8426 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8427 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8428 mWindow->assertNoEvents();
8429 mSecondWindow->assertNoEvents();
8430}
8431
Vishnu Nair062a8672021-09-03 16:07:44 -07008432class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8433
8434TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8435 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008436 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8437 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008438 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008439 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8440 window->setFocusable(true);
8441 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8442 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008443 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008444
8445 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008446 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008447 window->assertNoEvents();
8448
Prabir Pradhan678438e2023-04-13 19:32:51 +00008449 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8450 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008451 window->assertNoEvents();
8452
8453 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008454 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008455 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8456
Prabir Pradhan678438e2023-04-13 19:32:51 +00008457 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008458 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8459
Prabir Pradhan678438e2023-04-13 19:32:51 +00008460 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8461 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008462 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8463 window->assertNoEvents();
8464}
8465
8466TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8467 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8468 std::make_shared<FakeApplicationHandle>();
8469 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008470 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8471 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008472 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8473 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008474 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008475 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008476 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8477 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008478 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008479 window->setOwnerInfo(222, 222);
8480 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8481 window->setFocusable(true);
8482 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8483 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008484 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008485
8486 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008487 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008488 window->assertNoEvents();
8489
Prabir Pradhan678438e2023-04-13 19:32:51 +00008490 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8491 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008492 window->assertNoEvents();
8493
8494 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008495 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008496 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8497
Prabir Pradhan678438e2023-04-13 19:32:51 +00008498 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008499 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8500
Prabir Pradhan678438e2023-04-13 19:32:51 +00008501 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8502 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008503 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8504 window->assertNoEvents();
8505}
8506
8507TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8508 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8509 std::make_shared<FakeApplicationHandle>();
8510 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008511 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8512 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008513 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8514 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008515 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008516 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008517 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8518 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008519 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008520 window->setOwnerInfo(222, 222);
8521 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8522 window->setFocusable(true);
8523 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8524 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008525 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008526
8527 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008528 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008529 window->assertNoEvents();
8530
Prabir Pradhan678438e2023-04-13 19:32:51 +00008531 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8532 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008533 window->assertNoEvents();
8534
8535 // When the window is no longer obscured because it went on top, it should get input
8536 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8537
Prabir Pradhan678438e2023-04-13 19:32:51 +00008538 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008539 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8540
Prabir Pradhan678438e2023-04-13 19:32:51 +00008541 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8542 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008543 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8544 window->assertNoEvents();
8545}
8546
Antonio Kantekf16f2832021-09-28 04:39:20 +00008547class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8548protected:
8549 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008550 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008551 sp<FakeWindowHandle> mWindow;
8552 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008553 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008554
8555 void SetUp() override {
8556 InputDispatcherTest::SetUp();
8557
8558 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008559 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008560 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008561 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008562 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008563 mSecondWindow =
8564 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008565 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008566 mThirdWindow =
8567 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8568 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8569 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008570
8571 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008572 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8573 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8574 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008575 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008576
Antonio Kantek15beb512022-06-13 22:35:41 +00008577 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008578 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008579 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008580 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8581 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008582 mThirdWindow->assertNoEvents();
8583 }
8584
8585 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8586 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008587 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008588 SECOND_DISPLAY_ID)) {
8589 mWindow->assertNoEvents();
8590 mSecondWindow->assertNoEvents();
8591 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008592 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008593 }
8594
Antonio Kantek15beb512022-06-13 22:35:41 +00008595 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8596 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008597 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8598 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008599 mWindow->consumeTouchModeEvent(inTouchMode);
8600 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008601 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008602 }
8603};
8604
Antonio Kantek26defcf2022-02-08 01:12:27 +00008605TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008606 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008607 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8608 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008609 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008610}
8611
Antonio Kantek26defcf2022-02-08 01:12:27 +00008612TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8613 const WindowInfo& windowInfo = *mWindow->getInfo();
8614 int32_t ownerPid = windowInfo.ownerPid;
8615 int32_t ownerUid = windowInfo.ownerUid;
8616 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8617 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008618 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008619 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008620 mWindow->assertNoEvents();
8621 mSecondWindow->assertNoEvents();
8622}
8623
8624TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8625 const WindowInfo& windowInfo = *mWindow->getInfo();
8626 int32_t ownerPid = windowInfo.ownerPid;
8627 int32_t ownerUid = windowInfo.ownerUid;
8628 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008629 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008630 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008631}
8632
Antonio Kantekf16f2832021-09-28 04:39:20 +00008633TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008634 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008635 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8636 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008637 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008638 mWindow->assertNoEvents();
8639 mSecondWindow->assertNoEvents();
8640}
8641
Antonio Kantek15beb512022-06-13 22:35:41 +00008642TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8643 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8644 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8645 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008646 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008647 mWindow->assertNoEvents();
8648 mSecondWindow->assertNoEvents();
8649 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8650}
8651
Antonio Kantek48710e42022-03-24 14:19:30 -07008652TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8653 // Interact with the window first.
8654 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8655 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8656 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8657
8658 // Then remove focus.
8659 mWindow->setFocusable(false);
8660 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8661
8662 // Assert that caller can switch touch mode by owning one of the last interacted window.
8663 const WindowInfo& windowInfo = *mWindow->getInfo();
8664 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8665 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008666 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008667}
8668
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008669class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8670public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008671 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008672 std::shared_ptr<FakeApplicationHandle> application =
8673 std::make_shared<FakeApplicationHandle>();
8674 std::string name = "Fake Spy ";
8675 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008676 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8677 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008678 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008679 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008680 return spy;
8681 }
8682
8683 sp<FakeWindowHandle> createForeground() {
8684 std::shared_ptr<FakeApplicationHandle> application =
8685 std::make_shared<FakeApplicationHandle>();
8686 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008687 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8688 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008689 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008690 return window;
8691 }
8692
8693private:
8694 int mSpyCount{0};
8695};
8696
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008697using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008698/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008699 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8700 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008701TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8702 ScopedSilentDeath _silentDeath;
8703
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008704 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008705 spy->setTrustedOverlay(false);
8706 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8707 ".* not a trusted overlay");
8708}
8709
8710/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008711 * Input injection into a display with a spy window but no foreground windows should succeed.
8712 */
8713TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008714 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008715 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8716
8717 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8718 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8719 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8720 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8721}
8722
8723/**
8724 * Verify the order in which different input windows receive events. The touched foreground window
8725 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8726 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8727 * receive events before ones belows it.
8728 *
8729 * Here, we set up a scenario with four windows in the following Z order from the top:
8730 * spy1, spy2, window, spy3.
8731 * We then inject an event and verify that the foreground "window" receives it first, followed by
8732 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
8733 * window.
8734 */
8735TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
8736 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008737 auto spy1 = createSpy();
8738 auto spy2 = createSpy();
8739 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008740 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
8741 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
8742 const size_t numChannels = channels.size();
8743
Michael Wright8e9a8562022-02-09 13:44:29 +00008744 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008745 if (!epollFd.ok()) {
8746 FAIL() << "Failed to create epoll fd";
8747 }
8748
8749 for (size_t i = 0; i < numChannels; i++) {
8750 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
8751 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
8752 FAIL() << "Failed to add fd to epoll";
8753 }
8754 }
8755
8756 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8757 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8758 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8759
8760 std::vector<size_t> eventOrder;
8761 std::vector<struct epoll_event> events(numChannels);
8762 for (;;) {
8763 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
8764 (100ms).count());
8765 if (nFds < 0) {
8766 FAIL() << "Failed to call epoll_wait";
8767 }
8768 if (nFds == 0) {
8769 break; // epoll_wait timed out
8770 }
8771 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07008772 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07008773 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008774 channels[i]->consumeMotionDown();
8775 }
8776 }
8777
8778 // Verify the order in which the events were received.
8779 EXPECT_EQ(3u, eventOrder.size());
8780 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
8781 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
8782 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
8783}
8784
8785/**
8786 * A spy window using the NOT_TOUCHABLE flag does not receive events.
8787 */
8788TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
8789 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008790 auto spy = createSpy();
8791 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008792 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8793
8794 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8795 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8796 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8797 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8798 spy->assertNoEvents();
8799}
8800
8801/**
8802 * A spy window will only receive gestures that originate within its touchable region. Gestures that
8803 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
8804 * to the window.
8805 */
8806TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
8807 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008808 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008809 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
8810 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8811
8812 // Inject an event outside the spy window's touchable region.
8813 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8814 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8815 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8816 window->consumeMotionDown();
8817 spy->assertNoEvents();
8818 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8819 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8820 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8821 window->consumeMotionUp();
8822 spy->assertNoEvents();
8823
8824 // Inject an event inside the spy window's touchable region.
8825 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8826 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8827 {5, 10}))
8828 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8829 window->consumeMotionDown();
8830 spy->consumeMotionDown();
8831}
8832
8833/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008834 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008835 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008836 */
8837TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
8838 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008839 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008840 auto spy = createSpy();
8841 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008842 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008843 spy->setFrame(Rect{0, 0, 20, 20});
8844 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8845
8846 // Inject an event outside the spy window's frame and touchable region.
8847 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008848 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8849 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008850 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8851 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008852 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008853}
8854
8855/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008856 * Even when a spy window spans over multiple foreground windows, the spy should receive all
8857 * pointers that are down within its bounds.
8858 */
8859TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
8860 auto windowLeft = createForeground();
8861 windowLeft->setFrame({0, 0, 100, 200});
8862 auto windowRight = createForeground();
8863 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008864 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008865 spy->setFrame({0, 0, 200, 200});
8866 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
8867
8868 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8869 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8870 {50, 50}))
8871 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8872 windowLeft->consumeMotionDown();
8873 spy->consumeMotionDown();
8874
8875 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008876 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008877 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008878 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8879 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008880 .build();
8881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8882 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8883 InputEventInjectionSync::WAIT_FOR_RESULT))
8884 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8885 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00008886 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008887}
8888
8889/**
8890 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
8891 * the spy should receive the second pointer with ACTION_DOWN.
8892 */
8893TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
8894 auto window = createForeground();
8895 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008896 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008897 spyRight->setFrame({100, 0, 200, 200});
8898 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
8899
8900 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8901 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8902 {50, 50}))
8903 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8904 window->consumeMotionDown();
8905 spyRight->assertNoEvents();
8906
8907 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008908 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008909 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008910 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8911 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008912 .build();
8913 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8914 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8915 InputEventInjectionSync::WAIT_FOR_RESULT))
8916 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008917 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008918 spyRight->consumeMotionDown();
8919}
8920
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008921/**
8922 * The spy window should not be able to affect whether or not touches are split. Only the foreground
8923 * windows should be allowed to control split touch.
8924 */
8925TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008926 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008927 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008928 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008929 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008930
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008931 auto window = createForeground();
8932 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008933
8934 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8935
8936 // First finger down, no window touched.
8937 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8938 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8939 {100, 200}))
8940 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8941 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8942 window->assertNoEvents();
8943
8944 // Second finger down on window, the window should receive touch down.
8945 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008946 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008947 .displayId(ADISPLAY_ID_DEFAULT)
8948 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008949 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8950 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008951 .build();
8952 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8953 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8954 InputEventInjectionSync::WAIT_FOR_RESULT))
8955 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8956
8957 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00008958 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008959}
8960
8961/**
8962 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
8963 * do not receive key events.
8964 */
8965TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008966 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008967 spy->setFocusable(false);
8968
8969 auto window = createForeground();
8970 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8971 setFocusedWindow(window);
8972 window->consumeFocusEvent(true);
8973
8974 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
8975 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8976 window->consumeKeyDown(ADISPLAY_ID_NONE);
8977
8978 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
8979 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8980 window->consumeKeyUp(ADISPLAY_ID_NONE);
8981
8982 spy->assertNoEvents();
8983}
8984
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00008985using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
8986
8987/**
8988 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
8989 * are currently sent to any other windows - including other spy windows - will also be cancelled.
8990 */
8991TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
8992 auto window = createForeground();
8993 auto spy1 = createSpy();
8994 auto spy2 = createSpy();
8995 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
8996
8997 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8998 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8999 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9000 window->consumeMotionDown();
9001 spy1->consumeMotionDown();
9002 spy2->consumeMotionDown();
9003
9004 // Pilfer pointers from the second spy window.
9005 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9006 spy2->assertNoEvents();
9007 spy1->consumeMotionCancel();
9008 window->consumeMotionCancel();
9009
9010 // The rest of the gesture should only be sent to the second spy window.
9011 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9012 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9013 ADISPLAY_ID_DEFAULT))
9014 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9015 spy2->consumeMotionMove();
9016 spy1->assertNoEvents();
9017 window->assertNoEvents();
9018}
9019
9020/**
9021 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9022 * in the middle of the gesture.
9023 */
9024TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9025 auto window = createForeground();
9026 auto spy = createSpy();
9027 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9028
9029 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9030 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9031 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9032 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9033 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9034
9035 window->releaseChannel();
9036
9037 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9038
9039 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9040 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9041 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9042 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9043}
9044
9045/**
9046 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9047 * the spy, but not to any other windows.
9048 */
9049TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9050 auto spy = createSpy();
9051 auto window = createForeground();
9052
9053 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9054
9055 // First finger down on the window and the spy.
9056 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9057 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9058 {100, 200}))
9059 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9060 spy->consumeMotionDown();
9061 window->consumeMotionDown();
9062
9063 // Spy window pilfers the pointers.
9064 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9065 window->consumeMotionCancel();
9066
9067 // Second finger down on the window and spy, but the window should not receive the pointer down.
9068 const MotionEvent secondFingerDownEvent =
9069 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9070 .displayId(ADISPLAY_ID_DEFAULT)
9071 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009072 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9073 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009074 .build();
9075 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9076 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9077 InputEventInjectionSync::WAIT_FOR_RESULT))
9078 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9079
Harry Cutts33476232023-01-30 19:57:29 +00009080 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009081
9082 // Third finger goes down outside all windows, so injection should fail.
9083 const MotionEvent thirdFingerDownEvent =
9084 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9085 .displayId(ADISPLAY_ID_DEFAULT)
9086 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009087 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9088 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9089 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009090 .build();
9091 ASSERT_EQ(InputEventInjectionResult::FAILED,
9092 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9093 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009094 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009095
9096 spy->assertNoEvents();
9097 window->assertNoEvents();
9098}
9099
9100/**
9101 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9102 */
9103TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9104 auto spy = createSpy();
9105 spy->setFrame(Rect(0, 0, 100, 100));
9106 auto window = createForeground();
9107 window->setFrame(Rect(0, 0, 200, 200));
9108
9109 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9110
9111 // First finger down on the window only
9112 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9113 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9114 {150, 150}))
9115 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9116 window->consumeMotionDown();
9117
9118 // Second finger down on the spy and window
9119 const MotionEvent secondFingerDownEvent =
9120 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9121 .displayId(ADISPLAY_ID_DEFAULT)
9122 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009123 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9124 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009125 .build();
9126 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9127 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9128 InputEventInjectionSync::WAIT_FOR_RESULT))
9129 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9130 spy->consumeMotionDown();
9131 window->consumeMotionPointerDown(1);
9132
9133 // Third finger down on the spy and window
9134 const MotionEvent thirdFingerDownEvent =
9135 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9136 .displayId(ADISPLAY_ID_DEFAULT)
9137 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009138 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9139 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9140 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009141 .build();
9142 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9143 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9144 InputEventInjectionSync::WAIT_FOR_RESULT))
9145 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9146 spy->consumeMotionPointerDown(1);
9147 window->consumeMotionPointerDown(2);
9148
9149 // Spy window pilfers the pointers.
9150 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9151 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9152 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9153
9154 spy->assertNoEvents();
9155 window->assertNoEvents();
9156}
9157
9158/**
9159 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9160 * other windows should be canceled. If this results in the cancellation of all pointers for some
9161 * window, then that window should receive ACTION_CANCEL.
9162 */
9163TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9164 auto spy = createSpy();
9165 spy->setFrame(Rect(0, 0, 100, 100));
9166 auto window = createForeground();
9167 window->setFrame(Rect(0, 0, 200, 200));
9168
9169 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9170
9171 // First finger down on both spy and window
9172 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9173 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9174 {10, 10}))
9175 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9176 window->consumeMotionDown();
9177 spy->consumeMotionDown();
9178
9179 // Second finger down on the spy and window
9180 const MotionEvent secondFingerDownEvent =
9181 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9182 .displayId(ADISPLAY_ID_DEFAULT)
9183 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009184 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9185 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009186 .build();
9187 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9188 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9189 InputEventInjectionSync::WAIT_FOR_RESULT))
9190 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9191 spy->consumeMotionPointerDown(1);
9192 window->consumeMotionPointerDown(1);
9193
9194 // Spy window pilfers the pointers.
9195 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9196 window->consumeMotionCancel();
9197
9198 spy->assertNoEvents();
9199 window->assertNoEvents();
9200}
9201
9202/**
9203 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9204 * be sent to other windows
9205 */
9206TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9207 auto spy = createSpy();
9208 spy->setFrame(Rect(0, 0, 100, 100));
9209 auto window = createForeground();
9210 window->setFrame(Rect(0, 0, 200, 200));
9211
9212 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9213
9214 // First finger down on both window and spy
9215 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9216 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9217 {10, 10}))
9218 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9219 window->consumeMotionDown();
9220 spy->consumeMotionDown();
9221
9222 // Spy window pilfers the pointers.
9223 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9224 window->consumeMotionCancel();
9225
9226 // Second finger down on the window only
9227 const MotionEvent secondFingerDownEvent =
9228 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9229 .displayId(ADISPLAY_ID_DEFAULT)
9230 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009231 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9232 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009233 .build();
9234 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9235 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9236 InputEventInjectionSync::WAIT_FOR_RESULT))
9237 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9238 window->consumeMotionDown();
9239 window->assertNoEvents();
9240
9241 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9242 spy->consumeMotionMove();
9243 spy->assertNoEvents();
9244}
9245
Prabir Pradhand65552b2021-10-07 11:23:50 -07009246class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9247public:
9248 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9249 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9250 std::make_shared<FakeApplicationHandle>();
9251 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009252 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9253 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009254 overlay->setFocusable(false);
9255 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009256 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009257 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009258 overlay->setTrustedOverlay(true);
9259
9260 std::shared_ptr<FakeApplicationHandle> application =
9261 std::make_shared<FakeApplicationHandle>();
9262 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009263 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9264 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009265 window->setFocusable(true);
9266 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009267
9268 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9269 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9270 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009271 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009272 return {std::move(overlay), std::move(window)};
9273 }
9274
9275 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009276 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009277 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009278 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009279 }
9280
9281 void sendStylusEvent(int32_t action) {
9282 NotifyMotionArgs motionArgs =
9283 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9284 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009285 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009286 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009287 }
9288};
9289
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009290using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9291
9292TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9293 ScopedSilentDeath _silentDeath;
9294
Prabir Pradhand65552b2021-10-07 11:23:50 -07009295 auto [overlay, window] = setupStylusOverlayScenario();
9296 overlay->setTrustedOverlay(false);
9297 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9298 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9299 ".* not a trusted overlay");
9300}
9301
9302TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9303 auto [overlay, window] = setupStylusOverlayScenario();
9304 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9305
9306 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9307 overlay->consumeMotionDown();
9308 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9309 overlay->consumeMotionUp();
9310
9311 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9312 window->consumeMotionDown();
9313 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9314 window->consumeMotionUp();
9315
9316 overlay->assertNoEvents();
9317 window->assertNoEvents();
9318}
9319
9320TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9321 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009322 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009323 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9324
9325 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9326 overlay->consumeMotionDown();
9327 window->consumeMotionDown();
9328 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9329 overlay->consumeMotionUp();
9330 window->consumeMotionUp();
9331
9332 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9333 window->consumeMotionDown();
9334 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9335 window->consumeMotionUp();
9336
9337 overlay->assertNoEvents();
9338 window->assertNoEvents();
9339}
9340
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009341/**
9342 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9343 * The scenario is as follows:
9344 * - The stylus interceptor overlay is configured as a spy window.
9345 * - The stylus interceptor spy receives the start of a new stylus gesture.
9346 * - It pilfers pointers and then configures itself to no longer be a spy.
9347 * - The stylus interceptor continues to receive the rest of the gesture.
9348 */
9349TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9350 auto [overlay, window] = setupStylusOverlayScenario();
9351 overlay->setSpy(true);
9352 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9353
9354 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9355 overlay->consumeMotionDown();
9356 window->consumeMotionDown();
9357
9358 // The interceptor pilfers the pointers.
9359 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9360 window->consumeMotionCancel();
9361
9362 // The interceptor configures itself so that it is no longer a spy.
9363 overlay->setSpy(false);
9364 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9365
9366 // It continues to receive the rest of the stylus gesture.
9367 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9368 overlay->consumeMotionMove();
9369 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9370 overlay->consumeMotionUp();
9371
9372 window->assertNoEvents();
9373}
9374
Prabir Pradhan5735a322022-04-11 17:23:34 +00009375struct User {
9376 int32_t mPid;
9377 int32_t mUid;
9378 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9379 std::unique_ptr<InputDispatcher>& mDispatcher;
9380
9381 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9382 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9383
9384 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9385 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9386 ADISPLAY_ID_DEFAULT, {100, 200},
9387 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9388 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9389 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9390 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9391 }
9392
9393 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009394 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009395 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009396 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009397 mPolicyFlags);
9398 }
9399
9400 sp<FakeWindowHandle> createWindow() const {
9401 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9402 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009403 sp<FakeWindowHandle> window =
9404 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9405 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009406 window->setOwnerInfo(mPid, mUid);
9407 return window;
9408 }
9409};
9410
9411using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9412
9413TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9414 auto owner = User(mDispatcher, 10, 11);
9415 auto window = owner.createWindow();
9416 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9417
9418 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9419 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9420 window->consumeMotionDown();
9421
9422 setFocusedWindow(window);
9423 window->consumeFocusEvent(true);
9424
9425 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9426 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9427 window->consumeKeyDown(ADISPLAY_ID_NONE);
9428}
9429
9430TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9431 auto owner = User(mDispatcher, 10, 11);
9432 auto window = owner.createWindow();
9433 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9434
9435 auto rando = User(mDispatcher, 20, 21);
9436 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9437 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9438
9439 setFocusedWindow(window);
9440 window->consumeFocusEvent(true);
9441
9442 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9443 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9444 window->assertNoEvents();
9445}
9446
9447TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9448 auto owner = User(mDispatcher, 10, 11);
9449 auto window = owner.createWindow();
9450 auto spy = owner.createWindow();
9451 spy->setSpy(true);
9452 spy->setTrustedOverlay(true);
9453 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9454
9455 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9456 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9457 spy->consumeMotionDown();
9458 window->consumeMotionDown();
9459}
9460
9461TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9462 auto owner = User(mDispatcher, 10, 11);
9463 auto window = owner.createWindow();
9464
9465 auto rando = User(mDispatcher, 20, 21);
9466 auto randosSpy = rando.createWindow();
9467 randosSpy->setSpy(true);
9468 randosSpy->setTrustedOverlay(true);
9469 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9470
9471 // The event is targeted at owner's window, so injection should succeed, but the spy should
9472 // not receive the event.
9473 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9474 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9475 randosSpy->assertNoEvents();
9476 window->consumeMotionDown();
9477}
9478
9479TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9480 auto owner = User(mDispatcher, 10, 11);
9481 auto window = owner.createWindow();
9482
9483 auto rando = User(mDispatcher, 20, 21);
9484 auto randosSpy = rando.createWindow();
9485 randosSpy->setSpy(true);
9486 randosSpy->setTrustedOverlay(true);
9487 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9488
9489 // A user that has injection permission can inject into any window.
9490 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9491 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9492 ADISPLAY_ID_DEFAULT));
9493 randosSpy->consumeMotionDown();
9494 window->consumeMotionDown();
9495
9496 setFocusedWindow(randosSpy);
9497 randosSpy->consumeFocusEvent(true);
9498
9499 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9500 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9501 window->assertNoEvents();
9502}
9503
9504TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9505 auto owner = User(mDispatcher, 10, 11);
9506 auto window = owner.createWindow();
9507
9508 auto rando = User(mDispatcher, 20, 21);
9509 auto randosWindow = rando.createWindow();
9510 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9511 randosWindow->setWatchOutsideTouch(true);
9512 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9513
9514 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9515 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9516 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9517 window->consumeMotionDown();
9518 randosWindow->consumeMotionOutside();
9519}
9520
Garfield Tane84e6f92019-08-29 17:28:41 -07009521} // namespace android::inputdispatcher