blob: 779d389a9ddd740dc5a5f4d91bf03a3408765c9f [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
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -0700849namespace {
850
Arthur Hungb92218b2018-08-14 12:00:21 +0800851// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700852static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700853// Default input dispatching timeout if there is no focused application or paused window
854// from which to determine an appropriate dispatching timeout.
855static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
856 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
857 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800858
859class FakeApplicationHandle : public InputApplicationHandle {
860public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700861 FakeApplicationHandle() {
862 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700863 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500864 mInfo.dispatchingTimeoutMillis =
865 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700866 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800867 virtual ~FakeApplicationHandle() {}
868
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000869 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700870
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500871 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
872 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700873 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800874};
875
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800876class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800877public:
Garfield Tan15601662020-09-22 15:32:38 -0700878 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800879 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700880 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800881 }
882
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800883 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700884 InputEvent* event;
885 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
886 if (!consumeSeq) {
887 return nullptr;
888 }
889 finishEvent(*consumeSeq);
890 return event;
891 }
892
893 /**
894 * Receive an event without acknowledging it.
895 * Return the sequence number that could later be used to send finished signal.
896 */
897 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800898 uint32_t consumeSeq;
899 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800900
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800901 std::chrono::time_point start = std::chrono::steady_clock::now();
902 status_t status = WOULD_BLOCK;
903 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000904 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800905 &event);
906 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
907 if (elapsed > 100ms) {
908 break;
909 }
910 }
911
912 if (status == WOULD_BLOCK) {
913 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700914 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800915 }
916
917 if (status != OK) {
918 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700919 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800920 }
921 if (event == nullptr) {
922 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700923 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800924 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700925 if (outEvent != nullptr) {
926 *outEvent = event;
927 }
928 return consumeSeq;
929 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800930
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700931 /**
932 * To be used together with "receiveEvent" to complete the consumption of an event.
933 */
934 void finishEvent(uint32_t consumeSeq) {
935 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
936 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800937 }
938
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000939 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
940 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
941 ASSERT_EQ(OK, status);
942 }
943
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700944 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000945 std::optional<int32_t> expectedDisplayId,
946 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800947 InputEvent* event = consume();
948
949 ASSERT_NE(nullptr, event) << mName.c_str()
950 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800951 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700952 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
953 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800954
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000955 if (expectedDisplayId.has_value()) {
956 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
957 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800958
Tiger Huang8664f8c2018-10-11 19:14:35 +0800959 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700960 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800961 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
962 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000963 if (expectedFlags.has_value()) {
964 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
965 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800966 break;
967 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700968 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800969 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000970 assertMotionAction(expectedAction, motionEvent.getAction());
971
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000972 if (expectedFlags.has_value()) {
973 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
974 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800975 break;
976 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700977 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100978 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
979 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700980 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -0800981 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
982 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700983 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +0000984 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
985 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700986 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +0800987 FAIL() << "Use 'consumeDragEvent' for DRAG events";
988 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800989 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800990 }
991
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800992 MotionEvent* consumeMotion() {
993 InputEvent* event = consume();
994
995 if (event == nullptr) {
996 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
997 return nullptr;
998 }
999
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001000 if (event->getType() != InputEventType::MOTION) {
1001 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001002 return nullptr;
1003 }
1004 return static_cast<MotionEvent*>(event);
1005 }
1006
1007 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1008 MotionEvent* motionEvent = consumeMotion();
1009 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1010 ASSERT_THAT(*motionEvent, matcher);
1011 }
1012
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001013 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
1014 InputEvent* event = consume();
1015 ASSERT_NE(nullptr, event) << mName.c_str()
1016 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001017 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1018 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001019
1020 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1021 << mName.c_str() << ": event displayId should always be NONE.";
1022
1023 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1024 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001025 }
1026
Prabir Pradhan99987712020-11-10 18:43:05 -08001027 void consumeCaptureEvent(bool hasCapture) {
1028 const InputEvent* event = consume();
1029 ASSERT_NE(nullptr, event) << mName.c_str()
1030 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001031 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1032 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001033
1034 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1035 << mName.c_str() << ": event displayId should always be NONE.";
1036
1037 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1038 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1039 }
1040
arthurhungb89ccb02020-12-30 16:19:01 +08001041 void consumeDragEvent(bool isExiting, float x, float y) {
1042 const InputEvent* event = consume();
1043 ASSERT_NE(nullptr, event) << mName.c_str()
1044 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001045 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001046
1047 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1048 << mName.c_str() << ": event displayId should always be NONE.";
1049
1050 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1051 EXPECT_EQ(isExiting, dragEvent.isExiting());
1052 EXPECT_EQ(x, dragEvent.getX());
1053 EXPECT_EQ(y, dragEvent.getY());
1054 }
1055
Antonio Kantekf16f2832021-09-28 04:39:20 +00001056 void consumeTouchModeEvent(bool inTouchMode) {
1057 const InputEvent* event = consume();
1058 ASSERT_NE(nullptr, event) << mName.c_str()
1059 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001060 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1061 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001062
1063 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1064 << mName.c_str() << ": event displayId should always be NONE.";
1065 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1066 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1067 }
1068
chaviwd1c23182019-12-20 18:44:56 -08001069 void assertNoEvents() {
1070 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001071 if (event == nullptr) {
1072 return;
1073 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001074 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001075 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1076 ADD_FAILURE() << "Received key event "
1077 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001078 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001079 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1080 ADD_FAILURE() << "Received motion event "
1081 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001082 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001083 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1084 ADD_FAILURE() << "Received focus event, hasFocus = "
1085 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001086 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001087 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1088 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1089 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001090 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001091 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1092 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1093 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001094 }
1095 FAIL() << mName.c_str()
1096 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001097 }
1098
1099 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1100
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001101 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1102
chaviwd1c23182019-12-20 18:44:56 -08001103protected:
1104 std::unique_ptr<InputConsumer> mConsumer;
1105 PreallocatedInputEventFactory mEventFactory;
1106
1107 std::string mName;
1108};
1109
chaviw3277faf2021-05-19 16:45:23 -05001110class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001111public:
1112 static const int32_t WIDTH = 600;
1113 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001114
Chris Yea209fde2020-07-22 13:54:51 -07001115 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001116 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001117 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001118 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001119 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001120 base::Result<std::unique_ptr<InputChannel>> channel =
1121 dispatcher->createInputChannel(name);
1122 token = (*channel)->getConnectionToken();
1123 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001124 }
1125
1126 inputApplicationHandle->updateInfo();
1127 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1128
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001129 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001130 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001131 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001132 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001133 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001134 mInfo.frameLeft = 0;
1135 mInfo.frameTop = 0;
1136 mInfo.frameRight = WIDTH;
1137 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001138 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001139 mInfo.globalScaleFactor = 1.0;
1140 mInfo.touchableRegion.clear();
1141 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001142 mInfo.ownerPid = WINDOW_PID;
1143 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001144 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001145 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001146 }
1147
Arthur Hungabbb9d82021-09-01 14:52:30 +00001148 sp<FakeWindowHandle> clone(
1149 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001150 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001151 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001152 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1153 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001154 return handle;
1155 }
1156
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001157 void setTouchable(bool touchable) {
1158 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1159 }
chaviwd1c23182019-12-20 18:44:56 -08001160
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001161 void setFocusable(bool focusable) {
1162 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1163 }
1164
1165 void setVisible(bool visible) {
1166 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1167 }
Vishnu Nair958da932020-08-21 17:12:37 -07001168
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001169 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001170 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001171 }
1172
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001173 void setPaused(bool paused) {
1174 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1175 }
1176
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001177 void setPreventSplitting(bool preventSplitting) {
1178 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001179 }
1180
1181 void setSlippery(bool slippery) {
1182 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1183 }
1184
1185 void setWatchOutsideTouch(bool watchOutside) {
1186 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1187 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001188
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001189 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1190
1191 void setInterceptsStylus(bool interceptsStylus) {
1192 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1193 }
1194
1195 void setDropInput(bool dropInput) {
1196 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1197 }
1198
1199 void setDropInputIfObscured(bool dropInputIfObscured) {
1200 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1201 }
1202
1203 void setNoInputChannel(bool noInputChannel) {
1204 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1205 }
1206
Josep del Riob3981622023-04-18 15:49:45 +00001207 void setDisableUserActivity(bool disableUserActivity) {
1208 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1209 }
1210
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001211 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1212
chaviw3277faf2021-05-19 16:45:23 -05001213 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001214
Bernardo Rufino7393d172021-02-26 13:56:11 +00001215 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1216
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001217 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001218 mInfo.frameLeft = frame.left;
1219 mInfo.frameTop = frame.top;
1220 mInfo.frameRight = frame.right;
1221 mInfo.frameBottom = frame.bottom;
1222 mInfo.touchableRegion.clear();
1223 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001224
1225 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1226 ui::Transform translate;
1227 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1228 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001229 }
1230
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001231 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1232
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001233 void setIsWallpaper(bool isWallpaper) {
1234 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1235 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001236
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001237 void setDupTouchToWallpaper(bool hasWallpaper) {
1238 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1239 }
chaviwd1c23182019-12-20 18:44:56 -08001240
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001241 void setTrustedOverlay(bool trustedOverlay) {
1242 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1243 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001244
chaviw9eaa22c2020-07-01 16:21:27 -07001245 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1246 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1247 }
1248
1249 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001250
yunho.shinf4a80b82020-11-16 21:13:57 +09001251 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1252
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001253 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001254 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001255 }
1256
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001257 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001258 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001259 }
1260
Svet Ganov5d3bc372020-01-26 23:11:07 -08001261 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001262 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001263 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1264 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001265 }
1266
1267 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001268 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001269 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1270 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001271 }
1272
1273 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001274 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001275 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1276 }
1277
1278 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1279 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001280 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001281 expectedFlags);
1282 }
1283
Svet Ganov5d3bc372020-01-26 23:11:07 -08001284 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001285 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1286 int32_t expectedFlags = 0) {
1287 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1288 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001289 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001290 }
1291
1292 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001293 int32_t expectedFlags = 0) {
1294 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1295 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001296 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001297 }
1298
1299 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001300 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001301 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001302 expectedFlags);
1303 }
1304
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001305 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1306 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001307 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001308 expectedFlags);
1309 }
1310
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001311 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1312 int32_t expectedFlags = 0) {
1313 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001314 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001315 ASSERT_EQ(InputEventType::MOTION, event->getType());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001316 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1317 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1318 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1319 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1320 }
1321
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001322 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1323 ASSERT_NE(mInputReceiver, nullptr)
1324 << "Cannot consume events from a window with no receiver";
1325 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1326 }
1327
Prabir Pradhan99987712020-11-10 18:43:05 -08001328 void consumeCaptureEvent(bool hasCapture) {
1329 ASSERT_NE(mInputReceiver, nullptr)
1330 << "Cannot consume events from a window with no receiver";
1331 mInputReceiver->consumeCaptureEvent(hasCapture);
1332 }
1333
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001334 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1335 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001336 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001337 ASSERT_THAT(*motionEvent, matcher);
1338 }
1339
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001340 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001341 std::optional<int32_t> expectedDisplayId,
1342 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001343 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1344 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1345 expectedFlags);
1346 }
1347
arthurhungb89ccb02020-12-30 16:19:01 +08001348 void consumeDragEvent(bool isExiting, float x, float y) {
1349 mInputReceiver->consumeDragEvent(isExiting, x, y);
1350 }
1351
Antonio Kantekf16f2832021-09-28 04:39:20 +00001352 void consumeTouchModeEvent(bool inTouchMode) {
1353 ASSERT_NE(mInputReceiver, nullptr)
1354 << "Cannot consume events from a window with no receiver";
1355 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1356 }
1357
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001358 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001359 if (mInputReceiver == nullptr) {
1360 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1361 return std::nullopt;
1362 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001363 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001364 }
1365
1366 void finishEvent(uint32_t sequenceNum) {
1367 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1368 mInputReceiver->finishEvent(sequenceNum);
1369 }
1370
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001371 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1372 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1373 mInputReceiver->sendTimeline(inputEventId, timeline);
1374 }
1375
chaviwaf87b3e2019-10-01 16:59:28 -07001376 InputEvent* consume() {
1377 if (mInputReceiver == nullptr) {
1378 return nullptr;
1379 }
1380 return mInputReceiver->consume();
1381 }
1382
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001383 MotionEvent* consumeMotion() {
1384 InputEvent* event = consume();
1385 if (event == nullptr) {
1386 ADD_FAILURE() << "Consume failed : no event";
1387 return nullptr;
1388 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001389 if (event->getType() != InputEventType::MOTION) {
1390 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001391 return nullptr;
1392 }
1393 return static_cast<MotionEvent*>(event);
1394 }
1395
Arthur Hungb92218b2018-08-14 12:00:21 +08001396 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001397 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001398 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001399 return; // Can't receive events if the window does not have input channel
1400 }
1401 ASSERT_NE(nullptr, mInputReceiver)
1402 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001403 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001404 }
1405
chaviwaf87b3e2019-10-01 16:59:28 -07001406 sp<IBinder> getToken() { return mInfo.token; }
1407
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001408 const std::string& getName() { return mName; }
1409
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001410 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1411 mInfo.ownerPid = ownerPid;
1412 mInfo.ownerUid = ownerUid;
1413 }
1414
Prabir Pradhanedd96402022-02-15 01:46:16 -08001415 int32_t getPid() const { return mInfo.ownerPid; }
1416
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001417 void destroyReceiver() { mInputReceiver = nullptr; }
1418
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001419 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1420
chaviwd1c23182019-12-20 18:44:56 -08001421private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001422 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001423 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001424 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001425};
1426
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001427std::atomic<int32_t> FakeWindowHandle::sId{1};
1428
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001429static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001430 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001431 int32_t displayId = ADISPLAY_ID_NONE,
1432 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001433 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001434 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1435 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001436 KeyEvent event;
1437 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1438
1439 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001440 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001441 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1442 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001443
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001444 if (!allowKeyRepeat) {
1445 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1446 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001447 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001448 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001449}
1450
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001451static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001452 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001453 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001454}
1455
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001456// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1457// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1458// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001459static InputEventInjectionResult injectKeyDownNoRepeat(
1460 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001461 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001462 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001463 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001464}
1465
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001466static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001467 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001468 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001469}
1470
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001471static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001472 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001473 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001474 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1475 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1476 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1477 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001478}
1479
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001480static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001481 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001482 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001483 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001484 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1485 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001486 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001487 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1488 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001489 MotionEventBuilder motionBuilder =
1490 MotionEventBuilder(action, source)
1491 .displayId(displayId)
1492 .eventTime(eventTime)
1493 .rawXCursorPosition(cursorPosition.x)
1494 .rawYCursorPosition(cursorPosition.y)
1495 .pointer(
1496 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1497 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1498 motionBuilder.downTime(eventTime);
1499 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001500
1501 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001502 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1503 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001504}
1505
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001506static InputEventInjectionResult injectMotionDown(
1507 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1508 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001509 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001510}
1511
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001512static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001513 int32_t source, int32_t displayId,
1514 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001515 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001516}
1517
Jackal Guof9696682018-10-05 12:23:23 +08001518static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1519 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1520 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001521 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001522 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1523 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001524
1525 return args;
1526}
1527
Josep del Riob3981622023-04-18 15:49:45 +00001528static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1529 int32_t displayId = ADISPLAY_ID_NONE) {
1530 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1531 // Define a valid key event.
1532 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1533 displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON,
1534 currentTime);
1535
1536 return args;
1537}
1538
1539static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1540 int32_t displayId = ADISPLAY_ID_NONE) {
1541 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1542 // Define a valid key event.
1543 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1544 displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT,
1545 AMETA_NONE, currentTime);
1546
1547 return args;
1548}
1549
Prabir Pradhan678438e2023-04-13 19:32:51 +00001550[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1551 int32_t displayId,
1552 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001553 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001554 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1555 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1556 }
1557
chaviwd1c23182019-12-20 18:44:56 -08001558 PointerProperties pointerProperties[pointerCount];
1559 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001560
chaviwd1c23182019-12-20 18:44:56 -08001561 for (size_t i = 0; i < pointerCount; i++) {
1562 pointerProperties[i].clear();
1563 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001564 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001565
chaviwd1c23182019-12-20 18:44:56 -08001566 pointerCoords[i].clear();
1567 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1568 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1569 }
Jackal Guof9696682018-10-05 12:23:23 +08001570
1571 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1572 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001573 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001574 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1575 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001576 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1577 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001578 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1579 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001580
1581 return args;
1582}
1583
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001584static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1585 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1586}
1587
chaviwd1c23182019-12-20 18:44:56 -08001588static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1589 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1590}
1591
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001592static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1593 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001594 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001595}
1596
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -07001597} // namespace
1598
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001599/**
1600 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1601 * broken channel.
1602 */
1603TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1604 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1605 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001606 sp<FakeWindowHandle>::make(application, mDispatcher,
1607 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001608
1609 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1610
1611 // Window closes its channel, but the window remains.
1612 window->destroyReceiver();
1613 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1614}
1615
Arthur Hungb92218b2018-08-14 12:00:21 +08001616TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001617 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001618 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1619 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001620
Arthur Hung72d8dc32020-03-28 00:48:39 +00001621 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001622 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1623 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1624 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001625
1626 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001627 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001628}
1629
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001630TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1631 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001632 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1633 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001634
1635 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1636 // Inject a MotionEvent to an unknown display.
1637 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1638 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1639 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1640
1641 // Window should receive motion event.
1642 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1643}
1644
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001645/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001646 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001647 * This test serves as a sanity check for the next test, where setInputWindows is
1648 * called twice.
1649 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001650TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001651 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001652 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1653 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001654 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001655
1656 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001657 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001658 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1659 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001660 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001661
1662 // Window should receive motion event.
1663 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1664}
1665
1666/**
1667 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001668 */
1669TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001670 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001671 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1672 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001673 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001674
1675 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1676 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001677 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001678 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1679 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001680 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001681
1682 // Window should receive motion event.
1683 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1684}
1685
Arthur Hungb92218b2018-08-14 12:00:21 +08001686// The foreground window should receive the first touch down event.
1687TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001688 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001689 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001690 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001691 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001692 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001693
Arthur Hung72d8dc32020-03-28 00:48:39 +00001694 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001695 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1696 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1697 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001698
1699 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001700 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001701 windowSecond->assertNoEvents();
1702}
1703
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001704/**
1705 * Two windows: A top window, and a wallpaper behind the window.
1706 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1707 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001708 * 1. foregroundWindow <-- dup touch to wallpaper
1709 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001710 */
1711TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1712 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1713 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001714 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001715 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001716 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001717 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001718 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001719
1720 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1721 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1722 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1723 {100, 200}))
1724 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1725
1726 // Both foreground window and its wallpaper should receive the touch down
1727 foregroundWindow->consumeMotionDown();
1728 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1729
1730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1731 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1732 ADISPLAY_ID_DEFAULT, {110, 200}))
1733 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1734
1735 foregroundWindow->consumeMotionMove();
1736 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1737
1738 // Now the foreground window goes away, but the wallpaper stays
1739 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1740 foregroundWindow->consumeMotionCancel();
1741 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1742 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1743}
1744
1745/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001746 * Two fingers down on the window, and lift off the first finger.
1747 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1748 * contains a single pointer.
1749 */
1750TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1751 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1752 sp<FakeWindowHandle> window =
1753 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1754
1755 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001756 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001757 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1758 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1759 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001760 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001761 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1762 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1763 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1764 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001765 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001766 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1767 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1768 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1769 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001770 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1771 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1772 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1773
1774 // Remove the window. The gesture should be canceled
1775 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
1776 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1777 window->consumeMotionEvent(
1778 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1779}
1780
1781/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001782 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1783 * with the following differences:
1784 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1785 * clean up the connection.
1786 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1787 * Ensure that there's no crash in the dispatcher.
1788 */
1789TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1790 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1791 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001792 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001793 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001794 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001795 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001796 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001797
1798 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1799 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1800 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1801 {100, 200}))
1802 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1803
1804 // Both foreground window and its wallpaper should receive the touch down
1805 foregroundWindow->consumeMotionDown();
1806 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1807
1808 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1809 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1810 ADISPLAY_ID_DEFAULT, {110, 200}))
1811 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1812
1813 foregroundWindow->consumeMotionMove();
1814 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1815
1816 // Wallpaper closes its channel, but the window remains.
1817 wallpaperWindow->destroyReceiver();
1818 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1819
1820 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1821 // is no longer valid.
1822 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1823 foregroundWindow->consumeMotionCancel();
1824}
1825
Arthur Hungc539dbb2022-12-08 07:45:36 +00001826class ShouldSplitTouchFixture : public InputDispatcherTest,
1827 public ::testing::WithParamInterface<bool> {};
1828INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1829 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001830/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001831 * A single window that receives touch (on top), and a wallpaper window underneath it.
1832 * The top window gets a multitouch gesture.
1833 * Ensure that wallpaper gets the same gesture.
1834 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00001835TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001836 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001837 sp<FakeWindowHandle> foregroundWindow =
1838 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1839 foregroundWindow->setDupTouchToWallpaper(true);
1840 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001841
1842 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001843 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001844 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001845
Arthur Hungc539dbb2022-12-08 07:45:36 +00001846 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001847
1848 // Touch down on top window
1849 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1850 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1851 {100, 100}))
1852 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1853
1854 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00001855 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001856 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1857
1858 // Second finger down on the top window
1859 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001860 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001861 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001862 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1863 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001864 .build();
1865 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1866 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1867 InputEventInjectionSync::WAIT_FOR_RESULT))
1868 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1869
Harry Cutts33476232023-01-30 19:57:29 +00001870 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
1871 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001872 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001873
1874 const MotionEvent secondFingerUpEvent =
1875 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1876 .displayId(ADISPLAY_ID_DEFAULT)
1877 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001878 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1879 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001880 .build();
1881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1882 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
1883 InputEventInjectionSync::WAIT_FOR_RESULT))
1884 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1885 foregroundWindow->consumeMotionPointerUp(0);
1886 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1887
1888 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001889 injectMotionEvent(mDispatcher,
1890 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
1891 AINPUT_SOURCE_TOUCHSCREEN)
1892 .displayId(ADISPLAY_ID_DEFAULT)
1893 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1894 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001895 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001896 .x(100)
1897 .y(100))
1898 .build(),
1899 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001900 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1901 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1902 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001903}
1904
1905/**
1906 * Two windows: a window on the left and window on the right.
1907 * A third window, wallpaper, is behind both windows, and spans both top windows.
1908 * The first touch down goes to the left window. A second pointer touches down on the right window.
1909 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1910 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1911 * ACTION_POINTER_DOWN(1).
1912 */
1913TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1914 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1915 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001916 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001917 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001918 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001919
1920 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001921 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001922 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001923 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001924
1925 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001926 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001927 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001928 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001929
1930 mDispatcher->setInputWindows(
1931 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
1932
1933 // Touch down on left window
1934 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1935 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1936 {100, 100}))
1937 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1938
1939 // Both foreground window and its wallpaper should receive the touch down
1940 leftWindow->consumeMotionDown();
1941 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1942
1943 // Second finger down on the right window
1944 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001945 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001946 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001947 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1948 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001949 .build();
1950 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1951 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1952 InputEventInjectionSync::WAIT_FOR_RESULT))
1953 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1954
1955 leftWindow->consumeMotionMove();
1956 // Since the touch is split, right window gets ACTION_DOWN
1957 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00001958 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001959 expectedWallpaperFlags);
1960
1961 // Now, leftWindow, which received the first finger, disappears.
1962 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
1963 leftWindow->consumeMotionCancel();
1964 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1965 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1966
1967 // The pointer that's still down on the right window moves, and goes to the right window only.
1968 // As far as the dispatcher's concerned though, both pointers are still present.
1969 const MotionEvent secondFingerMoveEvent =
1970 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1971 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001972 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1973 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001974 .build();
1975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1976 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
1977 InputEventInjectionSync::WAIT_FOR_RESULT));
1978 rightWindow->consumeMotionMove();
1979
1980 leftWindow->assertNoEvents();
1981 rightWindow->assertNoEvents();
1982 wallpaperWindow->assertNoEvents();
1983}
1984
Arthur Hungc539dbb2022-12-08 07:45:36 +00001985/**
1986 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
1987 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
1988 * The right window should receive ACTION_DOWN.
1989 */
1990TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00001991 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001992 sp<FakeWindowHandle> leftWindow =
1993 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1994 leftWindow->setFrame(Rect(0, 0, 200, 200));
1995 leftWindow->setDupTouchToWallpaper(true);
1996 leftWindow->setSlippery(true);
1997
1998 sp<FakeWindowHandle> rightWindow =
1999 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2000 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002001
2002 sp<FakeWindowHandle> wallpaperWindow =
2003 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2004 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002005
Arthur Hungc539dbb2022-12-08 07:45:36 +00002006 mDispatcher->setInputWindows(
2007 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002008
Arthur Hungc539dbb2022-12-08 07:45:36 +00002009 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002010 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2011 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002012 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002013 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002014
2015 // Both foreground window and its wallpaper should receive the touch down
2016 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002017 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2018
Arthur Hungc539dbb2022-12-08 07:45:36 +00002019 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002020 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002021 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2022 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002023 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2024
Arthur Hungc539dbb2022-12-08 07:45:36 +00002025 leftWindow->consumeMotionCancel();
2026 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2027 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002028}
2029
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002030/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002031 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2032 * interactive, it might stop sending this flag.
2033 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2034 * to have a consistent input stream.
2035 *
2036 * Test procedure:
2037 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2038 * DOWN (new gesture).
2039 *
2040 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2041 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2042 *
2043 * We technically just need a single window here, but we are using two windows (spy on top and a
2044 * regular window below) to emulate the actual situation where it happens on the device.
2045 */
2046TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2047 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2048 sp<FakeWindowHandle> spyWindow =
2049 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2050 spyWindow->setFrame(Rect(0, 0, 200, 200));
2051 spyWindow->setTrustedOverlay(true);
2052 spyWindow->setSpy(true);
2053
2054 sp<FakeWindowHandle> window =
2055 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2056 window->setFrame(Rect(0, 0, 200, 200));
2057
2058 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2059 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002060
2061 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002062 mDispatcher->notifyMotion(
2063 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2064 .deviceId(touchDeviceId)
2065 .policyFlags(DEFAULT_POLICY_FLAGS)
2066 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2067 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002068
Prabir Pradhan678438e2023-04-13 19:32:51 +00002069 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2070 .deviceId(touchDeviceId)
2071 .policyFlags(DEFAULT_POLICY_FLAGS)
2072 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2073 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2074 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002075 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2076 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2077 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2078 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2079
2080 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002081 mDispatcher->notifyMotion(
2082 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2083 .deviceId(touchDeviceId)
2084 .policyFlags(0)
2085 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2086 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2087 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002088 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2089 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2090
2091 // We don't need to reset the device to reproduce the issue, but the reset event typically
2092 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002093 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002094
2095 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002096 mDispatcher->notifyMotion(
2097 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2098 .deviceId(touchDeviceId)
2099 .policyFlags(DEFAULT_POLICY_FLAGS)
2100 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2101 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002102 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2103 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2104
2105 // No more events
2106 spyWindow->assertNoEvents();
2107 window->assertNoEvents();
2108}
2109
2110/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002111 * Two windows: a window on the left and a window on the right.
2112 * Mouse is hovered from the right window into the left window.
2113 * Next, we tap on the left window, where the cursor was last seen.
2114 * The second tap is done onto the right window.
2115 * The mouse and tap are from two different devices.
2116 * We technically don't need to set the downtime / eventtime for these events, but setting these
2117 * explicitly helps during debugging.
2118 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2119 * In the buggy implementation, a tap on the right window would cause a crash.
2120 */
2121TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2122 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2123 sp<FakeWindowHandle> leftWindow =
2124 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2125 leftWindow->setFrame(Rect(0, 0, 200, 200));
2126
2127 sp<FakeWindowHandle> rightWindow =
2128 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2129 rightWindow->setFrame(Rect(200, 0, 400, 200));
2130
2131 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2132 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2133 // stale.
2134 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2135 const int32_t mouseDeviceId = 6;
2136 const int32_t touchDeviceId = 4;
2137 // Move the cursor from right
2138 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2139 injectMotionEvent(mDispatcher,
2140 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2141 AINPUT_SOURCE_MOUSE)
2142 .deviceId(mouseDeviceId)
2143 .downTime(baseTime + 10)
2144 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002145 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002146 .x(300)
2147 .y(100))
2148 .build()));
2149 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2150
2151 // .. to the left window
2152 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2153 injectMotionEvent(mDispatcher,
2154 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2155 AINPUT_SOURCE_MOUSE)
2156 .deviceId(mouseDeviceId)
2157 .downTime(baseTime + 10)
2158 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002159 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002160 .x(110)
2161 .y(100))
2162 .build()));
2163 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2164 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2165 // Now tap the left window
2166 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2167 injectMotionEvent(mDispatcher,
2168 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2169 AINPUT_SOURCE_TOUCHSCREEN)
2170 .deviceId(touchDeviceId)
2171 .downTime(baseTime + 40)
2172 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002173 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002174 .x(100)
2175 .y(100))
2176 .build()));
2177 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2178 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2179
2180 // release tap
2181 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2182 injectMotionEvent(mDispatcher,
2183 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2184 AINPUT_SOURCE_TOUCHSCREEN)
2185 .deviceId(touchDeviceId)
2186 .downTime(baseTime + 40)
2187 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002188 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002189 .x(100)
2190 .y(100))
2191 .build()));
2192 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2193
2194 // Tap the window on the right
2195 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2196 injectMotionEvent(mDispatcher,
2197 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2198 AINPUT_SOURCE_TOUCHSCREEN)
2199 .deviceId(touchDeviceId)
2200 .downTime(baseTime + 60)
2201 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002202 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002203 .x(300)
2204 .y(100))
2205 .build()));
2206 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2207
2208 // release tap
2209 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2210 injectMotionEvent(mDispatcher,
2211 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2212 AINPUT_SOURCE_TOUCHSCREEN)
2213 .deviceId(touchDeviceId)
2214 .downTime(baseTime + 60)
2215 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002216 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002217 .x(300)
2218 .y(100))
2219 .build()));
2220 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2221
2222 // No more events
2223 leftWindow->assertNoEvents();
2224 rightWindow->assertNoEvents();
2225}
2226
2227/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002228 * Start hovering in a window. While this hover is still active, make another window appear on top.
2229 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2230 * While the top window is present, the hovering is stopped.
2231 * Later, hovering gets resumed again.
2232 * Ensure that new hover gesture is handled correctly.
2233 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2234 * to the window that's currently being hovered over.
2235 */
2236TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2237 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2238 sp<FakeWindowHandle> window =
2239 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2240 window->setFrame(Rect(0, 0, 200, 200));
2241
2242 // Only a single window is present at first
2243 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2244
2245 // Start hovering in the window
2246 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2247 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2248 .build());
2249 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2250
2251 // Now, an obscuring window appears!
2252 sp<FakeWindowHandle> obscuringWindow =
2253 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2254 ADISPLAY_ID_DEFAULT,
2255 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2256 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2257 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2258 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2259 obscuringWindow->setNoInputChannel(true);
2260 obscuringWindow->setFocusable(false);
2261 obscuringWindow->setAlpha(1.0);
2262 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2263
2264 // While this new obscuring window is present, the hovering is stopped
2265 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2266 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2267 .build());
2268 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2269
2270 // Now the obscuring window goes away.
2271 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2272
2273 // And a new hover gesture starts.
2274 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2275 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2276 .build());
2277 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2278}
2279
2280/**
2281 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2282 * the obscuring window.
2283 */
2284TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2285 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2286 sp<FakeWindowHandle> window =
2287 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2288 window->setFrame(Rect(0, 0, 200, 200));
2289
2290 // Only a single window is present at first
2291 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2292
2293 // Start hovering in the window
2294 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2295 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2296 .build());
2297 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2298
2299 // Now, an obscuring window appears!
2300 sp<FakeWindowHandle> obscuringWindow =
2301 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2302 ADISPLAY_ID_DEFAULT,
2303 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2304 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2305 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2306 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2307 obscuringWindow->setNoInputChannel(true);
2308 obscuringWindow->setFocusable(false);
2309 obscuringWindow->setAlpha(1.0);
2310 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2311
2312 // While this new obscuring window is present, the hovering continues. The event can't go to the
2313 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2314 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2315 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2316 .build());
2317 obscuringWindow->assertNoEvents();
2318 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2319
2320 // Now the obscuring window goes away.
2321 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2322
2323 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2324 // so it should generate a HOVER_ENTER
2325 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2326 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2327 .build());
2328 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2329
2330 // Now the MOVE should be getting dispatched normally
2331 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2332 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2333 .build());
2334 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2335}
2336
2337/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002338 * Two windows: a window on the left and a window on the right.
2339 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2340 * down. Then, on the left window, also place second touch pointer down.
2341 * This test tries to reproduce a crash.
2342 * In the buggy implementation, second pointer down on the left window would cause a crash.
2343 */
2344TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2345 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2346 sp<FakeWindowHandle> leftWindow =
2347 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2348 leftWindow->setFrame(Rect(0, 0, 200, 200));
2349
2350 sp<FakeWindowHandle> rightWindow =
2351 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2352 rightWindow->setFrame(Rect(200, 0, 400, 200));
2353
2354 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2355
2356 const int32_t touchDeviceId = 4;
2357 const int32_t mouseDeviceId = 6;
2358 NotifyMotionArgs args;
2359
2360 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002361 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2362 .deviceId(mouseDeviceId)
2363 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2364 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002365 leftWindow->consumeMotionEvent(
2366 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2367
2368 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002369 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2370 .deviceId(mouseDeviceId)
2371 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2372 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2373 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002374
2375 leftWindow->consumeMotionEvent(
2376 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2377 leftWindow->consumeMotionEvent(
2378 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2379
Prabir Pradhan678438e2023-04-13 19:32:51 +00002380 mDispatcher->notifyMotion(
2381 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2382 .deviceId(mouseDeviceId)
2383 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2384 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2385 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2386 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002387 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2388
2389 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002390 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2391 .deviceId(touchDeviceId)
2392 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2393 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002394 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2395
2396 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2397
2398 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002399 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2400 .deviceId(touchDeviceId)
2401 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2402 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2403 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002404 leftWindow->consumeMotionEvent(
2405 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2406 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2407 // current implementation.
2408 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2409 rightWindow->consumeMotionEvent(
2410 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2411
2412 leftWindow->assertNoEvents();
2413 rightWindow->assertNoEvents();
2414}
2415
2416/**
2417 * On a single window, use two different devices: mouse and touch.
2418 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2419 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2420 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2421 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2422 * represent a new gesture.
2423 */
2424TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2425 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2426 sp<FakeWindowHandle> window =
2427 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2428 window->setFrame(Rect(0, 0, 400, 400));
2429
2430 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2431
2432 const int32_t touchDeviceId = 4;
2433 const int32_t mouseDeviceId = 6;
2434 NotifyMotionArgs args;
2435
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002436 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002437 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2438 .deviceId(touchDeviceId)
2439 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2440 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002441 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002442 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2443 .deviceId(touchDeviceId)
2444 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2445 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2446 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002447 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002448 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2449 .deviceId(touchDeviceId)
2450 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2451 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2452 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002453 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2454 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2455 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2456
2457 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002458 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2459 .deviceId(mouseDeviceId)
2460 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2461 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2462 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002463
2464 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002465 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002466 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2467
Prabir Pradhan678438e2023-04-13 19:32:51 +00002468 mDispatcher->notifyMotion(
2469 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2470 .deviceId(mouseDeviceId)
2471 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2472 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2473 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2474 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002475 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2476
2477 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002478 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2479 .deviceId(touchDeviceId)
2480 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2481 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2482 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002483 // The pointer_down event should be ignored
2484 window->assertNoEvents();
2485}
2486
2487/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002488 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2489 * the injected event.
2490 */
2491TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2492 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2493 sp<FakeWindowHandle> window =
2494 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2495 window->setFrame(Rect(0, 0, 400, 400));
2496
2497 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2498
2499 const int32_t touchDeviceId = 4;
2500 NotifyMotionArgs args;
2501 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2502 // completion.
2503 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2504 injectMotionEvent(mDispatcher,
2505 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2506 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002507 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002508 .x(50)
2509 .y(50))
2510 .build()));
2511 window->consumeMotionEvent(
2512 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2513
2514 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2515 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002516 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2517 .deviceId(touchDeviceId)
2518 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2519 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002520
2521 window->consumeMotionEvent(
2522 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2523 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2524}
2525
2526/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002527 * This test is similar to the test above, but the sequence of injected events is different.
2528 *
2529 * Two windows: a window on the left and a window on the right.
2530 * Mouse is hovered over the left window.
2531 * Next, we tap on the left window, where the cursor was last seen.
2532 *
2533 * After that, we inject one finger down onto the right window, and then a second finger down onto
2534 * the left window.
2535 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2536 * window (first), and then another on the left window (second).
2537 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2538 * In the buggy implementation, second finger down on the left window would cause a crash.
2539 */
2540TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2541 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2542 sp<FakeWindowHandle> leftWindow =
2543 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2544 leftWindow->setFrame(Rect(0, 0, 200, 200));
2545
2546 sp<FakeWindowHandle> rightWindow =
2547 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2548 rightWindow->setFrame(Rect(200, 0, 400, 200));
2549
2550 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2551
2552 const int32_t mouseDeviceId = 6;
2553 const int32_t touchDeviceId = 4;
2554 // Hover over the left window. Keep the cursor there.
2555 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2556 injectMotionEvent(mDispatcher,
2557 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2558 AINPUT_SOURCE_MOUSE)
2559 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002560 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002561 .x(50)
2562 .y(50))
2563 .build()));
2564 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2565
2566 // Tap on left window
2567 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2568 injectMotionEvent(mDispatcher,
2569 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2570 AINPUT_SOURCE_TOUCHSCREEN)
2571 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002572 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002573 .x(100)
2574 .y(100))
2575 .build()));
2576
2577 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2578 injectMotionEvent(mDispatcher,
2579 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2580 AINPUT_SOURCE_TOUCHSCREEN)
2581 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002582 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002583 .x(100)
2584 .y(100))
2585 .build()));
2586 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2587 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2588 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2589
2590 // First finger down on right window
2591 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2592 injectMotionEvent(mDispatcher,
2593 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2594 AINPUT_SOURCE_TOUCHSCREEN)
2595 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002596 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002597 .x(300)
2598 .y(100))
2599 .build()));
2600 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2601
2602 // Second finger down on the left window
2603 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2604 injectMotionEvent(mDispatcher,
2605 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2606 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002607 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002608 .x(300)
2609 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002610 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002611 .x(100)
2612 .y(100))
2613 .build()));
2614 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2615 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2616
2617 // No more events
2618 leftWindow->assertNoEvents();
2619 rightWindow->assertNoEvents();
2620}
2621
2622/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002623 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2624 * While the touch is down, new hover events from the stylus device should be ignored. After the
2625 * touch is gone, stylus hovering should start working again.
2626 */
2627TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2628 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2629 sp<FakeWindowHandle> window =
2630 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2631 window->setFrame(Rect(0, 0, 200, 200));
2632
2633 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2634
2635 const int32_t stylusDeviceId = 5;
2636 const int32_t touchDeviceId = 4;
2637 // Start hovering with stylus
2638 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2639 injectMotionEvent(mDispatcher,
2640 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2641 AINPUT_SOURCE_STYLUS)
2642 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002643 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002644 .x(50)
2645 .y(50))
2646 .build()));
2647 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2648
2649 // Finger down on the window
2650 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2651 injectMotionEvent(mDispatcher,
2652 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2653 AINPUT_SOURCE_TOUCHSCREEN)
2654 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002655 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002656 .x(100)
2657 .y(100))
2658 .build()));
2659 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2660 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2661
2662 // Try to continue hovering with stylus. Since we are already down, injection should fail
2663 ASSERT_EQ(InputEventInjectionResult::FAILED,
2664 injectMotionEvent(mDispatcher,
2665 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2666 AINPUT_SOURCE_STYLUS)
2667 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002668 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002669 .x(50)
2670 .y(50))
2671 .build()));
2672 // No event should be sent. This event should be ignored because a pointer from another device
2673 // is already down.
2674
2675 // Lift up the finger
2676 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2677 injectMotionEvent(mDispatcher,
2678 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2679 AINPUT_SOURCE_TOUCHSCREEN)
2680 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002681 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002682 .x(100)
2683 .y(100))
2684 .build()));
2685 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2686
2687 // Now that the touch is gone, stylus hovering should start working again
2688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2689 injectMotionEvent(mDispatcher,
2690 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2691 AINPUT_SOURCE_STYLUS)
2692 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002693 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002694 .x(50)
2695 .y(50))
2696 .build()));
2697 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2698 // No more events
2699 window->assertNoEvents();
2700}
2701
2702/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002703 * A spy window above a window with no input channel.
2704 * Start hovering with a stylus device, and then tap with it.
2705 * Ensure spy window receives the entire sequence.
2706 */
2707TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2708 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2709 sp<FakeWindowHandle> spyWindow =
2710 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2711 spyWindow->setFrame(Rect(0, 0, 200, 200));
2712 spyWindow->setTrustedOverlay(true);
2713 spyWindow->setSpy(true);
2714 sp<FakeWindowHandle> window =
2715 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2716 window->setNoInputChannel(true);
2717 window->setFrame(Rect(0, 0, 200, 200));
2718
2719 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2720
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002721 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002722 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2723 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2724 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002725 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2726 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002727 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2728 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2729 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002730 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2731
2732 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002733 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2734 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2735 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002736 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2737
2738 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002739 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
2740 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2741 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002742 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2743
2744 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002745 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2746 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2747 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002748 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2749 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002750 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2751 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2752 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002753 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2754
2755 // No more events
2756 spyWindow->assertNoEvents();
2757 window->assertNoEvents();
2758}
2759
2760/**
2761 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
2762 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
2763 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
2764 * While the mouse is down, new move events from the touch device should be ignored.
2765 */
2766TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
2767 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2768 sp<FakeWindowHandle> spyWindow =
2769 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2770 spyWindow->setFrame(Rect(0, 0, 200, 200));
2771 spyWindow->setTrustedOverlay(true);
2772 spyWindow->setSpy(true);
2773 sp<FakeWindowHandle> window =
2774 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2775 window->setFrame(Rect(0, 0, 200, 200));
2776
2777 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2778
2779 const int32_t mouseDeviceId = 7;
2780 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002781
2782 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00002783 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2784 .deviceId(mouseDeviceId)
2785 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2786 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002787 spyWindow->consumeMotionEvent(
2788 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2789 window->consumeMotionEvent(
2790 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2791
2792 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00002793 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2794 .deviceId(touchDeviceId)
2795 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
2796 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002797 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2798 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2799 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2800 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2801
Prabir Pradhan678438e2023-04-13 19:32:51 +00002802 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2803 .deviceId(touchDeviceId)
2804 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
2805 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002806 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2807 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2808
2809 // Pilfer the stream
2810 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2811 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2812
Prabir Pradhan678438e2023-04-13 19:32:51 +00002813 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2814 .deviceId(touchDeviceId)
2815 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
2816 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002817 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2818
2819 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002820 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2821 .deviceId(mouseDeviceId)
2822 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2823 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2824 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002825
2826 spyWindow->consumeMotionEvent(
2827 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2828 spyWindow->consumeMotionEvent(
2829 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2830 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2831
Prabir Pradhan678438e2023-04-13 19:32:51 +00002832 mDispatcher->notifyMotion(
2833 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2834 .deviceId(mouseDeviceId)
2835 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2836 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2837 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2838 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002839 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2840 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2841
2842 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002843 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
2844 .deviceId(mouseDeviceId)
2845 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2846 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
2847 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002848 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2849 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2850
2851 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002852 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2853 .deviceId(touchDeviceId)
2854 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
2855 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002856
2857 // No more events
2858 spyWindow->assertNoEvents();
2859 window->assertNoEvents();
2860}
2861
2862/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002863 * On the display, have a single window, and also an area where there's no window.
2864 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2865 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2866 */
2867TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2868 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2869 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002870 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002871
2872 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002873
2874 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00002875 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002876
2877 mDispatcher->waitForIdle();
2878 window->assertNoEvents();
2879
2880 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002881 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002882 mDispatcher->waitForIdle();
2883 window->consumeMotionDown();
2884}
2885
2886/**
2887 * Same test as above, but instead of touching the empty space, the first touch goes to
2888 * non-touchable window.
2889 */
2890TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2891 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2892 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002893 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002894 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2895 window1->setTouchable(false);
2896 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002897 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002898 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2899
2900 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2901
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002902 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002903 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002904
2905 mDispatcher->waitForIdle();
2906 window1->assertNoEvents();
2907 window2->assertNoEvents();
2908
2909 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002910 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002911 mDispatcher->waitForIdle();
2912 window2->consumeMotionDown();
2913}
2914
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002915/**
2916 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2917 * to the event time of the first ACTION_DOWN sent to the particular window.
2918 */
2919TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2920 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2921 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002922 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002923 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2924 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002925 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002926 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2927
2928 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2929
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002930 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002931 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002932
2933 mDispatcher->waitForIdle();
2934 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002935 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002936 window2->assertNoEvents();
2937 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2938 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2939 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2940
2941 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002942 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002943 mDispatcher->waitForIdle();
2944 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002945 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002946 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2947 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2948 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2949 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2950
2951 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002952 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002953 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002954 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002955
2956 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002957 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002958 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002959 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002960
2961 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
2962 window1->consumeMotionMove();
2963 window1->assertNoEvents();
2964
2965 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002966 mDispatcher->notifyMotion(
2967 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002968 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002969 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002970
Prabir Pradhan678438e2023-04-13 19:32:51 +00002971 mDispatcher->notifyMotion(
2972 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002973 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002974 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002975}
2976
Garfield Tandf26e862020-07-01 20:18:19 -07002977TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002978 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002979 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002980 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002981 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002982 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002983 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002984 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002985
2986 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2987
2988 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
2989
2990 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002991 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002992 injectMotionEvent(mDispatcher,
2993 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2994 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002995 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07002996 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002997 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002998
2999 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003000 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003001 injectMotionEvent(mDispatcher,
3002 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3003 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003004 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003005 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003006 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3007 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003008
3009 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003010 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003011 injectMotionEvent(mDispatcher,
3012 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3013 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003014 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003015 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003016 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3017 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003018
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003019 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003020 injectMotionEvent(mDispatcher,
3021 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3022 AINPUT_SOURCE_MOUSE)
3023 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3024 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003025 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003026 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003027 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003028
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003029 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003030 injectMotionEvent(mDispatcher,
3031 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3032 AINPUT_SOURCE_MOUSE)
3033 .buttonState(0)
3034 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003035 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003036 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003037 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003038
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003039 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003040 injectMotionEvent(mDispatcher,
3041 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3042 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003043 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003044 .build()));
3045 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3046
3047 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003048 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003049 injectMotionEvent(mDispatcher,
3050 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3051 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003052 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003053 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003054 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003055
3056 // No more events
3057 windowLeft->assertNoEvents();
3058 windowRight->assertNoEvents();
3059}
3060
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003061/**
3062 * Put two fingers down (and don't release them) and click the mouse button.
3063 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3064 * currently active gesture should be canceled, and the new one should proceed.
3065 */
3066TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3067 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3068 sp<FakeWindowHandle> window =
3069 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3070 window->setFrame(Rect(0, 0, 600, 800));
3071
3072 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3073
3074 const int32_t touchDeviceId = 4;
3075 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003076
3077 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003078 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3079 .deviceId(touchDeviceId)
3080 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3081 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003082
Prabir Pradhan678438e2023-04-13 19:32:51 +00003083 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3084 .deviceId(touchDeviceId)
3085 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3086 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3087 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003088 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3089 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3090
3091 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003092 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3093 .deviceId(mouseDeviceId)
3094 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3095 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3096 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003097 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3098 WithPointerCount(2u)));
3099 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3100
Prabir Pradhan678438e2023-04-13 19:32:51 +00003101 mDispatcher->notifyMotion(
3102 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3103 .deviceId(mouseDeviceId)
3104 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3105 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3106 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3107 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003108 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3109
3110 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3111 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003112 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3113 .deviceId(touchDeviceId)
3114 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3115 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3116 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003117 window->assertNoEvents();
3118}
3119
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003120TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3121 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3122
3123 sp<FakeWindowHandle> spyWindow =
3124 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3125 spyWindow->setFrame(Rect(0, 0, 600, 800));
3126 spyWindow->setTrustedOverlay(true);
3127 spyWindow->setSpy(true);
3128 sp<FakeWindowHandle> window =
3129 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3130 window->setFrame(Rect(0, 0, 600, 800));
3131
3132 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3133 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3134
3135 // Send mouse cursor to the window
3136 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3137 injectMotionEvent(mDispatcher,
3138 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3139 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003140 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003141 .x(100)
3142 .y(100))
3143 .build()));
3144
3145 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3146 WithSource(AINPUT_SOURCE_MOUSE)));
3147 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3148 WithSource(AINPUT_SOURCE_MOUSE)));
3149
3150 window->assertNoEvents();
3151 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003152}
3153
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003154TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3155 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3156
3157 sp<FakeWindowHandle> spyWindow =
3158 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3159 spyWindow->setFrame(Rect(0, 0, 600, 800));
3160 spyWindow->setTrustedOverlay(true);
3161 spyWindow->setSpy(true);
3162 sp<FakeWindowHandle> window =
3163 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3164 window->setFrame(Rect(0, 0, 600, 800));
3165
3166 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3167 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3168
3169 // Send mouse cursor to the window
3170 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3171 injectMotionEvent(mDispatcher,
3172 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3173 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003174 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003175 .x(100)
3176 .y(100))
3177 .build()));
3178
3179 // Move mouse cursor
3180 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3181 injectMotionEvent(mDispatcher,
3182 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3183 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003184 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003185 .x(110)
3186 .y(110))
3187 .build()));
3188
3189 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3190 WithSource(AINPUT_SOURCE_MOUSE)));
3191 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3192 WithSource(AINPUT_SOURCE_MOUSE)));
3193 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3194 WithSource(AINPUT_SOURCE_MOUSE)));
3195 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3196 WithSource(AINPUT_SOURCE_MOUSE)));
3197 // Touch down on the window
3198 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3199 injectMotionEvent(mDispatcher,
3200 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3201 AINPUT_SOURCE_TOUCHSCREEN)
3202 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003203 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003204 .x(200)
3205 .y(200))
3206 .build()));
3207 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3208 WithSource(AINPUT_SOURCE_MOUSE)));
3209 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3210 WithSource(AINPUT_SOURCE_MOUSE)));
3211 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3212 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3213 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3214 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3215
3216 // pilfer the motion, retaining the gesture on the spy window.
3217 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3218 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3219 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3220
3221 // Touch UP on the window
3222 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3223 injectMotionEvent(mDispatcher,
3224 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3225 AINPUT_SOURCE_TOUCHSCREEN)
3226 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003227 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003228 .x(200)
3229 .y(200))
3230 .build()));
3231 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3232 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3233
3234 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3235 // to send a new gesture. It should again go to both windows (spy and the window below), just
3236 // like the first gesture did, before pilfering. The window configuration has not changed.
3237
3238 // One more tap - DOWN
3239 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3240 injectMotionEvent(mDispatcher,
3241 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3242 AINPUT_SOURCE_TOUCHSCREEN)
3243 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003244 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003245 .x(250)
3246 .y(250))
3247 .build()));
3248 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3249 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3250 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3251 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3252
3253 // Touch UP on the window
3254 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3255 injectMotionEvent(mDispatcher,
3256 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3257 AINPUT_SOURCE_TOUCHSCREEN)
3258 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003259 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003260 .x(250)
3261 .y(250))
3262 .build()));
3263 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3264 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3265 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3266 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3267
3268 window->assertNoEvents();
3269 spyWindow->assertNoEvents();
3270}
3271
Garfield Tandf26e862020-07-01 20:18:19 -07003272// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3273// directly in this test.
3274TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003275 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003276 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003277 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003278 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003279
3280 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3281
3282 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3283
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003284 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003285 injectMotionEvent(mDispatcher,
3286 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3287 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003288 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003289 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003290 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003291 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003292 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003293 injectMotionEvent(mDispatcher,
3294 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3295 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003296 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003297 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003298 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3299 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003300
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003301 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003302 injectMotionEvent(mDispatcher,
3303 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3304 AINPUT_SOURCE_MOUSE)
3305 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3306 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003307 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003308 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003309 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003310
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003311 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003312 injectMotionEvent(mDispatcher,
3313 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3314 AINPUT_SOURCE_MOUSE)
3315 .buttonState(0)
3316 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003317 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003318 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003319 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003320
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003321 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003322 injectMotionEvent(mDispatcher,
3323 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3324 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003325 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003326 .build()));
3327 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3328
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07003329 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
3330 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
3331 ASSERT_EQ(InputEventInjectionResult::FAILED,
Garfield Tandf26e862020-07-01 20:18:19 -07003332 injectMotionEvent(mDispatcher,
3333 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3334 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003335 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003336 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003337 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003338}
3339
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003340/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003341 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3342 * is generated.
3343 */
3344TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3345 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3346 sp<FakeWindowHandle> window =
3347 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3348 window->setFrame(Rect(0, 0, 1200, 800));
3349
3350 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3351
3352 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3353
3354 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3355 injectMotionEvent(mDispatcher,
3356 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3357 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003358 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003359 .x(300)
3360 .y(400))
3361 .build()));
3362 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3363
3364 // Remove the window, but keep the channel.
3365 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3366 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3367}
3368
3369/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003370 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3371 */
3372TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3373 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3374 sp<FakeWindowHandle> window =
3375 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3376 window->setFrame(Rect(0, 0, 100, 100));
3377
3378 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3379
3380 const int32_t mouseDeviceId = 7;
3381 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003382
3383 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003384 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3385 .deviceId(mouseDeviceId)
3386 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3387 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003388 window->consumeMotionEvent(
3389 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3390
3391 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003392 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3393 .deviceId(touchDeviceId)
3394 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3395 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003396
3397 window->consumeMotionEvent(
3398 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3399 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3400}
3401
3402/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003403 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003404 * The tap causes a HOVER_EXIT event to be generated because the current event
3405 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003406 */
3407TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3408 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3409 sp<FakeWindowHandle> window =
3410 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3411 window->setFrame(Rect(0, 0, 100, 100));
3412
3413 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3414
3415 // Inject a hover_move from mouse.
3416 NotifyMotionArgs motionArgs =
3417 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3418 ADISPLAY_ID_DEFAULT, {{50, 50}});
3419 motionArgs.xCursorPosition = 50;
3420 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003421 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003422 ASSERT_NO_FATAL_FAILURE(
3423 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3424 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003425
3426 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003427 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3428 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3429 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003430 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003431 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3432 WithSource(AINPUT_SOURCE_MOUSE))));
3433
3434 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003435 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3436 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3437
Prabir Pradhan678438e2023-04-13 19:32:51 +00003438 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3439 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003440 ASSERT_NO_FATAL_FAILURE(
3441 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3442 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3443}
3444
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003445TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3446 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3447 sp<FakeWindowHandle> windowDefaultDisplay =
3448 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3449 ADISPLAY_ID_DEFAULT);
3450 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3451 sp<FakeWindowHandle> windowSecondDisplay =
3452 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3453 SECOND_DISPLAY_ID);
3454 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3455
3456 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3457 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3458
3459 // Set cursor position in window in default display and check that hover enter and move
3460 // events are generated.
3461 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3462 injectMotionEvent(mDispatcher,
3463 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3464 AINPUT_SOURCE_MOUSE)
3465 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003466 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003467 .x(300)
3468 .y(600))
3469 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003470 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003471
3472 // Remove all windows in secondary display and check that no event happens on window in
3473 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003474 mDispatcher->setInputWindows(
3475 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003476 windowDefaultDisplay->assertNoEvents();
3477
3478 // Move cursor position in window in default display and check that only hover move
3479 // event is generated and not hover enter event.
3480 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3481 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3482 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3483 injectMotionEvent(mDispatcher,
3484 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3485 AINPUT_SOURCE_MOUSE)
3486 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003487 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003488 .x(400)
3489 .y(700))
3490 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003491 windowDefaultDisplay->consumeMotionEvent(
3492 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3493 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003494 windowDefaultDisplay->assertNoEvents();
3495}
3496
Garfield Tan00f511d2019-06-12 16:55:40 -07003497TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003498 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003499
3500 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003501 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003502 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003503 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003504 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003505 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003506
3507 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3508
Arthur Hung72d8dc32020-03-28 00:48:39 +00003509 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003510
3511 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3512 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003513 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003514 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003515 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003516 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003517 windowRight->assertNoEvents();
3518}
3519
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003520TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003521 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003522 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3523 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003524 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003525
Arthur Hung72d8dc32020-03-28 00:48:39 +00003526 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003527 setFocusedWindow(window);
3528
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003529 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003530
Prabir Pradhan678438e2023-04-13 19:32:51 +00003531 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003532
3533 // Window should receive key down event.
3534 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3535
3536 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3537 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003538 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003539 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003540 AKEY_EVENT_FLAG_CANCELED);
3541}
3542
3543TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003544 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003545 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3546 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003547
Arthur Hung72d8dc32020-03-28 00:48:39 +00003548 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003549
Prabir Pradhan678438e2023-04-13 19:32:51 +00003550 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3551 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003552
3553 // Window should receive motion down event.
3554 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3555
3556 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3557 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003558 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003559 window->consumeMotionEvent(
3560 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003561}
3562
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003563TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
3564 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3565 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3566 "Fake Window", ADISPLAY_ID_DEFAULT);
3567
3568 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3569
3570 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3571 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
3572 .build());
3573
3574 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3575
3576 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
3577 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
3578 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3579
3580 // After the device has been reset, a new hovering stream can be sent to the window
3581 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3582 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
3583 .build());
3584 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3585}
3586
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003587TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3588 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003589 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3590 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003591 window->setFocusable(true);
3592
3593 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3594 setFocusedWindow(window);
3595
3596 window->consumeFocusEvent(true);
3597
Prabir Pradhan678438e2023-04-13 19:32:51 +00003598 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003599 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3600 const nsecs_t injectTime = keyArgs.eventTime;
3601 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003602 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003603 // The dispatching time should be always greater than or equal to intercept key timeout.
3604 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3605 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3606 std::chrono::nanoseconds(interceptKeyTimeout).count());
3607}
3608
3609TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3610 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003611 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3612 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003613 window->setFocusable(true);
3614
3615 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3616 setFocusedWindow(window);
3617
3618 window->consumeFocusEvent(true);
3619
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003620 mFakePolicy->setInterceptKeyTimeout(150ms);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003621 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
3622 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003623
3624 // Window should receive key event immediately when same key up.
3625 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3626 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3627}
3628
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003629/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003630 * Two windows. First is a regular window. Second does not overlap with the first, and has
3631 * WATCH_OUTSIDE_TOUCH.
3632 * Both windows are owned by the same UID.
3633 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3634 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3635 */
3636TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3637 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3638 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3639 "First Window", ADISPLAY_ID_DEFAULT);
3640 window->setFrame(Rect{0, 0, 100, 100});
3641
3642 sp<FakeWindowHandle> outsideWindow =
3643 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3644 ADISPLAY_ID_DEFAULT);
3645 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3646 outsideWindow->setWatchOutsideTouch(true);
3647 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3648 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3649
3650 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003651 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3652 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3653 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003654 window->consumeMotionDown();
3655 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3656 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3657 outsideWindow->consumeMotionEvent(
3658 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3659}
3660
3661/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003662 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3663 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3664 * ACTION_OUTSIDE event is sent per gesture.
3665 */
3666TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3667 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3668 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003669 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3670 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003671 window->setWatchOutsideTouch(true);
3672 window->setFrame(Rect{0, 0, 100, 100});
3673 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003674 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3675 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003676 secondWindow->setFrame(Rect{100, 100, 200, 200});
3677 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003678 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3679 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003680 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3681 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3682
3683 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003684 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3685 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3686 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003687 window->assertNoEvents();
3688 secondWindow->assertNoEvents();
3689
3690 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3691 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003692 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3693 ADISPLAY_ID_DEFAULT,
3694 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003695 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3696 window->consumeMotionEvent(
3697 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003698 secondWindow->consumeMotionDown();
3699 thirdWindow->assertNoEvents();
3700
3701 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3702 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003703 mDispatcher->notifyMotion(
3704 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3705 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003706 window->assertNoEvents();
3707 secondWindow->consumeMotionMove();
3708 thirdWindow->consumeMotionDown();
3709}
3710
Prabir Pradhan814fe082022-07-22 20:22:18 +00003711TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3712 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003713 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3714 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003715 window->setFocusable(true);
3716
Patrick Williamsd828f302023-04-28 17:52:08 -05003717 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003718 setFocusedWindow(window);
3719
3720 window->consumeFocusEvent(true);
3721
Prabir Pradhan678438e2023-04-13 19:32:51 +00003722 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3723 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3724 mDispatcher->notifyKey(keyDown);
3725 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003726
3727 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3728 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3729
3730 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003731 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003732
3733 window->consumeFocusEvent(false);
3734
Prabir Pradhan678438e2023-04-13 19:32:51 +00003735 mDispatcher->notifyKey(keyDown);
3736 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003737 window->assertNoEvents();
3738}
3739
Arthur Hung96483742022-11-15 03:30:48 +00003740TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3741 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3742 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3743 "Fake Window", ADISPLAY_ID_DEFAULT);
3744 // Ensure window is non-split and have some transform.
3745 window->setPreventSplitting(true);
3746 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05003747 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00003748
3749 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3750 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3751 {50, 50}))
3752 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3753 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3754
3755 const MotionEvent secondFingerDownEvent =
3756 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3757 .displayId(ADISPLAY_ID_DEFAULT)
3758 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003759 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3760 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003761 .build();
3762 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3763 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3764 InputEventInjectionSync::WAIT_FOR_RESULT))
3765 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3766
3767 const MotionEvent* event = window->consumeMotion();
3768 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3769 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3770 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3771 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3772 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3773}
3774
Harry Cuttsb166c002023-05-09 13:06:05 +00003775TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
3776 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3777 sp<FakeWindowHandle> window =
3778 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3779 window->setFrame(Rect(0, 0, 400, 400));
3780 sp<FakeWindowHandle> trustedOverlay =
3781 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
3782 ADISPLAY_ID_DEFAULT);
3783 trustedOverlay->setSpy(true);
3784 trustedOverlay->setTrustedOverlay(true);
3785
3786 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}});
3787
3788 // Start a three-finger touchpad swipe
3789 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3790 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3791 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3792 .build());
3793 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3794 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3795 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3796 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3797 .build());
3798 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3799 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3800 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3801 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3802 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3803 .build());
3804
3805 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3806 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3807 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
3808
3809 // Move the swipe a bit
3810 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3811 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3812 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3813 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3814 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3815 .build());
3816
3817 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3818
3819 // End the swipe
3820 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3821 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3822 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3823 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3824 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3825 .build());
3826 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3827 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3828 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3829 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3830 .build());
3831 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3832 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3833 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3834 .build());
3835
3836 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
3837 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
3838 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
3839
3840 window->assertNoEvents();
3841}
3842
3843TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
3844 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3845 sp<FakeWindowHandle> window =
3846 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3847 window->setFrame(Rect(0, 0, 400, 400));
3848 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3849
3850 // Start a three-finger touchpad swipe
3851 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3852 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3853 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3854 .build());
3855 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3856 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3857 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3858 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3859 .build());
3860 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3861 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3862 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3863 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3864 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3865 .build());
3866
3867 // Move the swipe a bit
3868 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3869 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3870 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3871 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3872 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3873 .build());
3874
3875 // End the swipe
3876 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3877 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3878 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3879 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3880 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3881 .build());
3882 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3883 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3884 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3885 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3886 .build());
3887 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3888 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3889 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3890 .build());
3891
3892 window->assertNoEvents();
3893}
3894
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003895/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003896 * Ensure the correct coordinate spaces are used by InputDispatcher.
3897 *
3898 * InputDispatcher works in the display space, so its coordinate system is relative to the display
3899 * panel. Windows get events in the window space, and get raw coordinates in the logical display
3900 * space.
3901 */
3902class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
3903public:
3904 void SetUp() override {
3905 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003906 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003907 }
3908
3909 void addDisplayInfo(int displayId, const ui::Transform& transform) {
3910 gui::DisplayInfo info;
3911 info.displayId = displayId;
3912 info.transform = transform;
3913 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05003914 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003915 }
3916
3917 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
3918 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05003919 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003920 }
3921
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003922 void removeAllWindowsAndDisplays() {
3923 mDisplayInfos.clear();
3924 mWindowInfos.clear();
3925 }
3926
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003927 // Set up a test scenario where the display has a scaled projection and there are two windows
3928 // on the display.
3929 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
3930 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
3931 // respectively.
3932 ui::Transform displayTransform;
3933 displayTransform.set(2, 0, 0, 4);
3934 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3935
3936 std::shared_ptr<FakeApplicationHandle> application =
3937 std::make_shared<FakeApplicationHandle>();
3938
3939 // Add two windows to the display. Their frames are represented in the display space.
3940 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003941 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3942 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003943 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
3944 addWindow(firstWindow);
3945
3946 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003947 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3948 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003949 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
3950 addWindow(secondWindow);
3951 return {std::move(firstWindow), std::move(secondWindow)};
3952 }
3953
3954private:
3955 std::vector<gui::DisplayInfo> mDisplayInfos;
3956 std::vector<gui::WindowInfo> mWindowInfos;
3957};
3958
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003959TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003960 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3961 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003962 // selected so that if the hit test was performed with the point and the bounds being in
3963 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003964 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3965 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3966 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003967
3968 firstWindow->consumeMotionDown();
3969 secondWindow->assertNoEvents();
3970}
3971
3972// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
3973// the event should be treated as being in the logical display space.
3974TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
3975 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3976 // Send down to the first window. The point is represented in the logical display space. The
3977 // point is selected so that if the hit test was done in logical display space, then it would
3978 // end up in the incorrect window.
3979 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3980 PointF{75 * 2, 55 * 4});
3981
3982 firstWindow->consumeMotionDown();
3983 secondWindow->assertNoEvents();
3984}
3985
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003986// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
3987// event should be treated as being in the logical display space.
3988TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
3989 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3990
3991 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
3992 ui::Transform injectedEventTransform;
3993 injectedEventTransform.set(matrix);
3994 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
3995 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
3996
3997 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3998 .displayId(ADISPLAY_ID_DEFAULT)
3999 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004000 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004001 .x(untransformedPoint.x)
4002 .y(untransformedPoint.y))
4003 .build();
4004 event.transform(matrix);
4005
4006 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4007 InputEventInjectionSync::WAIT_FOR_RESULT);
4008
4009 firstWindow->consumeMotionDown();
4010 secondWindow->assertNoEvents();
4011}
4012
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004013TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4014 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4015
4016 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004017 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4018 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4019 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004020
4021 firstWindow->assertNoEvents();
4022 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004023 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004024 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4025
4026 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4027 EXPECT_EQ(300, event->getRawX(0));
4028 EXPECT_EQ(880, event->getRawY(0));
4029
4030 // Ensure that the x and y values are in the window's coordinate space.
4031 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4032 // the logical display space. This will be the origin of the window space.
4033 EXPECT_EQ(100, event->getX(0));
4034 EXPECT_EQ(80, event->getY(0));
4035}
4036
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004037/** Ensure consistent behavior of InputDispatcher in all orientations. */
4038class InputDispatcherDisplayOrientationFixture
4039 : public InputDispatcherDisplayProjectionTest,
4040 public ::testing::WithParamInterface<ui::Rotation> {};
4041
4042// This test verifies the touchable region of a window for all rotations of the display by tapping
4043// in different locations on the display, specifically points close to the four corners of a
4044// window.
4045TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4046 constexpr static int32_t displayWidth = 400;
4047 constexpr static int32_t displayHeight = 800;
4048
4049 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4050
4051 const auto rotation = GetParam();
4052
4053 // Set up the display with the specified rotation.
4054 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4055 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4056 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4057 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4058 logicalDisplayWidth, logicalDisplayHeight);
4059 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4060
4061 // Create a window with its bounds determined in the logical display.
4062 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4063 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4064 sp<FakeWindowHandle> window =
4065 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4066 window->setFrame(frameInDisplay, displayTransform);
4067 addWindow(window);
4068
4069 // The following points in logical display space should be inside the window.
4070 static const std::array<vec2, 4> insidePoints{
4071 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4072 for (const auto pointInsideWindow : insidePoints) {
4073 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4074 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004075 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4076 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4077 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004078 window->consumeMotionDown();
4079
Prabir Pradhan678438e2023-04-13 19:32:51 +00004080 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4081 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4082 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004083 window->consumeMotionUp();
4084 }
4085
4086 // The following points in logical display space should be outside the window.
4087 static const std::array<vec2, 5> outsidePoints{
4088 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4089 for (const auto pointOutsideWindow : outsidePoints) {
4090 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4091 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004092 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4093 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4094 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004095
Prabir Pradhan678438e2023-04-13 19:32:51 +00004096 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4097 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4098 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004099 }
4100 window->assertNoEvents();
4101}
4102
4103// Run the precision tests for all rotations.
4104INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4105 InputDispatcherDisplayOrientationFixture,
4106 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4107 ui::ROTATION_270),
4108 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4109 return ftl::enum_string(testParamInfo.param);
4110 });
4111
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004112using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4113 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004114
4115class TransferTouchFixture : public InputDispatcherTest,
4116 public ::testing::WithParamInterface<TransferFunction> {};
4117
4118TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004119 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004120
4121 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004122 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004123 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4124 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004125 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004126 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004127 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4128 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004129 sp<FakeWindowHandle> wallpaper =
4130 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4131 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004132 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004133 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004134
4135 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004136 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4137 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004138
Svet Ganov5d3bc372020-01-26 23:11:07 -08004139 // Only the first window should get the down event
4140 firstWindow->consumeMotionDown();
4141 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004142 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004143
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004144 // Transfer touch to the second window
4145 TransferFunction f = GetParam();
4146 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4147 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004148 // The first window gets cancel and the second gets down
4149 firstWindow->consumeMotionCancel();
4150 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004151 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004152
4153 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004154 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4155 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004156 // The first window gets no events and the second gets up
4157 firstWindow->assertNoEvents();
4158 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004159 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004160}
4161
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004162/**
4163 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4164 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4165 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4166 * natural to the user.
4167 * In this test, we are sending a pointer to both spy window and first window. We then try to
4168 * transfer touch to the second window. The dispatcher should identify the first window as the
4169 * one that should lose the gesture, and therefore the action should be to move the gesture from
4170 * the first window to the second.
4171 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4172 * the other API, as well.
4173 */
4174TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4175 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4176
4177 // Create a couple of windows + a spy window
4178 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004179 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004180 spyWindow->setTrustedOverlay(true);
4181 spyWindow->setSpy(true);
4182 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004183 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004184 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004185 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004186
4187 // Add the windows to the dispatcher
4188 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4189
4190 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004191 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4192 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004193 // Only the first window and spy should get the down event
4194 spyWindow->consumeMotionDown();
4195 firstWindow->consumeMotionDown();
4196
4197 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4198 // if f === 'transferTouch'.
4199 TransferFunction f = GetParam();
4200 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4201 ASSERT_TRUE(success);
4202 // The first window gets cancel and the second gets down
4203 firstWindow->consumeMotionCancel();
4204 secondWindow->consumeMotionDown();
4205
4206 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004207 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4208 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004209 // The first window gets no events and the second+spy get up
4210 firstWindow->assertNoEvents();
4211 spyWindow->consumeMotionUp();
4212 secondWindow->consumeMotionUp();
4213}
4214
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004215TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004216 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004217
4218 PointF touchPoint = {10, 10};
4219
4220 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004221 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004222 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4223 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004224 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004225 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004226 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4227 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004228 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004229
4230 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004231 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004232
4233 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004234 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4235 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4236 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004237 // Only the first window should get the down event
4238 firstWindow->consumeMotionDown();
4239 secondWindow->assertNoEvents();
4240
4241 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004242 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4243 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004244 // Only the first window should get the pointer down event
4245 firstWindow->consumeMotionPointerDown(1);
4246 secondWindow->assertNoEvents();
4247
4248 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004249 TransferFunction f = GetParam();
4250 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4251 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004252 // The first window gets cancel and the second gets down and pointer down
4253 firstWindow->consumeMotionCancel();
4254 secondWindow->consumeMotionDown();
4255 secondWindow->consumeMotionPointerDown(1);
4256
4257 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004258 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4259 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004260 // The first window gets nothing and the second gets pointer up
4261 firstWindow->assertNoEvents();
4262 secondWindow->consumeMotionPointerUp(1);
4263
4264 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004265 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4266 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004267 // The first window gets nothing and the second gets up
4268 firstWindow->assertNoEvents();
4269 secondWindow->consumeMotionUp();
4270}
4271
Arthur Hungc539dbb2022-12-08 07:45:36 +00004272TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4273 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4274
4275 // Create a couple of windows
4276 sp<FakeWindowHandle> firstWindow =
4277 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4278 ADISPLAY_ID_DEFAULT);
4279 firstWindow->setDupTouchToWallpaper(true);
4280 sp<FakeWindowHandle> secondWindow =
4281 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4282 ADISPLAY_ID_DEFAULT);
4283 secondWindow->setDupTouchToWallpaper(true);
4284
4285 sp<FakeWindowHandle> wallpaper1 =
4286 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4287 wallpaper1->setIsWallpaper(true);
4288
4289 sp<FakeWindowHandle> wallpaper2 =
4290 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4291 wallpaper2->setIsWallpaper(true);
4292 // Add the windows to the dispatcher
4293 mDispatcher->setInputWindows(
4294 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4295
4296 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004297 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4298 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004299
4300 // Only the first window should get the down event
4301 firstWindow->consumeMotionDown();
4302 secondWindow->assertNoEvents();
4303 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4304 wallpaper2->assertNoEvents();
4305
4306 // Transfer touch focus to the second window
4307 TransferFunction f = GetParam();
4308 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4309 ASSERT_TRUE(success);
4310
4311 // The first window gets cancel and the second gets down
4312 firstWindow->consumeMotionCancel();
4313 secondWindow->consumeMotionDown();
4314 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4315 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4316
4317 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004318 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4319 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004320 // The first window gets no events and the second gets up
4321 firstWindow->assertNoEvents();
4322 secondWindow->consumeMotionUp();
4323 wallpaper1->assertNoEvents();
4324 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4325}
4326
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004327// For the cases of single pointer touch and two pointers non-split touch, the api's
4328// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4329// for the case where there are multiple pointers split across several windows.
4330INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4331 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004332 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4333 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004334 return dispatcher->transferTouch(destChannelToken,
4335 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004336 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004337 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4338 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004339 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004340 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004341 }));
4342
Svet Ganov5d3bc372020-01-26 23:11:07 -08004343TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004344 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004345
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004346 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004347 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4348 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004349 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004350
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004351 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004352 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4353 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004354 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004355
4356 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004357 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004358
4359 PointF pointInFirst = {300, 200};
4360 PointF pointInSecond = {300, 600};
4361
4362 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004363 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4364 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4365 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004366 // Only the first window should get the down event
4367 firstWindow->consumeMotionDown();
4368 secondWindow->assertNoEvents();
4369
4370 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004371 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4372 ADISPLAY_ID_DEFAULT,
4373 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004374 // The first window gets a move and the second a down
4375 firstWindow->consumeMotionMove();
4376 secondWindow->consumeMotionDown();
4377
4378 // Transfer touch focus to the second window
4379 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4380 // The first window gets cancel and the new gets pointer down (it already saw down)
4381 firstWindow->consumeMotionCancel();
4382 secondWindow->consumeMotionPointerDown(1);
4383
4384 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004385 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4386 ADISPLAY_ID_DEFAULT,
4387 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004388 // The first window gets nothing and the second gets pointer up
4389 firstWindow->assertNoEvents();
4390 secondWindow->consumeMotionPointerUp(1);
4391
4392 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004393 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4394 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004395 // The first window gets nothing and the second gets up
4396 firstWindow->assertNoEvents();
4397 secondWindow->consumeMotionUp();
4398}
4399
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004400// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4401// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4402// touch is not supported, so the touch should continue on those windows and the transferred-to
4403// window should get nothing.
4404TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4405 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4406
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004407 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004408 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4409 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004410 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004411
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004412 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004413 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4414 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004415 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004416
4417 // Add the windows to the dispatcher
4418 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4419
4420 PointF pointInFirst = {300, 200};
4421 PointF pointInSecond = {300, 600};
4422
4423 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004424 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4425 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4426 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004427 // Only the first window should get the down event
4428 firstWindow->consumeMotionDown();
4429 secondWindow->assertNoEvents();
4430
4431 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004432 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4433 ADISPLAY_ID_DEFAULT,
4434 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004435 // The first window gets a move and the second a down
4436 firstWindow->consumeMotionMove();
4437 secondWindow->consumeMotionDown();
4438
4439 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004440 const bool transferred =
4441 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004442 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4443 ASSERT_FALSE(transferred);
4444 firstWindow->assertNoEvents();
4445 secondWindow->assertNoEvents();
4446
4447 // The rest of the dispatch should proceed as normal
4448 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004449 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4450 ADISPLAY_ID_DEFAULT,
4451 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004452 // The first window gets MOVE and the second gets pointer up
4453 firstWindow->consumeMotionMove();
4454 secondWindow->consumeMotionUp();
4455
4456 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004457 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4458 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004459 // The first window gets nothing and the second gets up
4460 firstWindow->consumeMotionUp();
4461 secondWindow->assertNoEvents();
4462}
4463
Arthur Hungabbb9d82021-09-01 14:52:30 +00004464// This case will create two windows and one mirrored window on the default display and mirror
4465// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4466// the windows info of second display before default display.
4467TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4468 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4469 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004470 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004471 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004472 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004473 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004474 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004475
4476 sp<FakeWindowHandle> mirrorWindowInPrimary =
4477 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4478 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004479
4480 sp<FakeWindowHandle> firstWindowInSecondary =
4481 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4482 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004483
4484 sp<FakeWindowHandle> secondWindowInSecondary =
4485 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4486 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004487
4488 // Update window info, let it find window handle of second display first.
4489 mDispatcher->setInputWindows(
4490 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4491 {ADISPLAY_ID_DEFAULT,
4492 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4493
4494 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4495 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4496 {50, 50}))
4497 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4498
4499 // Window should receive motion event.
4500 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4501
4502 // Transfer touch focus
4503 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4504 secondWindowInPrimary->getToken()));
4505 // The first window gets cancel.
4506 firstWindowInPrimary->consumeMotionCancel();
4507 secondWindowInPrimary->consumeMotionDown();
4508
4509 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4510 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4511 ADISPLAY_ID_DEFAULT, {150, 50}))
4512 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4513 firstWindowInPrimary->assertNoEvents();
4514 secondWindowInPrimary->consumeMotionMove();
4515
4516 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4517 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4518 {150, 50}))
4519 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4520 firstWindowInPrimary->assertNoEvents();
4521 secondWindowInPrimary->consumeMotionUp();
4522}
4523
4524// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4525// 'transferTouch' api.
4526TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4527 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4528 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004529 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004530 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004531 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004532 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004533 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004534
4535 sp<FakeWindowHandle> mirrorWindowInPrimary =
4536 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4537 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004538
4539 sp<FakeWindowHandle> firstWindowInSecondary =
4540 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4541 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004542
4543 sp<FakeWindowHandle> secondWindowInSecondary =
4544 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4545 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004546
4547 // Update window info, let it find window handle of second display first.
4548 mDispatcher->setInputWindows(
4549 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4550 {ADISPLAY_ID_DEFAULT,
4551 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4552
4553 // Touch on second display.
4554 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4555 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4556 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4557
4558 // Window should receive motion event.
4559 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4560
4561 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004562 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004563
4564 // The first window gets cancel.
4565 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4566 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4567
4568 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4569 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4570 SECOND_DISPLAY_ID, {150, 50}))
4571 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4572 firstWindowInPrimary->assertNoEvents();
4573 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4574
4575 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4576 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4577 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4578 firstWindowInPrimary->assertNoEvents();
4579 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4580}
4581
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004582TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004583 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004584 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4585 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004586
Vishnu Nair47074b82020-08-14 11:54:47 -07004587 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004588 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004589 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004590
4591 window->consumeFocusEvent(true);
4592
Prabir Pradhan678438e2023-04-13 19:32:51 +00004593 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004594
4595 // Window should receive key down event.
4596 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004597
4598 // Should have poked user activity
4599 mFakePolicy->assertUserActivityPoked();
4600}
4601
4602TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4603 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4604 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4605 "Fake Window", ADISPLAY_ID_DEFAULT);
4606
4607 window->setDisableUserActivity(true);
4608 window->setFocusable(true);
4609 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4610 setFocusedWindow(window);
4611
4612 window->consumeFocusEvent(true);
4613
4614 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4615
4616 // Window should receive key down event.
4617 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4618
4619 // Should have poked user activity
4620 mFakePolicy->assertUserActivityNotPoked();
4621}
4622
4623TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4624 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4625 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4626 "Fake Window", ADISPLAY_ID_DEFAULT);
4627
4628 window->setFocusable(true);
4629 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4630 setFocusedWindow(window);
4631
4632 window->consumeFocusEvent(true);
4633
4634 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4635 mDispatcher->waitForIdle();
4636
4637 // System key is not passed down
4638 window->assertNoEvents();
4639
4640 // Should have poked user activity
4641 mFakePolicy->assertUserActivityPoked();
4642}
4643
4644TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4645 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4646 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4647 "Fake Window", ADISPLAY_ID_DEFAULT);
4648
4649 window->setFocusable(true);
4650 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4651 setFocusedWindow(window);
4652
4653 window->consumeFocusEvent(true);
4654
4655 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4656 mDispatcher->waitForIdle();
4657
4658 // System key is not passed down
4659 window->assertNoEvents();
4660
4661 // Should have poked user activity
4662 mFakePolicy->assertUserActivityPoked();
4663}
4664
4665TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4666 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4667 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4668 "Fake Window", ADISPLAY_ID_DEFAULT);
4669
4670 window->setDisableUserActivity(true);
4671 window->setFocusable(true);
4672 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4673 setFocusedWindow(window);
4674
4675 window->consumeFocusEvent(true);
4676
4677 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4678 mDispatcher->waitForIdle();
4679
4680 // System key is not passed down
4681 window->assertNoEvents();
4682
4683 // Should have poked user activity
4684 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004685}
4686
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004687TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
4688 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4689 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4690 "Fake Window", ADISPLAY_ID_DEFAULT);
4691
4692 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4693
4694 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4695 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4696 ADISPLAY_ID_DEFAULT, {100, 100}))
4697 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4698
4699 window->consumeMotionEvent(
4700 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
4701
4702 // Should have poked user activity
4703 mFakePolicy->assertUserActivityPoked();
4704}
4705
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004706TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004707 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004708 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4709 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004710
Arthur Hung72d8dc32020-03-28 00:48:39 +00004711 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004712
Prabir Pradhan678438e2023-04-13 19:32:51 +00004713 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004714 mDispatcher->waitForIdle();
4715
4716 window->assertNoEvents();
4717}
4718
4719// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4720TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004721 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004722 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4723 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004724
Arthur Hung72d8dc32020-03-28 00:48:39 +00004725 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004726
4727 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004728 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004729 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004730 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4731 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004732
4733 // Window should receive only the motion event
4734 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4735 window->assertNoEvents(); // Key event or focus event will not be received
4736}
4737
arthurhungea3f4fc2020-12-21 23:18:53 +08004738TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4739 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4740
arthurhungea3f4fc2020-12-21 23:18:53 +08004741 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004742 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4743 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004744 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004745
arthurhungea3f4fc2020-12-21 23:18:53 +08004746 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004747 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4748 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004749 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004750
4751 // Add the windows to the dispatcher
4752 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4753
4754 PointF pointInFirst = {300, 200};
4755 PointF pointInSecond = {300, 600};
4756
4757 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004758 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4759 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4760 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004761 // Only the first window should get the down event
4762 firstWindow->consumeMotionDown();
4763 secondWindow->assertNoEvents();
4764
4765 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004766 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4767 ADISPLAY_ID_DEFAULT,
4768 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004769 // The first window gets a move and the second a down
4770 firstWindow->consumeMotionMove();
4771 secondWindow->consumeMotionDown();
4772
4773 // Send pointer cancel to the second window
4774 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004775 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004776 {pointInFirst, pointInSecond});
4777 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004778 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004779 // The first window gets move and the second gets cancel.
4780 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4781 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4782
4783 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004784 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4785 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08004786 // The first window gets up and the second gets nothing.
4787 firstWindow->consumeMotionUp();
4788 secondWindow->assertNoEvents();
4789}
4790
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004791TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
4792 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4793
4794 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004795 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004796 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4797 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
4798 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
4799 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
4800
Harry Cutts33476232023-01-30 19:57:29 +00004801 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004802 window->assertNoEvents();
4803 mDispatcher->waitForIdle();
4804}
4805
chaviwd1c23182019-12-20 18:44:56 -08004806class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00004807public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004808 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004809 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07004810 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004811 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07004812 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00004813 }
4814
chaviwd1c23182019-12-20 18:44:56 -08004815 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
4816
4817 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004818 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
4819 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08004820 }
4821
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004822 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
4823
4824 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
4825
chaviwd1c23182019-12-20 18:44:56 -08004826 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004827 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08004828 expectedDisplayId, expectedFlags);
4829 }
4830
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004831 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004832 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004833 expectedDisplayId, expectedFlags);
4834 }
4835
chaviwd1c23182019-12-20 18:44:56 -08004836 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004837 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08004838 expectedDisplayId, expectedFlags);
4839 }
4840
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004841 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004842 mInputReceiver->consumeMotionEvent(
4843 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4844 WithDisplayId(expectedDisplayId),
4845 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004846 }
4847
Arthur Hungfbfa5722021-11-16 02:45:54 +00004848 void consumeMotionPointerDown(int32_t pointerIdx) {
4849 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4850 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004851 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004852 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004853 }
4854
Evan Rosky84f07f02021-04-16 10:42:42 -07004855 MotionEvent* consumeMotion() {
4856 InputEvent* event = mInputReceiver->consume();
4857 if (!event) {
4858 ADD_FAILURE() << "No event was produced";
4859 return nullptr;
4860 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004861 if (event->getType() != InputEventType::MOTION) {
4862 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07004863 return nullptr;
4864 }
4865 return static_cast<MotionEvent*>(event);
4866 }
4867
chaviwd1c23182019-12-20 18:44:56 -08004868 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4869
4870private:
4871 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004872};
4873
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004874using InputDispatcherMonitorTest = InputDispatcherTest;
4875
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004876/**
4877 * Two entities that receive touch: A window, and a global monitor.
4878 * The touch goes to the window, and then the window disappears.
4879 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4880 * for the monitor, as well.
4881 * 1. foregroundWindow
4882 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4883 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004884TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004885 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4886 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004887 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004888
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004889 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004890
4891 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4892 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4893 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4894 {100, 200}))
4895 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4896
4897 // Both the foreground window and the global monitor should receive the touch down
4898 window->consumeMotionDown();
4899 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4900
4901 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4902 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4903 ADISPLAY_ID_DEFAULT, {110, 200}))
4904 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4905
4906 window->consumeMotionMove();
4907 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4908
4909 // Now the foreground window goes away
4910 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4911 window->consumeMotionCancel();
4912 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
4913
4914 // If more events come in, there will be no more foreground window to send them to. This will
4915 // cause a cancel for the monitor, as well.
4916 ASSERT_EQ(InputEventInjectionResult::FAILED,
4917 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4918 ADISPLAY_ID_DEFAULT, {120, 200}))
4919 << "Injection should fail because the window was removed";
4920 window->assertNoEvents();
4921 // Global monitor now gets the cancel
4922 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4923}
4924
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004925TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07004926 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004927 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4928 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004929 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004930
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004931 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004932
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004933 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004934 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004935 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00004936 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004937 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004938}
4939
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004940TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
4941 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004942
Chris Yea209fde2020-07-22 13:54:51 -07004943 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004944 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4945 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004946 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004947
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004948 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004949 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004950 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08004951 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004952 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004953
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004954 // Pilfer pointers from the monitor.
4955 // This should not do anything and the window should continue to receive events.
4956 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00004957
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004958 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004959 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4960 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004961 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004962
4963 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4964 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004965}
4966
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004967TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07004968 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004969 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4970 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004971 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4972 window->setWindowOffset(20, 40);
4973 window->setWindowTransform(0, 1, -1, 0);
4974
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004975 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004976
4977 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4978 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4979 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4980 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4981 MotionEvent* event = monitor.consumeMotion();
4982 // Even though window has transform, gesture monitor must not.
4983 ASSERT_EQ(ui::Transform(), event->getTransform());
4984}
4985
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004986TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00004987 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004988 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00004989
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004990 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00004991 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004992 << "Injection should fail if there is a monitor, but no touchable window";
4993 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00004994}
4995
chaviw81e2bb92019-12-18 15:03:51 -08004996TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004997 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004998 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4999 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08005000
Arthur Hung72d8dc32020-03-28 00:48:39 +00005001 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08005002
5003 NotifyMotionArgs motionArgs =
5004 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5005 ADISPLAY_ID_DEFAULT);
5006
Prabir Pradhan678438e2023-04-13 19:32:51 +00005007 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005008 // Window should receive motion down event.
5009 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5010
5011 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005012 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005013 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5014 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5015 motionArgs.pointerCoords[0].getX() - 10);
5016
Prabir Pradhan678438e2023-04-13 19:32:51 +00005017 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005018 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005019 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005020}
5021
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005022/**
5023 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5024 * the device default right away. In the test scenario, we check both the default value,
5025 * and the action of enabling / disabling.
5026 */
5027TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005028 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005029 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5030 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005031 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005032
5033 // Set focused application.
5034 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005035 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005036
5037 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005038 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005039 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005040 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005041
5042 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005043 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005044 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005045 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005046
5047 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005048 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005049 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005050 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005051 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005052 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005053 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005054 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005055
5056 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005057 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005058 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005059 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005060
5061 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005062 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005063 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005064 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005065 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005066 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005067 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005068 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005069
5070 window->assertNoEvents();
5071}
5072
Gang Wange9087892020-01-07 12:17:14 -05005073TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005074 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005075 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5076 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005077
5078 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005079 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005080
Arthur Hung72d8dc32020-03-28 00:48:39 +00005081 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005082 setFocusedWindow(window);
5083
Harry Cutts33476232023-01-30 19:57:29 +00005084 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005085
Prabir Pradhan678438e2023-04-13 19:32:51 +00005086 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5087 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005088
5089 InputEvent* event = window->consume();
5090 ASSERT_NE(event, nullptr);
5091
5092 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5093 ASSERT_NE(verified, nullptr);
5094 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5095
5096 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5097 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5098 ASSERT_EQ(keyArgs.source, verified->source);
5099 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5100
5101 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5102
5103 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005104 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005105 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005106 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5107 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5108 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5109 ASSERT_EQ(0, verifiedKey.repeatCount);
5110}
5111
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005112TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005113 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005114 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5115 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005116
5117 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5118
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005119 ui::Transform transform;
5120 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5121
5122 gui::DisplayInfo displayInfo;
5123 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5124 displayInfo.transform = transform;
5125
Patrick Williamsd828f302023-04-28 17:52:08 -05005126 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005127
Prabir Pradhan678438e2023-04-13 19:32:51 +00005128 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005129 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5130 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005131 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005132
5133 InputEvent* event = window->consume();
5134 ASSERT_NE(event, nullptr);
5135
5136 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5137 ASSERT_NE(verified, nullptr);
5138 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5139
5140 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5141 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5142 EXPECT_EQ(motionArgs.source, verified->source);
5143 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5144
5145 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5146
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005147 const vec2 rawXY =
5148 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5149 motionArgs.pointerCoords[0].getXYValue());
5150 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5151 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005152 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005153 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005154 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005155 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5156 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5157}
5158
chaviw09c8d2d2020-08-24 15:48:26 -07005159/**
5160 * Ensure that separate calls to sign the same data are generating the same key.
5161 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5162 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5163 * tests.
5164 */
5165TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5166 KeyEvent event = getTestKeyEvent();
5167 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5168
5169 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5170 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5171 ASSERT_EQ(hmac1, hmac2);
5172}
5173
5174/**
5175 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5176 */
5177TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5178 KeyEvent event = getTestKeyEvent();
5179 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5180 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5181
5182 verifiedEvent.deviceId += 1;
5183 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5184
5185 verifiedEvent.source += 1;
5186 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5187
5188 verifiedEvent.eventTimeNanos += 1;
5189 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5190
5191 verifiedEvent.displayId += 1;
5192 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5193
5194 verifiedEvent.action += 1;
5195 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5196
5197 verifiedEvent.downTimeNanos += 1;
5198 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5199
5200 verifiedEvent.flags += 1;
5201 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5202
5203 verifiedEvent.keyCode += 1;
5204 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5205
5206 verifiedEvent.scanCode += 1;
5207 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5208
5209 verifiedEvent.metaState += 1;
5210 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5211
5212 verifiedEvent.repeatCount += 1;
5213 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5214}
5215
Vishnu Nair958da932020-08-21 17:12:37 -07005216TEST_F(InputDispatcherTest, SetFocusedWindow) {
5217 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5218 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005219 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005220 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005221 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005222 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5223
5224 // Top window is also focusable but is not granted focus.
5225 windowTop->setFocusable(true);
5226 windowSecond->setFocusable(true);
5227 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5228 setFocusedWindow(windowSecond);
5229
5230 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005231 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5232 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005233
5234 // Focused window should receive event.
5235 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5236 windowTop->assertNoEvents();
5237}
5238
5239TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5240 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5241 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005242 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005243 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5244
5245 window->setFocusable(true);
5246 // Release channel for window is no longer valid.
5247 window->releaseChannel();
5248 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5249 setFocusedWindow(window);
5250
5251 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005252 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5253 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005254
5255 // window channel is invalid, so it should not receive any input event.
5256 window->assertNoEvents();
5257}
5258
5259TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5260 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5261 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005262 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005263 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005264 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5265
Vishnu Nair958da932020-08-21 17:12:37 -07005266 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5267 setFocusedWindow(window);
5268
5269 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005270 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5271 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005272
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005273 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005274 window->assertNoEvents();
5275}
5276
5277TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5278 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5279 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005280 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005281 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005282 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005283 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5284
5285 windowTop->setFocusable(true);
5286 windowSecond->setFocusable(true);
5287 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5288 setFocusedWindow(windowTop);
5289 windowTop->consumeFocusEvent(true);
5290
Chavi Weingarten847e8512023-03-29 00:26:09 +00005291 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5292 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005293 windowSecond->consumeFocusEvent(true);
5294 windowTop->consumeFocusEvent(false);
5295
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005296 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5297 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005298
5299 // Focused window should receive event.
5300 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5301}
5302
Chavi Weingarten847e8512023-03-29 00:26:09 +00005303TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005304 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5305 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005306 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005307 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005308 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005309 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5310
5311 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005312 windowSecond->setFocusable(false);
5313 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005314 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005315 setFocusedWindow(windowTop);
5316 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005317
Chavi Weingarten847e8512023-03-29 00:26:09 +00005318 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5319 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005320
5321 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005322 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005323 windowSecond->assertNoEvents();
5324}
5325
5326TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5327 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5328 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005329 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005330 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005331 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5332 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005333 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5334
5335 window->setFocusable(true);
5336 previousFocusedWindow->setFocusable(true);
5337 window->setVisible(false);
5338 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5339 setFocusedWindow(previousFocusedWindow);
5340 previousFocusedWindow->consumeFocusEvent(true);
5341
5342 // Requesting focus on invisible window takes focus from currently focused window.
5343 setFocusedWindow(window);
5344 previousFocusedWindow->consumeFocusEvent(false);
5345
5346 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005347 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005348 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5349 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005350
5351 // Window does not get focus event or key down.
5352 window->assertNoEvents();
5353
5354 // Window becomes visible.
5355 window->setVisible(true);
5356 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5357
5358 // Window receives focus event.
5359 window->consumeFocusEvent(true);
5360 // Focused window receives key down.
5361 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5362}
5363
Vishnu Nair599f1412021-06-21 10:39:58 -07005364TEST_F(InputDispatcherTest, DisplayRemoved) {
5365 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5366 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005367 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005368 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5369
5370 // window is granted focus.
5371 window->setFocusable(true);
5372 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5373 setFocusedWindow(window);
5374 window->consumeFocusEvent(true);
5375
5376 // When a display is removed window loses focus.
5377 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5378 window->consumeFocusEvent(false);
5379}
5380
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005381/**
5382 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5383 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5384 * of the 'slipperyEnterWindow'.
5385 *
5386 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5387 * a way so that the touched location is no longer covered by the top window.
5388 *
5389 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5390 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5391 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5392 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5393 * with ACTION_DOWN).
5394 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5395 * window moved itself away from the touched location and had Flag::SLIPPERY.
5396 *
5397 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5398 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5399 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5400 *
5401 * In this test, we ensure that the event received by the bottom window has
5402 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5403 */
5404TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005405 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5406 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005407
5408 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5409 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5410
5411 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005412 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005413 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005414 // Make sure this one overlaps the bottom window
5415 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5416 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5417 // one. Windows with the same owner are not considered to be occluding each other.
5418 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5419
5420 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005421 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005422 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5423
5424 mDispatcher->setInputWindows(
5425 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5426
5427 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005428 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5429 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5430 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005431 slipperyExitWindow->consumeMotionDown();
5432 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5433 mDispatcher->setInputWindows(
5434 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5435
Prabir Pradhan678438e2023-04-13 19:32:51 +00005436 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5437 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5438 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005439
5440 slipperyExitWindow->consumeMotionCancel();
5441
5442 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5443 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5444}
5445
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005446/**
5447 * Two windows, one on the left and another on the right. The left window is slippery. The right
5448 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5449 * touch moves from the left window into the right window, the gesture should continue to go to the
5450 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5451 * reproduces a crash.
5452 */
5453TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5454 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5455
5456 sp<FakeWindowHandle> leftSlipperyWindow =
5457 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5458 leftSlipperyWindow->setSlippery(true);
5459 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5460
5461 sp<FakeWindowHandle> rightDropTouchesWindow =
5462 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5463 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5464 rightDropTouchesWindow->setDropInput(true);
5465
5466 mDispatcher->setInputWindows(
5467 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5468
5469 // Start touch in the left window
5470 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5471 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5472 .build());
5473 leftSlipperyWindow->consumeMotionDown();
5474
5475 // And move it into the right window
5476 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5477 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5478 .build());
5479
5480 // Since the right window isn't eligible to receive input, touch does not slip.
5481 // The left window continues to receive the gesture.
5482 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5483 rightDropTouchesWindow->assertNoEvents();
5484}
5485
Garfield Tan1c7bc862020-01-28 13:24:04 -08005486class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5487protected:
5488 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5489 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5490
Chris Yea209fde2020-07-22 13:54:51 -07005491 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005492 sp<FakeWindowHandle> mWindow;
5493
5494 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005495 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005496 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005497 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005498 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005499 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5500 ASSERT_EQ(OK, mDispatcher->start());
5501
5502 setUpWindow();
5503 }
5504
5505 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005506 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005507 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005508
Vishnu Nair47074b82020-08-14 11:54:47 -07005509 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005510 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005511 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005512 mWindow->consumeFocusEvent(true);
5513 }
5514
Chris Ye2ad95392020-09-01 13:44:44 -07005515 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005516 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005517 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005518 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005519 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005520
5521 // Window should receive key down event.
5522 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5523 }
5524
5525 void expectKeyRepeatOnce(int32_t repeatCount) {
5526 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5527 InputEvent* repeatEvent = mWindow->consume();
5528 ASSERT_NE(nullptr, repeatEvent);
5529
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005530 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005531
5532 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5533 uint32_t eventAction = repeatKeyEvent->getAction();
5534 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5535 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5536 }
5537
Chris Ye2ad95392020-09-01 13:44:44 -07005538 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005539 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005540 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005541 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005542 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005543
5544 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005545 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005546 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005547 }
5548};
5549
5550TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005551 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005552 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5553 expectKeyRepeatOnce(repeatCount);
5554 }
5555}
5556
5557TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005558 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005559 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5560 expectKeyRepeatOnce(repeatCount);
5561 }
Harry Cutts33476232023-01-30 19:57:29 +00005562 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005563 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005564 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5565 expectKeyRepeatOnce(repeatCount);
5566 }
5567}
5568
5569TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005570 sendAndConsumeKeyDown(/*deviceId=*/1);
5571 expectKeyRepeatOnce(/*repeatCount=*/1);
5572 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005573 mWindow->assertNoEvents();
5574}
5575
5576TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005577 sendAndConsumeKeyDown(/*deviceId=*/1);
5578 expectKeyRepeatOnce(/*repeatCount=*/1);
5579 sendAndConsumeKeyDown(/*deviceId=*/2);
5580 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005581 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005582 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005583 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005584 expectKeyRepeatOnce(/*repeatCount=*/2);
5585 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005586 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005587 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005588 mWindow->assertNoEvents();
5589}
5590
5591TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005592 sendAndConsumeKeyDown(/*deviceId=*/1);
5593 expectKeyRepeatOnce(/*repeatCount=*/1);
5594 sendAndConsumeKeyDown(/*deviceId=*/2);
5595 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005596 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005597 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005598 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005599 mWindow->assertNoEvents();
5600}
5601
liushenxiang42232912021-05-21 20:24:09 +08005602TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5603 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005604 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005605 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005606 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5607 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5608 mWindow->assertNoEvents();
5609}
5610
Garfield Tan1c7bc862020-01-28 13:24:04 -08005611TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005612 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005613 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005614 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5615 InputEvent* repeatEvent = mWindow->consume();
5616 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5617 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5618 IdGenerator::getSource(repeatEvent->getId()));
5619 }
5620}
5621
5622TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005623 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005624 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005625
5626 std::unordered_set<int32_t> idSet;
5627 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5628 InputEvent* repeatEvent = mWindow->consume();
5629 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5630 int32_t id = repeatEvent->getId();
5631 EXPECT_EQ(idSet.end(), idSet.find(id));
5632 idSet.insert(id);
5633 }
5634}
5635
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005636/* Test InputDispatcher for MultiDisplay */
5637class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5638public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005639 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005640 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005641
Chris Yea209fde2020-07-22 13:54:51 -07005642 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005643 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005644 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005645
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005646 // Set focus window for primary display, but focused display would be second one.
5647 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005648 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005649 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005650 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005651 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005652
Chris Yea209fde2020-07-22 13:54:51 -07005653 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005654 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005655 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005656 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005657 // Set focus display to second one.
5658 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5659 // Set focus window for second display.
5660 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005661 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005662 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005663 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005664 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005665 }
5666
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005667 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005668 InputDispatcherTest::TearDown();
5669
Chris Yea209fde2020-07-22 13:54:51 -07005670 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005671 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005672 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005673 windowInSecondary.clear();
5674 }
5675
5676protected:
Chris Yea209fde2020-07-22 13:54:51 -07005677 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005678 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005679 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005680 sp<FakeWindowHandle> windowInSecondary;
5681};
5682
5683TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5684 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005685 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5686 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5687 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005688 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005689 windowInSecondary->assertNoEvents();
5690
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005691 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005692 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5693 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5694 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005695 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005696 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005697}
5698
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005699TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005700 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005701 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5702 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005703 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005704 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005705 windowInSecondary->assertNoEvents();
5706
5707 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005708 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005709 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005710 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005711 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005712
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005713 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005714 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005715
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005716 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005717 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005718 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005719
5720 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005721 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005722 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005723 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005724 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005725 windowInSecondary->assertNoEvents();
5726}
5727
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005728// Test per-display input monitors for motion event.
5729TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005730 FakeMonitorReceiver monitorInPrimary =
5731 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5732 FakeMonitorReceiver monitorInSecondary =
5733 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005734
5735 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005736 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5737 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5738 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005739 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005740 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005741 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005742 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005743
5744 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5746 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5747 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005748 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005749 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005750 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005751 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005752
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005753 // Lift up the touch from the second display
5754 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5755 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5756 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5757 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5758 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5759
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005760 // Test inject a non-pointer motion event.
5761 // If specific a display, it will dispatch to the focused window of particular display,
5762 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005763 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5764 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5765 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005766 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005767 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005768 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005769 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005770}
5771
5772// Test per-display input monitors for key event.
5773TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005774 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005775 FakeMonitorReceiver monitorInPrimary =
5776 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5777 FakeMonitorReceiver monitorInSecondary =
5778 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005779
5780 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005781 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5782 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005783 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005784 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005785 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005786 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005787}
5788
Vishnu Nair958da932020-08-21 17:12:37 -07005789TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
5790 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005791 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005792 secondWindowInPrimary->setFocusable(true);
5793 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
5794 setFocusedWindow(secondWindowInPrimary);
5795 windowInPrimary->consumeFocusEvent(false);
5796 secondWindowInPrimary->consumeFocusEvent(true);
5797
5798 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005799 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
5800 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005801 windowInPrimary->assertNoEvents();
5802 windowInSecondary->assertNoEvents();
5803 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5804}
5805
Arthur Hungdfd528e2021-12-08 13:23:04 +00005806TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
5807 FakeMonitorReceiver monitorInPrimary =
5808 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5809 FakeMonitorReceiver monitorInSecondary =
5810 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
5811
5812 // Test touch down on primary display.
5813 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5814 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5815 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5816 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5817 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5818
5819 // Test touch down on second display.
5820 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5821 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5822 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5823 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
5824 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
5825
5826 // Trigger cancel touch.
5827 mDispatcher->cancelCurrentTouch();
5828 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5829 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5830 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
5831 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
5832
5833 // Test inject a move motion event, no window/monitor should receive the event.
5834 ASSERT_EQ(InputEventInjectionResult::FAILED,
5835 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5836 ADISPLAY_ID_DEFAULT, {110, 200}))
5837 << "Inject motion event should return InputEventInjectionResult::FAILED";
5838 windowInPrimary->assertNoEvents();
5839 monitorInPrimary.assertNoEvents();
5840
5841 ASSERT_EQ(InputEventInjectionResult::FAILED,
5842 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5843 SECOND_DISPLAY_ID, {110, 200}))
5844 << "Inject motion event should return InputEventInjectionResult::FAILED";
5845 windowInSecondary->assertNoEvents();
5846 monitorInSecondary.assertNoEvents();
5847}
5848
Jackal Guof9696682018-10-05 12:23:23 +08005849class InputFilterTest : public InputDispatcherTest {
5850protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005851 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
5852 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08005853 NotifyMotionArgs motionArgs;
5854
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005855 motionArgs =
5856 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005857 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005858 motionArgs =
5859 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005860 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005861 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005862 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005863 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
5864 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08005865 } else {
5866 mFakePolicy->assertFilterInputEventWasNotCalled();
5867 }
5868 }
5869
5870 void testNotifyKey(bool expectToBeFiltered) {
5871 NotifyKeyArgs keyArgs;
5872
5873 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005874 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005875 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005876 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005877 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005878
5879 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08005880 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005881 } else {
5882 mFakePolicy->assertFilterInputEventWasNotCalled();
5883 }
5884 }
5885};
5886
5887// Test InputFilter for MotionEvent
5888TEST_F(InputFilterTest, MotionEvent_InputFilter) {
5889 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
5890 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5891 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5892
5893 // Enable InputFilter
5894 mDispatcher->setInputFilterEnabled(true);
5895 // Test touch on both primary and second display, and check if both events are filtered.
5896 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
5897 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
5898
5899 // Disable InputFilter
5900 mDispatcher->setInputFilterEnabled(false);
5901 // Test touch on both primary and second display, and check if both events aren't filtered.
5902 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5903 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5904}
5905
5906// Test InputFilter for KeyEvent
5907TEST_F(InputFilterTest, KeyEvent_InputFilter) {
5908 // Since the InputFilter is disabled by default, check if key event aren't filtered.
5909 testNotifyKey(/*expectToBeFiltered*/ false);
5910
5911 // Enable InputFilter
5912 mDispatcher->setInputFilterEnabled(true);
5913 // Send a key event, and check if it is filtered.
5914 testNotifyKey(/*expectToBeFiltered*/ true);
5915
5916 // Disable InputFilter
5917 mDispatcher->setInputFilterEnabled(false);
5918 // Send a key event, and check if it isn't filtered.
5919 testNotifyKey(/*expectToBeFiltered*/ false);
5920}
5921
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005922// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
5923// logical display coordinate space.
5924TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
5925 ui::Transform firstDisplayTransform;
5926 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5927 ui::Transform secondDisplayTransform;
5928 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
5929
5930 std::vector<gui::DisplayInfo> displayInfos(2);
5931 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
5932 displayInfos[0].transform = firstDisplayTransform;
5933 displayInfos[1].displayId = SECOND_DISPLAY_ID;
5934 displayInfos[1].transform = secondDisplayTransform;
5935
Patrick Williamsd828f302023-04-28 17:52:08 -05005936 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005937
5938 // Enable InputFilter
5939 mDispatcher->setInputFilterEnabled(true);
5940
5941 // Ensure the correct transforms are used for the displays.
5942 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
5943 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
5944}
5945
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005946class InputFilterInjectionPolicyTest : public InputDispatcherTest {
5947protected:
5948 virtual void SetUp() override {
5949 InputDispatcherTest::SetUp();
5950
5951 /**
5952 * We don't need to enable input filter to test the injected event policy, but we enabled it
5953 * here to make the tests more realistic, since this policy only matters when inputfilter is
5954 * on.
5955 */
5956 mDispatcher->setInputFilterEnabled(true);
5957
5958 std::shared_ptr<InputApplicationHandle> application =
5959 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005960 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
5961 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005962
5963 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5964 mWindow->setFocusable(true);
5965 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5966 setFocusedWindow(mWindow);
5967 mWindow->consumeFocusEvent(true);
5968 }
5969
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005970 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5971 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005972 KeyEvent event;
5973
5974 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5975 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
5976 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00005977 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005978 const int32_t additionalPolicyFlags =
5979 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
5980 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005981 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005982 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5983 policyFlags | additionalPolicyFlags));
5984
5985 InputEvent* received = mWindow->consume();
5986 ASSERT_NE(nullptr, received);
5987 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005988 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005989 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
5990 ASSERT_EQ(flags, keyEvent.getFlags());
5991 }
5992
5993 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5994 int32_t flags) {
5995 MotionEvent event;
5996 PointerProperties pointerProperties[1];
5997 PointerCoords pointerCoords[1];
5998 pointerProperties[0].clear();
5999 pointerProperties[0].id = 0;
6000 pointerCoords[0].clear();
6001 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6002 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6003
6004 ui::Transform identityTransform;
6005 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6006 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6007 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6008 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6009 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006010 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006011 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006012 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6013
6014 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6015 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006016 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006017 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6018 policyFlags | additionalPolicyFlags));
6019
6020 InputEvent* received = mWindow->consume();
6021 ASSERT_NE(nullptr, received);
6022 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006023 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006024 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6025 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006026 }
6027
6028private:
6029 sp<FakeWindowHandle> mWindow;
6030};
6031
6032TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006033 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6034 // filter. Without it, the event will no different from a regularly injected event, and the
6035 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006036 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6037 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006038}
6039
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006040TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006041 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006042 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006043 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6044}
6045
6046TEST_F(InputFilterInjectionPolicyTest,
6047 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6048 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006049 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006050 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006051}
6052
6053TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006054 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6055 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006056}
6057
chaviwfd6d3512019-03-25 13:23:49 -07006058class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006059 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006060 InputDispatcherTest::SetUp();
6061
Chris Yea209fde2020-07-22 13:54:51 -07006062 std::shared_ptr<FakeApplicationHandle> application =
6063 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006064 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006065 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006066 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006067
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006068 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006069 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006070 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006071
6072 // Set focused application.
6073 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006074 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006075
6076 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006077 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006078 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006079 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006080 }
6081
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006082 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006083 InputDispatcherTest::TearDown();
6084
6085 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006086 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006087 }
6088
6089protected:
6090 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006091 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006092 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006093};
6094
6095// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6096// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6097// the onPointerDownOutsideFocus callback.
6098TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006099 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006100 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6101 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006102 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006103 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006104
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006105 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006106 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6107}
6108
6109// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6110// DOWN on the window that doesn't have focus. Ensure no window received the
6111// onPointerDownOutsideFocus callback.
6112TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006113 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006114 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006115 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006116 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006117
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006118 ASSERT_TRUE(mDispatcher->waitForIdle());
6119 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006120}
6121
6122// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6123// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6124TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006125 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6126 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006127 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006128 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006129
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006130 ASSERT_TRUE(mDispatcher->waitForIdle());
6131 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006132}
6133
6134// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6135// DOWN on the window that already has focus. Ensure no window received the
6136// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006137TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006138 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006139 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006140 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006141 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006142 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006143
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006144 ASSERT_TRUE(mDispatcher->waitForIdle());
6145 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006146}
6147
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006148// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6149// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6150TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6151 const MotionEvent event =
6152 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6153 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006154 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006155 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6156 .build();
6157 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6158 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6159 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6160
6161 ASSERT_TRUE(mDispatcher->waitForIdle());
6162 mFakePolicy->assertOnPointerDownWasNotCalled();
6163 // Ensure that the unfocused window did not receive any FOCUS events.
6164 mUnfocusedWindow->assertNoEvents();
6165}
6166
chaviwaf87b3e2019-10-01 16:59:28 -07006167// These tests ensures we can send touch events to a single client when there are multiple input
6168// windows that point to the same client token.
6169class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6170 virtual void SetUp() override {
6171 InputDispatcherTest::SetUp();
6172
Chris Yea209fde2020-07-22 13:54:51 -07006173 std::shared_ptr<FakeApplicationHandle> application =
6174 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006175 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6176 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006177 mWindow1->setFrame(Rect(0, 0, 100, 100));
6178
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006179 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6180 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006181 mWindow2->setFrame(Rect(100, 100, 200, 200));
6182
Arthur Hung72d8dc32020-03-28 00:48:39 +00006183 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006184 }
6185
6186protected:
6187 sp<FakeWindowHandle> mWindow1;
6188 sp<FakeWindowHandle> mWindow2;
6189
6190 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006191 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006192 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6193 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006194 }
6195
6196 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6197 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006198 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006199 InputEvent* event = window->consume();
6200
6201 ASSERT_NE(nullptr, event) << name.c_str()
6202 << ": consumer should have returned non-NULL event.";
6203
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006204 ASSERT_EQ(InputEventType::MOTION, event->getType())
6205 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006206
6207 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006208 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006209 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006210
6211 for (size_t i = 0; i < points.size(); i++) {
6212 float expectedX = points[i].x;
6213 float expectedY = points[i].y;
6214
6215 EXPECT_EQ(expectedX, motionEvent.getX(i))
6216 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6217 << ", got " << motionEvent.getX(i);
6218 EXPECT_EQ(expectedY, motionEvent.getY(i))
6219 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6220 << ", got " << motionEvent.getY(i);
6221 }
6222 }
chaviw9eaa22c2020-07-01 16:21:27 -07006223
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006224 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006225 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006226 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6227 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006228
6229 // Always consume from window1 since it's the window that has the InputReceiver
6230 consumeMotionEvent(mWindow1, action, expectedPoints);
6231 }
chaviwaf87b3e2019-10-01 16:59:28 -07006232};
6233
6234TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6235 // Touch Window 1
6236 PointF touchedPoint = {10, 10};
6237 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006238 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006239
6240 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006241 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006242
6243 // Touch Window 2
6244 touchedPoint = {150, 150};
6245 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006246 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006247}
6248
chaviw9eaa22c2020-07-01 16:21:27 -07006249TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6250 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006251 mWindow2->setWindowScale(0.5f, 0.5f);
6252
6253 // Touch Window 1
6254 PointF touchedPoint = {10, 10};
6255 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006256 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006257 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006258 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006259
6260 // Touch Window 2
6261 touchedPoint = {150, 150};
6262 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006263 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6264 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006265
chaviw9eaa22c2020-07-01 16:21:27 -07006266 // Update the transform so rotation is set
6267 mWindow2->setWindowTransform(0, -1, 1, 0);
6268 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6269 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006270}
6271
chaviw9eaa22c2020-07-01 16:21:27 -07006272TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006273 mWindow2->setWindowScale(0.5f, 0.5f);
6274
6275 // Touch Window 1
6276 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6277 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006278 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006279
6280 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006281 touchedPoints.push_back(PointF{150, 150});
6282 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006283 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006284
chaviw9eaa22c2020-07-01 16:21:27 -07006285 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006286 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006287 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006288
chaviw9eaa22c2020-07-01 16:21:27 -07006289 // Update the transform so rotation is set for Window 2
6290 mWindow2->setWindowTransform(0, -1, 1, 0);
6291 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006292 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006293}
6294
chaviw9eaa22c2020-07-01 16:21:27 -07006295TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006296 mWindow2->setWindowScale(0.5f, 0.5f);
6297
6298 // Touch Window 1
6299 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6300 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006301 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006302
6303 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006304 touchedPoints.push_back(PointF{150, 150});
6305 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006306
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006307 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006308
6309 // Move both windows
6310 touchedPoints = {{20, 20}, {175, 175}};
6311 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6312 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6313
chaviw9eaa22c2020-07-01 16:21:27 -07006314 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006315
chaviw9eaa22c2020-07-01 16:21:27 -07006316 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006317 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006318 expectedPoints.pop_back();
6319
6320 // Touch Window 2
6321 mWindow2->setWindowTransform(0, -1, 1, 0);
6322 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006323 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006324
6325 // Move both windows
6326 touchedPoints = {{20, 20}, {175, 175}};
6327 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6328 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6329
6330 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006331}
6332
6333TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6334 mWindow1->setWindowScale(0.5f, 0.5f);
6335
6336 // Touch Window 1
6337 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6338 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006339 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006340
6341 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006342 touchedPoints.push_back(PointF{150, 150});
6343 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006344
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006345 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006346
6347 // Move both windows
6348 touchedPoints = {{20, 20}, {175, 175}};
6349 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6350 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6351
chaviw9eaa22c2020-07-01 16:21:27 -07006352 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006353}
6354
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006355/**
6356 * When one of the windows is slippery, the touch should not slip into the other window with the
6357 * same input channel.
6358 */
6359TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6360 mWindow1->setSlippery(true);
6361 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6362
6363 // Touch down in window 1
6364 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6365 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6366 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6367
6368 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6369 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6370 // getting generated.
6371 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6372 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6373
6374 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6375}
6376
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07006377/**
6378 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
6379 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
6380 * that the pointer is hovering over may have a different transform.
6381 */
6382TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
6383 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6384
6385 // Start hover in window 1
6386 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN,
6387 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6388 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6389 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
6390
6391 // Move hover to window 2.
6392 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6393 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6394
6395 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
6396 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6397 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
6398}
6399
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006400class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6401 virtual void SetUp() override {
6402 InputDispatcherTest::SetUp();
6403
Chris Yea209fde2020-07-22 13:54:51 -07006404 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006405 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006406 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6407 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006408 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006409 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006410 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006411
6412 // Set focused application.
6413 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6414
6415 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006416 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006417 mWindow->consumeFocusEvent(true);
6418 }
6419
6420 virtual void TearDown() override {
6421 InputDispatcherTest::TearDown();
6422 mWindow.clear();
6423 }
6424
6425protected:
Chris Yea209fde2020-07-22 13:54:51 -07006426 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006427 sp<FakeWindowHandle> mWindow;
6428 static constexpr PointF WINDOW_LOCATION = {20, 20};
6429
6430 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006431 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006432 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6433 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006434 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006435 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6436 WINDOW_LOCATION));
6437 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006438
6439 sp<FakeWindowHandle> addSpyWindow() {
6440 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006441 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006442 spy->setTrustedOverlay(true);
6443 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006444 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006445 spy->setDispatchingTimeout(30ms);
6446 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6447 return spy;
6448 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006449};
6450
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006451// Send a tap and respond, which should not cause an ANR.
6452TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6453 tapOnWindow();
6454 mWindow->consumeMotionDown();
6455 mWindow->consumeMotionUp();
6456 ASSERT_TRUE(mDispatcher->waitForIdle());
6457 mFakePolicy->assertNotifyAnrWasNotCalled();
6458}
6459
6460// Send a regular key and respond, which should not cause an ANR.
6461TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006462 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006463 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6464 ASSERT_TRUE(mDispatcher->waitForIdle());
6465 mFakePolicy->assertNotifyAnrWasNotCalled();
6466}
6467
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006468TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6469 mWindow->setFocusable(false);
6470 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6471 mWindow->consumeFocusEvent(false);
6472
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006473 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006474 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6475 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6476 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006477 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006478 // Key will not go to window because we have no focused window.
6479 // The 'no focused window' ANR timer should start instead.
6480
6481 // Now, the focused application goes away.
6482 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6483 // The key should get dropped and there should be no ANR.
6484
6485 ASSERT_TRUE(mDispatcher->waitForIdle());
6486 mFakePolicy->assertNotifyAnrWasNotCalled();
6487}
6488
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006489// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006490// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6491// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006492TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006493 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006494 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6495 WINDOW_LOCATION));
6496
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006497 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6498 ASSERT_TRUE(sequenceNum);
6499 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006500 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006501
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006502 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006503 mWindow->consumeMotionEvent(
6504 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006505 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006506 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006507}
6508
6509// Send a key to the app and have the app not respond right away.
6510TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6511 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006512 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006513 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6514 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006515 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006516 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006517 ASSERT_TRUE(mDispatcher->waitForIdle());
6518}
6519
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006520// We have a focused application, but no focused window
6521TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006522 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006523 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6524 mWindow->consumeFocusEvent(false);
6525
6526 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006527 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006528 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6529 WINDOW_LOCATION));
6530 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6531 mDispatcher->waitForIdle();
6532 mFakePolicy->assertNotifyAnrWasNotCalled();
6533
6534 // Once a focused event arrives, we get an ANR for this application
6535 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6536 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006537 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006538 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6539 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006540 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006541 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006542 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006543 ASSERT_TRUE(mDispatcher->waitForIdle());
6544}
6545
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006546/**
6547 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6548 * there will not be an ANR.
6549 */
6550TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6551 mWindow->setFocusable(false);
6552 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6553 mWindow->consumeFocusEvent(false);
6554
6555 KeyEvent event;
6556 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6557 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6558
6559 // Define a valid key down event that is stale (too old).
6560 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6561 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006562 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006563
6564 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6565
6566 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006567 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006568 InputEventInjectionSync::WAIT_FOR_RESULT,
6569 INJECT_EVENT_TIMEOUT, policyFlags);
6570 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6571 << "Injection should fail because the event is stale";
6572
6573 ASSERT_TRUE(mDispatcher->waitForIdle());
6574 mFakePolicy->assertNotifyAnrWasNotCalled();
6575 mWindow->assertNoEvents();
6576}
6577
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006578// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006579// Make sure that we don't notify policy twice about the same ANR.
6580TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006581 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006582 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6583 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006584
6585 // Once a focused event arrives, we get an ANR for this application
6586 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6587 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006588 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006589 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6590 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006591 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006592 const std::chrono::duration appTimeout =
6593 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6594 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006595
Vishnu Naire4df8752022-09-08 09:17:55 -07006596 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006597 // ANR should not be raised again. It is up to policy to do that if it desires.
6598 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006599
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006600 // If we now get a focused window, the ANR should stop, but the policy handles that via
6601 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006602 ASSERT_TRUE(mDispatcher->waitForIdle());
6603}
6604
6605// We have a focused application, but no focused window
6606TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006607 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006608 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6609 mWindow->consumeFocusEvent(false);
6610
6611 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006612 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006613 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006614 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6615 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006616
Vishnu Naire4df8752022-09-08 09:17:55 -07006617 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6618 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006619
6620 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006621 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006622 ASSERT_TRUE(mDispatcher->waitForIdle());
6623 mWindow->assertNoEvents();
6624}
6625
6626/**
6627 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6628 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6629 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6630 * the ANR mechanism should still work.
6631 *
6632 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6633 * DOWN event, while not responding on the second one.
6634 */
6635TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6636 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6637 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6638 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6639 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6640 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006641 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006642
6643 // Now send ACTION_UP, with identical timestamp
6644 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6645 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6646 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6647 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006648 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006649
6650 // We have now sent down and up. Let's consume first event and then ANR on the second.
6651 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6652 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006653 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006654}
6655
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006656// A spy window can receive an ANR
6657TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6658 sp<FakeWindowHandle> spy = addSpyWindow();
6659
6660 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6661 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6662 WINDOW_LOCATION));
6663 mWindow->consumeMotionDown();
6664
6665 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6666 ASSERT_TRUE(sequenceNum);
6667 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006668 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006669
6670 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006671 spy->consumeMotionEvent(
6672 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006673 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006674 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006675}
6676
6677// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006678// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006679TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6680 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006681
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006682 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6683 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006684 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006685 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006686
6687 // Stuck on the ACTION_UP
6688 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006689 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006690
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006691 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006692 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006693 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6694 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006695
6696 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6697 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006698 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006699 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006700 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006701}
6702
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006703// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006704// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006705TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6706 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006707
6708 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006709 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6710 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006711
6712 mWindow->consumeMotionDown();
6713 // Stuck on the ACTION_UP
6714 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006715 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006716
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006717 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006718 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006719 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6720 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006721
6722 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6723 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006724 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006725 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006726 spy->assertNoEvents();
6727}
6728
6729TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6730 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6731
6732 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6733
6734 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6735 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6736 WINDOW_LOCATION));
6737
6738 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6739 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6740 ASSERT_TRUE(consumeSeq);
6741
Prabir Pradhanedd96402022-02-15 01:46:16 -08006742 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006743
6744 monitor.finishEvent(*consumeSeq);
6745 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6746
6747 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006748 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006749}
6750
6751// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6752// process events, you don't get an anr. When the window later becomes unresponsive again, you
6753// get an ANR again.
6754// 1. tap -> block on ACTION_UP -> receive ANR
6755// 2. consume all pending events (= queue becomes healthy again)
6756// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6757TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6758 tapOnWindow();
6759
6760 mWindow->consumeMotionDown();
6761 // Block on ACTION_UP
6762 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006763 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006764 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6765 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006766 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006767 mWindow->assertNoEvents();
6768
6769 tapOnWindow();
6770 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006771 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006772 mWindow->consumeMotionUp();
6773
6774 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006775 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006776 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006777 mWindow->assertNoEvents();
6778}
6779
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006780// If a connection remains unresponsive for a while, make sure policy is only notified once about
6781// it.
6782TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006783 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006784 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6785 WINDOW_LOCATION));
6786
6787 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006788 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006789 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006790 // 'notifyConnectionUnresponsive' should only be called once per connection
6791 mFakePolicy->assertNotifyAnrWasNotCalled();
6792 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006793 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006794 mWindow->consumeMotionEvent(
6795 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006796 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006797 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006798 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006799 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006800}
6801
6802/**
6803 * If a window is processing a motion event, and then a key event comes in, the key event should
6804 * not to to the focused window until the motion is processed.
6805 *
6806 * Warning!!!
6807 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6808 * and the injection timeout that we specify when injecting the key.
6809 * We must have the injection timeout (10ms) be smaller than
6810 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6811 *
6812 * If that value changes, this test should also change.
6813 */
6814TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
6815 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6816 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6817
6818 tapOnWindow();
6819 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6820 ASSERT_TRUE(downSequenceNum);
6821 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6822 ASSERT_TRUE(upSequenceNum);
6823 // Don't finish the events yet, and send a key
6824 // Injection will "succeed" because we will eventually give up and send the key to the focused
6825 // window even if motions are still being processed. But because the injection timeout is short,
6826 // we will receive INJECTION_TIMED_OUT as the result.
6827
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006828 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006829 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006830 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6831 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006832 // Key will not be sent to the window, yet, because the window is still processing events
6833 // and the key remains pending, waiting for the touch events to be processed
6834 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6835 ASSERT_FALSE(keySequenceNum);
6836
6837 std::this_thread::sleep_for(500ms);
6838 // if we wait long enough though, dispatcher will give up, and still send the key
6839 // to the focused window, even though we have not yet finished the motion event
6840 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6841 mWindow->finishEvent(*downSequenceNum);
6842 mWindow->finishEvent(*upSequenceNum);
6843}
6844
6845/**
6846 * If a window is processing a motion event, and then a key event comes in, the key event should
6847 * not go to the focused window until the motion is processed.
6848 * If then a new motion comes in, then the pending key event should be going to the currently
6849 * focused window right away.
6850 */
6851TEST_F(InputDispatcherSingleWindowAnr,
6852 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
6853 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6854 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6855
6856 tapOnWindow();
6857 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6858 ASSERT_TRUE(downSequenceNum);
6859 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6860 ASSERT_TRUE(upSequenceNum);
6861 // Don't finish the events yet, and send a key
6862 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006863 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006864 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6865 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006866 // At this point, key is still pending, and should not be sent to the application yet.
6867 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6868 ASSERT_FALSE(keySequenceNum);
6869
6870 // Now tap down again. It should cause the pending key to go to the focused window right away.
6871 tapOnWindow();
6872 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
6873 // the other events yet. We can finish events in any order.
6874 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
6875 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
6876 mWindow->consumeMotionDown();
6877 mWindow->consumeMotionUp();
6878 mWindow->assertNoEvents();
6879}
6880
6881class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
6882 virtual void SetUp() override {
6883 InputDispatcherTest::SetUp();
6884
Chris Yea209fde2020-07-22 13:54:51 -07006885 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006886 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006887 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
6888 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006889 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006890 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006891 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006892
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006893 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
6894 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006895 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006896 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006897
6898 // Set focused application.
6899 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07006900 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006901
6902 // Expect one focus window exist in display.
6903 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006904 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006905 mFocusedWindow->consumeFocusEvent(true);
6906 }
6907
6908 virtual void TearDown() override {
6909 InputDispatcherTest::TearDown();
6910
6911 mUnfocusedWindow.clear();
6912 mFocusedWindow.clear();
6913 }
6914
6915protected:
Chris Yea209fde2020-07-22 13:54:51 -07006916 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006917 sp<FakeWindowHandle> mUnfocusedWindow;
6918 sp<FakeWindowHandle> mFocusedWindow;
6919 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
6920 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
6921 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
6922
6923 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
6924
6925 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
6926
6927private:
6928 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006929 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006930 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6931 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006932 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006933 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6934 location));
6935 }
6936};
6937
6938// If we have 2 windows that are both unresponsive, the one with the shortest timeout
6939// should be ANR'd first.
6940TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006941 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006942 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6943 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006944 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006945 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006946 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006947 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006948 // We consumed all events, so no ANR
6949 ASSERT_TRUE(mDispatcher->waitForIdle());
6950 mFakePolicy->assertNotifyAnrWasNotCalled();
6951
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006952 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006953 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6954 FOCUSED_WINDOW_LOCATION));
6955 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
6956 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006957
6958 const std::chrono::duration timeout =
6959 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006960 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006961 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
6962 // sequence to make it consistent
6963 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006964 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006965 mFocusedWindow->consumeMotionDown();
6966 // This cancel is generated because the connection was unresponsive
6967 mFocusedWindow->consumeMotionCancel();
6968 mFocusedWindow->assertNoEvents();
6969 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006970 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006971 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6972 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006973 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006974}
6975
6976// If we have 2 windows with identical timeouts that are both unresponsive,
6977// it doesn't matter which order they should have ANR.
6978// But we should receive ANR for both.
6979TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
6980 // Set the timeout for unfocused window to match the focused window
6981 mUnfocusedWindow->setDispatchingTimeout(10ms);
6982 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6983
6984 tapOnFocusedWindow();
6985 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08006986 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
6987 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
6988 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006989
6990 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006991 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
6992 mFocusedWindow->getToken() == anrConnectionToken2);
6993 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
6994 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006995
6996 ASSERT_TRUE(mDispatcher->waitForIdle());
6997 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006998
6999 mFocusedWindow->consumeMotionDown();
7000 mFocusedWindow->consumeMotionUp();
7001 mUnfocusedWindow->consumeMotionOutside();
7002
Prabir Pradhanedd96402022-02-15 01:46:16 -08007003 sp<IBinder> responsiveToken1, responsiveToken2;
7004 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
7005 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007006
7007 // Both applications should be marked as responsive, in any order
7008 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
7009 mFocusedWindow->getToken() == responsiveToken2);
7010 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
7011 mUnfocusedWindow->getToken() == responsiveToken2);
7012 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007013}
7014
7015// If a window is already not responding, the second tap on the same window should be ignored.
7016// We should also log an error to account for the dropped event (not tested here).
7017// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7018TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7019 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007020 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007021 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007022 // Receive the events, but don't respond
7023 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7024 ASSERT_TRUE(downEventSequenceNum);
7025 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7026 ASSERT_TRUE(upEventSequenceNum);
7027 const std::chrono::duration timeout =
7028 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007029 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007030
7031 // Tap once again
7032 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007033 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007034 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7035 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007036 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007037 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7038 FOCUSED_WINDOW_LOCATION));
7039 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7040 // valid touch target
7041 mUnfocusedWindow->assertNoEvents();
7042
7043 // Consume the first tap
7044 mFocusedWindow->finishEvent(*downEventSequenceNum);
7045 mFocusedWindow->finishEvent(*upEventSequenceNum);
7046 ASSERT_TRUE(mDispatcher->waitForIdle());
7047 // The second tap did not go to the focused window
7048 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007049 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007050 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7051 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007052 mFakePolicy->assertNotifyAnrWasNotCalled();
7053}
7054
7055// If you tap outside of all windows, there will not be ANR
7056TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007057 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007058 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7059 LOCATION_OUTSIDE_ALL_WINDOWS));
7060 ASSERT_TRUE(mDispatcher->waitForIdle());
7061 mFakePolicy->assertNotifyAnrWasNotCalled();
7062}
7063
7064// Since the focused window is paused, tapping on it should not produce any events
7065TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7066 mFocusedWindow->setPaused(true);
7067 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7068
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007069 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007070 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7071 FOCUSED_WINDOW_LOCATION));
7072
7073 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7074 ASSERT_TRUE(mDispatcher->waitForIdle());
7075 // Should not ANR because the window is paused, and touches shouldn't go to it
7076 mFakePolicy->assertNotifyAnrWasNotCalled();
7077
7078 mFocusedWindow->assertNoEvents();
7079 mUnfocusedWindow->assertNoEvents();
7080}
7081
7082/**
7083 * If a window is processing a motion event, and then a key event comes in, the key event should
7084 * not to to the focused window until the motion is processed.
7085 * If a different window becomes focused at this time, the key should go to that window instead.
7086 *
7087 * Warning!!!
7088 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7089 * and the injection timeout that we specify when injecting the key.
7090 * We must have the injection timeout (10ms) be smaller than
7091 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7092 *
7093 * If that value changes, this test should also change.
7094 */
7095TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7096 // Set a long ANR timeout to prevent it from triggering
7097 mFocusedWindow->setDispatchingTimeout(2s);
7098 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7099
7100 tapOnUnfocusedWindow();
7101 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7102 ASSERT_TRUE(downSequenceNum);
7103 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7104 ASSERT_TRUE(upSequenceNum);
7105 // Don't finish the events yet, and send a key
7106 // Injection will succeed because we will eventually give up and send the key to the focused
7107 // window even if motions are still being processed.
7108
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007109 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007110 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7111 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007112 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007113 // Key will not be sent to the window, yet, because the window is still processing events
7114 // and the key remains pending, waiting for the touch events to be processed
7115 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7116 ASSERT_FALSE(keySequenceNum);
7117
7118 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007119 mFocusedWindow->setFocusable(false);
7120 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007121 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007122 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007123
7124 // Focus events should precede the key events
7125 mUnfocusedWindow->consumeFocusEvent(true);
7126 mFocusedWindow->consumeFocusEvent(false);
7127
7128 // Finish the tap events, which should unblock dispatcher
7129 mUnfocusedWindow->finishEvent(*downSequenceNum);
7130 mUnfocusedWindow->finishEvent(*upSequenceNum);
7131
7132 // Now that all queues are cleared and no backlog in the connections, the key event
7133 // can finally go to the newly focused "mUnfocusedWindow".
7134 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7135 mFocusedWindow->assertNoEvents();
7136 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007137 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007138}
7139
7140// When the touch stream is split across 2 windows, and one of them does not respond,
7141// then ANR should be raised and the touch should be canceled for the unresponsive window.
7142// The other window should not be affected by that.
7143TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7144 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007145 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7146 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7147 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007148 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007149 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007150
7151 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007152 mDispatcher->notifyMotion(
7153 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7154 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007155
7156 const std::chrono::duration timeout =
7157 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007158 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007159
7160 mUnfocusedWindow->consumeMotionDown();
7161 mFocusedWindow->consumeMotionDown();
7162 // Focused window may or may not receive ACTION_MOVE
7163 // But it should definitely receive ACTION_CANCEL due to the ANR
7164 InputEvent* event;
7165 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7166 ASSERT_TRUE(moveOrCancelSequenceNum);
7167 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7168 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007169 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007170 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7171 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7172 mFocusedWindow->consumeMotionCancel();
7173 } else {
7174 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7175 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007176 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007177 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7178 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007179
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007180 mUnfocusedWindow->assertNoEvents();
7181 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007182 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007183}
7184
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007185/**
7186 * If we have no focused window, and a key comes in, we start the ANR timer.
7187 * The focused application should add a focused window before the timer runs out to prevent ANR.
7188 *
7189 * If the user touches another application during this time, the key should be dropped.
7190 * Next, if a new focused window comes in, without toggling the focused application,
7191 * then no ANR should occur.
7192 *
7193 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7194 * but in some cases the policy may not update the focused application.
7195 */
7196TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7197 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7198 std::make_shared<FakeApplicationHandle>();
7199 focusedApplication->setDispatchingTimeout(60ms);
7200 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7201 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7202 mFocusedWindow->setFocusable(false);
7203
7204 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7205 mFocusedWindow->consumeFocusEvent(false);
7206
7207 // Send a key. The ANR timer should start because there is no focused window.
7208 // 'focusedApplication' will get blamed if this timer completes.
7209 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007210 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007211 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7212 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7213 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007214 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007215
7216 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7217 // then the injected touches won't cause the focused event to get dropped.
7218 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7219 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7220 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7221 // For this test, it means that the key would get delivered to the window once it becomes
7222 // focused.
7223 std::this_thread::sleep_for(10ms);
7224
7225 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007226 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7227 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7228 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007229
7230 // We do not consume the motion right away, because that would require dispatcher to first
7231 // process (== drop) the key event, and by that time, ANR will be raised.
7232 // Set the focused window first.
7233 mFocusedWindow->setFocusable(true);
7234 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7235 setFocusedWindow(mFocusedWindow);
7236 mFocusedWindow->consumeFocusEvent(true);
7237 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7238 // to another application. This could be a bug / behaviour in the policy.
7239
7240 mUnfocusedWindow->consumeMotionDown();
7241
7242 ASSERT_TRUE(mDispatcher->waitForIdle());
7243 // Should not ANR because we actually have a focused window. It was just added too slowly.
7244 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7245}
7246
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007247// These tests ensure we cannot send touch events to a window that's positioned behind a window
7248// that has feature NO_INPUT_CHANNEL.
7249// Layout:
7250// Top (closest to user)
7251// mNoInputWindow (above all windows)
7252// mBottomWindow
7253// Bottom (furthest from user)
7254class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7255 virtual void SetUp() override {
7256 InputDispatcherTest::SetUp();
7257
7258 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007259 mNoInputWindow =
7260 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7261 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007262 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007263 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007264 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7265 // It's perfectly valid for this window to not have an associated input channel
7266
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007267 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7268 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007269 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7270
7271 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7272 }
7273
7274protected:
7275 std::shared_ptr<FakeApplicationHandle> mApplication;
7276 sp<FakeWindowHandle> mNoInputWindow;
7277 sp<FakeWindowHandle> mBottomWindow;
7278};
7279
7280TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
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 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7289 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7290 // and therefore should prevent mBottomWindow from receiving touches
7291 mBottomWindow->assertNoEvents();
7292}
7293
7294/**
7295 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7296 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7297 */
7298TEST_F(InputDispatcherMultiWindowOcclusionTests,
7299 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007300 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7301 "Window with input channel and NO_INPUT_CHANNEL",
7302 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007303
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007304 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007305 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7306 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7307
7308 PointF touchedPoint = {10, 10};
7309
Prabir Pradhan678438e2023-04-13 19:32:51 +00007310 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7311 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7312 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007313
7314 mNoInputWindow->assertNoEvents();
7315 mBottomWindow->assertNoEvents();
7316}
7317
Vishnu Nair958da932020-08-21 17:12:37 -07007318class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7319protected:
7320 std::shared_ptr<FakeApplicationHandle> mApp;
7321 sp<FakeWindowHandle> mWindow;
7322 sp<FakeWindowHandle> mMirror;
7323
7324 virtual void SetUp() override {
7325 InputDispatcherTest::SetUp();
7326 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007327 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7328 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7329 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007330 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7331 mWindow->setFocusable(true);
7332 mMirror->setFocusable(true);
7333 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7334 }
7335};
7336
7337TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7338 // Request focus on a mirrored window
7339 setFocusedWindow(mMirror);
7340
7341 // window gets focused
7342 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007343 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7344 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007345 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7346}
7347
7348// A focused & mirrored window remains focused only if the window and its mirror are both
7349// focusable.
7350TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7351 setFocusedWindow(mMirror);
7352
7353 // window gets focused
7354 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007355 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7356 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007357 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007358 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7359 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007360 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7361
7362 mMirror->setFocusable(false);
7363 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7364
7365 // window loses focus since one of the windows associated with the token in not focusable
7366 mWindow->consumeFocusEvent(false);
7367
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007368 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7369 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007370 mWindow->assertNoEvents();
7371}
7372
7373// A focused & mirrored window remains focused until the window and its mirror both become
7374// invisible.
7375TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7376 setFocusedWindow(mMirror);
7377
7378 // window gets focused
7379 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007380 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7381 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007382 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007383 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7384 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007385 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7386
7387 mMirror->setVisible(false);
7388 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7389
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007390 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7391 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007392 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007393 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7394 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007395 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7396
7397 mWindow->setVisible(false);
7398 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7399
7400 // window loses focus only after all windows associated with the token become invisible.
7401 mWindow->consumeFocusEvent(false);
7402
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007403 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7404 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007405 mWindow->assertNoEvents();
7406}
7407
7408// A focused & mirrored window remains focused until both windows are removed.
7409TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7410 setFocusedWindow(mMirror);
7411
7412 // window gets focused
7413 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007414 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7415 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007416 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007417 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7418 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007419 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7420
7421 // single window is removed but the window token remains focused
7422 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7423
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7425 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007426 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007427 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7428 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007429 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7430
7431 // Both windows are removed
7432 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7433 mWindow->consumeFocusEvent(false);
7434
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007435 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7436 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007437 mWindow->assertNoEvents();
7438}
7439
7440// Focus request can be pending until one window becomes visible.
7441TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7442 // Request focus on an invisible mirror.
7443 mWindow->setVisible(false);
7444 mMirror->setVisible(false);
7445 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7446 setFocusedWindow(mMirror);
7447
7448 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007449 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007450 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7451 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007452
7453 mMirror->setVisible(true);
7454 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7455
7456 // window gets focused
7457 mWindow->consumeFocusEvent(true);
7458 // window gets the pending key event
7459 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7460}
Prabir Pradhan99987712020-11-10 18:43:05 -08007461
7462class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7463protected:
7464 std::shared_ptr<FakeApplicationHandle> mApp;
7465 sp<FakeWindowHandle> mWindow;
7466 sp<FakeWindowHandle> mSecondWindow;
7467
7468 void SetUp() override {
7469 InputDispatcherTest::SetUp();
7470 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007471 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007472 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007473 mSecondWindow =
7474 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007475 mSecondWindow->setFocusable(true);
7476
7477 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7478 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7479
7480 setFocusedWindow(mWindow);
7481 mWindow->consumeFocusEvent(true);
7482 }
7483
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007484 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007485 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007486 }
7487
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007488 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7489 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007490 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007491 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7492 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007493 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007494 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007495 }
7496};
7497
7498TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7499 // Ensure that capture cannot be obtained for unfocused windows.
7500 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7501 mFakePolicy->assertSetPointerCaptureNotCalled();
7502 mSecondWindow->assertNoEvents();
7503
7504 // Ensure that capture can be enabled from the focus window.
7505 requestAndVerifyPointerCapture(mWindow, true);
7506
7507 // Ensure that capture cannot be disabled from a window that does not have capture.
7508 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7509 mFakePolicy->assertSetPointerCaptureNotCalled();
7510
7511 // Ensure that capture can be disabled from the window with capture.
7512 requestAndVerifyPointerCapture(mWindow, false);
7513}
7514
7515TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007516 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007517
7518 setFocusedWindow(mSecondWindow);
7519
7520 // Ensure that the capture disabled event was sent first.
7521 mWindow->consumeCaptureEvent(false);
7522 mWindow->consumeFocusEvent(false);
7523 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007524 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007525
7526 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007527 notifyPointerCaptureChanged({});
7528 notifyPointerCaptureChanged(request);
7529 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007530 mWindow->assertNoEvents();
7531 mSecondWindow->assertNoEvents();
7532 mFakePolicy->assertSetPointerCaptureNotCalled();
7533}
7534
7535TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007536 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007537
7538 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007539 notifyPointerCaptureChanged({});
7540 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007541
7542 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007543 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007544 mWindow->consumeCaptureEvent(false);
7545 mWindow->assertNoEvents();
7546}
7547
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007548TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7549 requestAndVerifyPointerCapture(mWindow, true);
7550
7551 // The first window loses focus.
7552 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007553 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007554 mWindow->consumeCaptureEvent(false);
7555
7556 // Request Pointer Capture from the second window before the notification from InputReader
7557 // arrives.
7558 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007559 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007560
7561 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007562 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007563
7564 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007565 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007566
7567 mSecondWindow->consumeFocusEvent(true);
7568 mSecondWindow->consumeCaptureEvent(true);
7569}
7570
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007571TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7572 // App repeatedly enables and disables capture.
7573 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7574 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7575 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7576 mFakePolicy->assertSetPointerCaptureCalled(false);
7577 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7578 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7579
7580 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7581 // first request is now stale, this should do nothing.
7582 notifyPointerCaptureChanged(firstRequest);
7583 mWindow->assertNoEvents();
7584
7585 // InputReader notifies that the second request was enabled.
7586 notifyPointerCaptureChanged(secondRequest);
7587 mWindow->consumeCaptureEvent(true);
7588}
7589
Prabir Pradhan7092e262022-05-03 16:51:09 +00007590TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7591 requestAndVerifyPointerCapture(mWindow, true);
7592
7593 // App toggles pointer capture off and on.
7594 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7595 mFakePolicy->assertSetPointerCaptureCalled(false);
7596
7597 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7598 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7599
7600 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7601 // preceding "disable" request.
7602 notifyPointerCaptureChanged(enableRequest);
7603
7604 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7605 // any notifications.
7606 mWindow->assertNoEvents();
7607}
7608
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007609class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7610protected:
7611 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007612
7613 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7614 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7615
7616 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7617 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7618
7619 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7620 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7621 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7622 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7623 MAXIMUM_OBSCURING_OPACITY);
7624
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007625 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007626 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007627 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007628
7629 sp<FakeWindowHandle> mTouchWindow;
7630
7631 virtual void SetUp() override {
7632 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007633 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007634 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7635 }
7636
7637 virtual void TearDown() override {
7638 InputDispatcherTest::TearDown();
7639 mTouchWindow.clear();
7640 }
7641
chaviw3277faf2021-05-19 16:45:23 -05007642 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7643 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007644 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007645 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007646 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007647 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007648 return window;
7649 }
7650
7651 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7652 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7653 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007654 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007655 // Generate an arbitrary PID based on the UID
7656 window->setOwnerInfo(1777 + (uid % 10000), uid);
7657 return window;
7658 }
7659
7660 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007661 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7662 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7663 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007664 }
7665};
7666
7667TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007668 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007669 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007670 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007671
7672 touch();
7673
7674 mTouchWindow->assertNoEvents();
7675}
7676
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007677TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007678 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7679 const sp<FakeWindowHandle>& w =
7680 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7681 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7682
7683 touch();
7684
7685 mTouchWindow->assertNoEvents();
7686}
7687
7688TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007689 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7690 const sp<FakeWindowHandle>& w =
7691 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7692 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7693
7694 touch();
7695
7696 w->assertNoEvents();
7697}
7698
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007699TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007700 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7701 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007702
7703 touch();
7704
7705 mTouchWindow->consumeAnyMotionDown();
7706}
7707
7708TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007709 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007710 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007711 w->setFrame(Rect(0, 0, 50, 50));
7712 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007713
7714 touch({PointF{100, 100}});
7715
7716 mTouchWindow->consumeAnyMotionDown();
7717}
7718
7719TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007720 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007721 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007722 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7723
7724 touch();
7725
7726 mTouchWindow->consumeAnyMotionDown();
7727}
7728
7729TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7730 const sp<FakeWindowHandle>& w =
7731 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7732 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007733
7734 touch();
7735
7736 mTouchWindow->consumeAnyMotionDown();
7737}
7738
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007739TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7740 const sp<FakeWindowHandle>& w =
7741 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7742 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7743
7744 touch();
7745
7746 w->assertNoEvents();
7747}
7748
7749/**
7750 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7751 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7752 * window, the occluding window will still receive ACTION_OUTSIDE event.
7753 */
7754TEST_F(InputDispatcherUntrustedTouchesTest,
7755 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7756 const sp<FakeWindowHandle>& w =
7757 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007758 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007759 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7760
7761 touch();
7762
7763 w->consumeMotionOutside();
7764}
7765
7766TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
7767 const sp<FakeWindowHandle>& w =
7768 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007769 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007770 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7771
7772 touch();
7773
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007774 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007775}
7776
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007777TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007778 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007779 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7780 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007781 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7782
7783 touch();
7784
7785 mTouchWindow->consumeAnyMotionDown();
7786}
7787
7788TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
7789 const sp<FakeWindowHandle>& w =
7790 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7791 MAXIMUM_OBSCURING_OPACITY);
7792 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007793
7794 touch();
7795
7796 mTouchWindow->consumeAnyMotionDown();
7797}
7798
7799TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007800 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007801 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7802 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007803 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7804
7805 touch();
7806
7807 mTouchWindow->assertNoEvents();
7808}
7809
7810TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
7811 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
7812 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007813 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7814 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007815 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007816 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7817 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007818 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7819
7820 touch();
7821
7822 mTouchWindow->assertNoEvents();
7823}
7824
7825TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
7826 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
7827 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007828 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7829 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007830 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007831 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7832 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007833 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7834
7835 touch();
7836
7837 mTouchWindow->consumeAnyMotionDown();
7838}
7839
7840TEST_F(InputDispatcherUntrustedTouchesTest,
7841 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
7842 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007843 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7844 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007845 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007846 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7847 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007848 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7849
7850 touch();
7851
7852 mTouchWindow->consumeAnyMotionDown();
7853}
7854
7855TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
7856 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007857 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7858 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007859 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007860 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7861 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007862 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007863
7864 touch();
7865
7866 mTouchWindow->assertNoEvents();
7867}
7868
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007869TEST_F(InputDispatcherUntrustedTouchesTest,
7870 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
7871 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007872 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7873 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007874 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007875 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7876 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007877 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7878
7879 touch();
7880
7881 mTouchWindow->assertNoEvents();
7882}
7883
7884TEST_F(InputDispatcherUntrustedTouchesTest,
7885 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
7886 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007887 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7888 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007889 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007890 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7891 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007892 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7893
7894 touch();
7895
7896 mTouchWindow->consumeAnyMotionDown();
7897}
7898
7899TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
7900 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007901 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7902 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007903 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7904
7905 touch();
7906
7907 mTouchWindow->consumeAnyMotionDown();
7908}
7909
7910TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
7911 const sp<FakeWindowHandle>& w =
7912 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
7913 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7914
7915 touch();
7916
7917 mTouchWindow->consumeAnyMotionDown();
7918}
7919
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007920TEST_F(InputDispatcherUntrustedTouchesTest,
7921 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
7922 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7923 const sp<FakeWindowHandle>& w =
7924 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
7925 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7926
7927 touch();
7928
7929 mTouchWindow->assertNoEvents();
7930}
7931
7932TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
7933 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7934 const sp<FakeWindowHandle>& w =
7935 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
7936 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7937
7938 touch();
7939
7940 mTouchWindow->consumeAnyMotionDown();
7941}
7942
7943TEST_F(InputDispatcherUntrustedTouchesTest,
7944 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
7945 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
7946 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007947 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7948 OPACITY_ABOVE_THRESHOLD);
7949 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7950
7951 touch();
7952
7953 mTouchWindow->consumeAnyMotionDown();
7954}
7955
7956TEST_F(InputDispatcherUntrustedTouchesTest,
7957 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
7958 const sp<FakeWindowHandle>& w1 =
7959 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7960 OPACITY_BELOW_THRESHOLD);
7961 const sp<FakeWindowHandle>& w2 =
7962 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7963 OPACITY_BELOW_THRESHOLD);
7964 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7965
7966 touch();
7967
7968 mTouchWindow->assertNoEvents();
7969}
7970
7971/**
7972 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
7973 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
7974 * (which alone would result in allowing touches) does not affect the blocking behavior.
7975 */
7976TEST_F(InputDispatcherUntrustedTouchesTest,
7977 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
7978 const sp<FakeWindowHandle>& wB =
7979 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7980 OPACITY_BELOW_THRESHOLD);
7981 const sp<FakeWindowHandle>& wC =
7982 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7983 OPACITY_BELOW_THRESHOLD);
7984 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7985
7986 touch();
7987
7988 mTouchWindow->assertNoEvents();
7989}
7990
7991/**
7992 * This test is testing that a window from a different UID but with same application token doesn't
7993 * block the touch. Apps can share the application token for close UI collaboration for example.
7994 */
7995TEST_F(InputDispatcherUntrustedTouchesTest,
7996 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
7997 const sp<FakeWindowHandle>& w =
7998 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7999 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008000 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8001
8002 touch();
8003
8004 mTouchWindow->consumeAnyMotionDown();
8005}
8006
arthurhungb89ccb02020-12-30 16:19:01 +08008007class InputDispatcherDragTests : public InputDispatcherTest {
8008protected:
8009 std::shared_ptr<FakeApplicationHandle> mApp;
8010 sp<FakeWindowHandle> mWindow;
8011 sp<FakeWindowHandle> mSecondWindow;
8012 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008013 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008014 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8015 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008016
8017 void SetUp() override {
8018 InputDispatcherTest::SetUp();
8019 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008020 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008021 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008022
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008023 mSecondWindow =
8024 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008025 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008026
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008027 mSpyWindow =
8028 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008029 mSpyWindow->setSpy(true);
8030 mSpyWindow->setTrustedOverlay(true);
8031 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8032
arthurhungb89ccb02020-12-30 16:19:01 +08008033 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008034 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008035 }
8036
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008037 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8038 switch (fromSource) {
8039 case AINPUT_SOURCE_TOUCHSCREEN:
8040 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8041 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8042 ADISPLAY_ID_DEFAULT, {50, 50}))
8043 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8044 break;
8045 case AINPUT_SOURCE_STYLUS:
8046 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8047 injectMotionEvent(
8048 mDispatcher,
8049 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8050 AINPUT_SOURCE_STYLUS)
8051 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008052 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008053 .x(50)
8054 .y(50))
8055 .build()));
8056 break;
8057 case AINPUT_SOURCE_MOUSE:
8058 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8059 injectMotionEvent(
8060 mDispatcher,
8061 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8062 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8063 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008064 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008065 .x(50)
8066 .y(50))
8067 .build()));
8068 break;
8069 default:
8070 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8071 }
arthurhungb89ccb02020-12-30 16:19:01 +08008072
8073 // Window should receive motion event.
8074 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008075 // Spy window should also receive motion event
8076 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008077 }
8078
8079 // Start performing drag, we will create a drag window and transfer touch to it.
8080 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8081 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008082 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008083 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008084 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008085 }
arthurhungb89ccb02020-12-30 16:19:01 +08008086
8087 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008088 mDragWindow =
8089 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008090 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008091 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008092 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008093
8094 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008095 bool transferred =
8096 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008097 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008098 if (transferred) {
8099 mWindow->consumeMotionCancel();
8100 mDragWindow->consumeMotionDown();
8101 }
8102 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008103 }
8104};
8105
8106TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008107 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008108
8109 // Move on window.
8110 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8111 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8112 ADISPLAY_ID_DEFAULT, {50, 50}))
8113 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8114 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8115 mWindow->consumeDragEvent(false, 50, 50);
8116 mSecondWindow->assertNoEvents();
8117
8118 // Move to another window.
8119 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8120 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8121 ADISPLAY_ID_DEFAULT, {150, 50}))
8122 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8123 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8124 mWindow->consumeDragEvent(true, 150, 50);
8125 mSecondWindow->consumeDragEvent(false, 50, 50);
8126
8127 // Move back to original window.
8128 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8129 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8130 ADISPLAY_ID_DEFAULT, {50, 50}))
8131 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8132 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8133 mWindow->consumeDragEvent(false, 50, 50);
8134 mSecondWindow->consumeDragEvent(true, -50, 50);
8135
8136 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8137 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8138 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8139 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8140 mWindow->assertNoEvents();
8141 mSecondWindow->assertNoEvents();
8142}
8143
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008144TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008145 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008146
8147 // No cancel event after drag start
8148 mSpyWindow->assertNoEvents();
8149
8150 const MotionEvent secondFingerDownEvent =
8151 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8152 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008153 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8154 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008155 .build();
8156 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8157 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8158 InputEventInjectionSync::WAIT_FOR_RESULT))
8159 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8160
8161 // Receives cancel for first pointer after next pointer down
8162 mSpyWindow->consumeMotionCancel();
8163 mSpyWindow->consumeMotionDown();
8164
8165 mSpyWindow->assertNoEvents();
8166}
8167
arthurhungf452d0b2021-01-06 00:19:52 +08008168TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008169 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008170
8171 // Move on window.
8172 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8173 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8174 ADISPLAY_ID_DEFAULT, {50, 50}))
8175 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8176 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8177 mWindow->consumeDragEvent(false, 50, 50);
8178 mSecondWindow->assertNoEvents();
8179
8180 // Move to another window.
8181 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8182 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8183 ADISPLAY_ID_DEFAULT, {150, 50}))
8184 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8185 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8186 mWindow->consumeDragEvent(true, 150, 50);
8187 mSecondWindow->consumeDragEvent(false, 50, 50);
8188
8189 // drop to another window.
8190 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8191 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8192 {150, 50}))
8193 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8194 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8195 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8196 mWindow->assertNoEvents();
8197 mSecondWindow->assertNoEvents();
8198}
8199
arthurhung6d4bed92021-03-17 11:59:33 +08008200TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008201 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008202
8203 // Move on window and keep button pressed.
8204 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8205 injectMotionEvent(mDispatcher,
8206 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8207 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008208 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008209 .build()))
8210 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8211 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8212 mWindow->consumeDragEvent(false, 50, 50);
8213 mSecondWindow->assertNoEvents();
8214
8215 // Move to another window and release button, expect to drop item.
8216 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8217 injectMotionEvent(mDispatcher,
8218 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8219 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008220 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008221 .build()))
8222 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8223 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8224 mWindow->assertNoEvents();
8225 mSecondWindow->assertNoEvents();
8226 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8227
8228 // nothing to the window.
8229 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8230 injectMotionEvent(mDispatcher,
8231 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8232 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008233 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008234 .build()))
8235 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8236 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8237 mWindow->assertNoEvents();
8238 mSecondWindow->assertNoEvents();
8239}
8240
Arthur Hung54745652022-04-20 07:17:41 +00008241TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008242 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008243
8244 // Set second window invisible.
8245 mSecondWindow->setVisible(false);
8246 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8247
8248 // Move on window.
8249 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8250 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8251 ADISPLAY_ID_DEFAULT, {50, 50}))
8252 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8253 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8254 mWindow->consumeDragEvent(false, 50, 50);
8255 mSecondWindow->assertNoEvents();
8256
8257 // Move to another window.
8258 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8259 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8260 ADISPLAY_ID_DEFAULT, {150, 50}))
8261 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8262 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8263 mWindow->consumeDragEvent(true, 150, 50);
8264 mSecondWindow->assertNoEvents();
8265
8266 // drop to another window.
8267 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8268 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8269 {150, 50}))
8270 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8271 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8272 mFakePolicy->assertDropTargetEquals(nullptr);
8273 mWindow->assertNoEvents();
8274 mSecondWindow->assertNoEvents();
8275}
8276
Arthur Hung54745652022-04-20 07:17:41 +00008277TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008278 // Ensure window could track pointerIds if it didn't support split touch.
8279 mWindow->setPreventSplitting(true);
8280
Arthur Hung54745652022-04-20 07:17:41 +00008281 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8282 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8283 {50, 50}))
8284 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8285 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8286
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(50).y(50))
8292 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).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";
Harry Cutts33476232023-01-30 19:57:29 +00008298 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008299
8300 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008301 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008302}
8303
8304TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8305 // First down on second window.
8306 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8307 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8308 {150, 50}))
8309 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8310
8311 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8312
8313 // Second down on first window.
8314 const MotionEvent secondFingerDownEvent =
8315 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8316 .displayId(ADISPLAY_ID_DEFAULT)
8317 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008318 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8319 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008320 .build();
8321 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8322 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8323 InputEventInjectionSync::WAIT_FOR_RESULT))
8324 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8325 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8326
8327 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008328 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008329
8330 // Move on window.
8331 const MotionEvent secondFingerMoveEvent =
8332 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8333 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008334 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8335 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008336 .build();
8337 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8338 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8339 InputEventInjectionSync::WAIT_FOR_RESULT));
8340 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8341 mWindow->consumeDragEvent(false, 50, 50);
8342 mSecondWindow->consumeMotionMove();
8343
8344 // Release the drag pointer should perform drop.
8345 const MotionEvent secondFingerUpEvent =
8346 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8347 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008348 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8349 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008350 .build();
8351 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8352 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8353 InputEventInjectionSync::WAIT_FOR_RESULT));
8354 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8355 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8356 mWindow->assertNoEvents();
8357 mSecondWindow->consumeMotionMove();
8358}
8359
Arthur Hung3915c1f2022-05-31 07:17:17 +00008360TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008361 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008362
8363 // Update window of second display.
8364 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008365 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008366 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8367
8368 // Let second display has a touch state.
8369 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8370 injectMotionEvent(mDispatcher,
8371 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8372 AINPUT_SOURCE_TOUCHSCREEN)
8373 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008374 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008375 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008376 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008377 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008378 // Update window again.
8379 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8380
8381 // Move on window.
8382 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8383 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8384 ADISPLAY_ID_DEFAULT, {50, 50}))
8385 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8386 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8387 mWindow->consumeDragEvent(false, 50, 50);
8388 mSecondWindow->assertNoEvents();
8389
8390 // Move to another window.
8391 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8392 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8393 ADISPLAY_ID_DEFAULT, {150, 50}))
8394 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8395 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8396 mWindow->consumeDragEvent(true, 150, 50);
8397 mSecondWindow->consumeDragEvent(false, 50, 50);
8398
8399 // drop to another window.
8400 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8401 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8402 {150, 50}))
8403 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8404 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8405 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8406 mWindow->assertNoEvents();
8407 mSecondWindow->assertNoEvents();
8408}
8409
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008410TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8411 startDrag(true, AINPUT_SOURCE_MOUSE);
8412 // Move on window.
8413 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8414 injectMotionEvent(mDispatcher,
8415 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8416 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8417 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008418 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008419 .x(50)
8420 .y(50))
8421 .build()))
8422 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8423 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8424 mWindow->consumeDragEvent(false, 50, 50);
8425 mSecondWindow->assertNoEvents();
8426
8427 // Move to another window.
8428 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8429 injectMotionEvent(mDispatcher,
8430 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8431 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8432 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008433 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008434 .x(150)
8435 .y(50))
8436 .build()))
8437 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8438 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8439 mWindow->consumeDragEvent(true, 150, 50);
8440 mSecondWindow->consumeDragEvent(false, 50, 50);
8441
8442 // drop to another window.
8443 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8444 injectMotionEvent(mDispatcher,
8445 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8446 .buttonState(0)
8447 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008448 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008449 .x(150)
8450 .y(50))
8451 .build()))
8452 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8453 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8454 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8455 mWindow->assertNoEvents();
8456 mSecondWindow->assertNoEvents();
8457}
8458
Vishnu Nair062a8672021-09-03 16:07:44 -07008459class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8460
8461TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8462 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008463 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8464 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008465 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008466 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8467 window->setFocusable(true);
8468 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8469 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008470 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008471
8472 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008473 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008474 window->assertNoEvents();
8475
Prabir Pradhan678438e2023-04-13 19:32:51 +00008476 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8477 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008478 window->assertNoEvents();
8479
8480 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008481 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008482 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8483
Prabir Pradhan678438e2023-04-13 19:32:51 +00008484 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008485 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8486
Prabir Pradhan678438e2023-04-13 19:32:51 +00008487 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8488 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008489 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8490 window->assertNoEvents();
8491}
8492
8493TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8494 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8495 std::make_shared<FakeApplicationHandle>();
8496 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008497 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8498 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008499 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8500 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008501 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008502 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008503 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8504 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008505 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008506 window->setOwnerInfo(222, 222);
8507 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8508 window->setFocusable(true);
8509 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8510 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008511 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008512
8513 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008514 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008515 window->assertNoEvents();
8516
Prabir Pradhan678438e2023-04-13 19:32:51 +00008517 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8518 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008519 window->assertNoEvents();
8520
8521 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008522 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008523 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8524
Prabir Pradhan678438e2023-04-13 19:32:51 +00008525 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008526 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8527
Prabir Pradhan678438e2023-04-13 19:32:51 +00008528 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8529 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008530 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8531 window->assertNoEvents();
8532}
8533
8534TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8535 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8536 std::make_shared<FakeApplicationHandle>();
8537 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008538 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8539 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008540 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8541 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008542 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008543 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008544 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8545 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008546 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008547 window->setOwnerInfo(222, 222);
8548 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8549 window->setFocusable(true);
8550 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8551 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008552 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008553
8554 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008555 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008556 window->assertNoEvents();
8557
Prabir Pradhan678438e2023-04-13 19:32:51 +00008558 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8559 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008560 window->assertNoEvents();
8561
8562 // When the window is no longer obscured because it went on top, it should get input
8563 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8564
Prabir Pradhan678438e2023-04-13 19:32:51 +00008565 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008566 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8567
Prabir Pradhan678438e2023-04-13 19:32:51 +00008568 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8569 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008570 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8571 window->assertNoEvents();
8572}
8573
Antonio Kantekf16f2832021-09-28 04:39:20 +00008574class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8575protected:
8576 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008577 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008578 sp<FakeWindowHandle> mWindow;
8579 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008580 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008581
8582 void SetUp() override {
8583 InputDispatcherTest::SetUp();
8584
8585 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008586 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008587 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008588 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008589 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008590 mSecondWindow =
8591 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008592 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008593 mThirdWindow =
8594 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8595 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8596 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008597
8598 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008599 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8600 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8601 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008602 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008603
Antonio Kantek15beb512022-06-13 22:35:41 +00008604 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008605 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008606 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008607 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8608 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008609 mThirdWindow->assertNoEvents();
8610 }
8611
8612 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8613 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008614 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008615 SECOND_DISPLAY_ID)) {
8616 mWindow->assertNoEvents();
8617 mSecondWindow->assertNoEvents();
8618 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008619 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008620 }
8621
Antonio Kantek15beb512022-06-13 22:35:41 +00008622 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8623 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008624 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8625 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008626 mWindow->consumeTouchModeEvent(inTouchMode);
8627 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008628 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008629 }
8630};
8631
Antonio Kantek26defcf2022-02-08 01:12:27 +00008632TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008633 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008634 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8635 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008636 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008637}
8638
Antonio Kantek26defcf2022-02-08 01:12:27 +00008639TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8640 const WindowInfo& windowInfo = *mWindow->getInfo();
8641 int32_t ownerPid = windowInfo.ownerPid;
8642 int32_t ownerUid = windowInfo.ownerUid;
8643 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8644 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008645 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008646 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008647 mWindow->assertNoEvents();
8648 mSecondWindow->assertNoEvents();
8649}
8650
8651TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8652 const WindowInfo& windowInfo = *mWindow->getInfo();
8653 int32_t ownerPid = windowInfo.ownerPid;
8654 int32_t ownerUid = windowInfo.ownerUid;
8655 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008656 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008657 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008658}
8659
Antonio Kantekf16f2832021-09-28 04:39:20 +00008660TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008661 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008662 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8663 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008664 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008665 mWindow->assertNoEvents();
8666 mSecondWindow->assertNoEvents();
8667}
8668
Antonio Kantek15beb512022-06-13 22:35:41 +00008669TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8670 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8671 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8672 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008673 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008674 mWindow->assertNoEvents();
8675 mSecondWindow->assertNoEvents();
8676 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8677}
8678
Antonio Kantek48710e42022-03-24 14:19:30 -07008679TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8680 // Interact with the window first.
8681 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8682 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8683 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8684
8685 // Then remove focus.
8686 mWindow->setFocusable(false);
8687 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8688
8689 // Assert that caller can switch touch mode by owning one of the last interacted window.
8690 const WindowInfo& windowInfo = *mWindow->getInfo();
8691 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8692 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008693 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008694}
8695
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008696class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8697public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008698 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008699 std::shared_ptr<FakeApplicationHandle> application =
8700 std::make_shared<FakeApplicationHandle>();
8701 std::string name = "Fake Spy ";
8702 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008703 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8704 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008705 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008706 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008707 return spy;
8708 }
8709
8710 sp<FakeWindowHandle> createForeground() {
8711 std::shared_ptr<FakeApplicationHandle> application =
8712 std::make_shared<FakeApplicationHandle>();
8713 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008714 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8715 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008716 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008717 return window;
8718 }
8719
8720private:
8721 int mSpyCount{0};
8722};
8723
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008724using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008725/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008726 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8727 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008728TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8729 ScopedSilentDeath _silentDeath;
8730
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008731 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008732 spy->setTrustedOverlay(false);
8733 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8734 ".* not a trusted overlay");
8735}
8736
8737/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008738 * Input injection into a display with a spy window but no foreground windows should succeed.
8739 */
8740TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008741 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008742 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8743
8744 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8745 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8746 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8747 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8748}
8749
8750/**
8751 * Verify the order in which different input windows receive events. The touched foreground window
8752 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8753 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8754 * receive events before ones belows it.
8755 *
8756 * Here, we set up a scenario with four windows in the following Z order from the top:
8757 * spy1, spy2, window, spy3.
8758 * We then inject an event and verify that the foreground "window" receives it first, followed by
8759 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
8760 * window.
8761 */
8762TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
8763 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008764 auto spy1 = createSpy();
8765 auto spy2 = createSpy();
8766 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008767 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
8768 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
8769 const size_t numChannels = channels.size();
8770
Michael Wright8e9a8562022-02-09 13:44:29 +00008771 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008772 if (!epollFd.ok()) {
8773 FAIL() << "Failed to create epoll fd";
8774 }
8775
8776 for (size_t i = 0; i < numChannels; i++) {
8777 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
8778 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
8779 FAIL() << "Failed to add fd to epoll";
8780 }
8781 }
8782
8783 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8784 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8785 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8786
8787 std::vector<size_t> eventOrder;
8788 std::vector<struct epoll_event> events(numChannels);
8789 for (;;) {
8790 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
8791 (100ms).count());
8792 if (nFds < 0) {
8793 FAIL() << "Failed to call epoll_wait";
8794 }
8795 if (nFds == 0) {
8796 break; // epoll_wait timed out
8797 }
8798 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07008799 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07008800 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008801 channels[i]->consumeMotionDown();
8802 }
8803 }
8804
8805 // Verify the order in which the events were received.
8806 EXPECT_EQ(3u, eventOrder.size());
8807 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
8808 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
8809 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
8810}
8811
8812/**
8813 * A spy window using the NOT_TOUCHABLE flag does not receive events.
8814 */
8815TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
8816 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008817 auto spy = createSpy();
8818 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008819 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8820
8821 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8822 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8823 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8824 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8825 spy->assertNoEvents();
8826}
8827
8828/**
8829 * A spy window will only receive gestures that originate within its touchable region. Gestures that
8830 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
8831 * to the window.
8832 */
8833TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
8834 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008835 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008836 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
8837 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8838
8839 // Inject an event outside the spy window's touchable region.
8840 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8841 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8842 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8843 window->consumeMotionDown();
8844 spy->assertNoEvents();
8845 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8846 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8847 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8848 window->consumeMotionUp();
8849 spy->assertNoEvents();
8850
8851 // Inject an event inside the spy window's touchable region.
8852 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8853 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8854 {5, 10}))
8855 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8856 window->consumeMotionDown();
8857 spy->consumeMotionDown();
8858}
8859
8860/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008861 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008862 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008863 */
8864TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
8865 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008866 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008867 auto spy = createSpy();
8868 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008869 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008870 spy->setFrame(Rect{0, 0, 20, 20});
8871 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8872
8873 // Inject an event outside the spy window's frame and touchable region.
8874 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008875 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8876 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008877 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8878 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008879 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008880}
8881
8882/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008883 * Even when a spy window spans over multiple foreground windows, the spy should receive all
8884 * pointers that are down within its bounds.
8885 */
8886TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
8887 auto windowLeft = createForeground();
8888 windowLeft->setFrame({0, 0, 100, 200});
8889 auto windowRight = createForeground();
8890 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008891 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008892 spy->setFrame({0, 0, 200, 200});
8893 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
8894
8895 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8896 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8897 {50, 50}))
8898 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8899 windowLeft->consumeMotionDown();
8900 spy->consumeMotionDown();
8901
8902 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008903 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008904 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008905 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8906 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008907 .build();
8908 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8909 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8910 InputEventInjectionSync::WAIT_FOR_RESULT))
8911 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8912 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00008913 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008914}
8915
8916/**
8917 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
8918 * the spy should receive the second pointer with ACTION_DOWN.
8919 */
8920TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
8921 auto window = createForeground();
8922 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008923 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008924 spyRight->setFrame({100, 0, 200, 200});
8925 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
8926
8927 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8928 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8929 {50, 50}))
8930 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8931 window->consumeMotionDown();
8932 spyRight->assertNoEvents();
8933
8934 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008935 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008936 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008937 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8938 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008939 .build();
8940 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8941 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8942 InputEventInjectionSync::WAIT_FOR_RESULT))
8943 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008944 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008945 spyRight->consumeMotionDown();
8946}
8947
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008948/**
8949 * The spy window should not be able to affect whether or not touches are split. Only the foreground
8950 * windows should be allowed to control split touch.
8951 */
8952TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008953 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008954 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008955 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008956 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008957
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008958 auto window = createForeground();
8959 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008960
8961 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8962
8963 // First finger down, no window touched.
8964 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8965 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8966 {100, 200}))
8967 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8968 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8969 window->assertNoEvents();
8970
8971 // Second finger down on window, the window should receive touch down.
8972 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008973 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008974 .displayId(ADISPLAY_ID_DEFAULT)
8975 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008976 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8977 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008978 .build();
8979 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8980 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8981 InputEventInjectionSync::WAIT_FOR_RESULT))
8982 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8983
8984 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00008985 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008986}
8987
8988/**
8989 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
8990 * do not receive key events.
8991 */
8992TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008993 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008994 spy->setFocusable(false);
8995
8996 auto window = createForeground();
8997 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8998 setFocusedWindow(window);
8999 window->consumeFocusEvent(true);
9000
9001 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
9002 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9003 window->consumeKeyDown(ADISPLAY_ID_NONE);
9004
9005 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
9006 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9007 window->consumeKeyUp(ADISPLAY_ID_NONE);
9008
9009 spy->assertNoEvents();
9010}
9011
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009012using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9013
9014/**
9015 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9016 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9017 */
9018TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9019 auto window = createForeground();
9020 auto spy1 = createSpy();
9021 auto spy2 = createSpy();
9022 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
9023
9024 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9025 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9026 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9027 window->consumeMotionDown();
9028 spy1->consumeMotionDown();
9029 spy2->consumeMotionDown();
9030
9031 // Pilfer pointers from the second spy window.
9032 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9033 spy2->assertNoEvents();
9034 spy1->consumeMotionCancel();
9035 window->consumeMotionCancel();
9036
9037 // The rest of the gesture should only be sent to the second spy window.
9038 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9039 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9040 ADISPLAY_ID_DEFAULT))
9041 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9042 spy2->consumeMotionMove();
9043 spy1->assertNoEvents();
9044 window->assertNoEvents();
9045}
9046
9047/**
9048 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9049 * in the middle of the gesture.
9050 */
9051TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9052 auto window = createForeground();
9053 auto spy = createSpy();
9054 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9055
9056 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9057 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9058 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9059 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9060 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9061
9062 window->releaseChannel();
9063
9064 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9065
9066 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9067 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9068 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9069 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9070}
9071
9072/**
9073 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9074 * the spy, but not to any other windows.
9075 */
9076TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9077 auto spy = createSpy();
9078 auto window = createForeground();
9079
9080 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9081
9082 // First finger down on the window and the spy.
9083 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9084 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9085 {100, 200}))
9086 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9087 spy->consumeMotionDown();
9088 window->consumeMotionDown();
9089
9090 // Spy window pilfers the pointers.
9091 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9092 window->consumeMotionCancel();
9093
9094 // Second finger down on the window and spy, but the window should not receive the pointer down.
9095 const MotionEvent secondFingerDownEvent =
9096 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9097 .displayId(ADISPLAY_ID_DEFAULT)
9098 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009099 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9100 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009101 .build();
9102 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9103 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9104 InputEventInjectionSync::WAIT_FOR_RESULT))
9105 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9106
Harry Cutts33476232023-01-30 19:57:29 +00009107 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009108
9109 // Third finger goes down outside all windows, so injection should fail.
9110 const MotionEvent thirdFingerDownEvent =
9111 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9112 .displayId(ADISPLAY_ID_DEFAULT)
9113 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009114 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9115 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9116 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009117 .build();
9118 ASSERT_EQ(InputEventInjectionResult::FAILED,
9119 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9120 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009121 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009122
9123 spy->assertNoEvents();
9124 window->assertNoEvents();
9125}
9126
9127/**
9128 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9129 */
9130TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9131 auto spy = createSpy();
9132 spy->setFrame(Rect(0, 0, 100, 100));
9133 auto window = createForeground();
9134 window->setFrame(Rect(0, 0, 200, 200));
9135
9136 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9137
9138 // First finger down on the window only
9139 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9140 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9141 {150, 150}))
9142 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9143 window->consumeMotionDown();
9144
9145 // Second finger down on the spy and window
9146 const MotionEvent secondFingerDownEvent =
9147 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9148 .displayId(ADISPLAY_ID_DEFAULT)
9149 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009150 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9151 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009152 .build();
9153 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9154 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9155 InputEventInjectionSync::WAIT_FOR_RESULT))
9156 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9157 spy->consumeMotionDown();
9158 window->consumeMotionPointerDown(1);
9159
9160 // Third finger down on the spy and window
9161 const MotionEvent thirdFingerDownEvent =
9162 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9163 .displayId(ADISPLAY_ID_DEFAULT)
9164 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009165 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9166 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9167 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009168 .build();
9169 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9170 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9171 InputEventInjectionSync::WAIT_FOR_RESULT))
9172 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9173 spy->consumeMotionPointerDown(1);
9174 window->consumeMotionPointerDown(2);
9175
9176 // Spy window pilfers the pointers.
9177 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9178 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9179 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9180
9181 spy->assertNoEvents();
9182 window->assertNoEvents();
9183}
9184
9185/**
9186 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9187 * other windows should be canceled. If this results in the cancellation of all pointers for some
9188 * window, then that window should receive ACTION_CANCEL.
9189 */
9190TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9191 auto spy = createSpy();
9192 spy->setFrame(Rect(0, 0, 100, 100));
9193 auto window = createForeground();
9194 window->setFrame(Rect(0, 0, 200, 200));
9195
9196 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9197
9198 // First finger down on both spy and window
9199 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9200 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9201 {10, 10}))
9202 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9203 window->consumeMotionDown();
9204 spy->consumeMotionDown();
9205
9206 // Second finger down on the spy and window
9207 const MotionEvent secondFingerDownEvent =
9208 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9209 .displayId(ADISPLAY_ID_DEFAULT)
9210 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009211 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9212 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009213 .build();
9214 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9215 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9216 InputEventInjectionSync::WAIT_FOR_RESULT))
9217 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9218 spy->consumeMotionPointerDown(1);
9219 window->consumeMotionPointerDown(1);
9220
9221 // Spy window pilfers the pointers.
9222 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9223 window->consumeMotionCancel();
9224
9225 spy->assertNoEvents();
9226 window->assertNoEvents();
9227}
9228
9229/**
9230 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9231 * be sent to other windows
9232 */
9233TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9234 auto spy = createSpy();
9235 spy->setFrame(Rect(0, 0, 100, 100));
9236 auto window = createForeground();
9237 window->setFrame(Rect(0, 0, 200, 200));
9238
9239 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9240
9241 // First finger down on both window and spy
9242 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9243 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9244 {10, 10}))
9245 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9246 window->consumeMotionDown();
9247 spy->consumeMotionDown();
9248
9249 // Spy window pilfers the pointers.
9250 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9251 window->consumeMotionCancel();
9252
9253 // Second finger down on the window only
9254 const MotionEvent secondFingerDownEvent =
9255 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9256 .displayId(ADISPLAY_ID_DEFAULT)
9257 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009258 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9259 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009260 .build();
9261 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9262 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9263 InputEventInjectionSync::WAIT_FOR_RESULT))
9264 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9265 window->consumeMotionDown();
9266 window->assertNoEvents();
9267
9268 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9269 spy->consumeMotionMove();
9270 spy->assertNoEvents();
9271}
9272
Prabir Pradhand65552b2021-10-07 11:23:50 -07009273class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9274public:
9275 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9276 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9277 std::make_shared<FakeApplicationHandle>();
9278 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009279 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9280 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009281 overlay->setFocusable(false);
9282 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009283 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009284 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009285 overlay->setTrustedOverlay(true);
9286
9287 std::shared_ptr<FakeApplicationHandle> application =
9288 std::make_shared<FakeApplicationHandle>();
9289 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009290 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9291 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009292 window->setFocusable(true);
9293 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009294
9295 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9296 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9297 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009298 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009299 return {std::move(overlay), std::move(window)};
9300 }
9301
9302 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009303 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009304 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009305 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009306 }
9307
9308 void sendStylusEvent(int32_t action) {
9309 NotifyMotionArgs motionArgs =
9310 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9311 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009312 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009313 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009314 }
9315};
9316
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009317using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9318
9319TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9320 ScopedSilentDeath _silentDeath;
9321
Prabir Pradhand65552b2021-10-07 11:23:50 -07009322 auto [overlay, window] = setupStylusOverlayScenario();
9323 overlay->setTrustedOverlay(false);
9324 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9325 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9326 ".* not a trusted overlay");
9327}
9328
9329TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9330 auto [overlay, window] = setupStylusOverlayScenario();
9331 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9332
9333 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9334 overlay->consumeMotionDown();
9335 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9336 overlay->consumeMotionUp();
9337
9338 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9339 window->consumeMotionDown();
9340 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9341 window->consumeMotionUp();
9342
9343 overlay->assertNoEvents();
9344 window->assertNoEvents();
9345}
9346
9347TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9348 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009349 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009350 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9351
9352 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9353 overlay->consumeMotionDown();
9354 window->consumeMotionDown();
9355 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9356 overlay->consumeMotionUp();
9357 window->consumeMotionUp();
9358
9359 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9360 window->consumeMotionDown();
9361 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9362 window->consumeMotionUp();
9363
9364 overlay->assertNoEvents();
9365 window->assertNoEvents();
9366}
9367
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009368/**
9369 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9370 * The scenario is as follows:
9371 * - The stylus interceptor overlay is configured as a spy window.
9372 * - The stylus interceptor spy receives the start of a new stylus gesture.
9373 * - It pilfers pointers and then configures itself to no longer be a spy.
9374 * - The stylus interceptor continues to receive the rest of the gesture.
9375 */
9376TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9377 auto [overlay, window] = setupStylusOverlayScenario();
9378 overlay->setSpy(true);
9379 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9380
9381 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9382 overlay->consumeMotionDown();
9383 window->consumeMotionDown();
9384
9385 // The interceptor pilfers the pointers.
9386 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9387 window->consumeMotionCancel();
9388
9389 // The interceptor configures itself so that it is no longer a spy.
9390 overlay->setSpy(false);
9391 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9392
9393 // It continues to receive the rest of the stylus gesture.
9394 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9395 overlay->consumeMotionMove();
9396 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9397 overlay->consumeMotionUp();
9398
9399 window->assertNoEvents();
9400}
9401
Prabir Pradhan5735a322022-04-11 17:23:34 +00009402struct User {
9403 int32_t mPid;
9404 int32_t mUid;
9405 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9406 std::unique_ptr<InputDispatcher>& mDispatcher;
9407
9408 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9409 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9410
9411 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9412 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9413 ADISPLAY_ID_DEFAULT, {100, 200},
9414 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9415 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9416 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9417 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9418 }
9419
9420 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009421 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009422 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009423 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009424 mPolicyFlags);
9425 }
9426
9427 sp<FakeWindowHandle> createWindow() const {
9428 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9429 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009430 sp<FakeWindowHandle> window =
9431 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9432 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009433 window->setOwnerInfo(mPid, mUid);
9434 return window;
9435 }
9436};
9437
9438using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9439
9440TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9441 auto owner = User(mDispatcher, 10, 11);
9442 auto window = owner.createWindow();
9443 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9444
9445 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9446 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9447 window->consumeMotionDown();
9448
9449 setFocusedWindow(window);
9450 window->consumeFocusEvent(true);
9451
9452 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9453 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9454 window->consumeKeyDown(ADISPLAY_ID_NONE);
9455}
9456
9457TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9458 auto owner = User(mDispatcher, 10, 11);
9459 auto window = owner.createWindow();
9460 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9461
9462 auto rando = User(mDispatcher, 20, 21);
9463 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9464 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9465
9466 setFocusedWindow(window);
9467 window->consumeFocusEvent(true);
9468
9469 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9470 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9471 window->assertNoEvents();
9472}
9473
9474TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9475 auto owner = User(mDispatcher, 10, 11);
9476 auto window = owner.createWindow();
9477 auto spy = owner.createWindow();
9478 spy->setSpy(true);
9479 spy->setTrustedOverlay(true);
9480 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9481
9482 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9483 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9484 spy->consumeMotionDown();
9485 window->consumeMotionDown();
9486}
9487
9488TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9489 auto owner = User(mDispatcher, 10, 11);
9490 auto window = owner.createWindow();
9491
9492 auto rando = User(mDispatcher, 20, 21);
9493 auto randosSpy = rando.createWindow();
9494 randosSpy->setSpy(true);
9495 randosSpy->setTrustedOverlay(true);
9496 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9497
9498 // The event is targeted at owner's window, so injection should succeed, but the spy should
9499 // not receive the event.
9500 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9501 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9502 randosSpy->assertNoEvents();
9503 window->consumeMotionDown();
9504}
9505
9506TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9507 auto owner = User(mDispatcher, 10, 11);
9508 auto window = owner.createWindow();
9509
9510 auto rando = User(mDispatcher, 20, 21);
9511 auto randosSpy = rando.createWindow();
9512 randosSpy->setSpy(true);
9513 randosSpy->setTrustedOverlay(true);
9514 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9515
9516 // A user that has injection permission can inject into any window.
9517 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9518 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9519 ADISPLAY_ID_DEFAULT));
9520 randosSpy->consumeMotionDown();
9521 window->consumeMotionDown();
9522
9523 setFocusedWindow(randosSpy);
9524 randosSpy->consumeFocusEvent(true);
9525
9526 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9527 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9528 window->assertNoEvents();
9529}
9530
9531TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9532 auto owner = User(mDispatcher, 10, 11);
9533 auto window = owner.createWindow();
9534
9535 auto rando = User(mDispatcher, 20, 21);
9536 auto randosWindow = rando.createWindow();
9537 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9538 randosWindow->setWatchOutsideTouch(true);
9539 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9540
9541 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9542 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9543 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9544 window->consumeMotionDown();
9545 randosWindow->consumeMotionOutside();
9546}
9547
Garfield Tane84e6f92019-08-29 17:28:41 -07009548} // namespace android::inputdispatcher