blob: 6eb63bab17bf8e0fd416ec7cd9e24ceb122ab785 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Prabir Pradhan9205e422023-05-16 20:06:13 +000018#include "EventBuilders.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080019
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070020#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080021#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080022#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070023#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070024#include <binder/Binder.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000025#include <fcntl.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080026#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080027#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100028#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080030#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100031
Garfield Tan1c7bc862020-01-28 13:24:04 -080032#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080033#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070034#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080035#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080036#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037
Garfield Tan1c7bc862020-01-28 13:24:04 -080038using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050039using android::gui::FocusRequest;
40using android::gui::TouchOcclusionMode;
41using android::gui::WindowInfo;
42using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080043using android::os::InputEventInjectionResult;
44using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080045
Garfield Tane84e6f92019-08-29 17:28:41 -070046namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080047
Dominik Laskowski2f01d772022-03-23 16:01:29 -070048using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080049using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070050
Michael Wrightd02c5b62014-02-10 15:10:22 -080051// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000052static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080053
54// An arbitrary device id.
Prabir Pradhan9205e422023-05-16 20:06:13 +000055static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080056static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080057
Jeff Brownf086ddb2014-02-11 14:28:48 -080058// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000059static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
60static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080061
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080062static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
63static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
64static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
65static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -070066static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080067static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080068static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080069static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080070/**
71 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
72 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
73 * index 0) is the new pointer going down. The same pointer could have been placed at a different
74 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
75 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
76 * pointer id=0 leaves but the pointer id=1 remains.
77 */
78static constexpr int32_t POINTER_0_DOWN =
79 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080080static constexpr int32_t POINTER_1_DOWN =
81 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000082static constexpr int32_t POINTER_2_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000084static constexpr int32_t POINTER_3_DOWN =
85 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000086static constexpr int32_t POINTER_0_UP =
87 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080088static constexpr int32_t POINTER_1_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Harry Cuttsb166c002023-05-09 13:06:05 +000090static constexpr int32_t POINTER_2_UP =
91 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080092
Antonio Kantek15beb512022-06-13 22:35:41 +000093// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000094static constexpr int32_t WINDOW_PID = 999;
95static constexpr int32_t WINDOW_UID = 1001;
96
Antonio Kantek15beb512022-06-13 22:35:41 +000097// The default pid and uid for the windows created on the secondary display by the test.
98static constexpr int32_t SECONDARY_WINDOW_PID = 1010;
99static constexpr int32_t SECONDARY_WINDOW_UID = 1012;
100
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000101// An arbitrary pid of the gesture monitor window
102static constexpr int32_t MONITOR_PID = 2001;
103
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800104static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
105
Arthur Hungc539dbb2022-12-08 07:45:36 +0000106static constexpr int expectedWallpaperFlags =
107 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
108
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800109using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
110
chaviwd1c23182019-12-20 18:44:56 -0800111struct PointF {
112 float x;
113 float y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800114 auto operator<=>(const PointF&) const = default;
chaviwd1c23182019-12-20 18:44:56 -0800115};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
Gang Wang342c9272020-01-13 13:15:04 -0500117/**
118 * Return a DOWN key event with KEYCODE_A.
119 */
120static KeyEvent getTestKeyEvent() {
121 KeyEvent event;
122
Garfield Tanfbe732e2020-01-24 11:26:14 -0800123 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
124 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
125 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500126 return event;
127}
128
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000129static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
130 ASSERT_EQ(expectedAction, receivedAction)
131 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
132 << MotionEvent::actionToString(receivedAction);
133}
134
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800135MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
136 bool matches = action == arg.getAction();
137 if (!matches) {
138 *result_listener << "expected action " << MotionEvent::actionToString(action)
139 << ", but got " << MotionEvent::actionToString(arg.getAction());
140 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800141 if (action == AMOTION_EVENT_ACTION_DOWN) {
142 if (!matches) {
143 *result_listener << "; ";
144 }
145 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
146 matches &= arg.getDownTime() == arg.getEventTime();
147 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800148 if (action == AMOTION_EVENT_ACTION_CANCEL) {
149 if (!matches) {
150 *result_listener << "; ";
151 }
152 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
153 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
154 }
155 return matches;
156}
157
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800158MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
159 return arg.getDownTime() == downTime;
160}
161
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800162MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
163 return arg.getDisplayId() == displayId;
164}
165
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800166MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") {
167 return arg.getDeviceId() == deviceId;
168}
169
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800170MATCHER_P(WithSource, source, "InputEvent with specified source") {
171 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
172 << inputEventSourceToString(arg.getSource());
173 return arg.getSource() == source;
174}
175
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800176MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
177 return arg.getFlags() == flags;
178}
179
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800180MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
181 if (arg.getPointerCount() != 1) {
182 *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
183 return false;
184 }
Harry Cutts33476232023-01-30 19:57:29 +0000185 return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800186}
187
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800188MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") {
189 return arg.getPointerCount() == pointerCount;
190}
191
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800192MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
193 // Build a map for the received pointers, by pointer id
194 std::map<int32_t /*pointerId*/, PointF> actualPointers;
195 for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
196 const int32_t pointerId = arg.getPointerId(pointerIndex);
197 actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
198 }
199 return pointers == actualPointers;
200}
201
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202// --- FakeInputDispatcherPolicy ---
203
204class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
205 InputDispatcherConfiguration mConfig;
206
Prabir Pradhanedd96402022-02-15 01:46:16 -0800207 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
208
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209public:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000210 FakeInputDispatcherPolicy() = default;
211 virtual ~FakeInputDispatcherPolicy() = default;
Jackal Guof9696682018-10-05 12:23:23 +0800212
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800213 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700214 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700215 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700216 EXPECT_EQ(event.getDisplayId(), args.displayId);
217
218 const auto& keyEvent = static_cast<const KeyEvent&>(event);
219 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
220 EXPECT_EQ(keyEvent.getAction(), args.action);
221 });
Jackal Guof9696682018-10-05 12:23:23 +0800222 }
223
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700224 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
225 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700226 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700227 EXPECT_EQ(event.getDisplayId(), args.displayId);
228
229 const auto& motionEvent = static_cast<const MotionEvent&>(event);
230 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
231 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000232 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
233 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
234 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
235 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700236 });
Jackal Guof9696682018-10-05 12:23:23 +0800237 }
238
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700239 void assertFilterInputEventWasNotCalled() {
240 std::scoped_lock lock(mLock);
241 ASSERT_EQ(nullptr, mFilteredEvent);
242 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800244 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700245 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800246 ASSERT_TRUE(mConfigurationChangedTime)
247 << "Timed out waiting for configuration changed call";
248 ASSERT_EQ(*mConfigurationChangedTime, when);
249 mConfigurationChangedTime = std::nullopt;
250 }
251
252 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700253 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800254 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800255 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800256 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
257 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
258 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
259 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
260 mLastNotifySwitch = std::nullopt;
261 }
262
chaviwfd6d3512019-03-25 13:23:49 -0700263 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700264 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800265 ASSERT_EQ(touchedToken, mOnPointerDownToken);
266 mOnPointerDownToken.clear();
267 }
268
269 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700270 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800271 ASSERT_TRUE(mOnPointerDownToken == nullptr)
272 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700273 }
274
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700275 // This function must be called soon after the expected ANR timer starts,
276 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500277 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700278 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500279 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800280 std::unique_lock lock(mLock);
281 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500282 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800283 ASSERT_NO_FATAL_FAILURE(
284 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500285 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700286 }
287
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000288 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800289 const sp<WindowInfoHandle>& window) {
290 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
291 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
292 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500293 }
294
Prabir Pradhanedd96402022-02-15 01:46:16 -0800295 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
296 const sp<IBinder>& expectedToken,
297 int32_t expectedPid) {
298 std::unique_lock lock(mLock);
299 android::base::ScopedLockAssertion assumeLocked(mLock);
300 AnrResult result;
301 ASSERT_NO_FATAL_FAILURE(result =
302 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
303 const auto& [token, pid] = result;
304 ASSERT_EQ(expectedToken, token);
305 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500306 }
307
Prabir Pradhanedd96402022-02-15 01:46:16 -0800308 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000309 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500310 std::unique_lock lock(mLock);
311 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800312 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
313 const auto& [token, _] = result;
314 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000315 }
316
Prabir Pradhanedd96402022-02-15 01:46:16 -0800317 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
318 int32_t expectedPid) {
319 std::unique_lock lock(mLock);
320 android::base::ScopedLockAssertion assumeLocked(mLock);
321 AnrResult result;
322 ASSERT_NO_FATAL_FAILURE(
323 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
324 const auto& [token, pid] = result;
325 ASSERT_EQ(expectedToken, token);
326 ASSERT_EQ(expectedPid, pid);
327 }
328
329 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000330 sp<IBinder> getResponsiveWindowToken() {
331 std::unique_lock lock(mLock);
332 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800333 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
334 const auto& [token, _] = result;
335 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700336 }
337
338 void assertNotifyAnrWasNotCalled() {
339 std::scoped_lock lock(mLock);
340 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800341 ASSERT_TRUE(mAnrWindows.empty());
342 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500343 << "ANR was not called, but please also consume the 'connection is responsive' "
344 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700345 }
346
Garfield Tan1c7bc862020-01-28 13:24:04 -0800347 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
348 mConfig.keyRepeatTimeout = timeout;
349 mConfig.keyRepeatDelay = delay;
350 }
351
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000352 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800353 std::unique_lock lock(mLock);
354 base::ScopedLockAssertion assumeLocked(mLock);
355
356 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
357 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000358 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800359 enabled;
360 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000361 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
362 << ") to be called.";
363 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800364 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000365 auto request = *mPointerCaptureRequest;
366 mPointerCaptureRequest.reset();
367 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800368 }
369
370 void assertSetPointerCaptureNotCalled() {
371 std::unique_lock lock(mLock);
372 base::ScopedLockAssertion assumeLocked(mLock);
373
374 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000375 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800376 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000377 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800378 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000379 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800380 }
381
arthurhungf452d0b2021-01-06 00:19:52 +0800382 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
383 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800384 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800385 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800386 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800387 }
388
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800389 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
390 std::unique_lock lock(mLock);
391 base::ScopedLockAssertion assumeLocked(mLock);
392 std::optional<sp<IBinder>> receivedToken =
393 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
394 mNotifyInputChannelBroken);
395 ASSERT_TRUE(receivedToken.has_value());
396 ASSERT_EQ(token, *receivedToken);
397 }
398
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800399 /**
400 * Set policy timeout. A value of zero means next key will not be intercepted.
401 */
402 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
403 mInterceptKeyTimeout = timeout;
404 }
405
Josep del Riob3981622023-04-18 15:49:45 +0000406 void assertUserActivityPoked() {
407 std::scoped_lock lock(mLock);
408 ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked";
409 }
410
411 void assertUserActivityNotPoked() {
412 std::scoped_lock lock(mLock);
413 ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked";
414 }
415
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700417 std::mutex mLock;
418 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
419 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
420 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
421 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800422
Prabir Pradhan99987712020-11-10 18:43:05 -0800423 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000424
425 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800426
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700427 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700428 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800429 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
430 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700431 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800432 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
433 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700434
arthurhungf452d0b2021-01-06 00:19:52 +0800435 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800436 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Josep del Riob3981622023-04-18 15:49:45 +0000437 bool mPokedUserActivity GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800438
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800439 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
440
Prabir Pradhanedd96402022-02-15 01:46:16 -0800441 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
442 // for a specific container to become non-empty. When the container is non-empty, return the
443 // first entry from the container and erase it.
444 template <class T>
445 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
446 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
447 // If there is an ANR, Dispatcher won't be idle because there are still events
448 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
449 // before checking if ANR was called.
450 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
451 // to provide it some time to act. 100ms seems reasonable.
452 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
453 const std::chrono::time_point start = std::chrono::steady_clock::now();
454 std::optional<T> token =
455 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
456 if (!token.has_value()) {
457 ADD_FAILURE() << "Did not receive the ANR callback";
458 return {};
459 }
460
461 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
462 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
463 // the dispatcher started counting before this function was called
464 if (std::chrono::abs(timeout - waited) > 100ms) {
465 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
466 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
467 << "ms, but waited "
468 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
469 << "ms instead";
470 }
471 return *token;
472 }
473
474 template <class T>
475 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
476 std::queue<T>& storage,
477 std::unique_lock<std::mutex>& lock,
478 std::condition_variable& condition)
479 REQUIRES(mLock) {
480 condition.wait_for(lock, timeout,
481 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
482 if (storage.empty()) {
483 ADD_FAILURE() << "Did not receive the expected callback";
484 return std::nullopt;
485 }
486 T item = storage.front();
487 storage.pop();
488 return std::make_optional(item);
489 }
490
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600491 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700492 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800493 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 }
495
Prabir Pradhanedd96402022-02-15 01:46:16 -0800496 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
497 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700498 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800499 ASSERT_TRUE(pid.has_value());
500 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700501 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500502 }
503
Prabir Pradhanedd96402022-02-15 01:46:16 -0800504 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
505 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500506 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800507 ASSERT_TRUE(pid.has_value());
508 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500509 mNotifyAnr.notify_all();
510 }
511
512 void notifyNoFocusedWindowAnr(
513 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
514 std::scoped_lock lock(mLock);
515 mAnrApplications.push(applicationHandle);
516 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517 }
518
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800519 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
520 std::scoped_lock lock(mLock);
521 mBrokenInputChannels.push(connectionToken);
522 mNotifyInputChannelBroken.notify_all();
523 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600525 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700526
Chris Yef59a2f42020-10-16 12:55:26 -0700527 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
528 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
529 const std::vector<float>& values) override {}
530
531 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
532 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000533
Chris Yefb552902021-02-03 17:18:37 -0800534 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
535
Prabir Pradhana41d2442023-04-20 21:30:40 +0000536 InputDispatcherConfiguration getDispatcherConfiguration() override { return mConfig; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537
Prabir Pradhana41d2442023-04-20 21:30:40 +0000538 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700539 std::scoped_lock lock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +0000540 switch (inputEvent.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700541 case InputEventType::KEY: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000542 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent);
543 mFilteredEvent = std::make_unique<KeyEvent>(keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800544 break;
545 }
546
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700547 case InputEventType::MOTION: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000548 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent);
549 mFilteredEvent = std::make_unique<MotionEvent>(motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800550 break;
551 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700552 default: {
553 ADD_FAILURE() << "Should only filter keys or motions";
554 break;
555 }
Jackal Guof9696682018-10-05 12:23:23 +0800556 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 return true;
558 }
559
Prabir Pradhana41d2442023-04-20 21:30:40 +0000560 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override {
561 if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800562 // Clear intercept state when we handled the event.
563 mInterceptKeyTimeout = 0ms;
564 }
565 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600567 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568
Prabir Pradhana41d2442023-04-20 21:30:40 +0000569 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800570 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
571 // Clear intercept state so we could dispatch the event in next wake.
572 mInterceptKeyTimeout = 0ms;
573 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 }
575
Prabir Pradhana41d2442023-04-20 21:30:40 +0000576 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&,
577 uint32_t) override {
578 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 }
580
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600581 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
582 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700583 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800584 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
585 * essentially a passthrough for notifySwitch.
586 */
Harry Cutts33476232023-01-30 19:57:29 +0000587 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 }
589
Josep del Riob3981622023-04-18 15:49:45 +0000590 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {
591 std::scoped_lock lock(mLock);
592 mPokedUserActivity = true;
593 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600595 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700596 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700597 mOnPointerDownToken = newToken;
598 }
599
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000600 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800601 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000602 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800603 mPointerCaptureChangedCondition.notify_all();
604 }
605
arthurhungf452d0b2021-01-06 00:19:52 +0800606 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
607 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800608 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800609 mDropTargetWindowToken = token;
610 }
611
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700612 void assertFilterInputEventWasCalledInternal(
613 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700614 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800615 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700616 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800617 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800618 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619};
620
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621// --- InputDispatcherTest ---
622
623class InputDispatcherTest : public testing::Test {
624protected:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000625 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700626 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000628 void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000629 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
630 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800631 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000632 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700633 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634 }
635
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000636 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700637 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000638 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700639 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700641
642 /**
643 * Used for debugging when writing the test
644 */
645 void dumpDispatcherState() {
646 std::string dump;
647 mDispatcher->dump(dump);
648 std::stringstream ss(dump);
649 std::string to;
650
651 while (std::getline(ss, to, '\n')) {
652 ALOGE("%s", to.c_str());
653 }
654 }
Vishnu Nair958da932020-08-21 17:12:37 -0700655
Chavi Weingarten847e8512023-03-29 00:26:09 +0000656 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700657 FocusRequest request;
658 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000659 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700660 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
661 request.displayId = window->getInfo()->displayId;
662 mDispatcher->setFocusedWindow(request);
663 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664};
665
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
667 KeyEvent event;
668
669 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800670 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
671 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600672 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
673 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800674 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000675 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000676 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677 << "Should reject key events with undefined action.";
678
679 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800680 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
681 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600682 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800683 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000684 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000685 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686 << "Should reject key events with ACTION_MULTIPLE.";
687}
688
689TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
690 MotionEvent event;
691 PointerProperties pointerProperties[MAX_POINTERS + 1];
692 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800693 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694 pointerProperties[i].clear();
695 pointerProperties[i].id = i;
696 pointerCoords[i].clear();
697 }
698
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800699 // Some constants commonly used below
700 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
701 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
702 constexpr int32_t metaState = AMETA_NONE;
703 constexpr MotionClassification classification = MotionClassification::NONE;
704
chaviw9eaa22c2020-07-01 16:21:27 -0700705 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800707 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700708 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
709 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700710 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
711 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700712 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800713 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000714 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000715 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 << "Should reject motion events with undefined action.";
717
718 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800719 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800720 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
721 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
722 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
723 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500724 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800725 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000726 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000727 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728 << "Should reject motion events with pointer down index too large.";
729
Garfield Tanfbe732e2020-01-24 11:26:14 -0800730 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700731 AMOTION_EVENT_ACTION_POINTER_DOWN |
732 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700733 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
734 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700735 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500736 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800737 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000738 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000739 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740 << "Should reject motion events with pointer down index too small.";
741
742 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800743 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800744 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
745 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
746 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
747 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500748 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800749 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000750 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000751 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 << "Should reject motion events with pointer up index too large.";
753
Garfield Tanfbe732e2020-01-24 11:26:14 -0800754 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700755 AMOTION_EVENT_ACTION_POINTER_UP |
756 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700757 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
758 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700759 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500760 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800761 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000762 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000763 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764 << "Should reject motion events with pointer up index too small.";
765
766 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800767 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
768 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700769 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700770 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
771 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700772 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800773 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000774 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000775 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800776 << "Should reject motion events with 0 pointers.";
777
Garfield Tanfbe732e2020-01-24 11:26:14 -0800778 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
779 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700780 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700781 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
782 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700783 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800784 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000785 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000786 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787 << "Should reject motion events with more than MAX_POINTERS pointers.";
788
789 // Rejects motion events with invalid pointer ids.
790 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800791 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
792 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700793 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700794 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
795 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700796 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800797 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000798 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000799 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 << "Should reject motion events with pointer ids less than 0.";
801
802 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800803 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
804 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700805 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700806 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
807 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700808 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800809 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000810 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000811 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800812 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
813
814 // Rejects motion events with duplicate pointer ids.
815 pointerProperties[0].id = 1;
816 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800817 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
818 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700819 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700820 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
821 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700822 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800823 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000824 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000825 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826 << "Should reject motion events with duplicate pointer ids.";
827}
828
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800829/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
830
831TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
832 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000833 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800834 ASSERT_TRUE(mDispatcher->waitForIdle());
835
836 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
837}
838
839TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000840 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
841 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000842 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800843
844 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
845 args.policyFlags |= POLICY_FLAG_TRUSTED;
846 mFakePolicy->assertNotifySwitchWasCalled(args);
847}
848
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -0700849namespace {
850
Arthur Hungb92218b2018-08-14 12:00:21 +0800851// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700852static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700853// Default input dispatching timeout if there is no focused application or paused window
854// from which to determine an appropriate dispatching timeout.
855static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
856 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
857 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800858
859class FakeApplicationHandle : public InputApplicationHandle {
860public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700861 FakeApplicationHandle() {
862 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700863 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500864 mInfo.dispatchingTimeoutMillis =
865 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700866 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800867 virtual ~FakeApplicationHandle() {}
868
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000869 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700870
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500871 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
872 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700873 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800874};
875
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800876class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800877public:
Garfield Tan15601662020-09-22 15:32:38 -0700878 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800879 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700880 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800881 }
882
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800883 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700884 InputEvent* event;
885 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
886 if (!consumeSeq) {
887 return nullptr;
888 }
889 finishEvent(*consumeSeq);
890 return event;
891 }
892
893 /**
894 * Receive an event without acknowledging it.
895 * Return the sequence number that could later be used to send finished signal.
896 */
897 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800898 uint32_t consumeSeq;
899 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800900
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800901 std::chrono::time_point start = std::chrono::steady_clock::now();
902 status_t status = WOULD_BLOCK;
903 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000904 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800905 &event);
906 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
907 if (elapsed > 100ms) {
908 break;
909 }
910 }
911
912 if (status == WOULD_BLOCK) {
913 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700914 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800915 }
916
917 if (status != OK) {
918 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700919 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800920 }
921 if (event == nullptr) {
922 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700923 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800924 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700925 if (outEvent != nullptr) {
926 *outEvent = event;
927 }
928 return consumeSeq;
929 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800930
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700931 /**
932 * To be used together with "receiveEvent" to complete the consumption of an event.
933 */
934 void finishEvent(uint32_t consumeSeq) {
935 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
936 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800937 }
938
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000939 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
940 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
941 ASSERT_EQ(OK, status);
942 }
943
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700944 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000945 std::optional<int32_t> expectedDisplayId,
946 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800947 InputEvent* event = consume();
948
949 ASSERT_NE(nullptr, event) << mName.c_str()
950 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800951 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700952 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
953 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800954
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000955 if (expectedDisplayId.has_value()) {
956 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
957 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800958
Tiger Huang8664f8c2018-10-11 19:14:35 +0800959 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700960 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800961 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
962 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000963 if (expectedFlags.has_value()) {
964 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
965 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800966 break;
967 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700968 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800969 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000970 assertMotionAction(expectedAction, motionEvent.getAction());
971
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000972 if (expectedFlags.has_value()) {
973 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
974 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800975 break;
976 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700977 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100978 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
979 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700980 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -0800981 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
982 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700983 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +0000984 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
985 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700986 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +0800987 FAIL() << "Use 'consumeDragEvent' for DRAG events";
988 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800989 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800990 }
991
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800992 MotionEvent* consumeMotion() {
993 InputEvent* event = consume();
994
995 if (event == nullptr) {
996 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
997 return nullptr;
998 }
999
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001000 if (event->getType() != InputEventType::MOTION) {
1001 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001002 return nullptr;
1003 }
1004 return static_cast<MotionEvent*>(event);
1005 }
1006
1007 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1008 MotionEvent* motionEvent = consumeMotion();
1009 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1010 ASSERT_THAT(*motionEvent, matcher);
1011 }
1012
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001013 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
1014 InputEvent* event = consume();
1015 ASSERT_NE(nullptr, event) << mName.c_str()
1016 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001017 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1018 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001019
1020 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1021 << mName.c_str() << ": event displayId should always be NONE.";
1022
1023 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1024 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001025 }
1026
Prabir Pradhan99987712020-11-10 18:43:05 -08001027 void consumeCaptureEvent(bool hasCapture) {
1028 const InputEvent* event = consume();
1029 ASSERT_NE(nullptr, event) << mName.c_str()
1030 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001031 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1032 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001033
1034 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1035 << mName.c_str() << ": event displayId should always be NONE.";
1036
1037 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1038 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1039 }
1040
arthurhungb89ccb02020-12-30 16:19:01 +08001041 void consumeDragEvent(bool isExiting, float x, float y) {
1042 const InputEvent* event = consume();
1043 ASSERT_NE(nullptr, event) << mName.c_str()
1044 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001045 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001046
1047 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1048 << mName.c_str() << ": event displayId should always be NONE.";
1049
1050 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1051 EXPECT_EQ(isExiting, dragEvent.isExiting());
1052 EXPECT_EQ(x, dragEvent.getX());
1053 EXPECT_EQ(y, dragEvent.getY());
1054 }
1055
Antonio Kantekf16f2832021-09-28 04:39:20 +00001056 void consumeTouchModeEvent(bool inTouchMode) {
1057 const InputEvent* event = consume();
1058 ASSERT_NE(nullptr, event) << mName.c_str()
1059 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001060 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1061 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001062
1063 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1064 << mName.c_str() << ": event displayId should always be NONE.";
1065 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1066 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1067 }
1068
chaviwd1c23182019-12-20 18:44:56 -08001069 void assertNoEvents() {
1070 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001071 if (event == nullptr) {
1072 return;
1073 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001074 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001075 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1076 ADD_FAILURE() << "Received key event "
1077 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001078 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001079 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1080 ADD_FAILURE() << "Received motion event "
1081 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001082 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001083 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1084 ADD_FAILURE() << "Received focus event, hasFocus = "
1085 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001086 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001087 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1088 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1089 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001090 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001091 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1092 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1093 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001094 }
1095 FAIL() << mName.c_str()
1096 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001097 }
1098
1099 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1100
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001101 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1102
chaviwd1c23182019-12-20 18:44:56 -08001103protected:
1104 std::unique_ptr<InputConsumer> mConsumer;
1105 PreallocatedInputEventFactory mEventFactory;
1106
1107 std::string mName;
1108};
1109
chaviw3277faf2021-05-19 16:45:23 -05001110class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001111public:
1112 static const int32_t WIDTH = 600;
1113 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001114
Chris Yea209fde2020-07-22 13:54:51 -07001115 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001116 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001117 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001118 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001119 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001120 base::Result<std::unique_ptr<InputChannel>> channel =
1121 dispatcher->createInputChannel(name);
1122 token = (*channel)->getConnectionToken();
1123 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001124 }
1125
1126 inputApplicationHandle->updateInfo();
1127 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1128
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001129 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001130 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001131 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001132 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001133 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001134 mInfo.frameLeft = 0;
1135 mInfo.frameTop = 0;
1136 mInfo.frameRight = WIDTH;
1137 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001138 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001139 mInfo.globalScaleFactor = 1.0;
1140 mInfo.touchableRegion.clear();
1141 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001142 mInfo.ownerPid = WINDOW_PID;
1143 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001144 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001145 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001146 }
1147
Arthur Hungabbb9d82021-09-01 14:52:30 +00001148 sp<FakeWindowHandle> clone(
1149 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001150 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001151 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001152 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1153 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001154 return handle;
1155 }
1156
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001157 void setTouchable(bool touchable) {
1158 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1159 }
chaviwd1c23182019-12-20 18:44:56 -08001160
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001161 void setFocusable(bool focusable) {
1162 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1163 }
1164
1165 void setVisible(bool visible) {
1166 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1167 }
Vishnu Nair958da932020-08-21 17:12:37 -07001168
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001169 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001170 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001171 }
1172
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001173 void setPaused(bool paused) {
1174 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1175 }
1176
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001177 void setPreventSplitting(bool preventSplitting) {
1178 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001179 }
1180
1181 void setSlippery(bool slippery) {
1182 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1183 }
1184
1185 void setWatchOutsideTouch(bool watchOutside) {
1186 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1187 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001188
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001189 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1190
1191 void setInterceptsStylus(bool interceptsStylus) {
1192 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1193 }
1194
1195 void setDropInput(bool dropInput) {
1196 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1197 }
1198
1199 void setDropInputIfObscured(bool dropInputIfObscured) {
1200 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1201 }
1202
1203 void setNoInputChannel(bool noInputChannel) {
1204 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1205 }
1206
Josep del Riob3981622023-04-18 15:49:45 +00001207 void setDisableUserActivity(bool disableUserActivity) {
1208 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1209 }
1210
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001211 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1212
chaviw3277faf2021-05-19 16:45:23 -05001213 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001214
Bernardo Rufino7393d172021-02-26 13:56:11 +00001215 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1216
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001217 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001218 mInfo.frameLeft = frame.left;
1219 mInfo.frameTop = frame.top;
1220 mInfo.frameRight = frame.right;
1221 mInfo.frameBottom = frame.bottom;
1222 mInfo.touchableRegion.clear();
1223 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001224
1225 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1226 ui::Transform translate;
1227 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1228 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001229 }
1230
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001231 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1232
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001233 void setIsWallpaper(bool isWallpaper) {
1234 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1235 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001236
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001237 void setDupTouchToWallpaper(bool hasWallpaper) {
1238 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1239 }
chaviwd1c23182019-12-20 18:44:56 -08001240
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001241 void setTrustedOverlay(bool trustedOverlay) {
1242 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1243 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001244
chaviw9eaa22c2020-07-01 16:21:27 -07001245 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1246 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1247 }
1248
1249 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001250
yunho.shinf4a80b82020-11-16 21:13:57 +09001251 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1252
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001253 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001254 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001255 }
1256
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001257 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001258 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001259 }
1260
Svet Ganov5d3bc372020-01-26 23:11:07 -08001261 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001262 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001263 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1264 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001265 }
1266
1267 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001268 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001269 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1270 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001271 }
1272
1273 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001274 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001275 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1276 }
1277
1278 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1279 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001280 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001281 expectedFlags);
1282 }
1283
Svet Ganov5d3bc372020-01-26 23:11:07 -08001284 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001285 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1286 int32_t expectedFlags = 0) {
1287 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1288 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001289 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001290 }
1291
1292 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001293 int32_t expectedFlags = 0) {
1294 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1295 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001296 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001297 }
1298
1299 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001300 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001301 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001302 expectedFlags);
1303 }
1304
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001305 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1306 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001307 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001308 expectedFlags);
1309 }
1310
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001311 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1312 int32_t expectedFlags = 0) {
1313 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001314 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001315 ASSERT_EQ(InputEventType::MOTION, event->getType());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001316 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1317 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1318 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1319 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1320 }
1321
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001322 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1323 ASSERT_NE(mInputReceiver, nullptr)
1324 << "Cannot consume events from a window with no receiver";
1325 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1326 }
1327
Prabir Pradhan99987712020-11-10 18:43:05 -08001328 void consumeCaptureEvent(bool hasCapture) {
1329 ASSERT_NE(mInputReceiver, nullptr)
1330 << "Cannot consume events from a window with no receiver";
1331 mInputReceiver->consumeCaptureEvent(hasCapture);
1332 }
1333
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001334 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1335 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001336 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001337 ASSERT_THAT(*motionEvent, matcher);
1338 }
1339
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001340 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001341 std::optional<int32_t> expectedDisplayId,
1342 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001343 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1344 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1345 expectedFlags);
1346 }
1347
arthurhungb89ccb02020-12-30 16:19:01 +08001348 void consumeDragEvent(bool isExiting, float x, float y) {
1349 mInputReceiver->consumeDragEvent(isExiting, x, y);
1350 }
1351
Antonio Kantekf16f2832021-09-28 04:39:20 +00001352 void consumeTouchModeEvent(bool inTouchMode) {
1353 ASSERT_NE(mInputReceiver, nullptr)
1354 << "Cannot consume events from a window with no receiver";
1355 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1356 }
1357
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001358 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001359 if (mInputReceiver == nullptr) {
1360 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1361 return std::nullopt;
1362 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001363 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001364 }
1365
1366 void finishEvent(uint32_t sequenceNum) {
1367 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1368 mInputReceiver->finishEvent(sequenceNum);
1369 }
1370
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001371 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1372 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1373 mInputReceiver->sendTimeline(inputEventId, timeline);
1374 }
1375
chaviwaf87b3e2019-10-01 16:59:28 -07001376 InputEvent* consume() {
1377 if (mInputReceiver == nullptr) {
1378 return nullptr;
1379 }
1380 return mInputReceiver->consume();
1381 }
1382
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001383 MotionEvent* consumeMotion() {
1384 InputEvent* event = consume();
1385 if (event == nullptr) {
1386 ADD_FAILURE() << "Consume failed : no event";
1387 return nullptr;
1388 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001389 if (event->getType() != InputEventType::MOTION) {
1390 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001391 return nullptr;
1392 }
1393 return static_cast<MotionEvent*>(event);
1394 }
1395
Arthur Hungb92218b2018-08-14 12:00:21 +08001396 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001397 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001398 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001399 return; // Can't receive events if the window does not have input channel
1400 }
1401 ASSERT_NE(nullptr, mInputReceiver)
1402 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001403 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001404 }
1405
chaviwaf87b3e2019-10-01 16:59:28 -07001406 sp<IBinder> getToken() { return mInfo.token; }
1407
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001408 const std::string& getName() { return mName; }
1409
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001410 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1411 mInfo.ownerPid = ownerPid;
1412 mInfo.ownerUid = ownerUid;
1413 }
1414
Prabir Pradhanedd96402022-02-15 01:46:16 -08001415 int32_t getPid() const { return mInfo.ownerPid; }
1416
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001417 void destroyReceiver() { mInputReceiver = nullptr; }
1418
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001419 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1420
chaviwd1c23182019-12-20 18:44:56 -08001421private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001422 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001423 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001424 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001425};
1426
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001427std::atomic<int32_t> FakeWindowHandle::sId{1};
1428
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001429static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001430 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001431 int32_t displayId = ADISPLAY_ID_NONE,
1432 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001433 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001434 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1435 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001436 KeyEvent event;
1437 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1438
1439 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001440 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001441 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1442 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001443
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001444 if (!allowKeyRepeat) {
1445 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1446 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001447 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001448 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001449}
1450
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001451static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001452 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001453 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001454}
1455
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001456// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1457// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1458// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001459static InputEventInjectionResult injectKeyDownNoRepeat(
1460 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001461 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001462 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001463 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001464}
1465
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001466static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001467 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001468 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001469}
1470
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001471static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001472 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001473 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001474 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1475 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1476 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1477 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001478}
1479
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001480static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001481 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001482 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001483 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001484 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1485 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001486 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001487 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1488 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001489 MotionEventBuilder motionBuilder =
1490 MotionEventBuilder(action, source)
1491 .displayId(displayId)
1492 .eventTime(eventTime)
1493 .rawXCursorPosition(cursorPosition.x)
1494 .rawYCursorPosition(cursorPosition.y)
1495 .pointer(
1496 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1497 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1498 motionBuilder.downTime(eventTime);
1499 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001500
1501 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001502 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1503 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001504}
1505
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001506static InputEventInjectionResult injectMotionDown(
1507 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1508 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001509 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001510}
1511
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001512static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001513 int32_t source, int32_t displayId,
1514 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001515 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001516}
1517
Jackal Guof9696682018-10-05 12:23:23 +08001518static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1519 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1520 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001521 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001522 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1523 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001524
1525 return args;
1526}
1527
Josep del Riob3981622023-04-18 15:49:45 +00001528static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1529 int32_t displayId = ADISPLAY_ID_NONE) {
1530 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1531 // Define a valid key event.
1532 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1533 displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON,
1534 currentTime);
1535
1536 return args;
1537}
1538
1539static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1540 int32_t displayId = ADISPLAY_ID_NONE) {
1541 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1542 // Define a valid key event.
1543 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1544 displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT,
1545 AMETA_NONE, currentTime);
1546
1547 return args;
1548}
1549
Prabir Pradhan678438e2023-04-13 19:32:51 +00001550[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1551 int32_t displayId,
1552 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001553 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001554 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1555 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1556 }
1557
chaviwd1c23182019-12-20 18:44:56 -08001558 PointerProperties pointerProperties[pointerCount];
1559 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001560
chaviwd1c23182019-12-20 18:44:56 -08001561 for (size_t i = 0; i < pointerCount; i++) {
1562 pointerProperties[i].clear();
1563 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001564 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001565
chaviwd1c23182019-12-20 18:44:56 -08001566 pointerCoords[i].clear();
1567 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1568 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1569 }
Jackal Guof9696682018-10-05 12:23:23 +08001570
1571 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1572 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001573 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001574 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1575 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001576 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1577 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001578 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1579 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001580
1581 return args;
1582}
1583
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001584static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1585 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1586}
1587
chaviwd1c23182019-12-20 18:44:56 -08001588static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1589 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1590}
1591
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001592static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1593 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001594 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001595}
1596
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -07001597} // namespace
1598
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001599/**
1600 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1601 * broken channel.
1602 */
1603TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1604 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1605 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001606 sp<FakeWindowHandle>::make(application, mDispatcher,
1607 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001608
1609 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1610
1611 // Window closes its channel, but the window remains.
1612 window->destroyReceiver();
1613 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1614}
1615
Arthur Hungb92218b2018-08-14 12:00:21 +08001616TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001617 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001618 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1619 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001620
Arthur Hung72d8dc32020-03-28 00:48:39 +00001621 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001622 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1623 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1624 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001625
1626 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001627 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001628}
1629
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001630TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1631 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001632 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1633 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001634
1635 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1636 // Inject a MotionEvent to an unknown display.
1637 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1638 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1639 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1640
1641 // Window should receive motion event.
1642 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1643}
1644
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001645/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001646 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001647 * This test serves as a sanity check for the next test, where setInputWindows is
1648 * called twice.
1649 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001650TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001651 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001652 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1653 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001654 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001655
1656 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001657 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001658 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1659 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001660 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001661
1662 // Window should receive motion event.
1663 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1664}
1665
1666/**
1667 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001668 */
1669TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001670 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001671 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1672 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001673 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001674
1675 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1676 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001677 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001678 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1679 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001680 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001681
1682 // Window should receive motion event.
1683 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1684}
1685
Arthur Hungb92218b2018-08-14 12:00:21 +08001686// The foreground window should receive the first touch down event.
1687TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001688 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001689 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001690 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001691 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001692 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001693
Arthur Hung72d8dc32020-03-28 00:48:39 +00001694 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001695 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1696 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1697 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001698
1699 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001700 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001701 windowSecond->assertNoEvents();
1702}
1703
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001704/**
1705 * Two windows: A top window, and a wallpaper behind the window.
1706 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1707 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001708 * 1. foregroundWindow <-- dup touch to wallpaper
1709 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001710 */
1711TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1712 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1713 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001714 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001715 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001716 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001717 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001718 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001719
1720 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1721 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1722 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1723 {100, 200}))
1724 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1725
1726 // Both foreground window and its wallpaper should receive the touch down
1727 foregroundWindow->consumeMotionDown();
1728 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1729
1730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1731 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1732 ADISPLAY_ID_DEFAULT, {110, 200}))
1733 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1734
1735 foregroundWindow->consumeMotionMove();
1736 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1737
1738 // Now the foreground window goes away, but the wallpaper stays
1739 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1740 foregroundWindow->consumeMotionCancel();
1741 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1742 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1743}
1744
1745/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001746 * Two fingers down on the window, and lift off the first finger.
1747 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1748 * contains a single pointer.
1749 */
1750TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1751 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1752 sp<FakeWindowHandle> window =
1753 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1754
1755 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001756 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001757 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1758 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1759 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001760 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001761 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1762 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1763 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1764 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001765 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001766 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1767 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1768 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1769 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001770 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1771 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1772 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1773
1774 // Remove the window. The gesture should be canceled
1775 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
1776 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1777 window->consumeMotionEvent(
1778 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1779}
1780
1781/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001782 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1783 * with the following differences:
1784 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1785 * clean up the connection.
1786 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1787 * Ensure that there's no crash in the dispatcher.
1788 */
1789TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1790 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1791 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001792 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001793 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001794 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001795 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001796 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001797
1798 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1799 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1800 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1801 {100, 200}))
1802 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1803
1804 // Both foreground window and its wallpaper should receive the touch down
1805 foregroundWindow->consumeMotionDown();
1806 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1807
1808 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1809 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1810 ADISPLAY_ID_DEFAULT, {110, 200}))
1811 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1812
1813 foregroundWindow->consumeMotionMove();
1814 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1815
1816 // Wallpaper closes its channel, but the window remains.
1817 wallpaperWindow->destroyReceiver();
1818 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1819
1820 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1821 // is no longer valid.
1822 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1823 foregroundWindow->consumeMotionCancel();
1824}
1825
Arthur Hungc539dbb2022-12-08 07:45:36 +00001826class ShouldSplitTouchFixture : public InputDispatcherTest,
1827 public ::testing::WithParamInterface<bool> {};
1828INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1829 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001830/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001831 * A single window that receives touch (on top), and a wallpaper window underneath it.
1832 * The top window gets a multitouch gesture.
1833 * Ensure that wallpaper gets the same gesture.
1834 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00001835TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001836 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001837 sp<FakeWindowHandle> foregroundWindow =
1838 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1839 foregroundWindow->setDupTouchToWallpaper(true);
1840 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001841
1842 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001843 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001844 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001845
Arthur Hungc539dbb2022-12-08 07:45:36 +00001846 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001847
1848 // Touch down on top window
1849 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1850 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1851 {100, 100}))
1852 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1853
1854 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00001855 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001856 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1857
1858 // Second finger down on the top window
1859 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001860 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001861 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001862 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1863 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001864 .build();
1865 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1866 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1867 InputEventInjectionSync::WAIT_FOR_RESULT))
1868 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1869
Harry Cutts33476232023-01-30 19:57:29 +00001870 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
1871 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001872 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001873
1874 const MotionEvent secondFingerUpEvent =
1875 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1876 .displayId(ADISPLAY_ID_DEFAULT)
1877 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001878 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1879 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001880 .build();
1881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1882 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
1883 InputEventInjectionSync::WAIT_FOR_RESULT))
1884 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1885 foregroundWindow->consumeMotionPointerUp(0);
1886 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1887
1888 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001889 injectMotionEvent(mDispatcher,
1890 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
1891 AINPUT_SOURCE_TOUCHSCREEN)
1892 .displayId(ADISPLAY_ID_DEFAULT)
1893 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1894 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001895 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001896 .x(100)
1897 .y(100))
1898 .build(),
1899 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001900 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1901 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1902 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001903}
1904
1905/**
1906 * Two windows: a window on the left and window on the right.
1907 * A third window, wallpaper, is behind both windows, and spans both top windows.
1908 * The first touch down goes to the left window. A second pointer touches down on the right window.
1909 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1910 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1911 * ACTION_POINTER_DOWN(1).
1912 */
1913TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1914 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1915 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001916 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001917 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001918 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001919
1920 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001921 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001922 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001923 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001924
1925 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001926 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001927 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001928 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001929
1930 mDispatcher->setInputWindows(
1931 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
1932
1933 // Touch down on left window
1934 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1935 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1936 {100, 100}))
1937 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1938
1939 // Both foreground window and its wallpaper should receive the touch down
1940 leftWindow->consumeMotionDown();
1941 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1942
1943 // Second finger down on the right window
1944 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001945 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001946 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001947 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1948 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001949 .build();
1950 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1951 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1952 InputEventInjectionSync::WAIT_FOR_RESULT))
1953 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1954
1955 leftWindow->consumeMotionMove();
1956 // Since the touch is split, right window gets ACTION_DOWN
1957 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00001958 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001959 expectedWallpaperFlags);
1960
1961 // Now, leftWindow, which received the first finger, disappears.
1962 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
1963 leftWindow->consumeMotionCancel();
1964 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1965 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1966
1967 // The pointer that's still down on the right window moves, and goes to the right window only.
1968 // As far as the dispatcher's concerned though, both pointers are still present.
1969 const MotionEvent secondFingerMoveEvent =
1970 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1971 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001972 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1973 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001974 .build();
1975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1976 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
1977 InputEventInjectionSync::WAIT_FOR_RESULT));
1978 rightWindow->consumeMotionMove();
1979
1980 leftWindow->assertNoEvents();
1981 rightWindow->assertNoEvents();
1982 wallpaperWindow->assertNoEvents();
1983}
1984
Arthur Hungc539dbb2022-12-08 07:45:36 +00001985/**
1986 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
1987 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
1988 * The right window should receive ACTION_DOWN.
1989 */
1990TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00001991 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001992 sp<FakeWindowHandle> leftWindow =
1993 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1994 leftWindow->setFrame(Rect(0, 0, 200, 200));
1995 leftWindow->setDupTouchToWallpaper(true);
1996 leftWindow->setSlippery(true);
1997
1998 sp<FakeWindowHandle> rightWindow =
1999 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2000 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002001
2002 sp<FakeWindowHandle> wallpaperWindow =
2003 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2004 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002005
Arthur Hungc539dbb2022-12-08 07:45:36 +00002006 mDispatcher->setInputWindows(
2007 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002008
Arthur Hungc539dbb2022-12-08 07:45:36 +00002009 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002010 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2011 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002012 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002013 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002014
2015 // Both foreground window and its wallpaper should receive the touch down
2016 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002017 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2018
Arthur Hungc539dbb2022-12-08 07:45:36 +00002019 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002020 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002021 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2022 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002023 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2024
Arthur Hungc539dbb2022-12-08 07:45:36 +00002025 leftWindow->consumeMotionCancel();
2026 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2027 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002028}
2029
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002030/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002031 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2032 * interactive, it might stop sending this flag.
2033 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2034 * to have a consistent input stream.
2035 *
2036 * Test procedure:
2037 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2038 * DOWN (new gesture).
2039 *
2040 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2041 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2042 *
2043 * We technically just need a single window here, but we are using two windows (spy on top and a
2044 * regular window below) to emulate the actual situation where it happens on the device.
2045 */
2046TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2047 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2048 sp<FakeWindowHandle> spyWindow =
2049 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2050 spyWindow->setFrame(Rect(0, 0, 200, 200));
2051 spyWindow->setTrustedOverlay(true);
2052 spyWindow->setSpy(true);
2053
2054 sp<FakeWindowHandle> window =
2055 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2056 window->setFrame(Rect(0, 0, 200, 200));
2057
2058 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2059 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002060
2061 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002062 mDispatcher->notifyMotion(
2063 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2064 .deviceId(touchDeviceId)
2065 .policyFlags(DEFAULT_POLICY_FLAGS)
2066 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2067 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002068
Prabir Pradhan678438e2023-04-13 19:32:51 +00002069 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2070 .deviceId(touchDeviceId)
2071 .policyFlags(DEFAULT_POLICY_FLAGS)
2072 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2073 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2074 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002075 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2076 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2077 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2078 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2079
2080 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002081 mDispatcher->notifyMotion(
2082 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2083 .deviceId(touchDeviceId)
2084 .policyFlags(0)
2085 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2086 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2087 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002088 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2089 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2090
2091 // We don't need to reset the device to reproduce the issue, but the reset event typically
2092 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002093 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002094
2095 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002096 mDispatcher->notifyMotion(
2097 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2098 .deviceId(touchDeviceId)
2099 .policyFlags(DEFAULT_POLICY_FLAGS)
2100 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2101 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002102 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2103 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2104
2105 // No more events
2106 spyWindow->assertNoEvents();
2107 window->assertNoEvents();
2108}
2109
2110/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002111 * Two windows: a window on the left and a window on the right.
2112 * Mouse is hovered from the right window into the left window.
2113 * Next, we tap on the left window, where the cursor was last seen.
2114 * The second tap is done onto the right window.
2115 * The mouse and tap are from two different devices.
2116 * We technically don't need to set the downtime / eventtime for these events, but setting these
2117 * explicitly helps during debugging.
2118 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2119 * In the buggy implementation, a tap on the right window would cause a crash.
2120 */
2121TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2122 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2123 sp<FakeWindowHandle> leftWindow =
2124 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2125 leftWindow->setFrame(Rect(0, 0, 200, 200));
2126
2127 sp<FakeWindowHandle> rightWindow =
2128 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2129 rightWindow->setFrame(Rect(200, 0, 400, 200));
2130
2131 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2132 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2133 // stale.
2134 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2135 const int32_t mouseDeviceId = 6;
2136 const int32_t touchDeviceId = 4;
2137 // Move the cursor from right
2138 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2139 injectMotionEvent(mDispatcher,
2140 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2141 AINPUT_SOURCE_MOUSE)
2142 .deviceId(mouseDeviceId)
2143 .downTime(baseTime + 10)
2144 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002145 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002146 .x(300)
2147 .y(100))
2148 .build()));
2149 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2150
2151 // .. to the left window
2152 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2153 injectMotionEvent(mDispatcher,
2154 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2155 AINPUT_SOURCE_MOUSE)
2156 .deviceId(mouseDeviceId)
2157 .downTime(baseTime + 10)
2158 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002159 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002160 .x(110)
2161 .y(100))
2162 .build()));
2163 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2164 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2165 // Now tap the left window
2166 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2167 injectMotionEvent(mDispatcher,
2168 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2169 AINPUT_SOURCE_TOUCHSCREEN)
2170 .deviceId(touchDeviceId)
2171 .downTime(baseTime + 40)
2172 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002173 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002174 .x(100)
2175 .y(100))
2176 .build()));
2177 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2178 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2179
2180 // release tap
2181 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2182 injectMotionEvent(mDispatcher,
2183 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2184 AINPUT_SOURCE_TOUCHSCREEN)
2185 .deviceId(touchDeviceId)
2186 .downTime(baseTime + 40)
2187 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002188 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002189 .x(100)
2190 .y(100))
2191 .build()));
2192 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2193
2194 // Tap the window on the right
2195 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2196 injectMotionEvent(mDispatcher,
2197 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2198 AINPUT_SOURCE_TOUCHSCREEN)
2199 .deviceId(touchDeviceId)
2200 .downTime(baseTime + 60)
2201 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002202 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002203 .x(300)
2204 .y(100))
2205 .build()));
2206 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2207
2208 // release tap
2209 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2210 injectMotionEvent(mDispatcher,
2211 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2212 AINPUT_SOURCE_TOUCHSCREEN)
2213 .deviceId(touchDeviceId)
2214 .downTime(baseTime + 60)
2215 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002216 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002217 .x(300)
2218 .y(100))
2219 .build()));
2220 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2221
2222 // No more events
2223 leftWindow->assertNoEvents();
2224 rightWindow->assertNoEvents();
2225}
2226
2227/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002228 * Start hovering in a window. While this hover is still active, make another window appear on top.
2229 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2230 * While the top window is present, the hovering is stopped.
2231 * Later, hovering gets resumed again.
2232 * Ensure that new hover gesture is handled correctly.
2233 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2234 * to the window that's currently being hovered over.
2235 */
2236TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2237 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2238 sp<FakeWindowHandle> window =
2239 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2240 window->setFrame(Rect(0, 0, 200, 200));
2241
2242 // Only a single window is present at first
2243 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2244
2245 // Start hovering in the window
2246 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2247 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2248 .build());
2249 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2250
2251 // Now, an obscuring window appears!
2252 sp<FakeWindowHandle> obscuringWindow =
2253 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2254 ADISPLAY_ID_DEFAULT,
2255 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2256 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2257 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2258 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2259 obscuringWindow->setNoInputChannel(true);
2260 obscuringWindow->setFocusable(false);
2261 obscuringWindow->setAlpha(1.0);
2262 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2263
2264 // While this new obscuring window is present, the hovering is stopped
2265 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2266 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2267 .build());
2268 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2269
2270 // Now the obscuring window goes away.
2271 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2272
2273 // And a new hover gesture starts.
2274 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2275 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2276 .build());
2277 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2278}
2279
2280/**
2281 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2282 * the obscuring window.
2283 */
2284TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2285 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2286 sp<FakeWindowHandle> window =
2287 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2288 window->setFrame(Rect(0, 0, 200, 200));
2289
2290 // Only a single window is present at first
2291 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2292
2293 // Start hovering in the window
2294 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2295 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2296 .build());
2297 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2298
2299 // Now, an obscuring window appears!
2300 sp<FakeWindowHandle> obscuringWindow =
2301 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2302 ADISPLAY_ID_DEFAULT,
2303 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2304 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2305 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2306 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2307 obscuringWindow->setNoInputChannel(true);
2308 obscuringWindow->setFocusable(false);
2309 obscuringWindow->setAlpha(1.0);
2310 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2311
2312 // While this new obscuring window is present, the hovering continues. The event can't go to the
2313 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2314 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2315 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2316 .build());
2317 obscuringWindow->assertNoEvents();
2318 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2319
2320 // Now the obscuring window goes away.
2321 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2322
2323 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2324 // so it should generate a HOVER_ENTER
2325 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2326 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2327 .build());
2328 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2329
2330 // Now the MOVE should be getting dispatched normally
2331 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2332 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2333 .build());
2334 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2335}
2336
2337/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002338 * Two windows: a window on the left and a window on the right.
2339 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2340 * down. Then, on the left window, also place second touch pointer down.
2341 * This test tries to reproduce a crash.
2342 * In the buggy implementation, second pointer down on the left window would cause a crash.
2343 */
2344TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2345 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2346 sp<FakeWindowHandle> leftWindow =
2347 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2348 leftWindow->setFrame(Rect(0, 0, 200, 200));
2349
2350 sp<FakeWindowHandle> rightWindow =
2351 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2352 rightWindow->setFrame(Rect(200, 0, 400, 200));
2353
2354 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2355
2356 const int32_t touchDeviceId = 4;
2357 const int32_t mouseDeviceId = 6;
2358 NotifyMotionArgs args;
2359
2360 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002361 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2362 .deviceId(mouseDeviceId)
2363 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2364 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002365 leftWindow->consumeMotionEvent(
2366 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2367
2368 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002369 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2370 .deviceId(mouseDeviceId)
2371 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2372 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2373 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002374
2375 leftWindow->consumeMotionEvent(
2376 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2377 leftWindow->consumeMotionEvent(
2378 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2379
Prabir Pradhan678438e2023-04-13 19:32:51 +00002380 mDispatcher->notifyMotion(
2381 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2382 .deviceId(mouseDeviceId)
2383 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2384 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2385 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2386 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002387 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2388
2389 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002390 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2391 .deviceId(touchDeviceId)
2392 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2393 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002394 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2395
2396 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2397
2398 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002399 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2400 .deviceId(touchDeviceId)
2401 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2402 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2403 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002404 leftWindow->consumeMotionEvent(
2405 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2406 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2407 // current implementation.
2408 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2409 rightWindow->consumeMotionEvent(
2410 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2411
2412 leftWindow->assertNoEvents();
2413 rightWindow->assertNoEvents();
2414}
2415
2416/**
2417 * On a single window, use two different devices: mouse and touch.
2418 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2419 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2420 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2421 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2422 * represent a new gesture.
2423 */
2424TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2425 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2426 sp<FakeWindowHandle> window =
2427 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2428 window->setFrame(Rect(0, 0, 400, 400));
2429
2430 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2431
2432 const int32_t touchDeviceId = 4;
2433 const int32_t mouseDeviceId = 6;
2434 NotifyMotionArgs args;
2435
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002436 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002437 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2438 .deviceId(touchDeviceId)
2439 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2440 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002441 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002442 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2443 .deviceId(touchDeviceId)
2444 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2445 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2446 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002447 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002448 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2449 .deviceId(touchDeviceId)
2450 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2451 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2452 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002453 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2454 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2455 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2456
2457 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002458 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2459 .deviceId(mouseDeviceId)
2460 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2461 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2462 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002463
2464 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002465 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002466 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2467
Prabir Pradhan678438e2023-04-13 19:32:51 +00002468 mDispatcher->notifyMotion(
2469 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2470 .deviceId(mouseDeviceId)
2471 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2472 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2473 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2474 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002475 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2476
2477 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002478 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2479 .deviceId(touchDeviceId)
2480 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2481 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2482 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002483 // The pointer_down event should be ignored
2484 window->assertNoEvents();
2485}
2486
2487/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002488 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2489 * the injected event.
2490 */
2491TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2492 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2493 sp<FakeWindowHandle> window =
2494 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2495 window->setFrame(Rect(0, 0, 400, 400));
2496
2497 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2498
2499 const int32_t touchDeviceId = 4;
2500 NotifyMotionArgs args;
2501 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2502 // completion.
2503 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2504 injectMotionEvent(mDispatcher,
2505 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2506 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002507 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002508 .x(50)
2509 .y(50))
2510 .build()));
2511 window->consumeMotionEvent(
2512 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2513
2514 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2515 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002516 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2517 .deviceId(touchDeviceId)
2518 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2519 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002520
2521 window->consumeMotionEvent(
2522 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2523 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2524}
2525
2526/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002527 * This test is similar to the test above, but the sequence of injected events is different.
2528 *
2529 * Two windows: a window on the left and a window on the right.
2530 * Mouse is hovered over the left window.
2531 * Next, we tap on the left window, where the cursor was last seen.
2532 *
2533 * After that, we inject one finger down onto the right window, and then a second finger down onto
2534 * the left window.
2535 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2536 * window (first), and then another on the left window (second).
2537 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2538 * In the buggy implementation, second finger down on the left window would cause a crash.
2539 */
2540TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2541 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2542 sp<FakeWindowHandle> leftWindow =
2543 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2544 leftWindow->setFrame(Rect(0, 0, 200, 200));
2545
2546 sp<FakeWindowHandle> rightWindow =
2547 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2548 rightWindow->setFrame(Rect(200, 0, 400, 200));
2549
2550 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2551
2552 const int32_t mouseDeviceId = 6;
2553 const int32_t touchDeviceId = 4;
2554 // Hover over the left window. Keep the cursor there.
2555 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2556 injectMotionEvent(mDispatcher,
2557 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2558 AINPUT_SOURCE_MOUSE)
2559 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002560 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002561 .x(50)
2562 .y(50))
2563 .build()));
2564 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2565
2566 // Tap on left window
2567 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2568 injectMotionEvent(mDispatcher,
2569 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2570 AINPUT_SOURCE_TOUCHSCREEN)
2571 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002572 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002573 .x(100)
2574 .y(100))
2575 .build()));
2576
2577 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2578 injectMotionEvent(mDispatcher,
2579 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2580 AINPUT_SOURCE_TOUCHSCREEN)
2581 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002582 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002583 .x(100)
2584 .y(100))
2585 .build()));
2586 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2587 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2588 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2589
2590 // First finger down on right window
2591 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2592 injectMotionEvent(mDispatcher,
2593 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2594 AINPUT_SOURCE_TOUCHSCREEN)
2595 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002596 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002597 .x(300)
2598 .y(100))
2599 .build()));
2600 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2601
2602 // Second finger down on the left window
2603 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2604 injectMotionEvent(mDispatcher,
2605 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2606 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002607 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002608 .x(300)
2609 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002610 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002611 .x(100)
2612 .y(100))
2613 .build()));
2614 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2615 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2616
2617 // No more events
2618 leftWindow->assertNoEvents();
2619 rightWindow->assertNoEvents();
2620}
2621
2622/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002623 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2624 * While the touch is down, new hover events from the stylus device should be ignored. After the
2625 * touch is gone, stylus hovering should start working again.
2626 */
2627TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2628 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2629 sp<FakeWindowHandle> window =
2630 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2631 window->setFrame(Rect(0, 0, 200, 200));
2632
2633 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2634
2635 const int32_t stylusDeviceId = 5;
2636 const int32_t touchDeviceId = 4;
2637 // Start hovering with stylus
2638 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2639 injectMotionEvent(mDispatcher,
2640 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2641 AINPUT_SOURCE_STYLUS)
2642 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002643 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002644 .x(50)
2645 .y(50))
2646 .build()));
2647 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2648
2649 // Finger down on the window
2650 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2651 injectMotionEvent(mDispatcher,
2652 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2653 AINPUT_SOURCE_TOUCHSCREEN)
2654 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002655 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002656 .x(100)
2657 .y(100))
2658 .build()));
2659 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2660 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2661
2662 // Try to continue hovering with stylus. Since we are already down, injection should fail
2663 ASSERT_EQ(InputEventInjectionResult::FAILED,
2664 injectMotionEvent(mDispatcher,
2665 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2666 AINPUT_SOURCE_STYLUS)
2667 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002668 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002669 .x(50)
2670 .y(50))
2671 .build()));
2672 // No event should be sent. This event should be ignored because a pointer from another device
2673 // is already down.
2674
2675 // Lift up the finger
2676 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2677 injectMotionEvent(mDispatcher,
2678 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2679 AINPUT_SOURCE_TOUCHSCREEN)
2680 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002681 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002682 .x(100)
2683 .y(100))
2684 .build()));
2685 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2686
2687 // Now that the touch is gone, stylus hovering should start working again
2688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2689 injectMotionEvent(mDispatcher,
2690 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2691 AINPUT_SOURCE_STYLUS)
2692 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002693 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002694 .x(50)
2695 .y(50))
2696 .build()));
2697 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2698 // No more events
2699 window->assertNoEvents();
2700}
2701
2702/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002703 * A spy window above a window with no input channel.
2704 * Start hovering with a stylus device, and then tap with it.
2705 * Ensure spy window receives the entire sequence.
2706 */
2707TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2708 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2709 sp<FakeWindowHandle> spyWindow =
2710 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2711 spyWindow->setFrame(Rect(0, 0, 200, 200));
2712 spyWindow->setTrustedOverlay(true);
2713 spyWindow->setSpy(true);
2714 sp<FakeWindowHandle> window =
2715 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2716 window->setNoInputChannel(true);
2717 window->setFrame(Rect(0, 0, 200, 200));
2718
2719 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2720
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002721 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002722 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2723 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2724 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002725 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2726 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002727 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2728 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2729 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002730 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2731
2732 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002733 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2734 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2735 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002736 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2737
2738 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002739 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
2740 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2741 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002742 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2743
2744 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002745 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2746 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2747 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002748 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2749 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002750 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2751 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2752 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002753 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2754
2755 // No more events
2756 spyWindow->assertNoEvents();
2757 window->assertNoEvents();
2758}
2759
2760/**
2761 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
2762 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
2763 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
2764 * While the mouse is down, new move events from the touch device should be ignored.
2765 */
2766TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
2767 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2768 sp<FakeWindowHandle> spyWindow =
2769 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2770 spyWindow->setFrame(Rect(0, 0, 200, 200));
2771 spyWindow->setTrustedOverlay(true);
2772 spyWindow->setSpy(true);
2773 sp<FakeWindowHandle> window =
2774 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2775 window->setFrame(Rect(0, 0, 200, 200));
2776
2777 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2778
2779 const int32_t mouseDeviceId = 7;
2780 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002781
2782 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00002783 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2784 .deviceId(mouseDeviceId)
2785 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2786 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002787 spyWindow->consumeMotionEvent(
2788 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2789 window->consumeMotionEvent(
2790 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2791
2792 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00002793 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2794 .deviceId(touchDeviceId)
2795 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
2796 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002797 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2798 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2799 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2800 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2801
Prabir Pradhan678438e2023-04-13 19:32:51 +00002802 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2803 .deviceId(touchDeviceId)
2804 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
2805 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002806 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2807 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2808
2809 // Pilfer the stream
2810 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2811 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2812
Prabir Pradhan678438e2023-04-13 19:32:51 +00002813 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2814 .deviceId(touchDeviceId)
2815 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
2816 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002817 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2818
2819 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002820 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2821 .deviceId(mouseDeviceId)
2822 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2823 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2824 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002825
2826 spyWindow->consumeMotionEvent(
2827 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2828 spyWindow->consumeMotionEvent(
2829 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2830 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2831
Prabir Pradhan678438e2023-04-13 19:32:51 +00002832 mDispatcher->notifyMotion(
2833 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2834 .deviceId(mouseDeviceId)
2835 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2836 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2837 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2838 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002839 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2840 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2841
2842 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002843 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
2844 .deviceId(mouseDeviceId)
2845 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2846 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
2847 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002848 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2849 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2850
2851 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002852 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2853 .deviceId(touchDeviceId)
2854 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
2855 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002856
2857 // No more events
2858 spyWindow->assertNoEvents();
2859 window->assertNoEvents();
2860}
2861
2862/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002863 * On the display, have a single window, and also an area where there's no window.
2864 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2865 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2866 */
2867TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2868 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2869 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002870 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002871
2872 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002873
2874 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00002875 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002876
2877 mDispatcher->waitForIdle();
2878 window->assertNoEvents();
2879
2880 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002881 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002882 mDispatcher->waitForIdle();
2883 window->consumeMotionDown();
2884}
2885
2886/**
2887 * Same test as above, but instead of touching the empty space, the first touch goes to
2888 * non-touchable window.
2889 */
2890TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2891 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2892 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002893 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002894 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2895 window1->setTouchable(false);
2896 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002897 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002898 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2899
2900 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2901
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002902 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002903 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002904
2905 mDispatcher->waitForIdle();
2906 window1->assertNoEvents();
2907 window2->assertNoEvents();
2908
2909 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002910 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002911 mDispatcher->waitForIdle();
2912 window2->consumeMotionDown();
2913}
2914
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002915/**
2916 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2917 * to the event time of the first ACTION_DOWN sent to the particular window.
2918 */
2919TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2920 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2921 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002922 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002923 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2924 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002925 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002926 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2927
2928 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2929
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002930 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002931 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002932
2933 mDispatcher->waitForIdle();
2934 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002935 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002936 window2->assertNoEvents();
2937 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2938 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2939 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2940
2941 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002942 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002943 mDispatcher->waitForIdle();
2944 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002945 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002946 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2947 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2948 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2949 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2950
2951 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002952 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002953 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002954 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002955
2956 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002957 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002958 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002959 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002960
2961 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
2962 window1->consumeMotionMove();
2963 window1->assertNoEvents();
2964
2965 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002966 mDispatcher->notifyMotion(
2967 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002968 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002969 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002970
Prabir Pradhan678438e2023-04-13 19:32:51 +00002971 mDispatcher->notifyMotion(
2972 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002973 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002974 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002975}
2976
Garfield Tandf26e862020-07-01 20:18:19 -07002977TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002978 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002979 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002980 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002981 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002982 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002983 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002984 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002985
2986 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2987
2988 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
2989
2990 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002991 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002992 injectMotionEvent(mDispatcher,
2993 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2994 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002995 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07002996 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002997 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002998
2999 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003000 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003001 injectMotionEvent(mDispatcher,
3002 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3003 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003004 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003005 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003006 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3007 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003008
3009 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003010 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003011 injectMotionEvent(mDispatcher,
3012 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3013 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003014 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003015 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003016 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3017 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003018
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003019 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003020 injectMotionEvent(mDispatcher,
3021 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3022 AINPUT_SOURCE_MOUSE)
3023 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3024 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003025 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003026 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003027 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003028
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003029 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003030 injectMotionEvent(mDispatcher,
3031 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3032 AINPUT_SOURCE_MOUSE)
3033 .buttonState(0)
3034 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003035 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003036 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003037 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003038
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003039 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003040 injectMotionEvent(mDispatcher,
3041 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3042 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003043 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003044 .build()));
3045 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3046
3047 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003048 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003049 injectMotionEvent(mDispatcher,
3050 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3051 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003052 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003053 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003054 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003055
3056 // No more events
3057 windowLeft->assertNoEvents();
3058 windowRight->assertNoEvents();
3059}
3060
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003061/**
3062 * Put two fingers down (and don't release them) and click the mouse button.
3063 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3064 * currently active gesture should be canceled, and the new one should proceed.
3065 */
3066TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3067 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3068 sp<FakeWindowHandle> window =
3069 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3070 window->setFrame(Rect(0, 0, 600, 800));
3071
3072 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3073
3074 const int32_t touchDeviceId = 4;
3075 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003076
3077 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003078 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3079 .deviceId(touchDeviceId)
3080 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3081 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003082
Prabir Pradhan678438e2023-04-13 19:32:51 +00003083 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3084 .deviceId(touchDeviceId)
3085 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3086 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3087 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003088 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3089 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3090
3091 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003092 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3093 .deviceId(mouseDeviceId)
3094 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3095 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3096 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003097 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3098 WithPointerCount(2u)));
3099 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3100
Prabir Pradhan678438e2023-04-13 19:32:51 +00003101 mDispatcher->notifyMotion(
3102 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3103 .deviceId(mouseDeviceId)
3104 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3105 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3106 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3107 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003108 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3109
3110 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3111 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003112 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3113 .deviceId(touchDeviceId)
3114 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3115 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3116 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003117 window->assertNoEvents();
3118}
3119
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003120TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3121 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3122
3123 sp<FakeWindowHandle> spyWindow =
3124 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3125 spyWindow->setFrame(Rect(0, 0, 600, 800));
3126 spyWindow->setTrustedOverlay(true);
3127 spyWindow->setSpy(true);
3128 sp<FakeWindowHandle> window =
3129 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3130 window->setFrame(Rect(0, 0, 600, 800));
3131
3132 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3133 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3134
3135 // Send mouse cursor to the window
3136 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3137 injectMotionEvent(mDispatcher,
3138 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3139 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003140 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003141 .x(100)
3142 .y(100))
3143 .build()));
3144
3145 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3146 WithSource(AINPUT_SOURCE_MOUSE)));
3147 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3148 WithSource(AINPUT_SOURCE_MOUSE)));
3149
3150 window->assertNoEvents();
3151 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003152}
3153
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003154TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3155 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3156
3157 sp<FakeWindowHandle> spyWindow =
3158 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3159 spyWindow->setFrame(Rect(0, 0, 600, 800));
3160 spyWindow->setTrustedOverlay(true);
3161 spyWindow->setSpy(true);
3162 sp<FakeWindowHandle> window =
3163 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3164 window->setFrame(Rect(0, 0, 600, 800));
3165
3166 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3167 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3168
3169 // Send mouse cursor to the window
3170 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3171 injectMotionEvent(mDispatcher,
3172 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3173 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003174 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003175 .x(100)
3176 .y(100))
3177 .build()));
3178
3179 // Move mouse cursor
3180 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3181 injectMotionEvent(mDispatcher,
3182 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3183 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003184 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003185 .x(110)
3186 .y(110))
3187 .build()));
3188
3189 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3190 WithSource(AINPUT_SOURCE_MOUSE)));
3191 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3192 WithSource(AINPUT_SOURCE_MOUSE)));
3193 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3194 WithSource(AINPUT_SOURCE_MOUSE)));
3195 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3196 WithSource(AINPUT_SOURCE_MOUSE)));
3197 // Touch down on the window
3198 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3199 injectMotionEvent(mDispatcher,
3200 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3201 AINPUT_SOURCE_TOUCHSCREEN)
3202 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003203 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003204 .x(200)
3205 .y(200))
3206 .build()));
3207 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3208 WithSource(AINPUT_SOURCE_MOUSE)));
3209 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3210 WithSource(AINPUT_SOURCE_MOUSE)));
3211 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3212 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3213 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3214 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3215
3216 // pilfer the motion, retaining the gesture on the spy window.
3217 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3218 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3219 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3220
3221 // Touch UP on the window
3222 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3223 injectMotionEvent(mDispatcher,
3224 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3225 AINPUT_SOURCE_TOUCHSCREEN)
3226 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003227 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003228 .x(200)
3229 .y(200))
3230 .build()));
3231 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3232 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3233
3234 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3235 // to send a new gesture. It should again go to both windows (spy and the window below), just
3236 // like the first gesture did, before pilfering. The window configuration has not changed.
3237
3238 // One more tap - DOWN
3239 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3240 injectMotionEvent(mDispatcher,
3241 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3242 AINPUT_SOURCE_TOUCHSCREEN)
3243 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003244 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003245 .x(250)
3246 .y(250))
3247 .build()));
3248 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3249 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3250 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3251 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3252
3253 // Touch UP on the window
3254 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3255 injectMotionEvent(mDispatcher,
3256 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3257 AINPUT_SOURCE_TOUCHSCREEN)
3258 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003259 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003260 .x(250)
3261 .y(250))
3262 .build()));
3263 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3264 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3265 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3266 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3267
3268 window->assertNoEvents();
3269 spyWindow->assertNoEvents();
3270}
3271
Garfield Tandf26e862020-07-01 20:18:19 -07003272// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3273// directly in this test.
3274TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003275 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003276 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003277 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003278 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003279
3280 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3281
3282 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3283
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003284 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003285 injectMotionEvent(mDispatcher,
3286 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3287 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003288 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003289 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003290 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003291 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003292 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003293 injectMotionEvent(mDispatcher,
3294 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3295 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003296 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003297 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003298 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3299 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003300
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003301 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003302 injectMotionEvent(mDispatcher,
3303 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3304 AINPUT_SOURCE_MOUSE)
3305 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3306 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003307 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003308 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003309 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003310
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003311 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003312 injectMotionEvent(mDispatcher,
3313 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3314 AINPUT_SOURCE_MOUSE)
3315 .buttonState(0)
3316 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003317 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003318 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003319 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003320
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003321 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003322 injectMotionEvent(mDispatcher,
3323 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3324 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003325 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003326 .build()));
3327 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3328
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07003329 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
3330 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
3331 ASSERT_EQ(InputEventInjectionResult::FAILED,
Garfield Tandf26e862020-07-01 20:18:19 -07003332 injectMotionEvent(mDispatcher,
3333 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3334 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003335 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003336 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003337 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003338}
3339
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003340/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003341 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3342 * is generated.
3343 */
3344TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3345 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3346 sp<FakeWindowHandle> window =
3347 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3348 window->setFrame(Rect(0, 0, 1200, 800));
3349
3350 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3351
3352 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3353
3354 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3355 injectMotionEvent(mDispatcher,
3356 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3357 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003358 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003359 .x(300)
3360 .y(400))
3361 .build()));
3362 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3363
3364 // Remove the window, but keep the channel.
3365 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3366 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3367}
3368
3369/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003370 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3371 */
3372TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3373 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3374 sp<FakeWindowHandle> window =
3375 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3376 window->setFrame(Rect(0, 0, 100, 100));
3377
3378 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3379
3380 const int32_t mouseDeviceId = 7;
3381 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003382
3383 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003384 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3385 .deviceId(mouseDeviceId)
3386 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3387 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003388 window->consumeMotionEvent(
3389 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3390
3391 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003392 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3393 .deviceId(touchDeviceId)
3394 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3395 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003396
3397 window->consumeMotionEvent(
3398 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3399 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3400}
3401
3402/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003403 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003404 * The tap causes a HOVER_EXIT event to be generated because the current event
3405 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003406 */
3407TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3408 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3409 sp<FakeWindowHandle> window =
3410 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3411 window->setFrame(Rect(0, 0, 100, 100));
3412
3413 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3414
3415 // Inject a hover_move from mouse.
3416 NotifyMotionArgs motionArgs =
3417 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3418 ADISPLAY_ID_DEFAULT, {{50, 50}});
3419 motionArgs.xCursorPosition = 50;
3420 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003421 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003422 ASSERT_NO_FATAL_FAILURE(
3423 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3424 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003425
3426 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003427 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3428 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3429 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003430 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003431 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3432 WithSource(AINPUT_SOURCE_MOUSE))));
3433
3434 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003435 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3436 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3437
Prabir Pradhan678438e2023-04-13 19:32:51 +00003438 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3439 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003440 ASSERT_NO_FATAL_FAILURE(
3441 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3442 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3443}
3444
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003445TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3446 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3447 sp<FakeWindowHandle> windowDefaultDisplay =
3448 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3449 ADISPLAY_ID_DEFAULT);
3450 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3451 sp<FakeWindowHandle> windowSecondDisplay =
3452 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3453 SECOND_DISPLAY_ID);
3454 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3455
3456 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3457 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3458
3459 // Set cursor position in window in default display and check that hover enter and move
3460 // events are generated.
3461 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3462 injectMotionEvent(mDispatcher,
3463 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3464 AINPUT_SOURCE_MOUSE)
3465 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003466 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003467 .x(300)
3468 .y(600))
3469 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003470 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003471
3472 // Remove all windows in secondary display and check that no event happens on window in
3473 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003474 mDispatcher->setInputWindows(
3475 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003476 windowDefaultDisplay->assertNoEvents();
3477
3478 // Move cursor position in window in default display and check that only hover move
3479 // event is generated and not hover enter event.
3480 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3481 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3482 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3483 injectMotionEvent(mDispatcher,
3484 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3485 AINPUT_SOURCE_MOUSE)
3486 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003487 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003488 .x(400)
3489 .y(700))
3490 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003491 windowDefaultDisplay->consumeMotionEvent(
3492 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3493 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003494 windowDefaultDisplay->assertNoEvents();
3495}
3496
Garfield Tan00f511d2019-06-12 16:55:40 -07003497TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003498 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003499
3500 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003501 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003502 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003503 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003504 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003505 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003506
3507 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3508
Arthur Hung72d8dc32020-03-28 00:48:39 +00003509 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003510
3511 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3512 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003513 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003514 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003515 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003516 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003517 windowRight->assertNoEvents();
3518}
3519
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003520TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003521 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003522 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3523 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003524 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003525
Arthur Hung72d8dc32020-03-28 00:48:39 +00003526 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003527 setFocusedWindow(window);
3528
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003529 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003530
Prabir Pradhan678438e2023-04-13 19:32:51 +00003531 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003532
3533 // Window should receive key down event.
3534 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3535
3536 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3537 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003538 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003539 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003540 AKEY_EVENT_FLAG_CANCELED);
3541}
3542
3543TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003544 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003545 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3546 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003547
Arthur Hung72d8dc32020-03-28 00:48:39 +00003548 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003549
Prabir Pradhan678438e2023-04-13 19:32:51 +00003550 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3551 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003552
3553 // Window should receive motion down event.
3554 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3555
3556 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3557 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003558 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003559 window->consumeMotionEvent(
3560 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003561}
3562
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003563TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
3564 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3565 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3566 "Fake Window", ADISPLAY_ID_DEFAULT);
3567
3568 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3569
3570 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3571 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
3572 .build());
3573
3574 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3575
3576 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
3577 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
3578 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3579
3580 // After the device has been reset, a new hovering stream can be sent to the window
3581 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3582 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
3583 .build());
3584 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3585}
3586
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003587TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3588 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003589 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3590 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003591 window->setFocusable(true);
3592
3593 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3594 setFocusedWindow(window);
3595
3596 window->consumeFocusEvent(true);
3597
Prabir Pradhan678438e2023-04-13 19:32:51 +00003598 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003599 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3600 const nsecs_t injectTime = keyArgs.eventTime;
3601 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003602 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003603 // The dispatching time should be always greater than or equal to intercept key timeout.
3604 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3605 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3606 std::chrono::nanoseconds(interceptKeyTimeout).count());
3607}
3608
3609TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3610 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003611 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3612 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003613 window->setFocusable(true);
3614
3615 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3616 setFocusedWindow(window);
3617
3618 window->consumeFocusEvent(true);
3619
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003620 mFakePolicy->setInterceptKeyTimeout(150ms);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003621 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
3622 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003623
3624 // Window should receive key event immediately when same key up.
3625 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3626 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3627}
3628
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003629/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003630 * Two windows. First is a regular window. Second does not overlap with the first, and has
3631 * WATCH_OUTSIDE_TOUCH.
3632 * Both windows are owned by the same UID.
3633 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3634 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3635 */
3636TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3637 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3638 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3639 "First Window", ADISPLAY_ID_DEFAULT);
3640 window->setFrame(Rect{0, 0, 100, 100});
3641
3642 sp<FakeWindowHandle> outsideWindow =
3643 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3644 ADISPLAY_ID_DEFAULT);
3645 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3646 outsideWindow->setWatchOutsideTouch(true);
3647 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3648 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3649
3650 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003651 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3652 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3653 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003654 window->consumeMotionDown();
3655 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3656 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3657 outsideWindow->consumeMotionEvent(
3658 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3659}
3660
3661/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003662 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3663 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3664 * ACTION_OUTSIDE event is sent per gesture.
3665 */
3666TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3667 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3668 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003669 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3670 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003671 window->setWatchOutsideTouch(true);
3672 window->setFrame(Rect{0, 0, 100, 100});
3673 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003674 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3675 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003676 secondWindow->setFrame(Rect{100, 100, 200, 200});
3677 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003678 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3679 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003680 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3681 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3682
3683 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003684 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3685 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3686 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003687 window->assertNoEvents();
3688 secondWindow->assertNoEvents();
3689
3690 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3691 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003692 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3693 ADISPLAY_ID_DEFAULT,
3694 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003695 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3696 window->consumeMotionEvent(
3697 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003698 secondWindow->consumeMotionDown();
3699 thirdWindow->assertNoEvents();
3700
3701 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3702 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003703 mDispatcher->notifyMotion(
3704 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3705 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003706 window->assertNoEvents();
3707 secondWindow->consumeMotionMove();
3708 thirdWindow->consumeMotionDown();
3709}
3710
Prabir Pradhan814fe082022-07-22 20:22:18 +00003711TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3712 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003713 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3714 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003715 window->setFocusable(true);
3716
Patrick Williamsd828f302023-04-28 17:52:08 -05003717 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003718 setFocusedWindow(window);
3719
3720 window->consumeFocusEvent(true);
3721
Prabir Pradhan678438e2023-04-13 19:32:51 +00003722 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3723 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3724 mDispatcher->notifyKey(keyDown);
3725 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003726
3727 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3728 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3729
3730 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003731 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003732
3733 window->consumeFocusEvent(false);
3734
Prabir Pradhan678438e2023-04-13 19:32:51 +00003735 mDispatcher->notifyKey(keyDown);
3736 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003737 window->assertNoEvents();
3738}
3739
Arthur Hung96483742022-11-15 03:30:48 +00003740TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3741 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3742 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3743 "Fake Window", ADISPLAY_ID_DEFAULT);
3744 // Ensure window is non-split and have some transform.
3745 window->setPreventSplitting(true);
3746 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05003747 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00003748
3749 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3750 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3751 {50, 50}))
3752 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3753 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3754
3755 const MotionEvent secondFingerDownEvent =
3756 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3757 .displayId(ADISPLAY_ID_DEFAULT)
3758 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003759 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3760 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003761 .build();
3762 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3763 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3764 InputEventInjectionSync::WAIT_FOR_RESULT))
3765 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3766
3767 const MotionEvent* event = window->consumeMotion();
3768 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3769 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3770 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3771 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3772 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3773}
3774
Harry Cuttsb166c002023-05-09 13:06:05 +00003775TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
3776 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3777 sp<FakeWindowHandle> window =
3778 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3779 window->setFrame(Rect(0, 0, 400, 400));
3780 sp<FakeWindowHandle> trustedOverlay =
3781 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
3782 ADISPLAY_ID_DEFAULT);
3783 trustedOverlay->setSpy(true);
3784 trustedOverlay->setTrustedOverlay(true);
3785
3786 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}});
3787
3788 // Start a three-finger touchpad swipe
3789 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3790 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3791 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3792 .build());
3793 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3794 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3795 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3796 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3797 .build());
3798 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3799 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3800 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3801 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3802 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3803 .build());
3804
3805 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3806 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3807 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
3808
3809 // Move the swipe a bit
3810 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3811 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3812 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3813 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3814 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3815 .build());
3816
3817 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3818
3819 // End the swipe
3820 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3821 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3822 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3823 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3824 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3825 .build());
3826 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3827 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3828 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3829 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3830 .build());
3831 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3832 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3833 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3834 .build());
3835
3836 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
3837 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
3838 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
3839
3840 window->assertNoEvents();
3841}
3842
3843TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
3844 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3845 sp<FakeWindowHandle> window =
3846 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3847 window->setFrame(Rect(0, 0, 400, 400));
3848 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3849
3850 // Start a three-finger touchpad swipe
3851 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3852 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3853 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3854 .build());
3855 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3856 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3857 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3858 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3859 .build());
3860 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3861 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3862 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3863 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3864 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3865 .build());
3866
3867 // Move the swipe a bit
3868 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3869 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3870 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3871 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3872 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3873 .build());
3874
3875 // End the swipe
3876 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3877 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3878 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3879 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3880 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3881 .build());
3882 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3883 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3884 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3885 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3886 .build());
3887 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3888 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3889 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3890 .build());
3891
3892 window->assertNoEvents();
3893}
3894
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003895/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003896 * Ensure the correct coordinate spaces are used by InputDispatcher.
3897 *
3898 * InputDispatcher works in the display space, so its coordinate system is relative to the display
3899 * panel. Windows get events in the window space, and get raw coordinates in the logical display
3900 * space.
3901 */
3902class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
3903public:
3904 void SetUp() override {
3905 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003906 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003907 }
3908
3909 void addDisplayInfo(int displayId, const ui::Transform& transform) {
3910 gui::DisplayInfo info;
3911 info.displayId = displayId;
3912 info.transform = transform;
3913 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05003914 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003915 }
3916
3917 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
3918 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05003919 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003920 }
3921
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003922 void removeAllWindowsAndDisplays() {
3923 mDisplayInfos.clear();
3924 mWindowInfos.clear();
3925 }
3926
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003927 // Set up a test scenario where the display has a scaled projection and there are two windows
3928 // on the display.
3929 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
3930 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
3931 // respectively.
3932 ui::Transform displayTransform;
3933 displayTransform.set(2, 0, 0, 4);
3934 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3935
3936 std::shared_ptr<FakeApplicationHandle> application =
3937 std::make_shared<FakeApplicationHandle>();
3938
3939 // Add two windows to the display. Their frames are represented in the display space.
3940 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003941 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3942 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003943 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
3944 addWindow(firstWindow);
3945
3946 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003947 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3948 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003949 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
3950 addWindow(secondWindow);
3951 return {std::move(firstWindow), std::move(secondWindow)};
3952 }
3953
3954private:
3955 std::vector<gui::DisplayInfo> mDisplayInfos;
3956 std::vector<gui::WindowInfo> mWindowInfos;
3957};
3958
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003959TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003960 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3961 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003962 // selected so that if the hit test was performed with the point and the bounds being in
3963 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003964 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3965 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3966 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003967
3968 firstWindow->consumeMotionDown();
3969 secondWindow->assertNoEvents();
3970}
3971
3972// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
3973// the event should be treated as being in the logical display space.
3974TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
3975 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3976 // Send down to the first window. The point is represented in the logical display space. The
3977 // point is selected so that if the hit test was done in logical display space, then it would
3978 // end up in the incorrect window.
3979 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3980 PointF{75 * 2, 55 * 4});
3981
3982 firstWindow->consumeMotionDown();
3983 secondWindow->assertNoEvents();
3984}
3985
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003986// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
3987// event should be treated as being in the logical display space.
3988TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
3989 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3990
3991 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
3992 ui::Transform injectedEventTransform;
3993 injectedEventTransform.set(matrix);
3994 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
3995 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
3996
3997 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3998 .displayId(ADISPLAY_ID_DEFAULT)
3999 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004000 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004001 .x(untransformedPoint.x)
4002 .y(untransformedPoint.y))
4003 .build();
4004 event.transform(matrix);
4005
4006 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4007 InputEventInjectionSync::WAIT_FOR_RESULT);
4008
4009 firstWindow->consumeMotionDown();
4010 secondWindow->assertNoEvents();
4011}
4012
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004013TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4014 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4015
4016 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004017 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4018 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4019 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004020
4021 firstWindow->assertNoEvents();
4022 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004023 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004024 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4025
4026 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4027 EXPECT_EQ(300, event->getRawX(0));
4028 EXPECT_EQ(880, event->getRawY(0));
4029
4030 // Ensure that the x and y values are in the window's coordinate space.
4031 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4032 // the logical display space. This will be the origin of the window space.
4033 EXPECT_EQ(100, event->getX(0));
4034 EXPECT_EQ(80, event->getY(0));
4035}
4036
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004037/** Ensure consistent behavior of InputDispatcher in all orientations. */
4038class InputDispatcherDisplayOrientationFixture
4039 : public InputDispatcherDisplayProjectionTest,
4040 public ::testing::WithParamInterface<ui::Rotation> {};
4041
4042// This test verifies the touchable region of a window for all rotations of the display by tapping
4043// in different locations on the display, specifically points close to the four corners of a
4044// window.
4045TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4046 constexpr static int32_t displayWidth = 400;
4047 constexpr static int32_t displayHeight = 800;
4048
4049 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4050
4051 const auto rotation = GetParam();
4052
4053 // Set up the display with the specified rotation.
4054 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4055 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4056 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4057 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4058 logicalDisplayWidth, logicalDisplayHeight);
4059 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4060
4061 // Create a window with its bounds determined in the logical display.
4062 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4063 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4064 sp<FakeWindowHandle> window =
4065 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4066 window->setFrame(frameInDisplay, displayTransform);
4067 addWindow(window);
4068
4069 // The following points in logical display space should be inside the window.
4070 static const std::array<vec2, 4> insidePoints{
4071 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4072 for (const auto pointInsideWindow : insidePoints) {
4073 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4074 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004075 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4076 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4077 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004078 window->consumeMotionDown();
4079
Prabir Pradhan678438e2023-04-13 19:32:51 +00004080 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4081 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4082 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004083 window->consumeMotionUp();
4084 }
4085
4086 // The following points in logical display space should be outside the window.
4087 static const std::array<vec2, 5> outsidePoints{
4088 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4089 for (const auto pointOutsideWindow : outsidePoints) {
4090 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4091 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004092 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4093 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4094 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004095
Prabir Pradhan678438e2023-04-13 19:32:51 +00004096 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4097 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4098 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004099 }
4100 window->assertNoEvents();
4101}
4102
4103// Run the precision tests for all rotations.
4104INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4105 InputDispatcherDisplayOrientationFixture,
4106 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4107 ui::ROTATION_270),
4108 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4109 return ftl::enum_string(testParamInfo.param);
4110 });
4111
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004112using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4113 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004114
4115class TransferTouchFixture : public InputDispatcherTest,
4116 public ::testing::WithParamInterface<TransferFunction> {};
4117
4118TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004119 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004120
4121 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004122 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004123 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4124 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004125 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004126 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004127 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4128 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004129 sp<FakeWindowHandle> wallpaper =
4130 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4131 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004132 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004133 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004134
4135 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004136 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4137 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004138
Svet Ganov5d3bc372020-01-26 23:11:07 -08004139 // Only the first window should get the down event
4140 firstWindow->consumeMotionDown();
4141 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004142 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004143
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004144 // Transfer touch to the second window
4145 TransferFunction f = GetParam();
4146 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4147 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004148 // The first window gets cancel and the second gets down
4149 firstWindow->consumeMotionCancel();
4150 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004151 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004152
4153 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004154 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4155 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004156 // The first window gets no events and the second gets up
4157 firstWindow->assertNoEvents();
4158 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004159 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004160}
4161
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004162/**
4163 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4164 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4165 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4166 * natural to the user.
4167 * In this test, we are sending a pointer to both spy window and first window. We then try to
4168 * transfer touch to the second window. The dispatcher should identify the first window as the
4169 * one that should lose the gesture, and therefore the action should be to move the gesture from
4170 * the first window to the second.
4171 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4172 * the other API, as well.
4173 */
4174TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4175 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4176
4177 // Create a couple of windows + a spy window
4178 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004179 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004180 spyWindow->setTrustedOverlay(true);
4181 spyWindow->setSpy(true);
4182 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004183 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004184 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004185 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004186
4187 // Add the windows to the dispatcher
4188 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4189
4190 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004191 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4192 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004193 // Only the first window and spy should get the down event
4194 spyWindow->consumeMotionDown();
4195 firstWindow->consumeMotionDown();
4196
4197 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4198 // if f === 'transferTouch'.
4199 TransferFunction f = GetParam();
4200 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4201 ASSERT_TRUE(success);
4202 // The first window gets cancel and the second gets down
4203 firstWindow->consumeMotionCancel();
4204 secondWindow->consumeMotionDown();
4205
4206 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004207 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4208 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004209 // The first window gets no events and the second+spy get up
4210 firstWindow->assertNoEvents();
4211 spyWindow->consumeMotionUp();
4212 secondWindow->consumeMotionUp();
4213}
4214
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004215TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004216 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004217
4218 PointF touchPoint = {10, 10};
4219
4220 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004221 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004222 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4223 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004224 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004225 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004226 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4227 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004228 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004229
4230 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004231 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004232
4233 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004234 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4235 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4236 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004237 // Only the first window should get the down event
4238 firstWindow->consumeMotionDown();
4239 secondWindow->assertNoEvents();
4240
4241 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004242 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4243 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004244 // Only the first window should get the pointer down event
4245 firstWindow->consumeMotionPointerDown(1);
4246 secondWindow->assertNoEvents();
4247
4248 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004249 TransferFunction f = GetParam();
4250 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4251 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004252 // The first window gets cancel and the second gets down and pointer down
4253 firstWindow->consumeMotionCancel();
4254 secondWindow->consumeMotionDown();
4255 secondWindow->consumeMotionPointerDown(1);
4256
4257 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004258 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4259 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004260 // The first window gets nothing and the second gets pointer up
4261 firstWindow->assertNoEvents();
4262 secondWindow->consumeMotionPointerUp(1);
4263
4264 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004265 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4266 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004267 // The first window gets nothing and the second gets up
4268 firstWindow->assertNoEvents();
4269 secondWindow->consumeMotionUp();
4270}
4271
Arthur Hungc539dbb2022-12-08 07:45:36 +00004272TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4273 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4274
4275 // Create a couple of windows
4276 sp<FakeWindowHandle> firstWindow =
4277 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4278 ADISPLAY_ID_DEFAULT);
4279 firstWindow->setDupTouchToWallpaper(true);
4280 sp<FakeWindowHandle> secondWindow =
4281 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4282 ADISPLAY_ID_DEFAULT);
4283 secondWindow->setDupTouchToWallpaper(true);
4284
4285 sp<FakeWindowHandle> wallpaper1 =
4286 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4287 wallpaper1->setIsWallpaper(true);
4288
4289 sp<FakeWindowHandle> wallpaper2 =
4290 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4291 wallpaper2->setIsWallpaper(true);
4292 // Add the windows to the dispatcher
4293 mDispatcher->setInputWindows(
4294 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4295
4296 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004297 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4298 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004299
4300 // Only the first window should get the down event
4301 firstWindow->consumeMotionDown();
4302 secondWindow->assertNoEvents();
4303 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4304 wallpaper2->assertNoEvents();
4305
4306 // Transfer touch focus to the second window
4307 TransferFunction f = GetParam();
4308 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4309 ASSERT_TRUE(success);
4310
4311 // The first window gets cancel and the second gets down
4312 firstWindow->consumeMotionCancel();
4313 secondWindow->consumeMotionDown();
4314 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4315 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4316
4317 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004318 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4319 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004320 // The first window gets no events and the second gets up
4321 firstWindow->assertNoEvents();
4322 secondWindow->consumeMotionUp();
4323 wallpaper1->assertNoEvents();
4324 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4325}
4326
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004327// For the cases of single pointer touch and two pointers non-split touch, the api's
4328// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4329// for the case where there are multiple pointers split across several windows.
4330INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4331 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004332 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4333 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004334 return dispatcher->transferTouch(destChannelToken,
4335 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004336 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004337 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4338 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004339 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004340 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004341 }));
4342
Svet Ganov5d3bc372020-01-26 23:11:07 -08004343TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004344 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004345
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004346 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004347 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4348 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004349 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004350
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004351 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004352 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4353 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004354 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004355
4356 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004357 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004358
4359 PointF pointInFirst = {300, 200};
4360 PointF pointInSecond = {300, 600};
4361
4362 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004363 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4364 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4365 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004366 // Only the first window should get the down event
4367 firstWindow->consumeMotionDown();
4368 secondWindow->assertNoEvents();
4369
4370 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004371 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4372 ADISPLAY_ID_DEFAULT,
4373 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004374 // The first window gets a move and the second a down
4375 firstWindow->consumeMotionMove();
4376 secondWindow->consumeMotionDown();
4377
4378 // Transfer touch focus to the second window
4379 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4380 // The first window gets cancel and the new gets pointer down (it already saw down)
4381 firstWindow->consumeMotionCancel();
4382 secondWindow->consumeMotionPointerDown(1);
4383
4384 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004385 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4386 ADISPLAY_ID_DEFAULT,
4387 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004388 // The first window gets nothing and the second gets pointer up
4389 firstWindow->assertNoEvents();
4390 secondWindow->consumeMotionPointerUp(1);
4391
4392 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004393 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4394 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004395 // The first window gets nothing and the second gets up
4396 firstWindow->assertNoEvents();
4397 secondWindow->consumeMotionUp();
4398}
4399
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004400// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4401// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4402// touch is not supported, so the touch should continue on those windows and the transferred-to
4403// window should get nothing.
4404TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4405 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4406
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004407 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004408 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4409 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004410 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004411
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004412 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004413 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4414 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004415 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004416
4417 // Add the windows to the dispatcher
4418 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4419
4420 PointF pointInFirst = {300, 200};
4421 PointF pointInSecond = {300, 600};
4422
4423 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004424 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4425 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4426 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004427 // Only the first window should get the down event
4428 firstWindow->consumeMotionDown();
4429 secondWindow->assertNoEvents();
4430
4431 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004432 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4433 ADISPLAY_ID_DEFAULT,
4434 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004435 // The first window gets a move and the second a down
4436 firstWindow->consumeMotionMove();
4437 secondWindow->consumeMotionDown();
4438
4439 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004440 const bool transferred =
4441 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004442 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4443 ASSERT_FALSE(transferred);
4444 firstWindow->assertNoEvents();
4445 secondWindow->assertNoEvents();
4446
4447 // The rest of the dispatch should proceed as normal
4448 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004449 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4450 ADISPLAY_ID_DEFAULT,
4451 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004452 // The first window gets MOVE and the second gets pointer up
4453 firstWindow->consumeMotionMove();
4454 secondWindow->consumeMotionUp();
4455
4456 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004457 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4458 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004459 // The first window gets nothing and the second gets up
4460 firstWindow->consumeMotionUp();
4461 secondWindow->assertNoEvents();
4462}
4463
Arthur Hungabbb9d82021-09-01 14:52:30 +00004464// This case will create two windows and one mirrored window on the default display and mirror
4465// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4466// the windows info of second display before default display.
4467TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4468 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4469 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004470 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004471 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004472 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004473 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004474 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004475
4476 sp<FakeWindowHandle> mirrorWindowInPrimary =
4477 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4478 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004479
4480 sp<FakeWindowHandle> firstWindowInSecondary =
4481 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4482 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004483
4484 sp<FakeWindowHandle> secondWindowInSecondary =
4485 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4486 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004487
4488 // Update window info, let it find window handle of second display first.
4489 mDispatcher->setInputWindows(
4490 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4491 {ADISPLAY_ID_DEFAULT,
4492 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4493
4494 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4495 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4496 {50, 50}))
4497 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4498
4499 // Window should receive motion event.
4500 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4501
4502 // Transfer touch focus
4503 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4504 secondWindowInPrimary->getToken()));
4505 // The first window gets cancel.
4506 firstWindowInPrimary->consumeMotionCancel();
4507 secondWindowInPrimary->consumeMotionDown();
4508
4509 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4510 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4511 ADISPLAY_ID_DEFAULT, {150, 50}))
4512 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4513 firstWindowInPrimary->assertNoEvents();
4514 secondWindowInPrimary->consumeMotionMove();
4515
4516 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4517 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4518 {150, 50}))
4519 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4520 firstWindowInPrimary->assertNoEvents();
4521 secondWindowInPrimary->consumeMotionUp();
4522}
4523
4524// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4525// 'transferTouch' api.
4526TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4527 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4528 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004529 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004530 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004531 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004532 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004533 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004534
4535 sp<FakeWindowHandle> mirrorWindowInPrimary =
4536 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4537 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004538
4539 sp<FakeWindowHandle> firstWindowInSecondary =
4540 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4541 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004542
4543 sp<FakeWindowHandle> secondWindowInSecondary =
4544 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4545 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004546
4547 // Update window info, let it find window handle of second display first.
4548 mDispatcher->setInputWindows(
4549 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4550 {ADISPLAY_ID_DEFAULT,
4551 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4552
4553 // Touch on second display.
4554 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4555 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4556 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4557
4558 // Window should receive motion event.
4559 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4560
4561 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004562 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004563
4564 // The first window gets cancel.
4565 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4566 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4567
4568 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4569 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4570 SECOND_DISPLAY_ID, {150, 50}))
4571 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4572 firstWindowInPrimary->assertNoEvents();
4573 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4574
4575 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4576 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4577 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4578 firstWindowInPrimary->assertNoEvents();
4579 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4580}
4581
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004582TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004583 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004584 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4585 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004586
Vishnu Nair47074b82020-08-14 11:54:47 -07004587 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004588 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004589 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004590
4591 window->consumeFocusEvent(true);
4592
Prabir Pradhan678438e2023-04-13 19:32:51 +00004593 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004594
4595 // Window should receive key down event.
4596 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004597
4598 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004599 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00004600 mFakePolicy->assertUserActivityPoked();
4601}
4602
4603TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4604 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4605 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4606 "Fake Window", ADISPLAY_ID_DEFAULT);
4607
4608 window->setDisableUserActivity(true);
4609 window->setFocusable(true);
4610 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4611 setFocusedWindow(window);
4612
4613 window->consumeFocusEvent(true);
4614
4615 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4616
4617 // Window should receive key down event.
4618 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4619
4620 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004621 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00004622 mFakePolicy->assertUserActivityNotPoked();
4623}
4624
4625TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4626 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4627 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4628 "Fake Window", ADISPLAY_ID_DEFAULT);
4629
4630 window->setFocusable(true);
4631 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4632 setFocusedWindow(window);
4633
4634 window->consumeFocusEvent(true);
4635
4636 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4637 mDispatcher->waitForIdle();
4638
4639 // System key is not passed down
4640 window->assertNoEvents();
4641
4642 // Should have poked user activity
4643 mFakePolicy->assertUserActivityPoked();
4644}
4645
4646TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4647 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4648 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4649 "Fake Window", ADISPLAY_ID_DEFAULT);
4650
4651 window->setFocusable(true);
4652 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4653 setFocusedWindow(window);
4654
4655 window->consumeFocusEvent(true);
4656
4657 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4658 mDispatcher->waitForIdle();
4659
4660 // System key is not passed down
4661 window->assertNoEvents();
4662
4663 // Should have poked user activity
4664 mFakePolicy->assertUserActivityPoked();
4665}
4666
4667TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4668 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4669 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4670 "Fake Window", ADISPLAY_ID_DEFAULT);
4671
4672 window->setDisableUserActivity(true);
4673 window->setFocusable(true);
4674 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4675 setFocusedWindow(window);
4676
4677 window->consumeFocusEvent(true);
4678
4679 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4680 mDispatcher->waitForIdle();
4681
4682 // System key is not passed down
4683 window->assertNoEvents();
4684
4685 // Should have poked user activity
4686 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004687}
4688
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004689TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
4690 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4691 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4692 "Fake Window", ADISPLAY_ID_DEFAULT);
4693
4694 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4695
4696 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4697 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4698 ADISPLAY_ID_DEFAULT, {100, 100}))
4699 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4700
4701 window->consumeMotionEvent(
4702 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
4703
4704 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004705 mDispatcher->waitForIdle();
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004706 mFakePolicy->assertUserActivityPoked();
4707}
4708
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004709TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004710 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004711 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4712 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004713
Arthur Hung72d8dc32020-03-28 00:48:39 +00004714 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004715
Prabir Pradhan678438e2023-04-13 19:32:51 +00004716 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004717 mDispatcher->waitForIdle();
4718
4719 window->assertNoEvents();
4720}
4721
4722// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4723TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004724 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004725 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4726 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004727
Arthur Hung72d8dc32020-03-28 00:48:39 +00004728 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004729
4730 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004731 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004732 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004733 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4734 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004735
4736 // Window should receive only the motion event
4737 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4738 window->assertNoEvents(); // Key event or focus event will not be received
4739}
4740
arthurhungea3f4fc2020-12-21 23:18:53 +08004741TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4742 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4743
arthurhungea3f4fc2020-12-21 23:18:53 +08004744 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004745 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4746 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004747 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004748
arthurhungea3f4fc2020-12-21 23:18:53 +08004749 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004750 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4751 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004752 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004753
4754 // Add the windows to the dispatcher
4755 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4756
4757 PointF pointInFirst = {300, 200};
4758 PointF pointInSecond = {300, 600};
4759
4760 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004761 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4762 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4763 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004764 // Only the first window should get the down event
4765 firstWindow->consumeMotionDown();
4766 secondWindow->assertNoEvents();
4767
4768 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004769 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4770 ADISPLAY_ID_DEFAULT,
4771 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004772 // The first window gets a move and the second a down
4773 firstWindow->consumeMotionMove();
4774 secondWindow->consumeMotionDown();
4775
4776 // Send pointer cancel to the second window
4777 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004778 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004779 {pointInFirst, pointInSecond});
4780 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004781 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004782 // The first window gets move and the second gets cancel.
4783 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4784 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4785
4786 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004787 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4788 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08004789 // The first window gets up and the second gets nothing.
4790 firstWindow->consumeMotionUp();
4791 secondWindow->assertNoEvents();
4792}
4793
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004794TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
4795 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4796
4797 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004798 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004799 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4800 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
4801 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
4802 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
4803
Harry Cutts33476232023-01-30 19:57:29 +00004804 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004805 window->assertNoEvents();
4806 mDispatcher->waitForIdle();
4807}
4808
chaviwd1c23182019-12-20 18:44:56 -08004809class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00004810public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004811 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004812 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07004813 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004814 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07004815 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00004816 }
4817
chaviwd1c23182019-12-20 18:44:56 -08004818 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
4819
4820 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004821 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
4822 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08004823 }
4824
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004825 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
4826
4827 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
4828
chaviwd1c23182019-12-20 18:44:56 -08004829 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004830 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08004831 expectedDisplayId, expectedFlags);
4832 }
4833
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004834 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004835 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004836 expectedDisplayId, expectedFlags);
4837 }
4838
chaviwd1c23182019-12-20 18:44:56 -08004839 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004840 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08004841 expectedDisplayId, expectedFlags);
4842 }
4843
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004844 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004845 mInputReceiver->consumeMotionEvent(
4846 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4847 WithDisplayId(expectedDisplayId),
4848 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004849 }
4850
Arthur Hungfbfa5722021-11-16 02:45:54 +00004851 void consumeMotionPointerDown(int32_t pointerIdx) {
4852 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4853 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004854 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004855 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004856 }
4857
Evan Rosky84f07f02021-04-16 10:42:42 -07004858 MotionEvent* consumeMotion() {
4859 InputEvent* event = mInputReceiver->consume();
4860 if (!event) {
4861 ADD_FAILURE() << "No event was produced";
4862 return nullptr;
4863 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004864 if (event->getType() != InputEventType::MOTION) {
4865 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07004866 return nullptr;
4867 }
4868 return static_cast<MotionEvent*>(event);
4869 }
4870
chaviwd1c23182019-12-20 18:44:56 -08004871 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4872
4873private:
4874 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004875};
4876
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004877using InputDispatcherMonitorTest = InputDispatcherTest;
4878
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004879/**
4880 * Two entities that receive touch: A window, and a global monitor.
4881 * The touch goes to the window, and then the window disappears.
4882 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4883 * for the monitor, as well.
4884 * 1. foregroundWindow
4885 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4886 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004887TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004888 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4889 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004890 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004891
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004892 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004893
4894 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4895 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4896 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4897 {100, 200}))
4898 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4899
4900 // Both the foreground window and the global monitor should receive the touch down
4901 window->consumeMotionDown();
4902 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4903
4904 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4905 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4906 ADISPLAY_ID_DEFAULT, {110, 200}))
4907 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4908
4909 window->consumeMotionMove();
4910 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4911
4912 // Now the foreground window goes away
4913 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4914 window->consumeMotionCancel();
4915 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
4916
4917 // If more events come in, there will be no more foreground window to send them to. This will
4918 // cause a cancel for the monitor, as well.
4919 ASSERT_EQ(InputEventInjectionResult::FAILED,
4920 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4921 ADISPLAY_ID_DEFAULT, {120, 200}))
4922 << "Injection should fail because the window was removed";
4923 window->assertNoEvents();
4924 // Global monitor now gets the cancel
4925 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4926}
4927
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004928TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07004929 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004930 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4931 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004932 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004933
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004934 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004935
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004936 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004937 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004938 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00004939 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004940 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004941}
4942
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004943TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
4944 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004945
Chris Yea209fde2020-07-22 13:54:51 -07004946 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004947 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4948 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004949 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004950
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004951 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004952 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004953 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08004954 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004955 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004956
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004957 // Pilfer pointers from the monitor.
4958 // This should not do anything and the window should continue to receive events.
4959 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00004960
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004961 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004962 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4963 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004964 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004965
4966 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4967 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004968}
4969
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004970TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07004971 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004972 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4973 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004974 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4975 window->setWindowOffset(20, 40);
4976 window->setWindowTransform(0, 1, -1, 0);
4977
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004978 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004979
4980 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4981 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4982 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4983 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4984 MotionEvent* event = monitor.consumeMotion();
4985 // Even though window has transform, gesture monitor must not.
4986 ASSERT_EQ(ui::Transform(), event->getTransform());
4987}
4988
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004989TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00004990 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004991 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00004992
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004993 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00004994 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004995 << "Injection should fail if there is a monitor, but no touchable window";
4996 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00004997}
4998
chaviw81e2bb92019-12-18 15:03:51 -08004999TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005000 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005001 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5002 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08005003
Arthur Hung72d8dc32020-03-28 00:48:39 +00005004 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08005005
5006 NotifyMotionArgs motionArgs =
5007 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5008 ADISPLAY_ID_DEFAULT);
5009
Prabir Pradhan678438e2023-04-13 19:32:51 +00005010 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005011 // Window should receive motion down event.
5012 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5013
5014 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005015 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005016 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5017 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5018 motionArgs.pointerCoords[0].getX() - 10);
5019
Prabir Pradhan678438e2023-04-13 19:32:51 +00005020 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005021 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005022 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005023}
5024
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005025/**
5026 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5027 * the device default right away. In the test scenario, we check both the default value,
5028 * and the action of enabling / disabling.
5029 */
5030TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005031 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005032 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5033 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005034 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005035
5036 // Set focused application.
5037 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005038 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005039
5040 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005041 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005042 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005043 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005044
5045 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005046 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005047 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005048 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005049
5050 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005051 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005052 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005053 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005054 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005055 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005056 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005057 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005058
5059 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005060 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005061 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005062 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005063
5064 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005065 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005066 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005067 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005068 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005069 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005070 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005071 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005072
5073 window->assertNoEvents();
5074}
5075
Gang Wange9087892020-01-07 12:17:14 -05005076TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005077 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005078 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5079 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005080
5081 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005082 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005083
Arthur Hung72d8dc32020-03-28 00:48:39 +00005084 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005085 setFocusedWindow(window);
5086
Harry Cutts33476232023-01-30 19:57:29 +00005087 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005088
Prabir Pradhan678438e2023-04-13 19:32:51 +00005089 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5090 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005091
5092 InputEvent* event = window->consume();
5093 ASSERT_NE(event, nullptr);
5094
5095 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5096 ASSERT_NE(verified, nullptr);
5097 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5098
5099 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5100 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5101 ASSERT_EQ(keyArgs.source, verified->source);
5102 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5103
5104 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5105
5106 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005107 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005108 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005109 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5110 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5111 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5112 ASSERT_EQ(0, verifiedKey.repeatCount);
5113}
5114
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005115TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005116 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005117 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5118 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005119
5120 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5121
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005122 ui::Transform transform;
5123 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5124
5125 gui::DisplayInfo displayInfo;
5126 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5127 displayInfo.transform = transform;
5128
Patrick Williamsd828f302023-04-28 17:52:08 -05005129 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005130
Prabir Pradhan678438e2023-04-13 19:32:51 +00005131 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005132 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5133 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005134 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005135
5136 InputEvent* event = window->consume();
5137 ASSERT_NE(event, nullptr);
5138
5139 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5140 ASSERT_NE(verified, nullptr);
5141 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5142
5143 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5144 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5145 EXPECT_EQ(motionArgs.source, verified->source);
5146 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5147
5148 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5149
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005150 const vec2 rawXY =
5151 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5152 motionArgs.pointerCoords[0].getXYValue());
5153 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5154 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005155 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005156 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005157 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005158 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5159 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5160}
5161
chaviw09c8d2d2020-08-24 15:48:26 -07005162/**
5163 * Ensure that separate calls to sign the same data are generating the same key.
5164 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5165 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5166 * tests.
5167 */
5168TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5169 KeyEvent event = getTestKeyEvent();
5170 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5171
5172 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5173 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5174 ASSERT_EQ(hmac1, hmac2);
5175}
5176
5177/**
5178 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5179 */
5180TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5181 KeyEvent event = getTestKeyEvent();
5182 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5183 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5184
5185 verifiedEvent.deviceId += 1;
5186 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5187
5188 verifiedEvent.source += 1;
5189 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5190
5191 verifiedEvent.eventTimeNanos += 1;
5192 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5193
5194 verifiedEvent.displayId += 1;
5195 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5196
5197 verifiedEvent.action += 1;
5198 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5199
5200 verifiedEvent.downTimeNanos += 1;
5201 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5202
5203 verifiedEvent.flags += 1;
5204 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5205
5206 verifiedEvent.keyCode += 1;
5207 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5208
5209 verifiedEvent.scanCode += 1;
5210 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5211
5212 verifiedEvent.metaState += 1;
5213 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5214
5215 verifiedEvent.repeatCount += 1;
5216 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5217}
5218
Vishnu Nair958da932020-08-21 17:12:37 -07005219TEST_F(InputDispatcherTest, SetFocusedWindow) {
5220 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5221 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005222 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005223 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005224 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005225 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5226
5227 // Top window is also focusable but is not granted focus.
5228 windowTop->setFocusable(true);
5229 windowSecond->setFocusable(true);
5230 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5231 setFocusedWindow(windowSecond);
5232
5233 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005234 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5235 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005236
5237 // Focused window should receive event.
5238 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5239 windowTop->assertNoEvents();
5240}
5241
5242TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5243 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5244 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005245 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005246 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5247
5248 window->setFocusable(true);
5249 // Release channel for window is no longer valid.
5250 window->releaseChannel();
5251 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5252 setFocusedWindow(window);
5253
5254 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005255 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5256 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005257
5258 // window channel is invalid, so it should not receive any input event.
5259 window->assertNoEvents();
5260}
5261
5262TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5263 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5264 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005265 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005266 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005267 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5268
Vishnu Nair958da932020-08-21 17:12:37 -07005269 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5270 setFocusedWindow(window);
5271
5272 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005273 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5274 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005275
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005276 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005277 window->assertNoEvents();
5278}
5279
5280TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5281 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5282 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005283 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005284 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005285 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005286 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5287
5288 windowTop->setFocusable(true);
5289 windowSecond->setFocusable(true);
5290 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5291 setFocusedWindow(windowTop);
5292 windowTop->consumeFocusEvent(true);
5293
Chavi Weingarten847e8512023-03-29 00:26:09 +00005294 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5295 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005296 windowSecond->consumeFocusEvent(true);
5297 windowTop->consumeFocusEvent(false);
5298
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005299 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5300 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005301
5302 // Focused window should receive event.
5303 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5304}
5305
Chavi Weingarten847e8512023-03-29 00:26:09 +00005306TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005307 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5308 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005309 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005310 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005311 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005312 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5313
5314 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005315 windowSecond->setFocusable(false);
5316 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005317 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005318 setFocusedWindow(windowTop);
5319 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005320
Chavi Weingarten847e8512023-03-29 00:26:09 +00005321 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5322 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005323
5324 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005325 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005326 windowSecond->assertNoEvents();
5327}
5328
5329TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5330 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5331 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005332 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005333 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005334 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5335 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005336 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5337
5338 window->setFocusable(true);
5339 previousFocusedWindow->setFocusable(true);
5340 window->setVisible(false);
5341 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5342 setFocusedWindow(previousFocusedWindow);
5343 previousFocusedWindow->consumeFocusEvent(true);
5344
5345 // Requesting focus on invisible window takes focus from currently focused window.
5346 setFocusedWindow(window);
5347 previousFocusedWindow->consumeFocusEvent(false);
5348
5349 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005351 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5352 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005353
5354 // Window does not get focus event or key down.
5355 window->assertNoEvents();
5356
5357 // Window becomes visible.
5358 window->setVisible(true);
5359 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5360
5361 // Window receives focus event.
5362 window->consumeFocusEvent(true);
5363 // Focused window receives key down.
5364 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5365}
5366
Vishnu Nair599f1412021-06-21 10:39:58 -07005367TEST_F(InputDispatcherTest, DisplayRemoved) {
5368 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5369 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005370 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005371 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5372
5373 // window is granted focus.
5374 window->setFocusable(true);
5375 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5376 setFocusedWindow(window);
5377 window->consumeFocusEvent(true);
5378
5379 // When a display is removed window loses focus.
5380 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5381 window->consumeFocusEvent(false);
5382}
5383
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005384/**
5385 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5386 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5387 * of the 'slipperyEnterWindow'.
5388 *
5389 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5390 * a way so that the touched location is no longer covered by the top window.
5391 *
5392 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5393 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5394 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5395 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5396 * with ACTION_DOWN).
5397 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5398 * window moved itself away from the touched location and had Flag::SLIPPERY.
5399 *
5400 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5401 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5402 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5403 *
5404 * In this test, we ensure that the event received by the bottom window has
5405 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5406 */
5407TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005408 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5409 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005410
5411 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5412 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5413
5414 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005415 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005416 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005417 // Make sure this one overlaps the bottom window
5418 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5419 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5420 // one. Windows with the same owner are not considered to be occluding each other.
5421 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5422
5423 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005424 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005425 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5426
5427 mDispatcher->setInputWindows(
5428 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5429
5430 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005431 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5432 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5433 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005434 slipperyExitWindow->consumeMotionDown();
5435 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5436 mDispatcher->setInputWindows(
5437 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5438
Prabir Pradhan678438e2023-04-13 19:32:51 +00005439 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5440 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5441 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005442
5443 slipperyExitWindow->consumeMotionCancel();
5444
5445 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5446 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5447}
5448
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005449/**
5450 * Two windows, one on the left and another on the right. The left window is slippery. The right
5451 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5452 * touch moves from the left window into the right window, the gesture should continue to go to the
5453 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5454 * reproduces a crash.
5455 */
5456TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5457 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5458
5459 sp<FakeWindowHandle> leftSlipperyWindow =
5460 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5461 leftSlipperyWindow->setSlippery(true);
5462 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5463
5464 sp<FakeWindowHandle> rightDropTouchesWindow =
5465 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5466 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5467 rightDropTouchesWindow->setDropInput(true);
5468
5469 mDispatcher->setInputWindows(
5470 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5471
5472 // Start touch in the left window
5473 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5474 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5475 .build());
5476 leftSlipperyWindow->consumeMotionDown();
5477
5478 // And move it into the right window
5479 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5480 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5481 .build());
5482
5483 // Since the right window isn't eligible to receive input, touch does not slip.
5484 // The left window continues to receive the gesture.
5485 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5486 rightDropTouchesWindow->assertNoEvents();
5487}
5488
Garfield Tan1c7bc862020-01-28 13:24:04 -08005489class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5490protected:
5491 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5492 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5493
Chris Yea209fde2020-07-22 13:54:51 -07005494 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005495 sp<FakeWindowHandle> mWindow;
5496
5497 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005498 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005499 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005500 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005501 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005502 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5503 ASSERT_EQ(OK, mDispatcher->start());
5504
5505 setUpWindow();
5506 }
5507
5508 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005509 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005510 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005511
Vishnu Nair47074b82020-08-14 11:54:47 -07005512 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005513 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005514 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005515 mWindow->consumeFocusEvent(true);
5516 }
5517
Chris Ye2ad95392020-09-01 13:44:44 -07005518 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005519 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005520 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005521 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005522 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005523
5524 // Window should receive key down event.
5525 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5526 }
5527
5528 void expectKeyRepeatOnce(int32_t repeatCount) {
5529 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5530 InputEvent* repeatEvent = mWindow->consume();
5531 ASSERT_NE(nullptr, repeatEvent);
5532
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005533 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005534
5535 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5536 uint32_t eventAction = repeatKeyEvent->getAction();
5537 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5538 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5539 }
5540
Chris Ye2ad95392020-09-01 13:44:44 -07005541 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005542 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005543 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005544 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005545 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005546
5547 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005548 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005549 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005550 }
5551};
5552
5553TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005554 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005555 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5556 expectKeyRepeatOnce(repeatCount);
5557 }
5558}
5559
5560TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005561 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005562 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5563 expectKeyRepeatOnce(repeatCount);
5564 }
Harry Cutts33476232023-01-30 19:57:29 +00005565 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005566 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005567 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5568 expectKeyRepeatOnce(repeatCount);
5569 }
5570}
5571
5572TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005573 sendAndConsumeKeyDown(/*deviceId=*/1);
5574 expectKeyRepeatOnce(/*repeatCount=*/1);
5575 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005576 mWindow->assertNoEvents();
5577}
5578
5579TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005580 sendAndConsumeKeyDown(/*deviceId=*/1);
5581 expectKeyRepeatOnce(/*repeatCount=*/1);
5582 sendAndConsumeKeyDown(/*deviceId=*/2);
5583 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005584 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005585 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005586 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005587 expectKeyRepeatOnce(/*repeatCount=*/2);
5588 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005589 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005590 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005591 mWindow->assertNoEvents();
5592}
5593
5594TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005595 sendAndConsumeKeyDown(/*deviceId=*/1);
5596 expectKeyRepeatOnce(/*repeatCount=*/1);
5597 sendAndConsumeKeyDown(/*deviceId=*/2);
5598 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005599 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005600 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005601 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005602 mWindow->assertNoEvents();
5603}
5604
liushenxiang42232912021-05-21 20:24:09 +08005605TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5606 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005607 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005608 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005609 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5610 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5611 mWindow->assertNoEvents();
5612}
5613
Garfield Tan1c7bc862020-01-28 13:24:04 -08005614TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005615 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005616 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005617 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5618 InputEvent* repeatEvent = mWindow->consume();
5619 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5620 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5621 IdGenerator::getSource(repeatEvent->getId()));
5622 }
5623}
5624
5625TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005626 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005627 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005628
5629 std::unordered_set<int32_t> idSet;
5630 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5631 InputEvent* repeatEvent = mWindow->consume();
5632 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5633 int32_t id = repeatEvent->getId();
5634 EXPECT_EQ(idSet.end(), idSet.find(id));
5635 idSet.insert(id);
5636 }
5637}
5638
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005639/* Test InputDispatcher for MultiDisplay */
5640class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5641public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005642 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005643 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005644
Chris Yea209fde2020-07-22 13:54:51 -07005645 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005646 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005647 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005648
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005649 // Set focus window for primary display, but focused display would be second one.
5650 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005651 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005652 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005653 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005654 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005655
Chris Yea209fde2020-07-22 13:54:51 -07005656 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005657 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005658 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005659 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005660 // Set focus display to second one.
5661 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5662 // Set focus window for second display.
5663 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005664 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005665 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005666 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005667 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005668 }
5669
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005670 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005671 InputDispatcherTest::TearDown();
5672
Chris Yea209fde2020-07-22 13:54:51 -07005673 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005674 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005675 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005676 windowInSecondary.clear();
5677 }
5678
5679protected:
Chris Yea209fde2020-07-22 13:54:51 -07005680 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005681 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005682 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005683 sp<FakeWindowHandle> windowInSecondary;
5684};
5685
5686TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5687 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5689 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5690 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005691 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005692 windowInSecondary->assertNoEvents();
5693
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005694 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005695 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5696 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5697 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005698 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005699 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005700}
5701
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005702TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005703 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005704 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5705 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005706 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005707 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005708 windowInSecondary->assertNoEvents();
5709
5710 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005711 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005712 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005713 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005714 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005715
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005716 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005717 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005718
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005719 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005720 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005721 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005722
5723 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005724 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005725 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005726 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005727 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005728 windowInSecondary->assertNoEvents();
5729}
5730
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005731// Test per-display input monitors for motion event.
5732TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005733 FakeMonitorReceiver monitorInPrimary =
5734 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5735 FakeMonitorReceiver monitorInSecondary =
5736 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005737
5738 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005739 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5740 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5741 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005742 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005743 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005744 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005745 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005746
5747 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005748 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5749 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5750 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005751 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005752 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005753 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005754 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005755
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005756 // Lift up the touch from the second display
5757 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5758 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5759 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5760 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5761 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5762
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005763 // Test inject a non-pointer motion event.
5764 // If specific a display, it will dispatch to the focused window of particular display,
5765 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005766 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5767 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5768 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005769 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005770 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005771 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005772 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005773}
5774
5775// Test per-display input monitors for key event.
5776TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005777 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005778 FakeMonitorReceiver monitorInPrimary =
5779 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5780 FakeMonitorReceiver monitorInSecondary =
5781 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005782
5783 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005784 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5785 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005786 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005787 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005788 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005789 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005790}
5791
Vishnu Nair958da932020-08-21 17:12:37 -07005792TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
5793 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005794 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005795 secondWindowInPrimary->setFocusable(true);
5796 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
5797 setFocusedWindow(secondWindowInPrimary);
5798 windowInPrimary->consumeFocusEvent(false);
5799 secondWindowInPrimary->consumeFocusEvent(true);
5800
5801 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005802 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
5803 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005804 windowInPrimary->assertNoEvents();
5805 windowInSecondary->assertNoEvents();
5806 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5807}
5808
Arthur Hungdfd528e2021-12-08 13:23:04 +00005809TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
5810 FakeMonitorReceiver monitorInPrimary =
5811 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5812 FakeMonitorReceiver monitorInSecondary =
5813 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
5814
5815 // Test touch down on primary display.
5816 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5817 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5818 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5819 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5820 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5821
5822 // Test touch down on second display.
5823 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5824 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5825 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5826 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
5827 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
5828
5829 // Trigger cancel touch.
5830 mDispatcher->cancelCurrentTouch();
5831 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5832 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5833 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
5834 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
5835
5836 // Test inject a move motion event, no window/monitor should receive the event.
5837 ASSERT_EQ(InputEventInjectionResult::FAILED,
5838 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5839 ADISPLAY_ID_DEFAULT, {110, 200}))
5840 << "Inject motion event should return InputEventInjectionResult::FAILED";
5841 windowInPrimary->assertNoEvents();
5842 monitorInPrimary.assertNoEvents();
5843
5844 ASSERT_EQ(InputEventInjectionResult::FAILED,
5845 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5846 SECOND_DISPLAY_ID, {110, 200}))
5847 << "Inject motion event should return InputEventInjectionResult::FAILED";
5848 windowInSecondary->assertNoEvents();
5849 monitorInSecondary.assertNoEvents();
5850}
5851
Jackal Guof9696682018-10-05 12:23:23 +08005852class InputFilterTest : public InputDispatcherTest {
5853protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005854 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
5855 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08005856 NotifyMotionArgs motionArgs;
5857
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005858 motionArgs =
5859 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005860 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005861 motionArgs =
5862 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005863 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005864 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005865 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005866 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
5867 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08005868 } else {
5869 mFakePolicy->assertFilterInputEventWasNotCalled();
5870 }
5871 }
5872
5873 void testNotifyKey(bool expectToBeFiltered) {
5874 NotifyKeyArgs keyArgs;
5875
5876 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005877 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005878 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005879 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005880 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005881
5882 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08005883 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005884 } else {
5885 mFakePolicy->assertFilterInputEventWasNotCalled();
5886 }
5887 }
5888};
5889
5890// Test InputFilter for MotionEvent
5891TEST_F(InputFilterTest, MotionEvent_InputFilter) {
5892 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
5893 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5894 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5895
5896 // Enable InputFilter
5897 mDispatcher->setInputFilterEnabled(true);
5898 // Test touch on both primary and second display, and check if both events are filtered.
5899 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
5900 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
5901
5902 // Disable InputFilter
5903 mDispatcher->setInputFilterEnabled(false);
5904 // Test touch on both primary and second display, and check if both events aren't filtered.
5905 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5906 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5907}
5908
5909// Test InputFilter for KeyEvent
5910TEST_F(InputFilterTest, KeyEvent_InputFilter) {
5911 // Since the InputFilter is disabled by default, check if key event aren't filtered.
5912 testNotifyKey(/*expectToBeFiltered*/ false);
5913
5914 // Enable InputFilter
5915 mDispatcher->setInputFilterEnabled(true);
5916 // Send a key event, and check if it is filtered.
5917 testNotifyKey(/*expectToBeFiltered*/ true);
5918
5919 // Disable InputFilter
5920 mDispatcher->setInputFilterEnabled(false);
5921 // Send a key event, and check if it isn't filtered.
5922 testNotifyKey(/*expectToBeFiltered*/ false);
5923}
5924
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005925// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
5926// logical display coordinate space.
5927TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
5928 ui::Transform firstDisplayTransform;
5929 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5930 ui::Transform secondDisplayTransform;
5931 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
5932
5933 std::vector<gui::DisplayInfo> displayInfos(2);
5934 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
5935 displayInfos[0].transform = firstDisplayTransform;
5936 displayInfos[1].displayId = SECOND_DISPLAY_ID;
5937 displayInfos[1].transform = secondDisplayTransform;
5938
Patrick Williamsd828f302023-04-28 17:52:08 -05005939 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005940
5941 // Enable InputFilter
5942 mDispatcher->setInputFilterEnabled(true);
5943
5944 // Ensure the correct transforms are used for the displays.
5945 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
5946 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
5947}
5948
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005949class InputFilterInjectionPolicyTest : public InputDispatcherTest {
5950protected:
5951 virtual void SetUp() override {
5952 InputDispatcherTest::SetUp();
5953
5954 /**
5955 * We don't need to enable input filter to test the injected event policy, but we enabled it
5956 * here to make the tests more realistic, since this policy only matters when inputfilter is
5957 * on.
5958 */
5959 mDispatcher->setInputFilterEnabled(true);
5960
5961 std::shared_ptr<InputApplicationHandle> application =
5962 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005963 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
5964 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005965
5966 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5967 mWindow->setFocusable(true);
5968 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5969 setFocusedWindow(mWindow);
5970 mWindow->consumeFocusEvent(true);
5971 }
5972
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005973 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5974 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005975 KeyEvent event;
5976
5977 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5978 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
5979 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00005980 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005981 const int32_t additionalPolicyFlags =
5982 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
5983 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005984 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005985 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5986 policyFlags | additionalPolicyFlags));
5987
5988 InputEvent* received = mWindow->consume();
5989 ASSERT_NE(nullptr, received);
5990 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005991 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005992 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
5993 ASSERT_EQ(flags, keyEvent.getFlags());
5994 }
5995
5996 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5997 int32_t flags) {
5998 MotionEvent event;
5999 PointerProperties pointerProperties[1];
6000 PointerCoords pointerCoords[1];
6001 pointerProperties[0].clear();
6002 pointerProperties[0].id = 0;
6003 pointerCoords[0].clear();
6004 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6005 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6006
6007 ui::Transform identityTransform;
6008 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6009 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6010 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6011 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6012 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006013 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006014 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006015 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6016
6017 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6018 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006019 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006020 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6021 policyFlags | additionalPolicyFlags));
6022
6023 InputEvent* received = mWindow->consume();
6024 ASSERT_NE(nullptr, received);
6025 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006026 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006027 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6028 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006029 }
6030
6031private:
6032 sp<FakeWindowHandle> mWindow;
6033};
6034
6035TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006036 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6037 // filter. Without it, the event will no different from a regularly injected event, and the
6038 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006039 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6040 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006041}
6042
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006043TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006044 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006045 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006046 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6047}
6048
6049TEST_F(InputFilterInjectionPolicyTest,
6050 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6051 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006052 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006053 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006054}
6055
6056TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006057 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6058 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006059}
6060
chaviwfd6d3512019-03-25 13:23:49 -07006061class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006062 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006063 InputDispatcherTest::SetUp();
6064
Chris Yea209fde2020-07-22 13:54:51 -07006065 std::shared_ptr<FakeApplicationHandle> application =
6066 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006067 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006068 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006069 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006070
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006071 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006072 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006073 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006074
6075 // Set focused application.
6076 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006077 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006078
6079 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006080 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006081 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006082 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006083 }
6084
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006085 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006086 InputDispatcherTest::TearDown();
6087
6088 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006089 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006090 }
6091
6092protected:
6093 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006094 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006095 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006096};
6097
6098// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6099// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6100// the onPointerDownOutsideFocus callback.
6101TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006102 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006103 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6104 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006105 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006106 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006107
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006108 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006109 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6110}
6111
6112// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6113// DOWN on the window that doesn't have focus. Ensure no window received the
6114// onPointerDownOutsideFocus callback.
6115TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006116 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006117 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006118 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006119 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006120
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006121 ASSERT_TRUE(mDispatcher->waitForIdle());
6122 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006123}
6124
6125// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6126// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6127TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006128 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6129 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006130 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006131 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006132
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006133 ASSERT_TRUE(mDispatcher->waitForIdle());
6134 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006135}
6136
6137// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6138// DOWN on the window that already has focus. Ensure no window received the
6139// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006140TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006141 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006142 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006143 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006144 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006145 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006146
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006147 ASSERT_TRUE(mDispatcher->waitForIdle());
6148 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006149}
6150
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006151// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6152// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6153TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6154 const MotionEvent event =
6155 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6156 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006157 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006158 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6159 .build();
6160 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6161 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6162 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6163
6164 ASSERT_TRUE(mDispatcher->waitForIdle());
6165 mFakePolicy->assertOnPointerDownWasNotCalled();
6166 // Ensure that the unfocused window did not receive any FOCUS events.
6167 mUnfocusedWindow->assertNoEvents();
6168}
6169
chaviwaf87b3e2019-10-01 16:59:28 -07006170// These tests ensures we can send touch events to a single client when there are multiple input
6171// windows that point to the same client token.
6172class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6173 virtual void SetUp() override {
6174 InputDispatcherTest::SetUp();
6175
Chris Yea209fde2020-07-22 13:54:51 -07006176 std::shared_ptr<FakeApplicationHandle> application =
6177 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006178 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6179 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006180 mWindow1->setFrame(Rect(0, 0, 100, 100));
6181
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006182 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6183 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006184 mWindow2->setFrame(Rect(100, 100, 200, 200));
6185
Arthur Hung72d8dc32020-03-28 00:48:39 +00006186 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006187 }
6188
6189protected:
6190 sp<FakeWindowHandle> mWindow1;
6191 sp<FakeWindowHandle> mWindow2;
6192
6193 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006194 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006195 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6196 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006197 }
6198
6199 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6200 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006201 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006202 InputEvent* event = window->consume();
6203
6204 ASSERT_NE(nullptr, event) << name.c_str()
6205 << ": consumer should have returned non-NULL event.";
6206
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006207 ASSERT_EQ(InputEventType::MOTION, event->getType())
6208 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006209
6210 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006211 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006212 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006213
6214 for (size_t i = 0; i < points.size(); i++) {
6215 float expectedX = points[i].x;
6216 float expectedY = points[i].y;
6217
6218 EXPECT_EQ(expectedX, motionEvent.getX(i))
6219 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6220 << ", got " << motionEvent.getX(i);
6221 EXPECT_EQ(expectedY, motionEvent.getY(i))
6222 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6223 << ", got " << motionEvent.getY(i);
6224 }
6225 }
chaviw9eaa22c2020-07-01 16:21:27 -07006226
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006227 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006228 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006229 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6230 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006231
6232 // Always consume from window1 since it's the window that has the InputReceiver
6233 consumeMotionEvent(mWindow1, action, expectedPoints);
6234 }
chaviwaf87b3e2019-10-01 16:59:28 -07006235};
6236
6237TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6238 // Touch Window 1
6239 PointF touchedPoint = {10, 10};
6240 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006241 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006242
6243 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006244 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006245
6246 // Touch Window 2
6247 touchedPoint = {150, 150};
6248 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006249 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006250}
6251
chaviw9eaa22c2020-07-01 16:21:27 -07006252TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6253 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006254 mWindow2->setWindowScale(0.5f, 0.5f);
6255
6256 // Touch Window 1
6257 PointF touchedPoint = {10, 10};
6258 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006259 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006260 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006261 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006262
6263 // Touch Window 2
6264 touchedPoint = {150, 150};
6265 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006266 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6267 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006268
chaviw9eaa22c2020-07-01 16:21:27 -07006269 // Update the transform so rotation is set
6270 mWindow2->setWindowTransform(0, -1, 1, 0);
6271 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6272 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006273}
6274
chaviw9eaa22c2020-07-01 16:21:27 -07006275TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006276 mWindow2->setWindowScale(0.5f, 0.5f);
6277
6278 // Touch Window 1
6279 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6280 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006281 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006282
6283 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006284 touchedPoints.push_back(PointF{150, 150});
6285 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006286 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006287
chaviw9eaa22c2020-07-01 16:21:27 -07006288 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006289 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006290 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006291
chaviw9eaa22c2020-07-01 16:21:27 -07006292 // Update the transform so rotation is set for Window 2
6293 mWindow2->setWindowTransform(0, -1, 1, 0);
6294 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006295 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006296}
6297
chaviw9eaa22c2020-07-01 16:21:27 -07006298TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006299 mWindow2->setWindowScale(0.5f, 0.5f);
6300
6301 // Touch Window 1
6302 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6303 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006304 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006305
6306 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006307 touchedPoints.push_back(PointF{150, 150});
6308 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006309
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006310 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006311
6312 // Move both windows
6313 touchedPoints = {{20, 20}, {175, 175}};
6314 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6315 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6316
chaviw9eaa22c2020-07-01 16:21:27 -07006317 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006318
chaviw9eaa22c2020-07-01 16:21:27 -07006319 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006320 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006321 expectedPoints.pop_back();
6322
6323 // Touch Window 2
6324 mWindow2->setWindowTransform(0, -1, 1, 0);
6325 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006326 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006327
6328 // Move both windows
6329 touchedPoints = {{20, 20}, {175, 175}};
6330 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6331 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6332
6333 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006334}
6335
6336TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6337 mWindow1->setWindowScale(0.5f, 0.5f);
6338
6339 // Touch Window 1
6340 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6341 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006342 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006343
6344 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006345 touchedPoints.push_back(PointF{150, 150});
6346 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006347
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006348 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006349
6350 // Move both windows
6351 touchedPoints = {{20, 20}, {175, 175}};
6352 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6353 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6354
chaviw9eaa22c2020-07-01 16:21:27 -07006355 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006356}
6357
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006358/**
6359 * When one of the windows is slippery, the touch should not slip into the other window with the
6360 * same input channel.
6361 */
6362TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6363 mWindow1->setSlippery(true);
6364 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6365
6366 // Touch down in window 1
6367 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6368 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6369 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6370
6371 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6372 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6373 // getting generated.
6374 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6375 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6376
6377 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6378}
6379
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07006380/**
6381 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
6382 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
6383 * that the pointer is hovering over may have a different transform.
6384 */
6385TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
6386 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6387
6388 // Start hover in window 1
6389 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN,
6390 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6391 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6392 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
6393
6394 // Move hover to window 2.
6395 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6396 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6397
6398 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
6399 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6400 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
6401}
6402
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006403class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6404 virtual void SetUp() override {
6405 InputDispatcherTest::SetUp();
6406
Chris Yea209fde2020-07-22 13:54:51 -07006407 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006408 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006409 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6410 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006411 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006412 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006413 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006414
6415 // Set focused application.
6416 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6417
6418 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006419 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006420 mWindow->consumeFocusEvent(true);
6421 }
6422
6423 virtual void TearDown() override {
6424 InputDispatcherTest::TearDown();
6425 mWindow.clear();
6426 }
6427
6428protected:
Chris Yea209fde2020-07-22 13:54:51 -07006429 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006430 sp<FakeWindowHandle> mWindow;
6431 static constexpr PointF WINDOW_LOCATION = {20, 20};
6432
6433 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006434 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006435 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6436 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006437 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006438 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6439 WINDOW_LOCATION));
6440 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006441
6442 sp<FakeWindowHandle> addSpyWindow() {
6443 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006444 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006445 spy->setTrustedOverlay(true);
6446 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006447 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006448 spy->setDispatchingTimeout(30ms);
6449 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6450 return spy;
6451 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006452};
6453
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006454// Send a tap and respond, which should not cause an ANR.
6455TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6456 tapOnWindow();
6457 mWindow->consumeMotionDown();
6458 mWindow->consumeMotionUp();
6459 ASSERT_TRUE(mDispatcher->waitForIdle());
6460 mFakePolicy->assertNotifyAnrWasNotCalled();
6461}
6462
6463// Send a regular key and respond, which should not cause an ANR.
6464TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006465 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006466 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6467 ASSERT_TRUE(mDispatcher->waitForIdle());
6468 mFakePolicy->assertNotifyAnrWasNotCalled();
6469}
6470
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006471TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6472 mWindow->setFocusable(false);
6473 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6474 mWindow->consumeFocusEvent(false);
6475
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006476 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006477 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6478 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6479 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006480 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006481 // Key will not go to window because we have no focused window.
6482 // The 'no focused window' ANR timer should start instead.
6483
6484 // Now, the focused application goes away.
6485 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6486 // The key should get dropped and there should be no ANR.
6487
6488 ASSERT_TRUE(mDispatcher->waitForIdle());
6489 mFakePolicy->assertNotifyAnrWasNotCalled();
6490}
6491
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006492// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006493// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6494// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006495TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006496 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006497 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6498 WINDOW_LOCATION));
6499
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006500 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6501 ASSERT_TRUE(sequenceNum);
6502 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006503 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006504
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006505 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006506 mWindow->consumeMotionEvent(
6507 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006508 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006509 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006510}
6511
6512// Send a key to the app and have the app not respond right away.
6513TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6514 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006515 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006516 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6517 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006518 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006519 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006520 ASSERT_TRUE(mDispatcher->waitForIdle());
6521}
6522
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006523// We have a focused application, but no focused window
6524TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006525 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006526 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6527 mWindow->consumeFocusEvent(false);
6528
6529 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006530 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006531 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6532 WINDOW_LOCATION));
6533 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6534 mDispatcher->waitForIdle();
6535 mFakePolicy->assertNotifyAnrWasNotCalled();
6536
6537 // Once a focused event arrives, we get an ANR for this application
6538 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6539 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006540 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006541 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6542 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006543 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006544 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006545 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006546 ASSERT_TRUE(mDispatcher->waitForIdle());
6547}
6548
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006549/**
6550 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6551 * there will not be an ANR.
6552 */
6553TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6554 mWindow->setFocusable(false);
6555 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6556 mWindow->consumeFocusEvent(false);
6557
6558 KeyEvent event;
6559 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6560 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6561
6562 // Define a valid key down event that is stale (too old).
6563 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6564 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006565 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006566
6567 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6568
6569 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006570 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006571 InputEventInjectionSync::WAIT_FOR_RESULT,
6572 INJECT_EVENT_TIMEOUT, policyFlags);
6573 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6574 << "Injection should fail because the event is stale";
6575
6576 ASSERT_TRUE(mDispatcher->waitForIdle());
6577 mFakePolicy->assertNotifyAnrWasNotCalled();
6578 mWindow->assertNoEvents();
6579}
6580
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006581// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006582// Make sure that we don't notify policy twice about the same ANR.
6583TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006584 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006585 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6586 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006587
6588 // Once a focused event arrives, we get an ANR for this application
6589 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6590 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006591 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006592 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6593 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006594 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006595 const std::chrono::duration appTimeout =
6596 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6597 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006598
Vishnu Naire4df8752022-09-08 09:17:55 -07006599 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006600 // ANR should not be raised again. It is up to policy to do that if it desires.
6601 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006602
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006603 // If we now get a focused window, the ANR should stop, but the policy handles that via
6604 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006605 ASSERT_TRUE(mDispatcher->waitForIdle());
6606}
6607
6608// We have a focused application, but no focused window
6609TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006610 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006611 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6612 mWindow->consumeFocusEvent(false);
6613
6614 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006615 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006616 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006617 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6618 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006619
Vishnu Naire4df8752022-09-08 09:17:55 -07006620 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6621 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006622
6623 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006624 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006625 ASSERT_TRUE(mDispatcher->waitForIdle());
6626 mWindow->assertNoEvents();
6627}
6628
6629/**
6630 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6631 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6632 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6633 * the ANR mechanism should still work.
6634 *
6635 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6636 * DOWN event, while not responding on the second one.
6637 */
6638TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6639 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6640 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6641 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6642 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6643 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006644 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006645
6646 // Now send ACTION_UP, with identical timestamp
6647 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6648 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6649 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6650 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006651 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006652
6653 // We have now sent down and up. Let's consume first event and then ANR on the second.
6654 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6655 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006656 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006657}
6658
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006659// A spy window can receive an ANR
6660TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6661 sp<FakeWindowHandle> spy = addSpyWindow();
6662
6663 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6664 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6665 WINDOW_LOCATION));
6666 mWindow->consumeMotionDown();
6667
6668 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6669 ASSERT_TRUE(sequenceNum);
6670 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006671 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006672
6673 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006674 spy->consumeMotionEvent(
6675 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006676 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006677 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006678}
6679
6680// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006681// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006682TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6683 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006684
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006685 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6686 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006687 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006689
6690 // Stuck on the ACTION_UP
6691 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006692 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006693
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006694 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006695 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006696 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6697 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006698
6699 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6700 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006701 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006702 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006703 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006704}
6705
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006706// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006707// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006708TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6709 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006710
6711 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006712 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6713 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006714
6715 mWindow->consumeMotionDown();
6716 // Stuck on the ACTION_UP
6717 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006718 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006719
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006720 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006721 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006722 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6723 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006724
6725 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6726 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006727 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006728 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006729 spy->assertNoEvents();
6730}
6731
6732TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6733 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6734
6735 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6736
6737 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6738 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6739 WINDOW_LOCATION));
6740
6741 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6742 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6743 ASSERT_TRUE(consumeSeq);
6744
Prabir Pradhanedd96402022-02-15 01:46:16 -08006745 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006746
6747 monitor.finishEvent(*consumeSeq);
6748 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6749
6750 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006751 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006752}
6753
6754// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6755// process events, you don't get an anr. When the window later becomes unresponsive again, you
6756// get an ANR again.
6757// 1. tap -> block on ACTION_UP -> receive ANR
6758// 2. consume all pending events (= queue becomes healthy again)
6759// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6760TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6761 tapOnWindow();
6762
6763 mWindow->consumeMotionDown();
6764 // Block on ACTION_UP
6765 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006766 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006767 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6768 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006769 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006770 mWindow->assertNoEvents();
6771
6772 tapOnWindow();
6773 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006774 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006775 mWindow->consumeMotionUp();
6776
6777 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006778 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006779 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006780 mWindow->assertNoEvents();
6781}
6782
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006783// If a connection remains unresponsive for a while, make sure policy is only notified once about
6784// it.
6785TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006786 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006787 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6788 WINDOW_LOCATION));
6789
6790 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006791 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006792 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006793 // 'notifyConnectionUnresponsive' should only be called once per connection
6794 mFakePolicy->assertNotifyAnrWasNotCalled();
6795 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006796 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006797 mWindow->consumeMotionEvent(
6798 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006799 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006800 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006801 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006802 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006803}
6804
6805/**
6806 * If a window is processing a motion event, and then a key event comes in, the key event should
6807 * not to to the focused window until the motion is processed.
6808 *
6809 * Warning!!!
6810 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6811 * and the injection timeout that we specify when injecting the key.
6812 * We must have the injection timeout (10ms) be smaller than
6813 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6814 *
6815 * If that value changes, this test should also change.
6816 */
6817TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
6818 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6819 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6820
6821 tapOnWindow();
6822 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6823 ASSERT_TRUE(downSequenceNum);
6824 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6825 ASSERT_TRUE(upSequenceNum);
6826 // Don't finish the events yet, and send a key
6827 // Injection will "succeed" because we will eventually give up and send the key to the focused
6828 // window even if motions are still being processed. But because the injection timeout is short,
6829 // we will receive INJECTION_TIMED_OUT as the result.
6830
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006831 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006832 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006833 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6834 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006835 // Key will not be sent to the window, yet, because the window is still processing events
6836 // and the key remains pending, waiting for the touch events to be processed
6837 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6838 ASSERT_FALSE(keySequenceNum);
6839
6840 std::this_thread::sleep_for(500ms);
6841 // if we wait long enough though, dispatcher will give up, and still send the key
6842 // to the focused window, even though we have not yet finished the motion event
6843 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6844 mWindow->finishEvent(*downSequenceNum);
6845 mWindow->finishEvent(*upSequenceNum);
6846}
6847
6848/**
6849 * If a window is processing a motion event, and then a key event comes in, the key event should
6850 * not go to the focused window until the motion is processed.
6851 * If then a new motion comes in, then the pending key event should be going to the currently
6852 * focused window right away.
6853 */
6854TEST_F(InputDispatcherSingleWindowAnr,
6855 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
6856 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6857 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6858
6859 tapOnWindow();
6860 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6861 ASSERT_TRUE(downSequenceNum);
6862 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6863 ASSERT_TRUE(upSequenceNum);
6864 // Don't finish the events yet, and send a key
6865 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006866 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006867 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6868 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006869 // At this point, key is still pending, and should not be sent to the application yet.
6870 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6871 ASSERT_FALSE(keySequenceNum);
6872
6873 // Now tap down again. It should cause the pending key to go to the focused window right away.
6874 tapOnWindow();
6875 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
6876 // the other events yet. We can finish events in any order.
6877 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
6878 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
6879 mWindow->consumeMotionDown();
6880 mWindow->consumeMotionUp();
6881 mWindow->assertNoEvents();
6882}
6883
6884class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
6885 virtual void SetUp() override {
6886 InputDispatcherTest::SetUp();
6887
Chris Yea209fde2020-07-22 13:54:51 -07006888 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006889 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006890 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
6891 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006892 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006893 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006894 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006895
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006896 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
6897 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006898 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006899 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006900
6901 // Set focused application.
6902 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07006903 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006904
6905 // Expect one focus window exist in display.
6906 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006907 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006908 mFocusedWindow->consumeFocusEvent(true);
6909 }
6910
6911 virtual void TearDown() override {
6912 InputDispatcherTest::TearDown();
6913
6914 mUnfocusedWindow.clear();
6915 mFocusedWindow.clear();
6916 }
6917
6918protected:
Chris Yea209fde2020-07-22 13:54:51 -07006919 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006920 sp<FakeWindowHandle> mUnfocusedWindow;
6921 sp<FakeWindowHandle> mFocusedWindow;
6922 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
6923 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
6924 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
6925
6926 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
6927
6928 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
6929
6930private:
6931 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006932 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006933 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6934 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006935 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006936 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6937 location));
6938 }
6939};
6940
6941// If we have 2 windows that are both unresponsive, the one with the shortest timeout
6942// should be ANR'd first.
6943TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006944 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006945 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6946 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006947 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006948 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006949 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006950 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006951 // We consumed all events, so no ANR
6952 ASSERT_TRUE(mDispatcher->waitForIdle());
6953 mFakePolicy->assertNotifyAnrWasNotCalled();
6954
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006955 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006956 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6957 FOCUSED_WINDOW_LOCATION));
6958 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
6959 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006960
6961 const std::chrono::duration timeout =
6962 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006963 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006964 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
6965 // sequence to make it consistent
6966 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006967 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006968 mFocusedWindow->consumeMotionDown();
6969 // This cancel is generated because the connection was unresponsive
6970 mFocusedWindow->consumeMotionCancel();
6971 mFocusedWindow->assertNoEvents();
6972 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006973 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006974 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6975 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006976 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006977}
6978
6979// If we have 2 windows with identical timeouts that are both unresponsive,
6980// it doesn't matter which order they should have ANR.
6981// But we should receive ANR for both.
6982TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
6983 // Set the timeout for unfocused window to match the focused window
6984 mUnfocusedWindow->setDispatchingTimeout(10ms);
6985 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6986
6987 tapOnFocusedWindow();
6988 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08006989 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
6990 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
6991 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006992
6993 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006994 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
6995 mFocusedWindow->getToken() == anrConnectionToken2);
6996 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
6997 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006998
6999 ASSERT_TRUE(mDispatcher->waitForIdle());
7000 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007001
7002 mFocusedWindow->consumeMotionDown();
7003 mFocusedWindow->consumeMotionUp();
7004 mUnfocusedWindow->consumeMotionOutside();
7005
Prabir Pradhanedd96402022-02-15 01:46:16 -08007006 sp<IBinder> responsiveToken1, responsiveToken2;
7007 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
7008 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007009
7010 // Both applications should be marked as responsive, in any order
7011 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
7012 mFocusedWindow->getToken() == responsiveToken2);
7013 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
7014 mUnfocusedWindow->getToken() == responsiveToken2);
7015 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007016}
7017
7018// If a window is already not responding, the second tap on the same window should be ignored.
7019// We should also log an error to account for the dropped event (not tested here).
7020// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7021TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7022 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007023 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007024 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007025 // Receive the events, but don't respond
7026 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7027 ASSERT_TRUE(downEventSequenceNum);
7028 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7029 ASSERT_TRUE(upEventSequenceNum);
7030 const std::chrono::duration timeout =
7031 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007032 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007033
7034 // Tap once again
7035 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007036 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007037 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7038 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007039 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007040 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7041 FOCUSED_WINDOW_LOCATION));
7042 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7043 // valid touch target
7044 mUnfocusedWindow->assertNoEvents();
7045
7046 // Consume the first tap
7047 mFocusedWindow->finishEvent(*downEventSequenceNum);
7048 mFocusedWindow->finishEvent(*upEventSequenceNum);
7049 ASSERT_TRUE(mDispatcher->waitForIdle());
7050 // The second tap did not go to the focused window
7051 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007052 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007053 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7054 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007055 mFakePolicy->assertNotifyAnrWasNotCalled();
7056}
7057
7058// If you tap outside of all windows, there will not be ANR
7059TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007060 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007061 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7062 LOCATION_OUTSIDE_ALL_WINDOWS));
7063 ASSERT_TRUE(mDispatcher->waitForIdle());
7064 mFakePolicy->assertNotifyAnrWasNotCalled();
7065}
7066
7067// Since the focused window is paused, tapping on it should not produce any events
7068TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7069 mFocusedWindow->setPaused(true);
7070 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7071
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007072 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007073 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7074 FOCUSED_WINDOW_LOCATION));
7075
7076 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7077 ASSERT_TRUE(mDispatcher->waitForIdle());
7078 // Should not ANR because the window is paused, and touches shouldn't go to it
7079 mFakePolicy->assertNotifyAnrWasNotCalled();
7080
7081 mFocusedWindow->assertNoEvents();
7082 mUnfocusedWindow->assertNoEvents();
7083}
7084
7085/**
7086 * If a window is processing a motion event, and then a key event comes in, the key event should
7087 * not to to the focused window until the motion is processed.
7088 * If a different window becomes focused at this time, the key should go to that window instead.
7089 *
7090 * Warning!!!
7091 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7092 * and the injection timeout that we specify when injecting the key.
7093 * We must have the injection timeout (10ms) be smaller than
7094 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7095 *
7096 * If that value changes, this test should also change.
7097 */
7098TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7099 // Set a long ANR timeout to prevent it from triggering
7100 mFocusedWindow->setDispatchingTimeout(2s);
7101 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7102
7103 tapOnUnfocusedWindow();
7104 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7105 ASSERT_TRUE(downSequenceNum);
7106 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7107 ASSERT_TRUE(upSequenceNum);
7108 // Don't finish the events yet, and send a key
7109 // Injection will succeed because we will eventually give up and send the key to the focused
7110 // window even if motions are still being processed.
7111
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007112 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007113 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7114 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007115 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007116 // Key will not be sent to the window, yet, because the window is still processing events
7117 // and the key remains pending, waiting for the touch events to be processed
7118 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7119 ASSERT_FALSE(keySequenceNum);
7120
7121 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007122 mFocusedWindow->setFocusable(false);
7123 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007124 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007125 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007126
7127 // Focus events should precede the key events
7128 mUnfocusedWindow->consumeFocusEvent(true);
7129 mFocusedWindow->consumeFocusEvent(false);
7130
7131 // Finish the tap events, which should unblock dispatcher
7132 mUnfocusedWindow->finishEvent(*downSequenceNum);
7133 mUnfocusedWindow->finishEvent(*upSequenceNum);
7134
7135 // Now that all queues are cleared and no backlog in the connections, the key event
7136 // can finally go to the newly focused "mUnfocusedWindow".
7137 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7138 mFocusedWindow->assertNoEvents();
7139 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007140 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007141}
7142
7143// When the touch stream is split across 2 windows, and one of them does not respond,
7144// then ANR should be raised and the touch should be canceled for the unresponsive window.
7145// The other window should not be affected by that.
7146TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7147 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007148 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7149 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7150 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007151 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007152 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007153
7154 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007155 mDispatcher->notifyMotion(
7156 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7157 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007158
7159 const std::chrono::duration timeout =
7160 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007161 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007162
7163 mUnfocusedWindow->consumeMotionDown();
7164 mFocusedWindow->consumeMotionDown();
7165 // Focused window may or may not receive ACTION_MOVE
7166 // But it should definitely receive ACTION_CANCEL due to the ANR
7167 InputEvent* event;
7168 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7169 ASSERT_TRUE(moveOrCancelSequenceNum);
7170 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7171 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007172 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007173 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7174 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7175 mFocusedWindow->consumeMotionCancel();
7176 } else {
7177 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7178 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007179 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007180 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7181 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007182
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007183 mUnfocusedWindow->assertNoEvents();
7184 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007185 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007186}
7187
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007188/**
7189 * If we have no focused window, and a key comes in, we start the ANR timer.
7190 * The focused application should add a focused window before the timer runs out to prevent ANR.
7191 *
7192 * If the user touches another application during this time, the key should be dropped.
7193 * Next, if a new focused window comes in, without toggling the focused application,
7194 * then no ANR should occur.
7195 *
7196 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7197 * but in some cases the policy may not update the focused application.
7198 */
7199TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7200 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7201 std::make_shared<FakeApplicationHandle>();
7202 focusedApplication->setDispatchingTimeout(60ms);
7203 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7204 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7205 mFocusedWindow->setFocusable(false);
7206
7207 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7208 mFocusedWindow->consumeFocusEvent(false);
7209
7210 // Send a key. The ANR timer should start because there is no focused window.
7211 // 'focusedApplication' will get blamed if this timer completes.
7212 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007213 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007214 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7215 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7216 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007217 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007218
7219 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7220 // then the injected touches won't cause the focused event to get dropped.
7221 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7222 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7223 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7224 // For this test, it means that the key would get delivered to the window once it becomes
7225 // focused.
7226 std::this_thread::sleep_for(10ms);
7227
7228 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007229 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7230 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7231 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007232
7233 // We do not consume the motion right away, because that would require dispatcher to first
7234 // process (== drop) the key event, and by that time, ANR will be raised.
7235 // Set the focused window first.
7236 mFocusedWindow->setFocusable(true);
7237 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7238 setFocusedWindow(mFocusedWindow);
7239 mFocusedWindow->consumeFocusEvent(true);
7240 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7241 // to another application. This could be a bug / behaviour in the policy.
7242
7243 mUnfocusedWindow->consumeMotionDown();
7244
7245 ASSERT_TRUE(mDispatcher->waitForIdle());
7246 // Should not ANR because we actually have a focused window. It was just added too slowly.
7247 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7248}
7249
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007250// These tests ensure we cannot send touch events to a window that's positioned behind a window
7251// that has feature NO_INPUT_CHANNEL.
7252// Layout:
7253// Top (closest to user)
7254// mNoInputWindow (above all windows)
7255// mBottomWindow
7256// Bottom (furthest from user)
7257class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7258 virtual void SetUp() override {
7259 InputDispatcherTest::SetUp();
7260
7261 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007262 mNoInputWindow =
7263 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7264 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007265 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007266 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007267 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7268 // It's perfectly valid for this window to not have an associated input channel
7269
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007270 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7271 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007272 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7273
7274 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7275 }
7276
7277protected:
7278 std::shared_ptr<FakeApplicationHandle> mApplication;
7279 sp<FakeWindowHandle> mNoInputWindow;
7280 sp<FakeWindowHandle> mBottomWindow;
7281};
7282
7283TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7284 PointF touchedPoint = {10, 10};
7285
Prabir Pradhan678438e2023-04-13 19:32:51 +00007286 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7287 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7288 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007289
7290 mNoInputWindow->assertNoEvents();
7291 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7292 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7293 // and therefore should prevent mBottomWindow from receiving touches
7294 mBottomWindow->assertNoEvents();
7295}
7296
7297/**
7298 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7299 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7300 */
7301TEST_F(InputDispatcherMultiWindowOcclusionTests,
7302 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007303 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7304 "Window with input channel and NO_INPUT_CHANNEL",
7305 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007306
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007307 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007308 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7309 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7310
7311 PointF touchedPoint = {10, 10};
7312
Prabir Pradhan678438e2023-04-13 19:32:51 +00007313 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7314 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7315 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007316
7317 mNoInputWindow->assertNoEvents();
7318 mBottomWindow->assertNoEvents();
7319}
7320
Vishnu Nair958da932020-08-21 17:12:37 -07007321class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7322protected:
7323 std::shared_ptr<FakeApplicationHandle> mApp;
7324 sp<FakeWindowHandle> mWindow;
7325 sp<FakeWindowHandle> mMirror;
7326
7327 virtual void SetUp() override {
7328 InputDispatcherTest::SetUp();
7329 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007330 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7331 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7332 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007333 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7334 mWindow->setFocusable(true);
7335 mMirror->setFocusable(true);
7336 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7337 }
7338};
7339
7340TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7341 // Request focus on a mirrored window
7342 setFocusedWindow(mMirror);
7343
7344 // window gets focused
7345 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007346 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7347 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007348 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7349}
7350
7351// A focused & mirrored window remains focused only if the window and its mirror are both
7352// focusable.
7353TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7354 setFocusedWindow(mMirror);
7355
7356 // window gets focused
7357 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007358 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7359 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007360 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007361 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7362 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007363 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7364
7365 mMirror->setFocusable(false);
7366 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7367
7368 // window loses focus since one of the windows associated with the token in not focusable
7369 mWindow->consumeFocusEvent(false);
7370
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007371 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7372 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007373 mWindow->assertNoEvents();
7374}
7375
7376// A focused & mirrored window remains focused until the window and its mirror both become
7377// invisible.
7378TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7379 setFocusedWindow(mMirror);
7380
7381 // window gets focused
7382 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007383 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7384 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007385 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007386 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7387 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007388 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7389
7390 mMirror->setVisible(false);
7391 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7392
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007393 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7394 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007395 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007396 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7397 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007398 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7399
7400 mWindow->setVisible(false);
7401 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7402
7403 // window loses focus only after all windows associated with the token become invisible.
7404 mWindow->consumeFocusEvent(false);
7405
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007406 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7407 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007408 mWindow->assertNoEvents();
7409}
7410
7411// A focused & mirrored window remains focused until both windows are removed.
7412TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7413 setFocusedWindow(mMirror);
7414
7415 // window gets focused
7416 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007417 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7418 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007419 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007420 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7421 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007422 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7423
7424 // single window is removed but the window token remains focused
7425 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7426
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007427 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7428 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007429 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007430 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7431 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007432 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7433
7434 // Both windows are removed
7435 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7436 mWindow->consumeFocusEvent(false);
7437
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007438 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7439 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007440 mWindow->assertNoEvents();
7441}
7442
7443// Focus request can be pending until one window becomes visible.
7444TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7445 // Request focus on an invisible mirror.
7446 mWindow->setVisible(false);
7447 mMirror->setVisible(false);
7448 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7449 setFocusedWindow(mMirror);
7450
7451 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007452 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007453 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7454 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007455
7456 mMirror->setVisible(true);
7457 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7458
7459 // window gets focused
7460 mWindow->consumeFocusEvent(true);
7461 // window gets the pending key event
7462 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7463}
Prabir Pradhan99987712020-11-10 18:43:05 -08007464
7465class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7466protected:
7467 std::shared_ptr<FakeApplicationHandle> mApp;
7468 sp<FakeWindowHandle> mWindow;
7469 sp<FakeWindowHandle> mSecondWindow;
7470
7471 void SetUp() override {
7472 InputDispatcherTest::SetUp();
7473 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007474 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007475 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007476 mSecondWindow =
7477 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007478 mSecondWindow->setFocusable(true);
7479
7480 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7481 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7482
7483 setFocusedWindow(mWindow);
7484 mWindow->consumeFocusEvent(true);
7485 }
7486
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007487 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007488 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007489 }
7490
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007491 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7492 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007493 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007494 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7495 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007496 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007497 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007498 }
7499};
7500
7501TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7502 // Ensure that capture cannot be obtained for unfocused windows.
7503 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7504 mFakePolicy->assertSetPointerCaptureNotCalled();
7505 mSecondWindow->assertNoEvents();
7506
7507 // Ensure that capture can be enabled from the focus window.
7508 requestAndVerifyPointerCapture(mWindow, true);
7509
7510 // Ensure that capture cannot be disabled from a window that does not have capture.
7511 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7512 mFakePolicy->assertSetPointerCaptureNotCalled();
7513
7514 // Ensure that capture can be disabled from the window with capture.
7515 requestAndVerifyPointerCapture(mWindow, false);
7516}
7517
7518TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007519 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007520
7521 setFocusedWindow(mSecondWindow);
7522
7523 // Ensure that the capture disabled event was sent first.
7524 mWindow->consumeCaptureEvent(false);
7525 mWindow->consumeFocusEvent(false);
7526 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007527 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007528
7529 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007530 notifyPointerCaptureChanged({});
7531 notifyPointerCaptureChanged(request);
7532 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007533 mWindow->assertNoEvents();
7534 mSecondWindow->assertNoEvents();
7535 mFakePolicy->assertSetPointerCaptureNotCalled();
7536}
7537
7538TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007539 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007540
7541 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007542 notifyPointerCaptureChanged({});
7543 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007544
7545 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007546 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007547 mWindow->consumeCaptureEvent(false);
7548 mWindow->assertNoEvents();
7549}
7550
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007551TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7552 requestAndVerifyPointerCapture(mWindow, true);
7553
7554 // The first window loses focus.
7555 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007556 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007557 mWindow->consumeCaptureEvent(false);
7558
7559 // Request Pointer Capture from the second window before the notification from InputReader
7560 // arrives.
7561 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007562 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007563
7564 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007565 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007566
7567 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007568 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007569
7570 mSecondWindow->consumeFocusEvent(true);
7571 mSecondWindow->consumeCaptureEvent(true);
7572}
7573
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007574TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7575 // App repeatedly enables and disables capture.
7576 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7577 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7578 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7579 mFakePolicy->assertSetPointerCaptureCalled(false);
7580 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7581 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7582
7583 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7584 // first request is now stale, this should do nothing.
7585 notifyPointerCaptureChanged(firstRequest);
7586 mWindow->assertNoEvents();
7587
7588 // InputReader notifies that the second request was enabled.
7589 notifyPointerCaptureChanged(secondRequest);
7590 mWindow->consumeCaptureEvent(true);
7591}
7592
Prabir Pradhan7092e262022-05-03 16:51:09 +00007593TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7594 requestAndVerifyPointerCapture(mWindow, true);
7595
7596 // App toggles pointer capture off and on.
7597 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7598 mFakePolicy->assertSetPointerCaptureCalled(false);
7599
7600 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7601 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7602
7603 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7604 // preceding "disable" request.
7605 notifyPointerCaptureChanged(enableRequest);
7606
7607 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7608 // any notifications.
7609 mWindow->assertNoEvents();
7610}
7611
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007612class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7613protected:
7614 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007615
7616 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7617 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7618
7619 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7620 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7621
7622 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7623 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7624 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7625 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7626 MAXIMUM_OBSCURING_OPACITY);
7627
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007628 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007629 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007630 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007631
7632 sp<FakeWindowHandle> mTouchWindow;
7633
7634 virtual void SetUp() override {
7635 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007636 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007637 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7638 }
7639
7640 virtual void TearDown() override {
7641 InputDispatcherTest::TearDown();
7642 mTouchWindow.clear();
7643 }
7644
chaviw3277faf2021-05-19 16:45:23 -05007645 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7646 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007647 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007648 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007649 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007650 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007651 return window;
7652 }
7653
7654 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7655 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7656 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007657 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007658 // Generate an arbitrary PID based on the UID
7659 window->setOwnerInfo(1777 + (uid % 10000), uid);
7660 return window;
7661 }
7662
7663 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007664 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7665 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7666 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007667 }
7668};
7669
7670TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007671 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007672 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007673 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007674
7675 touch();
7676
7677 mTouchWindow->assertNoEvents();
7678}
7679
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007680TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007681 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7682 const sp<FakeWindowHandle>& w =
7683 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7684 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7685
7686 touch();
7687
7688 mTouchWindow->assertNoEvents();
7689}
7690
7691TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007692 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7693 const sp<FakeWindowHandle>& w =
7694 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7695 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7696
7697 touch();
7698
7699 w->assertNoEvents();
7700}
7701
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007702TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007703 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7704 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007705
7706 touch();
7707
7708 mTouchWindow->consumeAnyMotionDown();
7709}
7710
7711TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007712 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007713 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007714 w->setFrame(Rect(0, 0, 50, 50));
7715 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007716
7717 touch({PointF{100, 100}});
7718
7719 mTouchWindow->consumeAnyMotionDown();
7720}
7721
7722TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007723 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007724 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007725 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7726
7727 touch();
7728
7729 mTouchWindow->consumeAnyMotionDown();
7730}
7731
7732TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7733 const sp<FakeWindowHandle>& w =
7734 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7735 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007736
7737 touch();
7738
7739 mTouchWindow->consumeAnyMotionDown();
7740}
7741
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007742TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7743 const sp<FakeWindowHandle>& w =
7744 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7745 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7746
7747 touch();
7748
7749 w->assertNoEvents();
7750}
7751
7752/**
7753 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7754 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7755 * window, the occluding window will still receive ACTION_OUTSIDE event.
7756 */
7757TEST_F(InputDispatcherUntrustedTouchesTest,
7758 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7759 const sp<FakeWindowHandle>& w =
7760 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007761 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007762 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7763
7764 touch();
7765
7766 w->consumeMotionOutside();
7767}
7768
7769TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
7770 const sp<FakeWindowHandle>& w =
7771 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007772 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007773 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7774
7775 touch();
7776
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007777 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007778}
7779
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007780TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007781 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007782 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7783 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007784 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7785
7786 touch();
7787
7788 mTouchWindow->consumeAnyMotionDown();
7789}
7790
7791TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
7792 const sp<FakeWindowHandle>& w =
7793 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7794 MAXIMUM_OBSCURING_OPACITY);
7795 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007796
7797 touch();
7798
7799 mTouchWindow->consumeAnyMotionDown();
7800}
7801
7802TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007803 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007804 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7805 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007806 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7807
7808 touch();
7809
7810 mTouchWindow->assertNoEvents();
7811}
7812
7813TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
7814 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
7815 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007816 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7817 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007818 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007819 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7820 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007821 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7822
7823 touch();
7824
7825 mTouchWindow->assertNoEvents();
7826}
7827
7828TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
7829 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
7830 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007831 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7832 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007833 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007834 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7835 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007836 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7837
7838 touch();
7839
7840 mTouchWindow->consumeAnyMotionDown();
7841}
7842
7843TEST_F(InputDispatcherUntrustedTouchesTest,
7844 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
7845 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007846 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7847 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007848 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007849 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7850 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007851 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7852
7853 touch();
7854
7855 mTouchWindow->consumeAnyMotionDown();
7856}
7857
7858TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
7859 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007860 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7861 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007862 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007863 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7864 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007865 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007866
7867 touch();
7868
7869 mTouchWindow->assertNoEvents();
7870}
7871
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007872TEST_F(InputDispatcherUntrustedTouchesTest,
7873 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
7874 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007875 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7876 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007877 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007878 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7879 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007880 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7881
7882 touch();
7883
7884 mTouchWindow->assertNoEvents();
7885}
7886
7887TEST_F(InputDispatcherUntrustedTouchesTest,
7888 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
7889 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007890 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7891 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007892 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007893 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7894 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007895 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7896
7897 touch();
7898
7899 mTouchWindow->consumeAnyMotionDown();
7900}
7901
7902TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
7903 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007904 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7905 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007906 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7907
7908 touch();
7909
7910 mTouchWindow->consumeAnyMotionDown();
7911}
7912
7913TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
7914 const sp<FakeWindowHandle>& w =
7915 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
7916 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7917
7918 touch();
7919
7920 mTouchWindow->consumeAnyMotionDown();
7921}
7922
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007923TEST_F(InputDispatcherUntrustedTouchesTest,
7924 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
7925 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7926 const sp<FakeWindowHandle>& w =
7927 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
7928 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7929
7930 touch();
7931
7932 mTouchWindow->assertNoEvents();
7933}
7934
7935TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
7936 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7937 const sp<FakeWindowHandle>& w =
7938 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
7939 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7940
7941 touch();
7942
7943 mTouchWindow->consumeAnyMotionDown();
7944}
7945
7946TEST_F(InputDispatcherUntrustedTouchesTest,
7947 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
7948 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
7949 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007950 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7951 OPACITY_ABOVE_THRESHOLD);
7952 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7953
7954 touch();
7955
7956 mTouchWindow->consumeAnyMotionDown();
7957}
7958
7959TEST_F(InputDispatcherUntrustedTouchesTest,
7960 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
7961 const sp<FakeWindowHandle>& w1 =
7962 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7963 OPACITY_BELOW_THRESHOLD);
7964 const sp<FakeWindowHandle>& w2 =
7965 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7966 OPACITY_BELOW_THRESHOLD);
7967 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7968
7969 touch();
7970
7971 mTouchWindow->assertNoEvents();
7972}
7973
7974/**
7975 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
7976 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
7977 * (which alone would result in allowing touches) does not affect the blocking behavior.
7978 */
7979TEST_F(InputDispatcherUntrustedTouchesTest,
7980 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
7981 const sp<FakeWindowHandle>& wB =
7982 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7983 OPACITY_BELOW_THRESHOLD);
7984 const sp<FakeWindowHandle>& wC =
7985 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7986 OPACITY_BELOW_THRESHOLD);
7987 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7988
7989 touch();
7990
7991 mTouchWindow->assertNoEvents();
7992}
7993
7994/**
7995 * This test is testing that a window from a different UID but with same application token doesn't
7996 * block the touch. Apps can share the application token for close UI collaboration for example.
7997 */
7998TEST_F(InputDispatcherUntrustedTouchesTest,
7999 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
8000 const sp<FakeWindowHandle>& w =
8001 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
8002 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008003 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8004
8005 touch();
8006
8007 mTouchWindow->consumeAnyMotionDown();
8008}
8009
arthurhungb89ccb02020-12-30 16:19:01 +08008010class InputDispatcherDragTests : public InputDispatcherTest {
8011protected:
8012 std::shared_ptr<FakeApplicationHandle> mApp;
8013 sp<FakeWindowHandle> mWindow;
8014 sp<FakeWindowHandle> mSecondWindow;
8015 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008016 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008017 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8018 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008019
8020 void SetUp() override {
8021 InputDispatcherTest::SetUp();
8022 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008023 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008024 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008025
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008026 mSecondWindow =
8027 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008028 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008029
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008030 mSpyWindow =
8031 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008032 mSpyWindow->setSpy(true);
8033 mSpyWindow->setTrustedOverlay(true);
8034 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8035
arthurhungb89ccb02020-12-30 16:19:01 +08008036 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008037 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008038 }
8039
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008040 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8041 switch (fromSource) {
8042 case AINPUT_SOURCE_TOUCHSCREEN:
8043 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8044 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8045 ADISPLAY_ID_DEFAULT, {50, 50}))
8046 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8047 break;
8048 case AINPUT_SOURCE_STYLUS:
8049 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8050 injectMotionEvent(
8051 mDispatcher,
8052 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8053 AINPUT_SOURCE_STYLUS)
8054 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008055 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008056 .x(50)
8057 .y(50))
8058 .build()));
8059 break;
8060 case AINPUT_SOURCE_MOUSE:
8061 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8062 injectMotionEvent(
8063 mDispatcher,
8064 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8065 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8066 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008067 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008068 .x(50)
8069 .y(50))
8070 .build()));
8071 break;
8072 default:
8073 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8074 }
arthurhungb89ccb02020-12-30 16:19:01 +08008075
8076 // Window should receive motion event.
8077 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008078 // Spy window should also receive motion event
8079 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008080 }
8081
8082 // Start performing drag, we will create a drag window and transfer touch to it.
8083 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8084 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008085 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008086 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008087 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008088 }
arthurhungb89ccb02020-12-30 16:19:01 +08008089
8090 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008091 mDragWindow =
8092 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008093 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008094 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008095 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008096
8097 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008098 bool transferred =
8099 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008100 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008101 if (transferred) {
8102 mWindow->consumeMotionCancel();
8103 mDragWindow->consumeMotionDown();
8104 }
8105 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008106 }
8107};
8108
8109TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008110 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008111
8112 // Move on window.
8113 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8114 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8115 ADISPLAY_ID_DEFAULT, {50, 50}))
8116 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8117 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8118 mWindow->consumeDragEvent(false, 50, 50);
8119 mSecondWindow->assertNoEvents();
8120
8121 // Move to another window.
8122 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8123 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8124 ADISPLAY_ID_DEFAULT, {150, 50}))
8125 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8126 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8127 mWindow->consumeDragEvent(true, 150, 50);
8128 mSecondWindow->consumeDragEvent(false, 50, 50);
8129
8130 // Move back to original window.
8131 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8132 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8133 ADISPLAY_ID_DEFAULT, {50, 50}))
8134 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8135 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8136 mWindow->consumeDragEvent(false, 50, 50);
8137 mSecondWindow->consumeDragEvent(true, -50, 50);
8138
8139 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8140 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8141 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8142 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8143 mWindow->assertNoEvents();
8144 mSecondWindow->assertNoEvents();
8145}
8146
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008147TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008148 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008149
8150 // No cancel event after drag start
8151 mSpyWindow->assertNoEvents();
8152
8153 const MotionEvent secondFingerDownEvent =
8154 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8155 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008156 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8157 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008158 .build();
8159 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8160 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8161 InputEventInjectionSync::WAIT_FOR_RESULT))
8162 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8163
8164 // Receives cancel for first pointer after next pointer down
8165 mSpyWindow->consumeMotionCancel();
8166 mSpyWindow->consumeMotionDown();
8167
8168 mSpyWindow->assertNoEvents();
8169}
8170
arthurhungf452d0b2021-01-06 00:19:52 +08008171TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008172 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008173
8174 // Move on window.
8175 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8176 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8177 ADISPLAY_ID_DEFAULT, {50, 50}))
8178 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8179 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8180 mWindow->consumeDragEvent(false, 50, 50);
8181 mSecondWindow->assertNoEvents();
8182
8183 // Move to another window.
8184 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8185 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8186 ADISPLAY_ID_DEFAULT, {150, 50}))
8187 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8188 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8189 mWindow->consumeDragEvent(true, 150, 50);
8190 mSecondWindow->consumeDragEvent(false, 50, 50);
8191
8192 // drop to another window.
8193 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8194 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8195 {150, 50}))
8196 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8197 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8198 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8199 mWindow->assertNoEvents();
8200 mSecondWindow->assertNoEvents();
8201}
8202
arthurhung6d4bed92021-03-17 11:59:33 +08008203TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008204 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008205
8206 // Move on window and keep button pressed.
8207 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8208 injectMotionEvent(mDispatcher,
8209 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8210 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008211 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008212 .build()))
8213 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8214 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8215 mWindow->consumeDragEvent(false, 50, 50);
8216 mSecondWindow->assertNoEvents();
8217
8218 // Move to another window and release button, expect to drop item.
8219 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8220 injectMotionEvent(mDispatcher,
8221 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8222 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008223 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008224 .build()))
8225 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8226 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8227 mWindow->assertNoEvents();
8228 mSecondWindow->assertNoEvents();
8229 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8230
8231 // nothing to the window.
8232 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8233 injectMotionEvent(mDispatcher,
8234 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8235 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008236 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008237 .build()))
8238 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8239 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8240 mWindow->assertNoEvents();
8241 mSecondWindow->assertNoEvents();
8242}
8243
Arthur Hung54745652022-04-20 07:17:41 +00008244TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008245 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008246
8247 // Set second window invisible.
8248 mSecondWindow->setVisible(false);
8249 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8250
8251 // Move on window.
8252 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8253 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8254 ADISPLAY_ID_DEFAULT, {50, 50}))
8255 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8256 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8257 mWindow->consumeDragEvent(false, 50, 50);
8258 mSecondWindow->assertNoEvents();
8259
8260 // Move to another window.
8261 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8262 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8263 ADISPLAY_ID_DEFAULT, {150, 50}))
8264 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8265 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8266 mWindow->consumeDragEvent(true, 150, 50);
8267 mSecondWindow->assertNoEvents();
8268
8269 // drop to another window.
8270 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8271 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8272 {150, 50}))
8273 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8274 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8275 mFakePolicy->assertDropTargetEquals(nullptr);
8276 mWindow->assertNoEvents();
8277 mSecondWindow->assertNoEvents();
8278}
8279
Arthur Hung54745652022-04-20 07:17:41 +00008280TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008281 // Ensure window could track pointerIds if it didn't support split touch.
8282 mWindow->setPreventSplitting(true);
8283
Arthur Hung54745652022-04-20 07:17:41 +00008284 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8285 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8286 {50, 50}))
8287 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8288 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8289
8290 const MotionEvent secondFingerDownEvent =
8291 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8292 .displayId(ADISPLAY_ID_DEFAULT)
8293 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008294 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8295 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008296 .build();
8297 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8298 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8299 InputEventInjectionSync::WAIT_FOR_RESULT))
8300 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008301 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008302
8303 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008304 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008305}
8306
8307TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8308 // First down on second window.
8309 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8310 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8311 {150, 50}))
8312 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8313
8314 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8315
8316 // Second down on first window.
8317 const MotionEvent secondFingerDownEvent =
8318 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8319 .displayId(ADISPLAY_ID_DEFAULT)
8320 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008321 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8322 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008323 .build();
8324 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8325 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8326 InputEventInjectionSync::WAIT_FOR_RESULT))
8327 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8328 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8329
8330 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008331 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008332
8333 // Move on window.
8334 const MotionEvent secondFingerMoveEvent =
8335 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8336 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008337 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8338 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008339 .build();
8340 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8341 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8342 InputEventInjectionSync::WAIT_FOR_RESULT));
8343 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8344 mWindow->consumeDragEvent(false, 50, 50);
8345 mSecondWindow->consumeMotionMove();
8346
8347 // Release the drag pointer should perform drop.
8348 const MotionEvent secondFingerUpEvent =
8349 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8350 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008351 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8352 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008353 .build();
8354 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8355 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8356 InputEventInjectionSync::WAIT_FOR_RESULT));
8357 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8358 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8359 mWindow->assertNoEvents();
8360 mSecondWindow->consumeMotionMove();
8361}
8362
Arthur Hung3915c1f2022-05-31 07:17:17 +00008363TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008364 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008365
8366 // Update window of second display.
8367 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008368 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008369 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8370
8371 // Let second display has a touch state.
8372 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8373 injectMotionEvent(mDispatcher,
8374 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8375 AINPUT_SOURCE_TOUCHSCREEN)
8376 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008377 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008378 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008379 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008380 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008381 // Update window again.
8382 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8383
8384 // Move on window.
8385 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8386 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8387 ADISPLAY_ID_DEFAULT, {50, 50}))
8388 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8389 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8390 mWindow->consumeDragEvent(false, 50, 50);
8391 mSecondWindow->assertNoEvents();
8392
8393 // Move to another window.
8394 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8395 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8396 ADISPLAY_ID_DEFAULT, {150, 50}))
8397 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8398 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8399 mWindow->consumeDragEvent(true, 150, 50);
8400 mSecondWindow->consumeDragEvent(false, 50, 50);
8401
8402 // drop to another window.
8403 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8404 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8405 {150, 50}))
8406 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8407 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8408 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8409 mWindow->assertNoEvents();
8410 mSecondWindow->assertNoEvents();
8411}
8412
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008413TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8414 startDrag(true, AINPUT_SOURCE_MOUSE);
8415 // Move on window.
8416 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8417 injectMotionEvent(mDispatcher,
8418 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8419 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8420 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008421 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008422 .x(50)
8423 .y(50))
8424 .build()))
8425 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8426 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8427 mWindow->consumeDragEvent(false, 50, 50);
8428 mSecondWindow->assertNoEvents();
8429
8430 // Move to another window.
8431 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8432 injectMotionEvent(mDispatcher,
8433 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8434 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8435 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008436 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008437 .x(150)
8438 .y(50))
8439 .build()))
8440 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8441 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8442 mWindow->consumeDragEvent(true, 150, 50);
8443 mSecondWindow->consumeDragEvent(false, 50, 50);
8444
8445 // drop to another window.
8446 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8447 injectMotionEvent(mDispatcher,
8448 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8449 .buttonState(0)
8450 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008451 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008452 .x(150)
8453 .y(50))
8454 .build()))
8455 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8456 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8457 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8458 mWindow->assertNoEvents();
8459 mSecondWindow->assertNoEvents();
8460}
8461
Vishnu Nair062a8672021-09-03 16:07:44 -07008462class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8463
8464TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8465 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008466 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8467 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008468 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008469 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8470 window->setFocusable(true);
8471 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8472 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008473 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008474
8475 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008476 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008477 window->assertNoEvents();
8478
Prabir Pradhan678438e2023-04-13 19:32:51 +00008479 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8480 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008481 window->assertNoEvents();
8482
8483 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008484 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008485 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8486
Prabir Pradhan678438e2023-04-13 19:32:51 +00008487 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008488 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8489
Prabir Pradhan678438e2023-04-13 19:32:51 +00008490 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8491 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008492 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8493 window->assertNoEvents();
8494}
8495
8496TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8497 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8498 std::make_shared<FakeApplicationHandle>();
8499 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008500 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8501 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008502 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8503 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008504 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008505 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008506 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8507 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008508 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008509 window->setOwnerInfo(222, 222);
8510 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8511 window->setFocusable(true);
8512 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8513 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008514 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008515
8516 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008517 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008518 window->assertNoEvents();
8519
Prabir Pradhan678438e2023-04-13 19:32:51 +00008520 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8521 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008522 window->assertNoEvents();
8523
8524 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008525 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008526 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8527
Prabir Pradhan678438e2023-04-13 19:32:51 +00008528 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008529 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8530
Prabir Pradhan678438e2023-04-13 19:32:51 +00008531 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8532 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008533 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8534 window->assertNoEvents();
8535}
8536
8537TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8538 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8539 std::make_shared<FakeApplicationHandle>();
8540 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008541 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8542 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008543 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8544 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008545 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008546 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008547 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8548 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008549 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008550 window->setOwnerInfo(222, 222);
8551 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8552 window->setFocusable(true);
8553 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8554 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008555 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008556
8557 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008558 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008559 window->assertNoEvents();
8560
Prabir Pradhan678438e2023-04-13 19:32:51 +00008561 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8562 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008563 window->assertNoEvents();
8564
8565 // When the window is no longer obscured because it went on top, it should get input
8566 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8567
Prabir Pradhan678438e2023-04-13 19:32:51 +00008568 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008569 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8570
Prabir Pradhan678438e2023-04-13 19:32:51 +00008571 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8572 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008573 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8574 window->assertNoEvents();
8575}
8576
Antonio Kantekf16f2832021-09-28 04:39:20 +00008577class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8578protected:
8579 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008580 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008581 sp<FakeWindowHandle> mWindow;
8582 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008583 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008584
8585 void SetUp() override {
8586 InputDispatcherTest::SetUp();
8587
8588 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008589 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008590 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008591 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008592 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008593 mSecondWindow =
8594 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008595 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008596 mThirdWindow =
8597 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8598 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8599 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008600
8601 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008602 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8603 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8604 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008605 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008606
Antonio Kantek15beb512022-06-13 22:35:41 +00008607 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008608 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008609 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008610 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8611 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008612 mThirdWindow->assertNoEvents();
8613 }
8614
8615 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8616 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008617 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008618 SECOND_DISPLAY_ID)) {
8619 mWindow->assertNoEvents();
8620 mSecondWindow->assertNoEvents();
8621 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008622 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008623 }
8624
Antonio Kantek15beb512022-06-13 22:35:41 +00008625 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8626 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008627 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8628 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008629 mWindow->consumeTouchModeEvent(inTouchMode);
8630 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008631 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008632 }
8633};
8634
Antonio Kantek26defcf2022-02-08 01:12:27 +00008635TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008636 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008637 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8638 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008639 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008640}
8641
Antonio Kantek26defcf2022-02-08 01:12:27 +00008642TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8643 const WindowInfo& windowInfo = *mWindow->getInfo();
8644 int32_t ownerPid = windowInfo.ownerPid;
8645 int32_t ownerUid = windowInfo.ownerUid;
8646 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8647 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008648 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008649 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008650 mWindow->assertNoEvents();
8651 mSecondWindow->assertNoEvents();
8652}
8653
8654TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8655 const WindowInfo& windowInfo = *mWindow->getInfo();
8656 int32_t ownerPid = windowInfo.ownerPid;
8657 int32_t ownerUid = windowInfo.ownerUid;
8658 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008659 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008660 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008661}
8662
Antonio Kantekf16f2832021-09-28 04:39:20 +00008663TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008664 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008665 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8666 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008667 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008668 mWindow->assertNoEvents();
8669 mSecondWindow->assertNoEvents();
8670}
8671
Antonio Kantek15beb512022-06-13 22:35:41 +00008672TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8673 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8674 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8675 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008676 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008677 mWindow->assertNoEvents();
8678 mSecondWindow->assertNoEvents();
8679 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8680}
8681
Antonio Kantek48710e42022-03-24 14:19:30 -07008682TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8683 // Interact with the window first.
8684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8685 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8686 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8687
8688 // Then remove focus.
8689 mWindow->setFocusable(false);
8690 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8691
8692 // Assert that caller can switch touch mode by owning one of the last interacted window.
8693 const WindowInfo& windowInfo = *mWindow->getInfo();
8694 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8695 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008696 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008697}
8698
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008699class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8700public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008701 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008702 std::shared_ptr<FakeApplicationHandle> application =
8703 std::make_shared<FakeApplicationHandle>();
8704 std::string name = "Fake Spy ";
8705 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008706 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8707 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008708 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008709 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008710 return spy;
8711 }
8712
8713 sp<FakeWindowHandle> createForeground() {
8714 std::shared_ptr<FakeApplicationHandle> application =
8715 std::make_shared<FakeApplicationHandle>();
8716 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008717 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8718 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008719 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008720 return window;
8721 }
8722
8723private:
8724 int mSpyCount{0};
8725};
8726
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008727using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008728/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008729 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8730 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008731TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8732 ScopedSilentDeath _silentDeath;
8733
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008734 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008735 spy->setTrustedOverlay(false);
8736 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8737 ".* not a trusted overlay");
8738}
8739
8740/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008741 * Input injection into a display with a spy window but no foreground windows should succeed.
8742 */
8743TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008744 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008745 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8746
8747 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8748 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8749 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8750 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8751}
8752
8753/**
8754 * Verify the order in which different input windows receive events. The touched foreground window
8755 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8756 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8757 * receive events before ones belows it.
8758 *
8759 * Here, we set up a scenario with four windows in the following Z order from the top:
8760 * spy1, spy2, window, spy3.
8761 * We then inject an event and verify that the foreground "window" receives it first, followed by
8762 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
8763 * window.
8764 */
8765TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
8766 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008767 auto spy1 = createSpy();
8768 auto spy2 = createSpy();
8769 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008770 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
8771 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
8772 const size_t numChannels = channels.size();
8773
Michael Wright8e9a8562022-02-09 13:44:29 +00008774 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008775 if (!epollFd.ok()) {
8776 FAIL() << "Failed to create epoll fd";
8777 }
8778
8779 for (size_t i = 0; i < numChannels; i++) {
8780 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
8781 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
8782 FAIL() << "Failed to add fd to epoll";
8783 }
8784 }
8785
8786 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8787 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8788 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8789
8790 std::vector<size_t> eventOrder;
8791 std::vector<struct epoll_event> events(numChannels);
8792 for (;;) {
8793 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
8794 (100ms).count());
8795 if (nFds < 0) {
8796 FAIL() << "Failed to call epoll_wait";
8797 }
8798 if (nFds == 0) {
8799 break; // epoll_wait timed out
8800 }
8801 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07008802 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07008803 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008804 channels[i]->consumeMotionDown();
8805 }
8806 }
8807
8808 // Verify the order in which the events were received.
8809 EXPECT_EQ(3u, eventOrder.size());
8810 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
8811 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
8812 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
8813}
8814
8815/**
8816 * A spy window using the NOT_TOUCHABLE flag does not receive events.
8817 */
8818TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
8819 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008820 auto spy = createSpy();
8821 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008822 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8823
8824 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8825 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8826 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8827 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8828 spy->assertNoEvents();
8829}
8830
8831/**
8832 * A spy window will only receive gestures that originate within its touchable region. Gestures that
8833 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
8834 * to the window.
8835 */
8836TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
8837 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008838 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008839 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
8840 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8841
8842 // Inject an event outside the spy window's touchable region.
8843 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8844 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8845 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8846 window->consumeMotionDown();
8847 spy->assertNoEvents();
8848 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8849 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8850 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8851 window->consumeMotionUp();
8852 spy->assertNoEvents();
8853
8854 // Inject an event inside the spy window's touchable region.
8855 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8856 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8857 {5, 10}))
8858 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8859 window->consumeMotionDown();
8860 spy->consumeMotionDown();
8861}
8862
8863/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008864 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008865 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008866 */
8867TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
8868 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008869 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008870 auto spy = createSpy();
8871 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008872 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008873 spy->setFrame(Rect{0, 0, 20, 20});
8874 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8875
8876 // Inject an event outside the spy window's frame and touchable region.
8877 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008878 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8879 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008880 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8881 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008882 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008883}
8884
8885/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008886 * Even when a spy window spans over multiple foreground windows, the spy should receive all
8887 * pointers that are down within its bounds.
8888 */
8889TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
8890 auto windowLeft = createForeground();
8891 windowLeft->setFrame({0, 0, 100, 200});
8892 auto windowRight = createForeground();
8893 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008894 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008895 spy->setFrame({0, 0, 200, 200});
8896 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
8897
8898 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8899 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8900 {50, 50}))
8901 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8902 windowLeft->consumeMotionDown();
8903 spy->consumeMotionDown();
8904
8905 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008906 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008907 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008908 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8909 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008910 .build();
8911 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8912 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8913 InputEventInjectionSync::WAIT_FOR_RESULT))
8914 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8915 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00008916 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008917}
8918
8919/**
8920 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
8921 * the spy should receive the second pointer with ACTION_DOWN.
8922 */
8923TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
8924 auto window = createForeground();
8925 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008926 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008927 spyRight->setFrame({100, 0, 200, 200});
8928 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
8929
8930 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8931 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8932 {50, 50}))
8933 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8934 window->consumeMotionDown();
8935 spyRight->assertNoEvents();
8936
8937 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008938 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008939 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008940 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8941 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008942 .build();
8943 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8944 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8945 InputEventInjectionSync::WAIT_FOR_RESULT))
8946 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008947 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008948 spyRight->consumeMotionDown();
8949}
8950
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008951/**
8952 * The spy window should not be able to affect whether or not touches are split. Only the foreground
8953 * windows should be allowed to control split touch.
8954 */
8955TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008956 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008957 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008958 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008959 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008960
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008961 auto window = createForeground();
8962 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008963
8964 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8965
8966 // First finger down, no window touched.
8967 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8968 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8969 {100, 200}))
8970 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8971 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8972 window->assertNoEvents();
8973
8974 // Second finger down on window, the window should receive touch down.
8975 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008976 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008977 .displayId(ADISPLAY_ID_DEFAULT)
8978 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008979 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8980 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008981 .build();
8982 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8983 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8984 InputEventInjectionSync::WAIT_FOR_RESULT))
8985 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8986
8987 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00008988 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008989}
8990
8991/**
8992 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
8993 * do not receive key events.
8994 */
8995TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008996 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008997 spy->setFocusable(false);
8998
8999 auto window = createForeground();
9000 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9001 setFocusedWindow(window);
9002 window->consumeFocusEvent(true);
9003
9004 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
9005 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9006 window->consumeKeyDown(ADISPLAY_ID_NONE);
9007
9008 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
9009 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9010 window->consumeKeyUp(ADISPLAY_ID_NONE);
9011
9012 spy->assertNoEvents();
9013}
9014
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009015using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9016
9017/**
9018 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9019 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9020 */
9021TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9022 auto window = createForeground();
9023 auto spy1 = createSpy();
9024 auto spy2 = createSpy();
9025 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
9026
9027 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9028 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9029 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9030 window->consumeMotionDown();
9031 spy1->consumeMotionDown();
9032 spy2->consumeMotionDown();
9033
9034 // Pilfer pointers from the second spy window.
9035 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9036 spy2->assertNoEvents();
9037 spy1->consumeMotionCancel();
9038 window->consumeMotionCancel();
9039
9040 // The rest of the gesture should only be sent to the second spy window.
9041 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9042 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9043 ADISPLAY_ID_DEFAULT))
9044 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9045 spy2->consumeMotionMove();
9046 spy1->assertNoEvents();
9047 window->assertNoEvents();
9048}
9049
9050/**
9051 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9052 * in the middle of the gesture.
9053 */
9054TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9055 auto window = createForeground();
9056 auto spy = createSpy();
9057 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9058
9059 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9060 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9061 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9062 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9063 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9064
9065 window->releaseChannel();
9066
9067 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9068
9069 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9070 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9071 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9072 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9073}
9074
9075/**
9076 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9077 * the spy, but not to any other windows.
9078 */
9079TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9080 auto spy = createSpy();
9081 auto window = createForeground();
9082
9083 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9084
9085 // First finger down on the window and the spy.
9086 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9087 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9088 {100, 200}))
9089 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9090 spy->consumeMotionDown();
9091 window->consumeMotionDown();
9092
9093 // Spy window pilfers the pointers.
9094 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9095 window->consumeMotionCancel();
9096
9097 // Second finger down on the window and spy, but the window should not receive the pointer down.
9098 const MotionEvent secondFingerDownEvent =
9099 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9100 .displayId(ADISPLAY_ID_DEFAULT)
9101 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009102 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9103 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009104 .build();
9105 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9106 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9107 InputEventInjectionSync::WAIT_FOR_RESULT))
9108 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9109
Harry Cutts33476232023-01-30 19:57:29 +00009110 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009111
9112 // Third finger goes down outside all windows, so injection should fail.
9113 const MotionEvent thirdFingerDownEvent =
9114 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9115 .displayId(ADISPLAY_ID_DEFAULT)
9116 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009117 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9118 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9119 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009120 .build();
9121 ASSERT_EQ(InputEventInjectionResult::FAILED,
9122 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9123 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009124 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009125
9126 spy->assertNoEvents();
9127 window->assertNoEvents();
9128}
9129
9130/**
9131 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9132 */
9133TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9134 auto spy = createSpy();
9135 spy->setFrame(Rect(0, 0, 100, 100));
9136 auto window = createForeground();
9137 window->setFrame(Rect(0, 0, 200, 200));
9138
9139 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9140
9141 // First finger down on the window only
9142 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9143 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9144 {150, 150}))
9145 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9146 window->consumeMotionDown();
9147
9148 // Second finger down on the spy and window
9149 const MotionEvent secondFingerDownEvent =
9150 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9151 .displayId(ADISPLAY_ID_DEFAULT)
9152 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009153 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9154 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009155 .build();
9156 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9157 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9158 InputEventInjectionSync::WAIT_FOR_RESULT))
9159 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9160 spy->consumeMotionDown();
9161 window->consumeMotionPointerDown(1);
9162
9163 // Third finger down on the spy and window
9164 const MotionEvent thirdFingerDownEvent =
9165 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9166 .displayId(ADISPLAY_ID_DEFAULT)
9167 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009168 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9169 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9170 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009171 .build();
9172 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9173 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9174 InputEventInjectionSync::WAIT_FOR_RESULT))
9175 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9176 spy->consumeMotionPointerDown(1);
9177 window->consumeMotionPointerDown(2);
9178
9179 // Spy window pilfers the pointers.
9180 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9181 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9182 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9183
9184 spy->assertNoEvents();
9185 window->assertNoEvents();
9186}
9187
9188/**
9189 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9190 * other windows should be canceled. If this results in the cancellation of all pointers for some
9191 * window, then that window should receive ACTION_CANCEL.
9192 */
9193TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9194 auto spy = createSpy();
9195 spy->setFrame(Rect(0, 0, 100, 100));
9196 auto window = createForeground();
9197 window->setFrame(Rect(0, 0, 200, 200));
9198
9199 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9200
9201 // First finger down on both spy and window
9202 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9203 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9204 {10, 10}))
9205 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9206 window->consumeMotionDown();
9207 spy->consumeMotionDown();
9208
9209 // Second finger down on the spy and window
9210 const MotionEvent secondFingerDownEvent =
9211 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9212 .displayId(ADISPLAY_ID_DEFAULT)
9213 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009214 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9215 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009216 .build();
9217 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9218 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9219 InputEventInjectionSync::WAIT_FOR_RESULT))
9220 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9221 spy->consumeMotionPointerDown(1);
9222 window->consumeMotionPointerDown(1);
9223
9224 // Spy window pilfers the pointers.
9225 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9226 window->consumeMotionCancel();
9227
9228 spy->assertNoEvents();
9229 window->assertNoEvents();
9230}
9231
9232/**
9233 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9234 * be sent to other windows
9235 */
9236TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9237 auto spy = createSpy();
9238 spy->setFrame(Rect(0, 0, 100, 100));
9239 auto window = createForeground();
9240 window->setFrame(Rect(0, 0, 200, 200));
9241
9242 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9243
9244 // First finger down on both window and spy
9245 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9246 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9247 {10, 10}))
9248 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9249 window->consumeMotionDown();
9250 spy->consumeMotionDown();
9251
9252 // Spy window pilfers the pointers.
9253 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9254 window->consumeMotionCancel();
9255
9256 // Second finger down on the window only
9257 const MotionEvent secondFingerDownEvent =
9258 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9259 .displayId(ADISPLAY_ID_DEFAULT)
9260 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009261 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9262 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009263 .build();
9264 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9265 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9266 InputEventInjectionSync::WAIT_FOR_RESULT))
9267 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9268 window->consumeMotionDown();
9269 window->assertNoEvents();
9270
9271 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9272 spy->consumeMotionMove();
9273 spy->assertNoEvents();
9274}
9275
Prabir Pradhand65552b2021-10-07 11:23:50 -07009276class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9277public:
9278 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9279 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9280 std::make_shared<FakeApplicationHandle>();
9281 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009282 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9283 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009284 overlay->setFocusable(false);
9285 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009286 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009287 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009288 overlay->setTrustedOverlay(true);
9289
9290 std::shared_ptr<FakeApplicationHandle> application =
9291 std::make_shared<FakeApplicationHandle>();
9292 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009293 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9294 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009295 window->setFocusable(true);
9296 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009297
9298 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9299 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9300 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009301 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009302 return {std::move(overlay), std::move(window)};
9303 }
9304
9305 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009306 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009307 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009308 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009309 }
9310
9311 void sendStylusEvent(int32_t action) {
9312 NotifyMotionArgs motionArgs =
9313 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9314 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009315 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009316 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009317 }
9318};
9319
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009320using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9321
9322TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9323 ScopedSilentDeath _silentDeath;
9324
Prabir Pradhand65552b2021-10-07 11:23:50 -07009325 auto [overlay, window] = setupStylusOverlayScenario();
9326 overlay->setTrustedOverlay(false);
9327 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9328 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9329 ".* not a trusted overlay");
9330}
9331
9332TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9333 auto [overlay, window] = setupStylusOverlayScenario();
9334 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9335
9336 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9337 overlay->consumeMotionDown();
9338 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9339 overlay->consumeMotionUp();
9340
9341 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9342 window->consumeMotionDown();
9343 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9344 window->consumeMotionUp();
9345
9346 overlay->assertNoEvents();
9347 window->assertNoEvents();
9348}
9349
9350TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9351 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009352 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009353 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9354
9355 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9356 overlay->consumeMotionDown();
9357 window->consumeMotionDown();
9358 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9359 overlay->consumeMotionUp();
9360 window->consumeMotionUp();
9361
9362 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9363 window->consumeMotionDown();
9364 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9365 window->consumeMotionUp();
9366
9367 overlay->assertNoEvents();
9368 window->assertNoEvents();
9369}
9370
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009371/**
9372 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9373 * The scenario is as follows:
9374 * - The stylus interceptor overlay is configured as a spy window.
9375 * - The stylus interceptor spy receives the start of a new stylus gesture.
9376 * - It pilfers pointers and then configures itself to no longer be a spy.
9377 * - The stylus interceptor continues to receive the rest of the gesture.
9378 */
9379TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9380 auto [overlay, window] = setupStylusOverlayScenario();
9381 overlay->setSpy(true);
9382 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9383
9384 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9385 overlay->consumeMotionDown();
9386 window->consumeMotionDown();
9387
9388 // The interceptor pilfers the pointers.
9389 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9390 window->consumeMotionCancel();
9391
9392 // The interceptor configures itself so that it is no longer a spy.
9393 overlay->setSpy(false);
9394 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9395
9396 // It continues to receive the rest of the stylus gesture.
9397 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9398 overlay->consumeMotionMove();
9399 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9400 overlay->consumeMotionUp();
9401
9402 window->assertNoEvents();
9403}
9404
Prabir Pradhan5735a322022-04-11 17:23:34 +00009405struct User {
9406 int32_t mPid;
9407 int32_t mUid;
9408 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9409 std::unique_ptr<InputDispatcher>& mDispatcher;
9410
9411 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9412 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9413
9414 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9415 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9416 ADISPLAY_ID_DEFAULT, {100, 200},
9417 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9418 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9419 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9420 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9421 }
9422
9423 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009424 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009425 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009426 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009427 mPolicyFlags);
9428 }
9429
9430 sp<FakeWindowHandle> createWindow() const {
9431 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9432 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009433 sp<FakeWindowHandle> window =
9434 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9435 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009436 window->setOwnerInfo(mPid, mUid);
9437 return window;
9438 }
9439};
9440
9441using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9442
9443TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9444 auto owner = User(mDispatcher, 10, 11);
9445 auto window = owner.createWindow();
9446 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9447
9448 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9449 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9450 window->consumeMotionDown();
9451
9452 setFocusedWindow(window);
9453 window->consumeFocusEvent(true);
9454
9455 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9456 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9457 window->consumeKeyDown(ADISPLAY_ID_NONE);
9458}
9459
9460TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9461 auto owner = User(mDispatcher, 10, 11);
9462 auto window = owner.createWindow();
9463 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9464
9465 auto rando = User(mDispatcher, 20, 21);
9466 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9467 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9468
9469 setFocusedWindow(window);
9470 window->consumeFocusEvent(true);
9471
9472 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9473 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9474 window->assertNoEvents();
9475}
9476
9477TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9478 auto owner = User(mDispatcher, 10, 11);
9479 auto window = owner.createWindow();
9480 auto spy = owner.createWindow();
9481 spy->setSpy(true);
9482 spy->setTrustedOverlay(true);
9483 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9484
9485 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9486 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9487 spy->consumeMotionDown();
9488 window->consumeMotionDown();
9489}
9490
9491TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9492 auto owner = User(mDispatcher, 10, 11);
9493 auto window = owner.createWindow();
9494
9495 auto rando = User(mDispatcher, 20, 21);
9496 auto randosSpy = rando.createWindow();
9497 randosSpy->setSpy(true);
9498 randosSpy->setTrustedOverlay(true);
9499 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9500
9501 // The event is targeted at owner's window, so injection should succeed, but the spy should
9502 // not receive the event.
9503 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9504 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9505 randosSpy->assertNoEvents();
9506 window->consumeMotionDown();
9507}
9508
9509TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9510 auto owner = User(mDispatcher, 10, 11);
9511 auto window = owner.createWindow();
9512
9513 auto rando = User(mDispatcher, 20, 21);
9514 auto randosSpy = rando.createWindow();
9515 randosSpy->setSpy(true);
9516 randosSpy->setTrustedOverlay(true);
9517 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9518
9519 // A user that has injection permission can inject into any window.
9520 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9521 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9522 ADISPLAY_ID_DEFAULT));
9523 randosSpy->consumeMotionDown();
9524 window->consumeMotionDown();
9525
9526 setFocusedWindow(randosSpy);
9527 randosSpy->consumeFocusEvent(true);
9528
9529 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9530 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9531 window->assertNoEvents();
9532}
9533
9534TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9535 auto owner = User(mDispatcher, 10, 11);
9536 auto window = owner.createWindow();
9537
9538 auto rando = User(mDispatcher, 20, 21);
9539 auto randosWindow = rando.createWindow();
9540 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9541 randosWindow->setWatchOutsideTouch(true);
9542 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9543
9544 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9545 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9546 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9547 window->consumeMotionDown();
9548 randosWindow->consumeMotionOutside();
9549}
9550
Garfield Tane84e6f92019-08-29 17:28:41 -07009551} // namespace android::inputdispatcher