blob: d5062855dd79062e7f1f4e9abfc0301f52d696fd [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Prabir Pradhan9205e422023-05-16 20:06:13 +000018#include "EventBuilders.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080019
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070020#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080021#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080022#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070023#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070024#include <binder/Binder.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000025#include <fcntl.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080026#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080027#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100028#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080030#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100031
Garfield Tan1c7bc862020-01-28 13:24:04 -080032#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080033#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070034#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080035#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080036#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037
Garfield Tan1c7bc862020-01-28 13:24:04 -080038using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050039using android::gui::FocusRequest;
40using android::gui::TouchOcclusionMode;
41using android::gui::WindowInfo;
42using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080043using android::os::InputEventInjectionResult;
44using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080045
Garfield Tane84e6f92019-08-29 17:28:41 -070046namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080047
Dominik Laskowski2f01d772022-03-23 16:01:29 -070048using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080049using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070050
Michael Wrightd02c5b62014-02-10 15:10:22 -080051// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000052static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080053
54// An arbitrary device id.
Prabir Pradhan9205e422023-05-16 20:06:13 +000055static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080056static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080057
Jeff Brownf086ddb2014-02-11 14:28:48 -080058// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000059static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
60static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080061
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080062static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
63static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
64static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
65static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -070066static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080067static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080068static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080069static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080070/**
71 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
72 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
73 * index 0) is the new pointer going down. The same pointer could have been placed at a different
74 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
75 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
76 * pointer id=0 leaves but the pointer id=1 remains.
77 */
78static constexpr int32_t POINTER_0_DOWN =
79 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080080static constexpr int32_t POINTER_1_DOWN =
81 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000082static constexpr int32_t POINTER_2_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000084static constexpr int32_t POINTER_3_DOWN =
85 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000086static constexpr int32_t POINTER_0_UP =
87 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080088static constexpr int32_t POINTER_1_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Harry Cuttsb166c002023-05-09 13:06:05 +000090static constexpr int32_t POINTER_2_UP =
91 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080092
Antonio Kantek15beb512022-06-13 22:35:41 +000093// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000094static constexpr int32_t WINDOW_PID = 999;
95static constexpr int32_t WINDOW_UID = 1001;
96
Antonio Kantek15beb512022-06-13 22:35:41 +000097// The default pid and uid for the windows created on the secondary display by the test.
98static constexpr int32_t SECONDARY_WINDOW_PID = 1010;
99static constexpr int32_t SECONDARY_WINDOW_UID = 1012;
100
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000101// An arbitrary pid of the gesture monitor window
102static constexpr int32_t MONITOR_PID = 2001;
103
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800104static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
105
Arthur Hungc539dbb2022-12-08 07:45:36 +0000106static constexpr int expectedWallpaperFlags =
107 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
108
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800109using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
110
chaviwd1c23182019-12-20 18:44:56 -0800111struct PointF {
112 float x;
113 float y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800114 auto operator<=>(const PointF&) const = default;
chaviwd1c23182019-12-20 18:44:56 -0800115};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
Gang Wang342c9272020-01-13 13:15:04 -0500117/**
118 * Return a DOWN key event with KEYCODE_A.
119 */
120static KeyEvent getTestKeyEvent() {
121 KeyEvent event;
122
Garfield Tanfbe732e2020-01-24 11:26:14 -0800123 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
124 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
125 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500126 return event;
127}
128
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000129static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
130 ASSERT_EQ(expectedAction, receivedAction)
131 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
132 << MotionEvent::actionToString(receivedAction);
133}
134
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800135MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
136 bool matches = action == arg.getAction();
137 if (!matches) {
138 *result_listener << "expected action " << MotionEvent::actionToString(action)
139 << ", but got " << MotionEvent::actionToString(arg.getAction());
140 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800141 if (action == AMOTION_EVENT_ACTION_DOWN) {
142 if (!matches) {
143 *result_listener << "; ";
144 }
145 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
146 matches &= arg.getDownTime() == arg.getEventTime();
147 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800148 if (action == AMOTION_EVENT_ACTION_CANCEL) {
149 if (!matches) {
150 *result_listener << "; ";
151 }
152 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
153 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
154 }
155 return matches;
156}
157
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800158MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
159 return arg.getDownTime() == downTime;
160}
161
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800162MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
163 return arg.getDisplayId() == displayId;
164}
165
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800166MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") {
167 return arg.getDeviceId() == deviceId;
168}
169
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800170MATCHER_P(WithSource, source, "InputEvent with specified source") {
171 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
172 << inputEventSourceToString(arg.getSource());
173 return arg.getSource() == source;
174}
175
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800176MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
177 return arg.getFlags() == flags;
178}
179
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800180MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
181 if (arg.getPointerCount() != 1) {
182 *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
183 return false;
184 }
Harry Cutts33476232023-01-30 19:57:29 +0000185 return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800186}
187
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800188MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") {
189 return arg.getPointerCount() == pointerCount;
190}
191
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800192MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
193 // Build a map for the received pointers, by pointer id
194 std::map<int32_t /*pointerId*/, PointF> actualPointers;
195 for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
196 const int32_t pointerId = arg.getPointerId(pointerIndex);
197 actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
198 }
199 return pointers == actualPointers;
200}
201
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202// --- FakeInputDispatcherPolicy ---
203
204class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
205 InputDispatcherConfiguration mConfig;
206
Prabir Pradhanedd96402022-02-15 01:46:16 -0800207 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
208
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209public:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000210 FakeInputDispatcherPolicy() = default;
211 virtual ~FakeInputDispatcherPolicy() = default;
Jackal Guof9696682018-10-05 12:23:23 +0800212
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800213 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700214 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700215 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700216 EXPECT_EQ(event.getDisplayId(), args.displayId);
217
218 const auto& keyEvent = static_cast<const KeyEvent&>(event);
219 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
220 EXPECT_EQ(keyEvent.getAction(), args.action);
221 });
Jackal Guof9696682018-10-05 12:23:23 +0800222 }
223
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700224 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
225 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700226 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700227 EXPECT_EQ(event.getDisplayId(), args.displayId);
228
229 const auto& motionEvent = static_cast<const MotionEvent&>(event);
230 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
231 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000232 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
233 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
234 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
235 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700236 });
Jackal Guof9696682018-10-05 12:23:23 +0800237 }
238
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700239 void assertFilterInputEventWasNotCalled() {
240 std::scoped_lock lock(mLock);
241 ASSERT_EQ(nullptr, mFilteredEvent);
242 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800244 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700245 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800246 ASSERT_TRUE(mConfigurationChangedTime)
247 << "Timed out waiting for configuration changed call";
248 ASSERT_EQ(*mConfigurationChangedTime, when);
249 mConfigurationChangedTime = std::nullopt;
250 }
251
252 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700253 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800254 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800255 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800256 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
257 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
258 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
259 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
260 mLastNotifySwitch = std::nullopt;
261 }
262
chaviwfd6d3512019-03-25 13:23:49 -0700263 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700264 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800265 ASSERT_EQ(touchedToken, mOnPointerDownToken);
266 mOnPointerDownToken.clear();
267 }
268
269 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700270 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800271 ASSERT_TRUE(mOnPointerDownToken == nullptr)
272 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700273 }
274
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700275 // This function must be called soon after the expected ANR timer starts,
276 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500277 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700278 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500279 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800280 std::unique_lock lock(mLock);
281 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500282 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800283 ASSERT_NO_FATAL_FAILURE(
284 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500285 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700286 }
287
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000288 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800289 const sp<WindowInfoHandle>& window) {
290 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
291 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
292 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500293 }
294
Prabir Pradhanedd96402022-02-15 01:46:16 -0800295 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
296 const sp<IBinder>& expectedToken,
297 int32_t expectedPid) {
298 std::unique_lock lock(mLock);
299 android::base::ScopedLockAssertion assumeLocked(mLock);
300 AnrResult result;
301 ASSERT_NO_FATAL_FAILURE(result =
302 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
303 const auto& [token, pid] = result;
304 ASSERT_EQ(expectedToken, token);
305 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500306 }
307
Prabir Pradhanedd96402022-02-15 01:46:16 -0800308 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000309 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500310 std::unique_lock lock(mLock);
311 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800312 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
313 const auto& [token, _] = result;
314 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000315 }
316
Prabir Pradhanedd96402022-02-15 01:46:16 -0800317 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
318 int32_t expectedPid) {
319 std::unique_lock lock(mLock);
320 android::base::ScopedLockAssertion assumeLocked(mLock);
321 AnrResult result;
322 ASSERT_NO_FATAL_FAILURE(
323 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
324 const auto& [token, pid] = result;
325 ASSERT_EQ(expectedToken, token);
326 ASSERT_EQ(expectedPid, pid);
327 }
328
329 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000330 sp<IBinder> getResponsiveWindowToken() {
331 std::unique_lock lock(mLock);
332 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800333 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
334 const auto& [token, _] = result;
335 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700336 }
337
338 void assertNotifyAnrWasNotCalled() {
339 std::scoped_lock lock(mLock);
340 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800341 ASSERT_TRUE(mAnrWindows.empty());
342 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500343 << "ANR was not called, but please also consume the 'connection is responsive' "
344 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700345 }
346
Garfield Tan1c7bc862020-01-28 13:24:04 -0800347 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
348 mConfig.keyRepeatTimeout = timeout;
349 mConfig.keyRepeatDelay = delay;
350 }
351
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000352 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800353 std::unique_lock lock(mLock);
354 base::ScopedLockAssertion assumeLocked(mLock);
355
356 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
357 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000358 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800359 enabled;
360 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000361 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
362 << ") to be called.";
363 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800364 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000365 auto request = *mPointerCaptureRequest;
366 mPointerCaptureRequest.reset();
367 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800368 }
369
370 void assertSetPointerCaptureNotCalled() {
371 std::unique_lock lock(mLock);
372 base::ScopedLockAssertion assumeLocked(mLock);
373
374 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000375 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800376 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000377 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800378 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000379 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800380 }
381
arthurhungf452d0b2021-01-06 00:19:52 +0800382 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
383 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800384 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800385 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800386 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800387 }
388
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800389 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
390 std::unique_lock lock(mLock);
391 base::ScopedLockAssertion assumeLocked(mLock);
392 std::optional<sp<IBinder>> receivedToken =
393 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
394 mNotifyInputChannelBroken);
395 ASSERT_TRUE(receivedToken.has_value());
396 ASSERT_EQ(token, *receivedToken);
397 }
398
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800399 /**
400 * Set policy timeout. A value of zero means next key will not be intercepted.
401 */
402 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
403 mInterceptKeyTimeout = timeout;
404 }
405
Josep del Riob3981622023-04-18 15:49:45 +0000406 void assertUserActivityPoked() {
407 std::scoped_lock lock(mLock);
408 ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked";
409 }
410
411 void assertUserActivityNotPoked() {
412 std::scoped_lock lock(mLock);
413 ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked";
414 }
415
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700417 std::mutex mLock;
418 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
419 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
420 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
421 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800422
Prabir Pradhan99987712020-11-10 18:43:05 -0800423 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000424
425 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800426
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700427 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700428 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800429 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
430 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700431 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800432 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
433 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700434
arthurhungf452d0b2021-01-06 00:19:52 +0800435 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800436 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Josep del Riob3981622023-04-18 15:49:45 +0000437 bool mPokedUserActivity GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800438
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800439 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
440
Prabir Pradhanedd96402022-02-15 01:46:16 -0800441 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
442 // for a specific container to become non-empty. When the container is non-empty, return the
443 // first entry from the container and erase it.
444 template <class T>
445 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
446 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
447 // If there is an ANR, Dispatcher won't be idle because there are still events
448 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
449 // before checking if ANR was called.
450 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
451 // to provide it some time to act. 100ms seems reasonable.
452 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
453 const std::chrono::time_point start = std::chrono::steady_clock::now();
454 std::optional<T> token =
455 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
456 if (!token.has_value()) {
457 ADD_FAILURE() << "Did not receive the ANR callback";
458 return {};
459 }
460
461 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
462 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
463 // the dispatcher started counting before this function was called
464 if (std::chrono::abs(timeout - waited) > 100ms) {
465 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
466 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
467 << "ms, but waited "
468 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
469 << "ms instead";
470 }
471 return *token;
472 }
473
474 template <class T>
475 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
476 std::queue<T>& storage,
477 std::unique_lock<std::mutex>& lock,
478 std::condition_variable& condition)
479 REQUIRES(mLock) {
480 condition.wait_for(lock, timeout,
481 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
482 if (storage.empty()) {
483 ADD_FAILURE() << "Did not receive the expected callback";
484 return std::nullopt;
485 }
486 T item = storage.front();
487 storage.pop();
488 return std::make_optional(item);
489 }
490
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600491 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700492 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800493 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 }
495
Prabir Pradhanedd96402022-02-15 01:46:16 -0800496 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
497 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700498 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800499 ASSERT_TRUE(pid.has_value());
500 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700501 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500502 }
503
Prabir Pradhanedd96402022-02-15 01:46:16 -0800504 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
505 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500506 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800507 ASSERT_TRUE(pid.has_value());
508 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500509 mNotifyAnr.notify_all();
510 }
511
512 void notifyNoFocusedWindowAnr(
513 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
514 std::scoped_lock lock(mLock);
515 mAnrApplications.push(applicationHandle);
516 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517 }
518
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800519 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
520 std::scoped_lock lock(mLock);
521 mBrokenInputChannels.push(connectionToken);
522 mNotifyInputChannelBroken.notify_all();
523 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600525 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700526
Chris Yef59a2f42020-10-16 12:55:26 -0700527 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
528 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
529 const std::vector<float>& values) override {}
530
531 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
532 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000533
Chris Yefb552902021-02-03 17:18:37 -0800534 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
535
Prabir Pradhana41d2442023-04-20 21:30:40 +0000536 InputDispatcherConfiguration getDispatcherConfiguration() override { return mConfig; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537
Prabir Pradhana41d2442023-04-20 21:30:40 +0000538 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700539 std::scoped_lock lock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +0000540 switch (inputEvent.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700541 case InputEventType::KEY: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000542 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent);
543 mFilteredEvent = std::make_unique<KeyEvent>(keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800544 break;
545 }
546
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700547 case InputEventType::MOTION: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000548 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent);
549 mFilteredEvent = std::make_unique<MotionEvent>(motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800550 break;
551 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700552 default: {
553 ADD_FAILURE() << "Should only filter keys or motions";
554 break;
555 }
Jackal Guof9696682018-10-05 12:23:23 +0800556 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 return true;
558 }
559
Prabir Pradhana41d2442023-04-20 21:30:40 +0000560 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override {
561 if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800562 // Clear intercept state when we handled the event.
563 mInterceptKeyTimeout = 0ms;
564 }
565 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600567 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568
Prabir Pradhana41d2442023-04-20 21:30:40 +0000569 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800570 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
571 // Clear intercept state so we could dispatch the event in next wake.
572 mInterceptKeyTimeout = 0ms;
573 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 }
575
Prabir Pradhana41d2442023-04-20 21:30:40 +0000576 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&,
577 uint32_t) override {
578 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 }
580
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600581 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
582 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700583 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800584 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
585 * essentially a passthrough for notifySwitch.
586 */
Harry Cutts33476232023-01-30 19:57:29 +0000587 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 }
589
Josep del Riob3981622023-04-18 15:49:45 +0000590 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {
591 std::scoped_lock lock(mLock);
592 mPokedUserActivity = true;
593 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600595 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700596 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700597 mOnPointerDownToken = newToken;
598 }
599
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000600 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800601 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000602 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800603 mPointerCaptureChangedCondition.notify_all();
604 }
605
arthurhungf452d0b2021-01-06 00:19:52 +0800606 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
607 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800608 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800609 mDropTargetWindowToken = token;
610 }
611
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700612 void assertFilterInputEventWasCalledInternal(
613 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700614 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800615 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700616 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800617 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800618 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619};
620
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621// --- InputDispatcherTest ---
622
623class InputDispatcherTest : public testing::Test {
624protected:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000625 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700626 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000628 void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000629 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
630 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800631 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000632 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700633 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634 }
635
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000636 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700637 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000638 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700639 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700641
642 /**
643 * Used for debugging when writing the test
644 */
645 void dumpDispatcherState() {
646 std::string dump;
647 mDispatcher->dump(dump);
648 std::stringstream ss(dump);
649 std::string to;
650
651 while (std::getline(ss, to, '\n')) {
652 ALOGE("%s", to.c_str());
653 }
654 }
Vishnu Nair958da932020-08-21 17:12:37 -0700655
Chavi Weingarten847e8512023-03-29 00:26:09 +0000656 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700657 FocusRequest request;
658 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000659 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700660 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
661 request.displayId = window->getInfo()->displayId;
662 mDispatcher->setFocusedWindow(request);
663 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664};
665
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
667 KeyEvent event;
668
669 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800670 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
671 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600672 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
673 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800674 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000675 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000676 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677 << "Should reject key events with undefined action.";
678
679 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800680 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
681 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600682 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800683 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000684 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000685 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686 << "Should reject key events with ACTION_MULTIPLE.";
687}
688
689TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
690 MotionEvent event;
691 PointerProperties pointerProperties[MAX_POINTERS + 1];
692 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800693 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694 pointerProperties[i].clear();
695 pointerProperties[i].id = i;
696 pointerCoords[i].clear();
697 }
698
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800699 // Some constants commonly used below
700 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
701 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
702 constexpr int32_t metaState = AMETA_NONE;
703 constexpr MotionClassification classification = MotionClassification::NONE;
704
chaviw9eaa22c2020-07-01 16:21:27 -0700705 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800707 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700708 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
709 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700710 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
711 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700712 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800713 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000714 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000715 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 << "Should reject motion events with undefined action.";
717
718 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800719 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800720 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
721 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
722 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
723 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500724 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800725 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000726 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000727 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728 << "Should reject motion events with pointer down index too large.";
729
Garfield Tanfbe732e2020-01-24 11:26:14 -0800730 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700731 AMOTION_EVENT_ACTION_POINTER_DOWN |
732 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700733 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
734 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700735 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500736 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800737 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000738 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000739 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740 << "Should reject motion events with pointer down index too small.";
741
742 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800743 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800744 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
745 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
746 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
747 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500748 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800749 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000750 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000751 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 << "Should reject motion events with pointer up index too large.";
753
Garfield Tanfbe732e2020-01-24 11:26:14 -0800754 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700755 AMOTION_EVENT_ACTION_POINTER_UP |
756 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700757 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
758 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700759 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500760 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800761 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000762 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000763 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764 << "Should reject motion events with pointer up index too small.";
765
766 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800767 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
768 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700769 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700770 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
771 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700772 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800773 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000774 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000775 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800776 << "Should reject motion events with 0 pointers.";
777
Garfield Tanfbe732e2020-01-24 11:26:14 -0800778 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
779 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700780 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700781 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
782 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700783 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800784 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000785 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000786 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787 << "Should reject motion events with more than MAX_POINTERS pointers.";
788
789 // Rejects motion events with invalid pointer ids.
790 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800791 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
792 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700793 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700794 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
795 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700796 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800797 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000798 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000799 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 << "Should reject motion events with pointer ids less than 0.";
801
802 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800803 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
804 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700805 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700806 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
807 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700808 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800809 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000810 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000811 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800812 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
813
814 // Rejects motion events with duplicate pointer ids.
815 pointerProperties[0].id = 1;
816 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800817 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
818 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700819 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700820 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
821 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700822 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800823 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000824 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000825 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826 << "Should reject motion events with duplicate pointer ids.";
827}
828
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800829/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
830
831TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
832 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000833 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800834 ASSERT_TRUE(mDispatcher->waitForIdle());
835
836 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
837}
838
839TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000840 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
841 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000842 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800843
844 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
845 args.policyFlags |= POLICY_FLAG_TRUSTED;
846 mFakePolicy->assertNotifySwitchWasCalled(args);
847}
848
Arthur Hungb92218b2018-08-14 12:00:21 +0800849// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700850static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700851// Default input dispatching timeout if there is no focused application or paused window
852// from which to determine an appropriate dispatching timeout.
853static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
854 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
855 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800856
857class FakeApplicationHandle : public InputApplicationHandle {
858public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700859 FakeApplicationHandle() {
860 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700861 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500862 mInfo.dispatchingTimeoutMillis =
863 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700864 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800865 virtual ~FakeApplicationHandle() {}
866
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000867 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700868
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500869 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
870 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700871 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800872};
873
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800874class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800875public:
Garfield Tan15601662020-09-22 15:32:38 -0700876 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800877 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700878 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800879 }
880
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800881 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700882 InputEvent* event;
883 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
884 if (!consumeSeq) {
885 return nullptr;
886 }
887 finishEvent(*consumeSeq);
888 return event;
889 }
890
891 /**
892 * Receive an event without acknowledging it.
893 * Return the sequence number that could later be used to send finished signal.
894 */
895 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800896 uint32_t consumeSeq;
897 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800898
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800899 std::chrono::time_point start = std::chrono::steady_clock::now();
900 status_t status = WOULD_BLOCK;
901 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000902 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800903 &event);
904 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
905 if (elapsed > 100ms) {
906 break;
907 }
908 }
909
910 if (status == WOULD_BLOCK) {
911 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700912 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800913 }
914
915 if (status != OK) {
916 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700917 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800918 }
919 if (event == nullptr) {
920 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700921 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800922 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700923 if (outEvent != nullptr) {
924 *outEvent = event;
925 }
926 return consumeSeq;
927 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800928
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700929 /**
930 * To be used together with "receiveEvent" to complete the consumption of an event.
931 */
932 void finishEvent(uint32_t consumeSeq) {
933 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
934 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800935 }
936
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000937 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
938 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
939 ASSERT_EQ(OK, status);
940 }
941
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700942 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000943 std::optional<int32_t> expectedDisplayId,
944 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800945 InputEvent* event = consume();
946
947 ASSERT_NE(nullptr, event) << mName.c_str()
948 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800949 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700950 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
951 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800952
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000953 if (expectedDisplayId.has_value()) {
954 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
955 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800956
Tiger Huang8664f8c2018-10-11 19:14:35 +0800957 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700958 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800959 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
960 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000961 if (expectedFlags.has_value()) {
962 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
963 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800964 break;
965 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700966 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800967 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000968 assertMotionAction(expectedAction, motionEvent.getAction());
969
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000970 if (expectedFlags.has_value()) {
971 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
972 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800973 break;
974 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700975 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100976 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
977 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700978 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -0800979 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
980 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700981 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +0000982 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
983 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700984 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +0800985 FAIL() << "Use 'consumeDragEvent' for DRAG events";
986 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800987 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800988 }
989
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800990 MotionEvent* consumeMotion() {
991 InputEvent* event = consume();
992
993 if (event == nullptr) {
994 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
995 return nullptr;
996 }
997
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700998 if (event->getType() != InputEventType::MOTION) {
999 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001000 return nullptr;
1001 }
1002 return static_cast<MotionEvent*>(event);
1003 }
1004
1005 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1006 MotionEvent* motionEvent = consumeMotion();
1007 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1008 ASSERT_THAT(*motionEvent, matcher);
1009 }
1010
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001011 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
1012 InputEvent* event = consume();
1013 ASSERT_NE(nullptr, event) << mName.c_str()
1014 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001015 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1016 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001017
1018 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1019 << mName.c_str() << ": event displayId should always be NONE.";
1020
1021 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1022 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001023 }
1024
Prabir Pradhan99987712020-11-10 18:43:05 -08001025 void consumeCaptureEvent(bool hasCapture) {
1026 const InputEvent* event = consume();
1027 ASSERT_NE(nullptr, event) << mName.c_str()
1028 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001029 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1030 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001031
1032 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1033 << mName.c_str() << ": event displayId should always be NONE.";
1034
1035 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1036 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1037 }
1038
arthurhungb89ccb02020-12-30 16:19:01 +08001039 void consumeDragEvent(bool isExiting, float x, float y) {
1040 const InputEvent* event = consume();
1041 ASSERT_NE(nullptr, event) << mName.c_str()
1042 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001043 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001044
1045 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1046 << mName.c_str() << ": event displayId should always be NONE.";
1047
1048 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1049 EXPECT_EQ(isExiting, dragEvent.isExiting());
1050 EXPECT_EQ(x, dragEvent.getX());
1051 EXPECT_EQ(y, dragEvent.getY());
1052 }
1053
Antonio Kantekf16f2832021-09-28 04:39:20 +00001054 void consumeTouchModeEvent(bool inTouchMode) {
1055 const InputEvent* event = consume();
1056 ASSERT_NE(nullptr, event) << mName.c_str()
1057 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001058 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1059 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001060
1061 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1062 << mName.c_str() << ": event displayId should always be NONE.";
1063 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1064 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1065 }
1066
chaviwd1c23182019-12-20 18:44:56 -08001067 void assertNoEvents() {
1068 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001069 if (event == nullptr) {
1070 return;
1071 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001072 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001073 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1074 ADD_FAILURE() << "Received key event "
1075 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001076 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001077 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1078 ADD_FAILURE() << "Received motion event "
1079 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001080 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001081 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1082 ADD_FAILURE() << "Received focus event, hasFocus = "
1083 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001084 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001085 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1086 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1087 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001088 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001089 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1090 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1091 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001092 }
1093 FAIL() << mName.c_str()
1094 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001095 }
1096
1097 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1098
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001099 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1100
chaviwd1c23182019-12-20 18:44:56 -08001101protected:
1102 std::unique_ptr<InputConsumer> mConsumer;
1103 PreallocatedInputEventFactory mEventFactory;
1104
1105 std::string mName;
1106};
1107
chaviw3277faf2021-05-19 16:45:23 -05001108class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001109public:
1110 static const int32_t WIDTH = 600;
1111 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001112
Chris Yea209fde2020-07-22 13:54:51 -07001113 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001114 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001115 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001116 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001117 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001118 base::Result<std::unique_ptr<InputChannel>> channel =
1119 dispatcher->createInputChannel(name);
1120 token = (*channel)->getConnectionToken();
1121 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001122 }
1123
1124 inputApplicationHandle->updateInfo();
1125 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1126
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001127 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001128 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001129 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001130 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001131 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001132 mInfo.frameLeft = 0;
1133 mInfo.frameTop = 0;
1134 mInfo.frameRight = WIDTH;
1135 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001136 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001137 mInfo.globalScaleFactor = 1.0;
1138 mInfo.touchableRegion.clear();
1139 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001140 mInfo.ownerPid = WINDOW_PID;
1141 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001142 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001143 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001144 }
1145
Arthur Hungabbb9d82021-09-01 14:52:30 +00001146 sp<FakeWindowHandle> clone(
1147 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001148 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001149 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001150 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1151 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001152 return handle;
1153 }
1154
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001155 void setTouchable(bool touchable) {
1156 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1157 }
chaviwd1c23182019-12-20 18:44:56 -08001158
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001159 void setFocusable(bool focusable) {
1160 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1161 }
1162
1163 void setVisible(bool visible) {
1164 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1165 }
Vishnu Nair958da932020-08-21 17:12:37 -07001166
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001167 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001168 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001169 }
1170
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001171 void setPaused(bool paused) {
1172 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1173 }
1174
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001175 void setPreventSplitting(bool preventSplitting) {
1176 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001177 }
1178
1179 void setSlippery(bool slippery) {
1180 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1181 }
1182
1183 void setWatchOutsideTouch(bool watchOutside) {
1184 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1185 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001186
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001187 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1188
1189 void setInterceptsStylus(bool interceptsStylus) {
1190 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1191 }
1192
1193 void setDropInput(bool dropInput) {
1194 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1195 }
1196
1197 void setDropInputIfObscured(bool dropInputIfObscured) {
1198 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1199 }
1200
1201 void setNoInputChannel(bool noInputChannel) {
1202 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1203 }
1204
Josep del Riob3981622023-04-18 15:49:45 +00001205 void setDisableUserActivity(bool disableUserActivity) {
1206 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1207 }
1208
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001209 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1210
chaviw3277faf2021-05-19 16:45:23 -05001211 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001212
Bernardo Rufino7393d172021-02-26 13:56:11 +00001213 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1214
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001215 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001216 mInfo.frameLeft = frame.left;
1217 mInfo.frameTop = frame.top;
1218 mInfo.frameRight = frame.right;
1219 mInfo.frameBottom = frame.bottom;
1220 mInfo.touchableRegion.clear();
1221 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001222
1223 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1224 ui::Transform translate;
1225 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1226 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001227 }
1228
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001229 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1230
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001231 void setIsWallpaper(bool isWallpaper) {
1232 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1233 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001234
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001235 void setDupTouchToWallpaper(bool hasWallpaper) {
1236 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1237 }
chaviwd1c23182019-12-20 18:44:56 -08001238
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001239 void setTrustedOverlay(bool trustedOverlay) {
1240 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1241 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001242
chaviw9eaa22c2020-07-01 16:21:27 -07001243 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1244 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1245 }
1246
1247 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001248
yunho.shinf4a80b82020-11-16 21:13:57 +09001249 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1250
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001251 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001252 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001253 }
1254
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001255 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001256 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001257 }
1258
Svet Ganov5d3bc372020-01-26 23:11:07 -08001259 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001260 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001261 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1262 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001263 }
1264
1265 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001266 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001267 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1268 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001269 }
1270
1271 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001272 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001273 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1274 }
1275
1276 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1277 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001278 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001279 expectedFlags);
1280 }
1281
Svet Ganov5d3bc372020-01-26 23:11:07 -08001282 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001283 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1284 int32_t expectedFlags = 0) {
1285 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1286 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001287 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001288 }
1289
1290 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001291 int32_t expectedFlags = 0) {
1292 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1293 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001294 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001295 }
1296
1297 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001298 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001299 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001300 expectedFlags);
1301 }
1302
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001303 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1304 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001305 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001306 expectedFlags);
1307 }
1308
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001309 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1310 int32_t expectedFlags = 0) {
1311 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001312 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001313 ASSERT_EQ(InputEventType::MOTION, event->getType());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001314 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1315 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1316 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1317 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1318 }
1319
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001320 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1321 ASSERT_NE(mInputReceiver, nullptr)
1322 << "Cannot consume events from a window with no receiver";
1323 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1324 }
1325
Prabir Pradhan99987712020-11-10 18:43:05 -08001326 void consumeCaptureEvent(bool hasCapture) {
1327 ASSERT_NE(mInputReceiver, nullptr)
1328 << "Cannot consume events from a window with no receiver";
1329 mInputReceiver->consumeCaptureEvent(hasCapture);
1330 }
1331
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001332 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1333 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001334 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001335 ASSERT_THAT(*motionEvent, matcher);
1336 }
1337
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001338 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001339 std::optional<int32_t> expectedDisplayId,
1340 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001341 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1342 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1343 expectedFlags);
1344 }
1345
arthurhungb89ccb02020-12-30 16:19:01 +08001346 void consumeDragEvent(bool isExiting, float x, float y) {
1347 mInputReceiver->consumeDragEvent(isExiting, x, y);
1348 }
1349
Antonio Kantekf16f2832021-09-28 04:39:20 +00001350 void consumeTouchModeEvent(bool inTouchMode) {
1351 ASSERT_NE(mInputReceiver, nullptr)
1352 << "Cannot consume events from a window with no receiver";
1353 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1354 }
1355
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001356 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001357 if (mInputReceiver == nullptr) {
1358 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1359 return std::nullopt;
1360 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001361 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001362 }
1363
1364 void finishEvent(uint32_t sequenceNum) {
1365 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1366 mInputReceiver->finishEvent(sequenceNum);
1367 }
1368
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001369 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1370 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1371 mInputReceiver->sendTimeline(inputEventId, timeline);
1372 }
1373
chaviwaf87b3e2019-10-01 16:59:28 -07001374 InputEvent* consume() {
1375 if (mInputReceiver == nullptr) {
1376 return nullptr;
1377 }
1378 return mInputReceiver->consume();
1379 }
1380
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001381 MotionEvent* consumeMotion() {
1382 InputEvent* event = consume();
1383 if (event == nullptr) {
1384 ADD_FAILURE() << "Consume failed : no event";
1385 return nullptr;
1386 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001387 if (event->getType() != InputEventType::MOTION) {
1388 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001389 return nullptr;
1390 }
1391 return static_cast<MotionEvent*>(event);
1392 }
1393
Arthur Hungb92218b2018-08-14 12:00:21 +08001394 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001395 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001396 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001397 return; // Can't receive events if the window does not have input channel
1398 }
1399 ASSERT_NE(nullptr, mInputReceiver)
1400 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001401 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001402 }
1403
chaviwaf87b3e2019-10-01 16:59:28 -07001404 sp<IBinder> getToken() { return mInfo.token; }
1405
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001406 const std::string& getName() { return mName; }
1407
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001408 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1409 mInfo.ownerPid = ownerPid;
1410 mInfo.ownerUid = ownerUid;
1411 }
1412
Prabir Pradhanedd96402022-02-15 01:46:16 -08001413 int32_t getPid() const { return mInfo.ownerPid; }
1414
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001415 void destroyReceiver() { mInputReceiver = nullptr; }
1416
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001417 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1418
chaviwd1c23182019-12-20 18:44:56 -08001419private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001420 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001421 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001422 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001423};
1424
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001425std::atomic<int32_t> FakeWindowHandle::sId{1};
1426
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001427static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001428 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001429 int32_t displayId = ADISPLAY_ID_NONE,
1430 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001431 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001432 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1433 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001434 KeyEvent event;
1435 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1436
1437 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001438 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001439 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1440 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001441
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001442 if (!allowKeyRepeat) {
1443 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1444 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001445 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001446 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001447}
1448
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001449static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001450 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001451 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001452}
1453
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001454// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1455// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1456// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001457static InputEventInjectionResult injectKeyDownNoRepeat(
1458 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001459 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001460 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001461 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001462}
1463
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001464static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001465 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001466 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001467}
1468
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001469static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001470 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001471 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001472 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1473 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1474 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1475 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001476}
1477
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001478static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001479 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001480 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001481 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001482 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1483 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001484 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001485 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1486 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001487 MotionEventBuilder motionBuilder =
1488 MotionEventBuilder(action, source)
1489 .displayId(displayId)
1490 .eventTime(eventTime)
1491 .rawXCursorPosition(cursorPosition.x)
1492 .rawYCursorPosition(cursorPosition.y)
1493 .pointer(
1494 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1495 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1496 motionBuilder.downTime(eventTime);
1497 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001498
1499 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001500 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1501 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001502}
1503
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001504static InputEventInjectionResult injectMotionDown(
1505 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1506 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001507 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001508}
1509
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001510static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001511 int32_t source, int32_t displayId,
1512 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001513 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001514}
1515
Jackal Guof9696682018-10-05 12:23:23 +08001516static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1517 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1518 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001519 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001520 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1521 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001522
1523 return args;
1524}
1525
Josep del Riob3981622023-04-18 15:49:45 +00001526static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1527 int32_t displayId = ADISPLAY_ID_NONE) {
1528 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1529 // Define a valid key event.
1530 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1531 displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON,
1532 currentTime);
1533
1534 return args;
1535}
1536
1537static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1538 int32_t displayId = ADISPLAY_ID_NONE) {
1539 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1540 // Define a valid key event.
1541 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1542 displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT,
1543 AMETA_NONE, currentTime);
1544
1545 return args;
1546}
1547
Prabir Pradhan678438e2023-04-13 19:32:51 +00001548[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1549 int32_t displayId,
1550 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001551 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001552 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1553 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1554 }
1555
chaviwd1c23182019-12-20 18:44:56 -08001556 PointerProperties pointerProperties[pointerCount];
1557 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001558
chaviwd1c23182019-12-20 18:44:56 -08001559 for (size_t i = 0; i < pointerCount; i++) {
1560 pointerProperties[i].clear();
1561 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001562 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001563
chaviwd1c23182019-12-20 18:44:56 -08001564 pointerCoords[i].clear();
1565 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1566 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1567 }
Jackal Guof9696682018-10-05 12:23:23 +08001568
1569 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1570 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001571 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001572 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1573 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001574 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1575 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001576 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1577 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001578
1579 return args;
1580}
1581
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001582static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1583 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1584}
1585
chaviwd1c23182019-12-20 18:44:56 -08001586static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1587 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1588}
1589
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001590static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1591 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001592 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001593}
1594
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001595/**
1596 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1597 * broken channel.
1598 */
1599TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1600 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1601 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001602 sp<FakeWindowHandle>::make(application, mDispatcher,
1603 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001604
1605 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1606
1607 // Window closes its channel, but the window remains.
1608 window->destroyReceiver();
1609 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1610}
1611
Arthur Hungb92218b2018-08-14 12:00:21 +08001612TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001613 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001614 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1615 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001616
Arthur Hung72d8dc32020-03-28 00:48:39 +00001617 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001618 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1619 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1620 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001621
1622 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001623 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001624}
1625
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001626TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1627 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001628 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1629 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001630
1631 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1632 // Inject a MotionEvent to an unknown display.
1633 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1634 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1635 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1636
1637 // Window should receive motion event.
1638 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1639}
1640
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001641/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001642 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001643 * This test serves as a sanity check for the next test, where setInputWindows is
1644 * called twice.
1645 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001646TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001647 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001648 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1649 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001650 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001651
1652 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001653 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001654 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1655 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001656 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001657
1658 // Window should receive motion event.
1659 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1660}
1661
1662/**
1663 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001664 */
1665TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001666 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001667 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1668 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001669 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001670
1671 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1672 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001673 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001674 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1675 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001676 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001677
1678 // Window should receive motion event.
1679 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1680}
1681
Arthur Hungb92218b2018-08-14 12:00:21 +08001682// The foreground window should receive the first touch down event.
1683TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001684 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001685 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001686 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001687 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001688 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001689
Arthur Hung72d8dc32020-03-28 00:48:39 +00001690 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001691 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1692 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1693 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001694
1695 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001696 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001697 windowSecond->assertNoEvents();
1698}
1699
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001700/**
1701 * Two windows: A top window, and a wallpaper behind the window.
1702 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1703 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001704 * 1. foregroundWindow <-- dup touch to wallpaper
1705 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001706 */
1707TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1708 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1709 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001710 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001711 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001712 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001713 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001714 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001715
1716 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1717 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1718 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1719 {100, 200}))
1720 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1721
1722 // Both foreground window and its wallpaper should receive the touch down
1723 foregroundWindow->consumeMotionDown();
1724 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1725
1726 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1727 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1728 ADISPLAY_ID_DEFAULT, {110, 200}))
1729 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1730
1731 foregroundWindow->consumeMotionMove();
1732 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1733
1734 // Now the foreground window goes away, but the wallpaper stays
1735 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1736 foregroundWindow->consumeMotionCancel();
1737 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1738 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1739}
1740
1741/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001742 * Two fingers down on the window, and lift off the first finger.
1743 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1744 * contains a single pointer.
1745 */
1746TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1747 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1748 sp<FakeWindowHandle> window =
1749 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1750
1751 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001752 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001753 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1754 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1755 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001756 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001757 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1758 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1759 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1760 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001761 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001762 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1763 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1764 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1765 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001766 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1767 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1768 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1769
1770 // Remove the window. The gesture should be canceled
1771 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
1772 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1773 window->consumeMotionEvent(
1774 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1775}
1776
1777/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001778 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1779 * with the following differences:
1780 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1781 * clean up the connection.
1782 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1783 * Ensure that there's no crash in the dispatcher.
1784 */
1785TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1786 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1787 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001788 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001789 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001790 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001791 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001792 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001793
1794 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1795 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1796 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1797 {100, 200}))
1798 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1799
1800 // Both foreground window and its wallpaper should receive the touch down
1801 foregroundWindow->consumeMotionDown();
1802 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1803
1804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1805 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1806 ADISPLAY_ID_DEFAULT, {110, 200}))
1807 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1808
1809 foregroundWindow->consumeMotionMove();
1810 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1811
1812 // Wallpaper closes its channel, but the window remains.
1813 wallpaperWindow->destroyReceiver();
1814 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1815
1816 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1817 // is no longer valid.
1818 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1819 foregroundWindow->consumeMotionCancel();
1820}
1821
Arthur Hungc539dbb2022-12-08 07:45:36 +00001822class ShouldSplitTouchFixture : public InputDispatcherTest,
1823 public ::testing::WithParamInterface<bool> {};
1824INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1825 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001826/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001827 * A single window that receives touch (on top), and a wallpaper window underneath it.
1828 * The top window gets a multitouch gesture.
1829 * Ensure that wallpaper gets the same gesture.
1830 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00001831TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001832 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001833 sp<FakeWindowHandle> foregroundWindow =
1834 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1835 foregroundWindow->setDupTouchToWallpaper(true);
1836 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001837
1838 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001839 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001840 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001841
Arthur Hungc539dbb2022-12-08 07:45:36 +00001842 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001843
1844 // Touch down on top window
1845 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1846 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1847 {100, 100}))
1848 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1849
1850 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00001851 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001852 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1853
1854 // Second finger down on the top window
1855 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001856 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001857 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001858 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1859 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001860 .build();
1861 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1862 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1863 InputEventInjectionSync::WAIT_FOR_RESULT))
1864 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1865
Harry Cutts33476232023-01-30 19:57:29 +00001866 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
1867 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001868 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001869
1870 const MotionEvent secondFingerUpEvent =
1871 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1872 .displayId(ADISPLAY_ID_DEFAULT)
1873 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001874 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1875 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001876 .build();
1877 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1878 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
1879 InputEventInjectionSync::WAIT_FOR_RESULT))
1880 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1881 foregroundWindow->consumeMotionPointerUp(0);
1882 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1883
1884 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001885 injectMotionEvent(mDispatcher,
1886 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
1887 AINPUT_SOURCE_TOUCHSCREEN)
1888 .displayId(ADISPLAY_ID_DEFAULT)
1889 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1890 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001891 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001892 .x(100)
1893 .y(100))
1894 .build(),
1895 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001896 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1897 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1898 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001899}
1900
1901/**
1902 * Two windows: a window on the left and window on the right.
1903 * A third window, wallpaper, is behind both windows, and spans both top windows.
1904 * The first touch down goes to the left window. A second pointer touches down on the right window.
1905 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1906 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1907 * ACTION_POINTER_DOWN(1).
1908 */
1909TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1910 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1911 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001912 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001913 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001914 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001915
1916 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001917 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001918 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001919 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001920
1921 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001922 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001923 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001924 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001925
1926 mDispatcher->setInputWindows(
1927 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
1928
1929 // Touch down on left window
1930 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1931 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1932 {100, 100}))
1933 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1934
1935 // Both foreground window and its wallpaper should receive the touch down
1936 leftWindow->consumeMotionDown();
1937 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1938
1939 // Second finger down on the right window
1940 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001941 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001942 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001943 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1944 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001945 .build();
1946 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1947 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1948 InputEventInjectionSync::WAIT_FOR_RESULT))
1949 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1950
1951 leftWindow->consumeMotionMove();
1952 // Since the touch is split, right window gets ACTION_DOWN
1953 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00001954 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001955 expectedWallpaperFlags);
1956
1957 // Now, leftWindow, which received the first finger, disappears.
1958 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
1959 leftWindow->consumeMotionCancel();
1960 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1961 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1962
1963 // The pointer that's still down on the right window moves, and goes to the right window only.
1964 // As far as the dispatcher's concerned though, both pointers are still present.
1965 const MotionEvent secondFingerMoveEvent =
1966 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1967 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001968 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1969 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001970 .build();
1971 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1972 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
1973 InputEventInjectionSync::WAIT_FOR_RESULT));
1974 rightWindow->consumeMotionMove();
1975
1976 leftWindow->assertNoEvents();
1977 rightWindow->assertNoEvents();
1978 wallpaperWindow->assertNoEvents();
1979}
1980
Arthur Hungc539dbb2022-12-08 07:45:36 +00001981/**
1982 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
1983 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
1984 * The right window should receive ACTION_DOWN.
1985 */
1986TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00001987 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001988 sp<FakeWindowHandle> leftWindow =
1989 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1990 leftWindow->setFrame(Rect(0, 0, 200, 200));
1991 leftWindow->setDupTouchToWallpaper(true);
1992 leftWindow->setSlippery(true);
1993
1994 sp<FakeWindowHandle> rightWindow =
1995 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1996 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00001997
1998 sp<FakeWindowHandle> wallpaperWindow =
1999 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2000 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002001
Arthur Hungc539dbb2022-12-08 07:45:36 +00002002 mDispatcher->setInputWindows(
2003 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002004
Arthur Hungc539dbb2022-12-08 07:45:36 +00002005 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002006 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2007 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002008 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002009 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002010
2011 // Both foreground window and its wallpaper should receive the touch down
2012 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002013 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2014
Arthur Hungc539dbb2022-12-08 07:45:36 +00002015 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002016 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002017 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2018 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002019 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2020
Arthur Hungc539dbb2022-12-08 07:45:36 +00002021 leftWindow->consumeMotionCancel();
2022 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2023 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002024}
2025
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002026/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002027 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2028 * interactive, it might stop sending this flag.
2029 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2030 * to have a consistent input stream.
2031 *
2032 * Test procedure:
2033 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2034 * DOWN (new gesture).
2035 *
2036 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2037 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2038 *
2039 * We technically just need a single window here, but we are using two windows (spy on top and a
2040 * regular window below) to emulate the actual situation where it happens on the device.
2041 */
2042TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2043 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2044 sp<FakeWindowHandle> spyWindow =
2045 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2046 spyWindow->setFrame(Rect(0, 0, 200, 200));
2047 spyWindow->setTrustedOverlay(true);
2048 spyWindow->setSpy(true);
2049
2050 sp<FakeWindowHandle> window =
2051 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2052 window->setFrame(Rect(0, 0, 200, 200));
2053
2054 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2055 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002056
2057 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002058 mDispatcher->notifyMotion(
2059 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2060 .deviceId(touchDeviceId)
2061 .policyFlags(DEFAULT_POLICY_FLAGS)
2062 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2063 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002064
Prabir Pradhan678438e2023-04-13 19:32:51 +00002065 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2066 .deviceId(touchDeviceId)
2067 .policyFlags(DEFAULT_POLICY_FLAGS)
2068 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2069 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2070 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002071 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2072 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2073 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2074 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2075
2076 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002077 mDispatcher->notifyMotion(
2078 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2079 .deviceId(touchDeviceId)
2080 .policyFlags(0)
2081 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2082 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2083 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002084 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2085 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2086
2087 // We don't need to reset the device to reproduce the issue, but the reset event typically
2088 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002089 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002090
2091 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002092 mDispatcher->notifyMotion(
2093 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2094 .deviceId(touchDeviceId)
2095 .policyFlags(DEFAULT_POLICY_FLAGS)
2096 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2097 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002098 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2099 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2100
2101 // No more events
2102 spyWindow->assertNoEvents();
2103 window->assertNoEvents();
2104}
2105
2106/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002107 * Two windows: a window on the left and a window on the right.
2108 * Mouse is hovered from the right window into the left window.
2109 * Next, we tap on the left window, where the cursor was last seen.
2110 * The second tap is done onto the right window.
2111 * The mouse and tap are from two different devices.
2112 * We technically don't need to set the downtime / eventtime for these events, but setting these
2113 * explicitly helps during debugging.
2114 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2115 * In the buggy implementation, a tap on the right window would cause a crash.
2116 */
2117TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2118 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2119 sp<FakeWindowHandle> leftWindow =
2120 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2121 leftWindow->setFrame(Rect(0, 0, 200, 200));
2122
2123 sp<FakeWindowHandle> rightWindow =
2124 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2125 rightWindow->setFrame(Rect(200, 0, 400, 200));
2126
2127 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2128 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2129 // stale.
2130 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2131 const int32_t mouseDeviceId = 6;
2132 const int32_t touchDeviceId = 4;
2133 // Move the cursor from right
2134 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2135 injectMotionEvent(mDispatcher,
2136 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2137 AINPUT_SOURCE_MOUSE)
2138 .deviceId(mouseDeviceId)
2139 .downTime(baseTime + 10)
2140 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002141 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002142 .x(300)
2143 .y(100))
2144 .build()));
2145 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2146
2147 // .. to the left window
2148 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2149 injectMotionEvent(mDispatcher,
2150 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2151 AINPUT_SOURCE_MOUSE)
2152 .deviceId(mouseDeviceId)
2153 .downTime(baseTime + 10)
2154 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002155 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002156 .x(110)
2157 .y(100))
2158 .build()));
2159 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2160 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2161 // Now tap the left window
2162 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2163 injectMotionEvent(mDispatcher,
2164 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2165 AINPUT_SOURCE_TOUCHSCREEN)
2166 .deviceId(touchDeviceId)
2167 .downTime(baseTime + 40)
2168 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002169 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002170 .x(100)
2171 .y(100))
2172 .build()));
2173 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2174 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2175
2176 // release tap
2177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2178 injectMotionEvent(mDispatcher,
2179 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2180 AINPUT_SOURCE_TOUCHSCREEN)
2181 .deviceId(touchDeviceId)
2182 .downTime(baseTime + 40)
2183 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002184 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002185 .x(100)
2186 .y(100))
2187 .build()));
2188 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2189
2190 // Tap the window on the right
2191 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2192 injectMotionEvent(mDispatcher,
2193 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2194 AINPUT_SOURCE_TOUCHSCREEN)
2195 .deviceId(touchDeviceId)
2196 .downTime(baseTime + 60)
2197 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002198 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002199 .x(300)
2200 .y(100))
2201 .build()));
2202 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2203
2204 // release tap
2205 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2206 injectMotionEvent(mDispatcher,
2207 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2208 AINPUT_SOURCE_TOUCHSCREEN)
2209 .deviceId(touchDeviceId)
2210 .downTime(baseTime + 60)
2211 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002212 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002213 .x(300)
2214 .y(100))
2215 .build()));
2216 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2217
2218 // No more events
2219 leftWindow->assertNoEvents();
2220 rightWindow->assertNoEvents();
2221}
2222
2223/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002224 * Start hovering in a window. While this hover is still active, make another window appear on top.
2225 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2226 * While the top window is present, the hovering is stopped.
2227 * Later, hovering gets resumed again.
2228 * Ensure that new hover gesture is handled correctly.
2229 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2230 * to the window that's currently being hovered over.
2231 */
2232TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2233 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2234 sp<FakeWindowHandle> window =
2235 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2236 window->setFrame(Rect(0, 0, 200, 200));
2237
2238 // Only a single window is present at first
2239 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2240
2241 // Start hovering in the window
2242 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2243 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2244 .build());
2245 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2246
2247 // Now, an obscuring window appears!
2248 sp<FakeWindowHandle> obscuringWindow =
2249 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2250 ADISPLAY_ID_DEFAULT,
2251 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2252 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2253 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2254 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2255 obscuringWindow->setNoInputChannel(true);
2256 obscuringWindow->setFocusable(false);
2257 obscuringWindow->setAlpha(1.0);
2258 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2259
2260 // While this new obscuring window is present, the hovering is stopped
2261 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2262 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2263 .build());
2264 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2265
2266 // Now the obscuring window goes away.
2267 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2268
2269 // And a new hover gesture starts.
2270 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2271 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2272 .build());
2273 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2274}
2275
2276/**
2277 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2278 * the obscuring window.
2279 */
2280TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2281 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2282 sp<FakeWindowHandle> window =
2283 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2284 window->setFrame(Rect(0, 0, 200, 200));
2285
2286 // Only a single window is present at first
2287 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2288
2289 // Start hovering in the window
2290 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2291 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2292 .build());
2293 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2294
2295 // Now, an obscuring window appears!
2296 sp<FakeWindowHandle> obscuringWindow =
2297 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2298 ADISPLAY_ID_DEFAULT,
2299 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2300 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2301 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2302 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2303 obscuringWindow->setNoInputChannel(true);
2304 obscuringWindow->setFocusable(false);
2305 obscuringWindow->setAlpha(1.0);
2306 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2307
2308 // While this new obscuring window is present, the hovering continues. The event can't go to the
2309 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2310 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2311 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2312 .build());
2313 obscuringWindow->assertNoEvents();
2314 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2315
2316 // Now the obscuring window goes away.
2317 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2318
2319 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2320 // so it should generate a HOVER_ENTER
2321 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2322 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2323 .build());
2324 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2325
2326 // Now the MOVE should be getting dispatched normally
2327 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2328 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2329 .build());
2330 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2331}
2332
2333/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002334 * Two windows: a window on the left and a window on the right.
2335 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2336 * down. Then, on the left window, also place second touch pointer down.
2337 * This test tries to reproduce a crash.
2338 * In the buggy implementation, second pointer down on the left window would cause a crash.
2339 */
2340TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2341 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2342 sp<FakeWindowHandle> leftWindow =
2343 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2344 leftWindow->setFrame(Rect(0, 0, 200, 200));
2345
2346 sp<FakeWindowHandle> rightWindow =
2347 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2348 rightWindow->setFrame(Rect(200, 0, 400, 200));
2349
2350 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2351
2352 const int32_t touchDeviceId = 4;
2353 const int32_t mouseDeviceId = 6;
2354 NotifyMotionArgs args;
2355
2356 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002357 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2358 .deviceId(mouseDeviceId)
2359 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2360 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002361 leftWindow->consumeMotionEvent(
2362 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2363
2364 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002365 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2366 .deviceId(mouseDeviceId)
2367 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2368 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2369 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002370
2371 leftWindow->consumeMotionEvent(
2372 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2373 leftWindow->consumeMotionEvent(
2374 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2375
Prabir Pradhan678438e2023-04-13 19:32:51 +00002376 mDispatcher->notifyMotion(
2377 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2378 .deviceId(mouseDeviceId)
2379 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2380 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2381 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2382 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002383 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2384
2385 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002386 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2387 .deviceId(touchDeviceId)
2388 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2389 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002390 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2391
2392 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2393
2394 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002395 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2396 .deviceId(touchDeviceId)
2397 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2398 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2399 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002400 leftWindow->consumeMotionEvent(
2401 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2402 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2403 // current implementation.
2404 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2405 rightWindow->consumeMotionEvent(
2406 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2407
2408 leftWindow->assertNoEvents();
2409 rightWindow->assertNoEvents();
2410}
2411
2412/**
2413 * On a single window, use two different devices: mouse and touch.
2414 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2415 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2416 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2417 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2418 * represent a new gesture.
2419 */
2420TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2421 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2422 sp<FakeWindowHandle> window =
2423 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2424 window->setFrame(Rect(0, 0, 400, 400));
2425
2426 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2427
2428 const int32_t touchDeviceId = 4;
2429 const int32_t mouseDeviceId = 6;
2430 NotifyMotionArgs args;
2431
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002432 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002433 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2434 .deviceId(touchDeviceId)
2435 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2436 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002437 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002438 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2439 .deviceId(touchDeviceId)
2440 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2441 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2442 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002443 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002444 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2445 .deviceId(touchDeviceId)
2446 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2447 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2448 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002449 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2450 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2451 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2452
2453 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002454 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2455 .deviceId(mouseDeviceId)
2456 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2457 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2458 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002459
2460 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002461 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002462 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2463
Prabir Pradhan678438e2023-04-13 19:32:51 +00002464 mDispatcher->notifyMotion(
2465 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2466 .deviceId(mouseDeviceId)
2467 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2468 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2469 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2470 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002471 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2472
2473 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002474 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2475 .deviceId(touchDeviceId)
2476 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2477 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2478 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002479 // The pointer_down event should be ignored
2480 window->assertNoEvents();
2481}
2482
2483/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002484 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2485 * the injected event.
2486 */
2487TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2488 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2489 sp<FakeWindowHandle> window =
2490 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2491 window->setFrame(Rect(0, 0, 400, 400));
2492
2493 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2494
2495 const int32_t touchDeviceId = 4;
2496 NotifyMotionArgs args;
2497 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2498 // completion.
2499 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2500 injectMotionEvent(mDispatcher,
2501 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2502 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002503 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002504 .x(50)
2505 .y(50))
2506 .build()));
2507 window->consumeMotionEvent(
2508 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2509
2510 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2511 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002512 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2513 .deviceId(touchDeviceId)
2514 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2515 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002516
2517 window->consumeMotionEvent(
2518 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2519 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2520}
2521
2522/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002523 * This test is similar to the test above, but the sequence of injected events is different.
2524 *
2525 * Two windows: a window on the left and a window on the right.
2526 * Mouse is hovered over the left window.
2527 * Next, we tap on the left window, where the cursor was last seen.
2528 *
2529 * After that, we inject one finger down onto the right window, and then a second finger down onto
2530 * the left window.
2531 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2532 * window (first), and then another on the left window (second).
2533 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2534 * In the buggy implementation, second finger down on the left window would cause a crash.
2535 */
2536TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2537 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2538 sp<FakeWindowHandle> leftWindow =
2539 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2540 leftWindow->setFrame(Rect(0, 0, 200, 200));
2541
2542 sp<FakeWindowHandle> rightWindow =
2543 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2544 rightWindow->setFrame(Rect(200, 0, 400, 200));
2545
2546 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2547
2548 const int32_t mouseDeviceId = 6;
2549 const int32_t touchDeviceId = 4;
2550 // Hover over the left window. Keep the cursor there.
2551 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2552 injectMotionEvent(mDispatcher,
2553 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2554 AINPUT_SOURCE_MOUSE)
2555 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002556 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002557 .x(50)
2558 .y(50))
2559 .build()));
2560 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2561
2562 // Tap on left window
2563 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2564 injectMotionEvent(mDispatcher,
2565 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2566 AINPUT_SOURCE_TOUCHSCREEN)
2567 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002568 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002569 .x(100)
2570 .y(100))
2571 .build()));
2572
2573 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2574 injectMotionEvent(mDispatcher,
2575 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2576 AINPUT_SOURCE_TOUCHSCREEN)
2577 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002578 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002579 .x(100)
2580 .y(100))
2581 .build()));
2582 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2583 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2584 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2585
2586 // First finger down on right window
2587 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2588 injectMotionEvent(mDispatcher,
2589 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2590 AINPUT_SOURCE_TOUCHSCREEN)
2591 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002592 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002593 .x(300)
2594 .y(100))
2595 .build()));
2596 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2597
2598 // Second finger down on the left window
2599 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2600 injectMotionEvent(mDispatcher,
2601 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2602 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002603 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002604 .x(300)
2605 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002606 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002607 .x(100)
2608 .y(100))
2609 .build()));
2610 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2611 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2612
2613 // No more events
2614 leftWindow->assertNoEvents();
2615 rightWindow->assertNoEvents();
2616}
2617
2618/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002619 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2620 * While the touch is down, new hover events from the stylus device should be ignored. After the
2621 * touch is gone, stylus hovering should start working again.
2622 */
2623TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2624 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2625 sp<FakeWindowHandle> window =
2626 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2627 window->setFrame(Rect(0, 0, 200, 200));
2628
2629 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2630
2631 const int32_t stylusDeviceId = 5;
2632 const int32_t touchDeviceId = 4;
2633 // Start hovering with stylus
2634 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2635 injectMotionEvent(mDispatcher,
2636 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2637 AINPUT_SOURCE_STYLUS)
2638 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002639 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002640 .x(50)
2641 .y(50))
2642 .build()));
2643 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2644
2645 // Finger down on the window
2646 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2647 injectMotionEvent(mDispatcher,
2648 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2649 AINPUT_SOURCE_TOUCHSCREEN)
2650 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002651 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002652 .x(100)
2653 .y(100))
2654 .build()));
2655 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2656 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2657
2658 // Try to continue hovering with stylus. Since we are already down, injection should fail
2659 ASSERT_EQ(InputEventInjectionResult::FAILED,
2660 injectMotionEvent(mDispatcher,
2661 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2662 AINPUT_SOURCE_STYLUS)
2663 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002664 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002665 .x(50)
2666 .y(50))
2667 .build()));
2668 // No event should be sent. This event should be ignored because a pointer from another device
2669 // is already down.
2670
2671 // Lift up the finger
2672 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2673 injectMotionEvent(mDispatcher,
2674 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2675 AINPUT_SOURCE_TOUCHSCREEN)
2676 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002677 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002678 .x(100)
2679 .y(100))
2680 .build()));
2681 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2682
2683 // Now that the touch is gone, stylus hovering should start working again
2684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2685 injectMotionEvent(mDispatcher,
2686 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2687 AINPUT_SOURCE_STYLUS)
2688 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002689 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002690 .x(50)
2691 .y(50))
2692 .build()));
2693 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2694 // No more events
2695 window->assertNoEvents();
2696}
2697
2698/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002699 * A spy window above a window with no input channel.
2700 * Start hovering with a stylus device, and then tap with it.
2701 * Ensure spy window receives the entire sequence.
2702 */
2703TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2704 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2705 sp<FakeWindowHandle> spyWindow =
2706 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2707 spyWindow->setFrame(Rect(0, 0, 200, 200));
2708 spyWindow->setTrustedOverlay(true);
2709 spyWindow->setSpy(true);
2710 sp<FakeWindowHandle> window =
2711 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2712 window->setNoInputChannel(true);
2713 window->setFrame(Rect(0, 0, 200, 200));
2714
2715 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2716
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002717 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002718 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2719 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2720 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002721 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2722 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002723 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2724 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2725 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002726 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2727
2728 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002729 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2730 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2731 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002732 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2733
2734 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002735 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
2736 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2737 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002738 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2739
2740 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002741 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2742 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2743 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002744 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2745 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002746 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2747 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2748 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002749 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2750
2751 // No more events
2752 spyWindow->assertNoEvents();
2753 window->assertNoEvents();
2754}
2755
2756/**
2757 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
2758 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
2759 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
2760 * While the mouse is down, new move events from the touch device should be ignored.
2761 */
2762TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
2763 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2764 sp<FakeWindowHandle> spyWindow =
2765 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2766 spyWindow->setFrame(Rect(0, 0, 200, 200));
2767 spyWindow->setTrustedOverlay(true);
2768 spyWindow->setSpy(true);
2769 sp<FakeWindowHandle> window =
2770 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2771 window->setFrame(Rect(0, 0, 200, 200));
2772
2773 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2774
2775 const int32_t mouseDeviceId = 7;
2776 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002777
2778 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00002779 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2780 .deviceId(mouseDeviceId)
2781 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2782 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002783 spyWindow->consumeMotionEvent(
2784 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2785 window->consumeMotionEvent(
2786 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2787
2788 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00002789 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2790 .deviceId(touchDeviceId)
2791 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
2792 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002793 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2794 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2795 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2796 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2797
Prabir Pradhan678438e2023-04-13 19:32:51 +00002798 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2799 .deviceId(touchDeviceId)
2800 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
2801 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002802 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2803 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2804
2805 // Pilfer the stream
2806 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2807 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2808
Prabir Pradhan678438e2023-04-13 19:32:51 +00002809 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2810 .deviceId(touchDeviceId)
2811 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
2812 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002813 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2814
2815 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002816 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2817 .deviceId(mouseDeviceId)
2818 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2819 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2820 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002821
2822 spyWindow->consumeMotionEvent(
2823 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2824 spyWindow->consumeMotionEvent(
2825 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2826 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2827
Prabir Pradhan678438e2023-04-13 19:32:51 +00002828 mDispatcher->notifyMotion(
2829 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2830 .deviceId(mouseDeviceId)
2831 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2832 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2833 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2834 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002835 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2836 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2837
2838 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002839 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
2840 .deviceId(mouseDeviceId)
2841 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2842 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
2843 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002844 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2845 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2846
2847 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002848 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2849 .deviceId(touchDeviceId)
2850 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
2851 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002852
2853 // No more events
2854 spyWindow->assertNoEvents();
2855 window->assertNoEvents();
2856}
2857
2858/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002859 * On the display, have a single window, and also an area where there's no window.
2860 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2861 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2862 */
2863TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2864 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2865 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002866 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002867
2868 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002869
2870 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00002871 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002872
2873 mDispatcher->waitForIdle();
2874 window->assertNoEvents();
2875
2876 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002877 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002878 mDispatcher->waitForIdle();
2879 window->consumeMotionDown();
2880}
2881
2882/**
2883 * Same test as above, but instead of touching the empty space, the first touch goes to
2884 * non-touchable window.
2885 */
2886TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2887 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2888 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002889 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002890 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2891 window1->setTouchable(false);
2892 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002893 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002894 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2895
2896 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2897
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002898 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002899 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002900
2901 mDispatcher->waitForIdle();
2902 window1->assertNoEvents();
2903 window2->assertNoEvents();
2904
2905 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002906 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002907 mDispatcher->waitForIdle();
2908 window2->consumeMotionDown();
2909}
2910
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002911/**
2912 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2913 * to the event time of the first ACTION_DOWN sent to the particular window.
2914 */
2915TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2916 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2917 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002918 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002919 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2920 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002921 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002922 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2923
2924 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2925
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002926 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002927 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002928
2929 mDispatcher->waitForIdle();
2930 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002931 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002932 window2->assertNoEvents();
2933 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2934 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2935 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2936
2937 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002938 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002939 mDispatcher->waitForIdle();
2940 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002941 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002942 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2943 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2944 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2945 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2946
2947 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002948 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002949 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002950 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002951
2952 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002953 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002954 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002955 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002956
2957 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
2958 window1->consumeMotionMove();
2959 window1->assertNoEvents();
2960
2961 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002962 mDispatcher->notifyMotion(
2963 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002964 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002965 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002966
Prabir Pradhan678438e2023-04-13 19:32:51 +00002967 mDispatcher->notifyMotion(
2968 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002969 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002970 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002971}
2972
Garfield Tandf26e862020-07-01 20:18:19 -07002973TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002974 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002975 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002976 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002977 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002978 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002979 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002980 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002981
2982 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2983
2984 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
2985
2986 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002987 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002988 injectMotionEvent(mDispatcher,
2989 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2990 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002991 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07002992 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002993 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002994
2995 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002996 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002997 injectMotionEvent(mDispatcher,
2998 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2999 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003000 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003001 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003002 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3003 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003004
3005 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003006 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003007 injectMotionEvent(mDispatcher,
3008 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3009 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003010 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003011 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003012 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3013 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003014
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003015 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003016 injectMotionEvent(mDispatcher,
3017 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3018 AINPUT_SOURCE_MOUSE)
3019 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3020 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003021 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003022 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003023 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003024
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003025 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003026 injectMotionEvent(mDispatcher,
3027 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3028 AINPUT_SOURCE_MOUSE)
3029 .buttonState(0)
3030 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003031 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003032 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003033 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003034
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003035 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003036 injectMotionEvent(mDispatcher,
3037 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3038 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003039 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003040 .build()));
3041 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3042
3043 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003044 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003045 injectMotionEvent(mDispatcher,
3046 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3047 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003048 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003049 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003050 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003051
3052 // No more events
3053 windowLeft->assertNoEvents();
3054 windowRight->assertNoEvents();
3055}
3056
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003057/**
3058 * Put two fingers down (and don't release them) and click the mouse button.
3059 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3060 * currently active gesture should be canceled, and the new one should proceed.
3061 */
3062TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3063 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3064 sp<FakeWindowHandle> window =
3065 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3066 window->setFrame(Rect(0, 0, 600, 800));
3067
3068 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3069
3070 const int32_t touchDeviceId = 4;
3071 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003072
3073 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003074 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3075 .deviceId(touchDeviceId)
3076 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3077 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003078
Prabir Pradhan678438e2023-04-13 19:32:51 +00003079 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3080 .deviceId(touchDeviceId)
3081 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3082 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3083 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003084 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3085 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3086
3087 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003088 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3089 .deviceId(mouseDeviceId)
3090 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3091 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3092 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003093 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3094 WithPointerCount(2u)));
3095 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3096
Prabir Pradhan678438e2023-04-13 19:32:51 +00003097 mDispatcher->notifyMotion(
3098 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3099 .deviceId(mouseDeviceId)
3100 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3101 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3102 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3103 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003104 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3105
3106 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3107 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003108 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3109 .deviceId(touchDeviceId)
3110 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3111 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3112 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003113 window->assertNoEvents();
3114}
3115
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003116TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3117 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3118
3119 sp<FakeWindowHandle> spyWindow =
3120 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3121 spyWindow->setFrame(Rect(0, 0, 600, 800));
3122 spyWindow->setTrustedOverlay(true);
3123 spyWindow->setSpy(true);
3124 sp<FakeWindowHandle> window =
3125 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3126 window->setFrame(Rect(0, 0, 600, 800));
3127
3128 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3129 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3130
3131 // Send mouse cursor to the window
3132 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3133 injectMotionEvent(mDispatcher,
3134 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3135 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003136 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003137 .x(100)
3138 .y(100))
3139 .build()));
3140
3141 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3142 WithSource(AINPUT_SOURCE_MOUSE)));
3143 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3144 WithSource(AINPUT_SOURCE_MOUSE)));
3145
3146 window->assertNoEvents();
3147 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003148}
3149
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003150TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3151 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3152
3153 sp<FakeWindowHandle> spyWindow =
3154 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3155 spyWindow->setFrame(Rect(0, 0, 600, 800));
3156 spyWindow->setTrustedOverlay(true);
3157 spyWindow->setSpy(true);
3158 sp<FakeWindowHandle> window =
3159 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3160 window->setFrame(Rect(0, 0, 600, 800));
3161
3162 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3163 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3164
3165 // Send mouse cursor to the window
3166 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3167 injectMotionEvent(mDispatcher,
3168 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3169 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003170 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003171 .x(100)
3172 .y(100))
3173 .build()));
3174
3175 // Move mouse cursor
3176 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3177 injectMotionEvent(mDispatcher,
3178 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3179 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003180 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003181 .x(110)
3182 .y(110))
3183 .build()));
3184
3185 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3186 WithSource(AINPUT_SOURCE_MOUSE)));
3187 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3188 WithSource(AINPUT_SOURCE_MOUSE)));
3189 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3190 WithSource(AINPUT_SOURCE_MOUSE)));
3191 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3192 WithSource(AINPUT_SOURCE_MOUSE)));
3193 // Touch down on the window
3194 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3195 injectMotionEvent(mDispatcher,
3196 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3197 AINPUT_SOURCE_TOUCHSCREEN)
3198 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003199 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003200 .x(200)
3201 .y(200))
3202 .build()));
3203 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3204 WithSource(AINPUT_SOURCE_MOUSE)));
3205 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3206 WithSource(AINPUT_SOURCE_MOUSE)));
3207 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3208 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3209 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3210 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3211
3212 // pilfer the motion, retaining the gesture on the spy window.
3213 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3214 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3215 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3216
3217 // Touch UP on the window
3218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3219 injectMotionEvent(mDispatcher,
3220 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3221 AINPUT_SOURCE_TOUCHSCREEN)
3222 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003223 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003224 .x(200)
3225 .y(200))
3226 .build()));
3227 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3228 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3229
3230 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3231 // to send a new gesture. It should again go to both windows (spy and the window below), just
3232 // like the first gesture did, before pilfering. The window configuration has not changed.
3233
3234 // One more tap - DOWN
3235 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3236 injectMotionEvent(mDispatcher,
3237 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3238 AINPUT_SOURCE_TOUCHSCREEN)
3239 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003240 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003241 .x(250)
3242 .y(250))
3243 .build()));
3244 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3245 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3246 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3247 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3248
3249 // Touch UP on the window
3250 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3251 injectMotionEvent(mDispatcher,
3252 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3253 AINPUT_SOURCE_TOUCHSCREEN)
3254 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003255 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003256 .x(250)
3257 .y(250))
3258 .build()));
3259 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3260 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3261 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3262 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3263
3264 window->assertNoEvents();
3265 spyWindow->assertNoEvents();
3266}
3267
Garfield Tandf26e862020-07-01 20:18:19 -07003268// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3269// directly in this test.
3270TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003271 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003272 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003273 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003274 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003275
3276 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3277
3278 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3279
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003280 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003281 injectMotionEvent(mDispatcher,
3282 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3283 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003284 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003285 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003286 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003287 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003288 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003289 injectMotionEvent(mDispatcher,
3290 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3291 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003292 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003293 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003294 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3295 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003296
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003297 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003298 injectMotionEvent(mDispatcher,
3299 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3300 AINPUT_SOURCE_MOUSE)
3301 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3302 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003303 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003304 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003305 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003306
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003307 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003308 injectMotionEvent(mDispatcher,
3309 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3310 AINPUT_SOURCE_MOUSE)
3311 .buttonState(0)
3312 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003313 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003314 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003315 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003316
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003317 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003318 injectMotionEvent(mDispatcher,
3319 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3320 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003321 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003322 .build()));
3323 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3324
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07003325 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
3326 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
3327 ASSERT_EQ(InputEventInjectionResult::FAILED,
Garfield Tandf26e862020-07-01 20:18:19 -07003328 injectMotionEvent(mDispatcher,
3329 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3330 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003331 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003332 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003333 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003334}
3335
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003336/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003337 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3338 * is generated.
3339 */
3340TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3341 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3342 sp<FakeWindowHandle> window =
3343 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3344 window->setFrame(Rect(0, 0, 1200, 800));
3345
3346 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3347
3348 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3349
3350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3351 injectMotionEvent(mDispatcher,
3352 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3353 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003354 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003355 .x(300)
3356 .y(400))
3357 .build()));
3358 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3359
3360 // Remove the window, but keep the channel.
3361 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3362 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3363}
3364
3365/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003366 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3367 */
3368TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3369 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3370 sp<FakeWindowHandle> window =
3371 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3372 window->setFrame(Rect(0, 0, 100, 100));
3373
3374 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3375
3376 const int32_t mouseDeviceId = 7;
3377 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003378
3379 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003380 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3381 .deviceId(mouseDeviceId)
3382 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3383 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003384 window->consumeMotionEvent(
3385 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3386
3387 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003388 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3389 .deviceId(touchDeviceId)
3390 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3391 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003392
3393 window->consumeMotionEvent(
3394 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3395 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3396}
3397
3398/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003399 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003400 * The tap causes a HOVER_EXIT event to be generated because the current event
3401 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003402 */
3403TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3404 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3405 sp<FakeWindowHandle> window =
3406 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3407 window->setFrame(Rect(0, 0, 100, 100));
3408
3409 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3410
3411 // Inject a hover_move from mouse.
3412 NotifyMotionArgs motionArgs =
3413 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3414 ADISPLAY_ID_DEFAULT, {{50, 50}});
3415 motionArgs.xCursorPosition = 50;
3416 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003417 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003418 ASSERT_NO_FATAL_FAILURE(
3419 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3420 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003421
3422 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003423 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3424 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3425 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003426 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003427 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3428 WithSource(AINPUT_SOURCE_MOUSE))));
3429
3430 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003431 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3432 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3433
Prabir Pradhan678438e2023-04-13 19:32:51 +00003434 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3435 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003436 ASSERT_NO_FATAL_FAILURE(
3437 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3438 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3439}
3440
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003441TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3442 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3443 sp<FakeWindowHandle> windowDefaultDisplay =
3444 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3445 ADISPLAY_ID_DEFAULT);
3446 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3447 sp<FakeWindowHandle> windowSecondDisplay =
3448 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3449 SECOND_DISPLAY_ID);
3450 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3451
3452 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3453 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3454
3455 // Set cursor position in window in default display and check that hover enter and move
3456 // events are generated.
3457 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3458 injectMotionEvent(mDispatcher,
3459 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3460 AINPUT_SOURCE_MOUSE)
3461 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003462 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003463 .x(300)
3464 .y(600))
3465 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003466 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003467
3468 // Remove all windows in secondary display and check that no event happens on window in
3469 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003470 mDispatcher->setInputWindows(
3471 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003472 windowDefaultDisplay->assertNoEvents();
3473
3474 // Move cursor position in window in default display and check that only hover move
3475 // event is generated and not hover enter event.
3476 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3477 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3478 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3479 injectMotionEvent(mDispatcher,
3480 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3481 AINPUT_SOURCE_MOUSE)
3482 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003483 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003484 .x(400)
3485 .y(700))
3486 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003487 windowDefaultDisplay->consumeMotionEvent(
3488 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3489 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003490 windowDefaultDisplay->assertNoEvents();
3491}
3492
Garfield Tan00f511d2019-06-12 16:55:40 -07003493TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003494 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003495
3496 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003497 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003498 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003499 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003500 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003501 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003502
3503 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3504
Arthur Hung72d8dc32020-03-28 00:48:39 +00003505 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003506
3507 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3508 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003509 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003510 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003511 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003512 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003513 windowRight->assertNoEvents();
3514}
3515
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003516TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003517 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003518 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3519 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003520 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003521
Arthur Hung72d8dc32020-03-28 00:48:39 +00003522 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003523 setFocusedWindow(window);
3524
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003525 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003526
Prabir Pradhan678438e2023-04-13 19:32:51 +00003527 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003528
3529 // Window should receive key down event.
3530 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3531
3532 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3533 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003534 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003535 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003536 AKEY_EVENT_FLAG_CANCELED);
3537}
3538
3539TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003540 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003541 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3542 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003543
Arthur Hung72d8dc32020-03-28 00:48:39 +00003544 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003545
Prabir Pradhan678438e2023-04-13 19:32:51 +00003546 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3547 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003548
3549 // Window should receive motion down event.
3550 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3551
3552 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3553 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003554 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003555 window->consumeMotionEvent(
3556 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003557}
3558
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003559TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
3560 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3561 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3562 "Fake Window", ADISPLAY_ID_DEFAULT);
3563
3564 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3565
3566 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3567 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
3568 .build());
3569
3570 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3571
3572 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
3573 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
3574 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3575
3576 // After the device has been reset, a new hovering stream can be sent to the window
3577 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3578 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
3579 .build());
3580 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3581}
3582
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003583TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3584 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003585 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3586 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003587 window->setFocusable(true);
3588
3589 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3590 setFocusedWindow(window);
3591
3592 window->consumeFocusEvent(true);
3593
Prabir Pradhan678438e2023-04-13 19:32:51 +00003594 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003595 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3596 const nsecs_t injectTime = keyArgs.eventTime;
3597 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003598 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003599 // The dispatching time should be always greater than or equal to intercept key timeout.
3600 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3601 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3602 std::chrono::nanoseconds(interceptKeyTimeout).count());
3603}
3604
3605TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3606 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003607 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3608 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003609 window->setFocusable(true);
3610
3611 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3612 setFocusedWindow(window);
3613
3614 window->consumeFocusEvent(true);
3615
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003616 mFakePolicy->setInterceptKeyTimeout(150ms);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003617 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
3618 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003619
3620 // Window should receive key event immediately when same key up.
3621 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3622 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3623}
3624
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003625/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003626 * Two windows. First is a regular window. Second does not overlap with the first, and has
3627 * WATCH_OUTSIDE_TOUCH.
3628 * Both windows are owned by the same UID.
3629 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3630 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3631 */
3632TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3633 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3634 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3635 "First Window", ADISPLAY_ID_DEFAULT);
3636 window->setFrame(Rect{0, 0, 100, 100});
3637
3638 sp<FakeWindowHandle> outsideWindow =
3639 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3640 ADISPLAY_ID_DEFAULT);
3641 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3642 outsideWindow->setWatchOutsideTouch(true);
3643 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3644 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3645
3646 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003647 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3648 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3649 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003650 window->consumeMotionDown();
3651 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3652 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3653 outsideWindow->consumeMotionEvent(
3654 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3655}
3656
3657/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003658 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3659 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3660 * ACTION_OUTSIDE event is sent per gesture.
3661 */
3662TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3663 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3664 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003665 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3666 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003667 window->setWatchOutsideTouch(true);
3668 window->setFrame(Rect{0, 0, 100, 100});
3669 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003670 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3671 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003672 secondWindow->setFrame(Rect{100, 100, 200, 200});
3673 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003674 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3675 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003676 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3677 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3678
3679 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003680 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3681 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3682 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003683 window->assertNoEvents();
3684 secondWindow->assertNoEvents();
3685
3686 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3687 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003688 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3689 ADISPLAY_ID_DEFAULT,
3690 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003691 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3692 window->consumeMotionEvent(
3693 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003694 secondWindow->consumeMotionDown();
3695 thirdWindow->assertNoEvents();
3696
3697 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3698 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003699 mDispatcher->notifyMotion(
3700 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3701 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003702 window->assertNoEvents();
3703 secondWindow->consumeMotionMove();
3704 thirdWindow->consumeMotionDown();
3705}
3706
Prabir Pradhan814fe082022-07-22 20:22:18 +00003707TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3708 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003709 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3710 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003711 window->setFocusable(true);
3712
Patrick Williamsd828f302023-04-28 17:52:08 -05003713 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003714 setFocusedWindow(window);
3715
3716 window->consumeFocusEvent(true);
3717
Prabir Pradhan678438e2023-04-13 19:32:51 +00003718 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3719 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3720 mDispatcher->notifyKey(keyDown);
3721 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003722
3723 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3724 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3725
3726 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003727 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003728
3729 window->consumeFocusEvent(false);
3730
Prabir Pradhan678438e2023-04-13 19:32:51 +00003731 mDispatcher->notifyKey(keyDown);
3732 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003733 window->assertNoEvents();
3734}
3735
Arthur Hung96483742022-11-15 03:30:48 +00003736TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3737 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3738 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3739 "Fake Window", ADISPLAY_ID_DEFAULT);
3740 // Ensure window is non-split and have some transform.
3741 window->setPreventSplitting(true);
3742 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05003743 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00003744
3745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3746 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3747 {50, 50}))
3748 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3749 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3750
3751 const MotionEvent secondFingerDownEvent =
3752 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3753 .displayId(ADISPLAY_ID_DEFAULT)
3754 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003755 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3756 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003757 .build();
3758 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3759 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3760 InputEventInjectionSync::WAIT_FOR_RESULT))
3761 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3762
3763 const MotionEvent* event = window->consumeMotion();
3764 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3765 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3766 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3767 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3768 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3769}
3770
Harry Cuttsb166c002023-05-09 13:06:05 +00003771TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
3772 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3773 sp<FakeWindowHandle> window =
3774 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3775 window->setFrame(Rect(0, 0, 400, 400));
3776 sp<FakeWindowHandle> trustedOverlay =
3777 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
3778 ADISPLAY_ID_DEFAULT);
3779 trustedOverlay->setSpy(true);
3780 trustedOverlay->setTrustedOverlay(true);
3781
3782 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}});
3783
3784 // Start a three-finger touchpad swipe
3785 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3786 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3787 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3788 .build());
3789 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3790 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3791 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3792 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3793 .build());
3794 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3795 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3796 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3797 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3798 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3799 .build());
3800
3801 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3802 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3803 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
3804
3805 // Move the swipe a bit
3806 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3807 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3808 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3809 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3810 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3811 .build());
3812
3813 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3814
3815 // End the swipe
3816 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3817 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3818 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3819 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3820 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3821 .build());
3822 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3823 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3824 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3825 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3826 .build());
3827 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3828 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3829 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3830 .build());
3831
3832 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
3833 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
3834 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
3835
3836 window->assertNoEvents();
3837}
3838
3839TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
3840 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3841 sp<FakeWindowHandle> window =
3842 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3843 window->setFrame(Rect(0, 0, 400, 400));
3844 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3845
3846 // Start a three-finger touchpad swipe
3847 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3848 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3849 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3850 .build());
3851 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3852 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3853 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3854 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3855 .build());
3856 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3857 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3858 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3859 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3860 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3861 .build());
3862
3863 // Move the swipe a bit
3864 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3865 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3866 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3867 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3868 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3869 .build());
3870
3871 // End the swipe
3872 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3873 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3874 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3875 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3876 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3877 .build());
3878 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3879 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3880 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3881 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3882 .build());
3883 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3884 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3885 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3886 .build());
3887
3888 window->assertNoEvents();
3889}
3890
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003891/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003892 * Ensure the correct coordinate spaces are used by InputDispatcher.
3893 *
3894 * InputDispatcher works in the display space, so its coordinate system is relative to the display
3895 * panel. Windows get events in the window space, and get raw coordinates in the logical display
3896 * space.
3897 */
3898class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
3899public:
3900 void SetUp() override {
3901 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003902 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003903 }
3904
3905 void addDisplayInfo(int displayId, const ui::Transform& transform) {
3906 gui::DisplayInfo info;
3907 info.displayId = displayId;
3908 info.transform = transform;
3909 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05003910 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003911 }
3912
3913 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
3914 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05003915 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003916 }
3917
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003918 void removeAllWindowsAndDisplays() {
3919 mDisplayInfos.clear();
3920 mWindowInfos.clear();
3921 }
3922
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003923 // Set up a test scenario where the display has a scaled projection and there are two windows
3924 // on the display.
3925 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
3926 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
3927 // respectively.
3928 ui::Transform displayTransform;
3929 displayTransform.set(2, 0, 0, 4);
3930 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3931
3932 std::shared_ptr<FakeApplicationHandle> application =
3933 std::make_shared<FakeApplicationHandle>();
3934
3935 // Add two windows to the display. Their frames are represented in the display space.
3936 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003937 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3938 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003939 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
3940 addWindow(firstWindow);
3941
3942 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003943 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3944 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003945 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
3946 addWindow(secondWindow);
3947 return {std::move(firstWindow), std::move(secondWindow)};
3948 }
3949
3950private:
3951 std::vector<gui::DisplayInfo> mDisplayInfos;
3952 std::vector<gui::WindowInfo> mWindowInfos;
3953};
3954
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003955TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003956 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3957 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003958 // selected so that if the hit test was performed with the point and the bounds being in
3959 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003960 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3961 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3962 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003963
3964 firstWindow->consumeMotionDown();
3965 secondWindow->assertNoEvents();
3966}
3967
3968// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
3969// the event should be treated as being in the logical display space.
3970TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
3971 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3972 // Send down to the first window. The point is represented in the logical display space. The
3973 // point is selected so that if the hit test was done in logical display space, then it would
3974 // end up in the incorrect window.
3975 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3976 PointF{75 * 2, 55 * 4});
3977
3978 firstWindow->consumeMotionDown();
3979 secondWindow->assertNoEvents();
3980}
3981
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003982// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
3983// event should be treated as being in the logical display space.
3984TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
3985 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3986
3987 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
3988 ui::Transform injectedEventTransform;
3989 injectedEventTransform.set(matrix);
3990 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
3991 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
3992
3993 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3994 .displayId(ADISPLAY_ID_DEFAULT)
3995 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003996 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003997 .x(untransformedPoint.x)
3998 .y(untransformedPoint.y))
3999 .build();
4000 event.transform(matrix);
4001
4002 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4003 InputEventInjectionSync::WAIT_FOR_RESULT);
4004
4005 firstWindow->consumeMotionDown();
4006 secondWindow->assertNoEvents();
4007}
4008
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004009TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4010 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4011
4012 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004013 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4014 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4015 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004016
4017 firstWindow->assertNoEvents();
4018 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004019 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004020 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4021
4022 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4023 EXPECT_EQ(300, event->getRawX(0));
4024 EXPECT_EQ(880, event->getRawY(0));
4025
4026 // Ensure that the x and y values are in the window's coordinate space.
4027 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4028 // the logical display space. This will be the origin of the window space.
4029 EXPECT_EQ(100, event->getX(0));
4030 EXPECT_EQ(80, event->getY(0));
4031}
4032
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004033/** Ensure consistent behavior of InputDispatcher in all orientations. */
4034class InputDispatcherDisplayOrientationFixture
4035 : public InputDispatcherDisplayProjectionTest,
4036 public ::testing::WithParamInterface<ui::Rotation> {};
4037
4038// This test verifies the touchable region of a window for all rotations of the display by tapping
4039// in different locations on the display, specifically points close to the four corners of a
4040// window.
4041TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4042 constexpr static int32_t displayWidth = 400;
4043 constexpr static int32_t displayHeight = 800;
4044
4045 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4046
4047 const auto rotation = GetParam();
4048
4049 // Set up the display with the specified rotation.
4050 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4051 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4052 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4053 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4054 logicalDisplayWidth, logicalDisplayHeight);
4055 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4056
4057 // Create a window with its bounds determined in the logical display.
4058 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4059 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4060 sp<FakeWindowHandle> window =
4061 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4062 window->setFrame(frameInDisplay, displayTransform);
4063 addWindow(window);
4064
4065 // The following points in logical display space should be inside the window.
4066 static const std::array<vec2, 4> insidePoints{
4067 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4068 for (const auto pointInsideWindow : insidePoints) {
4069 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4070 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004071 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4072 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4073 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004074 window->consumeMotionDown();
4075
Prabir Pradhan678438e2023-04-13 19:32:51 +00004076 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4077 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4078 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004079 window->consumeMotionUp();
4080 }
4081
4082 // The following points in logical display space should be outside the window.
4083 static const std::array<vec2, 5> outsidePoints{
4084 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4085 for (const auto pointOutsideWindow : outsidePoints) {
4086 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4087 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004088 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4089 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4090 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004091
Prabir Pradhan678438e2023-04-13 19:32:51 +00004092 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4093 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4094 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004095 }
4096 window->assertNoEvents();
4097}
4098
4099// Run the precision tests for all rotations.
4100INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4101 InputDispatcherDisplayOrientationFixture,
4102 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4103 ui::ROTATION_270),
4104 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4105 return ftl::enum_string(testParamInfo.param);
4106 });
4107
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004108using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4109 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004110
4111class TransferTouchFixture : public InputDispatcherTest,
4112 public ::testing::WithParamInterface<TransferFunction> {};
4113
4114TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004115 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004116
4117 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004118 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004119 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4120 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004121 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004122 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004123 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4124 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004125 sp<FakeWindowHandle> wallpaper =
4126 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4127 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004128 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004129 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004130
4131 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004132 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4133 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004134
Svet Ganov5d3bc372020-01-26 23:11:07 -08004135 // Only the first window should get the down event
4136 firstWindow->consumeMotionDown();
4137 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004138 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004139
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004140 // Transfer touch to the second window
4141 TransferFunction f = GetParam();
4142 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4143 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004144 // The first window gets cancel and the second gets down
4145 firstWindow->consumeMotionCancel();
4146 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004147 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004148
4149 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004150 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4151 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004152 // The first window gets no events and the second gets up
4153 firstWindow->assertNoEvents();
4154 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004155 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004156}
4157
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004158/**
4159 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4160 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4161 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4162 * natural to the user.
4163 * In this test, we are sending a pointer to both spy window and first window. We then try to
4164 * transfer touch to the second window. The dispatcher should identify the first window as the
4165 * one that should lose the gesture, and therefore the action should be to move the gesture from
4166 * the first window to the second.
4167 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4168 * the other API, as well.
4169 */
4170TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4171 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4172
4173 // Create a couple of windows + a spy window
4174 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004175 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004176 spyWindow->setTrustedOverlay(true);
4177 spyWindow->setSpy(true);
4178 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004179 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004180 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004181 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004182
4183 // Add the windows to the dispatcher
4184 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4185
4186 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004187 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4188 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004189 // Only the first window and spy should get the down event
4190 spyWindow->consumeMotionDown();
4191 firstWindow->consumeMotionDown();
4192
4193 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4194 // if f === 'transferTouch'.
4195 TransferFunction f = GetParam();
4196 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4197 ASSERT_TRUE(success);
4198 // The first window gets cancel and the second gets down
4199 firstWindow->consumeMotionCancel();
4200 secondWindow->consumeMotionDown();
4201
4202 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004203 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4204 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004205 // The first window gets no events and the second+spy get up
4206 firstWindow->assertNoEvents();
4207 spyWindow->consumeMotionUp();
4208 secondWindow->consumeMotionUp();
4209}
4210
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004211TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004212 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004213
4214 PointF touchPoint = {10, 10};
4215
4216 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004217 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004218 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4219 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004220 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004221 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004222 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4223 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004224 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004225
4226 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004227 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004228
4229 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004230 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4231 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4232 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004233 // Only the first window should get the down event
4234 firstWindow->consumeMotionDown();
4235 secondWindow->assertNoEvents();
4236
4237 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004238 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4239 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004240 // Only the first window should get the pointer down event
4241 firstWindow->consumeMotionPointerDown(1);
4242 secondWindow->assertNoEvents();
4243
4244 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004245 TransferFunction f = GetParam();
4246 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4247 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004248 // The first window gets cancel and the second gets down and pointer down
4249 firstWindow->consumeMotionCancel();
4250 secondWindow->consumeMotionDown();
4251 secondWindow->consumeMotionPointerDown(1);
4252
4253 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004254 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4255 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004256 // The first window gets nothing and the second gets pointer up
4257 firstWindow->assertNoEvents();
4258 secondWindow->consumeMotionPointerUp(1);
4259
4260 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004261 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4262 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004263 // The first window gets nothing and the second gets up
4264 firstWindow->assertNoEvents();
4265 secondWindow->consumeMotionUp();
4266}
4267
Arthur Hungc539dbb2022-12-08 07:45:36 +00004268TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4269 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4270
4271 // Create a couple of windows
4272 sp<FakeWindowHandle> firstWindow =
4273 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4274 ADISPLAY_ID_DEFAULT);
4275 firstWindow->setDupTouchToWallpaper(true);
4276 sp<FakeWindowHandle> secondWindow =
4277 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4278 ADISPLAY_ID_DEFAULT);
4279 secondWindow->setDupTouchToWallpaper(true);
4280
4281 sp<FakeWindowHandle> wallpaper1 =
4282 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4283 wallpaper1->setIsWallpaper(true);
4284
4285 sp<FakeWindowHandle> wallpaper2 =
4286 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4287 wallpaper2->setIsWallpaper(true);
4288 // Add the windows to the dispatcher
4289 mDispatcher->setInputWindows(
4290 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4291
4292 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004293 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4294 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004295
4296 // Only the first window should get the down event
4297 firstWindow->consumeMotionDown();
4298 secondWindow->assertNoEvents();
4299 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4300 wallpaper2->assertNoEvents();
4301
4302 // Transfer touch focus to the second window
4303 TransferFunction f = GetParam();
4304 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4305 ASSERT_TRUE(success);
4306
4307 // The first window gets cancel and the second gets down
4308 firstWindow->consumeMotionCancel();
4309 secondWindow->consumeMotionDown();
4310 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4311 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4312
4313 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004314 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4315 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004316 // The first window gets no events and the second gets up
4317 firstWindow->assertNoEvents();
4318 secondWindow->consumeMotionUp();
4319 wallpaper1->assertNoEvents();
4320 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4321}
4322
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004323// For the cases of single pointer touch and two pointers non-split touch, the api's
4324// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4325// for the case where there are multiple pointers split across several windows.
4326INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4327 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004328 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4329 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004330 return dispatcher->transferTouch(destChannelToken,
4331 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004332 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004333 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4334 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004335 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004336 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004337 }));
4338
Svet Ganov5d3bc372020-01-26 23:11:07 -08004339TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004340 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004341
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004342 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004343 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4344 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004345 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004346
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004347 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004348 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4349 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004350 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004351
4352 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004353 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004354
4355 PointF pointInFirst = {300, 200};
4356 PointF pointInSecond = {300, 600};
4357
4358 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004359 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4360 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4361 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004362 // Only the first window should get the down event
4363 firstWindow->consumeMotionDown();
4364 secondWindow->assertNoEvents();
4365
4366 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004367 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4368 ADISPLAY_ID_DEFAULT,
4369 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004370 // The first window gets a move and the second a down
4371 firstWindow->consumeMotionMove();
4372 secondWindow->consumeMotionDown();
4373
4374 // Transfer touch focus to the second window
4375 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4376 // The first window gets cancel and the new gets pointer down (it already saw down)
4377 firstWindow->consumeMotionCancel();
4378 secondWindow->consumeMotionPointerDown(1);
4379
4380 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004381 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4382 ADISPLAY_ID_DEFAULT,
4383 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004384 // The first window gets nothing and the second gets pointer up
4385 firstWindow->assertNoEvents();
4386 secondWindow->consumeMotionPointerUp(1);
4387
4388 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004389 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4390 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004391 // The first window gets nothing and the second gets up
4392 firstWindow->assertNoEvents();
4393 secondWindow->consumeMotionUp();
4394}
4395
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004396// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4397// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4398// touch is not supported, so the touch should continue on those windows and the transferred-to
4399// window should get nothing.
4400TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4401 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4402
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004403 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004404 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4405 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004406 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004407
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004408 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004409 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4410 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004411 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004412
4413 // Add the windows to the dispatcher
4414 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4415
4416 PointF pointInFirst = {300, 200};
4417 PointF pointInSecond = {300, 600};
4418
4419 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004420 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4421 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4422 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004423 // Only the first window should get the down event
4424 firstWindow->consumeMotionDown();
4425 secondWindow->assertNoEvents();
4426
4427 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004428 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4429 ADISPLAY_ID_DEFAULT,
4430 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004431 // The first window gets a move and the second a down
4432 firstWindow->consumeMotionMove();
4433 secondWindow->consumeMotionDown();
4434
4435 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004436 const bool transferred =
4437 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004438 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4439 ASSERT_FALSE(transferred);
4440 firstWindow->assertNoEvents();
4441 secondWindow->assertNoEvents();
4442
4443 // The rest of the dispatch should proceed as normal
4444 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004445 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4446 ADISPLAY_ID_DEFAULT,
4447 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004448 // The first window gets MOVE and the second gets pointer up
4449 firstWindow->consumeMotionMove();
4450 secondWindow->consumeMotionUp();
4451
4452 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004453 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4454 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004455 // The first window gets nothing and the second gets up
4456 firstWindow->consumeMotionUp();
4457 secondWindow->assertNoEvents();
4458}
4459
Arthur Hungabbb9d82021-09-01 14:52:30 +00004460// This case will create two windows and one mirrored window on the default display and mirror
4461// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4462// the windows info of second display before default display.
4463TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4464 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4465 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004466 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004467 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004468 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004469 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004470 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004471
4472 sp<FakeWindowHandle> mirrorWindowInPrimary =
4473 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4474 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004475
4476 sp<FakeWindowHandle> firstWindowInSecondary =
4477 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4478 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004479
4480 sp<FakeWindowHandle> secondWindowInSecondary =
4481 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4482 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004483
4484 // Update window info, let it find window handle of second display first.
4485 mDispatcher->setInputWindows(
4486 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4487 {ADISPLAY_ID_DEFAULT,
4488 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4489
4490 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4491 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4492 {50, 50}))
4493 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4494
4495 // Window should receive motion event.
4496 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4497
4498 // Transfer touch focus
4499 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4500 secondWindowInPrimary->getToken()));
4501 // The first window gets cancel.
4502 firstWindowInPrimary->consumeMotionCancel();
4503 secondWindowInPrimary->consumeMotionDown();
4504
4505 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4506 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4507 ADISPLAY_ID_DEFAULT, {150, 50}))
4508 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4509 firstWindowInPrimary->assertNoEvents();
4510 secondWindowInPrimary->consumeMotionMove();
4511
4512 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4513 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4514 {150, 50}))
4515 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4516 firstWindowInPrimary->assertNoEvents();
4517 secondWindowInPrimary->consumeMotionUp();
4518}
4519
4520// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4521// 'transferTouch' api.
4522TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4523 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4524 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004525 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004526 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004527 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004528 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004529 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004530
4531 sp<FakeWindowHandle> mirrorWindowInPrimary =
4532 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4533 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004534
4535 sp<FakeWindowHandle> firstWindowInSecondary =
4536 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4537 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004538
4539 sp<FakeWindowHandle> secondWindowInSecondary =
4540 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4541 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004542
4543 // Update window info, let it find window handle of second display first.
4544 mDispatcher->setInputWindows(
4545 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4546 {ADISPLAY_ID_DEFAULT,
4547 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4548
4549 // Touch on second display.
4550 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4551 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4552 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4553
4554 // Window should receive motion event.
4555 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4556
4557 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004558 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004559
4560 // The first window gets cancel.
4561 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4562 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4563
4564 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4565 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4566 SECOND_DISPLAY_ID, {150, 50}))
4567 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4568 firstWindowInPrimary->assertNoEvents();
4569 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4570
4571 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4572 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4573 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4574 firstWindowInPrimary->assertNoEvents();
4575 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4576}
4577
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004578TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004579 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004580 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4581 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004582
Vishnu Nair47074b82020-08-14 11:54:47 -07004583 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004584 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004585 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004586
4587 window->consumeFocusEvent(true);
4588
Prabir Pradhan678438e2023-04-13 19:32:51 +00004589 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004590
4591 // Window should receive key down event.
4592 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004593
4594 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004595 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00004596 mFakePolicy->assertUserActivityPoked();
4597}
4598
4599TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4600 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4601 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4602 "Fake Window", ADISPLAY_ID_DEFAULT);
4603
4604 window->setDisableUserActivity(true);
4605 window->setFocusable(true);
4606 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4607 setFocusedWindow(window);
4608
4609 window->consumeFocusEvent(true);
4610
4611 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4612
4613 // Window should receive key down event.
4614 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4615
4616 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004617 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00004618 mFakePolicy->assertUserActivityNotPoked();
4619}
4620
4621TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4622 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4623 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4624 "Fake Window", ADISPLAY_ID_DEFAULT);
4625
4626 window->setFocusable(true);
4627 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4628 setFocusedWindow(window);
4629
4630 window->consumeFocusEvent(true);
4631
4632 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4633 mDispatcher->waitForIdle();
4634
4635 // System key is not passed down
4636 window->assertNoEvents();
4637
4638 // Should have poked user activity
4639 mFakePolicy->assertUserActivityPoked();
4640}
4641
4642TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4643 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4644 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4645 "Fake Window", ADISPLAY_ID_DEFAULT);
4646
4647 window->setFocusable(true);
4648 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4649 setFocusedWindow(window);
4650
4651 window->consumeFocusEvent(true);
4652
4653 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4654 mDispatcher->waitForIdle();
4655
4656 // System key is not passed down
4657 window->assertNoEvents();
4658
4659 // Should have poked user activity
4660 mFakePolicy->assertUserActivityPoked();
4661}
4662
4663TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4664 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4665 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4666 "Fake Window", ADISPLAY_ID_DEFAULT);
4667
4668 window->setDisableUserActivity(true);
4669 window->setFocusable(true);
4670 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4671 setFocusedWindow(window);
4672
4673 window->consumeFocusEvent(true);
4674
4675 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4676 mDispatcher->waitForIdle();
4677
4678 // System key is not passed down
4679 window->assertNoEvents();
4680
4681 // Should have poked user activity
4682 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004683}
4684
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004685TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
4686 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4687 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4688 "Fake Window", ADISPLAY_ID_DEFAULT);
4689
4690 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4691
4692 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4693 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4694 ADISPLAY_ID_DEFAULT, {100, 100}))
4695 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4696
4697 window->consumeMotionEvent(
4698 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
4699
4700 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004701 mDispatcher->waitForIdle();
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004702 mFakePolicy->assertUserActivityPoked();
4703}
4704
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004705TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004706 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004707 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4708 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004709
Arthur Hung72d8dc32020-03-28 00:48:39 +00004710 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004711
Prabir Pradhan678438e2023-04-13 19:32:51 +00004712 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004713 mDispatcher->waitForIdle();
4714
4715 window->assertNoEvents();
4716}
4717
4718// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4719TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004720 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004721 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4722 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004723
Arthur Hung72d8dc32020-03-28 00:48:39 +00004724 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004725
4726 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004727 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004728 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004729 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4730 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004731
4732 // Window should receive only the motion event
4733 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4734 window->assertNoEvents(); // Key event or focus event will not be received
4735}
4736
arthurhungea3f4fc2020-12-21 23:18:53 +08004737TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4738 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4739
arthurhungea3f4fc2020-12-21 23:18:53 +08004740 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004741 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4742 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004743 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004744
arthurhungea3f4fc2020-12-21 23:18:53 +08004745 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004746 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4747 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004748 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004749
4750 // Add the windows to the dispatcher
4751 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4752
4753 PointF pointInFirst = {300, 200};
4754 PointF pointInSecond = {300, 600};
4755
4756 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004757 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4758 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4759 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004760 // Only the first window should get the down event
4761 firstWindow->consumeMotionDown();
4762 secondWindow->assertNoEvents();
4763
4764 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004765 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4766 ADISPLAY_ID_DEFAULT,
4767 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004768 // The first window gets a move and the second a down
4769 firstWindow->consumeMotionMove();
4770 secondWindow->consumeMotionDown();
4771
4772 // Send pointer cancel to the second window
4773 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004774 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004775 {pointInFirst, pointInSecond});
4776 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004777 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004778 // The first window gets move and the second gets cancel.
4779 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4780 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4781
4782 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004783 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4784 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08004785 // The first window gets up and the second gets nothing.
4786 firstWindow->consumeMotionUp();
4787 secondWindow->assertNoEvents();
4788}
4789
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004790TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
4791 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4792
4793 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004794 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004795 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4796 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
4797 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
4798 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
4799
Harry Cutts33476232023-01-30 19:57:29 +00004800 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004801 window->assertNoEvents();
4802 mDispatcher->waitForIdle();
4803}
4804
chaviwd1c23182019-12-20 18:44:56 -08004805class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00004806public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004807 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004808 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07004809 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004810 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07004811 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00004812 }
4813
chaviwd1c23182019-12-20 18:44:56 -08004814 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
4815
4816 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004817 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
4818 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08004819 }
4820
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004821 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
4822
4823 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
4824
chaviwd1c23182019-12-20 18:44:56 -08004825 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004826 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08004827 expectedDisplayId, expectedFlags);
4828 }
4829
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004830 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004831 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004832 expectedDisplayId, expectedFlags);
4833 }
4834
chaviwd1c23182019-12-20 18:44:56 -08004835 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004836 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08004837 expectedDisplayId, expectedFlags);
4838 }
4839
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004840 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004841 mInputReceiver->consumeMotionEvent(
4842 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4843 WithDisplayId(expectedDisplayId),
4844 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004845 }
4846
Arthur Hungfbfa5722021-11-16 02:45:54 +00004847 void consumeMotionPointerDown(int32_t pointerIdx) {
4848 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4849 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004850 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004851 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004852 }
4853
Evan Rosky84f07f02021-04-16 10:42:42 -07004854 MotionEvent* consumeMotion() {
4855 InputEvent* event = mInputReceiver->consume();
4856 if (!event) {
4857 ADD_FAILURE() << "No event was produced";
4858 return nullptr;
4859 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004860 if (event->getType() != InputEventType::MOTION) {
4861 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07004862 return nullptr;
4863 }
4864 return static_cast<MotionEvent*>(event);
4865 }
4866
chaviwd1c23182019-12-20 18:44:56 -08004867 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4868
4869private:
4870 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004871};
4872
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004873using InputDispatcherMonitorTest = InputDispatcherTest;
4874
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004875/**
4876 * Two entities that receive touch: A window, and a global monitor.
4877 * The touch goes to the window, and then the window disappears.
4878 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4879 * for the monitor, as well.
4880 * 1. foregroundWindow
4881 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4882 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004883TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004884 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4885 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004886 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004887
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004888 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004889
4890 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4891 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4892 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4893 {100, 200}))
4894 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4895
4896 // Both the foreground window and the global monitor should receive the touch down
4897 window->consumeMotionDown();
4898 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4899
4900 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4901 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4902 ADISPLAY_ID_DEFAULT, {110, 200}))
4903 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4904
4905 window->consumeMotionMove();
4906 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4907
4908 // Now the foreground window goes away
4909 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4910 window->consumeMotionCancel();
4911 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
4912
4913 // If more events come in, there will be no more foreground window to send them to. This will
4914 // cause a cancel for the monitor, as well.
4915 ASSERT_EQ(InputEventInjectionResult::FAILED,
4916 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4917 ADISPLAY_ID_DEFAULT, {120, 200}))
4918 << "Injection should fail because the window was removed";
4919 window->assertNoEvents();
4920 // Global monitor now gets the cancel
4921 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4922}
4923
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004924TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07004925 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004926 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4927 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004928 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004929
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004930 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004931
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004932 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004933 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004934 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00004935 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004936 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004937}
4938
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004939TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
4940 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004941
Chris Yea209fde2020-07-22 13:54:51 -07004942 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004943 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4944 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004945 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004946
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004947 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004948 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004949 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08004950 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004951 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004952
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004953 // Pilfer pointers from the monitor.
4954 // This should not do anything and the window should continue to receive events.
4955 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00004956
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004957 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004958 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4959 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004960 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004961
4962 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4963 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004964}
4965
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004966TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07004967 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004968 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4969 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004970 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4971 window->setWindowOffset(20, 40);
4972 window->setWindowTransform(0, 1, -1, 0);
4973
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004974 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004975
4976 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4977 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4978 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4979 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4980 MotionEvent* event = monitor.consumeMotion();
4981 // Even though window has transform, gesture monitor must not.
4982 ASSERT_EQ(ui::Transform(), event->getTransform());
4983}
4984
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004985TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00004986 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004987 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00004988
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004989 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00004990 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004991 << "Injection should fail if there is a monitor, but no touchable window";
4992 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00004993}
4994
chaviw81e2bb92019-12-18 15:03:51 -08004995TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004996 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004997 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4998 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08004999
Arthur Hung72d8dc32020-03-28 00:48:39 +00005000 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08005001
5002 NotifyMotionArgs motionArgs =
5003 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5004 ADISPLAY_ID_DEFAULT);
5005
Prabir Pradhan678438e2023-04-13 19:32:51 +00005006 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005007 // Window should receive motion down event.
5008 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5009
5010 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005011 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005012 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5013 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5014 motionArgs.pointerCoords[0].getX() - 10);
5015
Prabir Pradhan678438e2023-04-13 19:32:51 +00005016 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005017 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005018 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005019}
5020
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005021/**
5022 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5023 * the device default right away. In the test scenario, we check both the default value,
5024 * and the action of enabling / disabling.
5025 */
5026TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005027 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005028 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5029 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005030 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005031
5032 // Set focused application.
5033 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005034 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005035
5036 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005037 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005038 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005039 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005040
5041 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005042 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005043 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005044 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005045
5046 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005047 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005048 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005049 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005050 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005051 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005052 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005053 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005054
5055 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005056 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005057 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005058 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005059
5060 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005061 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005062 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005063 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005064 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005065 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005066 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005067 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005068
5069 window->assertNoEvents();
5070}
5071
Gang Wange9087892020-01-07 12:17:14 -05005072TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005073 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005074 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5075 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005076
5077 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005078 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005079
Arthur Hung72d8dc32020-03-28 00:48:39 +00005080 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005081 setFocusedWindow(window);
5082
Harry Cutts33476232023-01-30 19:57:29 +00005083 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005084
Prabir Pradhan678438e2023-04-13 19:32:51 +00005085 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5086 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005087
5088 InputEvent* event = window->consume();
5089 ASSERT_NE(event, nullptr);
5090
5091 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5092 ASSERT_NE(verified, nullptr);
5093 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5094
5095 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5096 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5097 ASSERT_EQ(keyArgs.source, verified->source);
5098 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5099
5100 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5101
5102 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005103 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005104 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005105 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5106 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5107 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5108 ASSERT_EQ(0, verifiedKey.repeatCount);
5109}
5110
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005111TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005112 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005113 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5114 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005115
5116 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5117
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005118 ui::Transform transform;
5119 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5120
5121 gui::DisplayInfo displayInfo;
5122 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5123 displayInfo.transform = transform;
5124
Patrick Williamsd828f302023-04-28 17:52:08 -05005125 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005126
Prabir Pradhan678438e2023-04-13 19:32:51 +00005127 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005128 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5129 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005130 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005131
5132 InputEvent* event = window->consume();
5133 ASSERT_NE(event, nullptr);
5134
5135 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5136 ASSERT_NE(verified, nullptr);
5137 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5138
5139 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5140 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5141 EXPECT_EQ(motionArgs.source, verified->source);
5142 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5143
5144 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5145
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005146 const vec2 rawXY =
5147 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5148 motionArgs.pointerCoords[0].getXYValue());
5149 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5150 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005151 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005152 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005153 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005154 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5155 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5156}
5157
chaviw09c8d2d2020-08-24 15:48:26 -07005158/**
5159 * Ensure that separate calls to sign the same data are generating the same key.
5160 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5161 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5162 * tests.
5163 */
5164TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5165 KeyEvent event = getTestKeyEvent();
5166 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5167
5168 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5169 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5170 ASSERT_EQ(hmac1, hmac2);
5171}
5172
5173/**
5174 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5175 */
5176TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5177 KeyEvent event = getTestKeyEvent();
5178 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5179 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5180
5181 verifiedEvent.deviceId += 1;
5182 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5183
5184 verifiedEvent.source += 1;
5185 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5186
5187 verifiedEvent.eventTimeNanos += 1;
5188 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5189
5190 verifiedEvent.displayId += 1;
5191 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5192
5193 verifiedEvent.action += 1;
5194 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5195
5196 verifiedEvent.downTimeNanos += 1;
5197 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5198
5199 verifiedEvent.flags += 1;
5200 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5201
5202 verifiedEvent.keyCode += 1;
5203 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5204
5205 verifiedEvent.scanCode += 1;
5206 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5207
5208 verifiedEvent.metaState += 1;
5209 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5210
5211 verifiedEvent.repeatCount += 1;
5212 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5213}
5214
Vishnu Nair958da932020-08-21 17:12:37 -07005215TEST_F(InputDispatcherTest, SetFocusedWindow) {
5216 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5217 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005218 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005219 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005220 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005221 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5222
5223 // Top window is also focusable but is not granted focus.
5224 windowTop->setFocusable(true);
5225 windowSecond->setFocusable(true);
5226 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5227 setFocusedWindow(windowSecond);
5228
5229 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005230 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5231 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005232
5233 // Focused window should receive event.
5234 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5235 windowTop->assertNoEvents();
5236}
5237
5238TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5239 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5240 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005241 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005242 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5243
5244 window->setFocusable(true);
5245 // Release channel for window is no longer valid.
5246 window->releaseChannel();
5247 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5248 setFocusedWindow(window);
5249
5250 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005251 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5252 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005253
5254 // window channel is invalid, so it should not receive any input event.
5255 window->assertNoEvents();
5256}
5257
5258TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5259 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5260 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005261 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005262 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005263 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5264
Vishnu Nair958da932020-08-21 17:12:37 -07005265 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5266 setFocusedWindow(window);
5267
5268 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005269 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5270 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005271
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005272 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005273 window->assertNoEvents();
5274}
5275
5276TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5277 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5278 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005279 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005280 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005281 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005282 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5283
5284 windowTop->setFocusable(true);
5285 windowSecond->setFocusable(true);
5286 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5287 setFocusedWindow(windowTop);
5288 windowTop->consumeFocusEvent(true);
5289
Chavi Weingarten847e8512023-03-29 00:26:09 +00005290 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5291 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005292 windowSecond->consumeFocusEvent(true);
5293 windowTop->consumeFocusEvent(false);
5294
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005295 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5296 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005297
5298 // Focused window should receive event.
5299 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5300}
5301
Chavi Weingarten847e8512023-03-29 00:26:09 +00005302TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005303 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5304 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005305 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005306 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005307 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005308 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5309
5310 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005311 windowSecond->setFocusable(false);
5312 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005313 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005314 setFocusedWindow(windowTop);
5315 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005316
Chavi Weingarten847e8512023-03-29 00:26:09 +00005317 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5318 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005319
5320 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005321 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005322 windowSecond->assertNoEvents();
5323}
5324
5325TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5326 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5327 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005328 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005329 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005330 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5331 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005332 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5333
5334 window->setFocusable(true);
5335 previousFocusedWindow->setFocusable(true);
5336 window->setVisible(false);
5337 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5338 setFocusedWindow(previousFocusedWindow);
5339 previousFocusedWindow->consumeFocusEvent(true);
5340
5341 // Requesting focus on invisible window takes focus from currently focused window.
5342 setFocusedWindow(window);
5343 previousFocusedWindow->consumeFocusEvent(false);
5344
5345 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005346 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005347 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5348 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005349
5350 // Window does not get focus event or key down.
5351 window->assertNoEvents();
5352
5353 // Window becomes visible.
5354 window->setVisible(true);
5355 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5356
5357 // Window receives focus event.
5358 window->consumeFocusEvent(true);
5359 // Focused window receives key down.
5360 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5361}
5362
Vishnu Nair599f1412021-06-21 10:39:58 -07005363TEST_F(InputDispatcherTest, DisplayRemoved) {
5364 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5365 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005366 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005367 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5368
5369 // window is granted focus.
5370 window->setFocusable(true);
5371 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5372 setFocusedWindow(window);
5373 window->consumeFocusEvent(true);
5374
5375 // When a display is removed window loses focus.
5376 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5377 window->consumeFocusEvent(false);
5378}
5379
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005380/**
5381 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5382 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5383 * of the 'slipperyEnterWindow'.
5384 *
5385 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5386 * a way so that the touched location is no longer covered by the top window.
5387 *
5388 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5389 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5390 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5391 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5392 * with ACTION_DOWN).
5393 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5394 * window moved itself away from the touched location and had Flag::SLIPPERY.
5395 *
5396 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5397 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5398 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5399 *
5400 * In this test, we ensure that the event received by the bottom window has
5401 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5402 */
5403TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005404 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5405 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005406
5407 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5408 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5409
5410 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005411 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005412 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005413 // Make sure this one overlaps the bottom window
5414 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5415 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5416 // one. Windows with the same owner are not considered to be occluding each other.
5417 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5418
5419 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005420 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005421 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5422
5423 mDispatcher->setInputWindows(
5424 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5425
5426 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005427 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5428 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5429 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005430 slipperyExitWindow->consumeMotionDown();
5431 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5432 mDispatcher->setInputWindows(
5433 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5434
Prabir Pradhan678438e2023-04-13 19:32:51 +00005435 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5436 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5437 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005438
5439 slipperyExitWindow->consumeMotionCancel();
5440
5441 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5442 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5443}
5444
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005445/**
5446 * Two windows, one on the left and another on the right. The left window is slippery. The right
5447 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5448 * touch moves from the left window into the right window, the gesture should continue to go to the
5449 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5450 * reproduces a crash.
5451 */
5452TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5453 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5454
5455 sp<FakeWindowHandle> leftSlipperyWindow =
5456 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5457 leftSlipperyWindow->setSlippery(true);
5458 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5459
5460 sp<FakeWindowHandle> rightDropTouchesWindow =
5461 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5462 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5463 rightDropTouchesWindow->setDropInput(true);
5464
5465 mDispatcher->setInputWindows(
5466 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5467
5468 // Start touch in the left window
5469 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5470 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5471 .build());
5472 leftSlipperyWindow->consumeMotionDown();
5473
5474 // And move it into the right window
5475 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5476 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5477 .build());
5478
5479 // Since the right window isn't eligible to receive input, touch does not slip.
5480 // The left window continues to receive the gesture.
5481 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5482 rightDropTouchesWindow->assertNoEvents();
5483}
5484
Garfield Tan1c7bc862020-01-28 13:24:04 -08005485class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5486protected:
5487 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5488 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5489
Chris Yea209fde2020-07-22 13:54:51 -07005490 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005491 sp<FakeWindowHandle> mWindow;
5492
5493 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005494 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005495 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005496 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005497 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005498 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5499 ASSERT_EQ(OK, mDispatcher->start());
5500
5501 setUpWindow();
5502 }
5503
5504 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005505 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005506 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005507
Vishnu Nair47074b82020-08-14 11:54:47 -07005508 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005509 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005510 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005511 mWindow->consumeFocusEvent(true);
5512 }
5513
Chris Ye2ad95392020-09-01 13:44:44 -07005514 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005515 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005516 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005517 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005518 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005519
5520 // Window should receive key down event.
5521 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5522 }
5523
5524 void expectKeyRepeatOnce(int32_t repeatCount) {
5525 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5526 InputEvent* repeatEvent = mWindow->consume();
5527 ASSERT_NE(nullptr, repeatEvent);
5528
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005529 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005530
5531 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5532 uint32_t eventAction = repeatKeyEvent->getAction();
5533 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5534 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5535 }
5536
Chris Ye2ad95392020-09-01 13:44:44 -07005537 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005538 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005539 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005540 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005541 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005542
5543 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005544 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005545 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005546 }
5547};
5548
5549TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005550 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005551 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5552 expectKeyRepeatOnce(repeatCount);
5553 }
5554}
5555
5556TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005557 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005558 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5559 expectKeyRepeatOnce(repeatCount);
5560 }
Harry Cutts33476232023-01-30 19:57:29 +00005561 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005562 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005563 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5564 expectKeyRepeatOnce(repeatCount);
5565 }
5566}
5567
5568TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005569 sendAndConsumeKeyDown(/*deviceId=*/1);
5570 expectKeyRepeatOnce(/*repeatCount=*/1);
5571 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005572 mWindow->assertNoEvents();
5573}
5574
5575TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005576 sendAndConsumeKeyDown(/*deviceId=*/1);
5577 expectKeyRepeatOnce(/*repeatCount=*/1);
5578 sendAndConsumeKeyDown(/*deviceId=*/2);
5579 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005580 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005581 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005582 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005583 expectKeyRepeatOnce(/*repeatCount=*/2);
5584 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005585 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005586 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005587 mWindow->assertNoEvents();
5588}
5589
5590TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005591 sendAndConsumeKeyDown(/*deviceId=*/1);
5592 expectKeyRepeatOnce(/*repeatCount=*/1);
5593 sendAndConsumeKeyDown(/*deviceId=*/2);
5594 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005595 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005596 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005597 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005598 mWindow->assertNoEvents();
5599}
5600
liushenxiang42232912021-05-21 20:24:09 +08005601TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5602 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005603 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005604 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005605 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5606 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5607 mWindow->assertNoEvents();
5608}
5609
Garfield Tan1c7bc862020-01-28 13:24:04 -08005610TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005611 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005612 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005613 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5614 InputEvent* repeatEvent = mWindow->consume();
5615 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5616 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5617 IdGenerator::getSource(repeatEvent->getId()));
5618 }
5619}
5620
5621TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005622 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005623 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005624
5625 std::unordered_set<int32_t> idSet;
5626 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5627 InputEvent* repeatEvent = mWindow->consume();
5628 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5629 int32_t id = repeatEvent->getId();
5630 EXPECT_EQ(idSet.end(), idSet.find(id));
5631 idSet.insert(id);
5632 }
5633}
5634
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005635/* Test InputDispatcher for MultiDisplay */
5636class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5637public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005638 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005639 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005640
Chris Yea209fde2020-07-22 13:54:51 -07005641 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005642 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005643 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005644
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005645 // Set focus window for primary display, but focused display would be second one.
5646 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005647 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005648 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005649 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005650 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005651
Chris Yea209fde2020-07-22 13:54:51 -07005652 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005653 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005654 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005655 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005656 // Set focus display to second one.
5657 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5658 // Set focus window for second display.
5659 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005660 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005661 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005662 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005663 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005664 }
5665
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005666 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005667 InputDispatcherTest::TearDown();
5668
Chris Yea209fde2020-07-22 13:54:51 -07005669 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005670 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005671 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005672 windowInSecondary.clear();
5673 }
5674
5675protected:
Chris Yea209fde2020-07-22 13:54:51 -07005676 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005677 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005678 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005679 sp<FakeWindowHandle> windowInSecondary;
5680};
5681
5682TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5683 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5685 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5686 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005687 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005688 windowInSecondary->assertNoEvents();
5689
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005690 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005691 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5692 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5693 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005694 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005695 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005696}
5697
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005698TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005699 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005700 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5701 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005702 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005703 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005704 windowInSecondary->assertNoEvents();
5705
5706 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005707 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005708 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005709 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005710 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005711
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005712 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005713 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005714
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005715 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005716 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005717 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005718
5719 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005720 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005721 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005722 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005723 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005724 windowInSecondary->assertNoEvents();
5725}
5726
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005727// Test per-display input monitors for motion event.
5728TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005729 FakeMonitorReceiver monitorInPrimary =
5730 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5731 FakeMonitorReceiver monitorInSecondary =
5732 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005733
5734 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005735 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5736 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5737 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005738 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005739 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005740 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005741 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005742
5743 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005744 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5745 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5746 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005747 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005748 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005749 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005750 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005751
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005752 // Lift up the touch from the second display
5753 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5754 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5755 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5756 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5757 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5758
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005759 // Test inject a non-pointer motion event.
5760 // If specific a display, it will dispatch to the focused window of particular display,
5761 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005762 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5763 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5764 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005765 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005766 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005767 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005768 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005769}
5770
5771// Test per-display input monitors for key event.
5772TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005773 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005774 FakeMonitorReceiver monitorInPrimary =
5775 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5776 FakeMonitorReceiver monitorInSecondary =
5777 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005778
5779 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005780 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5781 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005782 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005783 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005784 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005785 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005786}
5787
Vishnu Nair958da932020-08-21 17:12:37 -07005788TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
5789 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005790 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005791 secondWindowInPrimary->setFocusable(true);
5792 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
5793 setFocusedWindow(secondWindowInPrimary);
5794 windowInPrimary->consumeFocusEvent(false);
5795 secondWindowInPrimary->consumeFocusEvent(true);
5796
5797 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005798 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
5799 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005800 windowInPrimary->assertNoEvents();
5801 windowInSecondary->assertNoEvents();
5802 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5803}
5804
Arthur Hungdfd528e2021-12-08 13:23:04 +00005805TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
5806 FakeMonitorReceiver monitorInPrimary =
5807 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5808 FakeMonitorReceiver monitorInSecondary =
5809 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
5810
5811 // Test touch down on primary display.
5812 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5813 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5814 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5815 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5816 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5817
5818 // Test touch down on second display.
5819 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5820 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5821 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5822 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
5823 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
5824
5825 // Trigger cancel touch.
5826 mDispatcher->cancelCurrentTouch();
5827 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5828 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5829 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
5830 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
5831
5832 // Test inject a move motion event, no window/monitor should receive the event.
5833 ASSERT_EQ(InputEventInjectionResult::FAILED,
5834 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5835 ADISPLAY_ID_DEFAULT, {110, 200}))
5836 << "Inject motion event should return InputEventInjectionResult::FAILED";
5837 windowInPrimary->assertNoEvents();
5838 monitorInPrimary.assertNoEvents();
5839
5840 ASSERT_EQ(InputEventInjectionResult::FAILED,
5841 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5842 SECOND_DISPLAY_ID, {110, 200}))
5843 << "Inject motion event should return InputEventInjectionResult::FAILED";
5844 windowInSecondary->assertNoEvents();
5845 monitorInSecondary.assertNoEvents();
5846}
5847
Jackal Guof9696682018-10-05 12:23:23 +08005848class InputFilterTest : public InputDispatcherTest {
5849protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005850 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
5851 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08005852 NotifyMotionArgs motionArgs;
5853
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005854 motionArgs =
5855 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005856 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005857 motionArgs =
5858 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005859 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005860 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005861 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005862 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
5863 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08005864 } else {
5865 mFakePolicy->assertFilterInputEventWasNotCalled();
5866 }
5867 }
5868
5869 void testNotifyKey(bool expectToBeFiltered) {
5870 NotifyKeyArgs keyArgs;
5871
5872 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005873 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005874 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005875 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005876 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005877
5878 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08005879 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005880 } else {
5881 mFakePolicy->assertFilterInputEventWasNotCalled();
5882 }
5883 }
5884};
5885
5886// Test InputFilter for MotionEvent
5887TEST_F(InputFilterTest, MotionEvent_InputFilter) {
5888 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
5889 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5890 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5891
5892 // Enable InputFilter
5893 mDispatcher->setInputFilterEnabled(true);
5894 // Test touch on both primary and second display, and check if both events are filtered.
5895 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
5896 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
5897
5898 // Disable InputFilter
5899 mDispatcher->setInputFilterEnabled(false);
5900 // Test touch on both primary and second display, and check if both events aren't filtered.
5901 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5902 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5903}
5904
5905// Test InputFilter for KeyEvent
5906TEST_F(InputFilterTest, KeyEvent_InputFilter) {
5907 // Since the InputFilter is disabled by default, check if key event aren't filtered.
5908 testNotifyKey(/*expectToBeFiltered*/ false);
5909
5910 // Enable InputFilter
5911 mDispatcher->setInputFilterEnabled(true);
5912 // Send a key event, and check if it is filtered.
5913 testNotifyKey(/*expectToBeFiltered*/ true);
5914
5915 // Disable InputFilter
5916 mDispatcher->setInputFilterEnabled(false);
5917 // Send a key event, and check if it isn't filtered.
5918 testNotifyKey(/*expectToBeFiltered*/ false);
5919}
5920
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005921// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
5922// logical display coordinate space.
5923TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
5924 ui::Transform firstDisplayTransform;
5925 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5926 ui::Transform secondDisplayTransform;
5927 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
5928
5929 std::vector<gui::DisplayInfo> displayInfos(2);
5930 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
5931 displayInfos[0].transform = firstDisplayTransform;
5932 displayInfos[1].displayId = SECOND_DISPLAY_ID;
5933 displayInfos[1].transform = secondDisplayTransform;
5934
Patrick Williamsd828f302023-04-28 17:52:08 -05005935 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005936
5937 // Enable InputFilter
5938 mDispatcher->setInputFilterEnabled(true);
5939
5940 // Ensure the correct transforms are used for the displays.
5941 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
5942 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
5943}
5944
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005945class InputFilterInjectionPolicyTest : public InputDispatcherTest {
5946protected:
5947 virtual void SetUp() override {
5948 InputDispatcherTest::SetUp();
5949
5950 /**
5951 * We don't need to enable input filter to test the injected event policy, but we enabled it
5952 * here to make the tests more realistic, since this policy only matters when inputfilter is
5953 * on.
5954 */
5955 mDispatcher->setInputFilterEnabled(true);
5956
5957 std::shared_ptr<InputApplicationHandle> application =
5958 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005959 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
5960 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005961
5962 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5963 mWindow->setFocusable(true);
5964 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5965 setFocusedWindow(mWindow);
5966 mWindow->consumeFocusEvent(true);
5967 }
5968
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005969 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5970 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005971 KeyEvent event;
5972
5973 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5974 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
5975 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00005976 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005977 const int32_t additionalPolicyFlags =
5978 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
5979 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005980 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005981 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5982 policyFlags | additionalPolicyFlags));
5983
5984 InputEvent* received = mWindow->consume();
5985 ASSERT_NE(nullptr, received);
5986 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005987 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005988 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
5989 ASSERT_EQ(flags, keyEvent.getFlags());
5990 }
5991
5992 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5993 int32_t flags) {
5994 MotionEvent event;
5995 PointerProperties pointerProperties[1];
5996 PointerCoords pointerCoords[1];
5997 pointerProperties[0].clear();
5998 pointerProperties[0].id = 0;
5999 pointerCoords[0].clear();
6000 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6001 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6002
6003 ui::Transform identityTransform;
6004 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6005 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6006 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6007 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6008 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006009 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006010 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006011 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6012
6013 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6014 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006015 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006016 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6017 policyFlags | additionalPolicyFlags));
6018
6019 InputEvent* received = mWindow->consume();
6020 ASSERT_NE(nullptr, received);
6021 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006022 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006023 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6024 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006025 }
6026
6027private:
6028 sp<FakeWindowHandle> mWindow;
6029};
6030
6031TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006032 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6033 // filter. Without it, the event will no different from a regularly injected event, and the
6034 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006035 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6036 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006037}
6038
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006039TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006040 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006041 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006042 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6043}
6044
6045TEST_F(InputFilterInjectionPolicyTest,
6046 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6047 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006048 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006049 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006050}
6051
6052TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006053 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6054 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006055}
6056
chaviwfd6d3512019-03-25 13:23:49 -07006057class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006058 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006059 InputDispatcherTest::SetUp();
6060
Chris Yea209fde2020-07-22 13:54:51 -07006061 std::shared_ptr<FakeApplicationHandle> application =
6062 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006063 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006064 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006065 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006066
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006067 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006068 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006069 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006070
6071 // Set focused application.
6072 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006073 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006074
6075 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006076 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006077 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006078 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006079 }
6080
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006081 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006082 InputDispatcherTest::TearDown();
6083
6084 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006085 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006086 }
6087
6088protected:
6089 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006090 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006091 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006092};
6093
6094// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6095// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6096// the onPointerDownOutsideFocus callback.
6097TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006098 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006099 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6100 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006101 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006102 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006103
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006104 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006105 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6106}
6107
6108// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6109// DOWN on the window that doesn't have focus. Ensure no window received the
6110// onPointerDownOutsideFocus callback.
6111TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006112 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006113 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006114 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006115 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006116
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006117 ASSERT_TRUE(mDispatcher->waitForIdle());
6118 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006119}
6120
6121// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6122// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6123TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006124 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6125 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006126 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006127 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006128
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006129 ASSERT_TRUE(mDispatcher->waitForIdle());
6130 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006131}
6132
6133// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6134// DOWN on the window that already has focus. Ensure no window received the
6135// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006136TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006137 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006138 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006139 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006140 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006141 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006142
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006143 ASSERT_TRUE(mDispatcher->waitForIdle());
6144 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006145}
6146
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006147// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6148// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6149TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6150 const MotionEvent event =
6151 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6152 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006153 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006154 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6155 .build();
6156 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6157 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6158 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6159
6160 ASSERT_TRUE(mDispatcher->waitForIdle());
6161 mFakePolicy->assertOnPointerDownWasNotCalled();
6162 // Ensure that the unfocused window did not receive any FOCUS events.
6163 mUnfocusedWindow->assertNoEvents();
6164}
6165
chaviwaf87b3e2019-10-01 16:59:28 -07006166// These tests ensures we can send touch events to a single client when there are multiple input
6167// windows that point to the same client token.
6168class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6169 virtual void SetUp() override {
6170 InputDispatcherTest::SetUp();
6171
Chris Yea209fde2020-07-22 13:54:51 -07006172 std::shared_ptr<FakeApplicationHandle> application =
6173 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006174 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6175 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006176 mWindow1->setFrame(Rect(0, 0, 100, 100));
6177
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006178 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6179 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006180 mWindow2->setFrame(Rect(100, 100, 200, 200));
6181
Arthur Hung72d8dc32020-03-28 00:48:39 +00006182 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006183 }
6184
6185protected:
6186 sp<FakeWindowHandle> mWindow1;
6187 sp<FakeWindowHandle> mWindow2;
6188
6189 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006190 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006191 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6192 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006193 }
6194
6195 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6196 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006197 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006198 InputEvent* event = window->consume();
6199
6200 ASSERT_NE(nullptr, event) << name.c_str()
6201 << ": consumer should have returned non-NULL event.";
6202
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006203 ASSERT_EQ(InputEventType::MOTION, event->getType())
6204 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006205
6206 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006207 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006208 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006209
6210 for (size_t i = 0; i < points.size(); i++) {
6211 float expectedX = points[i].x;
6212 float expectedY = points[i].y;
6213
6214 EXPECT_EQ(expectedX, motionEvent.getX(i))
6215 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6216 << ", got " << motionEvent.getX(i);
6217 EXPECT_EQ(expectedY, motionEvent.getY(i))
6218 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6219 << ", got " << motionEvent.getY(i);
6220 }
6221 }
chaviw9eaa22c2020-07-01 16:21:27 -07006222
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006223 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006224 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006225 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6226 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006227
6228 // Always consume from window1 since it's the window that has the InputReceiver
6229 consumeMotionEvent(mWindow1, action, expectedPoints);
6230 }
chaviwaf87b3e2019-10-01 16:59:28 -07006231};
6232
6233TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6234 // Touch Window 1
6235 PointF touchedPoint = {10, 10};
6236 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006237 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006238
6239 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006240 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006241
6242 // Touch Window 2
6243 touchedPoint = {150, 150};
6244 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006245 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006246}
6247
chaviw9eaa22c2020-07-01 16:21:27 -07006248TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6249 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006250 mWindow2->setWindowScale(0.5f, 0.5f);
6251
6252 // Touch Window 1
6253 PointF touchedPoint = {10, 10};
6254 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006255 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006256 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006257 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006258
6259 // Touch Window 2
6260 touchedPoint = {150, 150};
6261 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006262 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6263 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006264
chaviw9eaa22c2020-07-01 16:21:27 -07006265 // Update the transform so rotation is set
6266 mWindow2->setWindowTransform(0, -1, 1, 0);
6267 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6268 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006269}
6270
chaviw9eaa22c2020-07-01 16:21:27 -07006271TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006272 mWindow2->setWindowScale(0.5f, 0.5f);
6273
6274 // Touch Window 1
6275 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6276 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006277 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006278
6279 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006280 touchedPoints.push_back(PointF{150, 150});
6281 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006282 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006283
chaviw9eaa22c2020-07-01 16:21:27 -07006284 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006285 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006286 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006287
chaviw9eaa22c2020-07-01 16:21:27 -07006288 // Update the transform so rotation is set for Window 2
6289 mWindow2->setWindowTransform(0, -1, 1, 0);
6290 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006291 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006292}
6293
chaviw9eaa22c2020-07-01 16:21:27 -07006294TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006295 mWindow2->setWindowScale(0.5f, 0.5f);
6296
6297 // Touch Window 1
6298 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6299 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006300 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006301
6302 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006303 touchedPoints.push_back(PointF{150, 150});
6304 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006305
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006306 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006307
6308 // Move both windows
6309 touchedPoints = {{20, 20}, {175, 175}};
6310 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6311 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6312
chaviw9eaa22c2020-07-01 16:21:27 -07006313 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006314
chaviw9eaa22c2020-07-01 16:21:27 -07006315 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006316 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006317 expectedPoints.pop_back();
6318
6319 // Touch Window 2
6320 mWindow2->setWindowTransform(0, -1, 1, 0);
6321 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006322 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006323
6324 // Move both windows
6325 touchedPoints = {{20, 20}, {175, 175}};
6326 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6327 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6328
6329 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006330}
6331
6332TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6333 mWindow1->setWindowScale(0.5f, 0.5f);
6334
6335 // Touch Window 1
6336 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6337 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006338 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006339
6340 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006341 touchedPoints.push_back(PointF{150, 150});
6342 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006343
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006344 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006345
6346 // Move both windows
6347 touchedPoints = {{20, 20}, {175, 175}};
6348 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6349 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6350
chaviw9eaa22c2020-07-01 16:21:27 -07006351 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006352}
6353
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006354/**
6355 * When one of the windows is slippery, the touch should not slip into the other window with the
6356 * same input channel.
6357 */
6358TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6359 mWindow1->setSlippery(true);
6360 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6361
6362 // Touch down in window 1
6363 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6364 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6365 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6366
6367 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6368 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6369 // getting generated.
6370 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6371 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6372
6373 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6374}
6375
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07006376/**
6377 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
6378 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
6379 * that the pointer is hovering over may have a different transform.
6380 */
6381TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
6382 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6383
6384 // Start hover in window 1
6385 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN,
6386 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6387 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6388 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
6389
6390 // Move hover to window 2.
6391 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6392 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6393
6394 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
6395 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6396 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
6397}
6398
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006399class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6400 virtual void SetUp() override {
6401 InputDispatcherTest::SetUp();
6402
Chris Yea209fde2020-07-22 13:54:51 -07006403 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006404 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006405 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6406 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006407 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006408 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006409 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006410
6411 // Set focused application.
6412 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6413
6414 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006415 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006416 mWindow->consumeFocusEvent(true);
6417 }
6418
6419 virtual void TearDown() override {
6420 InputDispatcherTest::TearDown();
6421 mWindow.clear();
6422 }
6423
6424protected:
Chris Yea209fde2020-07-22 13:54:51 -07006425 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006426 sp<FakeWindowHandle> mWindow;
6427 static constexpr PointF WINDOW_LOCATION = {20, 20};
6428
6429 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006430 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006431 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6432 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006433 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006434 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6435 WINDOW_LOCATION));
6436 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006437
6438 sp<FakeWindowHandle> addSpyWindow() {
6439 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006440 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006441 spy->setTrustedOverlay(true);
6442 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006443 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006444 spy->setDispatchingTimeout(30ms);
6445 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6446 return spy;
6447 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006448};
6449
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006450// Send a tap and respond, which should not cause an ANR.
6451TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6452 tapOnWindow();
6453 mWindow->consumeMotionDown();
6454 mWindow->consumeMotionUp();
6455 ASSERT_TRUE(mDispatcher->waitForIdle());
6456 mFakePolicy->assertNotifyAnrWasNotCalled();
6457}
6458
6459// Send a regular key and respond, which should not cause an ANR.
6460TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006461 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006462 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6463 ASSERT_TRUE(mDispatcher->waitForIdle());
6464 mFakePolicy->assertNotifyAnrWasNotCalled();
6465}
6466
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006467TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6468 mWindow->setFocusable(false);
6469 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6470 mWindow->consumeFocusEvent(false);
6471
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006472 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006473 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6474 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6475 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006476 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006477 // Key will not go to window because we have no focused window.
6478 // The 'no focused window' ANR timer should start instead.
6479
6480 // Now, the focused application goes away.
6481 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6482 // The key should get dropped and there should be no ANR.
6483
6484 ASSERT_TRUE(mDispatcher->waitForIdle());
6485 mFakePolicy->assertNotifyAnrWasNotCalled();
6486}
6487
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006488// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006489// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6490// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006491TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006492 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006493 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6494 WINDOW_LOCATION));
6495
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006496 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6497 ASSERT_TRUE(sequenceNum);
6498 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006499 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006500
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006501 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006502 mWindow->consumeMotionEvent(
6503 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006504 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006505 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006506}
6507
6508// Send a key to the app and have the app not respond right away.
6509TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6510 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006511 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006512 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6513 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006514 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006515 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006516 ASSERT_TRUE(mDispatcher->waitForIdle());
6517}
6518
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006519// We have a focused application, but no focused window
6520TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006521 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006522 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6523 mWindow->consumeFocusEvent(false);
6524
6525 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006526 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006527 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6528 WINDOW_LOCATION));
6529 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6530 mDispatcher->waitForIdle();
6531 mFakePolicy->assertNotifyAnrWasNotCalled();
6532
6533 // Once a focused event arrives, we get an ANR for this application
6534 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6535 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006536 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006537 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6538 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006539 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006540 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006541 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006542 ASSERT_TRUE(mDispatcher->waitForIdle());
6543}
6544
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006545/**
6546 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6547 * there will not be an ANR.
6548 */
6549TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6550 mWindow->setFocusable(false);
6551 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6552 mWindow->consumeFocusEvent(false);
6553
6554 KeyEvent event;
6555 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6556 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6557
6558 // Define a valid key down event that is stale (too old).
6559 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6560 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006561 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006562
6563 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6564
6565 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006566 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006567 InputEventInjectionSync::WAIT_FOR_RESULT,
6568 INJECT_EVENT_TIMEOUT, policyFlags);
6569 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6570 << "Injection should fail because the event is stale";
6571
6572 ASSERT_TRUE(mDispatcher->waitForIdle());
6573 mFakePolicy->assertNotifyAnrWasNotCalled();
6574 mWindow->assertNoEvents();
6575}
6576
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006577// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006578// Make sure that we don't notify policy twice about the same ANR.
6579TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006580 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6582 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006583
6584 // Once a focused event arrives, we get an ANR for this application
6585 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6586 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006587 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006588 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6589 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006590 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006591 const std::chrono::duration appTimeout =
6592 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6593 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006594
Vishnu Naire4df8752022-09-08 09:17:55 -07006595 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006596 // ANR should not be raised again. It is up to policy to do that if it desires.
6597 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006598
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006599 // If we now get a focused window, the ANR should stop, but the policy handles that via
6600 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006601 ASSERT_TRUE(mDispatcher->waitForIdle());
6602}
6603
6604// We have a focused application, but no focused window
6605TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006606 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006607 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6608 mWindow->consumeFocusEvent(false);
6609
6610 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006611 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006612 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006613 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6614 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006615
Vishnu Naire4df8752022-09-08 09:17:55 -07006616 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6617 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006618
6619 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006620 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006621 ASSERT_TRUE(mDispatcher->waitForIdle());
6622 mWindow->assertNoEvents();
6623}
6624
6625/**
6626 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6627 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6628 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6629 * the ANR mechanism should still work.
6630 *
6631 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6632 * DOWN event, while not responding on the second one.
6633 */
6634TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6635 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6636 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6637 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6638 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6639 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006640 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006641
6642 // Now send ACTION_UP, with identical timestamp
6643 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6644 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6645 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6646 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006647 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006648
6649 // We have now sent down and up. Let's consume first event and then ANR on the second.
6650 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6651 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006652 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006653}
6654
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006655// A spy window can receive an ANR
6656TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6657 sp<FakeWindowHandle> spy = addSpyWindow();
6658
6659 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6660 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6661 WINDOW_LOCATION));
6662 mWindow->consumeMotionDown();
6663
6664 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6665 ASSERT_TRUE(sequenceNum);
6666 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006667 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006668
6669 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006670 spy->consumeMotionEvent(
6671 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006672 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006673 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006674}
6675
6676// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006677// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006678TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6679 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006680
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006681 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6682 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006683 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006685
6686 // Stuck on the ACTION_UP
6687 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006688 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006689
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006690 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006691 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006692 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6693 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006694
6695 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6696 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006697 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006698 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006699 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006700}
6701
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006702// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006703// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006704TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6705 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006706
6707 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006708 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6709 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006710
6711 mWindow->consumeMotionDown();
6712 // Stuck on the ACTION_UP
6713 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006714 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006715
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006716 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006717 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006718 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6719 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006720
6721 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6722 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006723 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006724 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006725 spy->assertNoEvents();
6726}
6727
6728TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6729 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6730
6731 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6732
6733 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6734 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6735 WINDOW_LOCATION));
6736
6737 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6738 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6739 ASSERT_TRUE(consumeSeq);
6740
Prabir Pradhanedd96402022-02-15 01:46:16 -08006741 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006742
6743 monitor.finishEvent(*consumeSeq);
6744 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6745
6746 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006747 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006748}
6749
6750// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6751// process events, you don't get an anr. When the window later becomes unresponsive again, you
6752// get an ANR again.
6753// 1. tap -> block on ACTION_UP -> receive ANR
6754// 2. consume all pending events (= queue becomes healthy again)
6755// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6756TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6757 tapOnWindow();
6758
6759 mWindow->consumeMotionDown();
6760 // Block on ACTION_UP
6761 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006762 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006763 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6764 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006765 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006766 mWindow->assertNoEvents();
6767
6768 tapOnWindow();
6769 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006770 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006771 mWindow->consumeMotionUp();
6772
6773 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006774 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006775 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006776 mWindow->assertNoEvents();
6777}
6778
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006779// If a connection remains unresponsive for a while, make sure policy is only notified once about
6780// it.
6781TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006782 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006783 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6784 WINDOW_LOCATION));
6785
6786 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006787 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006788 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006789 // 'notifyConnectionUnresponsive' should only be called once per connection
6790 mFakePolicy->assertNotifyAnrWasNotCalled();
6791 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006792 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006793 mWindow->consumeMotionEvent(
6794 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006795 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006796 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006797 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006798 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006799}
6800
6801/**
6802 * If a window is processing a motion event, and then a key event comes in, the key event should
6803 * not to to the focused window until the motion is processed.
6804 *
6805 * Warning!!!
6806 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6807 * and the injection timeout that we specify when injecting the key.
6808 * We must have the injection timeout (10ms) be smaller than
6809 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6810 *
6811 * If that value changes, this test should also change.
6812 */
6813TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
6814 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6815 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6816
6817 tapOnWindow();
6818 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6819 ASSERT_TRUE(downSequenceNum);
6820 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6821 ASSERT_TRUE(upSequenceNum);
6822 // Don't finish the events yet, and send a key
6823 // Injection will "succeed" because we will eventually give up and send the key to the focused
6824 // window even if motions are still being processed. But because the injection timeout is short,
6825 // we will receive INJECTION_TIMED_OUT as the result.
6826
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006827 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006828 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006829 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6830 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006831 // Key will not be sent to the window, yet, because the window is still processing events
6832 // and the key remains pending, waiting for the touch events to be processed
6833 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6834 ASSERT_FALSE(keySequenceNum);
6835
6836 std::this_thread::sleep_for(500ms);
6837 // if we wait long enough though, dispatcher will give up, and still send the key
6838 // to the focused window, even though we have not yet finished the motion event
6839 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6840 mWindow->finishEvent(*downSequenceNum);
6841 mWindow->finishEvent(*upSequenceNum);
6842}
6843
6844/**
6845 * If a window is processing a motion event, and then a key event comes in, the key event should
6846 * not go to the focused window until the motion is processed.
6847 * If then a new motion comes in, then the pending key event should be going to the currently
6848 * focused window right away.
6849 */
6850TEST_F(InputDispatcherSingleWindowAnr,
6851 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
6852 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6853 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6854
6855 tapOnWindow();
6856 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6857 ASSERT_TRUE(downSequenceNum);
6858 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6859 ASSERT_TRUE(upSequenceNum);
6860 // Don't finish the events yet, and send a key
6861 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006862 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006863 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6864 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006865 // At this point, key is still pending, and should not be sent to the application yet.
6866 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6867 ASSERT_FALSE(keySequenceNum);
6868
6869 // Now tap down again. It should cause the pending key to go to the focused window right away.
6870 tapOnWindow();
6871 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
6872 // the other events yet. We can finish events in any order.
6873 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
6874 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
6875 mWindow->consumeMotionDown();
6876 mWindow->consumeMotionUp();
6877 mWindow->assertNoEvents();
6878}
6879
6880class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
6881 virtual void SetUp() override {
6882 InputDispatcherTest::SetUp();
6883
Chris Yea209fde2020-07-22 13:54:51 -07006884 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006885 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006886 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
6887 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006888 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006889 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006890 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006891
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006892 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
6893 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006894 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006895 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006896
6897 // Set focused application.
6898 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07006899 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006900
6901 // Expect one focus window exist in display.
6902 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006903 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006904 mFocusedWindow->consumeFocusEvent(true);
6905 }
6906
6907 virtual void TearDown() override {
6908 InputDispatcherTest::TearDown();
6909
6910 mUnfocusedWindow.clear();
6911 mFocusedWindow.clear();
6912 }
6913
6914protected:
Chris Yea209fde2020-07-22 13:54:51 -07006915 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006916 sp<FakeWindowHandle> mUnfocusedWindow;
6917 sp<FakeWindowHandle> mFocusedWindow;
6918 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
6919 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
6920 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
6921
6922 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
6923
6924 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
6925
6926private:
6927 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006928 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006929 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6930 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006931 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006932 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6933 location));
6934 }
6935};
6936
6937// If we have 2 windows that are both unresponsive, the one with the shortest timeout
6938// should be ANR'd first.
6939TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006940 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006941 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6942 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006943 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006944 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006945 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006946 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006947 // We consumed all events, so no ANR
6948 ASSERT_TRUE(mDispatcher->waitForIdle());
6949 mFakePolicy->assertNotifyAnrWasNotCalled();
6950
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006951 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006952 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6953 FOCUSED_WINDOW_LOCATION));
6954 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
6955 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006956
6957 const std::chrono::duration timeout =
6958 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006959 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006960 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
6961 // sequence to make it consistent
6962 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006963 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006964 mFocusedWindow->consumeMotionDown();
6965 // This cancel is generated because the connection was unresponsive
6966 mFocusedWindow->consumeMotionCancel();
6967 mFocusedWindow->assertNoEvents();
6968 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006969 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006970 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6971 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006972 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006973}
6974
6975// If we have 2 windows with identical timeouts that are both unresponsive,
6976// it doesn't matter which order they should have ANR.
6977// But we should receive ANR for both.
6978TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
6979 // Set the timeout for unfocused window to match the focused window
6980 mUnfocusedWindow->setDispatchingTimeout(10ms);
6981 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6982
6983 tapOnFocusedWindow();
6984 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08006985 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
6986 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
6987 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006988
6989 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006990 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
6991 mFocusedWindow->getToken() == anrConnectionToken2);
6992 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
6993 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006994
6995 ASSERT_TRUE(mDispatcher->waitForIdle());
6996 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006997
6998 mFocusedWindow->consumeMotionDown();
6999 mFocusedWindow->consumeMotionUp();
7000 mUnfocusedWindow->consumeMotionOutside();
7001
Prabir Pradhanedd96402022-02-15 01:46:16 -08007002 sp<IBinder> responsiveToken1, responsiveToken2;
7003 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
7004 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007005
7006 // Both applications should be marked as responsive, in any order
7007 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
7008 mFocusedWindow->getToken() == responsiveToken2);
7009 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
7010 mUnfocusedWindow->getToken() == responsiveToken2);
7011 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007012}
7013
7014// If a window is already not responding, the second tap on the same window should be ignored.
7015// We should also log an error to account for the dropped event (not tested here).
7016// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7017TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7018 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007019 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007020 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007021 // Receive the events, but don't respond
7022 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7023 ASSERT_TRUE(downEventSequenceNum);
7024 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7025 ASSERT_TRUE(upEventSequenceNum);
7026 const std::chrono::duration timeout =
7027 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007028 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007029
7030 // Tap once again
7031 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007032 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007033 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7034 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007035 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007036 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7037 FOCUSED_WINDOW_LOCATION));
7038 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7039 // valid touch target
7040 mUnfocusedWindow->assertNoEvents();
7041
7042 // Consume the first tap
7043 mFocusedWindow->finishEvent(*downEventSequenceNum);
7044 mFocusedWindow->finishEvent(*upEventSequenceNum);
7045 ASSERT_TRUE(mDispatcher->waitForIdle());
7046 // The second tap did not go to the focused window
7047 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007048 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007049 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7050 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007051 mFakePolicy->assertNotifyAnrWasNotCalled();
7052}
7053
7054// If you tap outside of all windows, there will not be ANR
7055TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007056 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007057 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7058 LOCATION_OUTSIDE_ALL_WINDOWS));
7059 ASSERT_TRUE(mDispatcher->waitForIdle());
7060 mFakePolicy->assertNotifyAnrWasNotCalled();
7061}
7062
7063// Since the focused window is paused, tapping on it should not produce any events
7064TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7065 mFocusedWindow->setPaused(true);
7066 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7067
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007068 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007069 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7070 FOCUSED_WINDOW_LOCATION));
7071
7072 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7073 ASSERT_TRUE(mDispatcher->waitForIdle());
7074 // Should not ANR because the window is paused, and touches shouldn't go to it
7075 mFakePolicy->assertNotifyAnrWasNotCalled();
7076
7077 mFocusedWindow->assertNoEvents();
7078 mUnfocusedWindow->assertNoEvents();
7079}
7080
7081/**
7082 * If a window is processing a motion event, and then a key event comes in, the key event should
7083 * not to to the focused window until the motion is processed.
7084 * If a different window becomes focused at this time, the key should go to that window instead.
7085 *
7086 * Warning!!!
7087 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7088 * and the injection timeout that we specify when injecting the key.
7089 * We must have the injection timeout (10ms) be smaller than
7090 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7091 *
7092 * If that value changes, this test should also change.
7093 */
7094TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7095 // Set a long ANR timeout to prevent it from triggering
7096 mFocusedWindow->setDispatchingTimeout(2s);
7097 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7098
7099 tapOnUnfocusedWindow();
7100 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7101 ASSERT_TRUE(downSequenceNum);
7102 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7103 ASSERT_TRUE(upSequenceNum);
7104 // Don't finish the events yet, and send a key
7105 // Injection will succeed because we will eventually give up and send the key to the focused
7106 // window even if motions are still being processed.
7107
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007108 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007109 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7110 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007111 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007112 // Key will not be sent to the window, yet, because the window is still processing events
7113 // and the key remains pending, waiting for the touch events to be processed
7114 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7115 ASSERT_FALSE(keySequenceNum);
7116
7117 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007118 mFocusedWindow->setFocusable(false);
7119 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007120 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007121 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007122
7123 // Focus events should precede the key events
7124 mUnfocusedWindow->consumeFocusEvent(true);
7125 mFocusedWindow->consumeFocusEvent(false);
7126
7127 // Finish the tap events, which should unblock dispatcher
7128 mUnfocusedWindow->finishEvent(*downSequenceNum);
7129 mUnfocusedWindow->finishEvent(*upSequenceNum);
7130
7131 // Now that all queues are cleared and no backlog in the connections, the key event
7132 // can finally go to the newly focused "mUnfocusedWindow".
7133 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7134 mFocusedWindow->assertNoEvents();
7135 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007136 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007137}
7138
7139// When the touch stream is split across 2 windows, and one of them does not respond,
7140// then ANR should be raised and the touch should be canceled for the unresponsive window.
7141// The other window should not be affected by that.
7142TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7143 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007144 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7145 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7146 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007147 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007148 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007149
7150 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007151 mDispatcher->notifyMotion(
7152 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7153 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007154
7155 const std::chrono::duration timeout =
7156 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007157 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007158
7159 mUnfocusedWindow->consumeMotionDown();
7160 mFocusedWindow->consumeMotionDown();
7161 // Focused window may or may not receive ACTION_MOVE
7162 // But it should definitely receive ACTION_CANCEL due to the ANR
7163 InputEvent* event;
7164 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7165 ASSERT_TRUE(moveOrCancelSequenceNum);
7166 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7167 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007168 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007169 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7170 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7171 mFocusedWindow->consumeMotionCancel();
7172 } else {
7173 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7174 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007175 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007176 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7177 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007178
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007179 mUnfocusedWindow->assertNoEvents();
7180 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007181 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007182}
7183
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007184/**
7185 * If we have no focused window, and a key comes in, we start the ANR timer.
7186 * The focused application should add a focused window before the timer runs out to prevent ANR.
7187 *
7188 * If the user touches another application during this time, the key should be dropped.
7189 * Next, if a new focused window comes in, without toggling the focused application,
7190 * then no ANR should occur.
7191 *
7192 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7193 * but in some cases the policy may not update the focused application.
7194 */
7195TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7196 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7197 std::make_shared<FakeApplicationHandle>();
7198 focusedApplication->setDispatchingTimeout(60ms);
7199 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7200 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7201 mFocusedWindow->setFocusable(false);
7202
7203 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7204 mFocusedWindow->consumeFocusEvent(false);
7205
7206 // Send a key. The ANR timer should start because there is no focused window.
7207 // 'focusedApplication' will get blamed if this timer completes.
7208 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007209 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007210 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7211 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7212 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007214
7215 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7216 // then the injected touches won't cause the focused event to get dropped.
7217 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7218 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7219 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7220 // For this test, it means that the key would get delivered to the window once it becomes
7221 // focused.
7222 std::this_thread::sleep_for(10ms);
7223
7224 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007225 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7226 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7227 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007228
7229 // We do not consume the motion right away, because that would require dispatcher to first
7230 // process (== drop) the key event, and by that time, ANR will be raised.
7231 // Set the focused window first.
7232 mFocusedWindow->setFocusable(true);
7233 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7234 setFocusedWindow(mFocusedWindow);
7235 mFocusedWindow->consumeFocusEvent(true);
7236 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7237 // to another application. This could be a bug / behaviour in the policy.
7238
7239 mUnfocusedWindow->consumeMotionDown();
7240
7241 ASSERT_TRUE(mDispatcher->waitForIdle());
7242 // Should not ANR because we actually have a focused window. It was just added too slowly.
7243 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7244}
7245
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007246// These tests ensure we cannot send touch events to a window that's positioned behind a window
7247// that has feature NO_INPUT_CHANNEL.
7248// Layout:
7249// Top (closest to user)
7250// mNoInputWindow (above all windows)
7251// mBottomWindow
7252// Bottom (furthest from user)
7253class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7254 virtual void SetUp() override {
7255 InputDispatcherTest::SetUp();
7256
7257 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007258 mNoInputWindow =
7259 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7260 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007261 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007262 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007263 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7264 // It's perfectly valid for this window to not have an associated input channel
7265
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007266 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7267 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007268 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7269
7270 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7271 }
7272
7273protected:
7274 std::shared_ptr<FakeApplicationHandle> mApplication;
7275 sp<FakeWindowHandle> mNoInputWindow;
7276 sp<FakeWindowHandle> mBottomWindow;
7277};
7278
7279TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7280 PointF touchedPoint = {10, 10};
7281
Prabir Pradhan678438e2023-04-13 19:32:51 +00007282 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7283 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7284 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007285
7286 mNoInputWindow->assertNoEvents();
7287 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7288 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7289 // and therefore should prevent mBottomWindow from receiving touches
7290 mBottomWindow->assertNoEvents();
7291}
7292
7293/**
7294 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7295 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7296 */
7297TEST_F(InputDispatcherMultiWindowOcclusionTests,
7298 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007299 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7300 "Window with input channel and NO_INPUT_CHANNEL",
7301 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007302
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007303 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007304 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7305 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7306
7307 PointF touchedPoint = {10, 10};
7308
Prabir Pradhan678438e2023-04-13 19:32:51 +00007309 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7310 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7311 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007312
7313 mNoInputWindow->assertNoEvents();
7314 mBottomWindow->assertNoEvents();
7315}
7316
Vishnu Nair958da932020-08-21 17:12:37 -07007317class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7318protected:
7319 std::shared_ptr<FakeApplicationHandle> mApp;
7320 sp<FakeWindowHandle> mWindow;
7321 sp<FakeWindowHandle> mMirror;
7322
7323 virtual void SetUp() override {
7324 InputDispatcherTest::SetUp();
7325 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007326 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7327 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7328 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007329 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7330 mWindow->setFocusable(true);
7331 mMirror->setFocusable(true);
7332 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7333 }
7334};
7335
7336TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7337 // Request focus on a mirrored window
7338 setFocusedWindow(mMirror);
7339
7340 // window gets focused
7341 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007342 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7343 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007344 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7345}
7346
7347// A focused & mirrored window remains focused only if the window and its mirror are both
7348// focusable.
7349TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7350 setFocusedWindow(mMirror);
7351
7352 // window gets focused
7353 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007354 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7355 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007356 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007357 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7358 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007359 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7360
7361 mMirror->setFocusable(false);
7362 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7363
7364 // window loses focus since one of the windows associated with the token in not focusable
7365 mWindow->consumeFocusEvent(false);
7366
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007367 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7368 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007369 mWindow->assertNoEvents();
7370}
7371
7372// A focused & mirrored window remains focused until the window and its mirror both become
7373// invisible.
7374TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7375 setFocusedWindow(mMirror);
7376
7377 // window gets focused
7378 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007379 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7380 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007381 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007382 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7383 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007384 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7385
7386 mMirror->setVisible(false);
7387 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7388
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007389 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7390 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007391 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007392 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7393 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007394 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7395
7396 mWindow->setVisible(false);
7397 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7398
7399 // window loses focus only after all windows associated with the token become invisible.
7400 mWindow->consumeFocusEvent(false);
7401
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007402 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7403 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007404 mWindow->assertNoEvents();
7405}
7406
7407// A focused & mirrored window remains focused until both windows are removed.
7408TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7409 setFocusedWindow(mMirror);
7410
7411 // window gets focused
7412 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007413 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7414 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007415 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007416 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7417 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007418 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7419
7420 // single window is removed but the window token remains focused
7421 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7422
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007423 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7424 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007425 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007426 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7427 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007428 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7429
7430 // Both windows are removed
7431 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7432 mWindow->consumeFocusEvent(false);
7433
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007434 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7435 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007436 mWindow->assertNoEvents();
7437}
7438
7439// Focus request can be pending until one window becomes visible.
7440TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7441 // Request focus on an invisible mirror.
7442 mWindow->setVisible(false);
7443 mMirror->setVisible(false);
7444 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7445 setFocusedWindow(mMirror);
7446
7447 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007448 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007449 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7450 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007451
7452 mMirror->setVisible(true);
7453 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7454
7455 // window gets focused
7456 mWindow->consumeFocusEvent(true);
7457 // window gets the pending key event
7458 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7459}
Prabir Pradhan99987712020-11-10 18:43:05 -08007460
7461class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7462protected:
7463 std::shared_ptr<FakeApplicationHandle> mApp;
7464 sp<FakeWindowHandle> mWindow;
7465 sp<FakeWindowHandle> mSecondWindow;
7466
7467 void SetUp() override {
7468 InputDispatcherTest::SetUp();
7469 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007470 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007471 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007472 mSecondWindow =
7473 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007474 mSecondWindow->setFocusable(true);
7475
7476 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7477 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7478
7479 setFocusedWindow(mWindow);
7480 mWindow->consumeFocusEvent(true);
7481 }
7482
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007483 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007484 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007485 }
7486
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007487 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7488 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007489 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007490 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7491 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007492 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007493 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007494 }
7495};
7496
7497TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7498 // Ensure that capture cannot be obtained for unfocused windows.
7499 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7500 mFakePolicy->assertSetPointerCaptureNotCalled();
7501 mSecondWindow->assertNoEvents();
7502
7503 // Ensure that capture can be enabled from the focus window.
7504 requestAndVerifyPointerCapture(mWindow, true);
7505
7506 // Ensure that capture cannot be disabled from a window that does not have capture.
7507 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7508 mFakePolicy->assertSetPointerCaptureNotCalled();
7509
7510 // Ensure that capture can be disabled from the window with capture.
7511 requestAndVerifyPointerCapture(mWindow, false);
7512}
7513
7514TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007515 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007516
7517 setFocusedWindow(mSecondWindow);
7518
7519 // Ensure that the capture disabled event was sent first.
7520 mWindow->consumeCaptureEvent(false);
7521 mWindow->consumeFocusEvent(false);
7522 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007523 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007524
7525 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007526 notifyPointerCaptureChanged({});
7527 notifyPointerCaptureChanged(request);
7528 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007529 mWindow->assertNoEvents();
7530 mSecondWindow->assertNoEvents();
7531 mFakePolicy->assertSetPointerCaptureNotCalled();
7532}
7533
7534TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007535 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007536
7537 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007538 notifyPointerCaptureChanged({});
7539 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007540
7541 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007542 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007543 mWindow->consumeCaptureEvent(false);
7544 mWindow->assertNoEvents();
7545}
7546
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007547TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7548 requestAndVerifyPointerCapture(mWindow, true);
7549
7550 // The first window loses focus.
7551 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007552 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007553 mWindow->consumeCaptureEvent(false);
7554
7555 // Request Pointer Capture from the second window before the notification from InputReader
7556 // arrives.
7557 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007558 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007559
7560 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007561 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007562
7563 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007564 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007565
7566 mSecondWindow->consumeFocusEvent(true);
7567 mSecondWindow->consumeCaptureEvent(true);
7568}
7569
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007570TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7571 // App repeatedly enables and disables capture.
7572 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7573 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7574 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7575 mFakePolicy->assertSetPointerCaptureCalled(false);
7576 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7577 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7578
7579 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7580 // first request is now stale, this should do nothing.
7581 notifyPointerCaptureChanged(firstRequest);
7582 mWindow->assertNoEvents();
7583
7584 // InputReader notifies that the second request was enabled.
7585 notifyPointerCaptureChanged(secondRequest);
7586 mWindow->consumeCaptureEvent(true);
7587}
7588
Prabir Pradhan7092e262022-05-03 16:51:09 +00007589TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7590 requestAndVerifyPointerCapture(mWindow, true);
7591
7592 // App toggles pointer capture off and on.
7593 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7594 mFakePolicy->assertSetPointerCaptureCalled(false);
7595
7596 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7597 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7598
7599 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7600 // preceding "disable" request.
7601 notifyPointerCaptureChanged(enableRequest);
7602
7603 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7604 // any notifications.
7605 mWindow->assertNoEvents();
7606}
7607
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007608class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7609protected:
7610 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007611
7612 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7613 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7614
7615 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7616 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7617
7618 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7619 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7620 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7621 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7622 MAXIMUM_OBSCURING_OPACITY);
7623
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007624 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007625 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007626 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007627
7628 sp<FakeWindowHandle> mTouchWindow;
7629
7630 virtual void SetUp() override {
7631 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007632 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007633 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7634 }
7635
7636 virtual void TearDown() override {
7637 InputDispatcherTest::TearDown();
7638 mTouchWindow.clear();
7639 }
7640
chaviw3277faf2021-05-19 16:45:23 -05007641 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7642 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007643 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007644 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007645 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007646 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007647 return window;
7648 }
7649
7650 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7651 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7652 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007653 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007654 // Generate an arbitrary PID based on the UID
7655 window->setOwnerInfo(1777 + (uid % 10000), uid);
7656 return window;
7657 }
7658
7659 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007660 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7661 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7662 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007663 }
7664};
7665
7666TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007667 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007668 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007669 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007670
7671 touch();
7672
7673 mTouchWindow->assertNoEvents();
7674}
7675
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007676TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007677 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7678 const sp<FakeWindowHandle>& w =
7679 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7680 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7681
7682 touch();
7683
7684 mTouchWindow->assertNoEvents();
7685}
7686
7687TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007688 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7689 const sp<FakeWindowHandle>& w =
7690 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7691 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7692
7693 touch();
7694
7695 w->assertNoEvents();
7696}
7697
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007698TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007699 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7700 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007701
7702 touch();
7703
7704 mTouchWindow->consumeAnyMotionDown();
7705}
7706
7707TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007708 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007709 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007710 w->setFrame(Rect(0, 0, 50, 50));
7711 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007712
7713 touch({PointF{100, 100}});
7714
7715 mTouchWindow->consumeAnyMotionDown();
7716}
7717
7718TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007719 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007720 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007721 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7722
7723 touch();
7724
7725 mTouchWindow->consumeAnyMotionDown();
7726}
7727
7728TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7729 const sp<FakeWindowHandle>& w =
7730 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7731 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007732
7733 touch();
7734
7735 mTouchWindow->consumeAnyMotionDown();
7736}
7737
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007738TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7739 const sp<FakeWindowHandle>& w =
7740 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7741 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7742
7743 touch();
7744
7745 w->assertNoEvents();
7746}
7747
7748/**
7749 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7750 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7751 * window, the occluding window will still receive ACTION_OUTSIDE event.
7752 */
7753TEST_F(InputDispatcherUntrustedTouchesTest,
7754 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7755 const sp<FakeWindowHandle>& w =
7756 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007757 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007758 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7759
7760 touch();
7761
7762 w->consumeMotionOutside();
7763}
7764
7765TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
7766 const sp<FakeWindowHandle>& w =
7767 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007768 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007769 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7770
7771 touch();
7772
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007773 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007774}
7775
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007776TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007777 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007778 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7779 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007780 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7781
7782 touch();
7783
7784 mTouchWindow->consumeAnyMotionDown();
7785}
7786
7787TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
7788 const sp<FakeWindowHandle>& w =
7789 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7790 MAXIMUM_OBSCURING_OPACITY);
7791 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007792
7793 touch();
7794
7795 mTouchWindow->consumeAnyMotionDown();
7796}
7797
7798TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007799 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007800 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7801 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007802 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7803
7804 touch();
7805
7806 mTouchWindow->assertNoEvents();
7807}
7808
7809TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
7810 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
7811 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007812 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7813 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007814 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007815 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7816 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007817 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7818
7819 touch();
7820
7821 mTouchWindow->assertNoEvents();
7822}
7823
7824TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
7825 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
7826 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007827 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7828 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007829 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007830 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7831 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007832 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7833
7834 touch();
7835
7836 mTouchWindow->consumeAnyMotionDown();
7837}
7838
7839TEST_F(InputDispatcherUntrustedTouchesTest,
7840 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
7841 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007842 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7843 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007844 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007845 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7846 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007847 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7848
7849 touch();
7850
7851 mTouchWindow->consumeAnyMotionDown();
7852}
7853
7854TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
7855 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007856 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7857 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007858 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007859 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7860 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007861 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007862
7863 touch();
7864
7865 mTouchWindow->assertNoEvents();
7866}
7867
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007868TEST_F(InputDispatcherUntrustedTouchesTest,
7869 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
7870 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007871 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7872 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007873 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007874 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7875 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007876 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7877
7878 touch();
7879
7880 mTouchWindow->assertNoEvents();
7881}
7882
7883TEST_F(InputDispatcherUntrustedTouchesTest,
7884 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
7885 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007886 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7887 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007888 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007889 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7890 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007891 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7892
7893 touch();
7894
7895 mTouchWindow->consumeAnyMotionDown();
7896}
7897
7898TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
7899 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007900 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7901 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007902 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7903
7904 touch();
7905
7906 mTouchWindow->consumeAnyMotionDown();
7907}
7908
7909TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
7910 const sp<FakeWindowHandle>& w =
7911 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
7912 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7913
7914 touch();
7915
7916 mTouchWindow->consumeAnyMotionDown();
7917}
7918
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007919TEST_F(InputDispatcherUntrustedTouchesTest,
7920 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
7921 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7922 const sp<FakeWindowHandle>& w =
7923 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
7924 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7925
7926 touch();
7927
7928 mTouchWindow->assertNoEvents();
7929}
7930
7931TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
7932 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7933 const sp<FakeWindowHandle>& w =
7934 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
7935 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7936
7937 touch();
7938
7939 mTouchWindow->consumeAnyMotionDown();
7940}
7941
7942TEST_F(InputDispatcherUntrustedTouchesTest,
7943 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
7944 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
7945 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007946 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7947 OPACITY_ABOVE_THRESHOLD);
7948 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7949
7950 touch();
7951
7952 mTouchWindow->consumeAnyMotionDown();
7953}
7954
7955TEST_F(InputDispatcherUntrustedTouchesTest,
7956 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
7957 const sp<FakeWindowHandle>& w1 =
7958 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7959 OPACITY_BELOW_THRESHOLD);
7960 const sp<FakeWindowHandle>& w2 =
7961 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7962 OPACITY_BELOW_THRESHOLD);
7963 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7964
7965 touch();
7966
7967 mTouchWindow->assertNoEvents();
7968}
7969
7970/**
7971 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
7972 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
7973 * (which alone would result in allowing touches) does not affect the blocking behavior.
7974 */
7975TEST_F(InputDispatcherUntrustedTouchesTest,
7976 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
7977 const sp<FakeWindowHandle>& wB =
7978 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7979 OPACITY_BELOW_THRESHOLD);
7980 const sp<FakeWindowHandle>& wC =
7981 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7982 OPACITY_BELOW_THRESHOLD);
7983 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7984
7985 touch();
7986
7987 mTouchWindow->assertNoEvents();
7988}
7989
7990/**
7991 * This test is testing that a window from a different UID but with same application token doesn't
7992 * block the touch. Apps can share the application token for close UI collaboration for example.
7993 */
7994TEST_F(InputDispatcherUntrustedTouchesTest,
7995 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
7996 const sp<FakeWindowHandle>& w =
7997 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7998 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007999 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8000
8001 touch();
8002
8003 mTouchWindow->consumeAnyMotionDown();
8004}
8005
arthurhungb89ccb02020-12-30 16:19:01 +08008006class InputDispatcherDragTests : public InputDispatcherTest {
8007protected:
8008 std::shared_ptr<FakeApplicationHandle> mApp;
8009 sp<FakeWindowHandle> mWindow;
8010 sp<FakeWindowHandle> mSecondWindow;
8011 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008012 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008013 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8014 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008015
8016 void SetUp() override {
8017 InputDispatcherTest::SetUp();
8018 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008019 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008020 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008021
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008022 mSecondWindow =
8023 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008024 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008025
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008026 mSpyWindow =
8027 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008028 mSpyWindow->setSpy(true);
8029 mSpyWindow->setTrustedOverlay(true);
8030 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8031
arthurhungb89ccb02020-12-30 16:19:01 +08008032 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008033 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008034 }
8035
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008036 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8037 switch (fromSource) {
8038 case AINPUT_SOURCE_TOUCHSCREEN:
8039 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8040 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8041 ADISPLAY_ID_DEFAULT, {50, 50}))
8042 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8043 break;
8044 case AINPUT_SOURCE_STYLUS:
8045 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8046 injectMotionEvent(
8047 mDispatcher,
8048 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8049 AINPUT_SOURCE_STYLUS)
8050 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008051 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008052 .x(50)
8053 .y(50))
8054 .build()));
8055 break;
8056 case AINPUT_SOURCE_MOUSE:
8057 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8058 injectMotionEvent(
8059 mDispatcher,
8060 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8061 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8062 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008063 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008064 .x(50)
8065 .y(50))
8066 .build()));
8067 break;
8068 default:
8069 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8070 }
arthurhungb89ccb02020-12-30 16:19:01 +08008071
8072 // Window should receive motion event.
8073 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008074 // Spy window should also receive motion event
8075 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008076 }
8077
8078 // Start performing drag, we will create a drag window and transfer touch to it.
8079 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8080 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008081 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008082 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008083 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008084 }
arthurhungb89ccb02020-12-30 16:19:01 +08008085
8086 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008087 mDragWindow =
8088 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008089 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008090 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008091 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008092
8093 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008094 bool transferred =
8095 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008096 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008097 if (transferred) {
8098 mWindow->consumeMotionCancel();
8099 mDragWindow->consumeMotionDown();
8100 }
8101 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008102 }
8103};
8104
8105TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008106 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008107
8108 // Move on window.
8109 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8110 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8111 ADISPLAY_ID_DEFAULT, {50, 50}))
8112 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8113 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8114 mWindow->consumeDragEvent(false, 50, 50);
8115 mSecondWindow->assertNoEvents();
8116
8117 // Move to another window.
8118 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8119 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8120 ADISPLAY_ID_DEFAULT, {150, 50}))
8121 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8122 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8123 mWindow->consumeDragEvent(true, 150, 50);
8124 mSecondWindow->consumeDragEvent(false, 50, 50);
8125
8126 // Move back to original window.
8127 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8128 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8129 ADISPLAY_ID_DEFAULT, {50, 50}))
8130 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8131 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8132 mWindow->consumeDragEvent(false, 50, 50);
8133 mSecondWindow->consumeDragEvent(true, -50, 50);
8134
8135 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8136 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8137 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8138 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8139 mWindow->assertNoEvents();
8140 mSecondWindow->assertNoEvents();
8141}
8142
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008143TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008144 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008145
8146 // No cancel event after drag start
8147 mSpyWindow->assertNoEvents();
8148
8149 const MotionEvent secondFingerDownEvent =
8150 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8151 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008152 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8153 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008154 .build();
8155 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8156 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8157 InputEventInjectionSync::WAIT_FOR_RESULT))
8158 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8159
8160 // Receives cancel for first pointer after next pointer down
8161 mSpyWindow->consumeMotionCancel();
8162 mSpyWindow->consumeMotionDown();
8163
8164 mSpyWindow->assertNoEvents();
8165}
8166
arthurhungf452d0b2021-01-06 00:19:52 +08008167TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008168 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008169
8170 // Move on window.
8171 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8172 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8173 ADISPLAY_ID_DEFAULT, {50, 50}))
8174 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8175 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8176 mWindow->consumeDragEvent(false, 50, 50);
8177 mSecondWindow->assertNoEvents();
8178
8179 // Move to another window.
8180 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8181 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8182 ADISPLAY_ID_DEFAULT, {150, 50}))
8183 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8184 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8185 mWindow->consumeDragEvent(true, 150, 50);
8186 mSecondWindow->consumeDragEvent(false, 50, 50);
8187
8188 // drop to another window.
8189 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8190 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8191 {150, 50}))
8192 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8193 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8194 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8195 mWindow->assertNoEvents();
8196 mSecondWindow->assertNoEvents();
8197}
8198
arthurhung6d4bed92021-03-17 11:59:33 +08008199TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008200 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008201
8202 // Move on window and keep button pressed.
8203 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8204 injectMotionEvent(mDispatcher,
8205 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8206 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008207 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008208 .build()))
8209 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8210 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8211 mWindow->consumeDragEvent(false, 50, 50);
8212 mSecondWindow->assertNoEvents();
8213
8214 // Move to another window and release button, expect to drop item.
8215 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8216 injectMotionEvent(mDispatcher,
8217 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8218 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008219 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008220 .build()))
8221 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8222 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8223 mWindow->assertNoEvents();
8224 mSecondWindow->assertNoEvents();
8225 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8226
8227 // nothing to the window.
8228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8229 injectMotionEvent(mDispatcher,
8230 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8231 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008232 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008233 .build()))
8234 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8235 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8236 mWindow->assertNoEvents();
8237 mSecondWindow->assertNoEvents();
8238}
8239
Arthur Hung54745652022-04-20 07:17:41 +00008240TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008241 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008242
8243 // Set second window invisible.
8244 mSecondWindow->setVisible(false);
8245 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8246
8247 // Move on window.
8248 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8249 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8250 ADISPLAY_ID_DEFAULT, {50, 50}))
8251 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8252 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8253 mWindow->consumeDragEvent(false, 50, 50);
8254 mSecondWindow->assertNoEvents();
8255
8256 // Move to another window.
8257 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8258 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8259 ADISPLAY_ID_DEFAULT, {150, 50}))
8260 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8261 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8262 mWindow->consumeDragEvent(true, 150, 50);
8263 mSecondWindow->assertNoEvents();
8264
8265 // drop to another window.
8266 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8267 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8268 {150, 50}))
8269 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8270 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8271 mFakePolicy->assertDropTargetEquals(nullptr);
8272 mWindow->assertNoEvents();
8273 mSecondWindow->assertNoEvents();
8274}
8275
Arthur Hung54745652022-04-20 07:17:41 +00008276TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008277 // Ensure window could track pointerIds if it didn't support split touch.
8278 mWindow->setPreventSplitting(true);
8279
Arthur Hung54745652022-04-20 07:17:41 +00008280 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8281 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8282 {50, 50}))
8283 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8284 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8285
8286 const MotionEvent secondFingerDownEvent =
8287 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8288 .displayId(ADISPLAY_ID_DEFAULT)
8289 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008290 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8291 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008292 .build();
8293 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8294 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8295 InputEventInjectionSync::WAIT_FOR_RESULT))
8296 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008297 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008298
8299 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008300 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008301}
8302
8303TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8304 // First down on second window.
8305 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8306 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8307 {150, 50}))
8308 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8309
8310 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8311
8312 // Second down on first window.
8313 const MotionEvent secondFingerDownEvent =
8314 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8315 .displayId(ADISPLAY_ID_DEFAULT)
8316 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008317 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8318 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008319 .build();
8320 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8321 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8322 InputEventInjectionSync::WAIT_FOR_RESULT))
8323 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8324 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8325
8326 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008327 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008328
8329 // Move on window.
8330 const MotionEvent secondFingerMoveEvent =
8331 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8332 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008333 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8334 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008335 .build();
8336 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8337 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8338 InputEventInjectionSync::WAIT_FOR_RESULT));
8339 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8340 mWindow->consumeDragEvent(false, 50, 50);
8341 mSecondWindow->consumeMotionMove();
8342
8343 // Release the drag pointer should perform drop.
8344 const MotionEvent secondFingerUpEvent =
8345 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8346 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008347 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8348 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008349 .build();
8350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8351 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8352 InputEventInjectionSync::WAIT_FOR_RESULT));
8353 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8354 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8355 mWindow->assertNoEvents();
8356 mSecondWindow->consumeMotionMove();
8357}
8358
Arthur Hung3915c1f2022-05-31 07:17:17 +00008359TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008360 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008361
8362 // Update window of second display.
8363 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008364 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008365 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8366
8367 // Let second display has a touch state.
8368 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8369 injectMotionEvent(mDispatcher,
8370 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8371 AINPUT_SOURCE_TOUCHSCREEN)
8372 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008373 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008374 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008375 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008376 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008377 // Update window again.
8378 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8379
8380 // Move on window.
8381 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8382 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8383 ADISPLAY_ID_DEFAULT, {50, 50}))
8384 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8385 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8386 mWindow->consumeDragEvent(false, 50, 50);
8387 mSecondWindow->assertNoEvents();
8388
8389 // Move to another window.
8390 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8391 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8392 ADISPLAY_ID_DEFAULT, {150, 50}))
8393 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8394 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8395 mWindow->consumeDragEvent(true, 150, 50);
8396 mSecondWindow->consumeDragEvent(false, 50, 50);
8397
8398 // drop to another window.
8399 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8400 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8401 {150, 50}))
8402 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8403 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8404 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8405 mWindow->assertNoEvents();
8406 mSecondWindow->assertNoEvents();
8407}
8408
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008409TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8410 startDrag(true, AINPUT_SOURCE_MOUSE);
8411 // Move on window.
8412 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8413 injectMotionEvent(mDispatcher,
8414 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8415 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8416 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008417 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008418 .x(50)
8419 .y(50))
8420 .build()))
8421 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8422 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8423 mWindow->consumeDragEvent(false, 50, 50);
8424 mSecondWindow->assertNoEvents();
8425
8426 // Move to another window.
8427 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8428 injectMotionEvent(mDispatcher,
8429 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8430 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8431 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008432 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008433 .x(150)
8434 .y(50))
8435 .build()))
8436 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8437 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8438 mWindow->consumeDragEvent(true, 150, 50);
8439 mSecondWindow->consumeDragEvent(false, 50, 50);
8440
8441 // drop to another window.
8442 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8443 injectMotionEvent(mDispatcher,
8444 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8445 .buttonState(0)
8446 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008447 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008448 .x(150)
8449 .y(50))
8450 .build()))
8451 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8452 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8453 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8454 mWindow->assertNoEvents();
8455 mSecondWindow->assertNoEvents();
8456}
8457
Vishnu Nair062a8672021-09-03 16:07:44 -07008458class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8459
8460TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8461 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008462 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8463 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008464 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008465 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8466 window->setFocusable(true);
8467 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8468 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008469 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008470
8471 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008472 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008473 window->assertNoEvents();
8474
Prabir Pradhan678438e2023-04-13 19:32:51 +00008475 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8476 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008477 window->assertNoEvents();
8478
8479 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008480 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008481 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8482
Prabir Pradhan678438e2023-04-13 19:32:51 +00008483 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008484 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8485
Prabir Pradhan678438e2023-04-13 19:32:51 +00008486 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8487 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008488 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8489 window->assertNoEvents();
8490}
8491
8492TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8493 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8494 std::make_shared<FakeApplicationHandle>();
8495 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008496 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8497 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008498 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8499 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008500 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008501 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008502 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8503 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008504 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008505 window->setOwnerInfo(222, 222);
8506 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8507 window->setFocusable(true);
8508 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8509 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008510 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008511
8512 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008513 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008514 window->assertNoEvents();
8515
Prabir Pradhan678438e2023-04-13 19:32:51 +00008516 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8517 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008518 window->assertNoEvents();
8519
8520 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008521 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008522 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8523
Prabir Pradhan678438e2023-04-13 19:32:51 +00008524 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008525 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8526
Prabir Pradhan678438e2023-04-13 19:32:51 +00008527 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8528 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008529 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8530 window->assertNoEvents();
8531}
8532
8533TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8534 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8535 std::make_shared<FakeApplicationHandle>();
8536 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008537 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8538 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008539 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8540 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008541 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008542 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008543 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8544 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008545 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008546 window->setOwnerInfo(222, 222);
8547 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8548 window->setFocusable(true);
8549 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8550 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008551 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008552
8553 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008554 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008555 window->assertNoEvents();
8556
Prabir Pradhan678438e2023-04-13 19:32:51 +00008557 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8558 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008559 window->assertNoEvents();
8560
8561 // When the window is no longer obscured because it went on top, it should get input
8562 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8563
Prabir Pradhan678438e2023-04-13 19:32:51 +00008564 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008565 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8566
Prabir Pradhan678438e2023-04-13 19:32:51 +00008567 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8568 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008569 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8570 window->assertNoEvents();
8571}
8572
Antonio Kantekf16f2832021-09-28 04:39:20 +00008573class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8574protected:
8575 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008576 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008577 sp<FakeWindowHandle> mWindow;
8578 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008579 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008580
8581 void SetUp() override {
8582 InputDispatcherTest::SetUp();
8583
8584 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008585 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008586 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008587 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008588 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008589 mSecondWindow =
8590 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008591 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008592 mThirdWindow =
8593 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8594 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8595 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008596
8597 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008598 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8599 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8600 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008601 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008602
Antonio Kantek15beb512022-06-13 22:35:41 +00008603 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008604 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008605 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008606 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8607 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008608 mThirdWindow->assertNoEvents();
8609 }
8610
8611 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8612 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008613 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008614 SECOND_DISPLAY_ID)) {
8615 mWindow->assertNoEvents();
8616 mSecondWindow->assertNoEvents();
8617 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008618 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008619 }
8620
Antonio Kantek15beb512022-06-13 22:35:41 +00008621 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8622 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008623 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8624 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008625 mWindow->consumeTouchModeEvent(inTouchMode);
8626 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008627 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008628 }
8629};
8630
Antonio Kantek26defcf2022-02-08 01:12:27 +00008631TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008632 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008633 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8634 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008635 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008636}
8637
Antonio Kantek26defcf2022-02-08 01:12:27 +00008638TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8639 const WindowInfo& windowInfo = *mWindow->getInfo();
8640 int32_t ownerPid = windowInfo.ownerPid;
8641 int32_t ownerUid = windowInfo.ownerUid;
8642 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8643 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008644 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008645 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008646 mWindow->assertNoEvents();
8647 mSecondWindow->assertNoEvents();
8648}
8649
8650TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8651 const WindowInfo& windowInfo = *mWindow->getInfo();
8652 int32_t ownerPid = windowInfo.ownerPid;
8653 int32_t ownerUid = windowInfo.ownerUid;
8654 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008655 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008656 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008657}
8658
Antonio Kantekf16f2832021-09-28 04:39:20 +00008659TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008660 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008661 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8662 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008663 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008664 mWindow->assertNoEvents();
8665 mSecondWindow->assertNoEvents();
8666}
8667
Antonio Kantek15beb512022-06-13 22:35:41 +00008668TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8669 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8670 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8671 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008672 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008673 mWindow->assertNoEvents();
8674 mSecondWindow->assertNoEvents();
8675 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8676}
8677
Antonio Kantek48710e42022-03-24 14:19:30 -07008678TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8679 // Interact with the window first.
8680 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8681 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8682 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8683
8684 // Then remove focus.
8685 mWindow->setFocusable(false);
8686 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8687
8688 // Assert that caller can switch touch mode by owning one of the last interacted window.
8689 const WindowInfo& windowInfo = *mWindow->getInfo();
8690 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8691 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008692 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008693}
8694
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008695class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8696public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008697 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008698 std::shared_ptr<FakeApplicationHandle> application =
8699 std::make_shared<FakeApplicationHandle>();
8700 std::string name = "Fake Spy ";
8701 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008702 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8703 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008704 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008705 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008706 return spy;
8707 }
8708
8709 sp<FakeWindowHandle> createForeground() {
8710 std::shared_ptr<FakeApplicationHandle> application =
8711 std::make_shared<FakeApplicationHandle>();
8712 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008713 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8714 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008715 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008716 return window;
8717 }
8718
8719private:
8720 int mSpyCount{0};
8721};
8722
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008723using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008724/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008725 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8726 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008727TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8728 ScopedSilentDeath _silentDeath;
8729
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008730 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008731 spy->setTrustedOverlay(false);
8732 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8733 ".* not a trusted overlay");
8734}
8735
8736/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008737 * Input injection into a display with a spy window but no foreground windows should succeed.
8738 */
8739TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008740 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008741 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8742
8743 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8744 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8745 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8746 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8747}
8748
8749/**
8750 * Verify the order in which different input windows receive events. The touched foreground window
8751 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8752 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8753 * receive events before ones belows it.
8754 *
8755 * Here, we set up a scenario with four windows in the following Z order from the top:
8756 * spy1, spy2, window, spy3.
8757 * We then inject an event and verify that the foreground "window" receives it first, followed by
8758 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
8759 * window.
8760 */
8761TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
8762 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008763 auto spy1 = createSpy();
8764 auto spy2 = createSpy();
8765 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008766 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
8767 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
8768 const size_t numChannels = channels.size();
8769
Michael Wright8e9a8562022-02-09 13:44:29 +00008770 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008771 if (!epollFd.ok()) {
8772 FAIL() << "Failed to create epoll fd";
8773 }
8774
8775 for (size_t i = 0; i < numChannels; i++) {
8776 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
8777 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
8778 FAIL() << "Failed to add fd to epoll";
8779 }
8780 }
8781
8782 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8783 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8784 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8785
8786 std::vector<size_t> eventOrder;
8787 std::vector<struct epoll_event> events(numChannels);
8788 for (;;) {
8789 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
8790 (100ms).count());
8791 if (nFds < 0) {
8792 FAIL() << "Failed to call epoll_wait";
8793 }
8794 if (nFds == 0) {
8795 break; // epoll_wait timed out
8796 }
8797 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07008798 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07008799 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008800 channels[i]->consumeMotionDown();
8801 }
8802 }
8803
8804 // Verify the order in which the events were received.
8805 EXPECT_EQ(3u, eventOrder.size());
8806 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
8807 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
8808 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
8809}
8810
8811/**
8812 * A spy window using the NOT_TOUCHABLE flag does not receive events.
8813 */
8814TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
8815 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008816 auto spy = createSpy();
8817 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008818 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8819
8820 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8821 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8822 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8823 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8824 spy->assertNoEvents();
8825}
8826
8827/**
8828 * A spy window will only receive gestures that originate within its touchable region. Gestures that
8829 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
8830 * to the window.
8831 */
8832TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
8833 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008834 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008835 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
8836 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8837
8838 // Inject an event outside the spy window's touchable region.
8839 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8840 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8841 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8842 window->consumeMotionDown();
8843 spy->assertNoEvents();
8844 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8845 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8846 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8847 window->consumeMotionUp();
8848 spy->assertNoEvents();
8849
8850 // Inject an event inside the spy window's touchable region.
8851 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8852 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8853 {5, 10}))
8854 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8855 window->consumeMotionDown();
8856 spy->consumeMotionDown();
8857}
8858
8859/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008860 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008861 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008862 */
8863TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
8864 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008865 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008866 auto spy = createSpy();
8867 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008868 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008869 spy->setFrame(Rect{0, 0, 20, 20});
8870 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8871
8872 // Inject an event outside the spy window's frame and touchable region.
8873 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008874 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8875 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008876 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8877 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008878 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008879}
8880
8881/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008882 * Even when a spy window spans over multiple foreground windows, the spy should receive all
8883 * pointers that are down within its bounds.
8884 */
8885TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
8886 auto windowLeft = createForeground();
8887 windowLeft->setFrame({0, 0, 100, 200});
8888 auto windowRight = createForeground();
8889 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008890 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008891 spy->setFrame({0, 0, 200, 200});
8892 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
8893
8894 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8895 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8896 {50, 50}))
8897 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8898 windowLeft->consumeMotionDown();
8899 spy->consumeMotionDown();
8900
8901 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008902 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008903 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008904 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8905 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008906 .build();
8907 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8908 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8909 InputEventInjectionSync::WAIT_FOR_RESULT))
8910 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8911 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00008912 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008913}
8914
8915/**
8916 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
8917 * the spy should receive the second pointer with ACTION_DOWN.
8918 */
8919TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
8920 auto window = createForeground();
8921 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008922 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008923 spyRight->setFrame({100, 0, 200, 200});
8924 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
8925
8926 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8927 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8928 {50, 50}))
8929 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8930 window->consumeMotionDown();
8931 spyRight->assertNoEvents();
8932
8933 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008934 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008935 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008936 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8937 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008938 .build();
8939 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8940 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8941 InputEventInjectionSync::WAIT_FOR_RESULT))
8942 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008943 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008944 spyRight->consumeMotionDown();
8945}
8946
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008947/**
8948 * The spy window should not be able to affect whether or not touches are split. Only the foreground
8949 * windows should be allowed to control split touch.
8950 */
8951TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008952 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008953 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008954 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008955 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008956
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008957 auto window = createForeground();
8958 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008959
8960 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8961
8962 // First finger down, no window touched.
8963 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8964 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8965 {100, 200}))
8966 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8967 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8968 window->assertNoEvents();
8969
8970 // Second finger down on window, the window should receive touch down.
8971 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008972 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008973 .displayId(ADISPLAY_ID_DEFAULT)
8974 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008975 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8976 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008977 .build();
8978 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8979 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8980 InputEventInjectionSync::WAIT_FOR_RESULT))
8981 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8982
8983 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00008984 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008985}
8986
8987/**
8988 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
8989 * do not receive key events.
8990 */
8991TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008992 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008993 spy->setFocusable(false);
8994
8995 auto window = createForeground();
8996 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8997 setFocusedWindow(window);
8998 window->consumeFocusEvent(true);
8999
9000 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
9001 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9002 window->consumeKeyDown(ADISPLAY_ID_NONE);
9003
9004 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
9005 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9006 window->consumeKeyUp(ADISPLAY_ID_NONE);
9007
9008 spy->assertNoEvents();
9009}
9010
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009011using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9012
9013/**
9014 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9015 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9016 */
9017TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9018 auto window = createForeground();
9019 auto spy1 = createSpy();
9020 auto spy2 = createSpy();
9021 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
9022
9023 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9024 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9025 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9026 window->consumeMotionDown();
9027 spy1->consumeMotionDown();
9028 spy2->consumeMotionDown();
9029
9030 // Pilfer pointers from the second spy window.
9031 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9032 spy2->assertNoEvents();
9033 spy1->consumeMotionCancel();
9034 window->consumeMotionCancel();
9035
9036 // The rest of the gesture should only be sent to the second spy window.
9037 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9038 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9039 ADISPLAY_ID_DEFAULT))
9040 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9041 spy2->consumeMotionMove();
9042 spy1->assertNoEvents();
9043 window->assertNoEvents();
9044}
9045
9046/**
9047 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9048 * in the middle of the gesture.
9049 */
9050TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9051 auto window = createForeground();
9052 auto spy = createSpy();
9053 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9054
9055 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9056 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9057 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9058 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9059 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9060
9061 window->releaseChannel();
9062
9063 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9064
9065 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9066 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9067 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9068 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9069}
9070
9071/**
9072 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9073 * the spy, but not to any other windows.
9074 */
9075TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9076 auto spy = createSpy();
9077 auto window = createForeground();
9078
9079 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9080
9081 // First finger down on the window and the spy.
9082 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9083 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9084 {100, 200}))
9085 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9086 spy->consumeMotionDown();
9087 window->consumeMotionDown();
9088
9089 // Spy window pilfers the pointers.
9090 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9091 window->consumeMotionCancel();
9092
9093 // Second finger down on the window and spy, but the window should not receive the pointer down.
9094 const MotionEvent secondFingerDownEvent =
9095 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9096 .displayId(ADISPLAY_ID_DEFAULT)
9097 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009098 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9099 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009100 .build();
9101 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9102 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9103 InputEventInjectionSync::WAIT_FOR_RESULT))
9104 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9105
Harry Cutts33476232023-01-30 19:57:29 +00009106 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009107
9108 // Third finger goes down outside all windows, so injection should fail.
9109 const MotionEvent thirdFingerDownEvent =
9110 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9111 .displayId(ADISPLAY_ID_DEFAULT)
9112 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009113 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9114 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9115 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009116 .build();
9117 ASSERT_EQ(InputEventInjectionResult::FAILED,
9118 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9119 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009120 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009121
9122 spy->assertNoEvents();
9123 window->assertNoEvents();
9124}
9125
9126/**
9127 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9128 */
9129TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9130 auto spy = createSpy();
9131 spy->setFrame(Rect(0, 0, 100, 100));
9132 auto window = createForeground();
9133 window->setFrame(Rect(0, 0, 200, 200));
9134
9135 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9136
9137 // First finger down on the window only
9138 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9139 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9140 {150, 150}))
9141 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9142 window->consumeMotionDown();
9143
9144 // Second finger down on the spy and window
9145 const MotionEvent secondFingerDownEvent =
9146 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9147 .displayId(ADISPLAY_ID_DEFAULT)
9148 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009149 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9150 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009151 .build();
9152 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9153 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9154 InputEventInjectionSync::WAIT_FOR_RESULT))
9155 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9156 spy->consumeMotionDown();
9157 window->consumeMotionPointerDown(1);
9158
9159 // Third finger down on the spy and window
9160 const MotionEvent thirdFingerDownEvent =
9161 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9162 .displayId(ADISPLAY_ID_DEFAULT)
9163 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009164 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9165 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9166 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009167 .build();
9168 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9169 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9170 InputEventInjectionSync::WAIT_FOR_RESULT))
9171 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9172 spy->consumeMotionPointerDown(1);
9173 window->consumeMotionPointerDown(2);
9174
9175 // Spy window pilfers the pointers.
9176 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9177 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9178 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9179
9180 spy->assertNoEvents();
9181 window->assertNoEvents();
9182}
9183
9184/**
9185 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9186 * other windows should be canceled. If this results in the cancellation of all pointers for some
9187 * window, then that window should receive ACTION_CANCEL.
9188 */
9189TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9190 auto spy = createSpy();
9191 spy->setFrame(Rect(0, 0, 100, 100));
9192 auto window = createForeground();
9193 window->setFrame(Rect(0, 0, 200, 200));
9194
9195 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9196
9197 // First finger down on both spy and window
9198 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9199 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9200 {10, 10}))
9201 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9202 window->consumeMotionDown();
9203 spy->consumeMotionDown();
9204
9205 // Second finger down on the spy and window
9206 const MotionEvent secondFingerDownEvent =
9207 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9208 .displayId(ADISPLAY_ID_DEFAULT)
9209 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009210 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9211 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009212 .build();
9213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9214 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9215 InputEventInjectionSync::WAIT_FOR_RESULT))
9216 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9217 spy->consumeMotionPointerDown(1);
9218 window->consumeMotionPointerDown(1);
9219
9220 // Spy window pilfers the pointers.
9221 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9222 window->consumeMotionCancel();
9223
9224 spy->assertNoEvents();
9225 window->assertNoEvents();
9226}
9227
9228/**
9229 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9230 * be sent to other windows
9231 */
9232TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9233 auto spy = createSpy();
9234 spy->setFrame(Rect(0, 0, 100, 100));
9235 auto window = createForeground();
9236 window->setFrame(Rect(0, 0, 200, 200));
9237
9238 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9239
9240 // First finger down on both window and spy
9241 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9242 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9243 {10, 10}))
9244 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9245 window->consumeMotionDown();
9246 spy->consumeMotionDown();
9247
9248 // Spy window pilfers the pointers.
9249 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9250 window->consumeMotionCancel();
9251
9252 // Second finger down on the window only
9253 const MotionEvent secondFingerDownEvent =
9254 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9255 .displayId(ADISPLAY_ID_DEFAULT)
9256 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009257 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9258 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009259 .build();
9260 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9261 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9262 InputEventInjectionSync::WAIT_FOR_RESULT))
9263 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9264 window->consumeMotionDown();
9265 window->assertNoEvents();
9266
9267 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9268 spy->consumeMotionMove();
9269 spy->assertNoEvents();
9270}
9271
Prabir Pradhand65552b2021-10-07 11:23:50 -07009272class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9273public:
9274 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9275 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9276 std::make_shared<FakeApplicationHandle>();
9277 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009278 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9279 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009280 overlay->setFocusable(false);
9281 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009282 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009283 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009284 overlay->setTrustedOverlay(true);
9285
9286 std::shared_ptr<FakeApplicationHandle> application =
9287 std::make_shared<FakeApplicationHandle>();
9288 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009289 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9290 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009291 window->setFocusable(true);
9292 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009293
9294 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9295 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9296 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009297 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009298 return {std::move(overlay), std::move(window)};
9299 }
9300
9301 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009302 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009303 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009304 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009305 }
9306
9307 void sendStylusEvent(int32_t action) {
9308 NotifyMotionArgs motionArgs =
9309 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9310 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009311 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009312 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009313 }
9314};
9315
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009316using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9317
9318TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9319 ScopedSilentDeath _silentDeath;
9320
Prabir Pradhand65552b2021-10-07 11:23:50 -07009321 auto [overlay, window] = setupStylusOverlayScenario();
9322 overlay->setTrustedOverlay(false);
9323 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9324 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9325 ".* not a trusted overlay");
9326}
9327
9328TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9329 auto [overlay, window] = setupStylusOverlayScenario();
9330 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9331
9332 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9333 overlay->consumeMotionDown();
9334 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9335 overlay->consumeMotionUp();
9336
9337 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9338 window->consumeMotionDown();
9339 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9340 window->consumeMotionUp();
9341
9342 overlay->assertNoEvents();
9343 window->assertNoEvents();
9344}
9345
9346TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9347 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009348 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009349 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9350
9351 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9352 overlay->consumeMotionDown();
9353 window->consumeMotionDown();
9354 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9355 overlay->consumeMotionUp();
9356 window->consumeMotionUp();
9357
9358 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9359 window->consumeMotionDown();
9360 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9361 window->consumeMotionUp();
9362
9363 overlay->assertNoEvents();
9364 window->assertNoEvents();
9365}
9366
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009367/**
9368 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9369 * The scenario is as follows:
9370 * - The stylus interceptor overlay is configured as a spy window.
9371 * - The stylus interceptor spy receives the start of a new stylus gesture.
9372 * - It pilfers pointers and then configures itself to no longer be a spy.
9373 * - The stylus interceptor continues to receive the rest of the gesture.
9374 */
9375TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9376 auto [overlay, window] = setupStylusOverlayScenario();
9377 overlay->setSpy(true);
9378 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9379
9380 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9381 overlay->consumeMotionDown();
9382 window->consumeMotionDown();
9383
9384 // The interceptor pilfers the pointers.
9385 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9386 window->consumeMotionCancel();
9387
9388 // The interceptor configures itself so that it is no longer a spy.
9389 overlay->setSpy(false);
9390 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9391
9392 // It continues to receive the rest of the stylus gesture.
9393 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9394 overlay->consumeMotionMove();
9395 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9396 overlay->consumeMotionUp();
9397
9398 window->assertNoEvents();
9399}
9400
Prabir Pradhan5735a322022-04-11 17:23:34 +00009401struct User {
9402 int32_t mPid;
9403 int32_t mUid;
9404 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9405 std::unique_ptr<InputDispatcher>& mDispatcher;
9406
9407 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9408 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9409
9410 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9411 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9412 ADISPLAY_ID_DEFAULT, {100, 200},
9413 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9414 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9415 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9416 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9417 }
9418
9419 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009420 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009421 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009422 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009423 mPolicyFlags);
9424 }
9425
9426 sp<FakeWindowHandle> createWindow() const {
9427 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9428 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009429 sp<FakeWindowHandle> window =
9430 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9431 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009432 window->setOwnerInfo(mPid, mUid);
9433 return window;
9434 }
9435};
9436
9437using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9438
9439TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9440 auto owner = User(mDispatcher, 10, 11);
9441 auto window = owner.createWindow();
9442 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9443
9444 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9445 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9446 window->consumeMotionDown();
9447
9448 setFocusedWindow(window);
9449 window->consumeFocusEvent(true);
9450
9451 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9452 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9453 window->consumeKeyDown(ADISPLAY_ID_NONE);
9454}
9455
9456TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9457 auto owner = User(mDispatcher, 10, 11);
9458 auto window = owner.createWindow();
9459 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9460
9461 auto rando = User(mDispatcher, 20, 21);
9462 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9463 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9464
9465 setFocusedWindow(window);
9466 window->consumeFocusEvent(true);
9467
9468 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9469 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9470 window->assertNoEvents();
9471}
9472
9473TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9474 auto owner = User(mDispatcher, 10, 11);
9475 auto window = owner.createWindow();
9476 auto spy = owner.createWindow();
9477 spy->setSpy(true);
9478 spy->setTrustedOverlay(true);
9479 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9480
9481 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9482 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9483 spy->consumeMotionDown();
9484 window->consumeMotionDown();
9485}
9486
9487TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9488 auto owner = User(mDispatcher, 10, 11);
9489 auto window = owner.createWindow();
9490
9491 auto rando = User(mDispatcher, 20, 21);
9492 auto randosSpy = rando.createWindow();
9493 randosSpy->setSpy(true);
9494 randosSpy->setTrustedOverlay(true);
9495 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9496
9497 // The event is targeted at owner's window, so injection should succeed, but the spy should
9498 // not receive the event.
9499 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9500 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9501 randosSpy->assertNoEvents();
9502 window->consumeMotionDown();
9503}
9504
9505TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9506 auto owner = User(mDispatcher, 10, 11);
9507 auto window = owner.createWindow();
9508
9509 auto rando = User(mDispatcher, 20, 21);
9510 auto randosSpy = rando.createWindow();
9511 randosSpy->setSpy(true);
9512 randosSpy->setTrustedOverlay(true);
9513 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9514
9515 // A user that has injection permission can inject into any window.
9516 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9517 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9518 ADISPLAY_ID_DEFAULT));
9519 randosSpy->consumeMotionDown();
9520 window->consumeMotionDown();
9521
9522 setFocusedWindow(randosSpy);
9523 randosSpy->consumeFocusEvent(true);
9524
9525 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9526 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9527 window->assertNoEvents();
9528}
9529
9530TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9531 auto owner = User(mDispatcher, 10, 11);
9532 auto window = owner.createWindow();
9533
9534 auto rando = User(mDispatcher, 20, 21);
9535 auto randosWindow = rando.createWindow();
9536 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9537 randosWindow->setWatchOutsideTouch(true);
9538 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9539
9540 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9541 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9542 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9543 window->consumeMotionDown();
9544 randosWindow->consumeMotionOutside();
9545}
9546
Garfield Tane84e6f92019-08-29 17:28:41 -07009547} // namespace android::inputdispatcher