blob: e6b73af3e9cd373714efc3f1ea66b7ec6fae5396 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Prabir Pradhan9205e422023-05-16 20:06:13 +000018#include "EventBuilders.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080019
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070020#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080021#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080022#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070023#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070024#include <binder/Binder.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000025#include <fcntl.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080026#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080027#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100028#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080030#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100031
Garfield Tan1c7bc862020-01-28 13:24:04 -080032#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080033#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070034#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080035#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080036#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037
Garfield Tan1c7bc862020-01-28 13:24:04 -080038using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050039using android::gui::FocusRequest;
40using android::gui::TouchOcclusionMode;
41using android::gui::WindowInfo;
42using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080043using android::os::InputEventInjectionResult;
44using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080045
Garfield Tane84e6f92019-08-29 17:28:41 -070046namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080047
Dominik Laskowski2f01d772022-03-23 16:01:29 -070048using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080049using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070050
Michael Wrightd02c5b62014-02-10 15:10:22 -080051// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000052static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080053
54// An arbitrary device id.
Prabir Pradhan9205e422023-05-16 20:06:13 +000055static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080056static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080057
Jeff Brownf086ddb2014-02-11 14:28:48 -080058// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000059static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
60static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080061
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080062static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
63static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
64static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
65static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -070066static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080067static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080068static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080069static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080070/**
71 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
72 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
73 * index 0) is the new pointer going down. The same pointer could have been placed at a different
74 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
75 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
76 * pointer id=0 leaves but the pointer id=1 remains.
77 */
78static constexpr int32_t POINTER_0_DOWN =
79 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080080static constexpr int32_t POINTER_1_DOWN =
81 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000082static constexpr int32_t POINTER_2_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000084static constexpr int32_t POINTER_3_DOWN =
85 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000086static constexpr int32_t POINTER_0_UP =
87 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080088static constexpr int32_t POINTER_1_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Harry Cuttsb166c002023-05-09 13:06:05 +000090static constexpr int32_t POINTER_2_UP =
91 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080092
Antonio Kantek15beb512022-06-13 22:35:41 +000093// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000094static constexpr int32_t WINDOW_PID = 999;
95static constexpr int32_t WINDOW_UID = 1001;
96
Antonio Kantek15beb512022-06-13 22:35:41 +000097// The default pid and uid for the windows created on the secondary display by the test.
98static constexpr int32_t SECONDARY_WINDOW_PID = 1010;
99static constexpr int32_t SECONDARY_WINDOW_UID = 1012;
100
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000101// An arbitrary pid of the gesture monitor window
102static constexpr int32_t MONITOR_PID = 2001;
103
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800104static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
105
Arthur Hungc539dbb2022-12-08 07:45:36 +0000106static constexpr int expectedWallpaperFlags =
107 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
108
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800109using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
110
chaviwd1c23182019-12-20 18:44:56 -0800111struct PointF {
112 float x;
113 float y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800114 auto operator<=>(const PointF&) const = default;
chaviwd1c23182019-12-20 18:44:56 -0800115};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
Gang Wang342c9272020-01-13 13:15:04 -0500117/**
118 * Return a DOWN key event with KEYCODE_A.
119 */
120static KeyEvent getTestKeyEvent() {
121 KeyEvent event;
122
Garfield Tanfbe732e2020-01-24 11:26:14 -0800123 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
124 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
125 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500126 return event;
127}
128
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000129static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
130 ASSERT_EQ(expectedAction, receivedAction)
131 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
132 << MotionEvent::actionToString(receivedAction);
133}
134
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800135MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
136 bool matches = action == arg.getAction();
137 if (!matches) {
138 *result_listener << "expected action " << MotionEvent::actionToString(action)
139 << ", but got " << MotionEvent::actionToString(arg.getAction());
140 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800141 if (action == AMOTION_EVENT_ACTION_DOWN) {
142 if (!matches) {
143 *result_listener << "; ";
144 }
145 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
146 matches &= arg.getDownTime() == arg.getEventTime();
147 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800148 if (action == AMOTION_EVENT_ACTION_CANCEL) {
149 if (!matches) {
150 *result_listener << "; ";
151 }
152 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
153 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
154 }
155 return matches;
156}
157
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800158MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
159 return arg.getDownTime() == downTime;
160}
161
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800162MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
163 return arg.getDisplayId() == displayId;
164}
165
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800166MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") {
167 return arg.getDeviceId() == deviceId;
168}
169
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800170MATCHER_P(WithSource, source, "InputEvent with specified source") {
171 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
172 << inputEventSourceToString(arg.getSource());
173 return arg.getSource() == source;
174}
175
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800176MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
177 return arg.getFlags() == flags;
178}
179
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800180MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
181 if (arg.getPointerCount() != 1) {
182 *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
183 return false;
184 }
Harry Cutts33476232023-01-30 19:57:29 +0000185 return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800186}
187
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800188MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") {
189 return arg.getPointerCount() == pointerCount;
190}
191
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800192MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
193 // Build a map for the received pointers, by pointer id
194 std::map<int32_t /*pointerId*/, PointF> actualPointers;
195 for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
196 const int32_t pointerId = arg.getPointerId(pointerIndex);
197 actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
198 }
199 return pointers == actualPointers;
200}
201
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202// --- FakeInputDispatcherPolicy ---
203
204class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
205 InputDispatcherConfiguration mConfig;
206
Prabir Pradhanedd96402022-02-15 01:46:16 -0800207 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
208
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209public:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000210 FakeInputDispatcherPolicy() = default;
211 virtual ~FakeInputDispatcherPolicy() = default;
Jackal Guof9696682018-10-05 12:23:23 +0800212
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800213 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700214 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700215 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700216 EXPECT_EQ(event.getDisplayId(), args.displayId);
217
218 const auto& keyEvent = static_cast<const KeyEvent&>(event);
219 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
220 EXPECT_EQ(keyEvent.getAction(), args.action);
221 });
Jackal Guof9696682018-10-05 12:23:23 +0800222 }
223
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700224 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
225 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700226 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700227 EXPECT_EQ(event.getDisplayId(), args.displayId);
228
229 const auto& motionEvent = static_cast<const MotionEvent&>(event);
230 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
231 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000232 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
233 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
234 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
235 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700236 });
Jackal Guof9696682018-10-05 12:23:23 +0800237 }
238
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700239 void assertFilterInputEventWasNotCalled() {
240 std::scoped_lock lock(mLock);
241 ASSERT_EQ(nullptr, mFilteredEvent);
242 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800244 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700245 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800246 ASSERT_TRUE(mConfigurationChangedTime)
247 << "Timed out waiting for configuration changed call";
248 ASSERT_EQ(*mConfigurationChangedTime, when);
249 mConfigurationChangedTime = std::nullopt;
250 }
251
252 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700253 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800254 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800255 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800256 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
257 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
258 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
259 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
260 mLastNotifySwitch = std::nullopt;
261 }
262
chaviwfd6d3512019-03-25 13:23:49 -0700263 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700264 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800265 ASSERT_EQ(touchedToken, mOnPointerDownToken);
266 mOnPointerDownToken.clear();
267 }
268
269 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700270 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800271 ASSERT_TRUE(mOnPointerDownToken == nullptr)
272 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700273 }
274
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700275 // This function must be called soon after the expected ANR timer starts,
276 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500277 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700278 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500279 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800280 std::unique_lock lock(mLock);
281 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500282 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800283 ASSERT_NO_FATAL_FAILURE(
284 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500285 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700286 }
287
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000288 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800289 const sp<WindowInfoHandle>& window) {
290 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
291 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
292 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500293 }
294
Prabir Pradhanedd96402022-02-15 01:46:16 -0800295 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
296 const sp<IBinder>& expectedToken,
297 int32_t expectedPid) {
298 std::unique_lock lock(mLock);
299 android::base::ScopedLockAssertion assumeLocked(mLock);
300 AnrResult result;
301 ASSERT_NO_FATAL_FAILURE(result =
302 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
303 const auto& [token, pid] = result;
304 ASSERT_EQ(expectedToken, token);
305 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500306 }
307
Prabir Pradhanedd96402022-02-15 01:46:16 -0800308 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000309 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500310 std::unique_lock lock(mLock);
311 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800312 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
313 const auto& [token, _] = result;
314 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000315 }
316
Prabir Pradhanedd96402022-02-15 01:46:16 -0800317 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
318 int32_t expectedPid) {
319 std::unique_lock lock(mLock);
320 android::base::ScopedLockAssertion assumeLocked(mLock);
321 AnrResult result;
322 ASSERT_NO_FATAL_FAILURE(
323 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
324 const auto& [token, pid] = result;
325 ASSERT_EQ(expectedToken, token);
326 ASSERT_EQ(expectedPid, pid);
327 }
328
329 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000330 sp<IBinder> getResponsiveWindowToken() {
331 std::unique_lock lock(mLock);
332 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800333 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
334 const auto& [token, _] = result;
335 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700336 }
337
338 void assertNotifyAnrWasNotCalled() {
339 std::scoped_lock lock(mLock);
340 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800341 ASSERT_TRUE(mAnrWindows.empty());
342 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500343 << "ANR was not called, but please also consume the 'connection is responsive' "
344 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700345 }
346
Garfield Tan1c7bc862020-01-28 13:24:04 -0800347 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
348 mConfig.keyRepeatTimeout = timeout;
349 mConfig.keyRepeatDelay = delay;
350 }
351
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000352 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800353 std::unique_lock lock(mLock);
354 base::ScopedLockAssertion assumeLocked(mLock);
355
356 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
357 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000358 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800359 enabled;
360 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000361 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
362 << ") to be called.";
363 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800364 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000365 auto request = *mPointerCaptureRequest;
366 mPointerCaptureRequest.reset();
367 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800368 }
369
370 void assertSetPointerCaptureNotCalled() {
371 std::unique_lock lock(mLock);
372 base::ScopedLockAssertion assumeLocked(mLock);
373
374 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000375 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800376 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000377 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800378 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000379 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800380 }
381
arthurhungf452d0b2021-01-06 00:19:52 +0800382 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
383 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800384 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800385 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800386 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800387 }
388
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800389 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
390 std::unique_lock lock(mLock);
391 base::ScopedLockAssertion assumeLocked(mLock);
392 std::optional<sp<IBinder>> receivedToken =
393 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
394 mNotifyInputChannelBroken);
395 ASSERT_TRUE(receivedToken.has_value());
396 ASSERT_EQ(token, *receivedToken);
397 }
398
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800399 /**
400 * Set policy timeout. A value of zero means next key will not be intercepted.
401 */
402 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
403 mInterceptKeyTimeout = timeout;
404 }
405
Josep del Riob3981622023-04-18 15:49:45 +0000406 void assertUserActivityPoked() {
407 std::scoped_lock lock(mLock);
408 ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked";
409 }
410
411 void assertUserActivityNotPoked() {
412 std::scoped_lock lock(mLock);
413 ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked";
414 }
415
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700417 std::mutex mLock;
418 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
419 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
420 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
421 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800422
Prabir Pradhan99987712020-11-10 18:43:05 -0800423 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000424
425 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800426
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700427 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700428 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800429 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
430 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700431 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800432 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
433 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700434
arthurhungf452d0b2021-01-06 00:19:52 +0800435 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800436 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Josep del Riob3981622023-04-18 15:49:45 +0000437 bool mPokedUserActivity GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800438
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800439 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
440
Prabir Pradhanedd96402022-02-15 01:46:16 -0800441 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
442 // for a specific container to become non-empty. When the container is non-empty, return the
443 // first entry from the container and erase it.
444 template <class T>
445 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
446 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
447 // If there is an ANR, Dispatcher won't be idle because there are still events
448 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
449 // before checking if ANR was called.
450 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
451 // to provide it some time to act. 100ms seems reasonable.
452 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
453 const std::chrono::time_point start = std::chrono::steady_clock::now();
454 std::optional<T> token =
455 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
456 if (!token.has_value()) {
457 ADD_FAILURE() << "Did not receive the ANR callback";
458 return {};
459 }
460
461 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
462 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
463 // the dispatcher started counting before this function was called
464 if (std::chrono::abs(timeout - waited) > 100ms) {
465 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
466 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
467 << "ms, but waited "
468 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
469 << "ms instead";
470 }
471 return *token;
472 }
473
474 template <class T>
475 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
476 std::queue<T>& storage,
477 std::unique_lock<std::mutex>& lock,
478 std::condition_variable& condition)
479 REQUIRES(mLock) {
480 condition.wait_for(lock, timeout,
481 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
482 if (storage.empty()) {
483 ADD_FAILURE() << "Did not receive the expected callback";
484 return std::nullopt;
485 }
486 T item = storage.front();
487 storage.pop();
488 return std::make_optional(item);
489 }
490
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600491 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700492 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800493 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 }
495
Prabir Pradhanedd96402022-02-15 01:46:16 -0800496 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
497 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700498 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800499 ASSERT_TRUE(pid.has_value());
500 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700501 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500502 }
503
Prabir Pradhanedd96402022-02-15 01:46:16 -0800504 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
505 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500506 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800507 ASSERT_TRUE(pid.has_value());
508 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500509 mNotifyAnr.notify_all();
510 }
511
512 void notifyNoFocusedWindowAnr(
513 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
514 std::scoped_lock lock(mLock);
515 mAnrApplications.push(applicationHandle);
516 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517 }
518
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800519 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
520 std::scoped_lock lock(mLock);
521 mBrokenInputChannels.push(connectionToken);
522 mNotifyInputChannelBroken.notify_all();
523 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600525 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700526
Chris Yef59a2f42020-10-16 12:55:26 -0700527 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
528 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
529 const std::vector<float>& values) override {}
530
531 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
532 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000533
Chris Yefb552902021-02-03 17:18:37 -0800534 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
535
Prabir Pradhana41d2442023-04-20 21:30:40 +0000536 InputDispatcherConfiguration getDispatcherConfiguration() override { return mConfig; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537
Prabir Pradhana41d2442023-04-20 21:30:40 +0000538 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700539 std::scoped_lock lock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +0000540 switch (inputEvent.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700541 case InputEventType::KEY: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000542 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent);
543 mFilteredEvent = std::make_unique<KeyEvent>(keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800544 break;
545 }
546
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700547 case InputEventType::MOTION: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000548 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent);
549 mFilteredEvent = std::make_unique<MotionEvent>(motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800550 break;
551 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700552 default: {
553 ADD_FAILURE() << "Should only filter keys or motions";
554 break;
555 }
Jackal Guof9696682018-10-05 12:23:23 +0800556 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 return true;
558 }
559
Prabir Pradhana41d2442023-04-20 21:30:40 +0000560 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override {
561 if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800562 // Clear intercept state when we handled the event.
563 mInterceptKeyTimeout = 0ms;
564 }
565 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600567 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568
Prabir Pradhana41d2442023-04-20 21:30:40 +0000569 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800570 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
571 // Clear intercept state so we could dispatch the event in next wake.
572 mInterceptKeyTimeout = 0ms;
573 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 }
575
Prabir Pradhana41d2442023-04-20 21:30:40 +0000576 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&,
577 uint32_t) override {
578 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 }
580
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600581 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
582 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700583 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800584 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
585 * essentially a passthrough for notifySwitch.
586 */
Harry Cutts33476232023-01-30 19:57:29 +0000587 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 }
589
Josep del Riob3981622023-04-18 15:49:45 +0000590 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {
591 std::scoped_lock lock(mLock);
592 mPokedUserActivity = true;
593 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600595 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700596 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700597 mOnPointerDownToken = newToken;
598 }
599
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000600 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800601 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000602 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800603 mPointerCaptureChangedCondition.notify_all();
604 }
605
arthurhungf452d0b2021-01-06 00:19:52 +0800606 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
607 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800608 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800609 mDropTargetWindowToken = token;
610 }
611
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700612 void assertFilterInputEventWasCalledInternal(
613 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700614 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800615 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700616 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800617 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800618 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619};
620
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621// --- InputDispatcherTest ---
622
623class InputDispatcherTest : public testing::Test {
624protected:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000625 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700626 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000628 void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000629 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
630 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800631 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000632 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700633 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634 }
635
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000636 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700637 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000638 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700639 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700641
642 /**
643 * Used for debugging when writing the test
644 */
645 void dumpDispatcherState() {
646 std::string dump;
647 mDispatcher->dump(dump);
648 std::stringstream ss(dump);
649 std::string to;
650
651 while (std::getline(ss, to, '\n')) {
652 ALOGE("%s", to.c_str());
653 }
654 }
Vishnu Nair958da932020-08-21 17:12:37 -0700655
Chavi Weingarten847e8512023-03-29 00:26:09 +0000656 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700657 FocusRequest request;
658 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000659 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700660 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
661 request.displayId = window->getInfo()->displayId;
662 mDispatcher->setFocusedWindow(request);
663 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664};
665
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
667 KeyEvent event;
668
669 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800670 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
671 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600672 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
673 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800674 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000675 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000676 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677 << "Should reject key events with undefined action.";
678
679 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800680 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
681 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600682 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800683 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000684 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000685 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686 << "Should reject key events with ACTION_MULTIPLE.";
687}
688
689TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
690 MotionEvent event;
691 PointerProperties pointerProperties[MAX_POINTERS + 1];
692 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800693 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694 pointerProperties[i].clear();
695 pointerProperties[i].id = i;
696 pointerCoords[i].clear();
697 }
698
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800699 // Some constants commonly used below
700 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
701 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
702 constexpr int32_t metaState = AMETA_NONE;
703 constexpr MotionClassification classification = MotionClassification::NONE;
704
chaviw9eaa22c2020-07-01 16:21:27 -0700705 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800707 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700708 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
709 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700710 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
711 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700712 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800713 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000714 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000715 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 << "Should reject motion events with undefined action.";
717
718 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800719 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800720 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
721 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
722 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
723 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500724 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800725 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000726 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000727 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728 << "Should reject motion events with pointer down index too large.";
729
Garfield Tanfbe732e2020-01-24 11:26:14 -0800730 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700731 AMOTION_EVENT_ACTION_POINTER_DOWN |
732 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700733 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
734 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700735 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500736 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800737 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000738 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000739 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740 << "Should reject motion events with pointer down index too small.";
741
742 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800743 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800744 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
745 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
746 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
747 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500748 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800749 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000750 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000751 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 << "Should reject motion events with pointer up index too large.";
753
Garfield Tanfbe732e2020-01-24 11:26:14 -0800754 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700755 AMOTION_EVENT_ACTION_POINTER_UP |
756 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700757 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
758 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700759 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500760 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800761 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000762 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000763 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764 << "Should reject motion events with pointer up index too small.";
765
766 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800767 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
768 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700769 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700770 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
771 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700772 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800773 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000774 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000775 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800776 << "Should reject motion events with 0 pointers.";
777
Garfield Tanfbe732e2020-01-24 11:26:14 -0800778 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
779 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700780 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700781 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
782 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700783 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800784 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000785 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000786 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787 << "Should reject motion events with more than MAX_POINTERS pointers.";
788
789 // Rejects motion events with invalid pointer ids.
790 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800791 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
792 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700793 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700794 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
795 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700796 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800797 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000798 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000799 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800 << "Should reject motion events with pointer ids less than 0.";
801
802 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800803 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
804 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700805 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700806 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
807 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700808 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800809 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000810 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000811 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800812 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
813
814 // Rejects motion events with duplicate pointer ids.
815 pointerProperties[0].id = 1;
816 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800817 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
818 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700819 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700820 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
821 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700822 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800823 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000824 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000825 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826 << "Should reject motion events with duplicate pointer ids.";
827}
828
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800829/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
830
831TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
832 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000833 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800834 ASSERT_TRUE(mDispatcher->waitForIdle());
835
836 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
837}
838
839TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000840 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
841 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000842 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800843
844 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
845 args.policyFlags |= POLICY_FLAG_TRUSTED;
846 mFakePolicy->assertNotifySwitchWasCalled(args);
847}
848
Arthur Hungb92218b2018-08-14 12:00:21 +0800849// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700850static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700851// Default input dispatching timeout if there is no focused application or paused window
852// from which to determine an appropriate dispatching timeout.
853static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
854 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
855 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800856
857class FakeApplicationHandle : public InputApplicationHandle {
858public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700859 FakeApplicationHandle() {
860 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700861 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500862 mInfo.dispatchingTimeoutMillis =
863 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700864 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800865 virtual ~FakeApplicationHandle() {}
866
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000867 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700868
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500869 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
870 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700871 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800872};
873
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800874class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800875public:
Garfield Tan15601662020-09-22 15:32:38 -0700876 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800877 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700878 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800879 }
880
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800881 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700882 InputEvent* event;
883 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
884 if (!consumeSeq) {
885 return nullptr;
886 }
887 finishEvent(*consumeSeq);
888 return event;
889 }
890
891 /**
892 * Receive an event without acknowledging it.
893 * Return the sequence number that could later be used to send finished signal.
894 */
895 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800896 uint32_t consumeSeq;
897 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800898
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800899 std::chrono::time_point start = std::chrono::steady_clock::now();
900 status_t status = WOULD_BLOCK;
901 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000902 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800903 &event);
904 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
905 if (elapsed > 100ms) {
906 break;
907 }
908 }
909
910 if (status == WOULD_BLOCK) {
911 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700912 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800913 }
914
915 if (status != OK) {
916 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700917 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800918 }
919 if (event == nullptr) {
920 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700921 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800922 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700923 if (outEvent != nullptr) {
924 *outEvent = event;
925 }
926 return consumeSeq;
927 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800928
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700929 /**
930 * To be used together with "receiveEvent" to complete the consumption of an event.
931 */
932 void finishEvent(uint32_t consumeSeq) {
933 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
934 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800935 }
936
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000937 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
938 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
939 ASSERT_EQ(OK, status);
940 }
941
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700942 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000943 std::optional<int32_t> expectedDisplayId,
944 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800945 InputEvent* event = consume();
946
947 ASSERT_NE(nullptr, event) << mName.c_str()
948 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800949 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700950 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
951 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800952
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000953 if (expectedDisplayId.has_value()) {
954 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
955 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800956
Tiger Huang8664f8c2018-10-11 19:14:35 +0800957 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700958 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800959 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
960 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000961 if (expectedFlags.has_value()) {
962 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
963 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800964 break;
965 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700966 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800967 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000968 assertMotionAction(expectedAction, motionEvent.getAction());
969
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000970 if (expectedFlags.has_value()) {
971 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
972 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800973 break;
974 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700975 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100976 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
977 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700978 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -0800979 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
980 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700981 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +0000982 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
983 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700984 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +0800985 FAIL() << "Use 'consumeDragEvent' for DRAG events";
986 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800987 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800988 }
989
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800990 MotionEvent* consumeMotion() {
991 InputEvent* event = consume();
992
993 if (event == nullptr) {
994 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
995 return nullptr;
996 }
997
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700998 if (event->getType() != InputEventType::MOTION) {
999 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001000 return nullptr;
1001 }
1002 return static_cast<MotionEvent*>(event);
1003 }
1004
1005 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1006 MotionEvent* motionEvent = consumeMotion();
1007 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1008 ASSERT_THAT(*motionEvent, matcher);
1009 }
1010
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001011 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
1012 InputEvent* event = consume();
1013 ASSERT_NE(nullptr, event) << mName.c_str()
1014 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001015 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1016 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001017
1018 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1019 << mName.c_str() << ": event displayId should always be NONE.";
1020
1021 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1022 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001023 }
1024
Prabir Pradhan99987712020-11-10 18:43:05 -08001025 void consumeCaptureEvent(bool hasCapture) {
1026 const InputEvent* event = consume();
1027 ASSERT_NE(nullptr, event) << mName.c_str()
1028 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001029 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1030 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001031
1032 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1033 << mName.c_str() << ": event displayId should always be NONE.";
1034
1035 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1036 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1037 }
1038
arthurhungb89ccb02020-12-30 16:19:01 +08001039 void consumeDragEvent(bool isExiting, float x, float y) {
1040 const InputEvent* event = consume();
1041 ASSERT_NE(nullptr, event) << mName.c_str()
1042 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001043 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001044
1045 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1046 << mName.c_str() << ": event displayId should always be NONE.";
1047
1048 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1049 EXPECT_EQ(isExiting, dragEvent.isExiting());
1050 EXPECT_EQ(x, dragEvent.getX());
1051 EXPECT_EQ(y, dragEvent.getY());
1052 }
1053
Antonio Kantekf16f2832021-09-28 04:39:20 +00001054 void consumeTouchModeEvent(bool inTouchMode) {
1055 const InputEvent* event = consume();
1056 ASSERT_NE(nullptr, event) << mName.c_str()
1057 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001058 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1059 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001060
1061 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1062 << mName.c_str() << ": event displayId should always be NONE.";
1063 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1064 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1065 }
1066
chaviwd1c23182019-12-20 18:44:56 -08001067 void assertNoEvents() {
1068 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001069 if (event == nullptr) {
1070 return;
1071 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001072 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001073 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1074 ADD_FAILURE() << "Received key event "
1075 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001076 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001077 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1078 ADD_FAILURE() << "Received motion event "
1079 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001080 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001081 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1082 ADD_FAILURE() << "Received focus event, hasFocus = "
1083 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001084 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001085 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1086 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1087 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001088 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001089 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1090 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1091 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001092 }
1093 FAIL() << mName.c_str()
1094 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001095 }
1096
1097 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1098
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001099 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1100
chaviwd1c23182019-12-20 18:44:56 -08001101protected:
1102 std::unique_ptr<InputConsumer> mConsumer;
1103 PreallocatedInputEventFactory mEventFactory;
1104
1105 std::string mName;
1106};
1107
chaviw3277faf2021-05-19 16:45:23 -05001108class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001109public:
1110 static const int32_t WIDTH = 600;
1111 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001112
Chris Yea209fde2020-07-22 13:54:51 -07001113 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001114 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001115 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001116 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001117 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001118 base::Result<std::unique_ptr<InputChannel>> channel =
1119 dispatcher->createInputChannel(name);
1120 token = (*channel)->getConnectionToken();
1121 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001122 }
1123
1124 inputApplicationHandle->updateInfo();
1125 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1126
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001127 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001128 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001129 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001130 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001131 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001132 mInfo.frameLeft = 0;
1133 mInfo.frameTop = 0;
1134 mInfo.frameRight = WIDTH;
1135 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001136 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001137 mInfo.globalScaleFactor = 1.0;
1138 mInfo.touchableRegion.clear();
1139 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001140 mInfo.ownerPid = WINDOW_PID;
1141 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001142 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001143 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001144 }
1145
Arthur Hungabbb9d82021-09-01 14:52:30 +00001146 sp<FakeWindowHandle> clone(
1147 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001148 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001149 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001150 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1151 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001152 return handle;
1153 }
1154
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001155 void setTouchable(bool touchable) {
1156 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1157 }
chaviwd1c23182019-12-20 18:44:56 -08001158
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001159 void setFocusable(bool focusable) {
1160 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1161 }
1162
1163 void setVisible(bool visible) {
1164 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1165 }
Vishnu Nair958da932020-08-21 17:12:37 -07001166
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001167 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001168 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001169 }
1170
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001171 void setPaused(bool paused) {
1172 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1173 }
1174
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001175 void setPreventSplitting(bool preventSplitting) {
1176 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001177 }
1178
1179 void setSlippery(bool slippery) {
1180 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1181 }
1182
1183 void setWatchOutsideTouch(bool watchOutside) {
1184 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1185 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001186
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001187 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1188
1189 void setInterceptsStylus(bool interceptsStylus) {
1190 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1191 }
1192
1193 void setDropInput(bool dropInput) {
1194 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1195 }
1196
1197 void setDropInputIfObscured(bool dropInputIfObscured) {
1198 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1199 }
1200
1201 void setNoInputChannel(bool noInputChannel) {
1202 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1203 }
1204
Josep del Riob3981622023-04-18 15:49:45 +00001205 void setDisableUserActivity(bool disableUserActivity) {
1206 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1207 }
1208
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001209 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1210
chaviw3277faf2021-05-19 16:45:23 -05001211 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001212
Bernardo Rufino7393d172021-02-26 13:56:11 +00001213 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1214
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001215 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001216 mInfo.frameLeft = frame.left;
1217 mInfo.frameTop = frame.top;
1218 mInfo.frameRight = frame.right;
1219 mInfo.frameBottom = frame.bottom;
1220 mInfo.touchableRegion.clear();
1221 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001222
1223 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1224 ui::Transform translate;
1225 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1226 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001227 }
1228
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001229 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1230
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001231 void setIsWallpaper(bool isWallpaper) {
1232 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1233 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001234
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001235 void setDupTouchToWallpaper(bool hasWallpaper) {
1236 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1237 }
chaviwd1c23182019-12-20 18:44:56 -08001238
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001239 void setTrustedOverlay(bool trustedOverlay) {
1240 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1241 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001242
chaviw9eaa22c2020-07-01 16:21:27 -07001243 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1244 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1245 }
1246
1247 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001248
yunho.shinf4a80b82020-11-16 21:13:57 +09001249 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1250
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001251 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001252 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001253 }
1254
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001255 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001256 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001257 }
1258
Svet Ganov5d3bc372020-01-26 23:11:07 -08001259 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001260 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001261 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1262 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001263 }
1264
1265 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001266 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001267 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1268 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001269 }
1270
1271 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001272 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001273 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1274 }
1275
1276 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1277 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001278 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001279 expectedFlags);
1280 }
1281
Svet Ganov5d3bc372020-01-26 23:11:07 -08001282 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001283 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1284 int32_t expectedFlags = 0) {
1285 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1286 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001287 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001288 }
1289
1290 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001291 int32_t expectedFlags = 0) {
1292 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1293 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001294 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001295 }
1296
1297 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001298 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001299 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001300 expectedFlags);
1301 }
1302
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001303 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1304 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001305 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001306 expectedFlags);
1307 }
1308
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001309 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1310 int32_t expectedFlags = 0) {
1311 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001312 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001313 ASSERT_EQ(InputEventType::MOTION, event->getType());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001314 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1315 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1316 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1317 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1318 }
1319
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001320 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1321 ASSERT_NE(mInputReceiver, nullptr)
1322 << "Cannot consume events from a window with no receiver";
1323 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1324 }
1325
Prabir Pradhan99987712020-11-10 18:43:05 -08001326 void consumeCaptureEvent(bool hasCapture) {
1327 ASSERT_NE(mInputReceiver, nullptr)
1328 << "Cannot consume events from a window with no receiver";
1329 mInputReceiver->consumeCaptureEvent(hasCapture);
1330 }
1331
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001332 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1333 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001334 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001335 ASSERT_THAT(*motionEvent, matcher);
1336 }
1337
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001338 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001339 std::optional<int32_t> expectedDisplayId,
1340 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001341 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1342 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1343 expectedFlags);
1344 }
1345
arthurhungb89ccb02020-12-30 16:19:01 +08001346 void consumeDragEvent(bool isExiting, float x, float y) {
1347 mInputReceiver->consumeDragEvent(isExiting, x, y);
1348 }
1349
Antonio Kantekf16f2832021-09-28 04:39:20 +00001350 void consumeTouchModeEvent(bool inTouchMode) {
1351 ASSERT_NE(mInputReceiver, nullptr)
1352 << "Cannot consume events from a window with no receiver";
1353 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1354 }
1355
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001356 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001357 if (mInputReceiver == nullptr) {
1358 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1359 return std::nullopt;
1360 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001361 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001362 }
1363
1364 void finishEvent(uint32_t sequenceNum) {
1365 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1366 mInputReceiver->finishEvent(sequenceNum);
1367 }
1368
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001369 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1370 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1371 mInputReceiver->sendTimeline(inputEventId, timeline);
1372 }
1373
chaviwaf87b3e2019-10-01 16:59:28 -07001374 InputEvent* consume() {
1375 if (mInputReceiver == nullptr) {
1376 return nullptr;
1377 }
1378 return mInputReceiver->consume();
1379 }
1380
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001381 MotionEvent* consumeMotion() {
1382 InputEvent* event = consume();
1383 if (event == nullptr) {
1384 ADD_FAILURE() << "Consume failed : no event";
1385 return nullptr;
1386 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001387 if (event->getType() != InputEventType::MOTION) {
1388 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001389 return nullptr;
1390 }
1391 return static_cast<MotionEvent*>(event);
1392 }
1393
Arthur Hungb92218b2018-08-14 12:00:21 +08001394 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001395 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001396 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001397 return; // Can't receive events if the window does not have input channel
1398 }
1399 ASSERT_NE(nullptr, mInputReceiver)
1400 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001401 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001402 }
1403
chaviwaf87b3e2019-10-01 16:59:28 -07001404 sp<IBinder> getToken() { return mInfo.token; }
1405
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001406 const std::string& getName() { return mName; }
1407
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001408 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1409 mInfo.ownerPid = ownerPid;
1410 mInfo.ownerUid = ownerUid;
1411 }
1412
Prabir Pradhanedd96402022-02-15 01:46:16 -08001413 int32_t getPid() const { return mInfo.ownerPid; }
1414
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001415 void destroyReceiver() { mInputReceiver = nullptr; }
1416
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001417 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1418
chaviwd1c23182019-12-20 18:44:56 -08001419private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001420 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001421 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001422 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001423};
1424
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001425std::atomic<int32_t> FakeWindowHandle::sId{1};
1426
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001427static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001428 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001429 int32_t displayId = ADISPLAY_ID_NONE,
1430 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001431 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001432 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1433 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001434 KeyEvent event;
1435 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1436
1437 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001438 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001439 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1440 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001441
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001442 if (!allowKeyRepeat) {
1443 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1444 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001445 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001446 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001447}
1448
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001449static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001450 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001451 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001452}
1453
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001454// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1455// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1456// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001457static InputEventInjectionResult injectKeyDownNoRepeat(
1458 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001459 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001460 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001461 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001462}
1463
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001464static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001465 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001466 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001467}
1468
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001469static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001470 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001471 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001472 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1473 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1474 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1475 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001476}
1477
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001478static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001479 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001480 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001481 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001482 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1483 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001484 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001485 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1486 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001487 MotionEventBuilder motionBuilder =
1488 MotionEventBuilder(action, source)
1489 .displayId(displayId)
1490 .eventTime(eventTime)
1491 .rawXCursorPosition(cursorPosition.x)
1492 .rawYCursorPosition(cursorPosition.y)
1493 .pointer(
1494 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1495 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1496 motionBuilder.downTime(eventTime);
1497 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001498
1499 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001500 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1501 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001502}
1503
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001504static InputEventInjectionResult injectMotionDown(
1505 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1506 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001507 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001508}
1509
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001510static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001511 int32_t source, int32_t displayId,
1512 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001513 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001514}
1515
Jackal Guof9696682018-10-05 12:23:23 +08001516static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1517 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1518 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001519 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001520 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1521 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001522
1523 return args;
1524}
1525
Josep del Riob3981622023-04-18 15:49:45 +00001526static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1527 int32_t displayId = ADISPLAY_ID_NONE) {
1528 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1529 // Define a valid key event.
1530 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1531 displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON,
1532 currentTime);
1533
1534 return args;
1535}
1536
1537static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1538 int32_t displayId = ADISPLAY_ID_NONE) {
1539 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1540 // Define a valid key event.
1541 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1542 displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT,
1543 AMETA_NONE, currentTime);
1544
1545 return args;
1546}
1547
Prabir Pradhan678438e2023-04-13 19:32:51 +00001548[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1549 int32_t displayId,
1550 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001551 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001552 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1553 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1554 }
1555
chaviwd1c23182019-12-20 18:44:56 -08001556 PointerProperties pointerProperties[pointerCount];
1557 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001558
chaviwd1c23182019-12-20 18:44:56 -08001559 for (size_t i = 0; i < pointerCount; i++) {
1560 pointerProperties[i].clear();
1561 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001562 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001563
chaviwd1c23182019-12-20 18:44:56 -08001564 pointerCoords[i].clear();
1565 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1566 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1567 }
Jackal Guof9696682018-10-05 12:23:23 +08001568
1569 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1570 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001571 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001572 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1573 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001574 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1575 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001576 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1577 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001578
1579 return args;
1580}
1581
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001582static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1583 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1584}
1585
chaviwd1c23182019-12-20 18:44:56 -08001586static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1587 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1588}
1589
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001590static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1591 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001592 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001593}
1594
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001595/**
1596 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1597 * broken channel.
1598 */
1599TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1600 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1601 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001602 sp<FakeWindowHandle>::make(application, mDispatcher,
1603 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001604
1605 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1606
1607 // Window closes its channel, but the window remains.
1608 window->destroyReceiver();
1609 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1610}
1611
Arthur Hungb92218b2018-08-14 12:00:21 +08001612TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001613 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001614 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1615 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001616
Arthur Hung72d8dc32020-03-28 00:48:39 +00001617 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001618 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1619 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1620 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001621
1622 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001623 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001624}
1625
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001626TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1627 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001628 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1629 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001630
1631 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1632 // Inject a MotionEvent to an unknown display.
1633 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1634 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1635 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1636
1637 // Window should receive motion event.
1638 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1639}
1640
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001641/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001642 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001643 * This test serves as a sanity check for the next test, where setInputWindows is
1644 * called twice.
1645 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001646TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001647 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001648 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1649 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001650 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001651
1652 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001653 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001654 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1655 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001656 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001657
1658 // Window should receive motion event.
1659 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1660}
1661
1662/**
1663 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001664 */
1665TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001666 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001667 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1668 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001669 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001670
1671 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1672 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001673 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001674 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1675 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001676 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001677
1678 // Window should receive motion event.
1679 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1680}
1681
Arthur Hungb92218b2018-08-14 12:00:21 +08001682// The foreground window should receive the first touch down event.
1683TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001684 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001685 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001686 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001687 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001688 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001689
Arthur Hung72d8dc32020-03-28 00:48:39 +00001690 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001691 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1692 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1693 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001694
1695 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001696 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001697 windowSecond->assertNoEvents();
1698}
1699
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001700/**
1701 * Two windows: A top window, and a wallpaper behind the window.
1702 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1703 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001704 * 1. foregroundWindow <-- dup touch to wallpaper
1705 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001706 */
1707TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1708 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1709 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001710 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001711 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001712 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001713 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001714 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001715
1716 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1717 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1718 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1719 {100, 200}))
1720 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1721
1722 // Both foreground window and its wallpaper should receive the touch down
1723 foregroundWindow->consumeMotionDown();
1724 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1725
1726 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1727 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1728 ADISPLAY_ID_DEFAULT, {110, 200}))
1729 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1730
1731 foregroundWindow->consumeMotionMove();
1732 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1733
1734 // Now the foreground window goes away, but the wallpaper stays
1735 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1736 foregroundWindow->consumeMotionCancel();
1737 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1738 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1739}
1740
1741/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001742 * Two fingers down on the window, and lift off the first finger.
1743 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1744 * contains a single pointer.
1745 */
1746TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1747 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1748 sp<FakeWindowHandle> window =
1749 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1750
1751 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001752 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001753 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1754 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1755 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001756 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001757 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1758 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1759 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1760 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001761 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001762 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1763 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1764 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1765 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001766 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1767 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1768 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1769
1770 // Remove the window. The gesture should be canceled
1771 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
1772 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1773 window->consumeMotionEvent(
1774 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1775}
1776
1777/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001778 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1779 * with the following differences:
1780 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1781 * clean up the connection.
1782 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1783 * Ensure that there's no crash in the dispatcher.
1784 */
1785TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1786 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1787 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001788 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001789 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001790 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001791 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001792 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001793
1794 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1795 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1796 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1797 {100, 200}))
1798 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1799
1800 // Both foreground window and its wallpaper should receive the touch down
1801 foregroundWindow->consumeMotionDown();
1802 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1803
1804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1805 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1806 ADISPLAY_ID_DEFAULT, {110, 200}))
1807 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1808
1809 foregroundWindow->consumeMotionMove();
1810 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1811
1812 // Wallpaper closes its channel, but the window remains.
1813 wallpaperWindow->destroyReceiver();
1814 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1815
1816 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1817 // is no longer valid.
1818 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1819 foregroundWindow->consumeMotionCancel();
1820}
1821
Arthur Hungc539dbb2022-12-08 07:45:36 +00001822class ShouldSplitTouchFixture : public InputDispatcherTest,
1823 public ::testing::WithParamInterface<bool> {};
1824INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1825 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001826/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001827 * A single window that receives touch (on top), and a wallpaper window underneath it.
1828 * The top window gets a multitouch gesture.
1829 * Ensure that wallpaper gets the same gesture.
1830 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00001831TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001832 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001833 sp<FakeWindowHandle> foregroundWindow =
1834 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1835 foregroundWindow->setDupTouchToWallpaper(true);
1836 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001837
1838 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001839 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001840 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001841
Arthur Hungc539dbb2022-12-08 07:45:36 +00001842 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001843
1844 // Touch down on top window
1845 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1846 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1847 {100, 100}))
1848 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1849
1850 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00001851 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001852 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1853
1854 // Second finger down on the top window
1855 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001856 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001857 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001858 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1859 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001860 .build();
1861 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1862 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1863 InputEventInjectionSync::WAIT_FOR_RESULT))
1864 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1865
Harry Cutts33476232023-01-30 19:57:29 +00001866 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
1867 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001868 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001869
1870 const MotionEvent secondFingerUpEvent =
1871 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1872 .displayId(ADISPLAY_ID_DEFAULT)
1873 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001874 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1875 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001876 .build();
1877 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1878 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
1879 InputEventInjectionSync::WAIT_FOR_RESULT))
1880 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1881 foregroundWindow->consumeMotionPointerUp(0);
1882 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1883
1884 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001885 injectMotionEvent(mDispatcher,
1886 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
1887 AINPUT_SOURCE_TOUCHSCREEN)
1888 .displayId(ADISPLAY_ID_DEFAULT)
1889 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
1890 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001891 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001892 .x(100)
1893 .y(100))
1894 .build(),
1895 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001896 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1897 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1898 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001899}
1900
1901/**
1902 * Two windows: a window on the left and window on the right.
1903 * A third window, wallpaper, is behind both windows, and spans both top windows.
1904 * The first touch down goes to the left window. A second pointer touches down on the right window.
1905 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1906 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1907 * ACTION_POINTER_DOWN(1).
1908 */
1909TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1910 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1911 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001912 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001913 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001914 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001915
1916 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001917 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001918 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001919 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001920
1921 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001922 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001923 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001924 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001925
1926 mDispatcher->setInputWindows(
1927 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
1928
1929 // Touch down on left window
1930 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1931 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1932 {100, 100}))
1933 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1934
1935 // Both foreground window and its wallpaper should receive the touch down
1936 leftWindow->consumeMotionDown();
1937 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1938
1939 // Second finger down on the right window
1940 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001941 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001942 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001943 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1944 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001945 .build();
1946 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1947 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
1948 InputEventInjectionSync::WAIT_FOR_RESULT))
1949 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1950
1951 leftWindow->consumeMotionMove();
1952 // Since the touch is split, right window gets ACTION_DOWN
1953 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00001954 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001955 expectedWallpaperFlags);
1956
1957 // Now, leftWindow, which received the first finger, disappears.
1958 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
1959 leftWindow->consumeMotionCancel();
1960 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1961 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1962
1963 // The pointer that's still down on the right window moves, and goes to the right window only.
1964 // As far as the dispatcher's concerned though, both pointers are still present.
1965 const MotionEvent secondFingerMoveEvent =
1966 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1967 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001968 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1969 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001970 .build();
1971 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1972 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
1973 InputEventInjectionSync::WAIT_FOR_RESULT));
1974 rightWindow->consumeMotionMove();
1975
1976 leftWindow->assertNoEvents();
1977 rightWindow->assertNoEvents();
1978 wallpaperWindow->assertNoEvents();
1979}
1980
Arthur Hungc539dbb2022-12-08 07:45:36 +00001981/**
1982 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
1983 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
1984 * The right window should receive ACTION_DOWN.
1985 */
1986TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00001987 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001988 sp<FakeWindowHandle> leftWindow =
1989 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
1990 leftWindow->setFrame(Rect(0, 0, 200, 200));
1991 leftWindow->setDupTouchToWallpaper(true);
1992 leftWindow->setSlippery(true);
1993
1994 sp<FakeWindowHandle> rightWindow =
1995 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
1996 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00001997
1998 sp<FakeWindowHandle> wallpaperWindow =
1999 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2000 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002001
Arthur Hungc539dbb2022-12-08 07:45:36 +00002002 mDispatcher->setInputWindows(
2003 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002004
Arthur Hungc539dbb2022-12-08 07:45:36 +00002005 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002006 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2007 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002008 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002009 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002010
2011 // Both foreground window and its wallpaper should receive the touch down
2012 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002013 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2014
Arthur Hungc539dbb2022-12-08 07:45:36 +00002015 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002016 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002017 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2018 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002019 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2020
Arthur Hungc539dbb2022-12-08 07:45:36 +00002021 leftWindow->consumeMotionCancel();
2022 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2023 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002024}
2025
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002026/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002027 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2028 * interactive, it might stop sending this flag.
2029 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2030 * to have a consistent input stream.
2031 *
2032 * Test procedure:
2033 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2034 * DOWN (new gesture).
2035 *
2036 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2037 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2038 *
2039 * We technically just need a single window here, but we are using two windows (spy on top and a
2040 * regular window below) to emulate the actual situation where it happens on the device.
2041 */
2042TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2043 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2044 sp<FakeWindowHandle> spyWindow =
2045 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2046 spyWindow->setFrame(Rect(0, 0, 200, 200));
2047 spyWindow->setTrustedOverlay(true);
2048 spyWindow->setSpy(true);
2049
2050 sp<FakeWindowHandle> window =
2051 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2052 window->setFrame(Rect(0, 0, 200, 200));
2053
2054 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2055 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002056
2057 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002058 mDispatcher->notifyMotion(
2059 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2060 .deviceId(touchDeviceId)
2061 .policyFlags(DEFAULT_POLICY_FLAGS)
2062 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2063 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002064
Prabir Pradhan678438e2023-04-13 19:32:51 +00002065 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2066 .deviceId(touchDeviceId)
2067 .policyFlags(DEFAULT_POLICY_FLAGS)
2068 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2069 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2070 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002071 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2072 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2073 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2074 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2075
2076 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002077 mDispatcher->notifyMotion(
2078 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2079 .deviceId(touchDeviceId)
2080 .policyFlags(0)
2081 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2082 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2083 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002084 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2085 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2086
2087 // We don't need to reset the device to reproduce the issue, but the reset event typically
2088 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002089 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002090
2091 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002092 mDispatcher->notifyMotion(
2093 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2094 .deviceId(touchDeviceId)
2095 .policyFlags(DEFAULT_POLICY_FLAGS)
2096 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2097 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002098 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2099 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2100
2101 // No more events
2102 spyWindow->assertNoEvents();
2103 window->assertNoEvents();
2104}
2105
2106/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002107 * Two windows: a window on the left and a window on the right.
2108 * Mouse is hovered from the right window into the left window.
2109 * Next, we tap on the left window, where the cursor was last seen.
2110 * The second tap is done onto the right window.
2111 * The mouse and tap are from two different devices.
2112 * We technically don't need to set the downtime / eventtime for these events, but setting these
2113 * explicitly helps during debugging.
2114 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2115 * In the buggy implementation, a tap on the right window would cause a crash.
2116 */
2117TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2118 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2119 sp<FakeWindowHandle> leftWindow =
2120 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2121 leftWindow->setFrame(Rect(0, 0, 200, 200));
2122
2123 sp<FakeWindowHandle> rightWindow =
2124 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2125 rightWindow->setFrame(Rect(200, 0, 400, 200));
2126
2127 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2128 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2129 // stale.
2130 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2131 const int32_t mouseDeviceId = 6;
2132 const int32_t touchDeviceId = 4;
2133 // Move the cursor from right
2134 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2135 injectMotionEvent(mDispatcher,
2136 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2137 AINPUT_SOURCE_MOUSE)
2138 .deviceId(mouseDeviceId)
2139 .downTime(baseTime + 10)
2140 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002141 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002142 .x(300)
2143 .y(100))
2144 .build()));
2145 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2146
2147 // .. to the left window
2148 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2149 injectMotionEvent(mDispatcher,
2150 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2151 AINPUT_SOURCE_MOUSE)
2152 .deviceId(mouseDeviceId)
2153 .downTime(baseTime + 10)
2154 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002155 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002156 .x(110)
2157 .y(100))
2158 .build()));
2159 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2160 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2161 // Now tap the left window
2162 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2163 injectMotionEvent(mDispatcher,
2164 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2165 AINPUT_SOURCE_TOUCHSCREEN)
2166 .deviceId(touchDeviceId)
2167 .downTime(baseTime + 40)
2168 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002169 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002170 .x(100)
2171 .y(100))
2172 .build()));
2173 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2174 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2175
2176 // release tap
2177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2178 injectMotionEvent(mDispatcher,
2179 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2180 AINPUT_SOURCE_TOUCHSCREEN)
2181 .deviceId(touchDeviceId)
2182 .downTime(baseTime + 40)
2183 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002184 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002185 .x(100)
2186 .y(100))
2187 .build()));
2188 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2189
2190 // Tap the window on the right
2191 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2192 injectMotionEvent(mDispatcher,
2193 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2194 AINPUT_SOURCE_TOUCHSCREEN)
2195 .deviceId(touchDeviceId)
2196 .downTime(baseTime + 60)
2197 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002198 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002199 .x(300)
2200 .y(100))
2201 .build()));
2202 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2203
2204 // release tap
2205 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2206 injectMotionEvent(mDispatcher,
2207 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2208 AINPUT_SOURCE_TOUCHSCREEN)
2209 .deviceId(touchDeviceId)
2210 .downTime(baseTime + 60)
2211 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002212 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002213 .x(300)
2214 .y(100))
2215 .build()));
2216 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2217
2218 // No more events
2219 leftWindow->assertNoEvents();
2220 rightWindow->assertNoEvents();
2221}
2222
2223/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002224 * Start hovering in a window. While this hover is still active, make another window appear on top.
2225 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2226 * While the top window is present, the hovering is stopped.
2227 * Later, hovering gets resumed again.
2228 * Ensure that new hover gesture is handled correctly.
2229 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2230 * to the window that's currently being hovered over.
2231 */
2232TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2233 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2234 sp<FakeWindowHandle> window =
2235 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2236 window->setFrame(Rect(0, 0, 200, 200));
2237
2238 // Only a single window is present at first
2239 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2240
2241 // Start hovering in the window
2242 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2243 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2244 .build());
2245 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2246
2247 // Now, an obscuring window appears!
2248 sp<FakeWindowHandle> obscuringWindow =
2249 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2250 ADISPLAY_ID_DEFAULT,
2251 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2252 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2253 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2254 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2255 obscuringWindow->setNoInputChannel(true);
2256 obscuringWindow->setFocusable(false);
2257 obscuringWindow->setAlpha(1.0);
2258 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2259
2260 // While this new obscuring window is present, the hovering is stopped
2261 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2262 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2263 .build());
2264 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2265
2266 // Now the obscuring window goes away.
2267 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2268
2269 // And a new hover gesture starts.
2270 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2271 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2272 .build());
2273 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2274}
2275
2276/**
2277 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2278 * the obscuring window.
2279 */
2280TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2281 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2282 sp<FakeWindowHandle> window =
2283 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2284 window->setFrame(Rect(0, 0, 200, 200));
2285
2286 // Only a single window is present at first
2287 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2288
2289 // Start hovering in the window
2290 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2291 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2292 .build());
2293 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2294
2295 // Now, an obscuring window appears!
2296 sp<FakeWindowHandle> obscuringWindow =
2297 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2298 ADISPLAY_ID_DEFAULT,
2299 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2300 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2301 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2302 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2303 obscuringWindow->setNoInputChannel(true);
2304 obscuringWindow->setFocusable(false);
2305 obscuringWindow->setAlpha(1.0);
2306 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2307
2308 // While this new obscuring window is present, the hovering continues. The event can't go to the
2309 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2310 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2311 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2312 .build());
2313 obscuringWindow->assertNoEvents();
2314 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2315
2316 // Now the obscuring window goes away.
2317 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2318
2319 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2320 // so it should generate a HOVER_ENTER
2321 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2322 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2323 .build());
2324 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2325
2326 // Now the MOVE should be getting dispatched normally
2327 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2328 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2329 .build());
2330 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2331}
2332
2333/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002334 * Two windows: a window on the left and a window on the right.
2335 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2336 * down. Then, on the left window, also place second touch pointer down.
2337 * This test tries to reproduce a crash.
2338 * In the buggy implementation, second pointer down on the left window would cause a crash.
2339 */
2340TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2341 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2342 sp<FakeWindowHandle> leftWindow =
2343 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2344 leftWindow->setFrame(Rect(0, 0, 200, 200));
2345
2346 sp<FakeWindowHandle> rightWindow =
2347 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2348 rightWindow->setFrame(Rect(200, 0, 400, 200));
2349
2350 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2351
2352 const int32_t touchDeviceId = 4;
2353 const int32_t mouseDeviceId = 6;
2354 NotifyMotionArgs args;
2355
2356 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002357 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2358 .deviceId(mouseDeviceId)
2359 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2360 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002361 leftWindow->consumeMotionEvent(
2362 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2363
2364 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002365 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2366 .deviceId(mouseDeviceId)
2367 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2368 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2369 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002370
2371 leftWindow->consumeMotionEvent(
2372 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2373 leftWindow->consumeMotionEvent(
2374 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2375
Prabir Pradhan678438e2023-04-13 19:32:51 +00002376 mDispatcher->notifyMotion(
2377 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2378 .deviceId(mouseDeviceId)
2379 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2380 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2381 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2382 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002383 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2384
2385 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002386 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2387 .deviceId(touchDeviceId)
2388 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2389 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002390 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2391
2392 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2393
2394 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002395 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2396 .deviceId(touchDeviceId)
2397 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2398 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2399 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002400 leftWindow->consumeMotionEvent(
2401 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2402 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2403 // current implementation.
2404 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2405 rightWindow->consumeMotionEvent(
2406 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2407
2408 leftWindow->assertNoEvents();
2409 rightWindow->assertNoEvents();
2410}
2411
2412/**
2413 * On a single window, use two different devices: mouse and touch.
2414 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2415 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2416 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2417 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2418 * represent a new gesture.
2419 */
2420TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2421 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2422 sp<FakeWindowHandle> window =
2423 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2424 window->setFrame(Rect(0, 0, 400, 400));
2425
2426 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2427
2428 const int32_t touchDeviceId = 4;
2429 const int32_t mouseDeviceId = 6;
2430 NotifyMotionArgs args;
2431
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002432 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002433 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2434 .deviceId(touchDeviceId)
2435 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2436 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002437 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002438 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2439 .deviceId(touchDeviceId)
2440 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2441 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2442 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002443 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002444 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2445 .deviceId(touchDeviceId)
2446 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2447 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2448 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002449 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2450 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2451 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2452
2453 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002454 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2455 .deviceId(mouseDeviceId)
2456 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2457 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2458 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002459
2460 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002461 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002462 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2463
Prabir Pradhan678438e2023-04-13 19:32:51 +00002464 mDispatcher->notifyMotion(
2465 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2466 .deviceId(mouseDeviceId)
2467 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2468 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2469 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2470 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002471 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2472
2473 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002474 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2475 .deviceId(touchDeviceId)
2476 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2477 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2478 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002479 // The pointer_down event should be ignored
2480 window->assertNoEvents();
2481}
2482
2483/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002484 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2485 * the injected event.
2486 */
2487TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2488 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2489 sp<FakeWindowHandle> window =
2490 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2491 window->setFrame(Rect(0, 0, 400, 400));
2492
2493 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2494
2495 const int32_t touchDeviceId = 4;
2496 NotifyMotionArgs args;
2497 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2498 // completion.
2499 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2500 injectMotionEvent(mDispatcher,
2501 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2502 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002503 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002504 .x(50)
2505 .y(50))
2506 .build()));
2507 window->consumeMotionEvent(
2508 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2509
2510 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2511 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002512 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2513 .deviceId(touchDeviceId)
2514 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2515 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002516
2517 window->consumeMotionEvent(
2518 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2519 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2520}
2521
2522/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002523 * This test is similar to the test above, but the sequence of injected events is different.
2524 *
2525 * Two windows: a window on the left and a window on the right.
2526 * Mouse is hovered over the left window.
2527 * Next, we tap on the left window, where the cursor was last seen.
2528 *
2529 * After that, we inject one finger down onto the right window, and then a second finger down onto
2530 * the left window.
2531 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2532 * window (first), and then another on the left window (second).
2533 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2534 * In the buggy implementation, second finger down on the left window would cause a crash.
2535 */
2536TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2537 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2538 sp<FakeWindowHandle> leftWindow =
2539 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2540 leftWindow->setFrame(Rect(0, 0, 200, 200));
2541
2542 sp<FakeWindowHandle> rightWindow =
2543 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2544 rightWindow->setFrame(Rect(200, 0, 400, 200));
2545
2546 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2547
2548 const int32_t mouseDeviceId = 6;
2549 const int32_t touchDeviceId = 4;
2550 // Hover over the left window. Keep the cursor there.
2551 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2552 injectMotionEvent(mDispatcher,
2553 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2554 AINPUT_SOURCE_MOUSE)
2555 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002556 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002557 .x(50)
2558 .y(50))
2559 .build()));
2560 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2561
2562 // Tap on left window
2563 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2564 injectMotionEvent(mDispatcher,
2565 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2566 AINPUT_SOURCE_TOUCHSCREEN)
2567 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002568 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002569 .x(100)
2570 .y(100))
2571 .build()));
2572
2573 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2574 injectMotionEvent(mDispatcher,
2575 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2576 AINPUT_SOURCE_TOUCHSCREEN)
2577 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002578 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002579 .x(100)
2580 .y(100))
2581 .build()));
2582 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2583 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2584 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2585
2586 // First finger down on right window
2587 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2588 injectMotionEvent(mDispatcher,
2589 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2590 AINPUT_SOURCE_TOUCHSCREEN)
2591 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002592 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002593 .x(300)
2594 .y(100))
2595 .build()));
2596 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2597
2598 // Second finger down on the left window
2599 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2600 injectMotionEvent(mDispatcher,
2601 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2602 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002603 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002604 .x(300)
2605 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002606 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002607 .x(100)
2608 .y(100))
2609 .build()));
2610 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2611 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2612
2613 // No more events
2614 leftWindow->assertNoEvents();
2615 rightWindow->assertNoEvents();
2616}
2617
2618/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002619 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2620 * While the touch is down, new hover events from the stylus device should be ignored. After the
2621 * touch is gone, stylus hovering should start working again.
2622 */
2623TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2624 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2625 sp<FakeWindowHandle> window =
2626 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2627 window->setFrame(Rect(0, 0, 200, 200));
2628
2629 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2630
2631 const int32_t stylusDeviceId = 5;
2632 const int32_t touchDeviceId = 4;
2633 // Start hovering with stylus
2634 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2635 injectMotionEvent(mDispatcher,
2636 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2637 AINPUT_SOURCE_STYLUS)
2638 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002639 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002640 .x(50)
2641 .y(50))
2642 .build()));
2643 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2644
2645 // Finger down on the window
2646 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2647 injectMotionEvent(mDispatcher,
2648 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2649 AINPUT_SOURCE_TOUCHSCREEN)
2650 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002651 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002652 .x(100)
2653 .y(100))
2654 .build()));
2655 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2656 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2657
2658 // Try to continue hovering with stylus. Since we are already down, injection should fail
2659 ASSERT_EQ(InputEventInjectionResult::FAILED,
2660 injectMotionEvent(mDispatcher,
2661 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2662 AINPUT_SOURCE_STYLUS)
2663 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002664 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002665 .x(50)
2666 .y(50))
2667 .build()));
2668 // No event should be sent. This event should be ignored because a pointer from another device
2669 // is already down.
2670
2671 // Lift up the finger
2672 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2673 injectMotionEvent(mDispatcher,
2674 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2675 AINPUT_SOURCE_TOUCHSCREEN)
2676 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002677 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002678 .x(100)
2679 .y(100))
2680 .build()));
2681 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2682
2683 // Now that the touch is gone, stylus hovering should start working again
2684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2685 injectMotionEvent(mDispatcher,
2686 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2687 AINPUT_SOURCE_STYLUS)
2688 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002689 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002690 .x(50)
2691 .y(50))
2692 .build()));
2693 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2694 // No more events
2695 window->assertNoEvents();
2696}
2697
2698/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002699 * A spy window above a window with no input channel.
2700 * Start hovering with a stylus device, and then tap with it.
2701 * Ensure spy window receives the entire sequence.
2702 */
2703TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2704 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2705 sp<FakeWindowHandle> spyWindow =
2706 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2707 spyWindow->setFrame(Rect(0, 0, 200, 200));
2708 spyWindow->setTrustedOverlay(true);
2709 spyWindow->setSpy(true);
2710 sp<FakeWindowHandle> window =
2711 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2712 window->setNoInputChannel(true);
2713 window->setFrame(Rect(0, 0, 200, 200));
2714
2715 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2716
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002717 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002718 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2719 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2720 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002721 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2722 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002723 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2724 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2725 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002726 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2727
2728 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002729 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2730 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2731 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002732 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2733
2734 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002735 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
2736 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2737 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002738 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2739
2740 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002741 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2742 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2743 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002744 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2745 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002746 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2747 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2748 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002749 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2750
2751 // No more events
2752 spyWindow->assertNoEvents();
2753 window->assertNoEvents();
2754}
2755
2756/**
2757 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
2758 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
2759 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
2760 * While the mouse is down, new move events from the touch device should be ignored.
2761 */
2762TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
2763 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2764 sp<FakeWindowHandle> spyWindow =
2765 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2766 spyWindow->setFrame(Rect(0, 0, 200, 200));
2767 spyWindow->setTrustedOverlay(true);
2768 spyWindow->setSpy(true);
2769 sp<FakeWindowHandle> window =
2770 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2771 window->setFrame(Rect(0, 0, 200, 200));
2772
2773 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2774
2775 const int32_t mouseDeviceId = 7;
2776 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002777
2778 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00002779 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2780 .deviceId(mouseDeviceId)
2781 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2782 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002783 spyWindow->consumeMotionEvent(
2784 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2785 window->consumeMotionEvent(
2786 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2787
2788 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00002789 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2790 .deviceId(touchDeviceId)
2791 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
2792 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002793 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2794 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2795 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2796 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2797
Prabir Pradhan678438e2023-04-13 19:32:51 +00002798 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2799 .deviceId(touchDeviceId)
2800 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
2801 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002802 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2803 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2804
2805 // Pilfer the stream
2806 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2807 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2808
Prabir Pradhan678438e2023-04-13 19:32:51 +00002809 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2810 .deviceId(touchDeviceId)
2811 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
2812 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002813 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2814
2815 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002816 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2817 .deviceId(mouseDeviceId)
2818 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2819 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2820 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002821
2822 spyWindow->consumeMotionEvent(
2823 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2824 spyWindow->consumeMotionEvent(
2825 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2826 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2827
Prabir Pradhan678438e2023-04-13 19:32:51 +00002828 mDispatcher->notifyMotion(
2829 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2830 .deviceId(mouseDeviceId)
2831 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2832 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2833 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2834 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002835 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2836 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2837
2838 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002839 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
2840 .deviceId(mouseDeviceId)
2841 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2842 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
2843 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002844 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2845 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2846
2847 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002848 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2849 .deviceId(touchDeviceId)
2850 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
2851 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002852
2853 // No more events
2854 spyWindow->assertNoEvents();
2855 window->assertNoEvents();
2856}
2857
2858/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002859 * On the display, have a single window, and also an area where there's no window.
2860 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2861 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2862 */
2863TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2864 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2865 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002866 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002867
2868 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002869
2870 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00002871 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002872
2873 mDispatcher->waitForIdle();
2874 window->assertNoEvents();
2875
2876 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002877 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002878 mDispatcher->waitForIdle();
2879 window->consumeMotionDown();
2880}
2881
2882/**
2883 * Same test as above, but instead of touching the empty space, the first touch goes to
2884 * non-touchable window.
2885 */
2886TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2887 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2888 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002889 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002890 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2891 window1->setTouchable(false);
2892 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002893 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002894 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2895
2896 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2897
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002898 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002899 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002900
2901 mDispatcher->waitForIdle();
2902 window1->assertNoEvents();
2903 window2->assertNoEvents();
2904
2905 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002906 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002907 mDispatcher->waitForIdle();
2908 window2->consumeMotionDown();
2909}
2910
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002911/**
2912 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2913 * to the event time of the first ACTION_DOWN sent to the particular window.
2914 */
2915TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2916 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2917 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002918 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002919 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2920 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002921 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002922 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2923
2924 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
2925
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002926 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002927 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002928
2929 mDispatcher->waitForIdle();
2930 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002931 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002932 window2->assertNoEvents();
2933 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
2934 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
2935 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
2936
2937 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002938 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002939 mDispatcher->waitForIdle();
2940 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002941 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002942 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
2943 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
2944 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
2945 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
2946
2947 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002948 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002949 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002950 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002951
2952 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002953 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002954 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002955 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002956
2957 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
2958 window1->consumeMotionMove();
2959 window1->assertNoEvents();
2960
2961 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002962 mDispatcher->notifyMotion(
2963 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002964 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002965 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002966
Prabir Pradhan678438e2023-04-13 19:32:51 +00002967 mDispatcher->notifyMotion(
2968 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002969 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002970 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002971}
2972
Garfield Tandf26e862020-07-01 20:18:19 -07002973TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002974 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002975 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002976 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002977 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002978 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002979 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002980 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002981
2982 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2983
2984 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
2985
2986 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002987 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002988 injectMotionEvent(mDispatcher,
2989 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2990 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002991 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07002992 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002993 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002994
2995 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002996 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07002997 injectMotionEvent(mDispatcher,
2998 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2999 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003000 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003001 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003002 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3003 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003004
3005 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003006 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003007 injectMotionEvent(mDispatcher,
3008 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3009 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003010 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003011 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003012 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3013 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003014
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003015 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003016 injectMotionEvent(mDispatcher,
3017 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3018 AINPUT_SOURCE_MOUSE)
3019 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3020 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003021 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003022 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003023 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003024
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003025 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003026 injectMotionEvent(mDispatcher,
3027 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3028 AINPUT_SOURCE_MOUSE)
3029 .buttonState(0)
3030 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003031 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003032 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003033 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003034
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003035 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003036 injectMotionEvent(mDispatcher,
3037 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3038 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003039 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003040 .build()));
3041 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3042
3043 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003044 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003045 injectMotionEvent(mDispatcher,
3046 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3047 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003048 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003049 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003050 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003051
3052 // No more events
3053 windowLeft->assertNoEvents();
3054 windowRight->assertNoEvents();
3055}
3056
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003057/**
3058 * Put two fingers down (and don't release them) and click the mouse button.
3059 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3060 * currently active gesture should be canceled, and the new one should proceed.
3061 */
3062TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3063 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3064 sp<FakeWindowHandle> window =
3065 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3066 window->setFrame(Rect(0, 0, 600, 800));
3067
3068 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3069
3070 const int32_t touchDeviceId = 4;
3071 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003072
3073 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003074 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3075 .deviceId(touchDeviceId)
3076 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3077 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003078
Prabir Pradhan678438e2023-04-13 19:32:51 +00003079 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3080 .deviceId(touchDeviceId)
3081 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3082 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3083 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003084 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3085 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3086
3087 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003088 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3089 .deviceId(mouseDeviceId)
3090 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3091 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3092 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003093 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3094 WithPointerCount(2u)));
3095 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3096
Prabir Pradhan678438e2023-04-13 19:32:51 +00003097 mDispatcher->notifyMotion(
3098 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3099 .deviceId(mouseDeviceId)
3100 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3101 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3102 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3103 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003104 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3105
3106 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3107 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003108 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3109 .deviceId(touchDeviceId)
3110 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3111 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3112 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003113 window->assertNoEvents();
3114}
3115
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003116TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3117 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3118
3119 sp<FakeWindowHandle> spyWindow =
3120 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3121 spyWindow->setFrame(Rect(0, 0, 600, 800));
3122 spyWindow->setTrustedOverlay(true);
3123 spyWindow->setSpy(true);
3124 sp<FakeWindowHandle> window =
3125 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3126 window->setFrame(Rect(0, 0, 600, 800));
3127
3128 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3129 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3130
3131 // Send mouse cursor to the window
3132 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3133 injectMotionEvent(mDispatcher,
3134 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3135 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003136 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003137 .x(100)
3138 .y(100))
3139 .build()));
3140
3141 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3142 WithSource(AINPUT_SOURCE_MOUSE)));
3143 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3144 WithSource(AINPUT_SOURCE_MOUSE)));
3145
3146 window->assertNoEvents();
3147 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003148}
3149
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003150TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3151 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3152
3153 sp<FakeWindowHandle> spyWindow =
3154 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3155 spyWindow->setFrame(Rect(0, 0, 600, 800));
3156 spyWindow->setTrustedOverlay(true);
3157 spyWindow->setSpy(true);
3158 sp<FakeWindowHandle> window =
3159 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3160 window->setFrame(Rect(0, 0, 600, 800));
3161
3162 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3163 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3164
3165 // Send mouse cursor to the window
3166 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3167 injectMotionEvent(mDispatcher,
3168 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3169 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003170 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003171 .x(100)
3172 .y(100))
3173 .build()));
3174
3175 // Move mouse cursor
3176 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3177 injectMotionEvent(mDispatcher,
3178 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3179 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003180 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003181 .x(110)
3182 .y(110))
3183 .build()));
3184
3185 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3186 WithSource(AINPUT_SOURCE_MOUSE)));
3187 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3188 WithSource(AINPUT_SOURCE_MOUSE)));
3189 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3190 WithSource(AINPUT_SOURCE_MOUSE)));
3191 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3192 WithSource(AINPUT_SOURCE_MOUSE)));
3193 // Touch down on the window
3194 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3195 injectMotionEvent(mDispatcher,
3196 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3197 AINPUT_SOURCE_TOUCHSCREEN)
3198 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003199 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003200 .x(200)
3201 .y(200))
3202 .build()));
3203 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3204 WithSource(AINPUT_SOURCE_MOUSE)));
3205 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3206 WithSource(AINPUT_SOURCE_MOUSE)));
3207 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3208 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3209 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3210 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3211
3212 // pilfer the motion, retaining the gesture on the spy window.
3213 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3214 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3215 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3216
3217 // Touch UP on the window
3218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3219 injectMotionEvent(mDispatcher,
3220 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3221 AINPUT_SOURCE_TOUCHSCREEN)
3222 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003223 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003224 .x(200)
3225 .y(200))
3226 .build()));
3227 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3228 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3229
3230 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3231 // to send a new gesture. It should again go to both windows (spy and the window below), just
3232 // like the first gesture did, before pilfering. The window configuration has not changed.
3233
3234 // One more tap - DOWN
3235 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3236 injectMotionEvent(mDispatcher,
3237 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3238 AINPUT_SOURCE_TOUCHSCREEN)
3239 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003240 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003241 .x(250)
3242 .y(250))
3243 .build()));
3244 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3245 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3246 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3247 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3248
3249 // Touch UP on the window
3250 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3251 injectMotionEvent(mDispatcher,
3252 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3253 AINPUT_SOURCE_TOUCHSCREEN)
3254 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003255 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003256 .x(250)
3257 .y(250))
3258 .build()));
3259 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3260 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3261 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3262 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3263
3264 window->assertNoEvents();
3265 spyWindow->assertNoEvents();
3266}
3267
Garfield Tandf26e862020-07-01 20:18:19 -07003268// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3269// directly in this test.
3270TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003271 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003272 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003273 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003274 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003275
3276 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3277
3278 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3279
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003280 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003281 injectMotionEvent(mDispatcher,
3282 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3283 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003284 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003285 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003286 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003287 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003288 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003289 injectMotionEvent(mDispatcher,
3290 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3291 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003292 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003293 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003294 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3295 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003296
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003297 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003298 injectMotionEvent(mDispatcher,
3299 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3300 AINPUT_SOURCE_MOUSE)
3301 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3302 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003303 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003304 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003305 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003306
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003307 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003308 injectMotionEvent(mDispatcher,
3309 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3310 AINPUT_SOURCE_MOUSE)
3311 .buttonState(0)
3312 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003313 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003314 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003315 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003316
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003317 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003318 injectMotionEvent(mDispatcher,
3319 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3320 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003321 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003322 .build()));
3323 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3324
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07003325 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
3326 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
3327 ASSERT_EQ(InputEventInjectionResult::FAILED,
Garfield Tandf26e862020-07-01 20:18:19 -07003328 injectMotionEvent(mDispatcher,
3329 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3330 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003331 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003332 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003333 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003334}
3335
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003336/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003337 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3338 * is generated.
3339 */
3340TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3341 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3342 sp<FakeWindowHandle> window =
3343 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3344 window->setFrame(Rect(0, 0, 1200, 800));
3345
3346 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3347
3348 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3349
3350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3351 injectMotionEvent(mDispatcher,
3352 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3353 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003354 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003355 .x(300)
3356 .y(400))
3357 .build()));
3358 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3359
3360 // Remove the window, but keep the channel.
3361 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3362 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3363}
3364
3365/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003366 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3367 */
3368TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3369 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3370 sp<FakeWindowHandle> window =
3371 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3372 window->setFrame(Rect(0, 0, 100, 100));
3373
3374 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3375
3376 const int32_t mouseDeviceId = 7;
3377 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003378
3379 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003380 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3381 .deviceId(mouseDeviceId)
3382 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3383 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003384 window->consumeMotionEvent(
3385 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3386
3387 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003388 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3389 .deviceId(touchDeviceId)
3390 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3391 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003392
3393 window->consumeMotionEvent(
3394 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3395 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3396}
3397
3398/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003399 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003400 * The tap causes a HOVER_EXIT event to be generated because the current event
3401 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003402 */
3403TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3404 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3405 sp<FakeWindowHandle> window =
3406 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3407 window->setFrame(Rect(0, 0, 100, 100));
3408
3409 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3410
3411 // Inject a hover_move from mouse.
3412 NotifyMotionArgs motionArgs =
3413 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3414 ADISPLAY_ID_DEFAULT, {{50, 50}});
3415 motionArgs.xCursorPosition = 50;
3416 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003417 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003418 ASSERT_NO_FATAL_FAILURE(
3419 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3420 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003421
3422 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003423 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3424 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3425 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003426 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003427 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3428 WithSource(AINPUT_SOURCE_MOUSE))));
3429
3430 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003431 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3432 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3433
Prabir Pradhan678438e2023-04-13 19:32:51 +00003434 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3435 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003436 ASSERT_NO_FATAL_FAILURE(
3437 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3438 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3439}
3440
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003441TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3442 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3443 sp<FakeWindowHandle> windowDefaultDisplay =
3444 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3445 ADISPLAY_ID_DEFAULT);
3446 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3447 sp<FakeWindowHandle> windowSecondDisplay =
3448 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3449 SECOND_DISPLAY_ID);
3450 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3451
3452 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3453 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3454
3455 // Set cursor position in window in default display and check that hover enter and move
3456 // events are generated.
3457 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3458 injectMotionEvent(mDispatcher,
3459 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3460 AINPUT_SOURCE_MOUSE)
3461 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003462 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003463 .x(300)
3464 .y(600))
3465 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003466 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003467
3468 // Remove all windows in secondary display and check that no event happens on window in
3469 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003470 mDispatcher->setInputWindows(
3471 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003472 windowDefaultDisplay->assertNoEvents();
3473
3474 // Move cursor position in window in default display and check that only hover move
3475 // event is generated and not hover enter event.
3476 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3477 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3478 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3479 injectMotionEvent(mDispatcher,
3480 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3481 AINPUT_SOURCE_MOUSE)
3482 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003483 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003484 .x(400)
3485 .y(700))
3486 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003487 windowDefaultDisplay->consumeMotionEvent(
3488 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3489 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003490 windowDefaultDisplay->assertNoEvents();
3491}
3492
Garfield Tan00f511d2019-06-12 16:55:40 -07003493TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003494 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003495
3496 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003497 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003498 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003499 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003500 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003501 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003502
3503 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3504
Arthur Hung72d8dc32020-03-28 00:48:39 +00003505 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003506
3507 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3508 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003509 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003510 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003511 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003512 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003513 windowRight->assertNoEvents();
3514}
3515
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003516TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003517 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003518 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3519 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003520 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003521
Arthur Hung72d8dc32020-03-28 00:48:39 +00003522 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003523 setFocusedWindow(window);
3524
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003525 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003526
Prabir Pradhan678438e2023-04-13 19:32:51 +00003527 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003528
3529 // Window should receive key down event.
3530 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3531
3532 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3533 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003534 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003535 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003536 AKEY_EVENT_FLAG_CANCELED);
3537}
3538
3539TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003540 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003541 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3542 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003543
Arthur Hung72d8dc32020-03-28 00:48:39 +00003544 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003545
Prabir Pradhan678438e2023-04-13 19:32:51 +00003546 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3547 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003548
3549 // Window should receive motion down event.
3550 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3551
3552 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3553 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003554 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003555 window->consumeMotionEvent(
3556 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003557}
3558
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003559TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
3560 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3561 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3562 "Fake Window", ADISPLAY_ID_DEFAULT);
3563
3564 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3565
3566 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3567 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
3568 .build());
3569
3570 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3571
3572 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
3573 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
3574 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3575
3576 // After the device has been reset, a new hovering stream can be sent to the window
3577 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3578 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
3579 .build());
3580 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3581}
3582
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003583TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3584 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003585 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3586 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003587 window->setFocusable(true);
3588
3589 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3590 setFocusedWindow(window);
3591
3592 window->consumeFocusEvent(true);
3593
Prabir Pradhan678438e2023-04-13 19:32:51 +00003594 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003595 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3596 const nsecs_t injectTime = keyArgs.eventTime;
3597 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003598 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003599 // The dispatching time should be always greater than or equal to intercept key timeout.
3600 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3601 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3602 std::chrono::nanoseconds(interceptKeyTimeout).count());
3603}
3604
3605TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3606 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003607 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3608 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003609 window->setFocusable(true);
3610
3611 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3612 setFocusedWindow(window);
3613
3614 window->consumeFocusEvent(true);
3615
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003616 mFakePolicy->setInterceptKeyTimeout(150ms);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003617 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
3618 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003619
3620 // Window should receive key event immediately when same key up.
3621 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3622 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3623}
3624
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003625/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003626 * Two windows. First is a regular window. Second does not overlap with the first, and has
3627 * WATCH_OUTSIDE_TOUCH.
3628 * Both windows are owned by the same UID.
3629 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3630 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3631 */
3632TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3633 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3634 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3635 "First Window", ADISPLAY_ID_DEFAULT);
3636 window->setFrame(Rect{0, 0, 100, 100});
3637
3638 sp<FakeWindowHandle> outsideWindow =
3639 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3640 ADISPLAY_ID_DEFAULT);
3641 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3642 outsideWindow->setWatchOutsideTouch(true);
3643 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3644 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3645
3646 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003647 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3648 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3649 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003650 window->consumeMotionDown();
3651 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3652 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3653 outsideWindow->consumeMotionEvent(
3654 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3655}
3656
3657/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003658 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3659 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3660 * ACTION_OUTSIDE event is sent per gesture.
3661 */
3662TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3663 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3664 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003665 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3666 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003667 window->setWatchOutsideTouch(true);
3668 window->setFrame(Rect{0, 0, 100, 100});
3669 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003670 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3671 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003672 secondWindow->setFrame(Rect{100, 100, 200, 200});
3673 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003674 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3675 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003676 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3677 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3678
3679 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003680 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3681 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3682 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003683 window->assertNoEvents();
3684 secondWindow->assertNoEvents();
3685
3686 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3687 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003688 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3689 ADISPLAY_ID_DEFAULT,
3690 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003691 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3692 window->consumeMotionEvent(
3693 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003694 secondWindow->consumeMotionDown();
3695 thirdWindow->assertNoEvents();
3696
3697 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3698 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003699 mDispatcher->notifyMotion(
3700 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3701 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003702 window->assertNoEvents();
3703 secondWindow->consumeMotionMove();
3704 thirdWindow->consumeMotionDown();
3705}
3706
Prabir Pradhan814fe082022-07-22 20:22:18 +00003707TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3708 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003709 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3710 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003711 window->setFocusable(true);
3712
Patrick Williamsd828f302023-04-28 17:52:08 -05003713 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003714 setFocusedWindow(window);
3715
3716 window->consumeFocusEvent(true);
3717
Prabir Pradhan678438e2023-04-13 19:32:51 +00003718 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3719 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3720 mDispatcher->notifyKey(keyDown);
3721 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003722
3723 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3724 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3725
3726 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003727 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003728
3729 window->consumeFocusEvent(false);
3730
Prabir Pradhan678438e2023-04-13 19:32:51 +00003731 mDispatcher->notifyKey(keyDown);
3732 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003733 window->assertNoEvents();
3734}
3735
Arthur Hung96483742022-11-15 03:30:48 +00003736TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3737 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3738 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3739 "Fake Window", ADISPLAY_ID_DEFAULT);
3740 // Ensure window is non-split and have some transform.
3741 window->setPreventSplitting(true);
3742 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05003743 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00003744
3745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3746 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3747 {50, 50}))
3748 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3749 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3750
3751 const MotionEvent secondFingerDownEvent =
3752 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3753 .displayId(ADISPLAY_ID_DEFAULT)
3754 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003755 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3756 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003757 .build();
3758 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3759 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
3760 InputEventInjectionSync::WAIT_FOR_RESULT))
3761 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3762
3763 const MotionEvent* event = window->consumeMotion();
3764 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3765 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3766 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3767 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3768 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3769}
3770
Harry Cuttsb166c002023-05-09 13:06:05 +00003771TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
3772 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3773 sp<FakeWindowHandle> window =
3774 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3775 window->setFrame(Rect(0, 0, 400, 400));
3776 sp<FakeWindowHandle> trustedOverlay =
3777 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
3778 ADISPLAY_ID_DEFAULT);
3779 trustedOverlay->setSpy(true);
3780 trustedOverlay->setTrustedOverlay(true);
3781
3782 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}});
3783
3784 // Start a three-finger touchpad swipe
3785 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3786 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3787 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3788 .build());
3789 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3790 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3791 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3792 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3793 .build());
3794 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3795 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3796 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3797 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3798 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3799 .build());
3800
3801 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3802 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3803 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
3804
3805 // Move the swipe a bit
3806 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3807 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3808 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3809 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3810 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3811 .build());
3812
3813 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3814
3815 // End the swipe
3816 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3817 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3818 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3819 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3820 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3821 .build());
3822 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3823 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3824 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3825 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3826 .build());
3827 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3828 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3829 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3830 .build());
3831
3832 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
3833 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
3834 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
3835
3836 window->assertNoEvents();
3837}
3838
3839TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
3840 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3841 sp<FakeWindowHandle> window =
3842 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3843 window->setFrame(Rect(0, 0, 400, 400));
3844 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3845
3846 // Start a three-finger touchpad swipe
3847 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3848 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3849 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3850 .build());
3851 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3852 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3853 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3854 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3855 .build());
3856 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3857 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3858 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3859 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3860 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3861 .build());
3862
3863 // Move the swipe a bit
3864 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3865 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3866 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3867 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3868 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3869 .build());
3870
3871 // End the swipe
3872 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3873 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3874 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3875 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3876 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3877 .build());
3878 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3879 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3880 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3881 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3882 .build());
3883 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3884 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3885 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3886 .build());
3887
3888 window->assertNoEvents();
3889}
3890
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003891/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003892 * Ensure the correct coordinate spaces are used by InputDispatcher.
3893 *
3894 * InputDispatcher works in the display space, so its coordinate system is relative to the display
3895 * panel. Windows get events in the window space, and get raw coordinates in the logical display
3896 * space.
3897 */
3898class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
3899public:
3900 void SetUp() override {
3901 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003902 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003903 }
3904
3905 void addDisplayInfo(int displayId, const ui::Transform& transform) {
3906 gui::DisplayInfo info;
3907 info.displayId = displayId;
3908 info.transform = transform;
3909 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05003910 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003911 }
3912
3913 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
3914 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05003915 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003916 }
3917
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003918 void removeAllWindowsAndDisplays() {
3919 mDisplayInfos.clear();
3920 mWindowInfos.clear();
3921 }
3922
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003923 // Set up a test scenario where the display has a scaled projection and there are two windows
3924 // on the display.
3925 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
3926 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
3927 // respectively.
3928 ui::Transform displayTransform;
3929 displayTransform.set(2, 0, 0, 4);
3930 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
3931
3932 std::shared_ptr<FakeApplicationHandle> application =
3933 std::make_shared<FakeApplicationHandle>();
3934
3935 // Add two windows to the display. Their frames are represented in the display space.
3936 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003937 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
3938 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003939 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
3940 addWindow(firstWindow);
3941
3942 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003943 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3944 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003945 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
3946 addWindow(secondWindow);
3947 return {std::move(firstWindow), std::move(secondWindow)};
3948 }
3949
3950private:
3951 std::vector<gui::DisplayInfo> mDisplayInfos;
3952 std::vector<gui::WindowInfo> mWindowInfos;
3953};
3954
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003955TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003956 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3957 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00003958 // selected so that if the hit test was performed with the point and the bounds being in
3959 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003960 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3961 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3962 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003963
3964 firstWindow->consumeMotionDown();
3965 secondWindow->assertNoEvents();
3966}
3967
3968// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
3969// the event should be treated as being in the logical display space.
3970TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
3971 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3972 // Send down to the first window. The point is represented in the logical display space. The
3973 // point is selected so that if the hit test was done in logical display space, then it would
3974 // end up in the incorrect window.
3975 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3976 PointF{75 * 2, 55 * 4});
3977
3978 firstWindow->consumeMotionDown();
3979 secondWindow->assertNoEvents();
3980}
3981
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003982// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
3983// event should be treated as being in the logical display space.
3984TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
3985 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
3986
3987 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
3988 ui::Transform injectedEventTransform;
3989 injectedEventTransform.set(matrix);
3990 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
3991 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
3992
3993 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3994 .displayId(ADISPLAY_ID_DEFAULT)
3995 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003996 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00003997 .x(untransformedPoint.x)
3998 .y(untransformedPoint.y))
3999 .build();
4000 event.transform(matrix);
4001
4002 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4003 InputEventInjectionSync::WAIT_FOR_RESULT);
4004
4005 firstWindow->consumeMotionDown();
4006 secondWindow->assertNoEvents();
4007}
4008
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004009TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4010 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4011
4012 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004013 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4014 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4015 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004016
4017 firstWindow->assertNoEvents();
4018 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004019 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004020 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4021
4022 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4023 EXPECT_EQ(300, event->getRawX(0));
4024 EXPECT_EQ(880, event->getRawY(0));
4025
4026 // Ensure that the x and y values are in the window's coordinate space.
4027 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4028 // the logical display space. This will be the origin of the window space.
4029 EXPECT_EQ(100, event->getX(0));
4030 EXPECT_EQ(80, event->getY(0));
4031}
4032
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004033/** Ensure consistent behavior of InputDispatcher in all orientations. */
4034class InputDispatcherDisplayOrientationFixture
4035 : public InputDispatcherDisplayProjectionTest,
4036 public ::testing::WithParamInterface<ui::Rotation> {};
4037
4038// This test verifies the touchable region of a window for all rotations of the display by tapping
4039// in different locations on the display, specifically points close to the four corners of a
4040// window.
4041TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4042 constexpr static int32_t displayWidth = 400;
4043 constexpr static int32_t displayHeight = 800;
4044
4045 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4046
4047 const auto rotation = GetParam();
4048
4049 // Set up the display with the specified rotation.
4050 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4051 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4052 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4053 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4054 logicalDisplayWidth, logicalDisplayHeight);
4055 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4056
4057 // Create a window with its bounds determined in the logical display.
4058 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4059 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4060 sp<FakeWindowHandle> window =
4061 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4062 window->setFrame(frameInDisplay, displayTransform);
4063 addWindow(window);
4064
4065 // The following points in logical display space should be inside the window.
4066 static const std::array<vec2, 4> insidePoints{
4067 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4068 for (const auto pointInsideWindow : insidePoints) {
4069 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4070 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004071 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4072 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4073 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004074 window->consumeMotionDown();
4075
Prabir Pradhan678438e2023-04-13 19:32:51 +00004076 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4077 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4078 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004079 window->consumeMotionUp();
4080 }
4081
4082 // The following points in logical display space should be outside the window.
4083 static const std::array<vec2, 5> outsidePoints{
4084 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4085 for (const auto pointOutsideWindow : outsidePoints) {
4086 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4087 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004088 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4089 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4090 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004091
Prabir Pradhan678438e2023-04-13 19:32:51 +00004092 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4093 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4094 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004095 }
4096 window->assertNoEvents();
4097}
4098
4099// Run the precision tests for all rotations.
4100INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4101 InputDispatcherDisplayOrientationFixture,
4102 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4103 ui::ROTATION_270),
4104 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4105 return ftl::enum_string(testParamInfo.param);
4106 });
4107
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004108using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4109 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004110
4111class TransferTouchFixture : public InputDispatcherTest,
4112 public ::testing::WithParamInterface<TransferFunction> {};
4113
4114TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004115 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004116
4117 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004118 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004119 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4120 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004121 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004122 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004123 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4124 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004125 sp<FakeWindowHandle> wallpaper =
4126 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4127 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004128 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004129 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004130
4131 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004132 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4133 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004134
Svet Ganov5d3bc372020-01-26 23:11:07 -08004135 // Only the first window should get the down event
4136 firstWindow->consumeMotionDown();
4137 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004138 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004139
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004140 // Transfer touch to the second window
4141 TransferFunction f = GetParam();
4142 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4143 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004144 // The first window gets cancel and the second gets down
4145 firstWindow->consumeMotionCancel();
4146 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004147 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004148
4149 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004150 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4151 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004152 // The first window gets no events and the second gets up
4153 firstWindow->assertNoEvents();
4154 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004155 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004156}
4157
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004158/**
4159 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4160 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4161 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4162 * natural to the user.
4163 * In this test, we are sending a pointer to both spy window and first window. We then try to
4164 * transfer touch to the second window. The dispatcher should identify the first window as the
4165 * one that should lose the gesture, and therefore the action should be to move the gesture from
4166 * the first window to the second.
4167 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4168 * the other API, as well.
4169 */
4170TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4171 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4172
4173 // Create a couple of windows + a spy window
4174 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004175 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004176 spyWindow->setTrustedOverlay(true);
4177 spyWindow->setSpy(true);
4178 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004179 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004180 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004181 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004182
4183 // Add the windows to the dispatcher
4184 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4185
4186 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004187 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4188 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004189 // Only the first window and spy should get the down event
4190 spyWindow->consumeMotionDown();
4191 firstWindow->consumeMotionDown();
4192
4193 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4194 // if f === 'transferTouch'.
4195 TransferFunction f = GetParam();
4196 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4197 ASSERT_TRUE(success);
4198 // The first window gets cancel and the second gets down
4199 firstWindow->consumeMotionCancel();
4200 secondWindow->consumeMotionDown();
4201
4202 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004203 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4204 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004205 // The first window gets no events and the second+spy get up
4206 firstWindow->assertNoEvents();
4207 spyWindow->consumeMotionUp();
4208 secondWindow->consumeMotionUp();
4209}
4210
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004211TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004212 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004213
4214 PointF touchPoint = {10, 10};
4215
4216 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004217 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004218 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4219 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004220 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004221 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004222 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4223 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004224 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004225
4226 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004227 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004228
4229 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004230 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4231 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4232 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004233 // Only the first window should get the down event
4234 firstWindow->consumeMotionDown();
4235 secondWindow->assertNoEvents();
4236
4237 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004238 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4239 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004240 // Only the first window should get the pointer down event
4241 firstWindow->consumeMotionPointerDown(1);
4242 secondWindow->assertNoEvents();
4243
4244 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004245 TransferFunction f = GetParam();
4246 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4247 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004248 // The first window gets cancel and the second gets down and pointer down
4249 firstWindow->consumeMotionCancel();
4250 secondWindow->consumeMotionDown();
4251 secondWindow->consumeMotionPointerDown(1);
4252
4253 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004254 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4255 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004256 // The first window gets nothing and the second gets pointer up
4257 firstWindow->assertNoEvents();
4258 secondWindow->consumeMotionPointerUp(1);
4259
4260 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004261 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4262 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004263 // The first window gets nothing and the second gets up
4264 firstWindow->assertNoEvents();
4265 secondWindow->consumeMotionUp();
4266}
4267
Arthur Hungc539dbb2022-12-08 07:45:36 +00004268TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4269 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4270
4271 // Create a couple of windows
4272 sp<FakeWindowHandle> firstWindow =
4273 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4274 ADISPLAY_ID_DEFAULT);
4275 firstWindow->setDupTouchToWallpaper(true);
4276 sp<FakeWindowHandle> secondWindow =
4277 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4278 ADISPLAY_ID_DEFAULT);
4279 secondWindow->setDupTouchToWallpaper(true);
4280
4281 sp<FakeWindowHandle> wallpaper1 =
4282 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4283 wallpaper1->setIsWallpaper(true);
4284
4285 sp<FakeWindowHandle> wallpaper2 =
4286 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4287 wallpaper2->setIsWallpaper(true);
4288 // Add the windows to the dispatcher
4289 mDispatcher->setInputWindows(
4290 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4291
4292 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004293 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4294 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004295
4296 // Only the first window should get the down event
4297 firstWindow->consumeMotionDown();
4298 secondWindow->assertNoEvents();
4299 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4300 wallpaper2->assertNoEvents();
4301
4302 // Transfer touch focus to the second window
4303 TransferFunction f = GetParam();
4304 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4305 ASSERT_TRUE(success);
4306
4307 // The first window gets cancel and the second gets down
4308 firstWindow->consumeMotionCancel();
4309 secondWindow->consumeMotionDown();
4310 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4311 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4312
4313 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004314 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4315 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004316 // The first window gets no events and the second gets up
4317 firstWindow->assertNoEvents();
4318 secondWindow->consumeMotionUp();
4319 wallpaper1->assertNoEvents();
4320 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4321}
4322
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004323// For the cases of single pointer touch and two pointers non-split touch, the api's
4324// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4325// for the case where there are multiple pointers split across several windows.
4326INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4327 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004328 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4329 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004330 return dispatcher->transferTouch(destChannelToken,
4331 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004332 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004333 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4334 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004335 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004336 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004337 }));
4338
Svet Ganov5d3bc372020-01-26 23:11:07 -08004339TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004340 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004341
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004342 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004343 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4344 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004345 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004346
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004347 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004348 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4349 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004350 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004351
4352 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004353 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004354
4355 PointF pointInFirst = {300, 200};
4356 PointF pointInSecond = {300, 600};
4357
4358 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004359 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4360 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4361 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004362 // Only the first window should get the down event
4363 firstWindow->consumeMotionDown();
4364 secondWindow->assertNoEvents();
4365
4366 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004367 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4368 ADISPLAY_ID_DEFAULT,
4369 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004370 // The first window gets a move and the second a down
4371 firstWindow->consumeMotionMove();
4372 secondWindow->consumeMotionDown();
4373
4374 // Transfer touch focus to the second window
4375 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4376 // The first window gets cancel and the new gets pointer down (it already saw down)
4377 firstWindow->consumeMotionCancel();
4378 secondWindow->consumeMotionPointerDown(1);
4379
4380 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004381 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4382 ADISPLAY_ID_DEFAULT,
4383 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004384 // The first window gets nothing and the second gets pointer up
4385 firstWindow->assertNoEvents();
4386 secondWindow->consumeMotionPointerUp(1);
4387
4388 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004389 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4390 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004391 // The first window gets nothing and the second gets up
4392 firstWindow->assertNoEvents();
4393 secondWindow->consumeMotionUp();
4394}
4395
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004396// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4397// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4398// touch is not supported, so the touch should continue on those windows and the transferred-to
4399// window should get nothing.
4400TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4401 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4402
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004403 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004404 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4405 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004406 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004407
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004408 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004409 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4410 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004411 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004412
4413 // Add the windows to the dispatcher
4414 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4415
4416 PointF pointInFirst = {300, 200};
4417 PointF pointInSecond = {300, 600};
4418
4419 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004420 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4421 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4422 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004423 // Only the first window should get the down event
4424 firstWindow->consumeMotionDown();
4425 secondWindow->assertNoEvents();
4426
4427 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004428 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4429 ADISPLAY_ID_DEFAULT,
4430 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004431 // The first window gets a move and the second a down
4432 firstWindow->consumeMotionMove();
4433 secondWindow->consumeMotionDown();
4434
4435 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004436 const bool transferred =
4437 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004438 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4439 ASSERT_FALSE(transferred);
4440 firstWindow->assertNoEvents();
4441 secondWindow->assertNoEvents();
4442
4443 // The rest of the dispatch should proceed as normal
4444 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004445 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4446 ADISPLAY_ID_DEFAULT,
4447 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004448 // The first window gets MOVE and the second gets pointer up
4449 firstWindow->consumeMotionMove();
4450 secondWindow->consumeMotionUp();
4451
4452 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004453 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4454 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004455 // The first window gets nothing and the second gets up
4456 firstWindow->consumeMotionUp();
4457 secondWindow->assertNoEvents();
4458}
4459
Arthur Hungabbb9d82021-09-01 14:52:30 +00004460// This case will create two windows and one mirrored window on the default display and mirror
4461// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4462// the windows info of second display before default display.
4463TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4464 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4465 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004466 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004467 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004468 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004469 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004470 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004471
4472 sp<FakeWindowHandle> mirrorWindowInPrimary =
4473 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4474 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004475
4476 sp<FakeWindowHandle> firstWindowInSecondary =
4477 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4478 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004479
4480 sp<FakeWindowHandle> secondWindowInSecondary =
4481 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4482 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004483
4484 // Update window info, let it find window handle of second display first.
4485 mDispatcher->setInputWindows(
4486 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4487 {ADISPLAY_ID_DEFAULT,
4488 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4489
4490 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4491 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4492 {50, 50}))
4493 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4494
4495 // Window should receive motion event.
4496 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4497
4498 // Transfer touch focus
4499 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4500 secondWindowInPrimary->getToken()));
4501 // The first window gets cancel.
4502 firstWindowInPrimary->consumeMotionCancel();
4503 secondWindowInPrimary->consumeMotionDown();
4504
4505 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4506 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4507 ADISPLAY_ID_DEFAULT, {150, 50}))
4508 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4509 firstWindowInPrimary->assertNoEvents();
4510 secondWindowInPrimary->consumeMotionMove();
4511
4512 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4513 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4514 {150, 50}))
4515 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4516 firstWindowInPrimary->assertNoEvents();
4517 secondWindowInPrimary->consumeMotionUp();
4518}
4519
4520// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4521// 'transferTouch' api.
4522TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4523 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4524 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004525 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004526 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004527 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004528 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004529 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004530
4531 sp<FakeWindowHandle> mirrorWindowInPrimary =
4532 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4533 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004534
4535 sp<FakeWindowHandle> firstWindowInSecondary =
4536 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4537 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004538
4539 sp<FakeWindowHandle> secondWindowInSecondary =
4540 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4541 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004542
4543 // Update window info, let it find window handle of second display first.
4544 mDispatcher->setInputWindows(
4545 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4546 {ADISPLAY_ID_DEFAULT,
4547 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4548
4549 // Touch on second display.
4550 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4551 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4552 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4553
4554 // Window should receive motion event.
4555 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4556
4557 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004558 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004559
4560 // The first window gets cancel.
4561 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4562 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4563
4564 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4565 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4566 SECOND_DISPLAY_ID, {150, 50}))
4567 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4568 firstWindowInPrimary->assertNoEvents();
4569 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4570
4571 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4572 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4573 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4574 firstWindowInPrimary->assertNoEvents();
4575 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4576}
4577
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004578TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004579 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004580 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4581 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004582
Vishnu Nair47074b82020-08-14 11:54:47 -07004583 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004584 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004585 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004586
4587 window->consumeFocusEvent(true);
4588
Prabir Pradhan678438e2023-04-13 19:32:51 +00004589 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004590
4591 // Window should receive key down event.
4592 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004593
4594 // Should have poked user activity
4595 mFakePolicy->assertUserActivityPoked();
4596}
4597
4598TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4599 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4600 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4601 "Fake Window", ADISPLAY_ID_DEFAULT);
4602
4603 window->setDisableUserActivity(true);
4604 window->setFocusable(true);
4605 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4606 setFocusedWindow(window);
4607
4608 window->consumeFocusEvent(true);
4609
4610 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4611
4612 // Window should receive key down event.
4613 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4614
4615 // Should have poked user activity
4616 mFakePolicy->assertUserActivityNotPoked();
4617}
4618
4619TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4620 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4621 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4622 "Fake Window", ADISPLAY_ID_DEFAULT);
4623
4624 window->setFocusable(true);
4625 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4626 setFocusedWindow(window);
4627
4628 window->consumeFocusEvent(true);
4629
4630 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4631 mDispatcher->waitForIdle();
4632
4633 // System key is not passed down
4634 window->assertNoEvents();
4635
4636 // Should have poked user activity
4637 mFakePolicy->assertUserActivityPoked();
4638}
4639
4640TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4641 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4642 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4643 "Fake Window", ADISPLAY_ID_DEFAULT);
4644
4645 window->setFocusable(true);
4646 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4647 setFocusedWindow(window);
4648
4649 window->consumeFocusEvent(true);
4650
4651 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4652 mDispatcher->waitForIdle();
4653
4654 // System key is not passed down
4655 window->assertNoEvents();
4656
4657 // Should have poked user activity
4658 mFakePolicy->assertUserActivityPoked();
4659}
4660
4661TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4662 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4663 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4664 "Fake Window", ADISPLAY_ID_DEFAULT);
4665
4666 window->setDisableUserActivity(true);
4667 window->setFocusable(true);
4668 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4669 setFocusedWindow(window);
4670
4671 window->consumeFocusEvent(true);
4672
4673 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4674 mDispatcher->waitForIdle();
4675
4676 // System key is not passed down
4677 window->assertNoEvents();
4678
4679 // Should have poked user activity
4680 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004681}
4682
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004683TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
4684 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4685 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4686 "Fake Window", ADISPLAY_ID_DEFAULT);
4687
4688 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4689
4690 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4691 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4692 ADISPLAY_ID_DEFAULT, {100, 100}))
4693 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4694
4695 window->consumeMotionEvent(
4696 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
4697
4698 // Should have poked user activity
4699 mFakePolicy->assertUserActivityPoked();
4700}
4701
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004702TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004703 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004704 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4705 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004706
Arthur Hung72d8dc32020-03-28 00:48:39 +00004707 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004708
Prabir Pradhan678438e2023-04-13 19:32:51 +00004709 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004710 mDispatcher->waitForIdle();
4711
4712 window->assertNoEvents();
4713}
4714
4715// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4716TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004717 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004718 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4719 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004720
Arthur Hung72d8dc32020-03-28 00:48:39 +00004721 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004722
4723 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004724 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004725 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004726 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4727 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004728
4729 // Window should receive only the motion event
4730 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4731 window->assertNoEvents(); // Key event or focus event will not be received
4732}
4733
arthurhungea3f4fc2020-12-21 23:18:53 +08004734TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4735 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4736
arthurhungea3f4fc2020-12-21 23:18:53 +08004737 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004738 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4739 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004740 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004741
arthurhungea3f4fc2020-12-21 23:18:53 +08004742 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004743 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4744 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004745 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004746
4747 // Add the windows to the dispatcher
4748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4749
4750 PointF pointInFirst = {300, 200};
4751 PointF pointInSecond = {300, 600};
4752
4753 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004754 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4755 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4756 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004757 // Only the first window should get the down event
4758 firstWindow->consumeMotionDown();
4759 secondWindow->assertNoEvents();
4760
4761 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004762 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4763 ADISPLAY_ID_DEFAULT,
4764 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004765 // The first window gets a move and the second a down
4766 firstWindow->consumeMotionMove();
4767 secondWindow->consumeMotionDown();
4768
4769 // Send pointer cancel to the second window
4770 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004771 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004772 {pointInFirst, pointInSecond});
4773 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004774 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004775 // The first window gets move and the second gets cancel.
4776 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4777 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4778
4779 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004780 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4781 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08004782 // The first window gets up and the second gets nothing.
4783 firstWindow->consumeMotionUp();
4784 secondWindow->assertNoEvents();
4785}
4786
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004787TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
4788 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4789
4790 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004791 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004792 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4793 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
4794 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
4795 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
4796
Harry Cutts33476232023-01-30 19:57:29 +00004797 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004798 window->assertNoEvents();
4799 mDispatcher->waitForIdle();
4800}
4801
chaviwd1c23182019-12-20 18:44:56 -08004802class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00004803public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004804 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004805 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07004806 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004807 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07004808 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00004809 }
4810
chaviwd1c23182019-12-20 18:44:56 -08004811 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
4812
4813 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004814 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
4815 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08004816 }
4817
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004818 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
4819
4820 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
4821
chaviwd1c23182019-12-20 18:44:56 -08004822 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004823 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08004824 expectedDisplayId, expectedFlags);
4825 }
4826
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004827 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004828 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004829 expectedDisplayId, expectedFlags);
4830 }
4831
chaviwd1c23182019-12-20 18:44:56 -08004832 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004833 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08004834 expectedDisplayId, expectedFlags);
4835 }
4836
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004837 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004838 mInputReceiver->consumeMotionEvent(
4839 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4840 WithDisplayId(expectedDisplayId),
4841 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004842 }
4843
Arthur Hungfbfa5722021-11-16 02:45:54 +00004844 void consumeMotionPointerDown(int32_t pointerIdx) {
4845 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4846 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004847 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004848 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004849 }
4850
Evan Rosky84f07f02021-04-16 10:42:42 -07004851 MotionEvent* consumeMotion() {
4852 InputEvent* event = mInputReceiver->consume();
4853 if (!event) {
4854 ADD_FAILURE() << "No event was produced";
4855 return nullptr;
4856 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004857 if (event->getType() != InputEventType::MOTION) {
4858 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07004859 return nullptr;
4860 }
4861 return static_cast<MotionEvent*>(event);
4862 }
4863
chaviwd1c23182019-12-20 18:44:56 -08004864 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4865
4866private:
4867 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004868};
4869
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004870using InputDispatcherMonitorTest = InputDispatcherTest;
4871
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004872/**
4873 * Two entities that receive touch: A window, and a global monitor.
4874 * The touch goes to the window, and then the window disappears.
4875 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4876 * for the monitor, as well.
4877 * 1. foregroundWindow
4878 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4879 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004880TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004881 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4882 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004883 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004884
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004885 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004886
4887 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4888 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4889 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4890 {100, 200}))
4891 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4892
4893 // Both the foreground window and the global monitor should receive the touch down
4894 window->consumeMotionDown();
4895 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
4896
4897 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4898 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4899 ADISPLAY_ID_DEFAULT, {110, 200}))
4900 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4901
4902 window->consumeMotionMove();
4903 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4904
4905 // Now the foreground window goes away
4906 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
4907 window->consumeMotionCancel();
4908 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
4909
4910 // If more events come in, there will be no more foreground window to send them to. This will
4911 // cause a cancel for the monitor, as well.
4912 ASSERT_EQ(InputEventInjectionResult::FAILED,
4913 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4914 ADISPLAY_ID_DEFAULT, {120, 200}))
4915 << "Injection should fail because the window was removed";
4916 window->assertNoEvents();
4917 // Global monitor now gets the cancel
4918 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
4919}
4920
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004921TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07004922 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004923 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4924 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004925 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004926
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004927 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004928
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004929 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004930 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004931 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00004932 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08004933 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004934}
4935
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004936TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
4937 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004938
Chris Yea209fde2020-07-22 13:54:51 -07004939 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004940 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4941 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004942 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00004943
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004944 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00004945 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004946 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08004947 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004948 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004949
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004950 // Pilfer pointers from the monitor.
4951 // This should not do anything and the window should continue to receive events.
4952 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00004953
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004954 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004955 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4956 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004957 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004958
4959 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
4960 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00004961}
4962
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004963TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07004964 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004965 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4966 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004967 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4968 window->setWindowOffset(20, 40);
4969 window->setWindowTransform(0, 1, -1, 0);
4970
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004971 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07004972
4973 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4974 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
4975 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4976 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4977 MotionEvent* event = monitor.consumeMotion();
4978 // Even though window has transform, gesture monitor must not.
4979 ASSERT_EQ(ui::Transform(), event->getTransform());
4980}
4981
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004982TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00004983 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004984 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00004985
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004986 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00004987 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004988 << "Injection should fail if there is a monitor, but no touchable window";
4989 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00004990}
4991
chaviw81e2bb92019-12-18 15:03:51 -08004992TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004993 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004994 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4995 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08004996
Arthur Hung72d8dc32020-03-28 00:48:39 +00004997 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08004998
4999 NotifyMotionArgs motionArgs =
5000 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5001 ADISPLAY_ID_DEFAULT);
5002
Prabir Pradhan678438e2023-04-13 19:32:51 +00005003 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005004 // Window should receive motion down event.
5005 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5006
5007 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005008 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005009 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5010 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5011 motionArgs.pointerCoords[0].getX() - 10);
5012
Prabir Pradhan678438e2023-04-13 19:32:51 +00005013 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005014 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005015 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005016}
5017
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005018/**
5019 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5020 * the device default right away. In the test scenario, we check both the default value,
5021 * and the action of enabling / disabling.
5022 */
5023TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005024 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005025 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5026 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005027 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005028
5029 // Set focused application.
5030 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005031 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005032
5033 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005034 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005035 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005036 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005037
5038 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005039 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005040 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005041 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005042
5043 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005044 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005045 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005046 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005047 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005048 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005049 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005050 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005051
5052 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005053 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005054 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005055 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005056
5057 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005058 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005059 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005060 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005061 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005062 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005063 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005064 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005065
5066 window->assertNoEvents();
5067}
5068
Gang Wange9087892020-01-07 12:17:14 -05005069TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005070 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005071 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5072 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005073
5074 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005075 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005076
Arthur Hung72d8dc32020-03-28 00:48:39 +00005077 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005078 setFocusedWindow(window);
5079
Harry Cutts33476232023-01-30 19:57:29 +00005080 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005081
Prabir Pradhan678438e2023-04-13 19:32:51 +00005082 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5083 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005084
5085 InputEvent* event = window->consume();
5086 ASSERT_NE(event, nullptr);
5087
5088 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5089 ASSERT_NE(verified, nullptr);
5090 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5091
5092 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5093 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5094 ASSERT_EQ(keyArgs.source, verified->source);
5095 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5096
5097 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5098
5099 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005100 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005101 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005102 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5103 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5104 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5105 ASSERT_EQ(0, verifiedKey.repeatCount);
5106}
5107
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005108TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005109 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005110 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5111 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005112
5113 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5114
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005115 ui::Transform transform;
5116 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5117
5118 gui::DisplayInfo displayInfo;
5119 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5120 displayInfo.transform = transform;
5121
Patrick Williamsd828f302023-04-28 17:52:08 -05005122 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005123
Prabir Pradhan678438e2023-04-13 19:32:51 +00005124 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005125 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5126 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005127 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005128
5129 InputEvent* event = window->consume();
5130 ASSERT_NE(event, nullptr);
5131
5132 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5133 ASSERT_NE(verified, nullptr);
5134 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5135
5136 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5137 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5138 EXPECT_EQ(motionArgs.source, verified->source);
5139 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5140
5141 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5142
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005143 const vec2 rawXY =
5144 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5145 motionArgs.pointerCoords[0].getXYValue());
5146 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5147 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005148 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005149 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005150 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005151 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5152 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5153}
5154
chaviw09c8d2d2020-08-24 15:48:26 -07005155/**
5156 * Ensure that separate calls to sign the same data are generating the same key.
5157 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5158 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5159 * tests.
5160 */
5161TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5162 KeyEvent event = getTestKeyEvent();
5163 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5164
5165 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5166 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5167 ASSERT_EQ(hmac1, hmac2);
5168}
5169
5170/**
5171 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5172 */
5173TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5174 KeyEvent event = getTestKeyEvent();
5175 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5176 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5177
5178 verifiedEvent.deviceId += 1;
5179 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5180
5181 verifiedEvent.source += 1;
5182 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5183
5184 verifiedEvent.eventTimeNanos += 1;
5185 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5186
5187 verifiedEvent.displayId += 1;
5188 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5189
5190 verifiedEvent.action += 1;
5191 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5192
5193 verifiedEvent.downTimeNanos += 1;
5194 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5195
5196 verifiedEvent.flags += 1;
5197 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5198
5199 verifiedEvent.keyCode += 1;
5200 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5201
5202 verifiedEvent.scanCode += 1;
5203 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5204
5205 verifiedEvent.metaState += 1;
5206 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5207
5208 verifiedEvent.repeatCount += 1;
5209 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5210}
5211
Vishnu Nair958da932020-08-21 17:12:37 -07005212TEST_F(InputDispatcherTest, SetFocusedWindow) {
5213 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5214 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005215 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005216 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005217 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005218 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5219
5220 // Top window is also focusable but is not granted focus.
5221 windowTop->setFocusable(true);
5222 windowSecond->setFocusable(true);
5223 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5224 setFocusedWindow(windowSecond);
5225
5226 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005227 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5228 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005229
5230 // Focused window should receive event.
5231 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5232 windowTop->assertNoEvents();
5233}
5234
5235TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5236 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5237 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005238 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005239 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5240
5241 window->setFocusable(true);
5242 // Release channel for window is no longer valid.
5243 window->releaseChannel();
5244 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5245 setFocusedWindow(window);
5246
5247 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005248 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5249 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005250
5251 // window channel is invalid, so it should not receive any input event.
5252 window->assertNoEvents();
5253}
5254
5255TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5256 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5257 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005258 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005259 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005260 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5261
Vishnu Nair958da932020-08-21 17:12:37 -07005262 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5263 setFocusedWindow(window);
5264
5265 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005266 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5267 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005268
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005269 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005270 window->assertNoEvents();
5271}
5272
5273TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5274 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5275 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005276 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005277 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005278 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005279 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5280
5281 windowTop->setFocusable(true);
5282 windowSecond->setFocusable(true);
5283 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5284 setFocusedWindow(windowTop);
5285 windowTop->consumeFocusEvent(true);
5286
Chavi Weingarten847e8512023-03-29 00:26:09 +00005287 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5288 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005289 windowSecond->consumeFocusEvent(true);
5290 windowTop->consumeFocusEvent(false);
5291
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005292 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5293 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005294
5295 // Focused window should receive event.
5296 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5297}
5298
Chavi Weingarten847e8512023-03-29 00:26:09 +00005299TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005300 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5301 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005302 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005303 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005304 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005305 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5306
5307 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005308 windowSecond->setFocusable(false);
5309 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005310 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005311 setFocusedWindow(windowTop);
5312 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005313
Chavi Weingarten847e8512023-03-29 00:26:09 +00005314 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5315 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005316
5317 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005318 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005319 windowSecond->assertNoEvents();
5320}
5321
5322TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5323 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5324 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005325 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005326 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005327 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5328 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005329 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5330
5331 window->setFocusable(true);
5332 previousFocusedWindow->setFocusable(true);
5333 window->setVisible(false);
5334 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5335 setFocusedWindow(previousFocusedWindow);
5336 previousFocusedWindow->consumeFocusEvent(true);
5337
5338 // Requesting focus on invisible window takes focus from currently focused window.
5339 setFocusedWindow(window);
5340 previousFocusedWindow->consumeFocusEvent(false);
5341
5342 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005343 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005344 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5345 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005346
5347 // Window does not get focus event or key down.
5348 window->assertNoEvents();
5349
5350 // Window becomes visible.
5351 window->setVisible(true);
5352 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5353
5354 // Window receives focus event.
5355 window->consumeFocusEvent(true);
5356 // Focused window receives key down.
5357 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5358}
5359
Vishnu Nair599f1412021-06-21 10:39:58 -07005360TEST_F(InputDispatcherTest, DisplayRemoved) {
5361 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5362 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005363 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005364 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5365
5366 // window is granted focus.
5367 window->setFocusable(true);
5368 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5369 setFocusedWindow(window);
5370 window->consumeFocusEvent(true);
5371
5372 // When a display is removed window loses focus.
5373 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5374 window->consumeFocusEvent(false);
5375}
5376
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005377/**
5378 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5379 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5380 * of the 'slipperyEnterWindow'.
5381 *
5382 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5383 * a way so that the touched location is no longer covered by the top window.
5384 *
5385 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5386 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5387 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5388 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5389 * with ACTION_DOWN).
5390 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5391 * window moved itself away from the touched location and had Flag::SLIPPERY.
5392 *
5393 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5394 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5395 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5396 *
5397 * In this test, we ensure that the event received by the bottom window has
5398 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5399 */
5400TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005401 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5402 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005403
5404 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5405 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5406
5407 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005408 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005409 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005410 // Make sure this one overlaps the bottom window
5411 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5412 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5413 // one. Windows with the same owner are not considered to be occluding each other.
5414 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5415
5416 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005417 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005418 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5419
5420 mDispatcher->setInputWindows(
5421 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5422
5423 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005424 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5425 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5426 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005427 slipperyExitWindow->consumeMotionDown();
5428 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5429 mDispatcher->setInputWindows(
5430 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5431
Prabir Pradhan678438e2023-04-13 19:32:51 +00005432 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5433 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5434 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005435
5436 slipperyExitWindow->consumeMotionCancel();
5437
5438 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5439 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5440}
5441
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005442/**
5443 * Two windows, one on the left and another on the right. The left window is slippery. The right
5444 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5445 * touch moves from the left window into the right window, the gesture should continue to go to the
5446 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5447 * reproduces a crash.
5448 */
5449TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5450 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5451
5452 sp<FakeWindowHandle> leftSlipperyWindow =
5453 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5454 leftSlipperyWindow->setSlippery(true);
5455 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5456
5457 sp<FakeWindowHandle> rightDropTouchesWindow =
5458 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5459 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5460 rightDropTouchesWindow->setDropInput(true);
5461
5462 mDispatcher->setInputWindows(
5463 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5464
5465 // Start touch in the left window
5466 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5467 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5468 .build());
5469 leftSlipperyWindow->consumeMotionDown();
5470
5471 // And move it into the right window
5472 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5473 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5474 .build());
5475
5476 // Since the right window isn't eligible to receive input, touch does not slip.
5477 // The left window continues to receive the gesture.
5478 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5479 rightDropTouchesWindow->assertNoEvents();
5480}
5481
Garfield Tan1c7bc862020-01-28 13:24:04 -08005482class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5483protected:
5484 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5485 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5486
Chris Yea209fde2020-07-22 13:54:51 -07005487 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005488 sp<FakeWindowHandle> mWindow;
5489
5490 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005491 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005492 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005493 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005494 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005495 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5496 ASSERT_EQ(OK, mDispatcher->start());
5497
5498 setUpWindow();
5499 }
5500
5501 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005502 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005503 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005504
Vishnu Nair47074b82020-08-14 11:54:47 -07005505 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005506 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005507 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005508 mWindow->consumeFocusEvent(true);
5509 }
5510
Chris Ye2ad95392020-09-01 13:44:44 -07005511 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005512 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005513 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005514 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005515 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005516
5517 // Window should receive key down event.
5518 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5519 }
5520
5521 void expectKeyRepeatOnce(int32_t repeatCount) {
5522 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5523 InputEvent* repeatEvent = mWindow->consume();
5524 ASSERT_NE(nullptr, repeatEvent);
5525
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005526 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005527
5528 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5529 uint32_t eventAction = repeatKeyEvent->getAction();
5530 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5531 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5532 }
5533
Chris Ye2ad95392020-09-01 13:44:44 -07005534 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005535 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005536 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005537 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005538 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005539
5540 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005541 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005542 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005543 }
5544};
5545
5546TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005547 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005548 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5549 expectKeyRepeatOnce(repeatCount);
5550 }
5551}
5552
5553TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005554 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005555 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5556 expectKeyRepeatOnce(repeatCount);
5557 }
Harry Cutts33476232023-01-30 19:57:29 +00005558 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005559 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005560 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5561 expectKeyRepeatOnce(repeatCount);
5562 }
5563}
5564
5565TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005566 sendAndConsumeKeyDown(/*deviceId=*/1);
5567 expectKeyRepeatOnce(/*repeatCount=*/1);
5568 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005569 mWindow->assertNoEvents();
5570}
5571
5572TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005573 sendAndConsumeKeyDown(/*deviceId=*/1);
5574 expectKeyRepeatOnce(/*repeatCount=*/1);
5575 sendAndConsumeKeyDown(/*deviceId=*/2);
5576 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005577 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005578 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005579 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005580 expectKeyRepeatOnce(/*repeatCount=*/2);
5581 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005582 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005583 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005584 mWindow->assertNoEvents();
5585}
5586
5587TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005588 sendAndConsumeKeyDown(/*deviceId=*/1);
5589 expectKeyRepeatOnce(/*repeatCount=*/1);
5590 sendAndConsumeKeyDown(/*deviceId=*/2);
5591 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005592 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005593 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005594 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005595 mWindow->assertNoEvents();
5596}
5597
liushenxiang42232912021-05-21 20:24:09 +08005598TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5599 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005600 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005601 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005602 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5603 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5604 mWindow->assertNoEvents();
5605}
5606
Garfield Tan1c7bc862020-01-28 13:24:04 -08005607TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005608 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005609 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005610 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5611 InputEvent* repeatEvent = mWindow->consume();
5612 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5613 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5614 IdGenerator::getSource(repeatEvent->getId()));
5615 }
5616}
5617
5618TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005619 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005620 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005621
5622 std::unordered_set<int32_t> idSet;
5623 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5624 InputEvent* repeatEvent = mWindow->consume();
5625 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5626 int32_t id = repeatEvent->getId();
5627 EXPECT_EQ(idSet.end(), idSet.find(id));
5628 idSet.insert(id);
5629 }
5630}
5631
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005632/* Test InputDispatcher for MultiDisplay */
5633class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5634public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005635 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005636 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005637
Chris Yea209fde2020-07-22 13:54:51 -07005638 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005639 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005640 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005641
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005642 // Set focus window for primary display, but focused display would be second one.
5643 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005644 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005645 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005646 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005647 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005648
Chris Yea209fde2020-07-22 13:54:51 -07005649 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005650 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005651 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005652 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005653 // Set focus display to second one.
5654 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5655 // Set focus window for second display.
5656 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005657 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005658 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005659 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005660 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005661 }
5662
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005663 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005664 InputDispatcherTest::TearDown();
5665
Chris Yea209fde2020-07-22 13:54:51 -07005666 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005667 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005668 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005669 windowInSecondary.clear();
5670 }
5671
5672protected:
Chris Yea209fde2020-07-22 13:54:51 -07005673 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005674 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005675 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005676 sp<FakeWindowHandle> windowInSecondary;
5677};
5678
5679TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5680 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005681 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5682 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5683 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005684 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005685 windowInSecondary->assertNoEvents();
5686
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005687 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5689 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5690 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005691 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005692 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005693}
5694
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005695TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005696 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005697 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5698 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005699 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005700 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005701 windowInSecondary->assertNoEvents();
5702
5703 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005704 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005705 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005706 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005707 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005708
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005709 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005710 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005711
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005712 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005713 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005714 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005715
5716 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005717 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005718 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005719 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005720 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005721 windowInSecondary->assertNoEvents();
5722}
5723
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005724// Test per-display input monitors for motion event.
5725TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005726 FakeMonitorReceiver monitorInPrimary =
5727 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5728 FakeMonitorReceiver monitorInSecondary =
5729 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005730
5731 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005732 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5733 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5734 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005735 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005736 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005737 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005738 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005739
5740 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005741 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5742 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5743 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005744 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005745 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005746 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005747 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005748
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005749 // Lift up the touch from the second display
5750 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5751 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5752 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5753 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5754 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5755
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005756 // Test inject a non-pointer motion event.
5757 // If specific a display, it will dispatch to the focused window of particular display,
5758 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005759 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5760 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5761 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005762 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005763 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005764 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005765 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005766}
5767
5768// Test per-display input monitors for key event.
5769TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005770 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005771 FakeMonitorReceiver monitorInPrimary =
5772 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5773 FakeMonitorReceiver monitorInSecondary =
5774 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005775
5776 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005777 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5778 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005779 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005780 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005781 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005782 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005783}
5784
Vishnu Nair958da932020-08-21 17:12:37 -07005785TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
5786 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005787 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005788 secondWindowInPrimary->setFocusable(true);
5789 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
5790 setFocusedWindow(secondWindowInPrimary);
5791 windowInPrimary->consumeFocusEvent(false);
5792 secondWindowInPrimary->consumeFocusEvent(true);
5793
5794 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005795 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
5796 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005797 windowInPrimary->assertNoEvents();
5798 windowInSecondary->assertNoEvents();
5799 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5800}
5801
Arthur Hungdfd528e2021-12-08 13:23:04 +00005802TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
5803 FakeMonitorReceiver monitorInPrimary =
5804 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5805 FakeMonitorReceiver monitorInSecondary =
5806 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
5807
5808 // Test touch down on primary display.
5809 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5810 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5811 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5812 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5813 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5814
5815 // Test touch down on second display.
5816 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5817 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5818 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5819 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
5820 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
5821
5822 // Trigger cancel touch.
5823 mDispatcher->cancelCurrentTouch();
5824 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5825 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5826 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
5827 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
5828
5829 // Test inject a move motion event, no window/monitor should receive the event.
5830 ASSERT_EQ(InputEventInjectionResult::FAILED,
5831 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5832 ADISPLAY_ID_DEFAULT, {110, 200}))
5833 << "Inject motion event should return InputEventInjectionResult::FAILED";
5834 windowInPrimary->assertNoEvents();
5835 monitorInPrimary.assertNoEvents();
5836
5837 ASSERT_EQ(InputEventInjectionResult::FAILED,
5838 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5839 SECOND_DISPLAY_ID, {110, 200}))
5840 << "Inject motion event should return InputEventInjectionResult::FAILED";
5841 windowInSecondary->assertNoEvents();
5842 monitorInSecondary.assertNoEvents();
5843}
5844
Jackal Guof9696682018-10-05 12:23:23 +08005845class InputFilterTest : public InputDispatcherTest {
5846protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005847 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
5848 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08005849 NotifyMotionArgs motionArgs;
5850
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005851 motionArgs =
5852 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005853 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005854 motionArgs =
5855 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005856 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005857 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005858 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005859 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
5860 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08005861 } else {
5862 mFakePolicy->assertFilterInputEventWasNotCalled();
5863 }
5864 }
5865
5866 void testNotifyKey(bool expectToBeFiltered) {
5867 NotifyKeyArgs keyArgs;
5868
5869 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005870 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005871 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005872 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005873 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08005874
5875 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08005876 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08005877 } else {
5878 mFakePolicy->assertFilterInputEventWasNotCalled();
5879 }
5880 }
5881};
5882
5883// Test InputFilter for MotionEvent
5884TEST_F(InputFilterTest, MotionEvent_InputFilter) {
5885 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
5886 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5887 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5888
5889 // Enable InputFilter
5890 mDispatcher->setInputFilterEnabled(true);
5891 // Test touch on both primary and second display, and check if both events are filtered.
5892 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
5893 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
5894
5895 // Disable InputFilter
5896 mDispatcher->setInputFilterEnabled(false);
5897 // Test touch on both primary and second display, and check if both events aren't filtered.
5898 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
5899 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
5900}
5901
5902// Test InputFilter for KeyEvent
5903TEST_F(InputFilterTest, KeyEvent_InputFilter) {
5904 // Since the InputFilter is disabled by default, check if key event aren't filtered.
5905 testNotifyKey(/*expectToBeFiltered*/ false);
5906
5907 // Enable InputFilter
5908 mDispatcher->setInputFilterEnabled(true);
5909 // Send a key event, and check if it is filtered.
5910 testNotifyKey(/*expectToBeFiltered*/ true);
5911
5912 // Disable InputFilter
5913 mDispatcher->setInputFilterEnabled(false);
5914 // Send a key event, and check if it isn't filtered.
5915 testNotifyKey(/*expectToBeFiltered*/ false);
5916}
5917
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005918// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
5919// logical display coordinate space.
5920TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
5921 ui::Transform firstDisplayTransform;
5922 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5923 ui::Transform secondDisplayTransform;
5924 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
5925
5926 std::vector<gui::DisplayInfo> displayInfos(2);
5927 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
5928 displayInfos[0].transform = firstDisplayTransform;
5929 displayInfos[1].displayId = SECOND_DISPLAY_ID;
5930 displayInfos[1].transform = secondDisplayTransform;
5931
Patrick Williamsd828f302023-04-28 17:52:08 -05005932 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005933
5934 // Enable InputFilter
5935 mDispatcher->setInputFilterEnabled(true);
5936
5937 // Ensure the correct transforms are used for the displays.
5938 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
5939 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
5940}
5941
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005942class InputFilterInjectionPolicyTest : public InputDispatcherTest {
5943protected:
5944 virtual void SetUp() override {
5945 InputDispatcherTest::SetUp();
5946
5947 /**
5948 * We don't need to enable input filter to test the injected event policy, but we enabled it
5949 * here to make the tests more realistic, since this policy only matters when inputfilter is
5950 * on.
5951 */
5952 mDispatcher->setInputFilterEnabled(true);
5953
5954 std::shared_ptr<InputApplicationHandle> application =
5955 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005956 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
5957 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005958
5959 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5960 mWindow->setFocusable(true);
5961 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
5962 setFocusedWindow(mWindow);
5963 mWindow->consumeFocusEvent(true);
5964 }
5965
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005966 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5967 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005968 KeyEvent event;
5969
5970 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5971 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
5972 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00005973 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005974 const int32_t additionalPolicyFlags =
5975 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
5976 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005977 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00005978 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
5979 policyFlags | additionalPolicyFlags));
5980
5981 InputEvent* received = mWindow->consume();
5982 ASSERT_NE(nullptr, received);
5983 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005984 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00005985 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
5986 ASSERT_EQ(flags, keyEvent.getFlags());
5987 }
5988
5989 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
5990 int32_t flags) {
5991 MotionEvent event;
5992 PointerProperties pointerProperties[1];
5993 PointerCoords pointerCoords[1];
5994 pointerProperties[0].clear();
5995 pointerProperties[0].id = 0;
5996 pointerCoords[0].clear();
5997 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
5998 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
5999
6000 ui::Transform identityTransform;
6001 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6002 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6003 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6004 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6005 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006006 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006007 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006008 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6009
6010 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6011 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006012 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006013 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6014 policyFlags | additionalPolicyFlags));
6015
6016 InputEvent* received = mWindow->consume();
6017 ASSERT_NE(nullptr, received);
6018 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006019 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006020 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6021 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006022 }
6023
6024private:
6025 sp<FakeWindowHandle> mWindow;
6026};
6027
6028TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006029 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6030 // filter. Without it, the event will no different from a regularly injected event, and the
6031 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006032 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6033 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006034}
6035
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006036TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006037 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006038 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006039 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6040}
6041
6042TEST_F(InputFilterInjectionPolicyTest,
6043 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6044 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006045 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006046 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006047}
6048
6049TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006050 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6051 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006052}
6053
chaviwfd6d3512019-03-25 13:23:49 -07006054class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006055 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006056 InputDispatcherTest::SetUp();
6057
Chris Yea209fde2020-07-22 13:54:51 -07006058 std::shared_ptr<FakeApplicationHandle> application =
6059 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006060 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006061 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006062 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006063
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006064 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006065 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006066 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006067
6068 // Set focused application.
6069 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006070 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006071
6072 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006073 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006074 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006075 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006076 }
6077
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006078 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006079 InputDispatcherTest::TearDown();
6080
6081 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006082 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006083 }
6084
6085protected:
6086 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006087 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006088 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006089};
6090
6091// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6092// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6093// the onPointerDownOutsideFocus callback.
6094TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006095 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006096 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6097 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006098 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006099 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006100
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006101 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006102 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6103}
6104
6105// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6106// DOWN on the window that doesn't have focus. Ensure no window received the
6107// onPointerDownOutsideFocus callback.
6108TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006109 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006110 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006111 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006112 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006113
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006114 ASSERT_TRUE(mDispatcher->waitForIdle());
6115 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006116}
6117
6118// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6119// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6120TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006121 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6122 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006123 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006124 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006125
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006126 ASSERT_TRUE(mDispatcher->waitForIdle());
6127 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006128}
6129
6130// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6131// DOWN on the window that already has focus. Ensure no window received the
6132// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006133TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006134 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006135 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006136 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006137 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006138 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006139
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006140 ASSERT_TRUE(mDispatcher->waitForIdle());
6141 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006142}
6143
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006144// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6145// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6146TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6147 const MotionEvent event =
6148 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6149 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006150 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006151 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6152 .build();
6153 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6154 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6155 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6156
6157 ASSERT_TRUE(mDispatcher->waitForIdle());
6158 mFakePolicy->assertOnPointerDownWasNotCalled();
6159 // Ensure that the unfocused window did not receive any FOCUS events.
6160 mUnfocusedWindow->assertNoEvents();
6161}
6162
chaviwaf87b3e2019-10-01 16:59:28 -07006163// These tests ensures we can send touch events to a single client when there are multiple input
6164// windows that point to the same client token.
6165class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6166 virtual void SetUp() override {
6167 InputDispatcherTest::SetUp();
6168
Chris Yea209fde2020-07-22 13:54:51 -07006169 std::shared_ptr<FakeApplicationHandle> application =
6170 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006171 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6172 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006173 mWindow1->setFrame(Rect(0, 0, 100, 100));
6174
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006175 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6176 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006177 mWindow2->setFrame(Rect(100, 100, 200, 200));
6178
Arthur Hung72d8dc32020-03-28 00:48:39 +00006179 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006180 }
6181
6182protected:
6183 sp<FakeWindowHandle> mWindow1;
6184 sp<FakeWindowHandle> mWindow2;
6185
6186 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006187 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006188 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6189 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006190 }
6191
6192 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6193 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006194 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006195 InputEvent* event = window->consume();
6196
6197 ASSERT_NE(nullptr, event) << name.c_str()
6198 << ": consumer should have returned non-NULL event.";
6199
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006200 ASSERT_EQ(InputEventType::MOTION, event->getType())
6201 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006202
6203 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006204 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006205 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006206
6207 for (size_t i = 0; i < points.size(); i++) {
6208 float expectedX = points[i].x;
6209 float expectedY = points[i].y;
6210
6211 EXPECT_EQ(expectedX, motionEvent.getX(i))
6212 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6213 << ", got " << motionEvent.getX(i);
6214 EXPECT_EQ(expectedY, motionEvent.getY(i))
6215 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6216 << ", got " << motionEvent.getY(i);
6217 }
6218 }
chaviw9eaa22c2020-07-01 16:21:27 -07006219
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006220 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006221 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006222 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6223 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006224
6225 // Always consume from window1 since it's the window that has the InputReceiver
6226 consumeMotionEvent(mWindow1, action, expectedPoints);
6227 }
chaviwaf87b3e2019-10-01 16:59:28 -07006228};
6229
6230TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6231 // Touch Window 1
6232 PointF touchedPoint = {10, 10};
6233 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006234 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006235
6236 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006237 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006238
6239 // Touch Window 2
6240 touchedPoint = {150, 150};
6241 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006242 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006243}
6244
chaviw9eaa22c2020-07-01 16:21:27 -07006245TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6246 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006247 mWindow2->setWindowScale(0.5f, 0.5f);
6248
6249 // Touch Window 1
6250 PointF touchedPoint = {10, 10};
6251 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006252 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006253 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006254 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006255
6256 // Touch Window 2
6257 touchedPoint = {150, 150};
6258 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006259 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6260 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006261
chaviw9eaa22c2020-07-01 16:21:27 -07006262 // Update the transform so rotation is set
6263 mWindow2->setWindowTransform(0, -1, 1, 0);
6264 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6265 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006266}
6267
chaviw9eaa22c2020-07-01 16:21:27 -07006268TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006269 mWindow2->setWindowScale(0.5f, 0.5f);
6270
6271 // Touch Window 1
6272 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6273 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006274 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006275
6276 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006277 touchedPoints.push_back(PointF{150, 150});
6278 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006279 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006280
chaviw9eaa22c2020-07-01 16:21:27 -07006281 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006282 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006283 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006284
chaviw9eaa22c2020-07-01 16:21:27 -07006285 // Update the transform so rotation is set for Window 2
6286 mWindow2->setWindowTransform(0, -1, 1, 0);
6287 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006288 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006289}
6290
chaviw9eaa22c2020-07-01 16:21:27 -07006291TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006292 mWindow2->setWindowScale(0.5f, 0.5f);
6293
6294 // Touch Window 1
6295 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6296 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006297 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006298
6299 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006300 touchedPoints.push_back(PointF{150, 150});
6301 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006302
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006303 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006304
6305 // Move both windows
6306 touchedPoints = {{20, 20}, {175, 175}};
6307 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6308 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6309
chaviw9eaa22c2020-07-01 16:21:27 -07006310 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006311
chaviw9eaa22c2020-07-01 16:21:27 -07006312 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006313 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006314 expectedPoints.pop_back();
6315
6316 // Touch Window 2
6317 mWindow2->setWindowTransform(0, -1, 1, 0);
6318 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006319 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006320
6321 // Move both windows
6322 touchedPoints = {{20, 20}, {175, 175}};
6323 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6324 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6325
6326 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006327}
6328
6329TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6330 mWindow1->setWindowScale(0.5f, 0.5f);
6331
6332 // Touch Window 1
6333 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6334 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006335 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006336
6337 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006338 touchedPoints.push_back(PointF{150, 150});
6339 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006340
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006341 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006342
6343 // Move both windows
6344 touchedPoints = {{20, 20}, {175, 175}};
6345 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6346 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6347
chaviw9eaa22c2020-07-01 16:21:27 -07006348 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006349}
6350
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006351/**
6352 * When one of the windows is slippery, the touch should not slip into the other window with the
6353 * same input channel.
6354 */
6355TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6356 mWindow1->setSlippery(true);
6357 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6358
6359 // Touch down in window 1
6360 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6361 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6362 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6363
6364 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6365 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6366 // getting generated.
6367 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6368 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6369
6370 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6371}
6372
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07006373/**
6374 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
6375 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
6376 * that the pointer is hovering over may have a different transform.
6377 */
6378TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
6379 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6380
6381 // Start hover in window 1
6382 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN,
6383 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6384 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6385 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
6386
6387 // Move hover to window 2.
6388 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6389 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6390
6391 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
6392 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6393 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
6394}
6395
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006396class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6397 virtual void SetUp() override {
6398 InputDispatcherTest::SetUp();
6399
Chris Yea209fde2020-07-22 13:54:51 -07006400 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006401 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006402 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6403 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006404 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006405 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006406 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006407
6408 // Set focused application.
6409 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6410
6411 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006412 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006413 mWindow->consumeFocusEvent(true);
6414 }
6415
6416 virtual void TearDown() override {
6417 InputDispatcherTest::TearDown();
6418 mWindow.clear();
6419 }
6420
6421protected:
Chris Yea209fde2020-07-22 13:54:51 -07006422 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006423 sp<FakeWindowHandle> mWindow;
6424 static constexpr PointF WINDOW_LOCATION = {20, 20};
6425
6426 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006427 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006428 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6429 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006430 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006431 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6432 WINDOW_LOCATION));
6433 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006434
6435 sp<FakeWindowHandle> addSpyWindow() {
6436 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006437 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006438 spy->setTrustedOverlay(true);
6439 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006440 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006441 spy->setDispatchingTimeout(30ms);
6442 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6443 return spy;
6444 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006445};
6446
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006447// Send a tap and respond, which should not cause an ANR.
6448TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6449 tapOnWindow();
6450 mWindow->consumeMotionDown();
6451 mWindow->consumeMotionUp();
6452 ASSERT_TRUE(mDispatcher->waitForIdle());
6453 mFakePolicy->assertNotifyAnrWasNotCalled();
6454}
6455
6456// Send a regular key and respond, which should not cause an ANR.
6457TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006458 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006459 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6460 ASSERT_TRUE(mDispatcher->waitForIdle());
6461 mFakePolicy->assertNotifyAnrWasNotCalled();
6462}
6463
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006464TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6465 mWindow->setFocusable(false);
6466 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6467 mWindow->consumeFocusEvent(false);
6468
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006469 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006470 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6471 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6472 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006473 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006474 // Key will not go to window because we have no focused window.
6475 // The 'no focused window' ANR timer should start instead.
6476
6477 // Now, the focused application goes away.
6478 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6479 // The key should get dropped and there should be no ANR.
6480
6481 ASSERT_TRUE(mDispatcher->waitForIdle());
6482 mFakePolicy->assertNotifyAnrWasNotCalled();
6483}
6484
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006485// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006486// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6487// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006488TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006489 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006490 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6491 WINDOW_LOCATION));
6492
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006493 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6494 ASSERT_TRUE(sequenceNum);
6495 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006496 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006497
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006498 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006499 mWindow->consumeMotionEvent(
6500 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006501 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006502 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006503}
6504
6505// Send a key to the app and have the app not respond right away.
6506TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6507 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006508 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006509 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6510 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006511 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006512 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006513 ASSERT_TRUE(mDispatcher->waitForIdle());
6514}
6515
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006516// We have a focused application, but no focused window
6517TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006518 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006519 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6520 mWindow->consumeFocusEvent(false);
6521
6522 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006523 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006524 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6525 WINDOW_LOCATION));
6526 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6527 mDispatcher->waitForIdle();
6528 mFakePolicy->assertNotifyAnrWasNotCalled();
6529
6530 // Once a focused event arrives, we get an ANR for this application
6531 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6532 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006533 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006534 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6535 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006536 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006537 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006538 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006539 ASSERT_TRUE(mDispatcher->waitForIdle());
6540}
6541
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006542/**
6543 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6544 * there will not be an ANR.
6545 */
6546TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6547 mWindow->setFocusable(false);
6548 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6549 mWindow->consumeFocusEvent(false);
6550
6551 KeyEvent event;
6552 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6553 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6554
6555 // Define a valid key down event that is stale (too old).
6556 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6557 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006558 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006559
6560 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6561
6562 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006563 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006564 InputEventInjectionSync::WAIT_FOR_RESULT,
6565 INJECT_EVENT_TIMEOUT, policyFlags);
6566 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6567 << "Injection should fail because the event is stale";
6568
6569 ASSERT_TRUE(mDispatcher->waitForIdle());
6570 mFakePolicy->assertNotifyAnrWasNotCalled();
6571 mWindow->assertNoEvents();
6572}
6573
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006574// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006575// Make sure that we don't notify policy twice about the same ANR.
6576TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006577 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006578 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6579 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006580
6581 // Once a focused event arrives, we get an ANR for this application
6582 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6583 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006584 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006585 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6586 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006587 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006588 const std::chrono::duration appTimeout =
6589 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6590 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006591
Vishnu Naire4df8752022-09-08 09:17:55 -07006592 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006593 // ANR should not be raised again. It is up to policy to do that if it desires.
6594 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006595
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006596 // If we now get a focused window, the ANR should stop, but the policy handles that via
6597 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006598 ASSERT_TRUE(mDispatcher->waitForIdle());
6599}
6600
6601// We have a focused application, but no focused window
6602TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006603 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006604 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6605 mWindow->consumeFocusEvent(false);
6606
6607 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006608 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006609 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006610 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6611 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006612
Vishnu Naire4df8752022-09-08 09:17:55 -07006613 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6614 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006615
6616 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006617 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006618 ASSERT_TRUE(mDispatcher->waitForIdle());
6619 mWindow->assertNoEvents();
6620}
6621
6622/**
6623 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6624 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6625 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6626 * the ANR mechanism should still work.
6627 *
6628 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6629 * DOWN event, while not responding on the second one.
6630 */
6631TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6632 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6633 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6634 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6635 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6636 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006637 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006638
6639 // Now send ACTION_UP, with identical timestamp
6640 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, 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 // We have now sent down and up. Let's consume first event and then ANR on the second.
6647 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6648 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006649 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006650}
6651
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006652// A spy window can receive an ANR
6653TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6654 sp<FakeWindowHandle> spy = addSpyWindow();
6655
6656 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6657 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6658 WINDOW_LOCATION));
6659 mWindow->consumeMotionDown();
6660
6661 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6662 ASSERT_TRUE(sequenceNum);
6663 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006664 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006665
6666 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006667 spy->consumeMotionEvent(
6668 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006669 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006670 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006671}
6672
6673// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006674// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006675TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6676 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006677
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006678 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6679 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006680 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006681 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006682
6683 // Stuck on the ACTION_UP
6684 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006685 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006686
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006687 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006688 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006689 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6690 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006691
6692 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6693 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006694 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006695 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006696 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006697}
6698
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006699// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006700// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006701TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6702 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006703
6704 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006705 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6706 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006707
6708 mWindow->consumeMotionDown();
6709 // Stuck on the ACTION_UP
6710 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006711 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006712
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006713 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006714 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006715 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6716 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006717
6718 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6719 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006720 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006721 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006722 spy->assertNoEvents();
6723}
6724
6725TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6726 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6727
6728 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6729
6730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6731 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6732 WINDOW_LOCATION));
6733
6734 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6735 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6736 ASSERT_TRUE(consumeSeq);
6737
Prabir Pradhanedd96402022-02-15 01:46:16 -08006738 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006739
6740 monitor.finishEvent(*consumeSeq);
6741 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6742
6743 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006744 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006745}
6746
6747// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6748// process events, you don't get an anr. When the window later becomes unresponsive again, you
6749// get an ANR again.
6750// 1. tap -> block on ACTION_UP -> receive ANR
6751// 2. consume all pending events (= queue becomes healthy again)
6752// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6753TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6754 tapOnWindow();
6755
6756 mWindow->consumeMotionDown();
6757 // Block on ACTION_UP
6758 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006759 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006760 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6761 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006762 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006763 mWindow->assertNoEvents();
6764
6765 tapOnWindow();
6766 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006767 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006768 mWindow->consumeMotionUp();
6769
6770 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006771 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006772 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006773 mWindow->assertNoEvents();
6774}
6775
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006776// If a connection remains unresponsive for a while, make sure policy is only notified once about
6777// it.
6778TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006779 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006780 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6781 WINDOW_LOCATION));
6782
6783 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006784 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006785 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006786 // 'notifyConnectionUnresponsive' should only be called once per connection
6787 mFakePolicy->assertNotifyAnrWasNotCalled();
6788 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006789 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006790 mWindow->consumeMotionEvent(
6791 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006792 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006793 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006794 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006795 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006796}
6797
6798/**
6799 * If a window is processing a motion event, and then a key event comes in, the key event should
6800 * not to to the focused window until the motion is processed.
6801 *
6802 * Warning!!!
6803 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6804 * and the injection timeout that we specify when injecting the key.
6805 * We must have the injection timeout (10ms) be smaller than
6806 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6807 *
6808 * If that value changes, this test should also change.
6809 */
6810TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
6811 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6812 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6813
6814 tapOnWindow();
6815 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6816 ASSERT_TRUE(downSequenceNum);
6817 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6818 ASSERT_TRUE(upSequenceNum);
6819 // Don't finish the events yet, and send a key
6820 // Injection will "succeed" because we will eventually give up and send the key to the focused
6821 // window even if motions are still being processed. But because the injection timeout is short,
6822 // we will receive INJECTION_TIMED_OUT as the result.
6823
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006824 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006825 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006826 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6827 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006828 // Key will not be sent to the window, yet, because the window is still processing events
6829 // and the key remains pending, waiting for the touch events to be processed
6830 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6831 ASSERT_FALSE(keySequenceNum);
6832
6833 std::this_thread::sleep_for(500ms);
6834 // if we wait long enough though, dispatcher will give up, and still send the key
6835 // to the focused window, even though we have not yet finished the motion event
6836 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6837 mWindow->finishEvent(*downSequenceNum);
6838 mWindow->finishEvent(*upSequenceNum);
6839}
6840
6841/**
6842 * If a window is processing a motion event, and then a key event comes in, the key event should
6843 * not go to the focused window until the motion is processed.
6844 * If then a new motion comes in, then the pending key event should be going to the currently
6845 * focused window right away.
6846 */
6847TEST_F(InputDispatcherSingleWindowAnr,
6848 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
6849 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6850 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6851
6852 tapOnWindow();
6853 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6854 ASSERT_TRUE(downSequenceNum);
6855 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6856 ASSERT_TRUE(upSequenceNum);
6857 // Don't finish the events yet, and send a key
6858 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006859 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006860 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6861 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006862 // At this point, key is still pending, and should not be sent to the application yet.
6863 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6864 ASSERT_FALSE(keySequenceNum);
6865
6866 // Now tap down again. It should cause the pending key to go to the focused window right away.
6867 tapOnWindow();
6868 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
6869 // the other events yet. We can finish events in any order.
6870 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
6871 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
6872 mWindow->consumeMotionDown();
6873 mWindow->consumeMotionUp();
6874 mWindow->assertNoEvents();
6875}
6876
6877class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
6878 virtual void SetUp() override {
6879 InputDispatcherTest::SetUp();
6880
Chris Yea209fde2020-07-22 13:54:51 -07006881 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006882 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006883 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
6884 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006885 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006886 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006887 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006888
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006889 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
6890 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006891 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006892 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006893
6894 // Set focused application.
6895 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07006896 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006897
6898 // Expect one focus window exist in display.
6899 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006900 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006901 mFocusedWindow->consumeFocusEvent(true);
6902 }
6903
6904 virtual void TearDown() override {
6905 InputDispatcherTest::TearDown();
6906
6907 mUnfocusedWindow.clear();
6908 mFocusedWindow.clear();
6909 }
6910
6911protected:
Chris Yea209fde2020-07-22 13:54:51 -07006912 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006913 sp<FakeWindowHandle> mUnfocusedWindow;
6914 sp<FakeWindowHandle> mFocusedWindow;
6915 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
6916 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
6917 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
6918
6919 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
6920
6921 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
6922
6923private:
6924 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006925 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006926 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6927 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006928 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006929 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6930 location));
6931 }
6932};
6933
6934// If we have 2 windows that are both unresponsive, the one with the shortest timeout
6935// should be ANR'd first.
6936TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006937 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006938 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6939 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006940 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006941 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006942 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00006943 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006944 // We consumed all events, so no ANR
6945 ASSERT_TRUE(mDispatcher->waitForIdle());
6946 mFakePolicy->assertNotifyAnrWasNotCalled();
6947
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006948 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006949 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6950 FOCUSED_WINDOW_LOCATION));
6951 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
6952 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006953
6954 const std::chrono::duration timeout =
6955 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006956 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006957 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
6958 // sequence to make it consistent
6959 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006960 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006961 mFocusedWindow->consumeMotionDown();
6962 // This cancel is generated because the connection was unresponsive
6963 mFocusedWindow->consumeMotionCancel();
6964 mFocusedWindow->assertNoEvents();
6965 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006966 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006967 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
6968 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006969 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006970}
6971
6972// If we have 2 windows with identical timeouts that are both unresponsive,
6973// it doesn't matter which order they should have ANR.
6974// But we should receive ANR for both.
6975TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
6976 // Set the timeout for unfocused window to match the focused window
6977 mUnfocusedWindow->setDispatchingTimeout(10ms);
6978 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
6979
6980 tapOnFocusedWindow();
6981 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08006982 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
6983 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
6984 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006985
6986 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006987 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
6988 mFocusedWindow->getToken() == anrConnectionToken2);
6989 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
6990 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006991
6992 ASSERT_TRUE(mDispatcher->waitForIdle());
6993 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006994
6995 mFocusedWindow->consumeMotionDown();
6996 mFocusedWindow->consumeMotionUp();
6997 mUnfocusedWindow->consumeMotionOutside();
6998
Prabir Pradhanedd96402022-02-15 01:46:16 -08006999 sp<IBinder> responsiveToken1, responsiveToken2;
7000 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
7001 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007002
7003 // Both applications should be marked as responsive, in any order
7004 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
7005 mFocusedWindow->getToken() == responsiveToken2);
7006 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
7007 mUnfocusedWindow->getToken() == responsiveToken2);
7008 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007009}
7010
7011// If a window is already not responding, the second tap on the same window should be ignored.
7012// We should also log an error to account for the dropped event (not tested here).
7013// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7014TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7015 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007016 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007017 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007018 // Receive the events, but don't respond
7019 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7020 ASSERT_TRUE(downEventSequenceNum);
7021 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7022 ASSERT_TRUE(upEventSequenceNum);
7023 const std::chrono::duration timeout =
7024 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007025 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007026
7027 // Tap once again
7028 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007029 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007030 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7031 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007032 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007033 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7034 FOCUSED_WINDOW_LOCATION));
7035 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7036 // valid touch target
7037 mUnfocusedWindow->assertNoEvents();
7038
7039 // Consume the first tap
7040 mFocusedWindow->finishEvent(*downEventSequenceNum);
7041 mFocusedWindow->finishEvent(*upEventSequenceNum);
7042 ASSERT_TRUE(mDispatcher->waitForIdle());
7043 // The second tap did not go to the focused window
7044 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007045 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007046 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7047 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007048 mFakePolicy->assertNotifyAnrWasNotCalled();
7049}
7050
7051// If you tap outside of all windows, there will not be ANR
7052TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007053 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007054 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7055 LOCATION_OUTSIDE_ALL_WINDOWS));
7056 ASSERT_TRUE(mDispatcher->waitForIdle());
7057 mFakePolicy->assertNotifyAnrWasNotCalled();
7058}
7059
7060// Since the focused window is paused, tapping on it should not produce any events
7061TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7062 mFocusedWindow->setPaused(true);
7063 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7064
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007065 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007066 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7067 FOCUSED_WINDOW_LOCATION));
7068
7069 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7070 ASSERT_TRUE(mDispatcher->waitForIdle());
7071 // Should not ANR because the window is paused, and touches shouldn't go to it
7072 mFakePolicy->assertNotifyAnrWasNotCalled();
7073
7074 mFocusedWindow->assertNoEvents();
7075 mUnfocusedWindow->assertNoEvents();
7076}
7077
7078/**
7079 * If a window is processing a motion event, and then a key event comes in, the key event should
7080 * not to to the focused window until the motion is processed.
7081 * If a different window becomes focused at this time, the key should go to that window instead.
7082 *
7083 * Warning!!!
7084 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7085 * and the injection timeout that we specify when injecting the key.
7086 * We must have the injection timeout (10ms) be smaller than
7087 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7088 *
7089 * If that value changes, this test should also change.
7090 */
7091TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7092 // Set a long ANR timeout to prevent it from triggering
7093 mFocusedWindow->setDispatchingTimeout(2s);
7094 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7095
7096 tapOnUnfocusedWindow();
7097 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7098 ASSERT_TRUE(downSequenceNum);
7099 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7100 ASSERT_TRUE(upSequenceNum);
7101 // Don't finish the events yet, and send a key
7102 // Injection will succeed because we will eventually give up and send the key to the focused
7103 // window even if motions are still being processed.
7104
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007105 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007106 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7107 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007108 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007109 // Key will not be sent to the window, yet, because the window is still processing events
7110 // and the key remains pending, waiting for the touch events to be processed
7111 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7112 ASSERT_FALSE(keySequenceNum);
7113
7114 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007115 mFocusedWindow->setFocusable(false);
7116 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007117 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007118 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007119
7120 // Focus events should precede the key events
7121 mUnfocusedWindow->consumeFocusEvent(true);
7122 mFocusedWindow->consumeFocusEvent(false);
7123
7124 // Finish the tap events, which should unblock dispatcher
7125 mUnfocusedWindow->finishEvent(*downSequenceNum);
7126 mUnfocusedWindow->finishEvent(*upSequenceNum);
7127
7128 // Now that all queues are cleared and no backlog in the connections, the key event
7129 // can finally go to the newly focused "mUnfocusedWindow".
7130 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7131 mFocusedWindow->assertNoEvents();
7132 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007133 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007134}
7135
7136// When the touch stream is split across 2 windows, and one of them does not respond,
7137// then ANR should be raised and the touch should be canceled for the unresponsive window.
7138// The other window should not be affected by that.
7139TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7140 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007141 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7142 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7143 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007144 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007145 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007146
7147 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007148 mDispatcher->notifyMotion(
7149 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7150 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007151
7152 const std::chrono::duration timeout =
7153 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007154 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007155
7156 mUnfocusedWindow->consumeMotionDown();
7157 mFocusedWindow->consumeMotionDown();
7158 // Focused window may or may not receive ACTION_MOVE
7159 // But it should definitely receive ACTION_CANCEL due to the ANR
7160 InputEvent* event;
7161 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7162 ASSERT_TRUE(moveOrCancelSequenceNum);
7163 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7164 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007165 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007166 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7167 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7168 mFocusedWindow->consumeMotionCancel();
7169 } else {
7170 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7171 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007172 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007173 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7174 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007175
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007176 mUnfocusedWindow->assertNoEvents();
7177 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007178 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007179}
7180
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007181/**
7182 * If we have no focused window, and a key comes in, we start the ANR timer.
7183 * The focused application should add a focused window before the timer runs out to prevent ANR.
7184 *
7185 * If the user touches another application during this time, the key should be dropped.
7186 * Next, if a new focused window comes in, without toggling the focused application,
7187 * then no ANR should occur.
7188 *
7189 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7190 * but in some cases the policy may not update the focused application.
7191 */
7192TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7193 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7194 std::make_shared<FakeApplicationHandle>();
7195 focusedApplication->setDispatchingTimeout(60ms);
7196 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7197 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7198 mFocusedWindow->setFocusable(false);
7199
7200 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7201 mFocusedWindow->consumeFocusEvent(false);
7202
7203 // Send a key. The ANR timer should start because there is no focused window.
7204 // 'focusedApplication' will get blamed if this timer completes.
7205 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007206 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007207 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7208 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7209 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007210 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007211
7212 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7213 // then the injected touches won't cause the focused event to get dropped.
7214 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7215 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7216 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7217 // For this test, it means that the key would get delivered to the window once it becomes
7218 // focused.
7219 std::this_thread::sleep_for(10ms);
7220
7221 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007222 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7223 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7224 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007225
7226 // We do not consume the motion right away, because that would require dispatcher to first
7227 // process (== drop) the key event, and by that time, ANR will be raised.
7228 // Set the focused window first.
7229 mFocusedWindow->setFocusable(true);
7230 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7231 setFocusedWindow(mFocusedWindow);
7232 mFocusedWindow->consumeFocusEvent(true);
7233 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7234 // to another application. This could be a bug / behaviour in the policy.
7235
7236 mUnfocusedWindow->consumeMotionDown();
7237
7238 ASSERT_TRUE(mDispatcher->waitForIdle());
7239 // Should not ANR because we actually have a focused window. It was just added too slowly.
7240 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7241}
7242
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007243// These tests ensure we cannot send touch events to a window that's positioned behind a window
7244// that has feature NO_INPUT_CHANNEL.
7245// Layout:
7246// Top (closest to user)
7247// mNoInputWindow (above all windows)
7248// mBottomWindow
7249// Bottom (furthest from user)
7250class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7251 virtual void SetUp() override {
7252 InputDispatcherTest::SetUp();
7253
7254 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007255 mNoInputWindow =
7256 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7257 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007258 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007259 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007260 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7261 // It's perfectly valid for this window to not have an associated input channel
7262
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007263 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7264 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007265 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7266
7267 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7268 }
7269
7270protected:
7271 std::shared_ptr<FakeApplicationHandle> mApplication;
7272 sp<FakeWindowHandle> mNoInputWindow;
7273 sp<FakeWindowHandle> mBottomWindow;
7274};
7275
7276TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7277 PointF touchedPoint = {10, 10};
7278
Prabir Pradhan678438e2023-04-13 19:32:51 +00007279 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7280 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7281 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007282
7283 mNoInputWindow->assertNoEvents();
7284 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7285 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7286 // and therefore should prevent mBottomWindow from receiving touches
7287 mBottomWindow->assertNoEvents();
7288}
7289
7290/**
7291 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7292 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7293 */
7294TEST_F(InputDispatcherMultiWindowOcclusionTests,
7295 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007296 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7297 "Window with input channel and NO_INPUT_CHANNEL",
7298 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007299
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007300 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007301 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7302 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7303
7304 PointF touchedPoint = {10, 10};
7305
Prabir Pradhan678438e2023-04-13 19:32:51 +00007306 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7307 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7308 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007309
7310 mNoInputWindow->assertNoEvents();
7311 mBottomWindow->assertNoEvents();
7312}
7313
Vishnu Nair958da932020-08-21 17:12:37 -07007314class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7315protected:
7316 std::shared_ptr<FakeApplicationHandle> mApp;
7317 sp<FakeWindowHandle> mWindow;
7318 sp<FakeWindowHandle> mMirror;
7319
7320 virtual void SetUp() override {
7321 InputDispatcherTest::SetUp();
7322 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007323 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7324 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7325 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007326 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7327 mWindow->setFocusable(true);
7328 mMirror->setFocusable(true);
7329 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7330 }
7331};
7332
7333TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7334 // Request focus on a mirrored window
7335 setFocusedWindow(mMirror);
7336
7337 // window gets focused
7338 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007339 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7340 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007341 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7342}
7343
7344// A focused & mirrored window remains focused only if the window and its mirror are both
7345// focusable.
7346TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7347 setFocusedWindow(mMirror);
7348
7349 // window gets focused
7350 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007351 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7352 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007353 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007354 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7355 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007356 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7357
7358 mMirror->setFocusable(false);
7359 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7360
7361 // window loses focus since one of the windows associated with the token in not focusable
7362 mWindow->consumeFocusEvent(false);
7363
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007364 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7365 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007366 mWindow->assertNoEvents();
7367}
7368
7369// A focused & mirrored window remains focused until the window and its mirror both become
7370// invisible.
7371TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7372 setFocusedWindow(mMirror);
7373
7374 // window gets focused
7375 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007376 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7377 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007378 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007379 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7380 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007381 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7382
7383 mMirror->setVisible(false);
7384 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7385
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007386 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7387 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007388 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007389 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7390 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007391 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7392
7393 mWindow->setVisible(false);
7394 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7395
7396 // window loses focus only after all windows associated with the token become invisible.
7397 mWindow->consumeFocusEvent(false);
7398
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007399 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7400 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007401 mWindow->assertNoEvents();
7402}
7403
7404// A focused & mirrored window remains focused until both windows are removed.
7405TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7406 setFocusedWindow(mMirror);
7407
7408 // window gets focused
7409 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007410 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7411 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007412 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007413 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7414 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007415 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7416
7417 // single window is removed but the window token remains focused
7418 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7419
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007420 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7421 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007422 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007423 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7424 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007425 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7426
7427 // Both windows are removed
7428 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7429 mWindow->consumeFocusEvent(false);
7430
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007431 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7432 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007433 mWindow->assertNoEvents();
7434}
7435
7436// Focus request can be pending until one window becomes visible.
7437TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7438 // Request focus on an invisible mirror.
7439 mWindow->setVisible(false);
7440 mMirror->setVisible(false);
7441 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7442 setFocusedWindow(mMirror);
7443
7444 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007445 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007446 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7447 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007448
7449 mMirror->setVisible(true);
7450 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7451
7452 // window gets focused
7453 mWindow->consumeFocusEvent(true);
7454 // window gets the pending key event
7455 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7456}
Prabir Pradhan99987712020-11-10 18:43:05 -08007457
7458class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7459protected:
7460 std::shared_ptr<FakeApplicationHandle> mApp;
7461 sp<FakeWindowHandle> mWindow;
7462 sp<FakeWindowHandle> mSecondWindow;
7463
7464 void SetUp() override {
7465 InputDispatcherTest::SetUp();
7466 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007467 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007468 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007469 mSecondWindow =
7470 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007471 mSecondWindow->setFocusable(true);
7472
7473 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7474 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7475
7476 setFocusedWindow(mWindow);
7477 mWindow->consumeFocusEvent(true);
7478 }
7479
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007480 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007481 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007482 }
7483
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007484 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7485 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007486 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007487 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7488 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007489 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007490 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007491 }
7492};
7493
7494TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7495 // Ensure that capture cannot be obtained for unfocused windows.
7496 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7497 mFakePolicy->assertSetPointerCaptureNotCalled();
7498 mSecondWindow->assertNoEvents();
7499
7500 // Ensure that capture can be enabled from the focus window.
7501 requestAndVerifyPointerCapture(mWindow, true);
7502
7503 // Ensure that capture cannot be disabled from a window that does not have capture.
7504 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7505 mFakePolicy->assertSetPointerCaptureNotCalled();
7506
7507 // Ensure that capture can be disabled from the window with capture.
7508 requestAndVerifyPointerCapture(mWindow, false);
7509}
7510
7511TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007512 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007513
7514 setFocusedWindow(mSecondWindow);
7515
7516 // Ensure that the capture disabled event was sent first.
7517 mWindow->consumeCaptureEvent(false);
7518 mWindow->consumeFocusEvent(false);
7519 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007520 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007521
7522 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007523 notifyPointerCaptureChanged({});
7524 notifyPointerCaptureChanged(request);
7525 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007526 mWindow->assertNoEvents();
7527 mSecondWindow->assertNoEvents();
7528 mFakePolicy->assertSetPointerCaptureNotCalled();
7529}
7530
7531TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007532 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007533
7534 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007535 notifyPointerCaptureChanged({});
7536 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007537
7538 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007539 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007540 mWindow->consumeCaptureEvent(false);
7541 mWindow->assertNoEvents();
7542}
7543
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007544TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7545 requestAndVerifyPointerCapture(mWindow, true);
7546
7547 // The first window loses focus.
7548 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007549 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007550 mWindow->consumeCaptureEvent(false);
7551
7552 // Request Pointer Capture from the second window before the notification from InputReader
7553 // arrives.
7554 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007555 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007556
7557 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007558 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007559
7560 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007561 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007562
7563 mSecondWindow->consumeFocusEvent(true);
7564 mSecondWindow->consumeCaptureEvent(true);
7565}
7566
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007567TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7568 // App repeatedly enables and disables capture.
7569 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7570 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7571 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7572 mFakePolicy->assertSetPointerCaptureCalled(false);
7573 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7574 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7575
7576 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7577 // first request is now stale, this should do nothing.
7578 notifyPointerCaptureChanged(firstRequest);
7579 mWindow->assertNoEvents();
7580
7581 // InputReader notifies that the second request was enabled.
7582 notifyPointerCaptureChanged(secondRequest);
7583 mWindow->consumeCaptureEvent(true);
7584}
7585
Prabir Pradhan7092e262022-05-03 16:51:09 +00007586TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7587 requestAndVerifyPointerCapture(mWindow, true);
7588
7589 // App toggles pointer capture off and on.
7590 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7591 mFakePolicy->assertSetPointerCaptureCalled(false);
7592
7593 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7594 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7595
7596 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7597 // preceding "disable" request.
7598 notifyPointerCaptureChanged(enableRequest);
7599
7600 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7601 // any notifications.
7602 mWindow->assertNoEvents();
7603}
7604
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007605class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7606protected:
7607 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007608
7609 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7610 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7611
7612 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7613 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7614
7615 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7616 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7617 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7618 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7619 MAXIMUM_OBSCURING_OPACITY);
7620
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007621 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007622 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007623 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007624
7625 sp<FakeWindowHandle> mTouchWindow;
7626
7627 virtual void SetUp() override {
7628 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007629 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007630 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7631 }
7632
7633 virtual void TearDown() override {
7634 InputDispatcherTest::TearDown();
7635 mTouchWindow.clear();
7636 }
7637
chaviw3277faf2021-05-19 16:45:23 -05007638 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7639 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007640 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007641 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007642 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007643 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007644 return window;
7645 }
7646
7647 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7648 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7649 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007650 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007651 // Generate an arbitrary PID based on the UID
7652 window->setOwnerInfo(1777 + (uid % 10000), uid);
7653 return window;
7654 }
7655
7656 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007657 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7658 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7659 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007660 }
7661};
7662
7663TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007664 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007665 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007666 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007667
7668 touch();
7669
7670 mTouchWindow->assertNoEvents();
7671}
7672
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007673TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007674 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7675 const sp<FakeWindowHandle>& w =
7676 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7677 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7678
7679 touch();
7680
7681 mTouchWindow->assertNoEvents();
7682}
7683
7684TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007685 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7686 const sp<FakeWindowHandle>& w =
7687 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7688 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7689
7690 touch();
7691
7692 w->assertNoEvents();
7693}
7694
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007695TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007696 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7697 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007698
7699 touch();
7700
7701 mTouchWindow->consumeAnyMotionDown();
7702}
7703
7704TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007705 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007706 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007707 w->setFrame(Rect(0, 0, 50, 50));
7708 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007709
7710 touch({PointF{100, 100}});
7711
7712 mTouchWindow->consumeAnyMotionDown();
7713}
7714
7715TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007716 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007717 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007718 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7719
7720 touch();
7721
7722 mTouchWindow->consumeAnyMotionDown();
7723}
7724
7725TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7726 const sp<FakeWindowHandle>& w =
7727 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7728 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007729
7730 touch();
7731
7732 mTouchWindow->consumeAnyMotionDown();
7733}
7734
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007735TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7736 const sp<FakeWindowHandle>& w =
7737 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7738 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7739
7740 touch();
7741
7742 w->assertNoEvents();
7743}
7744
7745/**
7746 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7747 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7748 * window, the occluding window will still receive ACTION_OUTSIDE event.
7749 */
7750TEST_F(InputDispatcherUntrustedTouchesTest,
7751 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7752 const sp<FakeWindowHandle>& w =
7753 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007754 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007755 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7756
7757 touch();
7758
7759 w->consumeMotionOutside();
7760}
7761
7762TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
7763 const sp<FakeWindowHandle>& w =
7764 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007765 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007766 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7767
7768 touch();
7769
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007770 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007771}
7772
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007773TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007774 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007775 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7776 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007777 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7778
7779 touch();
7780
7781 mTouchWindow->consumeAnyMotionDown();
7782}
7783
7784TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
7785 const sp<FakeWindowHandle>& w =
7786 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7787 MAXIMUM_OBSCURING_OPACITY);
7788 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007789
7790 touch();
7791
7792 mTouchWindow->consumeAnyMotionDown();
7793}
7794
7795TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007796 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007797 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7798 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007799 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7800
7801 touch();
7802
7803 mTouchWindow->assertNoEvents();
7804}
7805
7806TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
7807 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
7808 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007809 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7810 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007811 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007812 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7813 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007814 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7815
7816 touch();
7817
7818 mTouchWindow->assertNoEvents();
7819}
7820
7821TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
7822 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
7823 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007824 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7825 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007826 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007827 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7828 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007829 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7830
7831 touch();
7832
7833 mTouchWindow->consumeAnyMotionDown();
7834}
7835
7836TEST_F(InputDispatcherUntrustedTouchesTest,
7837 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
7838 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007839 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7840 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007841 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007842 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7843 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007844 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7845
7846 touch();
7847
7848 mTouchWindow->consumeAnyMotionDown();
7849}
7850
7851TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
7852 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007853 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7854 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007855 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007856 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7857 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007858 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007859
7860 touch();
7861
7862 mTouchWindow->assertNoEvents();
7863}
7864
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007865TEST_F(InputDispatcherUntrustedTouchesTest,
7866 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
7867 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007868 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7869 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007870 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007871 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7872 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007873 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7874
7875 touch();
7876
7877 mTouchWindow->assertNoEvents();
7878}
7879
7880TEST_F(InputDispatcherUntrustedTouchesTest,
7881 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
7882 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007883 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7884 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007885 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007886 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7887 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
7889
7890 touch();
7891
7892 mTouchWindow->consumeAnyMotionDown();
7893}
7894
7895TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
7896 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007897 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
7898 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007899 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7900
7901 touch();
7902
7903 mTouchWindow->consumeAnyMotionDown();
7904}
7905
7906TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
7907 const sp<FakeWindowHandle>& w =
7908 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
7909 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7910
7911 touch();
7912
7913 mTouchWindow->consumeAnyMotionDown();
7914}
7915
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007916TEST_F(InputDispatcherUntrustedTouchesTest,
7917 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
7918 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7919 const sp<FakeWindowHandle>& w =
7920 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
7921 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7922
7923 touch();
7924
7925 mTouchWindow->assertNoEvents();
7926}
7927
7928TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
7929 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
7930 const sp<FakeWindowHandle>& w =
7931 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
7932 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7933
7934 touch();
7935
7936 mTouchWindow->consumeAnyMotionDown();
7937}
7938
7939TEST_F(InputDispatcherUntrustedTouchesTest,
7940 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
7941 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
7942 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007943 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7944 OPACITY_ABOVE_THRESHOLD);
7945 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7946
7947 touch();
7948
7949 mTouchWindow->consumeAnyMotionDown();
7950}
7951
7952TEST_F(InputDispatcherUntrustedTouchesTest,
7953 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
7954 const sp<FakeWindowHandle>& w1 =
7955 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7956 OPACITY_BELOW_THRESHOLD);
7957 const sp<FakeWindowHandle>& w2 =
7958 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7959 OPACITY_BELOW_THRESHOLD);
7960 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7961
7962 touch();
7963
7964 mTouchWindow->assertNoEvents();
7965}
7966
7967/**
7968 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
7969 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
7970 * (which alone would result in allowing touches) does not affect the blocking behavior.
7971 */
7972TEST_F(InputDispatcherUntrustedTouchesTest,
7973 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
7974 const sp<FakeWindowHandle>& wB =
7975 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
7976 OPACITY_BELOW_THRESHOLD);
7977 const sp<FakeWindowHandle>& wC =
7978 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
7979 OPACITY_BELOW_THRESHOLD);
7980 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
7981
7982 touch();
7983
7984 mTouchWindow->assertNoEvents();
7985}
7986
7987/**
7988 * This test is testing that a window from a different UID but with same application token doesn't
7989 * block the touch. Apps can share the application token for close UI collaboration for example.
7990 */
7991TEST_F(InputDispatcherUntrustedTouchesTest,
7992 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
7993 const sp<FakeWindowHandle>& w =
7994 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7995 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00007996 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7997
7998 touch();
7999
8000 mTouchWindow->consumeAnyMotionDown();
8001}
8002
arthurhungb89ccb02020-12-30 16:19:01 +08008003class InputDispatcherDragTests : public InputDispatcherTest {
8004protected:
8005 std::shared_ptr<FakeApplicationHandle> mApp;
8006 sp<FakeWindowHandle> mWindow;
8007 sp<FakeWindowHandle> mSecondWindow;
8008 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008009 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008010 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8011 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008012
8013 void SetUp() override {
8014 InputDispatcherTest::SetUp();
8015 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008016 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008017 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008018
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008019 mSecondWindow =
8020 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008021 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008022
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008023 mSpyWindow =
8024 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008025 mSpyWindow->setSpy(true);
8026 mSpyWindow->setTrustedOverlay(true);
8027 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8028
arthurhungb89ccb02020-12-30 16:19:01 +08008029 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008030 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008031 }
8032
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008033 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8034 switch (fromSource) {
8035 case AINPUT_SOURCE_TOUCHSCREEN:
8036 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8037 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8038 ADISPLAY_ID_DEFAULT, {50, 50}))
8039 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8040 break;
8041 case AINPUT_SOURCE_STYLUS:
8042 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8043 injectMotionEvent(
8044 mDispatcher,
8045 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8046 AINPUT_SOURCE_STYLUS)
8047 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008048 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008049 .x(50)
8050 .y(50))
8051 .build()));
8052 break;
8053 case AINPUT_SOURCE_MOUSE:
8054 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8055 injectMotionEvent(
8056 mDispatcher,
8057 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8058 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8059 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008060 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008061 .x(50)
8062 .y(50))
8063 .build()));
8064 break;
8065 default:
8066 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8067 }
arthurhungb89ccb02020-12-30 16:19:01 +08008068
8069 // Window should receive motion event.
8070 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008071 // Spy window should also receive motion event
8072 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008073 }
8074
8075 // Start performing drag, we will create a drag window and transfer touch to it.
8076 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8077 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008078 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008079 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008080 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008081 }
arthurhungb89ccb02020-12-30 16:19:01 +08008082
8083 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008084 mDragWindow =
8085 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008086 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008087 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008088 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008089
8090 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008091 bool transferred =
8092 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008093 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008094 if (transferred) {
8095 mWindow->consumeMotionCancel();
8096 mDragWindow->consumeMotionDown();
8097 }
8098 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008099 }
8100};
8101
8102TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008103 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008104
8105 // Move on window.
8106 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8107 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8108 ADISPLAY_ID_DEFAULT, {50, 50}))
8109 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8110 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8111 mWindow->consumeDragEvent(false, 50, 50);
8112 mSecondWindow->assertNoEvents();
8113
8114 // Move to another window.
8115 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8116 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8117 ADISPLAY_ID_DEFAULT, {150, 50}))
8118 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8119 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8120 mWindow->consumeDragEvent(true, 150, 50);
8121 mSecondWindow->consumeDragEvent(false, 50, 50);
8122
8123 // Move back to original window.
8124 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8125 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8126 ADISPLAY_ID_DEFAULT, {50, 50}))
8127 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8128 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8129 mWindow->consumeDragEvent(false, 50, 50);
8130 mSecondWindow->consumeDragEvent(true, -50, 50);
8131
8132 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8133 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8134 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8135 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8136 mWindow->assertNoEvents();
8137 mSecondWindow->assertNoEvents();
8138}
8139
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008140TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008141 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008142
8143 // No cancel event after drag start
8144 mSpyWindow->assertNoEvents();
8145
8146 const MotionEvent secondFingerDownEvent =
8147 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8148 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008149 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8150 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008151 .build();
8152 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8153 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8154 InputEventInjectionSync::WAIT_FOR_RESULT))
8155 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8156
8157 // Receives cancel for first pointer after next pointer down
8158 mSpyWindow->consumeMotionCancel();
8159 mSpyWindow->consumeMotionDown();
8160
8161 mSpyWindow->assertNoEvents();
8162}
8163
arthurhungf452d0b2021-01-06 00:19:52 +08008164TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008165 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008166
8167 // Move on window.
8168 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8169 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8170 ADISPLAY_ID_DEFAULT, {50, 50}))
8171 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8172 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8173 mWindow->consumeDragEvent(false, 50, 50);
8174 mSecondWindow->assertNoEvents();
8175
8176 // Move to another window.
8177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8178 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8179 ADISPLAY_ID_DEFAULT, {150, 50}))
8180 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8181 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8182 mWindow->consumeDragEvent(true, 150, 50);
8183 mSecondWindow->consumeDragEvent(false, 50, 50);
8184
8185 // drop to another window.
8186 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8187 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8188 {150, 50}))
8189 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8190 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8191 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8192 mWindow->assertNoEvents();
8193 mSecondWindow->assertNoEvents();
8194}
8195
arthurhung6d4bed92021-03-17 11:59:33 +08008196TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008197 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008198
8199 // Move on window and keep button pressed.
8200 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8201 injectMotionEvent(mDispatcher,
8202 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8203 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008204 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008205 .build()))
8206 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8207 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8208 mWindow->consumeDragEvent(false, 50, 50);
8209 mSecondWindow->assertNoEvents();
8210
8211 // Move to another window and release button, expect to drop item.
8212 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8213 injectMotionEvent(mDispatcher,
8214 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8215 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008216 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008217 .build()))
8218 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8219 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8220 mWindow->assertNoEvents();
8221 mSecondWindow->assertNoEvents();
8222 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8223
8224 // nothing to the window.
8225 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8226 injectMotionEvent(mDispatcher,
8227 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8228 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008229 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008230 .build()))
8231 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8232 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8233 mWindow->assertNoEvents();
8234 mSecondWindow->assertNoEvents();
8235}
8236
Arthur Hung54745652022-04-20 07:17:41 +00008237TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008238 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008239
8240 // Set second window invisible.
8241 mSecondWindow->setVisible(false);
8242 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8243
8244 // Move on window.
8245 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8246 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8247 ADISPLAY_ID_DEFAULT, {50, 50}))
8248 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8249 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8250 mWindow->consumeDragEvent(false, 50, 50);
8251 mSecondWindow->assertNoEvents();
8252
8253 // Move to another window.
8254 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8255 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8256 ADISPLAY_ID_DEFAULT, {150, 50}))
8257 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8258 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8259 mWindow->consumeDragEvent(true, 150, 50);
8260 mSecondWindow->assertNoEvents();
8261
8262 // drop to another window.
8263 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8264 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8265 {150, 50}))
8266 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8267 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8268 mFakePolicy->assertDropTargetEquals(nullptr);
8269 mWindow->assertNoEvents();
8270 mSecondWindow->assertNoEvents();
8271}
8272
Arthur Hung54745652022-04-20 07:17:41 +00008273TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008274 // Ensure window could track pointerIds if it didn't support split touch.
8275 mWindow->setPreventSplitting(true);
8276
Arthur Hung54745652022-04-20 07:17:41 +00008277 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8278 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8279 {50, 50}))
8280 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8281 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8282
8283 const MotionEvent secondFingerDownEvent =
8284 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8285 .displayId(ADISPLAY_ID_DEFAULT)
8286 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008287 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8288 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008289 .build();
8290 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8291 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8292 InputEventInjectionSync::WAIT_FOR_RESULT))
8293 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008294 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008295
8296 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008297 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008298}
8299
8300TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8301 // First down on second window.
8302 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8303 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8304 {150, 50}))
8305 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8306
8307 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8308
8309 // Second down on first window.
8310 const MotionEvent secondFingerDownEvent =
8311 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8312 .displayId(ADISPLAY_ID_DEFAULT)
8313 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008314 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8315 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008316 .build();
8317 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8318 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8319 InputEventInjectionSync::WAIT_FOR_RESULT))
8320 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8321 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8322
8323 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008324 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008325
8326 // Move on window.
8327 const MotionEvent secondFingerMoveEvent =
8328 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8329 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008330 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8331 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008332 .build();
8333 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8334 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8335 InputEventInjectionSync::WAIT_FOR_RESULT));
8336 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8337 mWindow->consumeDragEvent(false, 50, 50);
8338 mSecondWindow->consumeMotionMove();
8339
8340 // Release the drag pointer should perform drop.
8341 const MotionEvent secondFingerUpEvent =
8342 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8343 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008344 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8345 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008346 .build();
8347 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8348 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8349 InputEventInjectionSync::WAIT_FOR_RESULT));
8350 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8351 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8352 mWindow->assertNoEvents();
8353 mSecondWindow->consumeMotionMove();
8354}
8355
Arthur Hung3915c1f2022-05-31 07:17:17 +00008356TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008357 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008358
8359 // Update window of second display.
8360 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008361 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008362 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8363
8364 // Let second display has a touch state.
8365 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8366 injectMotionEvent(mDispatcher,
8367 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8368 AINPUT_SOURCE_TOUCHSCREEN)
8369 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008370 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008371 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008372 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008373 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008374 // Update window again.
8375 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8376
8377 // Move on window.
8378 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8379 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8380 ADISPLAY_ID_DEFAULT, {50, 50}))
8381 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8382 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8383 mWindow->consumeDragEvent(false, 50, 50);
8384 mSecondWindow->assertNoEvents();
8385
8386 // Move to another window.
8387 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8388 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8389 ADISPLAY_ID_DEFAULT, {150, 50}))
8390 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8391 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8392 mWindow->consumeDragEvent(true, 150, 50);
8393 mSecondWindow->consumeDragEvent(false, 50, 50);
8394
8395 // drop to another window.
8396 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8397 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8398 {150, 50}))
8399 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8400 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8401 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8402 mWindow->assertNoEvents();
8403 mSecondWindow->assertNoEvents();
8404}
8405
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008406TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8407 startDrag(true, AINPUT_SOURCE_MOUSE);
8408 // Move on window.
8409 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8410 injectMotionEvent(mDispatcher,
8411 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8412 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8413 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008414 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008415 .x(50)
8416 .y(50))
8417 .build()))
8418 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8419 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8420 mWindow->consumeDragEvent(false, 50, 50);
8421 mSecondWindow->assertNoEvents();
8422
8423 // Move to another window.
8424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8425 injectMotionEvent(mDispatcher,
8426 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8427 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8428 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008429 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008430 .x(150)
8431 .y(50))
8432 .build()))
8433 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8434 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8435 mWindow->consumeDragEvent(true, 150, 50);
8436 mSecondWindow->consumeDragEvent(false, 50, 50);
8437
8438 // drop to another window.
8439 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8440 injectMotionEvent(mDispatcher,
8441 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8442 .buttonState(0)
8443 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008444 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008445 .x(150)
8446 .y(50))
8447 .build()))
8448 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8449 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8450 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8451 mWindow->assertNoEvents();
8452 mSecondWindow->assertNoEvents();
8453}
8454
Vishnu Nair062a8672021-09-03 16:07:44 -07008455class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8456
8457TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8458 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008459 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8460 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008461 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008462 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8463 window->setFocusable(true);
8464 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8465 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008466 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008467
8468 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008469 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008470 window->assertNoEvents();
8471
Prabir Pradhan678438e2023-04-13 19:32:51 +00008472 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8473 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008474 window->assertNoEvents();
8475
8476 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008477 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008478 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8479
Prabir Pradhan678438e2023-04-13 19:32:51 +00008480 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008481 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8482
Prabir Pradhan678438e2023-04-13 19:32:51 +00008483 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8484 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008485 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8486 window->assertNoEvents();
8487}
8488
8489TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8490 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8491 std::make_shared<FakeApplicationHandle>();
8492 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008493 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8494 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008495 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8496 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008497 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008498 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008499 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8500 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008501 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008502 window->setOwnerInfo(222, 222);
8503 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8504 window->setFocusable(true);
8505 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8506 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008507 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008508
8509 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008510 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008511 window->assertNoEvents();
8512
Prabir Pradhan678438e2023-04-13 19:32:51 +00008513 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8514 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008515 window->assertNoEvents();
8516
8517 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008518 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008519 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8520
Prabir Pradhan678438e2023-04-13 19:32:51 +00008521 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008522 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8523
Prabir Pradhan678438e2023-04-13 19:32:51 +00008524 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8525 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008526 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8527 window->assertNoEvents();
8528}
8529
8530TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8531 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8532 std::make_shared<FakeApplicationHandle>();
8533 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008534 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8535 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008536 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8537 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008538 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008539 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008540 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8541 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008542 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008543 window->setOwnerInfo(222, 222);
8544 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8545 window->setFocusable(true);
8546 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8547 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008548 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008549
8550 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008551 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008552 window->assertNoEvents();
8553
Prabir Pradhan678438e2023-04-13 19:32:51 +00008554 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8555 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008556 window->assertNoEvents();
8557
8558 // When the window is no longer obscured because it went on top, it should get input
8559 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8560
Prabir Pradhan678438e2023-04-13 19:32:51 +00008561 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008562 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8563
Prabir Pradhan678438e2023-04-13 19:32:51 +00008564 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8565 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008566 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8567 window->assertNoEvents();
8568}
8569
Antonio Kantekf16f2832021-09-28 04:39:20 +00008570class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8571protected:
8572 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008573 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008574 sp<FakeWindowHandle> mWindow;
8575 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008576 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008577
8578 void SetUp() override {
8579 InputDispatcherTest::SetUp();
8580
8581 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008582 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008583 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008584 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008585 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008586 mSecondWindow =
8587 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008588 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008589 mThirdWindow =
8590 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8591 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8592 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008593
8594 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008595 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8596 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8597 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008598 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008599
Antonio Kantek15beb512022-06-13 22:35:41 +00008600 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008601 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008602 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008603 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8604 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008605 mThirdWindow->assertNoEvents();
8606 }
8607
8608 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8609 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008610 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008611 SECOND_DISPLAY_ID)) {
8612 mWindow->assertNoEvents();
8613 mSecondWindow->assertNoEvents();
8614 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008615 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008616 }
8617
Antonio Kantek15beb512022-06-13 22:35:41 +00008618 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8619 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008620 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8621 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008622 mWindow->consumeTouchModeEvent(inTouchMode);
8623 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008624 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008625 }
8626};
8627
Antonio Kantek26defcf2022-02-08 01:12:27 +00008628TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008629 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008630 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8631 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008632 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008633}
8634
Antonio Kantek26defcf2022-02-08 01:12:27 +00008635TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8636 const WindowInfo& windowInfo = *mWindow->getInfo();
8637 int32_t ownerPid = windowInfo.ownerPid;
8638 int32_t ownerUid = windowInfo.ownerUid;
8639 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8640 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008641 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008642 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008643 mWindow->assertNoEvents();
8644 mSecondWindow->assertNoEvents();
8645}
8646
8647TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8648 const WindowInfo& windowInfo = *mWindow->getInfo();
8649 int32_t ownerPid = windowInfo.ownerPid;
8650 int32_t ownerUid = windowInfo.ownerUid;
8651 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008652 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008653 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008654}
8655
Antonio Kantekf16f2832021-09-28 04:39:20 +00008656TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008657 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008658 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8659 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008660 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008661 mWindow->assertNoEvents();
8662 mSecondWindow->assertNoEvents();
8663}
8664
Antonio Kantek15beb512022-06-13 22:35:41 +00008665TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8666 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8667 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8668 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008669 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008670 mWindow->assertNoEvents();
8671 mSecondWindow->assertNoEvents();
8672 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8673}
8674
Antonio Kantek48710e42022-03-24 14:19:30 -07008675TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8676 // Interact with the window first.
8677 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8678 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8679 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8680
8681 // Then remove focus.
8682 mWindow->setFocusable(false);
8683 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8684
8685 // Assert that caller can switch touch mode by owning one of the last interacted window.
8686 const WindowInfo& windowInfo = *mWindow->getInfo();
8687 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8688 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008689 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008690}
8691
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008692class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8693public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008694 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008695 std::shared_ptr<FakeApplicationHandle> application =
8696 std::make_shared<FakeApplicationHandle>();
8697 std::string name = "Fake Spy ";
8698 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008699 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8700 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008701 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008702 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008703 return spy;
8704 }
8705
8706 sp<FakeWindowHandle> createForeground() {
8707 std::shared_ptr<FakeApplicationHandle> application =
8708 std::make_shared<FakeApplicationHandle>();
8709 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008710 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8711 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008712 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008713 return window;
8714 }
8715
8716private:
8717 int mSpyCount{0};
8718};
8719
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008720using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008721/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008722 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8723 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008724TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8725 ScopedSilentDeath _silentDeath;
8726
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008727 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008728 spy->setTrustedOverlay(false);
8729 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8730 ".* not a trusted overlay");
8731}
8732
8733/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008734 * Input injection into a display with a spy window but no foreground windows should succeed.
8735 */
8736TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008737 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008738 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8739
8740 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8741 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8742 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8743 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8744}
8745
8746/**
8747 * Verify the order in which different input windows receive events. The touched foreground window
8748 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8749 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8750 * receive events before ones belows it.
8751 *
8752 * Here, we set up a scenario with four windows in the following Z order from the top:
8753 * spy1, spy2, window, spy3.
8754 * We then inject an event and verify that the foreground "window" receives it first, followed by
8755 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
8756 * window.
8757 */
8758TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
8759 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008760 auto spy1 = createSpy();
8761 auto spy2 = createSpy();
8762 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008763 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
8764 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
8765 const size_t numChannels = channels.size();
8766
Michael Wright8e9a8562022-02-09 13:44:29 +00008767 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008768 if (!epollFd.ok()) {
8769 FAIL() << "Failed to create epoll fd";
8770 }
8771
8772 for (size_t i = 0; i < numChannels; i++) {
8773 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
8774 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
8775 FAIL() << "Failed to add fd to epoll";
8776 }
8777 }
8778
8779 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8780 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8781 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8782
8783 std::vector<size_t> eventOrder;
8784 std::vector<struct epoll_event> events(numChannels);
8785 for (;;) {
8786 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
8787 (100ms).count());
8788 if (nFds < 0) {
8789 FAIL() << "Failed to call epoll_wait";
8790 }
8791 if (nFds == 0) {
8792 break; // epoll_wait timed out
8793 }
8794 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07008795 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07008796 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008797 channels[i]->consumeMotionDown();
8798 }
8799 }
8800
8801 // Verify the order in which the events were received.
8802 EXPECT_EQ(3u, eventOrder.size());
8803 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
8804 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
8805 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
8806}
8807
8808/**
8809 * A spy window using the NOT_TOUCHABLE flag does not receive events.
8810 */
8811TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
8812 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008813 auto spy = createSpy();
8814 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008815 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8816
8817 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8818 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8819 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8820 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8821 spy->assertNoEvents();
8822}
8823
8824/**
8825 * A spy window will only receive gestures that originate within its touchable region. Gestures that
8826 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
8827 * to the window.
8828 */
8829TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
8830 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008831 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008832 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
8833 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8834
8835 // Inject an event outside the spy window's touchable region.
8836 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8837 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8838 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8839 window->consumeMotionDown();
8840 spy->assertNoEvents();
8841 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8842 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8843 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8844 window->consumeMotionUp();
8845 spy->assertNoEvents();
8846
8847 // Inject an event inside the spy window's touchable region.
8848 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8849 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8850 {5, 10}))
8851 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8852 window->consumeMotionDown();
8853 spy->consumeMotionDown();
8854}
8855
8856/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008857 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008858 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008859 */
8860TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
8861 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008862 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008863 auto spy = createSpy();
8864 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008865 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008866 spy->setFrame(Rect{0, 0, 20, 20});
8867 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8868
8869 // Inject an event outside the spy window's frame and touchable region.
8870 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008871 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8872 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008873 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8874 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008875 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008876}
8877
8878/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008879 * Even when a spy window spans over multiple foreground windows, the spy should receive all
8880 * pointers that are down within its bounds.
8881 */
8882TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
8883 auto windowLeft = createForeground();
8884 windowLeft->setFrame({0, 0, 100, 200});
8885 auto windowRight = createForeground();
8886 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008887 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008888 spy->setFrame({0, 0, 200, 200});
8889 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
8890
8891 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8892 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8893 {50, 50}))
8894 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8895 windowLeft->consumeMotionDown();
8896 spy->consumeMotionDown();
8897
8898 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008899 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008900 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008901 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8902 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008903 .build();
8904 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8905 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8906 InputEventInjectionSync::WAIT_FOR_RESULT))
8907 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8908 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00008909 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008910}
8911
8912/**
8913 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
8914 * the spy should receive the second pointer with ACTION_DOWN.
8915 */
8916TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
8917 auto window = createForeground();
8918 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008919 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008920 spyRight->setFrame({100, 0, 200, 200});
8921 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
8922
8923 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8924 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8925 {50, 50}))
8926 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8927 window->consumeMotionDown();
8928 spyRight->assertNoEvents();
8929
8930 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008931 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008932 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008933 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8934 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008935 .build();
8936 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8937 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8938 InputEventInjectionSync::WAIT_FOR_RESULT))
8939 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008940 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008941 spyRight->consumeMotionDown();
8942}
8943
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008944/**
8945 * The spy window should not be able to affect whether or not touches are split. Only the foreground
8946 * windows should be allowed to control split touch.
8947 */
8948TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008949 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008950 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008951 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08008952 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008953
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008954 auto window = createForeground();
8955 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008956
8957 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8958
8959 // First finger down, no window touched.
8960 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8961 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8962 {100, 200}))
8963 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8964 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8965 window->assertNoEvents();
8966
8967 // Second finger down on window, the window should receive touch down.
8968 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008969 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008970 .displayId(ADISPLAY_ID_DEFAULT)
8971 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008972 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8973 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008974 .build();
8975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8976 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8977 InputEventInjectionSync::WAIT_FOR_RESULT))
8978 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8979
8980 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00008981 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008982}
8983
8984/**
8985 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
8986 * do not receive key events.
8987 */
8988TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008989 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008990 spy->setFocusable(false);
8991
8992 auto window = createForeground();
8993 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8994 setFocusedWindow(window);
8995 window->consumeFocusEvent(true);
8996
8997 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
8998 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8999 window->consumeKeyDown(ADISPLAY_ID_NONE);
9000
9001 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
9002 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9003 window->consumeKeyUp(ADISPLAY_ID_NONE);
9004
9005 spy->assertNoEvents();
9006}
9007
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009008using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9009
9010/**
9011 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9012 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9013 */
9014TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9015 auto window = createForeground();
9016 auto spy1 = createSpy();
9017 auto spy2 = createSpy();
9018 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
9019
9020 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9021 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9022 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9023 window->consumeMotionDown();
9024 spy1->consumeMotionDown();
9025 spy2->consumeMotionDown();
9026
9027 // Pilfer pointers from the second spy window.
9028 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9029 spy2->assertNoEvents();
9030 spy1->consumeMotionCancel();
9031 window->consumeMotionCancel();
9032
9033 // The rest of the gesture should only be sent to the second spy window.
9034 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9035 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9036 ADISPLAY_ID_DEFAULT))
9037 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9038 spy2->consumeMotionMove();
9039 spy1->assertNoEvents();
9040 window->assertNoEvents();
9041}
9042
9043/**
9044 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9045 * in the middle of the gesture.
9046 */
9047TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9048 auto window = createForeground();
9049 auto spy = createSpy();
9050 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9051
9052 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9053 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9054 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9055 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9056 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9057
9058 window->releaseChannel();
9059
9060 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9061
9062 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9063 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9064 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9065 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9066}
9067
9068/**
9069 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9070 * the spy, but not to any other windows.
9071 */
9072TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9073 auto spy = createSpy();
9074 auto window = createForeground();
9075
9076 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9077
9078 // First finger down on the window and the spy.
9079 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9080 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9081 {100, 200}))
9082 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9083 spy->consumeMotionDown();
9084 window->consumeMotionDown();
9085
9086 // Spy window pilfers the pointers.
9087 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9088 window->consumeMotionCancel();
9089
9090 // Second finger down on the window and spy, but the window should not receive the pointer down.
9091 const MotionEvent secondFingerDownEvent =
9092 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9093 .displayId(ADISPLAY_ID_DEFAULT)
9094 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009095 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9096 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009097 .build();
9098 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9099 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9100 InputEventInjectionSync::WAIT_FOR_RESULT))
9101 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9102
Harry Cutts33476232023-01-30 19:57:29 +00009103 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009104
9105 // Third finger goes down outside all windows, so injection should fail.
9106 const MotionEvent thirdFingerDownEvent =
9107 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9108 .displayId(ADISPLAY_ID_DEFAULT)
9109 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009110 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9111 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9112 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009113 .build();
9114 ASSERT_EQ(InputEventInjectionResult::FAILED,
9115 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9116 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009117 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009118
9119 spy->assertNoEvents();
9120 window->assertNoEvents();
9121}
9122
9123/**
9124 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9125 */
9126TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9127 auto spy = createSpy();
9128 spy->setFrame(Rect(0, 0, 100, 100));
9129 auto window = createForeground();
9130 window->setFrame(Rect(0, 0, 200, 200));
9131
9132 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9133
9134 // First finger down on the window only
9135 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9136 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9137 {150, 150}))
9138 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9139 window->consumeMotionDown();
9140
9141 // Second finger down on the spy and window
9142 const MotionEvent secondFingerDownEvent =
9143 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9144 .displayId(ADISPLAY_ID_DEFAULT)
9145 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009146 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9147 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009148 .build();
9149 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9150 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9151 InputEventInjectionSync::WAIT_FOR_RESULT))
9152 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9153 spy->consumeMotionDown();
9154 window->consumeMotionPointerDown(1);
9155
9156 // Third finger down on the spy and window
9157 const MotionEvent thirdFingerDownEvent =
9158 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9159 .displayId(ADISPLAY_ID_DEFAULT)
9160 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009161 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9162 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9163 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009164 .build();
9165 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9166 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9167 InputEventInjectionSync::WAIT_FOR_RESULT))
9168 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9169 spy->consumeMotionPointerDown(1);
9170 window->consumeMotionPointerDown(2);
9171
9172 // Spy window pilfers the pointers.
9173 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9174 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9175 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9176
9177 spy->assertNoEvents();
9178 window->assertNoEvents();
9179}
9180
9181/**
9182 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9183 * other windows should be canceled. If this results in the cancellation of all pointers for some
9184 * window, then that window should receive ACTION_CANCEL.
9185 */
9186TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9187 auto spy = createSpy();
9188 spy->setFrame(Rect(0, 0, 100, 100));
9189 auto window = createForeground();
9190 window->setFrame(Rect(0, 0, 200, 200));
9191
9192 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9193
9194 // First finger down on both spy and window
9195 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9196 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9197 {10, 10}))
9198 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9199 window->consumeMotionDown();
9200 spy->consumeMotionDown();
9201
9202 // Second finger down on the spy and window
9203 const MotionEvent secondFingerDownEvent =
9204 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9205 .displayId(ADISPLAY_ID_DEFAULT)
9206 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009207 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9208 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009209 .build();
9210 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9211 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9212 InputEventInjectionSync::WAIT_FOR_RESULT))
9213 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9214 spy->consumeMotionPointerDown(1);
9215 window->consumeMotionPointerDown(1);
9216
9217 // Spy window pilfers the pointers.
9218 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9219 window->consumeMotionCancel();
9220
9221 spy->assertNoEvents();
9222 window->assertNoEvents();
9223}
9224
9225/**
9226 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9227 * be sent to other windows
9228 */
9229TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9230 auto spy = createSpy();
9231 spy->setFrame(Rect(0, 0, 100, 100));
9232 auto window = createForeground();
9233 window->setFrame(Rect(0, 0, 200, 200));
9234
9235 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9236
9237 // First finger down on both window and spy
9238 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9239 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9240 {10, 10}))
9241 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9242 window->consumeMotionDown();
9243 spy->consumeMotionDown();
9244
9245 // Spy window pilfers the pointers.
9246 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9247 window->consumeMotionCancel();
9248
9249 // Second finger down on the window only
9250 const MotionEvent secondFingerDownEvent =
9251 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9252 .displayId(ADISPLAY_ID_DEFAULT)
9253 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009254 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9255 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009256 .build();
9257 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9258 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9259 InputEventInjectionSync::WAIT_FOR_RESULT))
9260 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9261 window->consumeMotionDown();
9262 window->assertNoEvents();
9263
9264 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9265 spy->consumeMotionMove();
9266 spy->assertNoEvents();
9267}
9268
Prabir Pradhand65552b2021-10-07 11:23:50 -07009269class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9270public:
9271 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9272 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9273 std::make_shared<FakeApplicationHandle>();
9274 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009275 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9276 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009277 overlay->setFocusable(false);
9278 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009279 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009280 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009281 overlay->setTrustedOverlay(true);
9282
9283 std::shared_ptr<FakeApplicationHandle> application =
9284 std::make_shared<FakeApplicationHandle>();
9285 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009286 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9287 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009288 window->setFocusable(true);
9289 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009290
9291 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9292 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9293 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009294 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009295 return {std::move(overlay), std::move(window)};
9296 }
9297
9298 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009299 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009300 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009301 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009302 }
9303
9304 void sendStylusEvent(int32_t action) {
9305 NotifyMotionArgs motionArgs =
9306 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9307 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009308 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009309 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009310 }
9311};
9312
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009313using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9314
9315TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9316 ScopedSilentDeath _silentDeath;
9317
Prabir Pradhand65552b2021-10-07 11:23:50 -07009318 auto [overlay, window] = setupStylusOverlayScenario();
9319 overlay->setTrustedOverlay(false);
9320 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9321 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9322 ".* not a trusted overlay");
9323}
9324
9325TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9326 auto [overlay, window] = setupStylusOverlayScenario();
9327 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9328
9329 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9330 overlay->consumeMotionDown();
9331 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9332 overlay->consumeMotionUp();
9333
9334 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9335 window->consumeMotionDown();
9336 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9337 window->consumeMotionUp();
9338
9339 overlay->assertNoEvents();
9340 window->assertNoEvents();
9341}
9342
9343TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9344 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009345 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009346 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9347
9348 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9349 overlay->consumeMotionDown();
9350 window->consumeMotionDown();
9351 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9352 overlay->consumeMotionUp();
9353 window->consumeMotionUp();
9354
9355 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9356 window->consumeMotionDown();
9357 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9358 window->consumeMotionUp();
9359
9360 overlay->assertNoEvents();
9361 window->assertNoEvents();
9362}
9363
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009364/**
9365 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9366 * The scenario is as follows:
9367 * - The stylus interceptor overlay is configured as a spy window.
9368 * - The stylus interceptor spy receives the start of a new stylus gesture.
9369 * - It pilfers pointers and then configures itself to no longer be a spy.
9370 * - The stylus interceptor continues to receive the rest of the gesture.
9371 */
9372TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9373 auto [overlay, window] = setupStylusOverlayScenario();
9374 overlay->setSpy(true);
9375 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9376
9377 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9378 overlay->consumeMotionDown();
9379 window->consumeMotionDown();
9380
9381 // The interceptor pilfers the pointers.
9382 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9383 window->consumeMotionCancel();
9384
9385 // The interceptor configures itself so that it is no longer a spy.
9386 overlay->setSpy(false);
9387 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9388
9389 // It continues to receive the rest of the stylus gesture.
9390 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9391 overlay->consumeMotionMove();
9392 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9393 overlay->consumeMotionUp();
9394
9395 window->assertNoEvents();
9396}
9397
Prabir Pradhan5735a322022-04-11 17:23:34 +00009398struct User {
9399 int32_t mPid;
9400 int32_t mUid;
9401 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9402 std::unique_ptr<InputDispatcher>& mDispatcher;
9403
9404 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9405 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9406
9407 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9408 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9409 ADISPLAY_ID_DEFAULT, {100, 200},
9410 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9411 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9412 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9413 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9414 }
9415
9416 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009417 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009418 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009419 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009420 mPolicyFlags);
9421 }
9422
9423 sp<FakeWindowHandle> createWindow() const {
9424 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9425 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009426 sp<FakeWindowHandle> window =
9427 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9428 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009429 window->setOwnerInfo(mPid, mUid);
9430 return window;
9431 }
9432};
9433
9434using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9435
9436TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9437 auto owner = User(mDispatcher, 10, 11);
9438 auto window = owner.createWindow();
9439 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9440
9441 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9442 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9443 window->consumeMotionDown();
9444
9445 setFocusedWindow(window);
9446 window->consumeFocusEvent(true);
9447
9448 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9449 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9450 window->consumeKeyDown(ADISPLAY_ID_NONE);
9451}
9452
9453TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9454 auto owner = User(mDispatcher, 10, 11);
9455 auto window = owner.createWindow();
9456 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9457
9458 auto rando = User(mDispatcher, 20, 21);
9459 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9460 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9461
9462 setFocusedWindow(window);
9463 window->consumeFocusEvent(true);
9464
9465 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9466 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9467 window->assertNoEvents();
9468}
9469
9470TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9471 auto owner = User(mDispatcher, 10, 11);
9472 auto window = owner.createWindow();
9473 auto spy = owner.createWindow();
9474 spy->setSpy(true);
9475 spy->setTrustedOverlay(true);
9476 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9477
9478 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9479 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9480 spy->consumeMotionDown();
9481 window->consumeMotionDown();
9482}
9483
9484TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9485 auto owner = User(mDispatcher, 10, 11);
9486 auto window = owner.createWindow();
9487
9488 auto rando = User(mDispatcher, 20, 21);
9489 auto randosSpy = rando.createWindow();
9490 randosSpy->setSpy(true);
9491 randosSpy->setTrustedOverlay(true);
9492 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9493
9494 // The event is targeted at owner's window, so injection should succeed, but the spy should
9495 // not receive the event.
9496 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9497 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9498 randosSpy->assertNoEvents();
9499 window->consumeMotionDown();
9500}
9501
9502TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9503 auto owner = User(mDispatcher, 10, 11);
9504 auto window = owner.createWindow();
9505
9506 auto rando = User(mDispatcher, 20, 21);
9507 auto randosSpy = rando.createWindow();
9508 randosSpy->setSpy(true);
9509 randosSpy->setTrustedOverlay(true);
9510 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9511
9512 // A user that has injection permission can inject into any window.
9513 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9514 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9515 ADISPLAY_ID_DEFAULT));
9516 randosSpy->consumeMotionDown();
9517 window->consumeMotionDown();
9518
9519 setFocusedWindow(randosSpy);
9520 randosSpy->consumeFocusEvent(true);
9521
9522 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9523 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9524 window->assertNoEvents();
9525}
9526
9527TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9528 auto owner = User(mDispatcher, 10, 11);
9529 auto window = owner.createWindow();
9530
9531 auto rando = User(mDispatcher, 20, 21);
9532 auto randosWindow = rando.createWindow();
9533 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9534 randosWindow->setWatchOutsideTouch(true);
9535 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9536
9537 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9538 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9539 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9540 window->consumeMotionDown();
9541 randosWindow->consumeMotionOutside();
9542}
9543
Garfield Tane84e6f92019-08-29 17:28:41 -07009544} // namespace android::inputdispatcher