blob: dd003a692189433192781858e8a716fdd217d761 [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 Pradhan8ede1d12023-05-08 19:37:44 +000018#include "../BlockingQueue.h"
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070019#include "TestInputListenerMatchers.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080020
Cody Heiner166a5af2023-07-07 12:25:00 -070021#include <NotifyArgsBuilders.h>
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070022#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080023#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080024#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070025#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070026#include <binder/Binder.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000027#include <fcntl.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080028#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100030#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080032#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100033
Garfield Tan1c7bc862020-01-28 13:24:04 -080034#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080035#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070036#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080037#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080038#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080039
Garfield Tan1c7bc862020-01-28 13:24:04 -080040using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050041using android::gui::FocusRequest;
42using android::gui::TouchOcclusionMode;
43using android::gui::WindowInfo;
44using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080045using android::os::InputEventInjectionResult;
46using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080047
Garfield Tane84e6f92019-08-29 17:28:41 -070048namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080049
Dominik Laskowski2f01d772022-03-23 16:01:29 -070050using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080051using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070052
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080055
56// An arbitrary device id.
Prabir Pradhan9205e422023-05-16 20:06:13 +000057static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080058static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080059
Jeff Brownf086ddb2014-02-11 14:28:48 -080060// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000061static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
62static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080063
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000064// Ensure common actions are interchangeable between keys and motions for convenience.
65static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN);
66static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080067static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
68static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
69static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
70static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -070071static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080072static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080073static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080074static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080075/**
76 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
77 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
78 * index 0) is the new pointer going down. The same pointer could have been placed at a different
79 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
80 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
81 * pointer id=0 leaves but the pointer id=1 remains.
82 */
83static constexpr int32_t POINTER_0_DOWN =
84 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080085static constexpr int32_t POINTER_1_DOWN =
86 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000087static constexpr int32_t POINTER_2_DOWN =
88 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000089static constexpr int32_t POINTER_3_DOWN =
90 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000091static constexpr int32_t POINTER_0_UP =
92 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080093static constexpr int32_t POINTER_1_UP =
94 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Harry Cuttsb166c002023-05-09 13:06:05 +000095static constexpr int32_t POINTER_2_UP =
96 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080097
Antonio Kantek15beb512022-06-13 22:35:41 +000098// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000099static constexpr gui::Pid WINDOW_PID{999};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000100static constexpr gui::Uid WINDOW_UID{1001};
Prabir Pradhan5735a322022-04-11 17:23:34 +0000101
Antonio Kantek15beb512022-06-13 22:35:41 +0000102// The default pid and uid for the windows created on the secondary display by the test.
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000103static constexpr gui::Pid SECONDARY_WINDOW_PID{1010};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000104static constexpr gui::Uid SECONDARY_WINDOW_UID{1012};
Antonio Kantek15beb512022-06-13 22:35:41 +0000105
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000106// An arbitrary pid of the gesture monitor window
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000107static constexpr gui::Pid MONITOR_PID{2001};
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000108
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800109static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
110
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700111/**
112 * If we expect to receive the event, the timeout can be made very long. When the test are running
113 * correctly, we will actually never wait until the end of the timeout because the wait will end
114 * when the event comes in. Still, this value shouldn't be infinite. During development, a local
115 * change may cause the test to fail. This timeout should be short enough to not annoy so that the
116 * developer can see the failure quickly (on human scale).
117 */
118static constexpr std::chrono::duration CONSUME_TIMEOUT_EVENT_EXPECTED = 1000ms;
119/**
120 * When no event is expected, we can have a very short timeout. A large value here would slow down
121 * the tests. In the unlikely event of system being too slow, the event may still be present but the
122 * timeout would complete before it is consumed. This would result in test flakiness. If this
123 * occurs, the flakiness rate would be high. Since the flakes are treated with high priority, this
124 * would get noticed and addressed quickly.
125 */
126static constexpr std::chrono::duration CONSUME_TIMEOUT_NO_EVENT_EXPECTED = 10ms;
127
Arthur Hungc539dbb2022-12-08 07:45:36 +0000128static constexpr int expectedWallpaperFlags =
129 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
130
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800131using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
132
chaviwd1c23182019-12-20 18:44:56 -0800133struct PointF {
134 float x;
135 float y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800136 auto operator<=>(const PointF&) const = default;
chaviwd1c23182019-12-20 18:44:56 -0800137};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138
Gang Wang342c9272020-01-13 13:15:04 -0500139/**
140 * Return a DOWN key event with KEYCODE_A.
141 */
142static KeyEvent getTestKeyEvent() {
143 KeyEvent event;
144
Garfield Tanfbe732e2020-01-24 11:26:14 -0800145 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
146 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
147 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500148 return event;
149}
150
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000151static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
152 ASSERT_EQ(expectedAction, receivedAction)
153 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
154 << MotionEvent::actionToString(receivedAction);
155}
156
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800157MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
158 return arg.getDownTime() == downTime;
159}
160
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800161MATCHER_P(WithSource, source, "InputEvent with specified source") {
162 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
163 << inputEventSourceToString(arg.getSource());
164 return arg.getSource() == source;
165}
166
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800167MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
168 return arg.getFlags() == flags;
169}
170
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800171MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
172 if (arg.getPointerCount() != 1) {
173 *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
174 return false;
175 }
Harry Cutts33476232023-01-30 19:57:29 +0000176 return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800177}
178
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800179MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") {
180 return arg.getPointerCount() == pointerCount;
181}
182
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800183MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
184 // Build a map for the received pointers, by pointer id
185 std::map<int32_t /*pointerId*/, PointF> actualPointers;
186 for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
187 const int32_t pointerId = arg.getPointerId(pointerIndex);
188 actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
189 }
190 return pointers == actualPointers;
191}
192
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193// --- FakeInputDispatcherPolicy ---
194
195class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000196 struct AnrResult {
197 sp<IBinder> token{};
198 gui::Pid pid{gui::Pid::INVALID};
199 };
Prabir Pradhanedd96402022-02-15 01:46:16 -0800200
Michael Wrightd02c5b62014-02-10 15:10:22 -0800201public:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000202 FakeInputDispatcherPolicy() = default;
203 virtual ~FakeInputDispatcherPolicy() = default;
Jackal Guof9696682018-10-05 12:23:23 +0800204
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800205 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700206 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700207 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700208 EXPECT_EQ(event.getDisplayId(), args.displayId);
209
210 const auto& keyEvent = static_cast<const KeyEvent&>(event);
211 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
212 EXPECT_EQ(keyEvent.getAction(), args.action);
213 });
Jackal Guof9696682018-10-05 12:23:23 +0800214 }
215
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700216 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
217 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700218 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700219 EXPECT_EQ(event.getDisplayId(), args.displayId);
220
221 const auto& motionEvent = static_cast<const MotionEvent&>(event);
222 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
223 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000224 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
225 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
226 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
227 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700228 });
Jackal Guof9696682018-10-05 12:23:23 +0800229 }
230
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700231 void assertFilterInputEventWasNotCalled() {
232 std::scoped_lock lock(mLock);
233 ASSERT_EQ(nullptr, mFilteredEvent);
234 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800235
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800236 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700237 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800238 ASSERT_TRUE(mConfigurationChangedTime)
239 << "Timed out waiting for configuration changed call";
240 ASSERT_EQ(*mConfigurationChangedTime, when);
241 mConfigurationChangedTime = std::nullopt;
242 }
243
244 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700245 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800246 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800247 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800248 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
249 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
250 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
251 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
252 mLastNotifySwitch = std::nullopt;
253 }
254
chaviwfd6d3512019-03-25 13:23:49 -0700255 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700256 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800257 ASSERT_EQ(touchedToken, mOnPointerDownToken);
258 mOnPointerDownToken.clear();
259 }
260
261 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700262 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800263 ASSERT_TRUE(mOnPointerDownToken == nullptr)
264 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700265 }
266
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700267 // This function must be called soon after the expected ANR timer starts,
268 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500269 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700270 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500271 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800272 std::unique_lock lock(mLock);
273 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500274 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800275 ASSERT_NO_FATAL_FAILURE(
276 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500277 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700278 }
279
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000280 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800281 const sp<WindowInfoHandle>& window) {
282 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
283 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
284 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500285 }
286
Prabir Pradhanedd96402022-02-15 01:46:16 -0800287 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
288 const sp<IBinder>& expectedToken,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000289 gui::Pid expectedPid) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800290 std::unique_lock lock(mLock);
291 android::base::ScopedLockAssertion assumeLocked(mLock);
292 AnrResult result;
293 ASSERT_NO_FATAL_FAILURE(result =
294 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000295 ASSERT_EQ(expectedToken, result.token);
296 ASSERT_EQ(expectedPid, result.pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500297 }
298
Prabir Pradhanedd96402022-02-15 01:46:16 -0800299 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000300 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500301 std::unique_lock lock(mLock);
302 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800303 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
304 const auto& [token, _] = result;
305 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000306 }
307
Prabir Pradhanedd96402022-02-15 01:46:16 -0800308 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000309 gui::Pid expectedPid) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800310 std::unique_lock lock(mLock);
311 android::base::ScopedLockAssertion assumeLocked(mLock);
312 AnrResult result;
313 ASSERT_NO_FATAL_FAILURE(
314 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000315 ASSERT_EQ(expectedToken, result.token);
316 ASSERT_EQ(expectedPid, result.pid);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800317 }
318
319 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000320 sp<IBinder> getResponsiveWindowToken() {
321 std::unique_lock lock(mLock);
322 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800323 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
324 const auto& [token, _] = result;
325 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700326 }
327
328 void assertNotifyAnrWasNotCalled() {
329 std::scoped_lock lock(mLock);
330 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800331 ASSERT_TRUE(mAnrWindows.empty());
332 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500333 << "ANR was not called, but please also consume the 'connection is responsive' "
334 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700335 }
336
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000337 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800338 std::unique_lock lock(mLock);
339 base::ScopedLockAssertion assumeLocked(mLock);
340
341 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
342 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000343 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800344 enabled;
345 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000346 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
347 << ") to be called.";
348 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800349 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000350 auto request = *mPointerCaptureRequest;
351 mPointerCaptureRequest.reset();
352 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800353 }
354
355 void assertSetPointerCaptureNotCalled() {
356 std::unique_lock lock(mLock);
357 base::ScopedLockAssertion assumeLocked(mLock);
358
359 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000360 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800361 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000362 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800363 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000364 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800365 }
366
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -0700367 void assertDropTargetEquals(const InputDispatcherInterface& dispatcher,
368 const sp<IBinder>& targetToken) {
369 dispatcher.waitForIdle();
arthurhungf452d0b2021-01-06 00:19:52 +0800370 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800371 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800372 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800373 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800374 }
375
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800376 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
377 std::unique_lock lock(mLock);
378 base::ScopedLockAssertion assumeLocked(mLock);
379 std::optional<sp<IBinder>> receivedToken =
380 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
381 mNotifyInputChannelBroken);
382 ASSERT_TRUE(receivedToken.has_value());
383 ASSERT_EQ(token, *receivedToken);
384 }
385
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800386 /**
387 * Set policy timeout. A value of zero means next key will not be intercepted.
388 */
389 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
390 mInterceptKeyTimeout = timeout;
391 }
392
Josep del Riob3981622023-04-18 15:49:45 +0000393 void assertUserActivityPoked() {
394 std::scoped_lock lock(mLock);
395 ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked";
396 }
397
398 void assertUserActivityNotPoked() {
399 std::scoped_lock lock(mLock);
400 ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked";
401 }
402
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000403 void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) {
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000404 ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms));
405 }
406
407 void assertNotifyDeviceInteractionWasNotCalled() {
408 ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms));
409 }
410
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700412 std::mutex mLock;
413 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
414 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
415 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
416 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800417
Prabir Pradhan99987712020-11-10 18:43:05 -0800418 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000419
420 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800421
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700422 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700423 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800424 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
425 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700426 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800427 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
428 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700429
arthurhungf452d0b2021-01-06 00:19:52 +0800430 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800431 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Josep del Riob3981622023-04-18 15:49:45 +0000432 bool mPokedUserActivity GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800433
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800434 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
435
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000436 BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000437
Prabir Pradhanedd96402022-02-15 01:46:16 -0800438 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
439 // for a specific container to become non-empty. When the container is non-empty, return the
440 // first entry from the container and erase it.
441 template <class T>
442 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
443 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
444 // If there is an ANR, Dispatcher won't be idle because there are still events
445 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
446 // before checking if ANR was called.
447 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
448 // to provide it some time to act. 100ms seems reasonable.
449 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
450 const std::chrono::time_point start = std::chrono::steady_clock::now();
451 std::optional<T> token =
452 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
453 if (!token.has_value()) {
454 ADD_FAILURE() << "Did not receive the ANR callback";
455 return {};
456 }
457
458 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
459 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
460 // the dispatcher started counting before this function was called
461 if (std::chrono::abs(timeout - waited) > 100ms) {
462 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
463 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
464 << "ms, but waited "
465 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
466 << "ms instead";
467 }
468 return *token;
469 }
470
471 template <class T>
472 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
473 std::queue<T>& storage,
474 std::unique_lock<std::mutex>& lock,
475 std::condition_variable& condition)
476 REQUIRES(mLock) {
477 condition.wait_for(lock, timeout,
478 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
479 if (storage.empty()) {
480 ADD_FAILURE() << "Did not receive the expected callback";
481 return std::nullopt;
482 }
483 T item = storage.front();
484 storage.pop();
485 return std::make_optional(item);
486 }
487
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600488 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700489 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800490 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491 }
492
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000493 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800494 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700495 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800496 ASSERT_TRUE(pid.has_value());
497 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700498 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500499 }
500
Prabir Pradhanedd96402022-02-15 01:46:16 -0800501 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000502 std::optional<gui::Pid> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500503 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800504 ASSERT_TRUE(pid.has_value());
505 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500506 mNotifyAnr.notify_all();
507 }
508
509 void notifyNoFocusedWindowAnr(
510 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
511 std::scoped_lock lock(mLock);
512 mAnrApplications.push(applicationHandle);
513 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 }
515
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800516 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
517 std::scoped_lock lock(mLock);
518 mBrokenInputChannels.push(connectionToken);
519 mNotifyInputChannelBroken.notify_all();
520 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800521
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600522 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700523
Chris Yef59a2f42020-10-16 12:55:26 -0700524 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
525 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
526 const std::vector<float>& values) override {}
527
528 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
529 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000530
Chris Yefb552902021-02-03 17:18:37 -0800531 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
532
Prabir Pradhana41d2442023-04-20 21:30:40 +0000533 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700534 std::scoped_lock lock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +0000535 switch (inputEvent.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700536 case InputEventType::KEY: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000537 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent);
538 mFilteredEvent = std::make_unique<KeyEvent>(keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800539 break;
540 }
541
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700542 case InputEventType::MOTION: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000543 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent);
544 mFilteredEvent = std::make_unique<MotionEvent>(motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800545 break;
546 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700547 default: {
548 ADD_FAILURE() << "Should only filter keys or motions";
549 break;
550 }
Jackal Guof9696682018-10-05 12:23:23 +0800551 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800552 return true;
553 }
554
Prabir Pradhana41d2442023-04-20 21:30:40 +0000555 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override {
556 if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800557 // Clear intercept state when we handled the event.
558 mInterceptKeyTimeout = 0ms;
559 }
560 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800561
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600562 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800563
Prabir Pradhana41d2442023-04-20 21:30:40 +0000564 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800565 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
566 // Clear intercept state so we could dispatch the event in next wake.
567 mInterceptKeyTimeout = 0ms;
568 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800569 }
570
Prabir Pradhana41d2442023-04-20 21:30:40 +0000571 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&,
572 uint32_t) override {
573 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 }
575
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600576 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
577 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700578 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800579 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
580 * essentially a passthrough for notifySwitch.
581 */
Harry Cutts33476232023-01-30 19:57:29 +0000582 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800583 }
584
Josep del Riob3981622023-04-18 15:49:45 +0000585 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {
586 std::scoped_lock lock(mLock);
587 mPokedUserActivity = true;
588 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600590 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700591 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700592 mOnPointerDownToken = newToken;
593 }
594
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000595 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800596 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000597 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800598 mPointerCaptureChangedCondition.notify_all();
599 }
600
arthurhungf452d0b2021-01-06 00:19:52 +0800601 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
602 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800603 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800604 mDropTargetWindowToken = token;
605 }
606
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000607 void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000608 const std::set<gui::Uid>& uids) override {
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000609 ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids));
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);
Harry Cutts101ee9b2023-07-06 18:04:14 +0000631 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,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000672 /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600673 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,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000708 /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700709 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,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000712 /*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,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000724 /*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,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000736 /*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,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000748 /*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,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000760 /*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,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000772 /*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,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000783 /*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,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000796 /*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,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000808 /*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,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000822 /*pointerCount=*/2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800823 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000824 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000825 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826 << "Should reject motion events with duplicate pointer ids.";
827}
828
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800829/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
830
831TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
832 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000833 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800834 ASSERT_TRUE(mDispatcher->waitForIdle());
835
836 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
837}
838
839TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000840 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
841 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000842 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800843
844 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
845 args.policyFlags |= POLICY_FLAG_TRUSTED;
846 mFakePolicy->assertNotifySwitchWasCalled(args);
847}
848
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -0700849namespace {
850
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700851static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700852// Default input dispatching timeout if there is no focused application or paused window
853// from which to determine an appropriate dispatching timeout.
854static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
855 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
856 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800857
858class FakeApplicationHandle : public InputApplicationHandle {
859public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700860 FakeApplicationHandle() {
861 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700862 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500863 mInfo.dispatchingTimeoutMillis =
864 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700865 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800866 virtual ~FakeApplicationHandle() {}
867
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000868 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700869
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500870 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
871 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700872 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800873};
874
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800875class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800876public:
Garfield Tan15601662020-09-22 15:32:38 -0700877 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800878 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700879 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800880 }
881
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700882 InputEvent* consume(std::chrono::milliseconds timeout) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700883 InputEvent* event;
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700884 std::optional<uint32_t> consumeSeq = receiveEvent(timeout, &event);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700885 if (!consumeSeq) {
886 return nullptr;
887 }
888 finishEvent(*consumeSeq);
889 return event;
890 }
891
892 /**
893 * Receive an event without acknowledging it.
894 * Return the sequence number that could later be used to send finished signal.
895 */
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700896 std::optional<uint32_t> receiveEvent(std::chrono::milliseconds timeout,
897 InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800898 uint32_t consumeSeq;
899 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800900
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800901 std::chrono::time_point start = std::chrono::steady_clock::now();
902 status_t status = WOULD_BLOCK;
903 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000904 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800905 &event);
906 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700907 if (elapsed > timeout) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800908 break;
909 }
910 }
911
912 if (status == WOULD_BLOCK) {
913 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700914 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800915 }
916
917 if (status != OK) {
918 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700919 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800920 }
921 if (event == nullptr) {
922 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700923 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800924 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700925 if (outEvent != nullptr) {
926 *outEvent = event;
927 }
928 return consumeSeq;
929 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800930
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700931 /**
932 * To be used together with "receiveEvent" to complete the consumption of an event.
933 */
934 void finishEvent(uint32_t consumeSeq) {
935 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
936 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800937 }
938
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000939 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
940 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
941 ASSERT_EQ(OK, status);
942 }
943
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700944 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000945 std::optional<int32_t> expectedDisplayId,
946 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700947 InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800948
949 ASSERT_NE(nullptr, event) << mName.c_str()
950 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800951 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700952 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
953 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800954
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000955 if (expectedDisplayId.has_value()) {
956 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
957 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800958
Tiger Huang8664f8c2018-10-11 19:14:35 +0800959 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700960 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800961 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
962 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000963 if (expectedFlags.has_value()) {
964 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
965 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800966 break;
967 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700968 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800969 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000970 assertMotionAction(expectedAction, motionEvent.getAction());
971
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000972 if (expectedFlags.has_value()) {
973 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
974 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800975 break;
976 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700977 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100978 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
979 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700980 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -0800981 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
982 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700983 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +0000984 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
985 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700986 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +0800987 FAIL() << "Use 'consumeDragEvent' for DRAG events";
988 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800989 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800990 }
991
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800992 MotionEvent* consumeMotion() {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700993 InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800994
995 if (event == nullptr) {
996 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
997 return nullptr;
998 }
999
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001000 if (event->getType() != InputEventType::MOTION) {
1001 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001002 return nullptr;
1003 }
1004 return static_cast<MotionEvent*>(event);
1005 }
1006
1007 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1008 MotionEvent* motionEvent = consumeMotion();
1009 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1010 ASSERT_THAT(*motionEvent, matcher);
1011 }
1012
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001013 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001014 InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001015 ASSERT_NE(nullptr, event) << mName.c_str()
1016 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001017 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1018 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001019
1020 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1021 << mName.c_str() << ": event displayId should always be NONE.";
1022
1023 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1024 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001025 }
1026
Prabir Pradhan99987712020-11-10 18:43:05 -08001027 void consumeCaptureEvent(bool hasCapture) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001028 const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Prabir Pradhan99987712020-11-10 18:43:05 -08001029 ASSERT_NE(nullptr, event) << mName.c_str()
1030 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001031 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1032 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001033
1034 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1035 << mName.c_str() << ": event displayId should always be NONE.";
1036
1037 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1038 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1039 }
1040
arthurhungb89ccb02020-12-30 16:19:01 +08001041 void consumeDragEvent(bool isExiting, float x, float y) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001042 const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
arthurhungb89ccb02020-12-30 16:19:01 +08001043 ASSERT_NE(nullptr, event) << mName.c_str()
1044 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001045 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001046
1047 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1048 << mName.c_str() << ": event displayId should always be NONE.";
1049
1050 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1051 EXPECT_EQ(isExiting, dragEvent.isExiting());
1052 EXPECT_EQ(x, dragEvent.getX());
1053 EXPECT_EQ(y, dragEvent.getY());
1054 }
1055
Antonio Kantekf16f2832021-09-28 04:39:20 +00001056 void consumeTouchModeEvent(bool inTouchMode) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001057 const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Antonio Kantekf16f2832021-09-28 04:39:20 +00001058 ASSERT_NE(nullptr, event) << mName.c_str()
1059 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001060 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1061 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001062
1063 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1064 << mName.c_str() << ": event displayId should always be NONE.";
1065 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1066 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1067 }
1068
chaviwd1c23182019-12-20 18:44:56 -08001069 void assertNoEvents() {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001070 InputEvent* event = consume(CONSUME_TIMEOUT_NO_EVENT_EXPECTED);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001071 if (event == nullptr) {
1072 return;
1073 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001074 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001075 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1076 ADD_FAILURE() << "Received key event "
1077 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001078 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001079 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1080 ADD_FAILURE() << "Received motion event "
1081 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001082 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001083 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1084 ADD_FAILURE() << "Received focus event, hasFocus = "
1085 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001086 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001087 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1088 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1089 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001090 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001091 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1092 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1093 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001094 }
1095 FAIL() << mName.c_str()
1096 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001097 }
1098
1099 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1100
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001101 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1102
chaviwd1c23182019-12-20 18:44:56 -08001103protected:
1104 std::unique_ptr<InputConsumer> mConsumer;
1105 PreallocatedInputEventFactory mEventFactory;
1106
1107 std::string mName;
1108};
1109
chaviw3277faf2021-05-19 16:45:23 -05001110class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001111public:
1112 static const int32_t WIDTH = 600;
1113 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001114
Chris Yea209fde2020-07-22 13:54:51 -07001115 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001116 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001117 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001118 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001119 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001120 base::Result<std::unique_ptr<InputChannel>> channel =
1121 dispatcher->createInputChannel(name);
1122 token = (*channel)->getConnectionToken();
1123 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001124 }
1125
1126 inputApplicationHandle->updateInfo();
1127 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1128
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001129 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001130 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001131 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001132 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001133 mInfo.alpha = 1.0;
Chavi Weingarten7f019192023-08-08 20:39:01 +00001134 mInfo.frame = Rect(0, 0, WIDTH, HEIGHT);
chaviw1ff3d1e2020-07-01 15:53:47 -07001135 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001136 mInfo.globalScaleFactor = 1.0;
1137 mInfo.touchableRegion.clear();
1138 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001139 mInfo.ownerPid = WINDOW_PID;
1140 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001141 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001142 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001143 }
1144
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001145 sp<FakeWindowHandle> clone(int32_t displayId) {
1146 sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)");
1147 handle->mInfo = mInfo;
1148 handle->mInfo.displayId = displayId;
1149 handle->mInfo.id = sId++;
1150 handle->mInputReceiver = mInputReceiver;
1151 return handle;
1152 }
1153
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001154 void setTouchable(bool touchable) {
1155 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1156 }
chaviwd1c23182019-12-20 18:44:56 -08001157
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001158 void setFocusable(bool focusable) {
1159 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1160 }
1161
1162 void setVisible(bool visible) {
1163 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1164 }
Vishnu Nair958da932020-08-21 17:12:37 -07001165
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001166 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001167 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001168 }
1169
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001170 void setPaused(bool paused) {
1171 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1172 }
1173
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001174 void setPreventSplitting(bool preventSplitting) {
1175 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001176 }
1177
1178 void setSlippery(bool slippery) {
1179 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1180 }
1181
1182 void setWatchOutsideTouch(bool watchOutside) {
1183 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1184 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001185
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001186 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1187
1188 void setInterceptsStylus(bool interceptsStylus) {
1189 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1190 }
1191
1192 void setDropInput(bool dropInput) {
1193 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1194 }
1195
1196 void setDropInputIfObscured(bool dropInputIfObscured) {
1197 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1198 }
1199
1200 void setNoInputChannel(bool noInputChannel) {
1201 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1202 }
1203
Josep del Riob3981622023-04-18 15:49:45 +00001204 void setDisableUserActivity(bool disableUserActivity) {
1205 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1206 }
1207
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001208 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1209
chaviw3277faf2021-05-19 16:45:23 -05001210 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001211
Bernardo Rufino7393d172021-02-26 13:56:11 +00001212 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1213
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001214 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
Chavi Weingarten7f019192023-08-08 20:39:01 +00001215 mInfo.frame = frame;
chaviwd1c23182019-12-20 18:44:56 -08001216 mInfo.touchableRegion.clear();
1217 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001218
1219 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1220 ui::Transform translate;
1221 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1222 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001223 }
1224
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001225 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1226
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001227 void setIsWallpaper(bool isWallpaper) {
1228 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1229 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001230
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001231 void setDupTouchToWallpaper(bool hasWallpaper) {
1232 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1233 }
chaviwd1c23182019-12-20 18:44:56 -08001234
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001235 void setTrustedOverlay(bool trustedOverlay) {
1236 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1237 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001238
chaviw9eaa22c2020-07-01 16:21:27 -07001239 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1240 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1241 }
1242
1243 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001244
yunho.shinf4a80b82020-11-16 21:13:57 +09001245 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1246
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001247 KeyEvent* consumeKey() {
1248 InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
1249 if (event == nullptr) {
1250 ADD_FAILURE() << "Consume failed : no event";
1251 return nullptr;
1252 }
1253 if (event->getType() != InputEventType::KEY) {
1254 ADD_FAILURE() << "Instead of key event, got " << *event;
1255 return nullptr;
1256 }
1257 return static_cast<KeyEvent*>(event);
1258 }
1259
1260 void consumeKeyEvent(const ::testing::Matcher<KeyEvent>& matcher) {
1261 KeyEvent* keyEvent = consumeKey();
1262 ASSERT_NE(nullptr, keyEvent) << "Did not get a key event, but expected " << matcher;
1263 ASSERT_THAT(*keyEvent, matcher);
1264 }
1265
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001266 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001267 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001268 }
1269
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001270 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001271 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001272 }
1273
Svet Ganov5d3bc372020-01-26 23:11:07 -08001274 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001275 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001276 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1277 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001278 }
1279
1280 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001281 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001282 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1283 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001284 }
1285
1286 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001287 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001288 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1289 }
1290
1291 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1292 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001293 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001294 expectedFlags);
1295 }
1296
Svet Ganov5d3bc372020-01-26 23:11:07 -08001297 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001298 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1299 int32_t expectedFlags = 0) {
1300 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1301 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001302 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001303 }
1304
1305 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001306 int32_t expectedFlags = 0) {
1307 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1308 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001309 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001310 }
1311
1312 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001313 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001314 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001315 expectedFlags);
1316 }
1317
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001318 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1319 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001320 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001321 expectedFlags);
1322 }
1323
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001324 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1325 int32_t expectedFlags = 0) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001326 MotionEvent* motionEvent = consumeMotion();
1327 ASSERT_NE(nullptr, motionEvent);
1328 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent->getActionMasked());
1329 EXPECT_EQ(0.f, motionEvent->getRawPointerCoords(0)->getX());
1330 EXPECT_EQ(0.f, motionEvent->getRawPointerCoords(0)->getY());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001331 }
1332
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001333 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1334 ASSERT_NE(mInputReceiver, nullptr)
1335 << "Cannot consume events from a window with no receiver";
1336 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1337 }
1338
Prabir Pradhan99987712020-11-10 18:43:05 -08001339 void consumeCaptureEvent(bool hasCapture) {
1340 ASSERT_NE(mInputReceiver, nullptr)
1341 << "Cannot consume events from a window with no receiver";
1342 mInputReceiver->consumeCaptureEvent(hasCapture);
1343 }
1344
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001345 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1346 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001347 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001348 ASSERT_THAT(*motionEvent, matcher);
1349 }
1350
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001351 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001352 std::optional<int32_t> expectedDisplayId,
1353 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001354 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1355 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1356 expectedFlags);
1357 }
1358
arthurhungb89ccb02020-12-30 16:19:01 +08001359 void consumeDragEvent(bool isExiting, float x, float y) {
1360 mInputReceiver->consumeDragEvent(isExiting, x, y);
1361 }
1362
Antonio Kantekf16f2832021-09-28 04:39:20 +00001363 void consumeTouchModeEvent(bool inTouchMode) {
1364 ASSERT_NE(mInputReceiver, nullptr)
1365 << "Cannot consume events from a window with no receiver";
1366 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1367 }
1368
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001369 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001370 if (mInputReceiver == nullptr) {
1371 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1372 return std::nullopt;
1373 }
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001374 return mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED, outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001375 }
1376
1377 void finishEvent(uint32_t sequenceNum) {
1378 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1379 mInputReceiver->finishEvent(sequenceNum);
1380 }
1381
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001382 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1383 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1384 mInputReceiver->sendTimeline(inputEventId, timeline);
1385 }
1386
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001387 InputEvent* consume(std::chrono::milliseconds timeout) {
chaviwaf87b3e2019-10-01 16:59:28 -07001388 if (mInputReceiver == nullptr) {
1389 return nullptr;
1390 }
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001391 return mInputReceiver->consume(timeout);
chaviwaf87b3e2019-10-01 16:59:28 -07001392 }
1393
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001394 MotionEvent* consumeMotion() {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001395 InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001396 if (event == nullptr) {
1397 ADD_FAILURE() << "Consume failed : no event";
1398 return nullptr;
1399 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001400 if (event->getType() != InputEventType::MOTION) {
1401 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001402 return nullptr;
1403 }
1404 return static_cast<MotionEvent*>(event);
1405 }
1406
Arthur Hungb92218b2018-08-14 12:00:21 +08001407 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001408 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001409 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001410 return; // Can't receive events if the window does not have input channel
1411 }
1412 ASSERT_NE(nullptr, mInputReceiver)
1413 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001414 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001415 }
1416
chaviwaf87b3e2019-10-01 16:59:28 -07001417 sp<IBinder> getToken() { return mInfo.token; }
1418
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001419 const std::string& getName() { return mName; }
1420
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00001421 void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001422 mInfo.ownerPid = ownerPid;
1423 mInfo.ownerUid = ownerUid;
1424 }
1425
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00001426 gui::Pid getPid() const { return mInfo.ownerPid; }
Prabir Pradhanedd96402022-02-15 01:46:16 -08001427
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001428 void destroyReceiver() { mInputReceiver = nullptr; }
1429
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001430 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1431
chaviwd1c23182019-12-20 18:44:56 -08001432private:
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001433 FakeWindowHandle(std::string name) : mName(name){};
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001434 const std::string mName;
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001435 std::shared_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001436 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001437 friend class sp<FakeWindowHandle>;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001438};
1439
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001440std::atomic<int32_t> FakeWindowHandle::sId{1};
1441
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001442static InputEventInjectionResult injectKey(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001443 InputDispatcher& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001444 int32_t displayId = ADISPLAY_ID_NONE,
1445 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001446 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001447 bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {},
Prabir Pradhan5735a322022-04-11 17:23:34 +00001448 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001449 KeyEvent event;
1450 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1451
1452 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001453 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001454 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1455 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001456
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001457 if (!allowKeyRepeat) {
1458 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1459 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001460 // Inject event until dispatch out.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001461 return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001462}
1463
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001464static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) {
1465 InputEventInjectionResult result =
1466 injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_NONE,
1467 InputEventInjectionSync::WAIT_FOR_RESULT, CONSUME_TIMEOUT_NO_EVENT_EXPECTED);
1468 if (result != InputEventInjectionResult::TIMED_OUT) {
1469 FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result);
1470 }
1471}
1472
1473static InputEventInjectionResult injectKeyDown(InputDispatcher& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001474 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001475 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001476}
1477
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001478// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1479// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1480// has to be woken up to process the repeating key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001481static InputEventInjectionResult injectKeyDownNoRepeat(InputDispatcher& dispatcher,
1482 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001483 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001484 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001485 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001486}
1487
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001488static InputEventInjectionResult injectKeyUp(InputDispatcher& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001489 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001490 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001491}
1492
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001493static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001494 InputDispatcher& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001495 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001496 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001497 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001498 return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1499 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001500}
1501
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001502static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001503 InputDispatcher& dispatcher, int32_t action, int32_t source, int32_t displayId,
1504 const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001505 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001506 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1507 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001508 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001509 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001510 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001511 MotionEventBuilder motionBuilder =
1512 MotionEventBuilder(action, source)
1513 .displayId(displayId)
1514 .eventTime(eventTime)
1515 .rawXCursorPosition(cursorPosition.x)
1516 .rawYCursorPosition(cursorPosition.y)
1517 .pointer(
1518 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1519 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1520 motionBuilder.downTime(eventTime);
1521 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001522
1523 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001524 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1525 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001526}
1527
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001528static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source,
1529 int32_t displayId,
1530 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001531 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001532}
1533
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001534static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source,
1535 int32_t displayId,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001536 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001537 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001538}
1539
Jackal Guof9696682018-10-05 12:23:23 +08001540static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1541 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1542 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001543 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001544 displayId, POLICY_FLAG_PASS_TO_USER, action, /*flags=*/0, AKEYCODE_A, KEY_A,
1545 AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001546
1547 return args;
1548}
1549
Josep del Riob3981622023-04-18 15:49:45 +00001550static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1551 int32_t displayId = ADISPLAY_ID_NONE) {
1552 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1553 // Define a valid key event.
1554 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001555 displayId, 0, action, /*flags=*/0, AKEYCODE_C, KEY_C, AMETA_META_ON,
Josep del Riob3981622023-04-18 15:49:45 +00001556 currentTime);
1557
1558 return args;
1559}
1560
1561static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1562 int32_t displayId = ADISPLAY_ID_NONE) {
1563 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1564 // Define a valid key event.
1565 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001566 displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST, KEY_ASSISTANT,
Josep del Riob3981622023-04-18 15:49:45 +00001567 AMETA_NONE, currentTime);
1568
1569 return args;
1570}
1571
Prabir Pradhan678438e2023-04-13 19:32:51 +00001572[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1573 int32_t displayId,
1574 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001575 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001576 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1577 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1578 }
1579
chaviwd1c23182019-12-20 18:44:56 -08001580 PointerProperties pointerProperties[pointerCount];
1581 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001582
chaviwd1c23182019-12-20 18:44:56 -08001583 for (size_t i = 0; i < pointerCount; i++) {
1584 pointerProperties[i].clear();
1585 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001586 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001587
chaviwd1c23182019-12-20 18:44:56 -08001588 pointerCoords[i].clear();
1589 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1590 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1591 }
Jackal Guof9696682018-10-05 12:23:23 +08001592
1593 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1594 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001595 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001596 POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0, /*flags=*/0,
1597 AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001598 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001599 pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001600 AMOTION_EVENT_INVALID_CURSOR_POSITION,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001601 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{});
Jackal Guof9696682018-10-05 12:23:23 +08001602
1603 return args;
1604}
1605
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001606static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1607 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1608}
1609
chaviwd1c23182019-12-20 18:44:56 -08001610static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1611 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1612}
1613
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001614static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1615 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001616 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001617}
1618
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -07001619} // namespace
1620
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001621/**
1622 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1623 * broken channel.
1624 */
1625TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1626 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1627 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001628 sp<FakeWindowHandle>::make(application, mDispatcher,
1629 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001630
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001631 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001632
1633 // Window closes its channel, but the window remains.
1634 window->destroyReceiver();
1635 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1636}
1637
Arthur Hungb92218b2018-08-14 12:00:21 +08001638TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001639 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001640 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1641 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001642
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001643 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001644 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001645 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001646 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001647
1648 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001649 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001650}
1651
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001652TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1653 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001654 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1655 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001656
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001657 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001658 // Inject a MotionEvent to an unknown display.
1659 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001660 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001661 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1662
1663 // Window should receive motion event.
1664 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1665}
1666
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001667/**
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001668 * Calling onWindowInfosChanged once should not cause any issues.
1669 * This test serves as a sanity check for the next test, where onWindowInfosChanged is
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001670 * called twice.
1671 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001672TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001673 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001674 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1675 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001676 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001677
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001678 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001679 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001680 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001681 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001682 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001683
1684 // Window should receive motion event.
1685 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1686}
1687
1688/**
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001689 * Calling onWindowInfosChanged twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001690 */
1691TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001692 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001693 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1694 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001695 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001696
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001697 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
1698 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001699 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001700 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001701 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001702 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001703
1704 // Window should receive motion event.
1705 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1706}
1707
Arthur Hungb92218b2018-08-14 12:00:21 +08001708// The foreground window should receive the first touch down event.
1709TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001710 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001711 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001712 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001713 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001714 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001715
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001716 mDispatcher->onWindowInfosChanged(
1717 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001718 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001719 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001720 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001721
1722 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001723 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001724 windowSecond->assertNoEvents();
1725}
1726
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001727/**
1728 * Two windows: A top window, and a wallpaper behind the window.
1729 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1730 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001731 * 1. foregroundWindow <-- dup touch to wallpaper
1732 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001733 */
1734TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1735 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1736 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001737 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001738 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001739 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001740 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001741 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001742
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001743 mDispatcher->onWindowInfosChanged(
1744 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001746 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001747 {100, 200}))
1748 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1749
1750 // Both foreground window and its wallpaper should receive the touch down
1751 foregroundWindow->consumeMotionDown();
1752 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1753
1754 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001755 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001756 ADISPLAY_ID_DEFAULT, {110, 200}))
1757 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1758
1759 foregroundWindow->consumeMotionMove();
1760 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1761
1762 // Now the foreground window goes away, but the wallpaper stays
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001763 mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001764 foregroundWindow->consumeMotionCancel();
1765 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1766 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1767}
1768
1769/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001770 * Two fingers down on the window, and lift off the first finger.
1771 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1772 * contains a single pointer.
1773 */
1774TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1775 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1776 sp<FakeWindowHandle> window =
1777 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1778
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001779 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001780 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001781 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1782 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1783 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001784 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001785 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1786 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1787 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1788 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001789 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001790 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1791 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1792 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1793 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001794 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1795 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1796 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1797
1798 // Remove the window. The gesture should be canceled
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001799 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001800 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1801 window->consumeMotionEvent(
1802 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1803}
1804
1805/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001806 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1807 * with the following differences:
1808 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1809 * clean up the connection.
1810 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1811 * Ensure that there's no crash in the dispatcher.
1812 */
1813TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1814 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1815 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001816 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001817 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001818 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001819 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001820 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001821
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001822 mDispatcher->onWindowInfosChanged(
1823 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001824 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001825 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001826 {100, 200}))
1827 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1828
1829 // Both foreground window and its wallpaper should receive the touch down
1830 foregroundWindow->consumeMotionDown();
1831 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1832
1833 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001834 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001835 ADISPLAY_ID_DEFAULT, {110, 200}))
1836 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1837
1838 foregroundWindow->consumeMotionMove();
1839 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1840
1841 // Wallpaper closes its channel, but the window remains.
1842 wallpaperWindow->destroyReceiver();
1843 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1844
1845 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1846 // is no longer valid.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001847 mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001848 foregroundWindow->consumeMotionCancel();
1849}
1850
Arthur Hungc539dbb2022-12-08 07:45:36 +00001851class ShouldSplitTouchFixture : public InputDispatcherTest,
1852 public ::testing::WithParamInterface<bool> {};
1853INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1854 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001855/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001856 * A single window that receives touch (on top), and a wallpaper window underneath it.
1857 * The top window gets a multitouch gesture.
1858 * Ensure that wallpaper gets the same gesture.
1859 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00001860TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001861 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001862 sp<FakeWindowHandle> foregroundWindow =
1863 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1864 foregroundWindow->setDupTouchToWallpaper(true);
1865 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001866
1867 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001868 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001869 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001870
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001871 mDispatcher->onWindowInfosChanged(
1872 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001873
1874 // Touch down on top window
1875 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001876 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001877 {100, 100}))
1878 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1879
1880 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00001881 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001882 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1883
1884 // Second finger down on the top window
1885 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001886 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001887 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001888 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1889 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001890 .build();
1891 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001892 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001893 InputEventInjectionSync::WAIT_FOR_RESULT))
1894 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1895
Harry Cutts33476232023-01-30 19:57:29 +00001896 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
1897 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001898 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001899
1900 const MotionEvent secondFingerUpEvent =
1901 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1902 .displayId(ADISPLAY_ID_DEFAULT)
1903 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001904 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1905 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001906 .build();
1907 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001908 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00001909 InputEventInjectionSync::WAIT_FOR_RESULT))
1910 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1911 foregroundWindow->consumeMotionPointerUp(0);
1912 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1913
1914 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001915 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001916 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
1917 AINPUT_SOURCE_TOUCHSCREEN)
1918 .displayId(ADISPLAY_ID_DEFAULT)
1919 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts101ee9b2023-07-06 18:04:14 +00001920 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001921 .x(100)
1922 .y(100))
1923 .build(),
1924 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001925 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1926 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1927 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001928}
1929
1930/**
1931 * Two windows: a window on the left and window on the right.
1932 * A third window, wallpaper, is behind both windows, and spans both top windows.
1933 * The first touch down goes to the left window. A second pointer touches down on the right window.
1934 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1935 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1936 * ACTION_POINTER_DOWN(1).
1937 */
1938TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1939 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1940 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001941 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001942 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001943 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001944
1945 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001946 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001947 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001948 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001949
1950 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001951 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001952 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001953 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001954
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001955 mDispatcher->onWindowInfosChanged(
1956 {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()},
1957 {},
1958 0,
1959 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001960
1961 // Touch down on left window
1962 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001963 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001964 {100, 100}))
1965 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1966
1967 // Both foreground window and its wallpaper should receive the touch down
1968 leftWindow->consumeMotionDown();
1969 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1970
1971 // Second finger down on the right window
1972 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001973 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001974 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001975 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1976 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001977 .build();
1978 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001979 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001980 InputEventInjectionSync::WAIT_FOR_RESULT))
1981 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1982
1983 leftWindow->consumeMotionMove();
1984 // Since the touch is split, right window gets ACTION_DOWN
1985 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00001986 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001987 expectedWallpaperFlags);
1988
1989 // Now, leftWindow, which received the first finger, disappears.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001990 mDispatcher->onWindowInfosChanged(
1991 {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001992 leftWindow->consumeMotionCancel();
1993 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1994 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1995
1996 // The pointer that's still down on the right window moves, and goes to the right window only.
1997 // As far as the dispatcher's concerned though, both pointers are still present.
1998 const MotionEvent secondFingerMoveEvent =
1999 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2000 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002001 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2002 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002003 .build();
2004 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002005 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002006 InputEventInjectionSync::WAIT_FOR_RESULT));
2007 rightWindow->consumeMotionMove();
2008
2009 leftWindow->assertNoEvents();
2010 rightWindow->assertNoEvents();
2011 wallpaperWindow->assertNoEvents();
2012}
2013
Arthur Hungc539dbb2022-12-08 07:45:36 +00002014/**
2015 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2016 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2017 * The right window should receive ACTION_DOWN.
2018 */
2019TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002020 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002021 sp<FakeWindowHandle> leftWindow =
2022 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2023 leftWindow->setFrame(Rect(0, 0, 200, 200));
2024 leftWindow->setDupTouchToWallpaper(true);
2025 leftWindow->setSlippery(true);
2026
2027 sp<FakeWindowHandle> rightWindow =
2028 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2029 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002030
2031 sp<FakeWindowHandle> wallpaperWindow =
2032 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2033 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002034
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002035 mDispatcher->onWindowInfosChanged(
2036 {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()},
2037 {},
2038 0,
2039 0});
Arthur Hung74c248d2022-11-23 07:09:59 +00002040
Arthur Hungc539dbb2022-12-08 07:45:36 +00002041 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002042 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002043 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002044 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002045 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002046
2047 // Both foreground window and its wallpaper should receive the touch down
2048 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002049 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2050
Arthur Hungc539dbb2022-12-08 07:45:36 +00002051 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002052 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002053 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002054 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002055 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2056
Arthur Hungc539dbb2022-12-08 07:45:36 +00002057 leftWindow->consumeMotionCancel();
2058 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2059 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002060}
2061
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002062/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002063 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2064 * interactive, it might stop sending this flag.
2065 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2066 * to have a consistent input stream.
2067 *
2068 * Test procedure:
2069 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2070 * DOWN (new gesture).
2071 *
2072 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2073 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2074 *
2075 * We technically just need a single window here, but we are using two windows (spy on top and a
2076 * regular window below) to emulate the actual situation where it happens on the device.
2077 */
2078TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2079 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2080 sp<FakeWindowHandle> spyWindow =
2081 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2082 spyWindow->setFrame(Rect(0, 0, 200, 200));
2083 spyWindow->setTrustedOverlay(true);
2084 spyWindow->setSpy(true);
2085
2086 sp<FakeWindowHandle> window =
2087 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2088 window->setFrame(Rect(0, 0, 200, 200));
2089
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002090 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002091 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002092
2093 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002094 mDispatcher->notifyMotion(
2095 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2096 .deviceId(touchDeviceId)
2097 .policyFlags(DEFAULT_POLICY_FLAGS)
2098 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2099 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002100
Prabir Pradhan678438e2023-04-13 19:32:51 +00002101 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2102 .deviceId(touchDeviceId)
2103 .policyFlags(DEFAULT_POLICY_FLAGS)
2104 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2105 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2106 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002107 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2108 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2109 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2110 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2111
2112 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002113 mDispatcher->notifyMotion(
2114 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2115 .deviceId(touchDeviceId)
2116 .policyFlags(0)
2117 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2118 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2119 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002120 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2121 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2122
2123 // We don't need to reset the device to reproduce the issue, but the reset event typically
2124 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002125 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002126
2127 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002128 mDispatcher->notifyMotion(
2129 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2130 .deviceId(touchDeviceId)
2131 .policyFlags(DEFAULT_POLICY_FLAGS)
2132 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2133 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002134 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2135 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2136
2137 // No more events
2138 spyWindow->assertNoEvents();
2139 window->assertNoEvents();
2140}
2141
2142/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002143 * Two windows: a window on the left and a window on the right.
2144 * Mouse is hovered from the right window into the left window.
2145 * Next, we tap on the left window, where the cursor was last seen.
2146 * The second tap is done onto the right window.
2147 * The mouse and tap are from two different devices.
2148 * We technically don't need to set the downtime / eventtime for these events, but setting these
2149 * explicitly helps during debugging.
2150 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2151 * In the buggy implementation, a tap on the right window would cause a crash.
2152 */
2153TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2154 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2155 sp<FakeWindowHandle> leftWindow =
2156 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2157 leftWindow->setFrame(Rect(0, 0, 200, 200));
2158
2159 sp<FakeWindowHandle> rightWindow =
2160 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2161 rightWindow->setFrame(Rect(200, 0, 400, 200));
2162
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002163 mDispatcher->onWindowInfosChanged(
2164 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002165 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2166 // stale.
2167 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2168 const int32_t mouseDeviceId = 6;
2169 const int32_t touchDeviceId = 4;
2170 // Move the cursor from right
2171 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002172 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002173 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2174 AINPUT_SOURCE_MOUSE)
2175 .deviceId(mouseDeviceId)
2176 .downTime(baseTime + 10)
2177 .eventTime(baseTime + 20)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002178 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002179 .build()));
2180 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2181
2182 // .. to the left window
2183 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002184 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002185 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2186 AINPUT_SOURCE_MOUSE)
2187 .deviceId(mouseDeviceId)
2188 .downTime(baseTime + 10)
2189 .eventTime(baseTime + 30)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002190 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002191 .build()));
2192 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2193 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2194 // Now tap the left window
2195 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002196 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002197 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2198 AINPUT_SOURCE_TOUCHSCREEN)
2199 .deviceId(touchDeviceId)
2200 .downTime(baseTime + 40)
2201 .eventTime(baseTime + 40)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002202 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002203 .build()));
2204 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2205 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2206
2207 // release tap
2208 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002209 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002210 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2211 AINPUT_SOURCE_TOUCHSCREEN)
2212 .deviceId(touchDeviceId)
2213 .downTime(baseTime + 40)
2214 .eventTime(baseTime + 50)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002215 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002216 .build()));
2217 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2218
2219 // Tap the window on the right
2220 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002221 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002222 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2223 AINPUT_SOURCE_TOUCHSCREEN)
2224 .deviceId(touchDeviceId)
2225 .downTime(baseTime + 60)
2226 .eventTime(baseTime + 60)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002227 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002228 .build()));
2229 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2230
2231 // release tap
2232 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002233 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002234 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2235 AINPUT_SOURCE_TOUCHSCREEN)
2236 .deviceId(touchDeviceId)
2237 .downTime(baseTime + 60)
2238 .eventTime(baseTime + 70)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002239 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002240 .build()));
2241 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2242
2243 // No more events
2244 leftWindow->assertNoEvents();
2245 rightWindow->assertNoEvents();
2246}
2247
2248/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002249 * Start hovering in a window. While this hover is still active, make another window appear on top.
2250 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2251 * While the top window is present, the hovering is stopped.
2252 * Later, hovering gets resumed again.
2253 * Ensure that new hover gesture is handled correctly.
2254 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2255 * to the window that's currently being hovered over.
2256 */
2257TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2258 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2259 sp<FakeWindowHandle> window =
2260 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2261 window->setFrame(Rect(0, 0, 200, 200));
2262
2263 // Only a single window is present at first
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002264 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002265
2266 // Start hovering in the window
2267 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2268 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2269 .build());
2270 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2271
2272 // Now, an obscuring window appears!
2273 sp<FakeWindowHandle> obscuringWindow =
2274 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2275 ADISPLAY_ID_DEFAULT,
2276 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2277 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2278 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2279 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2280 obscuringWindow->setNoInputChannel(true);
2281 obscuringWindow->setFocusable(false);
2282 obscuringWindow->setAlpha(1.0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002283 mDispatcher->onWindowInfosChanged(
2284 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002285
2286 // While this new obscuring window is present, the hovering is stopped
2287 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2288 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2289 .build());
2290 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2291
2292 // Now the obscuring window goes away.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002293 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002294
2295 // And a new hover gesture starts.
2296 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2297 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2298 .build());
2299 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2300}
2301
2302/**
2303 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2304 * the obscuring window.
2305 */
2306TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2307 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2308 sp<FakeWindowHandle> window =
2309 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2310 window->setFrame(Rect(0, 0, 200, 200));
2311
2312 // Only a single window is present at first
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002313 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002314
2315 // Start hovering in the window
2316 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2317 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2318 .build());
2319 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2320
2321 // Now, an obscuring window appears!
2322 sp<FakeWindowHandle> obscuringWindow =
2323 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2324 ADISPLAY_ID_DEFAULT,
2325 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2326 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2327 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2328 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2329 obscuringWindow->setNoInputChannel(true);
2330 obscuringWindow->setFocusable(false);
2331 obscuringWindow->setAlpha(1.0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002332 mDispatcher->onWindowInfosChanged(
2333 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002334
2335 // While this new obscuring window is present, the hovering continues. The event can't go to the
2336 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2337 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2338 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2339 .build());
2340 obscuringWindow->assertNoEvents();
2341 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2342
2343 // Now the obscuring window goes away.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002344 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002345
2346 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2347 // so it should generate a HOVER_ENTER
2348 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2349 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2350 .build());
2351 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2352
2353 // Now the MOVE should be getting dispatched normally
2354 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2355 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2356 .build());
2357 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2358}
2359
2360/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002361 * Two windows: a window on the left and a window on the right.
2362 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2363 * down. Then, on the left window, also place second touch pointer down.
2364 * This test tries to reproduce a crash.
2365 * In the buggy implementation, second pointer down on the left window would cause a crash.
2366 */
2367TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2368 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2369 sp<FakeWindowHandle> leftWindow =
2370 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2371 leftWindow->setFrame(Rect(0, 0, 200, 200));
2372
2373 sp<FakeWindowHandle> rightWindow =
2374 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2375 rightWindow->setFrame(Rect(200, 0, 400, 200));
2376
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002377 mDispatcher->onWindowInfosChanged(
2378 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002379
2380 const int32_t touchDeviceId = 4;
2381 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002382
2383 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002384 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2385 .deviceId(mouseDeviceId)
2386 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2387 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002388 leftWindow->consumeMotionEvent(
2389 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2390
2391 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002392 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2393 .deviceId(mouseDeviceId)
2394 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2395 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2396 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002397
2398 leftWindow->consumeMotionEvent(
2399 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2400 leftWindow->consumeMotionEvent(
2401 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2402
Prabir Pradhan678438e2023-04-13 19:32:51 +00002403 mDispatcher->notifyMotion(
2404 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2405 .deviceId(mouseDeviceId)
2406 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2407 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2408 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2409 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002410 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2411
2412 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002413 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2414 .deviceId(touchDeviceId)
2415 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2416 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002417 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2418
2419 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2420
2421 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002422 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2423 .deviceId(touchDeviceId)
2424 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2425 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2426 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002427 leftWindow->consumeMotionEvent(
2428 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2429 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2430 // current implementation.
2431 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2432 rightWindow->consumeMotionEvent(
2433 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2434
2435 leftWindow->assertNoEvents();
2436 rightWindow->assertNoEvents();
2437}
2438
2439/**
2440 * On a single window, use two different devices: mouse and touch.
2441 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2442 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2443 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2444 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2445 * represent a new gesture.
2446 */
2447TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2448 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2449 sp<FakeWindowHandle> window =
2450 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2451 window->setFrame(Rect(0, 0, 400, 400));
2452
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002453 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002454
2455 const int32_t touchDeviceId = 4;
2456 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002457
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002458 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002459 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2460 .deviceId(touchDeviceId)
2461 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2462 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002463 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002464 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2465 .deviceId(touchDeviceId)
2466 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2467 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2468 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002469 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002470 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2471 .deviceId(touchDeviceId)
2472 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2473 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2474 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002475 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2476 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2477 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2478
2479 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002480 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2481 .deviceId(mouseDeviceId)
2482 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2483 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2484 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002485
2486 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002487 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002488 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2489
Prabir Pradhan678438e2023-04-13 19:32:51 +00002490 mDispatcher->notifyMotion(
2491 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2492 .deviceId(mouseDeviceId)
2493 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2494 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2495 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2496 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002497 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2498
2499 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002500 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2501 .deviceId(touchDeviceId)
2502 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2503 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2504 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002505 // The pointer_down event should be ignored
2506 window->assertNoEvents();
2507}
2508
2509/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002510 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2511 * the injected event.
2512 */
2513TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2514 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2515 sp<FakeWindowHandle> window =
2516 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2517 window->setFrame(Rect(0, 0, 400, 400));
2518
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002519 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002520
2521 const int32_t touchDeviceId = 4;
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002522 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2523 // completion.
2524 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002525 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002526 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2527 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002528 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002529 .build()));
2530 window->consumeMotionEvent(
2531 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2532
2533 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2534 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002535 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2536 .deviceId(touchDeviceId)
2537 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2538 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002539
2540 window->consumeMotionEvent(
2541 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2542 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2543}
2544
2545/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002546 * This test is similar to the test above, but the sequence of injected events is different.
2547 *
2548 * Two windows: a window on the left and a window on the right.
2549 * Mouse is hovered over the left window.
2550 * Next, we tap on the left window, where the cursor was last seen.
2551 *
2552 * After that, we inject one finger down onto the right window, and then a second finger down onto
2553 * the left window.
2554 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2555 * window (first), and then another on the left window (second).
2556 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2557 * In the buggy implementation, second finger down on the left window would cause a crash.
2558 */
2559TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2560 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2561 sp<FakeWindowHandle> leftWindow =
2562 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2563 leftWindow->setFrame(Rect(0, 0, 200, 200));
2564
2565 sp<FakeWindowHandle> rightWindow =
2566 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2567 rightWindow->setFrame(Rect(200, 0, 400, 200));
2568
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002569 mDispatcher->onWindowInfosChanged(
2570 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002571
2572 const int32_t mouseDeviceId = 6;
2573 const int32_t touchDeviceId = 4;
2574 // Hover over the left window. Keep the cursor there.
2575 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002576 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002577 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2578 AINPUT_SOURCE_MOUSE)
2579 .deviceId(mouseDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002580 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002581 .build()));
2582 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2583
2584 // Tap on left window
2585 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002586 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002587 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2588 AINPUT_SOURCE_TOUCHSCREEN)
2589 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002590 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002591 .build()));
2592
2593 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002594 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002595 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2596 AINPUT_SOURCE_TOUCHSCREEN)
2597 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002598 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002599 .build()));
2600 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2601 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2602 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2603
2604 // First finger down on right window
2605 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002606 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002607 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2608 AINPUT_SOURCE_TOUCHSCREEN)
2609 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002610 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002611 .build()));
2612 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2613
2614 // Second finger down on the left window
2615 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002616 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002617 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2618 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002619 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2620 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002621 .build()));
2622 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2623 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2624
2625 // No more events
2626 leftWindow->assertNoEvents();
2627 rightWindow->assertNoEvents();
2628}
2629
2630/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002631 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2632 * While the touch is down, new hover events from the stylus device should be ignored. After the
2633 * touch is gone, stylus hovering should start working again.
2634 */
2635TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2636 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2637 sp<FakeWindowHandle> window =
2638 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2639 window->setFrame(Rect(0, 0, 200, 200));
2640
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002641 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002642
2643 const int32_t stylusDeviceId = 5;
2644 const int32_t touchDeviceId = 4;
2645 // Start hovering with stylus
2646 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002647 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002648 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2649 AINPUT_SOURCE_STYLUS)
2650 .deviceId(stylusDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002651 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002652 .build()));
2653 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2654
2655 // Finger down on the window
2656 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002657 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002658 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2659 AINPUT_SOURCE_TOUCHSCREEN)
2660 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002661 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002662 .build()));
2663 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2664 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2665
2666 // Try to continue hovering with stylus. Since we are already down, injection should fail
2667 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002668 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002669 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2670 AINPUT_SOURCE_STYLUS)
2671 .deviceId(stylusDeviceId)
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07002672 .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002673 .build()));
2674 // No event should be sent. This event should be ignored because a pointer from another device
2675 // is already down.
2676
2677 // Lift up the finger
2678 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002679 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002680 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2681 AINPUT_SOURCE_TOUCHSCREEN)
2682 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002683 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002684 .build()));
2685 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2686
2687 // Now that the touch is gone, stylus hovering should start working again
2688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002689 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002690 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2691 AINPUT_SOURCE_STYLUS)
2692 .deviceId(stylusDeviceId)
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07002693 .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002694 .build()));
2695 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2696 // No more events
2697 window->assertNoEvents();
2698}
2699
2700/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002701 * A spy window above a window with no input channel.
2702 * Start hovering with a stylus device, and then tap with it.
2703 * Ensure spy window receives the entire sequence.
2704 */
2705TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2706 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2707 sp<FakeWindowHandle> spyWindow =
2708 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2709 spyWindow->setFrame(Rect(0, 0, 200, 200));
2710 spyWindow->setTrustedOverlay(true);
2711 spyWindow->setSpy(true);
2712 sp<FakeWindowHandle> window =
2713 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2714 window->setNoInputChannel(true);
2715 window->setFrame(Rect(0, 0, 200, 200));
2716
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002717 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002718
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002719 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002720 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2721 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2722 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002723 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2724 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002725 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2726 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2727 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002728 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2729
2730 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002731 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2732 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2733 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002734 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2735
2736 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002737 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
2738 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2739 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002740 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2741
2742 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002743 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2744 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2745 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002746 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2747 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002748 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2749 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2750 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002751 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2752
2753 // No more events
2754 spyWindow->assertNoEvents();
2755 window->assertNoEvents();
2756}
2757
2758/**
2759 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
2760 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
2761 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
2762 * While the mouse is down, new move events from the touch device should be ignored.
2763 */
2764TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
2765 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2766 sp<FakeWindowHandle> spyWindow =
2767 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2768 spyWindow->setFrame(Rect(0, 0, 200, 200));
2769 spyWindow->setTrustedOverlay(true);
2770 spyWindow->setSpy(true);
2771 sp<FakeWindowHandle> window =
2772 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2773 window->setFrame(Rect(0, 0, 200, 200));
2774
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002775 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002776
2777 const int32_t mouseDeviceId = 7;
2778 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002779
2780 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00002781 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2782 .deviceId(mouseDeviceId)
2783 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2784 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002785 spyWindow->consumeMotionEvent(
2786 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2787 window->consumeMotionEvent(
2788 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2789
2790 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00002791 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2792 .deviceId(touchDeviceId)
2793 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
2794 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002795 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2796 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2797 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2798 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2799
Prabir Pradhan678438e2023-04-13 19:32:51 +00002800 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2801 .deviceId(touchDeviceId)
2802 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
2803 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002804 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2805 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2806
2807 // Pilfer the stream
2808 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
2809 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2810
Prabir Pradhan678438e2023-04-13 19:32:51 +00002811 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2812 .deviceId(touchDeviceId)
2813 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
2814 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002815 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2816
2817 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002818 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2819 .deviceId(mouseDeviceId)
2820 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2821 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2822 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002823
2824 spyWindow->consumeMotionEvent(
2825 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2826 spyWindow->consumeMotionEvent(
2827 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2828 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2829
Prabir Pradhan678438e2023-04-13 19:32:51 +00002830 mDispatcher->notifyMotion(
2831 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2832 .deviceId(mouseDeviceId)
2833 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2834 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2835 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2836 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002837 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2838 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2839
2840 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002841 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
2842 .deviceId(mouseDeviceId)
2843 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2844 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
2845 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002846 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2847 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
2848
2849 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00002850 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2851 .deviceId(touchDeviceId)
2852 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
2853 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002854
2855 // No more events
2856 spyWindow->assertNoEvents();
2857 window->assertNoEvents();
2858}
2859
2860/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002861 * On the display, have a single window, and also an area where there's no window.
2862 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
2863 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
2864 */
2865TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
2866 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2867 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002868 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002869
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002870 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002871
2872 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00002873 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002874
2875 mDispatcher->waitForIdle();
2876 window->assertNoEvents();
2877
2878 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002879 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002880 mDispatcher->waitForIdle();
2881 window->consumeMotionDown();
2882}
2883
2884/**
2885 * Same test as above, but instead of touching the empty space, the first touch goes to
2886 * non-touchable window.
2887 */
2888TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
2889 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2890 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002891 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002892 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2893 window1->setTouchable(false);
2894 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002895 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002896 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2897
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002898 mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002899
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002900 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002901 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002902
2903 mDispatcher->waitForIdle();
2904 window1->assertNoEvents();
2905 window2->assertNoEvents();
2906
2907 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002908 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002909 mDispatcher->waitForIdle();
2910 window2->consumeMotionDown();
2911}
2912
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002913/**
2914 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
2915 * to the event time of the first ACTION_DOWN sent to the particular window.
2916 */
2917TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
2918 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2919 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002920 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002921 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
2922 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002923 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002924 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
2925
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002926 mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0});
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002927
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002928 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002929 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002930
2931 mDispatcher->waitForIdle();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002932
2933 MotionEvent* motionEvent1 = window1->consumeMotion();
2934 ASSERT_NE(motionEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002935 window2->assertNoEvents();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002936 nsecs_t downTimeForWindow1 = motionEvent1->getDownTime();
2937 ASSERT_EQ(motionEvent1->getDownTime(), motionEvent1->getEventTime());
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002938
2939 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00002940 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002941 mDispatcher->waitForIdle();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002942 MotionEvent* motionEvent2 = window2->consumeMotion();
2943 ASSERT_NE(motionEvent2, nullptr);
2944 nsecs_t downTimeForWindow2 = motionEvent2->getDownTime();
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002945 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002946 ASSERT_EQ(motionEvent2->getDownTime(), motionEvent2->getEventTime());
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002947
2948 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002949 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002950 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002951 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002952
2953 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002954 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002955 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002956 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002957
2958 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
2959 window1->consumeMotionMove();
2960 window1->assertNoEvents();
2961
2962 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002963 mDispatcher->notifyMotion(
2964 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002965 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002966 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002967
Prabir Pradhan678438e2023-04-13 19:32:51 +00002968 mDispatcher->notifyMotion(
2969 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002970 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08002971 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002972}
2973
Garfield Tandf26e862020-07-01 20:18:19 -07002974TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07002975 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07002976 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002977 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002978 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002979 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002980 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07002981 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07002982
2983 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
2984
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002985 mDispatcher->onWindowInfosChanged(
2986 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07002987
2988 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002989 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002990 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07002991 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2992 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002993 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07002994 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002995 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07002996
2997 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002998 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002999 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003000 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3001 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003002 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003003 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003004 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3005 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003006
3007 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003008 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003009 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003010 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3011 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003012 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003013 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003014 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3015 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003016
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003017 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003018 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003019 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3020 AINPUT_SOURCE_MOUSE)
3021 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3022 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003023 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003024 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003025 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003026
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003027 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003028 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003029 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3030 AINPUT_SOURCE_MOUSE)
3031 .buttonState(0)
3032 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003033 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003034 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003035 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003036
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003037 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003038 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003039 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3040 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003041 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003042 .build()));
3043 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3044
3045 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003046 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003047 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003048 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3049 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003050 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003051 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003052 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003053
3054 // No more events
3055 windowLeft->assertNoEvents();
3056 windowRight->assertNoEvents();
3057}
3058
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003059/**
3060 * Put two fingers down (and don't release them) and click the mouse button.
3061 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3062 * currently active gesture should be canceled, and the new one should proceed.
3063 */
3064TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3065 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3066 sp<FakeWindowHandle> window =
3067 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3068 window->setFrame(Rect(0, 0, 600, 800));
3069
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003070 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003071
3072 const int32_t touchDeviceId = 4;
3073 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003074
3075 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003076 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3077 .deviceId(touchDeviceId)
3078 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3079 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003080
Prabir Pradhan678438e2023-04-13 19:32:51 +00003081 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3082 .deviceId(touchDeviceId)
3083 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3084 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3085 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003086 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3087 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3088
3089 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003090 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3091 .deviceId(mouseDeviceId)
3092 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3093 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3094 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003095 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3096 WithPointerCount(2u)));
3097 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3098
Prabir Pradhan678438e2023-04-13 19:32:51 +00003099 mDispatcher->notifyMotion(
3100 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3101 .deviceId(mouseDeviceId)
3102 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3103 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3104 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3105 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003106 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3107
3108 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3109 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003110 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3111 .deviceId(touchDeviceId)
3112 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3113 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3114 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003115 window->assertNoEvents();
3116}
3117
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003118TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3119 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3120
3121 sp<FakeWindowHandle> spyWindow =
3122 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3123 spyWindow->setFrame(Rect(0, 0, 600, 800));
3124 spyWindow->setTrustedOverlay(true);
3125 spyWindow->setSpy(true);
3126 sp<FakeWindowHandle> window =
3127 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3128 window->setFrame(Rect(0, 0, 600, 800));
3129
3130 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003131 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003132
3133 // Send mouse cursor to the window
3134 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003135 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003136 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3137 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003138 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003139 .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);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003163 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003164
3165 // Send mouse cursor to the window
3166 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003167 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003168 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3169 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003170 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003171 .build()));
3172
3173 // Move mouse cursor
3174 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003175 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003176 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3177 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003178 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003179 .build()));
3180
3181 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3182 WithSource(AINPUT_SOURCE_MOUSE)));
3183 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3184 WithSource(AINPUT_SOURCE_MOUSE)));
3185 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3186 WithSource(AINPUT_SOURCE_MOUSE)));
3187 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3188 WithSource(AINPUT_SOURCE_MOUSE)));
3189 // Touch down on the window
3190 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003191 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003192 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3193 AINPUT_SOURCE_TOUCHSCREEN)
3194 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003195 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003196 .build()));
3197 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3198 WithSource(AINPUT_SOURCE_MOUSE)));
3199 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3200 WithSource(AINPUT_SOURCE_MOUSE)));
3201 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3202 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3203 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3204 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3205
3206 // pilfer the motion, retaining the gesture on the spy window.
3207 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3208 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3209 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3210
3211 // Touch UP on the window
3212 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003213 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003214 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3215 AINPUT_SOURCE_TOUCHSCREEN)
3216 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003217 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003218 .build()));
3219 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3220 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3221
3222 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3223 // to send a new gesture. It should again go to both windows (spy and the window below), just
3224 // like the first gesture did, before pilfering. The window configuration has not changed.
3225
3226 // One more tap - DOWN
3227 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003228 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003229 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3230 AINPUT_SOURCE_TOUCHSCREEN)
3231 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003232 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003233 .build()));
3234 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3235 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3236 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3237 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3238
3239 // Touch UP on the window
3240 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003241 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003242 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3243 AINPUT_SOURCE_TOUCHSCREEN)
3244 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003245 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003246 .build()));
3247 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3248 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3249 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3250 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3251
3252 window->assertNoEvents();
3253 spyWindow->assertNoEvents();
3254}
3255
Garfield Tandf26e862020-07-01 20:18:19 -07003256// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3257// directly in this test.
3258TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003259 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003260 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003261 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003262 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003263
3264 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3265
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003266 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07003267
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003268 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003269 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003270 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3271 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003272 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003273 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003274 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003275 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003276 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003277 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003278 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3279 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003280 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003281 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003282 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3283 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003284
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003286 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003287 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3288 AINPUT_SOURCE_MOUSE)
3289 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3290 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003291 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003292 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003293 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003294
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003295 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003296 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003297 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3298 AINPUT_SOURCE_MOUSE)
3299 .buttonState(0)
3300 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003301 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003302 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003303 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003304
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003305 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003306 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003307 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3308 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003309 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003310 .build()));
3311 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3312
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07003313 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
3314 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
3315 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003316 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003317 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3318 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003319 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003320 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003321 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003322}
3323
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003324/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003325 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3326 * is generated.
3327 */
3328TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3329 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3330 sp<FakeWindowHandle> window =
3331 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3332 window->setFrame(Rect(0, 0, 1200, 800));
3333
3334 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3335
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003336 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003337
3338 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003339 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003340 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3341 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003342 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003343 .build()));
3344 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3345
3346 // Remove the window, but keep the channel.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003347 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003348 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3349}
3350
3351/**
Daniel Norman7487dfa2023-08-02 16:39:45 -07003352 * Test that invalid HOVER events sent by accessibility do not cause a fatal crash.
3353 */
3354TEST_F(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash) {
3355 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3356 sp<FakeWindowHandle> window =
3357 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3358 window->setFrame(Rect(0, 0, 1200, 800));
3359
3360 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3361
3362 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
3363
3364 MotionEventBuilder hoverEnterBuilder =
3365 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3366 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3367 .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
3368 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3369 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
3370 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3371 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
3372 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3373 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3374}
3375
3376/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003377 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3378 */
3379TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3380 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3381 sp<FakeWindowHandle> window =
3382 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3383 window->setFrame(Rect(0, 0, 100, 100));
3384
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003385 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003386
3387 const int32_t mouseDeviceId = 7;
3388 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003389
3390 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003391 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3392 .deviceId(mouseDeviceId)
3393 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3394 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003395 window->consumeMotionEvent(
3396 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3397
3398 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003399 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3400 .deviceId(touchDeviceId)
3401 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3402 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003403
3404 window->consumeMotionEvent(
3405 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3406 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3407}
3408
3409/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003410 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003411 * The tap causes a HOVER_EXIT event to be generated because the current event
3412 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003413 */
3414TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3415 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3416 sp<FakeWindowHandle> window =
3417 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3418 window->setFrame(Rect(0, 0, 100, 100));
3419
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003420 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003421
3422 // Inject a hover_move from mouse.
3423 NotifyMotionArgs motionArgs =
3424 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3425 ADISPLAY_ID_DEFAULT, {{50, 50}});
3426 motionArgs.xCursorPosition = 50;
3427 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003428 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003429 ASSERT_NO_FATAL_FAILURE(
3430 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3431 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003432
3433 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003434 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3435 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3436 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003437 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003438 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3439 WithSource(AINPUT_SOURCE_MOUSE))));
3440
3441 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003442 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3443 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3444
Prabir Pradhan678438e2023-04-13 19:32:51 +00003445 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3446 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003447 ASSERT_NO_FATAL_FAILURE(
3448 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3449 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3450}
3451
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003452TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3453 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3454 sp<FakeWindowHandle> windowDefaultDisplay =
3455 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3456 ADISPLAY_ID_DEFAULT);
3457 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3458 sp<FakeWindowHandle> windowSecondDisplay =
3459 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3460 SECOND_DISPLAY_ID);
3461 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3462
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003463 mDispatcher->onWindowInfosChanged(
3464 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003465
3466 // Set cursor position in window in default display and check that hover enter and move
3467 // events are generated.
3468 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003469 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003470 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3471 AINPUT_SOURCE_MOUSE)
3472 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003473 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003474 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003475 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003476
3477 // Remove all windows in secondary display and check that no event happens on window in
3478 // primary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003479 mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0});
3480
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003481 windowDefaultDisplay->assertNoEvents();
3482
3483 // Move cursor position in window in default display and check that only hover move
3484 // event is generated and not hover enter event.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003485 mDispatcher->onWindowInfosChanged(
3486 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003487 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003488 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003489 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3490 AINPUT_SOURCE_MOUSE)
3491 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003492 .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003493 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003494 windowDefaultDisplay->consumeMotionEvent(
3495 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3496 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003497 windowDefaultDisplay->assertNoEvents();
3498}
3499
Garfield Tan00f511d2019-06-12 16:55:40 -07003500TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003501 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003502
3503 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003504 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003505 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003506 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003507 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003508 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003509
3510 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3511
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003512 mDispatcher->onWindowInfosChanged(
3513 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tan00f511d2019-06-12 16:55:40 -07003514
3515 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3516 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003517 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003518 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003519 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003520 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003521 windowRight->assertNoEvents();
3522}
3523
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003524TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003525 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003526 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3527 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003528 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003529
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003530 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07003531 setFocusedWindow(window);
3532
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003533 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003534
Prabir Pradhan678438e2023-04-13 19:32:51 +00003535 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003536
3537 // Window should receive key down event.
3538 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3539
3540 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3541 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003542 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003543 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003544 AKEY_EVENT_FLAG_CANCELED);
3545}
3546
3547TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003548 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003549 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3550 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003551
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003552 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003553
Prabir Pradhan678438e2023-04-13 19:32:51 +00003554 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3555 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003556
3557 // Window should receive motion down event.
3558 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3559
3560 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3561 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003562 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003563 window->consumeMotionEvent(
3564 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003565}
3566
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003567TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
3568 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3569 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3570 "Fake Window", ADISPLAY_ID_DEFAULT);
3571
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003572 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003573
3574 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3575 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
3576 .build());
3577
3578 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3579
3580 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
3581 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
3582 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3583
3584 // After the device has been reset, a new hovering stream can be sent to the window
3585 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3586 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
3587 .build());
3588 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3589}
3590
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003591TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3592 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003593 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3594 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003595 window->setFocusable(true);
3596
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003597 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003598 setFocusedWindow(window);
3599
3600 window->consumeFocusEvent(true);
3601
Prabir Pradhan678438e2023-04-13 19:32:51 +00003602 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003603 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3604 const nsecs_t injectTime = keyArgs.eventTime;
3605 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003606 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003607 // The dispatching time should be always greater than or equal to intercept key timeout.
3608 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3609 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3610 std::chrono::nanoseconds(interceptKeyTimeout).count());
3611}
3612
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07003613/**
3614 * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set.
3615 */
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003616TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3617 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003618 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3619 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003620 window->setFocusable(true);
3621
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003622 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003623 setFocusedWindow(window);
3624
3625 window->consumeFocusEvent(true);
3626
Prabir Pradhan678438e2023-04-13 19:32:51 +00003627 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003628 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07003629
3630 // Set a value that's significantly larger than the default consumption timeout. If the
3631 // implementation is correct, the actual value doesn't matter; it won't slow down the test.
3632 mFakePolicy->setInterceptKeyTimeout(600ms);
3633 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
3634 // Window should receive key event immediately when same key up.
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003635 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3636}
3637
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003638/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003639 * Two windows. First is a regular window. Second does not overlap with the first, and has
3640 * WATCH_OUTSIDE_TOUCH.
3641 * Both windows are owned by the same UID.
3642 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3643 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3644 */
3645TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3646 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3647 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3648 "First Window", ADISPLAY_ID_DEFAULT);
3649 window->setFrame(Rect{0, 0, 100, 100});
3650
3651 sp<FakeWindowHandle> outsideWindow =
3652 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3653 ADISPLAY_ID_DEFAULT);
3654 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3655 outsideWindow->setWatchOutsideTouch(true);
3656 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003657 mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003658
3659 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003660 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3661 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3662 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003663 window->consumeMotionDown();
3664 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3665 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3666 outsideWindow->consumeMotionEvent(
3667 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3668}
3669
3670/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003671 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3672 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3673 * ACTION_OUTSIDE event is sent per gesture.
3674 */
3675TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3676 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3677 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003678 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3679 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003680 window->setWatchOutsideTouch(true);
3681 window->setFrame(Rect{0, 0, 100, 100});
3682 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003683 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3684 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003685 secondWindow->setFrame(Rect{100, 100, 200, 200});
3686 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003687 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3688 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003689 thirdWindow->setFrame(Rect{200, 200, 300, 300});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003690 mDispatcher->onWindowInfosChanged(
3691 {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0});
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003692
3693 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003694 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3695 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3696 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003697 window->assertNoEvents();
3698 secondWindow->assertNoEvents();
3699
3700 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3701 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003702 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3703 ADISPLAY_ID_DEFAULT,
3704 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003705 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3706 window->consumeMotionEvent(
3707 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003708 secondWindow->consumeMotionDown();
3709 thirdWindow->assertNoEvents();
3710
3711 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3712 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003713 mDispatcher->notifyMotion(
3714 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3715 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003716 window->assertNoEvents();
3717 secondWindow->consumeMotionMove();
3718 thirdWindow->consumeMotionDown();
3719}
3720
Prabir Pradhan814fe082022-07-22 20:22:18 +00003721TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3722 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003723 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3724 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003725 window->setFocusable(true);
3726
Patrick Williamsd828f302023-04-28 17:52:08 -05003727 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003728 setFocusedWindow(window);
3729
3730 window->consumeFocusEvent(true);
3731
Prabir Pradhan678438e2023-04-13 19:32:51 +00003732 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3733 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3734 mDispatcher->notifyKey(keyDown);
3735 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003736
3737 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3738 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3739
3740 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003741 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003742
3743 window->consumeFocusEvent(false);
3744
Prabir Pradhan678438e2023-04-13 19:32:51 +00003745 mDispatcher->notifyKey(keyDown);
3746 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003747 window->assertNoEvents();
3748}
3749
Arthur Hung96483742022-11-15 03:30:48 +00003750TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3751 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3752 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3753 "Fake Window", ADISPLAY_ID_DEFAULT);
3754 // Ensure window is non-split and have some transform.
3755 window->setPreventSplitting(true);
3756 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05003757 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00003758
3759 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003760 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung96483742022-11-15 03:30:48 +00003761 {50, 50}))
3762 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3763 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3764
3765 const MotionEvent secondFingerDownEvent =
3766 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3767 .displayId(ADISPLAY_ID_DEFAULT)
3768 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003769 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3770 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003771 .build();
3772 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003773 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung96483742022-11-15 03:30:48 +00003774 InputEventInjectionSync::WAIT_FOR_RESULT))
3775 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3776
3777 const MotionEvent* event = window->consumeMotion();
3778 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
3779 EXPECT_EQ(70, event->getX(0)); // 50 + 20
3780 EXPECT_EQ(90, event->getY(0)); // 50 + 40
3781 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
3782 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
3783}
3784
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07003785/**
3786 * Two windows: a splittable and a non-splittable.
3787 * The non-splittable window shouldn't receive any "incomplete" gestures.
3788 * Send the first pointer to the splittable window, and then touch the non-splittable window.
3789 * The second pointer should be dropped because the initial window is splittable, so it won't get
3790 * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any
3791 * "incomplete" gestures.
3792 */
3793TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) {
3794 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3795 sp<FakeWindowHandle> leftWindow =
3796 sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window",
3797 ADISPLAY_ID_DEFAULT);
3798 leftWindow->setPreventSplitting(false);
3799 leftWindow->setFrame(Rect(0, 0, 100, 100));
3800 sp<FakeWindowHandle> rightWindow =
3801 sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window",
3802 ADISPLAY_ID_DEFAULT);
3803 rightWindow->setPreventSplitting(true);
3804 rightWindow->setFrame(Rect(100, 100, 200, 200));
3805 mDispatcher->onWindowInfosChanged(
3806 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
3807
3808 // Touch down on left, splittable window
3809 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3810 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3811 .build());
3812 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3813
3814 mDispatcher->notifyMotion(
3815 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3816 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3817 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
3818 .build());
3819 leftWindow->assertNoEvents();
3820 rightWindow->assertNoEvents();
3821}
3822
Harry Cuttsb166c002023-05-09 13:06:05 +00003823TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
3824 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3825 sp<FakeWindowHandle> window =
3826 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3827 window->setFrame(Rect(0, 0, 400, 400));
3828 sp<FakeWindowHandle> trustedOverlay =
3829 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
3830 ADISPLAY_ID_DEFAULT);
3831 trustedOverlay->setSpy(true);
3832 trustedOverlay->setTrustedOverlay(true);
3833
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003834 mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00003835
3836 // Start a three-finger touchpad swipe
3837 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3838 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3839 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3840 .build());
3841 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3842 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3843 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3844 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3845 .build());
3846 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3847 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3848 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3849 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3850 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3851 .build());
3852
3853 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3854 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3855 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
3856
3857 // Move the swipe a bit
3858 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3859 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3860 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3861 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3862 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3863 .build());
3864
3865 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3866
3867 // End the swipe
3868 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3869 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3870 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3871 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3872 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3873 .build());
3874 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3875 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3876 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3877 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3878 .build());
3879 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3880 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3881 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3882 .build());
3883
3884 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
3885 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
3886 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
3887
3888 window->assertNoEvents();
3889}
3890
3891TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
3892 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3893 sp<FakeWindowHandle> window =
3894 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3895 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003896 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00003897
3898 // Start a three-finger touchpad swipe
3899 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3900 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3901 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3902 .build());
3903 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
3904 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3905 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3906 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3907 .build());
3908 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
3909 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
3910 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
3911 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
3912 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3913 .build());
3914
3915 // Move the swipe a bit
3916 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3917 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3918 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3919 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3920 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3921 .build());
3922
3923 // End the swipe
3924 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
3925 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3926 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3927 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
3928 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3929 .build());
3930 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
3931 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3932 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
3933 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3934 .build());
3935 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
3936 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
3937 .classification(MotionClassification::MULTI_FINGER_SWIPE)
3938 .build());
3939
3940 window->assertNoEvents();
3941}
3942
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003943/**
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07003944 * Send a two-pointer gesture to a single window. The window's orientation changes in response to
3945 * the first pointer.
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00003946 * Ensure that the second pointer and the subsequent gesture is correctly delivered to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07003947 */
3948TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) {
3949 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3950 sp<FakeWindowHandle> window =
3951 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3952 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07003953 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07003954
3955 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
3956 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3957 .downTime(baseTime + 10)
3958 .eventTime(baseTime + 10)
3959 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3960 .build());
3961
3962 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3963
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07003964 // Change the transform so that the orientation is now different from original.
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07003965 window->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07003966
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07003967 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07003968
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07003969 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3970 .downTime(baseTime + 10)
3971 .eventTime(baseTime + 30)
3972 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3973 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
3974 .build());
3975
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00003976 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3977
3978 // Finish the gesture and start a new one. Ensure all events are sent to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07003979 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
3980 .downTime(baseTime + 10)
3981 .eventTime(baseTime + 40)
3982 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3983 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
3984 .build());
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00003985
3986 window->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
3987
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07003988 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
3989 .downTime(baseTime + 10)
3990 .eventTime(baseTime + 50)
3991 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3992 .build());
3993
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00003994 window->consumeMotionEvent(WithMotionAction(ACTION_UP));
3995
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07003996 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3997 .downTime(baseTime + 60)
3998 .eventTime(baseTime + 60)
3999 .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40))
4000 .build());
4001
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004002 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004003}
4004
4005/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004006 * Ensure the correct coordinate spaces are used by InputDispatcher.
4007 *
4008 * InputDispatcher works in the display space, so its coordinate system is relative to the display
4009 * panel. Windows get events in the window space, and get raw coordinates in the logical display
4010 * space.
4011 */
4012class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
4013public:
4014 void SetUp() override {
4015 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004016 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004017 }
4018
4019 void addDisplayInfo(int displayId, const ui::Transform& transform) {
4020 gui::DisplayInfo info;
4021 info.displayId = displayId;
4022 info.transform = transform;
4023 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05004024 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004025 }
4026
4027 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
4028 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05004029 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004030 }
4031
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004032 void removeAllWindowsAndDisplays() {
4033 mDisplayInfos.clear();
4034 mWindowInfos.clear();
4035 }
4036
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004037 // Set up a test scenario where the display has a scaled projection and there are two windows
4038 // on the display.
4039 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
4040 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
4041 // respectively.
4042 ui::Transform displayTransform;
4043 displayTransform.set(2, 0, 0, 4);
4044 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4045
4046 std::shared_ptr<FakeApplicationHandle> application =
4047 std::make_shared<FakeApplicationHandle>();
4048
4049 // Add two windows to the display. Their frames are represented in the display space.
4050 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004051 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4052 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004053 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
4054 addWindow(firstWindow);
4055
4056 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004057 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4058 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004059 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
4060 addWindow(secondWindow);
4061 return {std::move(firstWindow), std::move(secondWindow)};
4062 }
4063
4064private:
4065 std::vector<gui::DisplayInfo> mDisplayInfos;
4066 std::vector<gui::WindowInfo> mWindowInfos;
4067};
4068
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004069TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004070 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4071 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004072 // selected so that if the hit test was performed with the point and the bounds being in
4073 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004074 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4075 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4076 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004077
4078 firstWindow->consumeMotionDown();
4079 secondWindow->assertNoEvents();
4080}
4081
4082// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
4083// the event should be treated as being in the logical display space.
4084TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
4085 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4086 // Send down to the first window. The point is represented in the logical display space. The
4087 // point is selected so that if the hit test was done in logical display space, then it would
4088 // end up in the incorrect window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004089 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004090 PointF{75 * 2, 55 * 4});
4091
4092 firstWindow->consumeMotionDown();
4093 secondWindow->assertNoEvents();
4094}
4095
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004096// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
4097// event should be treated as being in the logical display space.
4098TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
4099 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4100
4101 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
4102 ui::Transform injectedEventTransform;
4103 injectedEventTransform.set(matrix);
4104 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
4105 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
4106
4107 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4108 .displayId(ADISPLAY_ID_DEFAULT)
4109 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004110 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004111 .x(untransformedPoint.x)
4112 .y(untransformedPoint.y))
4113 .build();
4114 event.transform(matrix);
4115
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004116 injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT,
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004117 InputEventInjectionSync::WAIT_FOR_RESULT);
4118
4119 firstWindow->consumeMotionDown();
4120 secondWindow->assertNoEvents();
4121}
4122
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004123TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4124 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4125
4126 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004127 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4128 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4129 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004130
4131 firstWindow->assertNoEvents();
4132 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004133 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004134 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4135
4136 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4137 EXPECT_EQ(300, event->getRawX(0));
4138 EXPECT_EQ(880, event->getRawY(0));
4139
4140 // Ensure that the x and y values are in the window's coordinate space.
4141 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4142 // the logical display space. This will be the origin of the window space.
4143 EXPECT_EQ(100, event->getX(0));
4144 EXPECT_EQ(80, event->getY(0));
4145}
4146
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004147/** Ensure consistent behavior of InputDispatcher in all orientations. */
4148class InputDispatcherDisplayOrientationFixture
4149 : public InputDispatcherDisplayProjectionTest,
4150 public ::testing::WithParamInterface<ui::Rotation> {};
4151
4152// This test verifies the touchable region of a window for all rotations of the display by tapping
4153// in different locations on the display, specifically points close to the four corners of a
4154// window.
4155TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4156 constexpr static int32_t displayWidth = 400;
4157 constexpr static int32_t displayHeight = 800;
4158
4159 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4160
4161 const auto rotation = GetParam();
4162
4163 // Set up the display with the specified rotation.
4164 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4165 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4166 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4167 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4168 logicalDisplayWidth, logicalDisplayHeight);
4169 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4170
4171 // Create a window with its bounds determined in the logical display.
4172 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4173 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4174 sp<FakeWindowHandle> window =
4175 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4176 window->setFrame(frameInDisplay, displayTransform);
4177 addWindow(window);
4178
4179 // The following points in logical display space should be inside the window.
4180 static const std::array<vec2, 4> insidePoints{
4181 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4182 for (const auto pointInsideWindow : insidePoints) {
4183 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4184 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004185 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4186 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4187 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004188 window->consumeMotionDown();
4189
Prabir Pradhan678438e2023-04-13 19:32:51 +00004190 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4191 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4192 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004193 window->consumeMotionUp();
4194 }
4195
4196 // The following points in logical display space should be outside the window.
4197 static const std::array<vec2, 5> outsidePoints{
4198 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4199 for (const auto pointOutsideWindow : outsidePoints) {
4200 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4201 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004202 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4203 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4204 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004205
Prabir Pradhan678438e2023-04-13 19:32:51 +00004206 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4207 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4208 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004209 }
4210 window->assertNoEvents();
4211}
4212
4213// Run the precision tests for all rotations.
4214INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4215 InputDispatcherDisplayOrientationFixture,
4216 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4217 ui::ROTATION_270),
4218 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4219 return ftl::enum_string(testParamInfo.param);
4220 });
4221
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004222using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4223 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004224
4225class TransferTouchFixture : public InputDispatcherTest,
4226 public ::testing::WithParamInterface<TransferFunction> {};
4227
4228TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004229 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004230
4231 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004232 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004233 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4234 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004235 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004236 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004237 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4238 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004239 sp<FakeWindowHandle> wallpaper =
4240 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4241 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004242 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004243 mDispatcher->onWindowInfosChanged(
4244 {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004245
4246 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004247 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4248 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004249
Svet Ganov5d3bc372020-01-26 23:11:07 -08004250 // Only the first window should get the down event
4251 firstWindow->consumeMotionDown();
4252 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004253 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004254
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004255 // Transfer touch to the second window
4256 TransferFunction f = GetParam();
4257 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4258 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004259 // The first window gets cancel and the second gets down
4260 firstWindow->consumeMotionCancel();
4261 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004262 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004263
4264 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004265 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4266 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004267 // The first window gets no events and the second gets up
4268 firstWindow->assertNoEvents();
4269 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004270 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004271}
4272
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004273/**
4274 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4275 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4276 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4277 * natural to the user.
4278 * In this test, we are sending a pointer to both spy window and first window. We then try to
4279 * transfer touch to the second window. The dispatcher should identify the first window as the
4280 * one that should lose the gesture, and therefore the action should be to move the gesture from
4281 * the first window to the second.
4282 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4283 * the other API, as well.
4284 */
4285TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4286 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4287
4288 // Create a couple of windows + a spy window
4289 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004290 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004291 spyWindow->setTrustedOverlay(true);
4292 spyWindow->setSpy(true);
4293 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004294 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004295 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004296 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004297
4298 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004299 mDispatcher->onWindowInfosChanged(
4300 {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004301
4302 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004303 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4304 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004305 // Only the first window and spy should get the down event
4306 spyWindow->consumeMotionDown();
4307 firstWindow->consumeMotionDown();
4308
4309 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4310 // if f === 'transferTouch'.
4311 TransferFunction f = GetParam();
4312 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4313 ASSERT_TRUE(success);
4314 // The first window gets cancel and the second gets down
4315 firstWindow->consumeMotionCancel();
4316 secondWindow->consumeMotionDown();
4317
4318 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004319 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4320 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004321 // The first window gets no events and the second+spy get up
4322 firstWindow->assertNoEvents();
4323 spyWindow->consumeMotionUp();
4324 secondWindow->consumeMotionUp();
4325}
4326
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004327TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004328 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004329
4330 PointF touchPoint = {10, 10};
4331
4332 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004333 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004334 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4335 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004336 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004337 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004338 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4339 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004340 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004341
4342 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004343 mDispatcher->onWindowInfosChanged(
4344 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004345
4346 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004347 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4348 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4349 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004350 // Only the first window should get the down event
4351 firstWindow->consumeMotionDown();
4352 secondWindow->assertNoEvents();
4353
4354 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004355 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4356 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004357 // Only the first window should get the pointer down event
4358 firstWindow->consumeMotionPointerDown(1);
4359 secondWindow->assertNoEvents();
4360
4361 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004362 TransferFunction f = GetParam();
4363 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4364 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004365 // The first window gets cancel and the second gets down and pointer down
4366 firstWindow->consumeMotionCancel();
4367 secondWindow->consumeMotionDown();
4368 secondWindow->consumeMotionPointerDown(1);
4369
4370 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004371 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4372 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004373 // The first window gets nothing and the second gets pointer up
4374 firstWindow->assertNoEvents();
4375 secondWindow->consumeMotionPointerUp(1);
4376
4377 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004378 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4379 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004380 // The first window gets nothing and the second gets up
4381 firstWindow->assertNoEvents();
4382 secondWindow->consumeMotionUp();
4383}
4384
Arthur Hungc539dbb2022-12-08 07:45:36 +00004385TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4386 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4387
4388 // Create a couple of windows
4389 sp<FakeWindowHandle> firstWindow =
4390 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4391 ADISPLAY_ID_DEFAULT);
4392 firstWindow->setDupTouchToWallpaper(true);
4393 sp<FakeWindowHandle> secondWindow =
4394 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4395 ADISPLAY_ID_DEFAULT);
4396 secondWindow->setDupTouchToWallpaper(true);
4397
4398 sp<FakeWindowHandle> wallpaper1 =
4399 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4400 wallpaper1->setIsWallpaper(true);
4401
4402 sp<FakeWindowHandle> wallpaper2 =
4403 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4404 wallpaper2->setIsWallpaper(true);
4405 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004406 mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(),
4407 *secondWindow->getInfo(), *wallpaper2->getInfo()},
4408 {},
4409 0,
4410 0});
Arthur Hungc539dbb2022-12-08 07:45:36 +00004411
4412 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004413 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4414 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004415
4416 // Only the first window should get the down event
4417 firstWindow->consumeMotionDown();
4418 secondWindow->assertNoEvents();
4419 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4420 wallpaper2->assertNoEvents();
4421
4422 // Transfer touch focus to the second window
4423 TransferFunction f = GetParam();
4424 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4425 ASSERT_TRUE(success);
4426
4427 // The first window gets cancel and the second gets down
4428 firstWindow->consumeMotionCancel();
4429 secondWindow->consumeMotionDown();
4430 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4431 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4432
4433 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004434 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4435 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004436 // The first window gets no events and the second gets up
4437 firstWindow->assertNoEvents();
4438 secondWindow->consumeMotionUp();
4439 wallpaper1->assertNoEvents();
4440 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4441}
4442
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004443// For the cases of single pointer touch and two pointers non-split touch, the api's
4444// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4445// for the case where there are multiple pointers split across several windows.
4446INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4447 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004448 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4449 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004450 return dispatcher->transferTouch(destChannelToken,
4451 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004452 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004453 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4454 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004455 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004456 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004457 }));
4458
Svet Ganov5d3bc372020-01-26 23:11:07 -08004459TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004460 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004461
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004462 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004463 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4464 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004465 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004466
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004467 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004468 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4469 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004470 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004471
4472 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004473 mDispatcher->onWindowInfosChanged(
4474 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004475
4476 PointF pointInFirst = {300, 200};
4477 PointF pointInSecond = {300, 600};
4478
4479 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004480 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4481 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4482 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004483 // Only the first window should get the down event
4484 firstWindow->consumeMotionDown();
4485 secondWindow->assertNoEvents();
4486
4487 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004488 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4489 ADISPLAY_ID_DEFAULT,
4490 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004491 // The first window gets a move and the second a down
4492 firstWindow->consumeMotionMove();
4493 secondWindow->consumeMotionDown();
4494
4495 // Transfer touch focus to the second window
4496 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4497 // The first window gets cancel and the new gets pointer down (it already saw down)
4498 firstWindow->consumeMotionCancel();
4499 secondWindow->consumeMotionPointerDown(1);
4500
4501 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004502 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4503 ADISPLAY_ID_DEFAULT,
4504 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004505 // The first window gets nothing and the second gets pointer up
4506 firstWindow->assertNoEvents();
4507 secondWindow->consumeMotionPointerUp(1);
4508
4509 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004510 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4511 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004512 // The first window gets nothing and the second gets up
4513 firstWindow->assertNoEvents();
4514 secondWindow->consumeMotionUp();
4515}
4516
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004517// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4518// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4519// touch is not supported, so the touch should continue on those windows and the transferred-to
4520// window should get nothing.
4521TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4522 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4523
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004524 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004525 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4526 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004527 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004528
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004529 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004530 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4531 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004532 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004533
4534 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004535 mDispatcher->onWindowInfosChanged(
4536 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004537
4538 PointF pointInFirst = {300, 200};
4539 PointF pointInSecond = {300, 600};
4540
4541 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004542 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4543 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4544 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004545 // Only the first window should get the down event
4546 firstWindow->consumeMotionDown();
4547 secondWindow->assertNoEvents();
4548
4549 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004550 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4551 ADISPLAY_ID_DEFAULT,
4552 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004553 // The first window gets a move and the second a down
4554 firstWindow->consumeMotionMove();
4555 secondWindow->consumeMotionDown();
4556
4557 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004558 const bool transferred =
4559 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004560 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4561 ASSERT_FALSE(transferred);
4562 firstWindow->assertNoEvents();
4563 secondWindow->assertNoEvents();
4564
4565 // The rest of the dispatch should proceed as normal
4566 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004567 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4568 ADISPLAY_ID_DEFAULT,
4569 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004570 // The first window gets MOVE and the second gets pointer up
4571 firstWindow->consumeMotionMove();
4572 secondWindow->consumeMotionUp();
4573
4574 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004575 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4576 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004577 // The first window gets nothing and the second gets up
4578 firstWindow->consumeMotionUp();
4579 secondWindow->assertNoEvents();
4580}
4581
Arthur Hungabbb9d82021-09-01 14:52:30 +00004582// This case will create two windows and one mirrored window on the default display and mirror
4583// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4584// the windows info of second display before default display.
4585TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4586 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4587 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004588 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004589 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004590 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004591 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004592 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004593
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004594 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004595 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004596
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004597 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004598 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004599
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004600 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004601 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004602
4603 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004604 mDispatcher->onWindowInfosChanged(
4605 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
4606 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
4607 *secondWindowInPrimary->getInfo()},
4608 {},
4609 0,
4610 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00004611
4612 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004613 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungabbb9d82021-09-01 14:52:30 +00004614 {50, 50}))
4615 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4616
4617 // Window should receive motion event.
4618 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4619
4620 // Transfer touch focus
4621 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4622 secondWindowInPrimary->getToken()));
4623 // The first window gets cancel.
4624 firstWindowInPrimary->consumeMotionCancel();
4625 secondWindowInPrimary->consumeMotionDown();
4626
4627 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004628 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungabbb9d82021-09-01 14:52:30 +00004629 ADISPLAY_ID_DEFAULT, {150, 50}))
4630 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4631 firstWindowInPrimary->assertNoEvents();
4632 secondWindowInPrimary->consumeMotionMove();
4633
4634 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004635 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungabbb9d82021-09-01 14:52:30 +00004636 {150, 50}))
4637 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4638 firstWindowInPrimary->assertNoEvents();
4639 secondWindowInPrimary->consumeMotionUp();
4640}
4641
4642// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4643// 'transferTouch' api.
4644TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4645 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4646 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004647 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004648 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004649 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004650 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004651 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004652
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004653 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004654 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004655
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004656 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004657 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004658
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004659 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004660 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004661
4662 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004663 mDispatcher->onWindowInfosChanged(
4664 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
4665 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
4666 *secondWindowInPrimary->getInfo()},
4667 {},
4668 0,
4669 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00004670
4671 // Touch on second display.
4672 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004673 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
4674 {50, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00004675 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4676
4677 // Window should receive motion event.
4678 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4679
4680 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004681 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004682
4683 // The first window gets cancel.
4684 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4685 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4686
4687 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004688 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungabbb9d82021-09-01 14:52:30 +00004689 SECOND_DISPLAY_ID, {150, 50}))
4690 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4691 firstWindowInPrimary->assertNoEvents();
4692 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4693
4694 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004695 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00004696 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4697 firstWindowInPrimary->assertNoEvents();
4698 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4699}
4700
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004701TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004702 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004703 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4704 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004705
Vishnu Nair47074b82020-08-14 11:54:47 -07004706 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004707 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07004708 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004709
4710 window->consumeFocusEvent(true);
4711
Prabir Pradhan678438e2023-04-13 19:32:51 +00004712 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004713
4714 // Window should receive key down event.
4715 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004716
4717 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004718 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00004719 mFakePolicy->assertUserActivityPoked();
4720}
4721
4722TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4723 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4724 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4725 "Fake Window", ADISPLAY_ID_DEFAULT);
4726
4727 window->setDisableUserActivity(true);
4728 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004729 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00004730 setFocusedWindow(window);
4731
4732 window->consumeFocusEvent(true);
4733
4734 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4735
4736 // Window should receive key down event.
4737 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4738
4739 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004740 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00004741 mFakePolicy->assertUserActivityNotPoked();
4742}
4743
4744TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4745 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4746 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4747 "Fake Window", ADISPLAY_ID_DEFAULT);
4748
4749 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004750 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00004751 setFocusedWindow(window);
4752
4753 window->consumeFocusEvent(true);
4754
4755 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4756 mDispatcher->waitForIdle();
4757
4758 // System key is not passed down
4759 window->assertNoEvents();
4760
4761 // Should have poked user activity
4762 mFakePolicy->assertUserActivityPoked();
4763}
4764
4765TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4766 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4767 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4768 "Fake Window", ADISPLAY_ID_DEFAULT);
4769
4770 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004771 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00004772 setFocusedWindow(window);
4773
4774 window->consumeFocusEvent(true);
4775
4776 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4777 mDispatcher->waitForIdle();
4778
4779 // System key is not passed down
4780 window->assertNoEvents();
4781
4782 // Should have poked user activity
4783 mFakePolicy->assertUserActivityPoked();
4784}
4785
4786TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4787 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4788 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4789 "Fake Window", ADISPLAY_ID_DEFAULT);
4790
4791 window->setDisableUserActivity(true);
4792 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004793 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00004794 setFocusedWindow(window);
4795
4796 window->consumeFocusEvent(true);
4797
4798 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4799 mDispatcher->waitForIdle();
4800
4801 // System key is not passed down
4802 window->assertNoEvents();
4803
4804 // Should have poked user activity
4805 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004806}
4807
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004808TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
4809 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4810 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4811 "Fake Window", ADISPLAY_ID_DEFAULT);
4812
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004813 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004814
4815 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004816 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004817 ADISPLAY_ID_DEFAULT, {100, 100}))
4818 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4819
4820 window->consumeMotionEvent(
4821 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
4822
4823 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00004824 mDispatcher->waitForIdle();
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004825 mFakePolicy->assertUserActivityPoked();
4826}
4827
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004828TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004829 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004830 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4831 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004832
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004833 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004834
Prabir Pradhan678438e2023-04-13 19:32:51 +00004835 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004836 mDispatcher->waitForIdle();
4837
4838 window->assertNoEvents();
4839}
4840
4841// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4842TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004843 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004844 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4845 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004846
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004847 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004848
4849 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004850 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004851 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004852 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4853 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004854
4855 // Window should receive only the motion event
4856 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4857 window->assertNoEvents(); // Key event or focus event will not be received
4858}
4859
arthurhungea3f4fc2020-12-21 23:18:53 +08004860TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4861 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4862
arthurhungea3f4fc2020-12-21 23:18:53 +08004863 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004864 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4865 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004866 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004867
arthurhungea3f4fc2020-12-21 23:18:53 +08004868 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004869 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4870 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004871 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004872
4873 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004874 mDispatcher->onWindowInfosChanged(
4875 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
arthurhungea3f4fc2020-12-21 23:18:53 +08004876
4877 PointF pointInFirst = {300, 200};
4878 PointF pointInSecond = {300, 600};
4879
4880 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004881 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4882 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4883 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004884 // Only the first window should get the down event
4885 firstWindow->consumeMotionDown();
4886 secondWindow->assertNoEvents();
4887
4888 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004889 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4890 ADISPLAY_ID_DEFAULT,
4891 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004892 // The first window gets a move and the second a down
4893 firstWindow->consumeMotionMove();
4894 secondWindow->consumeMotionDown();
4895
4896 // Send pointer cancel to the second window
4897 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004898 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004899 {pointInFirst, pointInSecond});
4900 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004901 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004902 // The first window gets move and the second gets cancel.
4903 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4904 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4905
4906 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004907 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4908 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08004909 // The first window gets up and the second gets nothing.
4910 firstWindow->consumeMotionUp();
4911 secondWindow->assertNoEvents();
4912}
4913
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004914TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
4915 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4916
4917 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004918 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004919 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004920 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
4921 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
4922 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
4923
Harry Cutts33476232023-01-30 19:57:29 +00004924 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00004925 window->assertNoEvents();
4926 mDispatcher->waitForIdle();
4927}
4928
chaviwd1c23182019-12-20 18:44:56 -08004929class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00004930public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004931 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004932 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07004933 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004934 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07004935 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00004936 }
4937
chaviwd1c23182019-12-20 18:44:56 -08004938 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
4939
4940 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004941 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
4942 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08004943 }
4944
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004945 std::optional<int32_t> receiveEvent() {
4946 return mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED);
4947 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07004948
4949 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
4950
chaviwd1c23182019-12-20 18:44:56 -08004951 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004952 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08004953 expectedDisplayId, expectedFlags);
4954 }
4955
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004956 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004957 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004958 expectedDisplayId, expectedFlags);
4959 }
4960
chaviwd1c23182019-12-20 18:44:56 -08004961 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004962 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08004963 expectedDisplayId, expectedFlags);
4964 }
4965
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004966 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004967 mInputReceiver->consumeMotionEvent(
4968 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4969 WithDisplayId(expectedDisplayId),
4970 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004971 }
4972
Arthur Hungfbfa5722021-11-16 02:45:54 +00004973 void consumeMotionPointerDown(int32_t pointerIdx) {
4974 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
4975 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004976 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00004977 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00004978 }
4979
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004980 MotionEvent* consumeMotion() { return mInputReceiver->consumeMotion(); }
Evan Rosky84f07f02021-04-16 10:42:42 -07004981
chaviwd1c23182019-12-20 18:44:56 -08004982 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
4983
4984private:
4985 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00004986};
4987
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004988using InputDispatcherMonitorTest = InputDispatcherTest;
4989
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004990/**
4991 * Two entities that receive touch: A window, and a global monitor.
4992 * The touch goes to the window, and then the window disappears.
4993 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
4994 * for the monitor, as well.
4995 * 1. foregroundWindow
4996 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
4997 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08004998TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00004999 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5000 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005001 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005002
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005003 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005004
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005005 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005006 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005007 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005008 {100, 200}))
5009 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5010
5011 // Both the foreground window and the global monitor should receive the touch down
5012 window->consumeMotionDown();
5013 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5014
5015 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005016 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005017 ADISPLAY_ID_DEFAULT, {110, 200}))
5018 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5019
5020 window->consumeMotionMove();
5021 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5022
5023 // Now the foreground window goes away
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005024 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005025 window->consumeMotionCancel();
5026 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
5027
5028 // If more events come in, there will be no more foreground window to send them to. This will
5029 // cause a cancel for the monitor, as well.
5030 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005031 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005032 ADISPLAY_ID_DEFAULT, {120, 200}))
5033 << "Injection should fail because the window was removed";
5034 window->assertNoEvents();
5035 // Global monitor now gets the cancel
5036 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5037}
5038
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005039TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07005040 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005041 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5042 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005043 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00005044
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005045 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005046
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005047 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005048 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005049 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00005050 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005051 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005052}
5053
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005054TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
5055 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005056
Chris Yea209fde2020-07-22 13:54:51 -07005057 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005058 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5059 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005060 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00005061
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005062 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005063 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005064 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08005065 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005066 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005067
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005068 // Pilfer pointers from the monitor.
5069 // This should not do anything and the window should continue to receive events.
5070 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00005071
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005072 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005073 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005074 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005075 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005076
5077 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5078 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005079}
5080
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005081TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07005082 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005083 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5084 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005085 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Evan Rosky84f07f02021-04-16 10:42:42 -07005086 window->setWindowOffset(20, 40);
5087 window->setWindowTransform(0, 1, -1, 0);
5088
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005089 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005090
5091 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005092 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Evan Rosky84f07f02021-04-16 10:42:42 -07005093 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5094 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5095 MotionEvent* event = monitor.consumeMotion();
5096 // Even though window has transform, gesture monitor must not.
5097 ASSERT_EQ(ui::Transform(), event->getTransform());
5098}
5099
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005100TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00005101 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005102 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00005103
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005104 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005105 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005106 << "Injection should fail if there is a monitor, but no touchable window";
5107 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00005108}
5109
chaviw81e2bb92019-12-18 15:03:51 -08005110TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005111 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005112 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5113 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08005114
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005115 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
chaviw81e2bb92019-12-18 15:03:51 -08005116
5117 NotifyMotionArgs motionArgs =
5118 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5119 ADISPLAY_ID_DEFAULT);
5120
Prabir Pradhan678438e2023-04-13 19:32:51 +00005121 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005122 // Window should receive motion down event.
5123 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5124
5125 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005126 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005127 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5128 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5129 motionArgs.pointerCoords[0].getX() - 10);
5130
Prabir Pradhan678438e2023-04-13 19:32:51 +00005131 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005132 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005133 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005134}
5135
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005136/**
5137 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5138 * the device default right away. In the test scenario, we check both the default value,
5139 * and the action of enabling / disabling.
5140 */
5141TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005142 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005143 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5144 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005145 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005146
5147 // Set focused application.
5148 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005149 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005150
5151 SCOPED_TRACE("Check default value of touch mode");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005152 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005153 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005154 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005155
5156 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005157 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005158 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00005159 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005160
5161 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005162 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005163 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005164 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005165 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005166 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005167 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005168 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005169
5170 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005171 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005172 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00005173 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005174
5175 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005176 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005177 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005178 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005179 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005180 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005181 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005182 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005183
5184 window->assertNoEvents();
5185}
5186
Gang Wange9087892020-01-07 12:17:14 -05005187TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005188 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005189 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5190 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005191
5192 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005193 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005194
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005195 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005196 setFocusedWindow(window);
5197
Harry Cutts33476232023-01-30 19:57:29 +00005198 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005199
Prabir Pradhan678438e2023-04-13 19:32:51 +00005200 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5201 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005202
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005203 KeyEvent* event = window->consumeKey();
Gang Wange9087892020-01-07 12:17:14 -05005204 ASSERT_NE(event, nullptr);
5205
5206 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5207 ASSERT_NE(verified, nullptr);
5208 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5209
5210 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5211 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5212 ASSERT_EQ(keyArgs.source, verified->source);
5213 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5214
5215 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5216
5217 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005218 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005219 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005220 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5221 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5222 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5223 ASSERT_EQ(0, verifiedKey.repeatCount);
5224}
5225
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005226TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005227 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005228 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5229 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005230
5231 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5232
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005233 ui::Transform transform;
5234 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5235
5236 gui::DisplayInfo displayInfo;
5237 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5238 displayInfo.transform = transform;
5239
Patrick Williamsd828f302023-04-28 17:52:08 -05005240 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005241
Prabir Pradhan678438e2023-04-13 19:32:51 +00005242 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005243 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5244 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005245 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005246
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005247 MotionEvent* event = window->consumeMotion();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005248 ASSERT_NE(event, nullptr);
5249
5250 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5251 ASSERT_NE(verified, nullptr);
5252 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5253
5254 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5255 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5256 EXPECT_EQ(motionArgs.source, verified->source);
5257 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5258
5259 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5260
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005261 const vec2 rawXY =
5262 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5263 motionArgs.pointerCoords[0].getXYValue());
5264 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5265 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005266 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005267 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005268 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005269 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5270 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5271}
5272
chaviw09c8d2d2020-08-24 15:48:26 -07005273/**
5274 * Ensure that separate calls to sign the same data are generating the same key.
5275 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5276 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5277 * tests.
5278 */
5279TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5280 KeyEvent event = getTestKeyEvent();
5281 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5282
5283 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5284 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5285 ASSERT_EQ(hmac1, hmac2);
5286}
5287
5288/**
5289 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5290 */
5291TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5292 KeyEvent event = getTestKeyEvent();
5293 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5294 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5295
5296 verifiedEvent.deviceId += 1;
5297 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5298
5299 verifiedEvent.source += 1;
5300 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5301
5302 verifiedEvent.eventTimeNanos += 1;
5303 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5304
5305 verifiedEvent.displayId += 1;
5306 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5307
5308 verifiedEvent.action += 1;
5309 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5310
5311 verifiedEvent.downTimeNanos += 1;
5312 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5313
5314 verifiedEvent.flags += 1;
5315 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5316
5317 verifiedEvent.keyCode += 1;
5318 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5319
5320 verifiedEvent.scanCode += 1;
5321 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5322
5323 verifiedEvent.metaState += 1;
5324 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5325
5326 verifiedEvent.repeatCount += 1;
5327 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5328}
5329
Vishnu Nair958da932020-08-21 17:12:37 -07005330TEST_F(InputDispatcherTest, SetFocusedWindow) {
5331 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5332 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005333 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005334 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005335 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005336 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5337
5338 // Top window is also focusable but is not granted focus.
5339 windowTop->setFocusable(true);
5340 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005341 mDispatcher->onWindowInfosChanged(
5342 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005343 setFocusedWindow(windowSecond);
5344
5345 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005346 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005347 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005348
5349 // Focused window should receive event.
5350 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5351 windowTop->assertNoEvents();
5352}
5353
5354TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5355 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5356 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005357 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005358 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5359
5360 window->setFocusable(true);
5361 // Release channel for window is no longer valid.
5362 window->releaseChannel();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005363 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005364 setFocusedWindow(window);
5365
5366 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005367 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07005368
5369 // window channel is invalid, so it should not receive any input event.
5370 window->assertNoEvents();
5371}
5372
5373TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5374 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5375 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005376 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005377 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005378 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5379
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005380 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005381 setFocusedWindow(window);
5382
5383 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005384 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07005385
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005386 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005387 window->assertNoEvents();
5388}
5389
5390TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5391 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5392 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005393 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005394 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005395 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005396 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5397
5398 windowTop->setFocusable(true);
5399 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005400 mDispatcher->onWindowInfosChanged(
5401 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005402 setFocusedWindow(windowTop);
5403 windowTop->consumeFocusEvent(true);
5404
Chavi Weingarten847e8512023-03-29 00:26:09 +00005405 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005406 mDispatcher->onWindowInfosChanged(
5407 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005408 windowSecond->consumeFocusEvent(true);
5409 windowTop->consumeFocusEvent(false);
5410
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005411 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005412 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005413
5414 // Focused window should receive event.
5415 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5416}
5417
Chavi Weingarten847e8512023-03-29 00:26:09 +00005418TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005419 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5420 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005421 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005422 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005423 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005424 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5425
5426 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005427 windowSecond->setFocusable(false);
5428 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005429 mDispatcher->onWindowInfosChanged(
5430 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005431 setFocusedWindow(windowTop);
5432 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005433
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005434 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Chavi Weingarten847e8512023-03-29 00:26:09 +00005435 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005436
5437 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005438 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005439 windowSecond->assertNoEvents();
5440}
5441
5442TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5443 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5444 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005445 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005446 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005447 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5448 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005449 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5450
5451 window->setFocusable(true);
5452 previousFocusedWindow->setFocusable(true);
5453 window->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005454 mDispatcher->onWindowInfosChanged(
5455 {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005456 setFocusedWindow(previousFocusedWindow);
5457 previousFocusedWindow->consumeFocusEvent(true);
5458
5459 // Requesting focus on invisible window takes focus from currently focused window.
5460 setFocusedWindow(window);
5461 previousFocusedWindow->consumeFocusEvent(false);
5462
5463 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005464 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005465 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
5466 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005467
5468 // Window does not get focus event or key down.
5469 window->assertNoEvents();
5470
5471 // Window becomes visible.
5472 window->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005473 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005474
5475 // Window receives focus event.
5476 window->consumeFocusEvent(true);
5477 // Focused window receives key down.
5478 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5479}
5480
Vishnu Nair599f1412021-06-21 10:39:58 -07005481TEST_F(InputDispatcherTest, DisplayRemoved) {
5482 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5483 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005484 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005485 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5486
5487 // window is granted focus.
5488 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005489 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair599f1412021-06-21 10:39:58 -07005490 setFocusedWindow(window);
5491 window->consumeFocusEvent(true);
5492
5493 // When a display is removed window loses focus.
5494 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5495 window->consumeFocusEvent(false);
5496}
5497
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005498/**
5499 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5500 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5501 * of the 'slipperyEnterWindow'.
5502 *
5503 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5504 * a way so that the touched location is no longer covered by the top window.
5505 *
5506 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5507 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5508 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5509 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5510 * with ACTION_DOWN).
5511 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5512 * window moved itself away from the touched location and had Flag::SLIPPERY.
5513 *
5514 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5515 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5516 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5517 *
5518 * In this test, we ensure that the event received by the bottom window has
5519 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5520 */
5521TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005522 constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005523 constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1};
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005524
5525 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5526 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5527
5528 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005529 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005530 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005531 // Make sure this one overlaps the bottom window
5532 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5533 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5534 // one. Windows with the same owner are not considered to be occluding each other.
5535 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5536
5537 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005538 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005539 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5540
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005541 mDispatcher->onWindowInfosChanged(
5542 {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005543
5544 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005545 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5546 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5547 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005548 slipperyExitWindow->consumeMotionDown();
5549 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005550 mDispatcher->onWindowInfosChanged(
5551 {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005552
Prabir Pradhan678438e2023-04-13 19:32:51 +00005553 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5554 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5555 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005556
5557 slipperyExitWindow->consumeMotionCancel();
5558
5559 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5560 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5561}
5562
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005563/**
5564 * Two windows, one on the left and another on the right. The left window is slippery. The right
5565 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5566 * touch moves from the left window into the right window, the gesture should continue to go to the
5567 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5568 * reproduces a crash.
5569 */
5570TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5571 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5572
5573 sp<FakeWindowHandle> leftSlipperyWindow =
5574 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5575 leftSlipperyWindow->setSlippery(true);
5576 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5577
5578 sp<FakeWindowHandle> rightDropTouchesWindow =
5579 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5580 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5581 rightDropTouchesWindow->setDropInput(true);
5582
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005583 mDispatcher->onWindowInfosChanged(
5584 {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005585
5586 // Start touch in the left window
5587 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5588 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5589 .build());
5590 leftSlipperyWindow->consumeMotionDown();
5591
5592 // And move it into the right window
5593 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5594 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5595 .build());
5596
5597 // Since the right window isn't eligible to receive input, touch does not slip.
5598 // The left window continues to receive the gesture.
5599 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5600 rightDropTouchesWindow->assertNoEvents();
5601}
5602
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005603TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005604 using Uid = gui::Uid;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005605 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5606
5607 sp<FakeWindowHandle> leftWindow =
5608 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5609 leftWindow->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005610 leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005611
5612 sp<FakeWindowHandle> rightSpy =
5613 sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT);
5614 rightSpy->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005615 rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005616 rightSpy->setSpy(true);
5617 rightSpy->setTrustedOverlay(true);
5618
5619 sp<FakeWindowHandle> rightWindow =
5620 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5621 rightWindow->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005622 rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005623
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005624 mDispatcher->onWindowInfosChanged(
5625 {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005626
5627 // Touch in the left window
5628 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5629 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5630 .build());
5631 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown());
5632 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005633 ASSERT_NO_FATAL_FAILURE(
5634 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005635
5636 // Touch another finger over the right windows
5637 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5638 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5639 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5640 .build());
5641 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown());
5642 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown());
5643 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove());
5644 mDispatcher->waitForIdle();
5645 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005646 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID,
5647 {Uid{101}, Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005648
5649 // Release finger over left window. The UP actions are not treated as device interaction.
5650 // The windows that did not receive the UP pointer will receive MOVE events, but since this
5651 // is part of the UP action, we do not treat this as device interaction.
5652 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
5653 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5654 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5655 .build());
5656 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp());
5657 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
5658 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
5659 mDispatcher->waitForIdle();
5660 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5661
5662 // Move remaining finger
5663 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5664 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5665 .build());
5666 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
5667 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
5668 mDispatcher->waitForIdle();
5669 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005670 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005671
5672 // Release all fingers
5673 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
5674 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
5675 .build());
5676 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp());
5677 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp());
5678 mDispatcher->waitForIdle();
5679 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5680}
5681
5682TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) {
5683 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5684
5685 sp<FakeWindowHandle> window =
5686 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
5687 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005688 window->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005689
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005690 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005691 setFocusedWindow(window);
5692 ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true));
5693
5694 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build());
5695 ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT));
5696 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005697 ASSERT_NO_FATAL_FAILURE(
5698 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00005699
5700 // The UP actions are not treated as device interaction.
5701 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build());
5702 ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT));
5703 mDispatcher->waitForIdle();
5704 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
5705}
5706
Garfield Tan1c7bc862020-01-28 13:24:04 -08005707class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5708protected:
5709 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5710 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5711
Chris Yea209fde2020-07-22 13:54:51 -07005712 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005713 sp<FakeWindowHandle> mWindow;
5714
5715 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005716 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Prabir Pradhana41d2442023-04-20 21:30:40 +00005717 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Harry Cutts101ee9b2023-07-06 18:04:14 +00005718 mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false);
Nergi Rahardi730cf3c2023-04-13 12:41:17 +09005719 mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005720 ASSERT_EQ(OK, mDispatcher->start());
5721
5722 setUpWindow();
5723 }
5724
5725 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005726 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005727 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005728
Vishnu Nair47074b82020-08-14 11:54:47 -07005729 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005730 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005731 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005732 mWindow->consumeFocusEvent(true);
5733 }
5734
Chris Ye2ad95392020-09-01 13:44:44 -07005735 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005736 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005737 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005738 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005739 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005740
5741 // Window should receive key down event.
5742 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5743 }
5744
5745 void expectKeyRepeatOnce(int32_t repeatCount) {
5746 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005747 mWindow->consumeKeyEvent(
5748 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount)));
Garfield Tan1c7bc862020-01-28 13:24:04 -08005749 }
5750
Chris Ye2ad95392020-09-01 13:44:44 -07005751 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005752 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005753 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005754 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005755 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005756
5757 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005758 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005759 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005760 }
5761};
5762
5763TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005764 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005765 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5766 expectKeyRepeatOnce(repeatCount);
5767 }
5768}
5769
5770TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005771 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005772 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5773 expectKeyRepeatOnce(repeatCount);
5774 }
Harry Cutts33476232023-01-30 19:57:29 +00005775 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005776 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005777 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5778 expectKeyRepeatOnce(repeatCount);
5779 }
5780}
5781
5782TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005783 sendAndConsumeKeyDown(/*deviceId=*/1);
5784 expectKeyRepeatOnce(/*repeatCount=*/1);
5785 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005786 mWindow->assertNoEvents();
5787}
5788
5789TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005790 sendAndConsumeKeyDown(/*deviceId=*/1);
5791 expectKeyRepeatOnce(/*repeatCount=*/1);
5792 sendAndConsumeKeyDown(/*deviceId=*/2);
5793 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005794 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005795 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005796 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005797 expectKeyRepeatOnce(/*repeatCount=*/2);
5798 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005799 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005800 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005801 mWindow->assertNoEvents();
5802}
5803
5804TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005805 sendAndConsumeKeyDown(/*deviceId=*/1);
5806 expectKeyRepeatOnce(/*repeatCount=*/1);
5807 sendAndConsumeKeyDown(/*deviceId=*/2);
5808 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005809 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005810 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005811 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005812 mWindow->assertNoEvents();
5813}
5814
liushenxiang42232912021-05-21 20:24:09 +08005815TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5816 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005817 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005818 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005819 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5820 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5821 mWindow->assertNoEvents();
5822}
5823
Garfield Tan1c7bc862020-01-28 13:24:04 -08005824TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005825 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005826 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005827 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005828 KeyEvent* repeatEvent = mWindow->consumeKey();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005829 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5830 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5831 IdGenerator::getSource(repeatEvent->getId()));
5832 }
5833}
5834
5835TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005836 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005837 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005838
5839 std::unordered_set<int32_t> idSet;
5840 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005841 KeyEvent* repeatEvent = mWindow->consumeKey();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005842 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5843 int32_t id = repeatEvent->getId();
5844 EXPECT_EQ(idSet.end(), idSet.find(id));
5845 idSet.insert(id);
5846 }
5847}
5848
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005849/* Test InputDispatcher for MultiDisplay */
5850class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5851public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005852 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005853 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005854
Chris Yea209fde2020-07-22 13:54:51 -07005855 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005856 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005857 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005858
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005859 // Set focus window for primary display, but focused display would be second one.
5860 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005861 windowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005862 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
5863
Vishnu Nair958da932020-08-21 17:12:37 -07005864 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005865 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005866
Chris Yea209fde2020-07-22 13:54:51 -07005867 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005868 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005869 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005870 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005871 // Set focus display to second one.
5872 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5873 // Set focus window for second display.
5874 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005875 windowInSecondary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005876 mDispatcher->onWindowInfosChanged(
5877 {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005878 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005879 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005880 }
5881
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005882 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005883 InputDispatcherTest::TearDown();
5884
Chris Yea209fde2020-07-22 13:54:51 -07005885 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005886 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005887 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005888 windowInSecondary.clear();
5889 }
5890
5891protected:
Chris Yea209fde2020-07-22 13:54:51 -07005892 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005893 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005894 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005895 sp<FakeWindowHandle> windowInSecondary;
5896};
5897
5898TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5899 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005900 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005901 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005902 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005903 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005904 windowInSecondary->assertNoEvents();
5905
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005906 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005907 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005908 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005909 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005910 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005911 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005912}
5913
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005914TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005915 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005916 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005917 injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005918 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005919 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005920 windowInSecondary->assertNoEvents();
5921
5922 // Test inject a key down without display id specified.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005923 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005924 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005925 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005926 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005927
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005928 // Remove all windows in secondary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005929 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
Arthur Hungb92218b2018-08-14 12:00:21 +08005930
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005931 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005932 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005933 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005934
5935 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005936 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Arthur Hungb92218b2018-08-14 12:00:21 +08005937 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005938 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005939 windowInSecondary->assertNoEvents();
5940}
5941
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005942// Test per-display input monitors for motion event.
5943TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005944 FakeMonitorReceiver monitorInPrimary =
5945 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5946 FakeMonitorReceiver monitorInSecondary =
5947 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005948
5949 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005950 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005951 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005952 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005953 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005954 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005955 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005956 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005957
5958 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005959 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005960 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005961 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005962 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005963 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005964 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005965 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005966
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005967 // Lift up the touch from the second display
5968 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005969 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005970 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5971 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5972 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5973
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005974 // Test inject a non-pointer motion event.
5975 // If specific a display, it will dispatch to the focused window of particular display,
5976 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005977 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005978 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005979 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005980 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005981 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005982 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005983 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005984}
5985
5986// Test per-display input monitors for key event.
5987TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005988 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005989 FakeMonitorReceiver monitorInPrimary =
5990 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5991 FakeMonitorReceiver monitorInSecondary =
5992 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005993
5994 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005995 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005996 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005997 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005998 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005999 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08006000 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006001}
6002
Vishnu Nair958da932020-08-21 17:12:37 -07006003TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
6004 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006005 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006006 secondWindowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006007 mDispatcher->onWindowInfosChanged(
6008 {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(),
6009 *windowInSecondary->getInfo()},
6010 {},
6011 0,
6012 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006013 setFocusedWindow(secondWindowInPrimary);
6014 windowInPrimary->consumeFocusEvent(false);
6015 secondWindowInPrimary->consumeFocusEvent(true);
6016
6017 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006018 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6019 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006020 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006021 windowInPrimary->assertNoEvents();
6022 windowInSecondary->assertNoEvents();
6023 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6024}
6025
Arthur Hungdfd528e2021-12-08 13:23:04 +00006026TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
6027 FakeMonitorReceiver monitorInPrimary =
6028 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6029 FakeMonitorReceiver monitorInSecondary =
6030 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
6031
6032 // Test touch down on primary display.
6033 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006034 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungdfd528e2021-12-08 13:23:04 +00006035 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6036 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6037 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6038
6039 // Test touch down on second display.
6040 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006041 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungdfd528e2021-12-08 13:23:04 +00006042 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6043 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
6044 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
6045
6046 // Trigger cancel touch.
6047 mDispatcher->cancelCurrentTouch();
6048 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6049 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6050 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
6051 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
6052
6053 // Test inject a move motion event, no window/monitor should receive the event.
6054 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006055 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00006056 ADISPLAY_ID_DEFAULT, {110, 200}))
6057 << "Inject motion event should return InputEventInjectionResult::FAILED";
6058 windowInPrimary->assertNoEvents();
6059 monitorInPrimary.assertNoEvents();
6060
6061 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006062 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00006063 SECOND_DISPLAY_ID, {110, 200}))
6064 << "Inject motion event should return InputEventInjectionResult::FAILED";
6065 windowInSecondary->assertNoEvents();
6066 monitorInSecondary.assertNoEvents();
6067}
6068
Jackal Guof9696682018-10-05 12:23:23 +08006069class InputFilterTest : public InputDispatcherTest {
6070protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006071 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
6072 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08006073 NotifyMotionArgs motionArgs;
6074
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006075 motionArgs =
6076 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006077 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006078 motionArgs =
6079 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006080 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006081 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006082 if (expectToBeFiltered) {
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006083 const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue());
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006084 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08006085 } else {
6086 mFakePolicy->assertFilterInputEventWasNotCalled();
6087 }
6088 }
6089
6090 void testNotifyKey(bool expectToBeFiltered) {
6091 NotifyKeyArgs keyArgs;
6092
6093 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006094 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006095 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006096 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006097 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006098
6099 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08006100 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006101 } else {
6102 mFakePolicy->assertFilterInputEventWasNotCalled();
6103 }
6104 }
6105};
6106
6107// Test InputFilter for MotionEvent
6108TEST_F(InputFilterTest, MotionEvent_InputFilter) {
6109 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00006110 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false);
6111 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08006112
6113 // Enable InputFilter
6114 mDispatcher->setInputFilterEnabled(true);
6115 // Test touch on both primary and second display, and check if both events are filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00006116 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true);
6117 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08006118
6119 // Disable InputFilter
6120 mDispatcher->setInputFilterEnabled(false);
6121 // Test touch on both primary and second display, and check if both events aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00006122 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false);
6123 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08006124}
6125
6126// Test InputFilter for KeyEvent
6127TEST_F(InputFilterTest, KeyEvent_InputFilter) {
6128 // Since the InputFilter is disabled by default, check if key event aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00006129 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08006130
6131 // Enable InputFilter
6132 mDispatcher->setInputFilterEnabled(true);
6133 // Send a key event, and check if it is filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00006134 testNotifyKey(/*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08006135
6136 // Disable InputFilter
6137 mDispatcher->setInputFilterEnabled(false);
6138 // Send a key event, and check if it isn't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00006139 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08006140}
6141
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006142// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
6143// logical display coordinate space.
6144TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
6145 ui::Transform firstDisplayTransform;
6146 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6147 ui::Transform secondDisplayTransform;
6148 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
6149
6150 std::vector<gui::DisplayInfo> displayInfos(2);
6151 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
6152 displayInfos[0].transform = firstDisplayTransform;
6153 displayInfos[1].displayId = SECOND_DISPLAY_ID;
6154 displayInfos[1].transform = secondDisplayTransform;
6155
Patrick Williamsd828f302023-04-28 17:52:08 -05006156 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006157
6158 // Enable InputFilter
6159 mDispatcher->setInputFilterEnabled(true);
6160
6161 // Ensure the correct transforms are used for the displays.
Harry Cutts101ee9b2023-07-06 18:04:14 +00006162 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform);
6163 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform);
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006164}
6165
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006166class InputFilterInjectionPolicyTest : public InputDispatcherTest {
6167protected:
6168 virtual void SetUp() override {
6169 InputDispatcherTest::SetUp();
6170
6171 /**
6172 * We don't need to enable input filter to test the injected event policy, but we enabled it
6173 * here to make the tests more realistic, since this policy only matters when inputfilter is
6174 * on.
6175 */
6176 mDispatcher->setInputFilterEnabled(true);
6177
6178 std::shared_ptr<InputApplicationHandle> application =
6179 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006180 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
6181 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006182
6183 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6184 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006185 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006186 setFocusedWindow(mWindow);
6187 mWindow->consumeFocusEvent(true);
6188 }
6189
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006190 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6191 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006192 KeyEvent event;
6193
6194 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6195 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
6196 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00006197 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006198 const int32_t additionalPolicyFlags =
6199 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
6200 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006201 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00006202 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006203 policyFlags | additionalPolicyFlags));
6204
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006205 mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags)));
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006206 }
6207
6208 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6209 int32_t flags) {
6210 MotionEvent event;
6211 PointerProperties pointerProperties[1];
6212 PointerCoords pointerCoords[1];
6213 pointerProperties[0].clear();
6214 pointerProperties[0].id = 0;
6215 pointerCoords[0].clear();
6216 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6217 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6218
6219 ui::Transform identityTransform;
6220 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6221 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6222 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6223 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6224 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006225 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006226 eventTime,
Harry Cutts101ee9b2023-07-06 18:04:14 +00006227 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006228
6229 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6230 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006231 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00006232 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006233 policyFlags | additionalPolicyFlags));
6234
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006235 mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId)));
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006236 }
6237
6238private:
6239 sp<FakeWindowHandle> mWindow;
6240};
6241
6242TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006243 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6244 // filter. Without it, the event will no different from a regularly injected event, and the
6245 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006246 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6247 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006248}
6249
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006250TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006251 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006252 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006253 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6254}
6255
6256TEST_F(InputFilterInjectionPolicyTest,
6257 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6258 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006259 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006260 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006261}
6262
6263TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006264 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6265 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006266}
6267
chaviwfd6d3512019-03-25 13:23:49 -07006268class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006269 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006270 InputDispatcherTest::SetUp();
6271
Chris Yea209fde2020-07-22 13:54:51 -07006272 std::shared_ptr<FakeApplicationHandle> application =
6273 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006274 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006275 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006276 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006277
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006278 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006279 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006280 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006281
6282 // Set focused application.
6283 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006284 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006285
6286 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006287 mDispatcher->onWindowInfosChanged(
6288 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006289 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006290 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006291 }
6292
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006293 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006294 InputDispatcherTest::TearDown();
6295
6296 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006297 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006298 }
6299
6300protected:
6301 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006302 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006303 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006304};
6305
6306// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6307// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6308// the onPointerDownOutsideFocus callback.
6309TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006310 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006311 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006312 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006313 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006314 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006315
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006316 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006317 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6318}
6319
6320// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6321// DOWN on the window that doesn't have focus. Ensure no window received the
6322// onPointerDownOutsideFocus callback.
6323TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006324 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006325 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT,
6326 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006327 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006328 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006329
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006330 ASSERT_TRUE(mDispatcher->waitForIdle());
6331 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006332}
6333
6334// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6335// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6336TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006337 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006338 injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006339 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006340 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006341
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006342 ASSERT_TRUE(mDispatcher->waitForIdle());
6343 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006344}
6345
6346// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6347// DOWN on the window that already has focus. Ensure no window received the
6348// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006349TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006351 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006352 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006353 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006354 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006355
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006356 ASSERT_TRUE(mDispatcher->waitForIdle());
6357 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006358}
6359
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006360// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6361// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6362TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6363 const MotionEvent event =
6364 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6365 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006366 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006367 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6368 .build();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006369 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006370 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6371 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6372
6373 ASSERT_TRUE(mDispatcher->waitForIdle());
6374 mFakePolicy->assertOnPointerDownWasNotCalled();
6375 // Ensure that the unfocused window did not receive any FOCUS events.
6376 mUnfocusedWindow->assertNoEvents();
6377}
6378
chaviwaf87b3e2019-10-01 16:59:28 -07006379// These tests ensures we can send touch events to a single client when there are multiple input
6380// windows that point to the same client token.
6381class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6382 virtual void SetUp() override {
6383 InputDispatcherTest::SetUp();
6384
Chris Yea209fde2020-07-22 13:54:51 -07006385 std::shared_ptr<FakeApplicationHandle> application =
6386 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006387 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6388 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006389 mWindow1->setFrame(Rect(0, 0, 100, 100));
6390
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006391 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6392 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006393 mWindow2->setFrame(Rect(100, 100, 200, 200));
6394
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006395 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07006396 }
6397
6398protected:
6399 sp<FakeWindowHandle> mWindow1;
6400 sp<FakeWindowHandle> mWindow2;
6401
6402 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006403 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006404 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6405 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006406 }
6407
6408 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6409 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006410 const std::string name = window->getName();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006411 MotionEvent* motionEvent = window->consumeMotion();
chaviwaf87b3e2019-10-01 16:59:28 -07006412
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006413 ASSERT_NE(nullptr, motionEvent)
6414 << name.c_str() << ": consumer should have returned non-NULL event.";
chaviwaf87b3e2019-10-01 16:59:28 -07006415
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006416 assertMotionAction(expectedAction, motionEvent->getAction());
6417 ASSERT_EQ(points.size(), motionEvent->getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006418
6419 for (size_t i = 0; i < points.size(); i++) {
6420 float expectedX = points[i].x;
6421 float expectedY = points[i].y;
6422
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006423 EXPECT_EQ(expectedX, motionEvent->getX(i))
chaviwaf87b3e2019-10-01 16:59:28 -07006424 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006425 << ", got " << motionEvent->getX(i);
6426 EXPECT_EQ(expectedY, motionEvent->getY(i))
chaviwaf87b3e2019-10-01 16:59:28 -07006427 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006428 << ", got " << motionEvent->getY(i);
chaviwaf87b3e2019-10-01 16:59:28 -07006429 }
6430 }
chaviw9eaa22c2020-07-01 16:21:27 -07006431
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006432 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006433 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006434 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6435 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006436
6437 // Always consume from window1 since it's the window that has the InputReceiver
6438 consumeMotionEvent(mWindow1, action, expectedPoints);
6439 }
chaviwaf87b3e2019-10-01 16:59:28 -07006440};
6441
6442TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6443 // Touch Window 1
6444 PointF touchedPoint = {10, 10};
6445 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006446 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006447
6448 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006449 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006450
6451 // Touch Window 2
6452 touchedPoint = {150, 150};
6453 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006454 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006455}
6456
chaviw9eaa22c2020-07-01 16:21:27 -07006457TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6458 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006459 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006460 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07006461
6462 // Touch Window 1
6463 PointF touchedPoint = {10, 10};
6464 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006465 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006466 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006467 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006468
6469 // Touch Window 2
6470 touchedPoint = {150, 150};
6471 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006472 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6473 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006474
chaviw9eaa22c2020-07-01 16:21:27 -07006475 // Update the transform so rotation is set
6476 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006477 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07006478 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6479 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006480}
6481
chaviw9eaa22c2020-07-01 16:21:27 -07006482TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006483 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006484 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006485
6486 // Touch Window 1
6487 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6488 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006489 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006490
6491 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006492 touchedPoints.push_back(PointF{150, 150});
6493 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006494 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006495
chaviw9eaa22c2020-07-01 16:21:27 -07006496 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006497 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006498 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006499
chaviw9eaa22c2020-07-01 16:21:27 -07006500 // Update the transform so rotation is set for Window 2
6501 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006502 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07006503 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006504 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006505}
6506
chaviw9eaa22c2020-07-01 16:21:27 -07006507TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006508 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006509 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006510
6511 // Touch Window 1
6512 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6513 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006514 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006515
6516 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006517 touchedPoints.push_back(PointF{150, 150});
6518 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006519
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006520 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006521
6522 // Move both windows
6523 touchedPoints = {{20, 20}, {175, 175}};
6524 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6525 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6526
chaviw9eaa22c2020-07-01 16:21:27 -07006527 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006528
chaviw9eaa22c2020-07-01 16:21:27 -07006529 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006530 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006531 expectedPoints.pop_back();
6532
6533 // Touch Window 2
6534 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006535 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07006536 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006537 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006538
6539 // Move both windows
6540 touchedPoints = {{20, 20}, {175, 175}};
6541 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6542 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6543
6544 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006545}
6546
6547TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6548 mWindow1->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006549 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006550
6551 // Touch Window 1
6552 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6553 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006554 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006555
6556 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006557 touchedPoints.push_back(PointF{150, 150});
6558 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006559
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006560 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006561
6562 // Move both windows
6563 touchedPoints = {{20, 20}, {175, 175}};
6564 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6565 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6566
chaviw9eaa22c2020-07-01 16:21:27 -07006567 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006568}
6569
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006570/**
6571 * When one of the windows is slippery, the touch should not slip into the other window with the
6572 * same input channel.
6573 */
6574TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6575 mWindow1->setSlippery(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006576 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006577
6578 // Touch down in window 1
6579 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6580 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6581 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6582
6583 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6584 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6585 // getting generated.
6586 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6587 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6588
6589 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6590}
6591
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07006592/**
6593 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
6594 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
6595 * that the pointer is hovering over may have a different transform.
6596 */
6597TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006598 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07006599
6600 // Start hover in window 1
6601 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN,
6602 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6603 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6604 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
6605
6606 // Move hover to window 2.
6607 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6608 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6609
6610 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
6611 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6612 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
6613}
6614
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006615class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6616 virtual void SetUp() override {
6617 InputDispatcherTest::SetUp();
6618
Chris Yea209fde2020-07-22 13:54:51 -07006619 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07006620 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006621 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6622 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006623 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07006624 mWindow->setDispatchingTimeout(100ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006625 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006626
6627 // Set focused application.
6628 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6629
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006630 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006631 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006632 mWindow->consumeFocusEvent(true);
6633 }
6634
6635 virtual void TearDown() override {
6636 InputDispatcherTest::TearDown();
6637 mWindow.clear();
6638 }
6639
6640protected:
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006641 static constexpr std::chrono::duration SPY_TIMEOUT = 200ms;
Chris Yea209fde2020-07-22 13:54:51 -07006642 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006643 sp<FakeWindowHandle> mWindow;
6644 static constexpr PointF WINDOW_LOCATION = {20, 20};
6645
6646 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006647 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006648 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006649 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006650 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006651 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006652 WINDOW_LOCATION));
6653 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006654
6655 sp<FakeWindowHandle> addSpyWindow() {
6656 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006657 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006658 spy->setTrustedOverlay(true);
6659 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006660 spy->setSpy(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006661 spy->setDispatchingTimeout(SPY_TIMEOUT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006662 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006663 return spy;
6664 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006665};
6666
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006667// Send a tap and respond, which should not cause an ANR.
6668TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6669 tapOnWindow();
6670 mWindow->consumeMotionDown();
6671 mWindow->consumeMotionUp();
6672 ASSERT_TRUE(mDispatcher->waitForIdle());
6673 mFakePolicy->assertNotifyAnrWasNotCalled();
6674}
6675
6676// Send a regular key and respond, which should not cause an ANR.
6677TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006678 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006679 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6680 ASSERT_TRUE(mDispatcher->waitForIdle());
6681 mFakePolicy->assertNotifyAnrWasNotCalled();
6682}
6683
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006684TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6685 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006686 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006687 mWindow->consumeFocusEvent(false);
6688
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006689 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006690 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6691 InputEventInjectionSync::NONE, CONSUME_TIMEOUT_EVENT_EXPECTED,
Harry Cutts33476232023-01-30 19:57:29 +00006692 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006693 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006694 // Key will not go to window because we have no focused window.
6695 // The 'no focused window' ANR timer should start instead.
6696
6697 // Now, the focused application goes away.
6698 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6699 // The key should get dropped and there should be no ANR.
6700
6701 ASSERT_TRUE(mDispatcher->waitForIdle());
6702 mFakePolicy->assertNotifyAnrWasNotCalled();
6703}
6704
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006705// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006706// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6707// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006708TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006709 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006710 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006711 WINDOW_LOCATION));
6712
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006713 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6714 ASSERT_TRUE(sequenceNum);
6715 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006716 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006717
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006718 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006719 mWindow->consumeMotionEvent(
6720 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006721 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006722 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006723}
6724
6725// Send a key to the app and have the app not respond right away.
6726TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6727 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006728 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006729 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6730 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006731 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006732 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006733 ASSERT_TRUE(mDispatcher->waitForIdle());
6734}
6735
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006736// We have a focused application, but no focused window
6737TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006738 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006739 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006740 mWindow->consumeFocusEvent(false);
6741
6742 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006743 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006744 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006745 WINDOW_LOCATION));
6746 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6747 mDispatcher->waitForIdle();
6748 mFakePolicy->assertNotifyAnrWasNotCalled();
6749
6750 // Once a focused event arrives, we get an ANR for this application
6751 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6752 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006753 const InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006754 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07006755 InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006756 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006757 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006758 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006759 ASSERT_TRUE(mDispatcher->waitForIdle());
6760}
6761
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006762/**
6763 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6764 * there will not be an ANR.
6765 */
6766TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6767 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006768 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006769 mWindow->consumeFocusEvent(false);
6770
6771 KeyEvent event;
6772 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6773 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6774
6775 // Define a valid key down event that is stale (too old).
6776 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
Harry Cutts101ee9b2023-07-06 18:04:14 +00006777 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /*flags=*/0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006778 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006779
6780 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6781
6782 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006783 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006784 InputEventInjectionSync::WAIT_FOR_RESULT,
6785 INJECT_EVENT_TIMEOUT, policyFlags);
6786 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6787 << "Injection should fail because the event is stale";
6788
6789 ASSERT_TRUE(mDispatcher->waitForIdle());
6790 mFakePolicy->assertNotifyAnrWasNotCalled();
6791 mWindow->assertNoEvents();
6792}
6793
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006794// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006795// Make sure that we don't notify policy twice about the same ANR.
6796TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006797 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006798 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006799 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006800
6801 // Once a focused event arrives, we get an ANR for this application
6802 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6803 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006804 const InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006805 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6806 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006807 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006808 const std::chrono::duration appTimeout =
6809 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6810 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006811
Vishnu Naire4df8752022-09-08 09:17:55 -07006812 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006813 // ANR should not be raised again. It is up to policy to do that if it desires.
6814 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006815
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006816 // If we now get a focused window, the ANR should stop, but the policy handles that via
6817 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006818 ASSERT_TRUE(mDispatcher->waitForIdle());
6819}
6820
6821// We have a focused application, but no focused window
6822TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006823 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006824 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006825 mWindow->consumeFocusEvent(false);
6826
6827 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006828 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006829
Vishnu Naire4df8752022-09-08 09:17:55 -07006830 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6831 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006832
6833 // Future focused events get dropped right away
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006834 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006835 ASSERT_TRUE(mDispatcher->waitForIdle());
6836 mWindow->assertNoEvents();
6837}
6838
6839/**
6840 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6841 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6842 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6843 * the ANR mechanism should still work.
6844 *
6845 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6846 * DOWN event, while not responding on the second one.
6847 */
6848TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6849 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006850 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006851 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6852 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6853 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006854 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006855
6856 // Now send ACTION_UP, with identical timestamp
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006857 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006858 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6859 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6860 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006861 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006862
6863 // We have now sent down and up. Let's consume first event and then ANR on the second.
6864 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6865 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006866 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006867}
6868
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006869// A spy window can receive an ANR
6870TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6871 sp<FakeWindowHandle> spy = addSpyWindow();
6872
6873 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006874 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006875 WINDOW_LOCATION));
6876 mWindow->consumeMotionDown();
6877
6878 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6879 ASSERT_TRUE(sequenceNum);
6880 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006881 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006882
6883 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006884 spy->consumeMotionEvent(
6885 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006886 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006887 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006888}
6889
6890// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006891// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006892TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6893 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006894
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006895 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006896 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006897 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006898 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006899
6900 // Stuck on the ACTION_UP
6901 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006902 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006903
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006904 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006905 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006906 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6907 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006908
6909 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6910 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006911 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006912 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006913 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006914}
6915
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006916// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006917// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006918TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6919 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006920
6921 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006922 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6923 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006924
6925 mWindow->consumeMotionDown();
6926 // Stuck on the ACTION_UP
6927 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006928 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006929
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006930 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006931 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006932 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6933 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006934
6935 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6936 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006937 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006938 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006939 spy->assertNoEvents();
6940}
6941
6942TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006943 mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006944
6945 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6946
6947 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006948 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006949 WINDOW_LOCATION));
6950
6951 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6952 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6953 ASSERT_TRUE(consumeSeq);
6954
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006955 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(),
6956 MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006957
6958 monitor.finishEvent(*consumeSeq);
6959 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6960
6961 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006962 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006963}
6964
6965// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6966// process events, you don't get an anr. When the window later becomes unresponsive again, you
6967// get an ANR again.
6968// 1. tap -> block on ACTION_UP -> receive ANR
6969// 2. consume all pending events (= queue becomes healthy again)
6970// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6971TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6972 tapOnWindow();
6973
6974 mWindow->consumeMotionDown();
6975 // Block on ACTION_UP
6976 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006977 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006978 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6979 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006980 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006981 mWindow->assertNoEvents();
6982
6983 tapOnWindow();
6984 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006985 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006986 mWindow->consumeMotionUp();
6987
6988 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006989 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006990 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006991 mWindow->assertNoEvents();
6992}
6993
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006994// If a connection remains unresponsive for a while, make sure policy is only notified once about
6995// it.
6996TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006997 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006998 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006999 WINDOW_LOCATION));
7000
7001 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007002 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007003 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007004 // 'notifyConnectionUnresponsive' should only be called once per connection
7005 mFakePolicy->assertNotifyAnrWasNotCalled();
7006 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007007 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08007008 mWindow->consumeMotionEvent(
7009 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007010 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007011 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007012 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007013 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007014}
7015
7016/**
7017 * If a window is processing a motion event, and then a key event comes in, the key event should
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007018 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007019 *
7020 * Warning!!!
7021 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7022 * and the injection timeout that we specify when injecting the key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007023 * We must have the injection timeout (100ms) be smaller than
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007024 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7025 *
7026 * If that value changes, this test should also change.
7027 */
7028TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
7029 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007030 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007031
7032 tapOnWindow();
7033 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7034 ASSERT_TRUE(downSequenceNum);
7035 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7036 ASSERT_TRUE(upSequenceNum);
7037 // Don't finish the events yet, and send a key
7038 // Injection will "succeed" because we will eventually give up and send the key to the focused
7039 // window even if motions are still being processed. But because the injection timeout is short,
7040 // we will receive INJECTION_TIMED_OUT as the result.
7041
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007042 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007043 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7044 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007045 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007046 // Key will not be sent to the window, yet, because the window is still processing events
7047 // and the key remains pending, waiting for the touch events to be processed
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007048 // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR.
7049 // Rely here on the fact that it uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood.
7050 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
7051 mWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007052
7053 std::this_thread::sleep_for(500ms);
7054 // if we wait long enough though, dispatcher will give up, and still send the key
7055 // to the focused window, even though we have not yet finished the motion event
7056 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7057 mWindow->finishEvent(*downSequenceNum);
7058 mWindow->finishEvent(*upSequenceNum);
7059}
7060
7061/**
7062 * If a window is processing a motion event, and then a key event comes in, the key event should
7063 * not go to the focused window until the motion is processed.
7064 * If then a new motion comes in, then the pending key event should be going to the currently
7065 * focused window right away.
7066 */
7067TEST_F(InputDispatcherSingleWindowAnr,
7068 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
7069 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007070 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007071
7072 tapOnWindow();
7073 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7074 ASSERT_TRUE(downSequenceNum);
7075 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7076 ASSERT_TRUE(upSequenceNum);
7077 // Don't finish the events yet, and send a key
7078 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007079 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007080 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
7081 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007082 // At this point, key is still pending, and should not be sent to the application yet.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007083 // Make sure the `assertNoEvents` check doesn't take too long. It uses
7084 // CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood.
7085 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
7086 mWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007087
7088 // Now tap down again. It should cause the pending key to go to the focused window right away.
7089 tapOnWindow();
7090 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
7091 // the other events yet. We can finish events in any order.
7092 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
7093 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
7094 mWindow->consumeMotionDown();
7095 mWindow->consumeMotionUp();
7096 mWindow->assertNoEvents();
7097}
7098
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07007099/**
7100 * Send an event to the app and have the app not respond right away.
7101 * When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
7102 * So InputDispatcher will enqueue ACTION_CANCEL event as well.
7103 * At some point, the window becomes responsive again.
7104 * Ensure that subsequent events get dropped, and the next gesture is delivered.
7105 */
7106TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) {
7107 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7108 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
7109 .build());
7110
7111 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
7112 ASSERT_TRUE(sequenceNum);
7113 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
7114 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
7115
7116 mWindow->finishEvent(*sequenceNum);
7117 mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
7118 ASSERT_TRUE(mDispatcher->waitForIdle());
7119 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
7120
7121 // Now that the window is responsive, let's continue the gesture.
7122 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7123 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7124 .build());
7125
7126 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7127 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7128 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
7129 .build());
7130
7131 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
7132 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7133 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
7134 .build());
7135 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
7136 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
7137 .build());
7138 // We already canceled this pointer, so the window shouldn't get any new events.
7139 mWindow->assertNoEvents();
7140
7141 // Start another one.
7142 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7143 .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15))
7144 .build());
7145 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
7146}
7147
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007148class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
7149 virtual void SetUp() override {
7150 InputDispatcherTest::SetUp();
7151
Chris Yea209fde2020-07-22 13:54:51 -07007152 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07007153 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007154 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
7155 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007156 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007157 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007158 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007159
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007160 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
7161 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07007162 mFocusedWindow->setDispatchingTimeout(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007163 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007164
7165 // Set focused application.
7166 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07007167 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007168
7169 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007170 mDispatcher->onWindowInfosChanged(
7171 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007172 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007173 mFocusedWindow->consumeFocusEvent(true);
7174 }
7175
7176 virtual void TearDown() override {
7177 InputDispatcherTest::TearDown();
7178
7179 mUnfocusedWindow.clear();
7180 mFocusedWindow.clear();
7181 }
7182
7183protected:
Chris Yea209fde2020-07-22 13:54:51 -07007184 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007185 sp<FakeWindowHandle> mUnfocusedWindow;
7186 sp<FakeWindowHandle> mFocusedWindow;
7187 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
7188 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
7189 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
7190
7191 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
7192
7193 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
7194
7195private:
7196 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007197 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007198 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007199 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007200 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007201 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007202 location));
7203 }
7204};
7205
7206// If we have 2 windows that are both unresponsive, the one with the shortest timeout
7207// should be ANR'd first.
7208TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007209 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007210 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007211 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007212 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007213 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007214 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007215 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007216 // We consumed all events, so no ANR
7217 ASSERT_TRUE(mDispatcher->waitForIdle());
7218 mFakePolicy->assertNotifyAnrWasNotCalled();
7219
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007220 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007221 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007222 FOCUSED_WINDOW_LOCATION));
7223 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
7224 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007225
7226 const std::chrono::duration timeout =
7227 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007228 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007229 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
7230 // sequence to make it consistent
7231 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007232 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007233 mFocusedWindow->consumeMotionDown();
7234 // This cancel is generated because the connection was unresponsive
7235 mFocusedWindow->consumeMotionCancel();
7236 mFocusedWindow->assertNoEvents();
7237 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007238 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007239 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7240 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007241 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007242}
7243
7244// If we have 2 windows with identical timeouts that are both unresponsive,
7245// it doesn't matter which order they should have ANR.
7246// But we should receive ANR for both.
7247TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
7248 // Set the timeout for unfocused window to match the focused window
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07007249 mUnfocusedWindow->setDispatchingTimeout(
7250 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007251 mDispatcher->onWindowInfosChanged(
7252 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007253
7254 tapOnFocusedWindow();
7255 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007256 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07007257 std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken(
7258 mFocusedWindow->getDispatchingTimeout(
7259 DISPATCHING_TIMEOUT)),
7260 mFakePolicy->getUnresponsiveWindowToken(0ms)};
7261
7262 ASSERT_THAT(anrConnectionTokens,
7263 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
7264 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007265
7266 ASSERT_TRUE(mDispatcher->waitForIdle());
7267 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007268
7269 mFocusedWindow->consumeMotionDown();
7270 mFocusedWindow->consumeMotionUp();
7271 mUnfocusedWindow->consumeMotionOutside();
7272
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07007273 std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(),
7274 mFakePolicy->getResponsiveWindowToken()};
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007275
7276 // Both applications should be marked as responsive, in any order
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07007277 ASSERT_THAT(responsiveTokens,
7278 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
7279 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007280 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007281}
7282
7283// If a window is already not responding, the second tap on the same window should be ignored.
7284// We should also log an error to account for the dropped event (not tested here).
7285// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7286TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7287 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007288 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007289 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007290 // Receive the events, but don't respond
7291 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7292 ASSERT_TRUE(downEventSequenceNum);
7293 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7294 ASSERT_TRUE(upEventSequenceNum);
7295 const std::chrono::duration timeout =
7296 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007297 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007298
7299 // Tap once again
7300 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007301 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007302 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007303 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007304 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007305 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007306 FOCUSED_WINDOW_LOCATION));
7307 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7308 // valid touch target
7309 mUnfocusedWindow->assertNoEvents();
7310
7311 // Consume the first tap
7312 mFocusedWindow->finishEvent(*downEventSequenceNum);
7313 mFocusedWindow->finishEvent(*upEventSequenceNum);
7314 ASSERT_TRUE(mDispatcher->waitForIdle());
7315 // The second tap did not go to the focused window
7316 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007317 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007318 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7319 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007320 mFakePolicy->assertNotifyAnrWasNotCalled();
7321}
7322
7323// If you tap outside of all windows, there will not be ANR
7324TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007325 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007326 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007327 LOCATION_OUTSIDE_ALL_WINDOWS));
7328 ASSERT_TRUE(mDispatcher->waitForIdle());
7329 mFakePolicy->assertNotifyAnrWasNotCalled();
7330}
7331
7332// Since the focused window is paused, tapping on it should not produce any events
7333TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7334 mFocusedWindow->setPaused(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007335 mDispatcher->onWindowInfosChanged(
7336 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007337
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007338 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007339 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007340 FOCUSED_WINDOW_LOCATION));
7341
7342 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7343 ASSERT_TRUE(mDispatcher->waitForIdle());
7344 // Should not ANR because the window is paused, and touches shouldn't go to it
7345 mFakePolicy->assertNotifyAnrWasNotCalled();
7346
7347 mFocusedWindow->assertNoEvents();
7348 mUnfocusedWindow->assertNoEvents();
7349}
7350
7351/**
7352 * If a window is processing a motion event, and then a key event comes in, the key event should
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007353 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007354 * If a different window becomes focused at this time, the key should go to that window instead.
7355 *
7356 * Warning!!!
7357 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7358 * and the injection timeout that we specify when injecting the key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007359 * We must have the injection timeout (100ms) be smaller than
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007360 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7361 *
7362 * If that value changes, this test should also change.
7363 */
7364TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7365 // Set a long ANR timeout to prevent it from triggering
7366 mFocusedWindow->setDispatchingTimeout(2s);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007367 mDispatcher->onWindowInfosChanged(
7368 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007369
7370 tapOnUnfocusedWindow();
7371 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7372 ASSERT_TRUE(downSequenceNum);
7373 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7374 ASSERT_TRUE(upSequenceNum);
7375 // Don't finish the events yet, and send a key
7376 // Injection will succeed because we will eventually give up and send the key to the focused
7377 // window even if motions are still being processed.
7378
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007379 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007380 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7381 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007382 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007383 // Key will not be sent to the window, yet, because the window is still processing events
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007384 // and the key remains pending, waiting for the touch events to be processed.
7385 // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED
7386 // under the hood.
7387 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
7388 mFocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007389
7390 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007391 mFocusedWindow->setFocusable(false);
7392 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007393 mDispatcher->onWindowInfosChanged(
7394 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007395 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007396
7397 // Focus events should precede the key events
7398 mUnfocusedWindow->consumeFocusEvent(true);
7399 mFocusedWindow->consumeFocusEvent(false);
7400
7401 // Finish the tap events, which should unblock dispatcher
7402 mUnfocusedWindow->finishEvent(*downSequenceNum);
7403 mUnfocusedWindow->finishEvent(*upSequenceNum);
7404
7405 // Now that all queues are cleared and no backlog in the connections, the key event
7406 // can finally go to the newly focused "mUnfocusedWindow".
7407 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7408 mFocusedWindow->assertNoEvents();
7409 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007410 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007411}
7412
7413// When the touch stream is split across 2 windows, and one of them does not respond,
7414// then ANR should be raised and the touch should be canceled for the unresponsive window.
7415// The other window should not be affected by that.
7416TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7417 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007418 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7419 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7420 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007421 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007422 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007423
7424 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007425 mDispatcher->notifyMotion(
7426 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7427 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007428
7429 const std::chrono::duration timeout =
7430 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007431 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007432
7433 mUnfocusedWindow->consumeMotionDown();
7434 mFocusedWindow->consumeMotionDown();
7435 // Focused window may or may not receive ACTION_MOVE
7436 // But it should definitely receive ACTION_CANCEL due to the ANR
7437 InputEvent* event;
7438 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7439 ASSERT_TRUE(moveOrCancelSequenceNum);
7440 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7441 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007442 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007443 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7444 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7445 mFocusedWindow->consumeMotionCancel();
7446 } else {
7447 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7448 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007449 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007450 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7451 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007452
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007453 mUnfocusedWindow->assertNoEvents();
7454 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007455 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007456}
7457
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007458/**
7459 * If we have no focused window, and a key comes in, we start the ANR timer.
7460 * The focused application should add a focused window before the timer runs out to prevent ANR.
7461 *
7462 * If the user touches another application during this time, the key should be dropped.
7463 * Next, if a new focused window comes in, without toggling the focused application,
7464 * then no ANR should occur.
7465 *
7466 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7467 * but in some cases the policy may not update the focused application.
7468 */
7469TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7470 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7471 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007472 focusedApplication->setDispatchingTimeout(200ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007473 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7474 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7475 mFocusedWindow->setFocusable(false);
7476
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007477 mDispatcher->onWindowInfosChanged(
7478 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007479 mFocusedWindow->consumeFocusEvent(false);
7480
7481 // Send a key. The ANR timer should start because there is no focused window.
7482 // 'focusedApplication' will get blamed if this timer completes.
7483 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007484 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007485 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7486 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms,
Harry Cutts33476232023-01-30 19:57:29 +00007487 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007488 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007489
7490 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7491 // then the injected touches won't cause the focused event to get dropped.
7492 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7493 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7494 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7495 // For this test, it means that the key would get delivered to the window once it becomes
7496 // focused.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007497 std::this_thread::sleep_for(100ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007498
7499 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007500 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7501 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7502 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007503
7504 // We do not consume the motion right away, because that would require dispatcher to first
7505 // process (== drop) the key event, and by that time, ANR will be raised.
7506 // Set the focused window first.
7507 mFocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007508 mDispatcher->onWindowInfosChanged(
7509 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007510 setFocusedWindow(mFocusedWindow);
7511 mFocusedWindow->consumeFocusEvent(true);
7512 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7513 // to another application. This could be a bug / behaviour in the policy.
7514
7515 mUnfocusedWindow->consumeMotionDown();
7516
7517 ASSERT_TRUE(mDispatcher->waitForIdle());
7518 // Should not ANR because we actually have a focused window. It was just added too slowly.
7519 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7520}
7521
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007522// These tests ensure we cannot send touch events to a window that's positioned behind a window
7523// that has feature NO_INPUT_CHANNEL.
7524// Layout:
7525// Top (closest to user)
7526// mNoInputWindow (above all windows)
7527// mBottomWindow
7528// Bottom (furthest from user)
7529class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7530 virtual void SetUp() override {
7531 InputDispatcherTest::SetUp();
7532
7533 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007534 mNoInputWindow =
7535 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7536 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007537 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007538 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007539 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7540 // It's perfectly valid for this window to not have an associated input channel
7541
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007542 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7543 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007544 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7545
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007546 mDispatcher->onWindowInfosChanged(
7547 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007548 }
7549
7550protected:
7551 std::shared_ptr<FakeApplicationHandle> mApplication;
7552 sp<FakeWindowHandle> mNoInputWindow;
7553 sp<FakeWindowHandle> mBottomWindow;
7554};
7555
7556TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7557 PointF touchedPoint = {10, 10};
7558
Prabir Pradhan678438e2023-04-13 19:32:51 +00007559 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7560 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7561 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007562
7563 mNoInputWindow->assertNoEvents();
7564 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7565 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7566 // and therefore should prevent mBottomWindow from receiving touches
7567 mBottomWindow->assertNoEvents();
7568}
7569
7570/**
7571 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7572 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7573 */
7574TEST_F(InputDispatcherMultiWindowOcclusionTests,
7575 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007576 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7577 "Window with input channel and NO_INPUT_CHANNEL",
7578 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007579
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007580 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007581 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007582 mDispatcher->onWindowInfosChanged(
7583 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007584
7585 PointF touchedPoint = {10, 10};
7586
Prabir Pradhan678438e2023-04-13 19:32:51 +00007587 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7588 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7589 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007590
7591 mNoInputWindow->assertNoEvents();
7592 mBottomWindow->assertNoEvents();
7593}
7594
Vishnu Nair958da932020-08-21 17:12:37 -07007595class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7596protected:
7597 std::shared_ptr<FakeApplicationHandle> mApp;
7598 sp<FakeWindowHandle> mWindow;
7599 sp<FakeWindowHandle> mMirror;
7600
7601 virtual void SetUp() override {
7602 InputDispatcherTest::SetUp();
7603 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007604 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7605 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7606 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007607 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7608 mWindow->setFocusable(true);
7609 mMirror->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007610 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007611 }
7612};
7613
7614TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7615 // Request focus on a mirrored window
7616 setFocusedWindow(mMirror);
7617
7618 // window gets focused
7619 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007620 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007621 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007622 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7623}
7624
7625// A focused & mirrored window remains focused only if the window and its mirror are both
7626// focusable.
7627TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7628 setFocusedWindow(mMirror);
7629
7630 // window gets focused
7631 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007632 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007633 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007634 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007635 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007636 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007637 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7638
7639 mMirror->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007640 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007641
7642 // window loses focus since one of the windows associated with the token in not focusable
7643 mWindow->consumeFocusEvent(false);
7644
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007645 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007646 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007647 mWindow->assertNoEvents();
7648}
7649
7650// A focused & mirrored window remains focused until the window and its mirror both become
7651// invisible.
7652TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7653 setFocusedWindow(mMirror);
7654
7655 // window gets focused
7656 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007657 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007658 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007659 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007660 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007661 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007662 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7663
7664 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007665 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007666
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007667 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007668 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007669 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007670 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007671 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007672 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7673
7674 mWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007675 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007676
7677 // window loses focus only after all windows associated with the token become invisible.
7678 mWindow->consumeFocusEvent(false);
7679
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007680 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007681 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007682 mWindow->assertNoEvents();
7683}
7684
7685// A focused & mirrored window remains focused until both windows are removed.
7686TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7687 setFocusedWindow(mMirror);
7688
7689 // window gets focused
7690 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007691 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007692 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007693 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007694 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007695 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007696 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7697
7698 // single window is removed but the window token remains focused
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007699 mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007700
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007701 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007702 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007703 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007704 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007705 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007706 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7707
7708 // Both windows are removed
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007709 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007710 mWindow->consumeFocusEvent(false);
7711
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007712 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007713 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007714 mWindow->assertNoEvents();
7715}
7716
7717// Focus request can be pending until one window becomes visible.
7718TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7719 // Request focus on an invisible mirror.
7720 mWindow->setVisible(false);
7721 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007722 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007723 setFocusedWindow(mMirror);
7724
7725 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007726 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007727 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
7728 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007729
7730 mMirror->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007731 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007732
7733 // window gets focused
7734 mWindow->consumeFocusEvent(true);
7735 // window gets the pending key event
7736 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7737}
Prabir Pradhan99987712020-11-10 18:43:05 -08007738
7739class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7740protected:
7741 std::shared_ptr<FakeApplicationHandle> mApp;
7742 sp<FakeWindowHandle> mWindow;
7743 sp<FakeWindowHandle> mSecondWindow;
7744
7745 void SetUp() override {
7746 InputDispatcherTest::SetUp();
7747 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007748 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007749 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007750 mSecondWindow =
7751 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007752 mSecondWindow->setFocusable(true);
7753
7754 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007755 mDispatcher->onWindowInfosChanged(
7756 {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan99987712020-11-10 18:43:05 -08007757
7758 setFocusedWindow(mWindow);
7759 mWindow->consumeFocusEvent(true);
7760 }
7761
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007762 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007763 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007764 }
7765
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007766 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7767 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007768 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007769 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7770 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007771 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007772 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007773 }
7774};
7775
7776TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7777 // Ensure that capture cannot be obtained for unfocused windows.
7778 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7779 mFakePolicy->assertSetPointerCaptureNotCalled();
7780 mSecondWindow->assertNoEvents();
7781
7782 // Ensure that capture can be enabled from the focus window.
7783 requestAndVerifyPointerCapture(mWindow, true);
7784
7785 // Ensure that capture cannot be disabled from a window that does not have capture.
7786 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7787 mFakePolicy->assertSetPointerCaptureNotCalled();
7788
7789 // Ensure that capture can be disabled from the window with capture.
7790 requestAndVerifyPointerCapture(mWindow, false);
7791}
7792
7793TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007794 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007795
7796 setFocusedWindow(mSecondWindow);
7797
7798 // Ensure that the capture disabled event was sent first.
7799 mWindow->consumeCaptureEvent(false);
7800 mWindow->consumeFocusEvent(false);
7801 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007802 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007803
7804 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007805 notifyPointerCaptureChanged({});
7806 notifyPointerCaptureChanged(request);
7807 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007808 mWindow->assertNoEvents();
7809 mSecondWindow->assertNoEvents();
7810 mFakePolicy->assertSetPointerCaptureNotCalled();
7811}
7812
7813TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007814 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007815
7816 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007817 notifyPointerCaptureChanged({});
7818 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007819
7820 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007821 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007822 mWindow->consumeCaptureEvent(false);
7823 mWindow->assertNoEvents();
7824}
7825
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007826TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7827 requestAndVerifyPointerCapture(mWindow, true);
7828
7829 // The first window loses focus.
7830 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007831 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007832 mWindow->consumeCaptureEvent(false);
7833
7834 // Request Pointer Capture from the second window before the notification from InputReader
7835 // arrives.
7836 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007837 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007838
7839 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007840 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007841
7842 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007843 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007844
7845 mSecondWindow->consumeFocusEvent(true);
7846 mSecondWindow->consumeCaptureEvent(true);
7847}
7848
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007849TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7850 // App repeatedly enables and disables capture.
7851 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7852 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7853 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7854 mFakePolicy->assertSetPointerCaptureCalled(false);
7855 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7856 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7857
7858 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7859 // first request is now stale, this should do nothing.
7860 notifyPointerCaptureChanged(firstRequest);
7861 mWindow->assertNoEvents();
7862
7863 // InputReader notifies that the second request was enabled.
7864 notifyPointerCaptureChanged(secondRequest);
7865 mWindow->consumeCaptureEvent(true);
7866}
7867
Prabir Pradhan7092e262022-05-03 16:51:09 +00007868TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7869 requestAndVerifyPointerCapture(mWindow, true);
7870
7871 // App toggles pointer capture off and on.
7872 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7873 mFakePolicy->assertSetPointerCaptureCalled(false);
7874
7875 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7876 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7877
7878 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7879 // preceding "disable" request.
7880 notifyPointerCaptureChanged(enableRequest);
7881
7882 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7883 // any notifications.
7884 mWindow->assertNoEvents();
7885}
7886
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007887class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7888protected:
7889 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007890
7891 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7892 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7893
7894 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7895 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7896
7897 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7898 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7899 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7900 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7901 MAXIMUM_OBSCURING_OPACITY);
7902
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007903 static constexpr gui::Uid TOUCHED_APP_UID{10001};
7904 static constexpr gui::Uid APP_B_UID{10002};
7905 static constexpr gui::Uid APP_C_UID{10003};
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007906
7907 sp<FakeWindowHandle> mTouchWindow;
7908
7909 virtual void SetUp() override {
7910 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007911 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007912 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7913 }
7914
7915 virtual void TearDown() override {
7916 InputDispatcherTest::TearDown();
7917 mTouchWindow.clear();
7918 }
7919
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007920 sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode,
chaviw3277faf2021-05-19 16:45:23 -05007921 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007922 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007923 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007924 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007925 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007926 return window;
7927 }
7928
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007929 sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007930 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7931 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007932 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007933 // Generate an arbitrary PID based on the UID
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00007934 window->setOwnerInfo(gui::Pid{static_cast<pid_t>(1777 + (uid.val() % 10000))}, uid);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007935 return window;
7936 }
7937
7938 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007939 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7940 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7941 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007942 }
7943};
7944
7945TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007946 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007947 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007948 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007949
7950 touch();
7951
7952 mTouchWindow->assertNoEvents();
7953}
7954
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007955TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007956 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7957 const sp<FakeWindowHandle>& w =
7958 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007959 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +00007960
7961 touch();
7962
7963 mTouchWindow->assertNoEvents();
7964}
7965
7966TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007967 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7968 const sp<FakeWindowHandle>& w =
7969 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007970 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007971
7972 touch();
7973
7974 w->assertNoEvents();
7975}
7976
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007977TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007978 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007979 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007980
7981 touch();
7982
7983 mTouchWindow->consumeAnyMotionDown();
7984}
7985
7986TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007987 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007988 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007989 w->setFrame(Rect(0, 0, 50, 50));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007990 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007991
7992 touch({PointF{100, 100}});
7993
7994 mTouchWindow->consumeAnyMotionDown();
7995}
7996
7997TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007998 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007999 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008000 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008001
8002 touch();
8003
8004 mTouchWindow->consumeAnyMotionDown();
8005}
8006
8007TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
8008 const sp<FakeWindowHandle>& w =
8009 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008010 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008011
8012 touch();
8013
8014 mTouchWindow->consumeAnyMotionDown();
8015}
8016
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008017TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
8018 const sp<FakeWindowHandle>& w =
8019 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008020 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008021
8022 touch();
8023
8024 w->assertNoEvents();
8025}
8026
8027/**
8028 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
8029 * inside) while letting them pass-through. Note that even though touch passes through the occluding
8030 * window, the occluding window will still receive ACTION_OUTSIDE event.
8031 */
8032TEST_F(InputDispatcherUntrustedTouchesTest,
8033 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
8034 const sp<FakeWindowHandle>& w =
8035 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008036 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008037 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008038
8039 touch();
8040
8041 w->consumeMotionOutside();
8042}
8043
8044TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
8045 const sp<FakeWindowHandle>& w =
8046 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008047 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008048 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008049
8050 touch();
8051
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008052 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008053}
8054
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008055TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008056 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008057 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8058 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008059 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008060
8061 touch();
8062
8063 mTouchWindow->consumeAnyMotionDown();
8064}
8065
8066TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
8067 const sp<FakeWindowHandle>& w =
8068 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8069 MAXIMUM_OBSCURING_OPACITY);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008070 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008071
8072 touch();
8073
8074 mTouchWindow->consumeAnyMotionDown();
8075}
8076
8077TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008078 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008079 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8080 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008081 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008082
8083 touch();
8084
8085 mTouchWindow->assertNoEvents();
8086}
8087
8088TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
8089 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
8090 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008091 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8092 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008093 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008094 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8095 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008096 mDispatcher->onWindowInfosChanged(
8097 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008098
8099 touch();
8100
8101 mTouchWindow->assertNoEvents();
8102}
8103
8104TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
8105 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
8106 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008107 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8108 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008109 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008110 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8111 OPACITY_FAR_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008112 mDispatcher->onWindowInfosChanged(
8113 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008114
8115 touch();
8116
8117 mTouchWindow->consumeAnyMotionDown();
8118}
8119
8120TEST_F(InputDispatcherUntrustedTouchesTest,
8121 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
8122 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008123 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8124 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008125 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008126 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8127 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008128 mDispatcher->onWindowInfosChanged(
8129 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008130
8131 touch();
8132
8133 mTouchWindow->consumeAnyMotionDown();
8134}
8135
8136TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
8137 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008138 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8139 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008140 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008141 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8142 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008143 mDispatcher->onWindowInfosChanged(
8144 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008145
8146 touch();
8147
8148 mTouchWindow->assertNoEvents();
8149}
8150
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008151TEST_F(InputDispatcherUntrustedTouchesTest,
8152 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
8153 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008154 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8155 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008156 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008157 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8158 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008159 mDispatcher->onWindowInfosChanged(
8160 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008161
8162 touch();
8163
8164 mTouchWindow->assertNoEvents();
8165}
8166
8167TEST_F(InputDispatcherUntrustedTouchesTest,
8168 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
8169 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008170 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8171 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008172 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008173 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8174 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008175 mDispatcher->onWindowInfosChanged(
8176 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008177
8178 touch();
8179
8180 mTouchWindow->consumeAnyMotionDown();
8181}
8182
8183TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
8184 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008185 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8186 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008187 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008188
8189 touch();
8190
8191 mTouchWindow->consumeAnyMotionDown();
8192}
8193
8194TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
8195 const sp<FakeWindowHandle>& w =
8196 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008197 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008198
8199 touch();
8200
8201 mTouchWindow->consumeAnyMotionDown();
8202}
8203
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008204TEST_F(InputDispatcherUntrustedTouchesTest,
8205 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
8206 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8207 const sp<FakeWindowHandle>& w =
8208 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008209 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008210
8211 touch();
8212
8213 mTouchWindow->assertNoEvents();
8214}
8215
8216TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
8217 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8218 const sp<FakeWindowHandle>& w =
8219 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008220 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008221
8222 touch();
8223
8224 mTouchWindow->consumeAnyMotionDown();
8225}
8226
8227TEST_F(InputDispatcherUntrustedTouchesTest,
8228 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
8229 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
8230 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008231 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8232 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008233 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +00008234
8235 touch();
8236
8237 mTouchWindow->consumeAnyMotionDown();
8238}
8239
8240TEST_F(InputDispatcherUntrustedTouchesTest,
8241 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
8242 const sp<FakeWindowHandle>& w1 =
8243 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8244 OPACITY_BELOW_THRESHOLD);
8245 const sp<FakeWindowHandle>& w2 =
8246 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8247 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008248 mDispatcher->onWindowInfosChanged(
8249 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +00008250
8251 touch();
8252
8253 mTouchWindow->assertNoEvents();
8254}
8255
8256/**
8257 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
8258 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
8259 * (which alone would result in allowing touches) does not affect the blocking behavior.
8260 */
8261TEST_F(InputDispatcherUntrustedTouchesTest,
8262 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
8263 const sp<FakeWindowHandle>& wB =
8264 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8265 OPACITY_BELOW_THRESHOLD);
8266 const sp<FakeWindowHandle>& wC =
8267 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8268 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008269 mDispatcher->onWindowInfosChanged(
8270 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +00008271
8272 touch();
8273
8274 mTouchWindow->assertNoEvents();
8275}
8276
8277/**
8278 * This test is testing that a window from a different UID but with same application token doesn't
8279 * block the touch. Apps can share the application token for close UI collaboration for example.
8280 */
8281TEST_F(InputDispatcherUntrustedTouchesTest,
8282 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
8283 const sp<FakeWindowHandle>& w =
8284 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
8285 w->setApplicationToken(mTouchWindow->getApplicationToken());
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008286 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008287
8288 touch();
8289
8290 mTouchWindow->consumeAnyMotionDown();
8291}
8292
arthurhungb89ccb02020-12-30 16:19:01 +08008293class InputDispatcherDragTests : public InputDispatcherTest {
8294protected:
8295 std::shared_ptr<FakeApplicationHandle> mApp;
8296 sp<FakeWindowHandle> mWindow;
8297 sp<FakeWindowHandle> mSecondWindow;
8298 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008299 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008300 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8301 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008302
8303 void SetUp() override {
8304 InputDispatcherTest::SetUp();
8305 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008306 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008307 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008308
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008309 mSecondWindow =
8310 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008311 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008312
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008313 mSpyWindow =
8314 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008315 mSpyWindow->setSpy(true);
8316 mSpyWindow->setTrustedOverlay(true);
8317 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8318
arthurhungb89ccb02020-12-30 16:19:01 +08008319 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008320 mDispatcher->onWindowInfosChanged(
8321 {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()},
8322 {},
8323 0,
8324 0});
arthurhungb89ccb02020-12-30 16:19:01 +08008325 }
8326
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008327 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8328 switch (fromSource) {
8329 case AINPUT_SOURCE_TOUCHSCREEN:
8330 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008331 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008332 ADISPLAY_ID_DEFAULT, {50, 50}))
8333 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8334 break;
8335 case AINPUT_SOURCE_STYLUS:
8336 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008337 injectMotionEvent(*mDispatcher,
8338 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8339 AINPUT_SOURCE_STYLUS)
8340 .buttonState(
8341 AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
8342 .pointer(PointerBuilder(0, ToolType::STYLUS)
8343 .x(50)
8344 .y(50))
8345 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008346 break;
8347 case AINPUT_SOURCE_MOUSE:
8348 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008349 injectMotionEvent(*mDispatcher,
8350 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8351 AINPUT_SOURCE_MOUSE)
8352 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8353 .pointer(PointerBuilder(MOUSE_POINTER_ID,
8354 ToolType::MOUSE)
8355 .x(50)
8356 .y(50))
8357 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008358 break;
8359 default:
8360 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8361 }
arthurhungb89ccb02020-12-30 16:19:01 +08008362
8363 // Window should receive motion event.
8364 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008365 // Spy window should also receive motion event
8366 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008367 }
8368
8369 // Start performing drag, we will create a drag window and transfer touch to it.
8370 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8371 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008372 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008373 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008374 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008375 }
arthurhungb89ccb02020-12-30 16:19:01 +08008376
8377 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008378 mDragWindow =
8379 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008380 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008381 mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(),
8382 *mWindow->getInfo(), *mSecondWindow->getInfo()},
8383 {},
8384 0,
8385 0});
arthurhungb89ccb02020-12-30 16:19:01 +08008386
8387 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008388 bool transferred =
8389 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008390 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008391 if (transferred) {
8392 mWindow->consumeMotionCancel();
8393 mDragWindow->consumeMotionDown();
8394 }
8395 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008396 }
8397};
8398
8399TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008400 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008401
8402 // Move on window.
8403 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008404 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +08008405 ADISPLAY_ID_DEFAULT, {50, 50}))
8406 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8407 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8408 mWindow->consumeDragEvent(false, 50, 50);
8409 mSecondWindow->assertNoEvents();
8410
8411 // Move to another window.
8412 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008413 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +08008414 ADISPLAY_ID_DEFAULT, {150, 50}))
8415 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8416 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8417 mWindow->consumeDragEvent(true, 150, 50);
8418 mSecondWindow->consumeDragEvent(false, 50, 50);
8419
8420 // Move back to original window.
8421 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008422 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +08008423 ADISPLAY_ID_DEFAULT, {50, 50}))
8424 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8425 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8426 mWindow->consumeDragEvent(false, 50, 50);
8427 mSecondWindow->consumeDragEvent(true, -50, 50);
8428
8429 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008430 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8431 {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +08008432 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8433 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8434 mWindow->assertNoEvents();
8435 mSecondWindow->assertNoEvents();
8436}
8437
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008438TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008439 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008440
8441 // No cancel event after drag start
8442 mSpyWindow->assertNoEvents();
8443
8444 const MotionEvent secondFingerDownEvent =
8445 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8446 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008447 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8448 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008449 .build();
8450 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008451 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008452 InputEventInjectionSync::WAIT_FOR_RESULT))
8453 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8454
8455 // Receives cancel for first pointer after next pointer down
8456 mSpyWindow->consumeMotionCancel();
8457 mSpyWindow->consumeMotionDown();
8458
8459 mSpyWindow->assertNoEvents();
8460}
8461
arthurhungf452d0b2021-01-06 00:19:52 +08008462TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008463 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008464
8465 // Move on window.
8466 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008467 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungf452d0b2021-01-06 00:19:52 +08008468 ADISPLAY_ID_DEFAULT, {50, 50}))
8469 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8470 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8471 mWindow->consumeDragEvent(false, 50, 50);
8472 mSecondWindow->assertNoEvents();
8473
8474 // Move to another window.
8475 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008476 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungf452d0b2021-01-06 00:19:52 +08008477 ADISPLAY_ID_DEFAULT, {150, 50}))
8478 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8479 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8480 mWindow->consumeDragEvent(true, 150, 50);
8481 mSecondWindow->consumeDragEvent(false, 50, 50);
8482
8483 // drop to another window.
8484 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008485 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungf452d0b2021-01-06 00:19:52 +08008486 {150, 50}))
8487 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8488 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008489 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhungf452d0b2021-01-06 00:19:52 +08008490 mWindow->assertNoEvents();
8491 mSecondWindow->assertNoEvents();
8492}
8493
arthurhung6d4bed92021-03-17 11:59:33 +08008494TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008495 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008496
8497 // Move on window and keep button pressed.
8498 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008499 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +08008500 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8501 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008502 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008503 .build()))
8504 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8505 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8506 mWindow->consumeDragEvent(false, 50, 50);
8507 mSecondWindow->assertNoEvents();
8508
8509 // Move to another window and release button, expect to drop item.
8510 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008511 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +08008512 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8513 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008514 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008515 .build()))
8516 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8517 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8518 mWindow->assertNoEvents();
8519 mSecondWindow->assertNoEvents();
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008520 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhung6d4bed92021-03-17 11:59:33 +08008521
8522 // nothing to the window.
8523 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008524 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +08008525 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8526 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008527 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008528 .build()))
8529 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8530 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8531 mWindow->assertNoEvents();
8532 mSecondWindow->assertNoEvents();
8533}
8534
Arthur Hung54745652022-04-20 07:17:41 +00008535TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008536 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008537
8538 // Set second window invisible.
8539 mSecondWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008540 mDispatcher->onWindowInfosChanged(
8541 {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Arthur Hung6d0571e2021-04-09 20:18:16 +08008542
8543 // Move on window.
8544 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008545 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung6d0571e2021-04-09 20:18:16 +08008546 ADISPLAY_ID_DEFAULT, {50, 50}))
8547 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8548 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8549 mWindow->consumeDragEvent(false, 50, 50);
8550 mSecondWindow->assertNoEvents();
8551
8552 // Move to another window.
8553 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008554 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung6d0571e2021-04-09 20:18:16 +08008555 ADISPLAY_ID_DEFAULT, {150, 50}))
8556 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8557 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8558 mWindow->consumeDragEvent(true, 150, 50);
8559 mSecondWindow->assertNoEvents();
8560
8561 // drop to another window.
8562 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008563 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung6d0571e2021-04-09 20:18:16 +08008564 {150, 50}))
8565 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8566 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008567 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
Arthur Hung6d0571e2021-04-09 20:18:16 +08008568 mWindow->assertNoEvents();
8569 mSecondWindow->assertNoEvents();
8570}
8571
Arthur Hung54745652022-04-20 07:17:41 +00008572TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008573 // Ensure window could track pointerIds if it didn't support split touch.
8574 mWindow->setPreventSplitting(true);
8575
Arthur Hung54745652022-04-20 07:17:41 +00008576 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008577 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung54745652022-04-20 07:17:41 +00008578 {50, 50}))
8579 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8580 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8581
8582 const MotionEvent secondFingerDownEvent =
8583 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8584 .displayId(ADISPLAY_ID_DEFAULT)
8585 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008586 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8587 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008588 .build();
8589 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008590 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +00008591 InputEventInjectionSync::WAIT_FOR_RESULT))
8592 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008593 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008594
8595 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008596 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008597}
8598
8599TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8600 // First down on second window.
8601 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008602 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung54745652022-04-20 07:17:41 +00008603 {150, 50}))
8604 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8605
8606 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8607
8608 // Second down on first window.
8609 const MotionEvent secondFingerDownEvent =
8610 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8611 .displayId(ADISPLAY_ID_DEFAULT)
8612 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008613 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8614 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008615 .build();
8616 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008617 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +00008618 InputEventInjectionSync::WAIT_FOR_RESULT))
8619 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8620 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8621
8622 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008623 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008624
8625 // Move on window.
8626 const MotionEvent secondFingerMoveEvent =
8627 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8628 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008629 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8630 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008631 .build();
8632 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008633 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +00008634 InputEventInjectionSync::WAIT_FOR_RESULT));
8635 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8636 mWindow->consumeDragEvent(false, 50, 50);
8637 mSecondWindow->consumeMotionMove();
8638
8639 // Release the drag pointer should perform drop.
8640 const MotionEvent secondFingerUpEvent =
8641 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8642 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008643 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8644 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008645 .build();
8646 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008647 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +00008648 InputEventInjectionSync::WAIT_FOR_RESULT));
8649 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008650 mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken());
Arthur Hung54745652022-04-20 07:17:41 +00008651 mWindow->assertNoEvents();
8652 mSecondWindow->consumeMotionMove();
8653}
8654
Arthur Hung3915c1f2022-05-31 07:17:17 +00008655TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008656 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008657
8658 // Update window of second display.
8659 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008660 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008661 mDispatcher->onWindowInfosChanged(
8662 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
8663 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
8664 {},
8665 0,
8666 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +00008667
8668 // Let second display has a touch state.
8669 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008670 injectMotionEvent(*mDispatcher,
Arthur Hung3915c1f2022-05-31 07:17:17 +00008671 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8672 AINPUT_SOURCE_TOUCHSCREEN)
8673 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008674 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008675 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008676 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008677 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008678 // Update window again.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008679 mDispatcher->onWindowInfosChanged(
8680 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
8681 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
8682 {},
8683 0,
8684 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +00008685
8686 // Move on window.
8687 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008688 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung3915c1f2022-05-31 07:17:17 +00008689 ADISPLAY_ID_DEFAULT, {50, 50}))
8690 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8691 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8692 mWindow->consumeDragEvent(false, 50, 50);
8693 mSecondWindow->assertNoEvents();
8694
8695 // Move to another window.
8696 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008697 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung3915c1f2022-05-31 07:17:17 +00008698 ADISPLAY_ID_DEFAULT, {150, 50}))
8699 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8700 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8701 mWindow->consumeDragEvent(true, 150, 50);
8702 mSecondWindow->consumeDragEvent(false, 50, 50);
8703
8704 // drop to another window.
8705 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008706 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung3915c1f2022-05-31 07:17:17 +00008707 {150, 50}))
8708 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8709 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008710 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hung3915c1f2022-05-31 07:17:17 +00008711 mWindow->assertNoEvents();
8712 mSecondWindow->assertNoEvents();
8713}
8714
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008715TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8716 startDrag(true, AINPUT_SOURCE_MOUSE);
8717 // Move on window.
8718 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008719 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008720 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8721 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008722 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008723 .x(50)
8724 .y(50))
8725 .build()))
8726 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8727 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8728 mWindow->consumeDragEvent(false, 50, 50);
8729 mSecondWindow->assertNoEvents();
8730
8731 // Move to another window.
8732 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008733 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008734 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8735 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008736 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008737 .x(150)
8738 .y(50))
8739 .build()))
8740 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8741 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8742 mWindow->consumeDragEvent(true, 150, 50);
8743 mSecondWindow->consumeDragEvent(false, 50, 50);
8744
8745 // drop to another window.
8746 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008747 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008748 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8749 .buttonState(0)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008750 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008751 .x(150)
8752 .y(50))
8753 .build()))
8754 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8755 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07008756 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008757 mWindow->assertNoEvents();
8758 mSecondWindow->assertNoEvents();
8759}
8760
Vishnu Nair062a8672021-09-03 16:07:44 -07008761class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8762
8763TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8764 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008765 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8766 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008767 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008768 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8769 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008770 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -07008771 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008772 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008773
8774 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008775 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008776 window->assertNoEvents();
8777
Prabir Pradhan678438e2023-04-13 19:32:51 +00008778 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8779 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07008780 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
8781 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008782 window->assertNoEvents();
8783
8784 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008785 window->setDropInput(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008786 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -07008787
Prabir Pradhan678438e2023-04-13 19:32:51 +00008788 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008789 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8790
Prabir Pradhan678438e2023-04-13 19:32:51 +00008791 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8792 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008793 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8794 window->assertNoEvents();
8795}
8796
8797TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8798 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8799 std::make_shared<FakeApplicationHandle>();
8800 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008801 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8802 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008803 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00008804 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008805 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008806 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008807 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8808 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008809 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00008810 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -07008811 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8812 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008813 mDispatcher->onWindowInfosChanged(
8814 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -07008815 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008816 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008817
8818 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008819 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008820 window->assertNoEvents();
8821
Prabir Pradhan678438e2023-04-13 19:32:51 +00008822 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8823 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07008824 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
8825 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008826 window->assertNoEvents();
8827
8828 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008829 window->setDropInputIfObscured(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008830 mDispatcher->onWindowInfosChanged(
8831 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -07008832
Prabir Pradhan678438e2023-04-13 19:32:51 +00008833 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008834 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8835
Prabir Pradhan678438e2023-04-13 19:32:51 +00008836 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8837 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008838 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8839 window->assertNoEvents();
8840}
8841
8842TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8843 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8844 std::make_shared<FakeApplicationHandle>();
8845 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008846 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8847 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008848 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00008849 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008850 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008851 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008852 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8853 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008854 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00008855 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -07008856 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8857 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008858 mDispatcher->onWindowInfosChanged(
8859 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -07008860 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008861 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008862
8863 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008864 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008865 window->assertNoEvents();
8866
Prabir Pradhan678438e2023-04-13 19:32:51 +00008867 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8868 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07008869 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
8870 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008871 window->assertNoEvents();
8872
8873 // When the window is no longer obscured because it went on top, it should get input
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008874 mDispatcher->onWindowInfosChanged(
8875 {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -07008876
Prabir Pradhan678438e2023-04-13 19:32:51 +00008877 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008878 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8879
Prabir Pradhan678438e2023-04-13 19:32:51 +00008880 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8881 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008882 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8883 window->assertNoEvents();
8884}
8885
Antonio Kantekf16f2832021-09-28 04:39:20 +00008886class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8887protected:
8888 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008889 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008890 sp<FakeWindowHandle> mWindow;
8891 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008892 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008893
8894 void SetUp() override {
8895 InputDispatcherTest::SetUp();
8896
8897 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008898 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008899 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008900 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008901 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008902 mSecondWindow =
8903 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008904 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008905 mThirdWindow =
8906 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8907 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8908 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008909
8910 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008911 mDispatcher->onWindowInfosChanged(
8912 {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()},
8913 {},
8914 0,
8915 0});
Antonio Kantek15beb512022-06-13 22:35:41 +00008916 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008917 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008918
Antonio Kantek15beb512022-06-13 22:35:41 +00008919 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008920 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008921 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008922 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8923 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008924 mThirdWindow->assertNoEvents();
8925 }
8926
8927 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8928 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008929 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008930 SECOND_DISPLAY_ID)) {
8931 mWindow->assertNoEvents();
8932 mSecondWindow->assertNoEvents();
8933 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008934 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008935 }
8936
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00008937 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Antonio Kantek15beb512022-06-13 22:35:41 +00008938 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008939 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8940 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008941 mWindow->consumeTouchModeEvent(inTouchMode);
8942 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008943 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008944 }
8945};
8946
Antonio Kantek26defcf2022-02-08 01:12:27 +00008947TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008948 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008949 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8950 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008951 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008952}
8953
Antonio Kantek26defcf2022-02-08 01:12:27 +00008954TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8955 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00008956 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008957 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00008958 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008959 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008960 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008961 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008962 mWindow->assertNoEvents();
8963 mSecondWindow->assertNoEvents();
8964}
8965
8966TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8967 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00008968 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008969 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00008970 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek15beb512022-06-13 22:35:41 +00008971 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008972 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008973}
8974
Antonio Kantekf16f2832021-09-28 04:39:20 +00008975TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008976 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008977 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8978 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008979 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008980 mWindow->assertNoEvents();
8981 mSecondWindow->assertNoEvents();
8982}
8983
Antonio Kantek15beb512022-06-13 22:35:41 +00008984TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8985 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8986 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8987 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008988 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008989 mWindow->assertNoEvents();
8990 mSecondWindow->assertNoEvents();
8991 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8992}
8993
Antonio Kantek48710e42022-03-24 14:19:30 -07008994TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8995 // Interact with the window first.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008996 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8997 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT))
Antonio Kantek48710e42022-03-24 14:19:30 -07008998 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8999 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
9000
9001 // Then remove focus.
9002 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009003 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Antonio Kantek48710e42022-03-24 14:19:30 -07009004
9005 // Assert that caller can switch touch mode by owning one of the last interacted window.
9006 const WindowInfo& windowInfo = *mWindow->getInfo();
9007 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
9008 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00009009 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07009010}
9011
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009012class InputDispatcherSpyWindowTest : public InputDispatcherTest {
9013public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009014 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009015 std::shared_ptr<FakeApplicationHandle> application =
9016 std::make_shared<FakeApplicationHandle>();
9017 std::string name = "Fake Spy ";
9018 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009019 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
9020 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009021 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08009022 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009023 return spy;
9024 }
9025
9026 sp<FakeWindowHandle> createForeground() {
9027 std::shared_ptr<FakeApplicationHandle> application =
9028 std::make_shared<FakeApplicationHandle>();
9029 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009030 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
9031 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009032 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009033 return window;
9034 }
9035
9036private:
9037 int mSpyCount{0};
9038};
9039
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009040using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009041/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08009042 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
9043 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009044TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
Siarhei Vishniakou21f77bd2023-07-18 17:51:35 -07009045 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009046 ScopedSilentDeath _silentDeath;
9047
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009048 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08009049 spy->setTrustedOverlay(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009050 ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}),
Prabir Pradhan5c85e052021-12-22 02:27:12 -08009051 ".* not a trusted overlay");
9052}
9053
9054/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009055 * Input injection into a display with a spy window but no foreground windows should succeed.
9056 */
9057TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009058 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009059 mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009060
9061 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009062 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009063 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9064 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9065}
9066
9067/**
9068 * Verify the order in which different input windows receive events. The touched foreground window
9069 * (if there is one) should always receive the event first. When there are multiple spy windows, the
9070 * spy windows will receive the event according to their Z-order, where the top-most spy window will
9071 * receive events before ones belows it.
9072 *
9073 * Here, we set up a scenario with four windows in the following Z order from the top:
9074 * spy1, spy2, window, spy3.
9075 * We then inject an event and verify that the foreground "window" receives it first, followed by
9076 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
9077 * window.
9078 */
9079TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
9080 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009081 auto spy1 = createSpy();
9082 auto spy2 = createSpy();
9083 auto spy3 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009084 mDispatcher->onWindowInfosChanged(
9085 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009086 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
9087 const size_t numChannels = channels.size();
9088
Michael Wright8e9a8562022-02-09 13:44:29 +00009089 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009090 if (!epollFd.ok()) {
9091 FAIL() << "Failed to create epoll fd";
9092 }
9093
9094 for (size_t i = 0; i < numChannels; i++) {
9095 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
9096 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
9097 FAIL() << "Failed to add fd to epoll";
9098 }
9099 }
9100
9101 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009102 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009103 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9104
9105 std::vector<size_t> eventOrder;
9106 std::vector<struct epoll_event> events(numChannels);
9107 for (;;) {
9108 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
9109 (100ms).count());
9110 if (nFds < 0) {
9111 FAIL() << "Failed to call epoll_wait";
9112 }
9113 if (nFds == 0) {
9114 break; // epoll_wait timed out
9115 }
9116 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07009117 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07009118 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009119 channels[i]->consumeMotionDown();
9120 }
9121 }
9122
9123 // Verify the order in which the events were received.
9124 EXPECT_EQ(3u, eventOrder.size());
9125 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
9126 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
9127 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
9128}
9129
9130/**
9131 * A spy window using the NOT_TOUCHABLE flag does not receive events.
9132 */
9133TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
9134 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009135 auto spy = createSpy();
9136 spy->setTouchable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009137 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009138
9139 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009140 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009141 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9142 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9143 spy->assertNoEvents();
9144}
9145
9146/**
9147 * A spy window will only receive gestures that originate within its touchable region. Gestures that
9148 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
9149 * to the window.
9150 */
9151TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
9152 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009153 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009154 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009155 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009156
9157 // Inject an event outside the spy window's touchable region.
9158 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009159 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009160 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9161 window->consumeMotionDown();
9162 spy->assertNoEvents();
9163 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009164 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009165 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9166 window->consumeMotionUp();
9167 spy->assertNoEvents();
9168
9169 // Inject an event inside the spy window's touchable region.
9170 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009171 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009172 {5, 10}))
9173 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9174 window->consumeMotionDown();
9175 spy->consumeMotionDown();
9176}
9177
9178/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009179 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009180 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009181 */
9182TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
9183 auto window = createForeground();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009184 window->setOwnerInfo(gui::Pid{12}, gui::Uid{34});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009185 auto spy = createSpy();
9186 spy->setWatchOutsideTouch(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009187 spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78});
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009188 spy->setFrame(Rect{0, 0, 20, 20});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009189 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009190
9191 // Inject an event outside the spy window's frame and touchable region.
9192 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009193 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009194 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009195 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9196 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009197 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009198}
9199
9200/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009201 * Even when a spy window spans over multiple foreground windows, the spy should receive all
9202 * pointers that are down within its bounds.
9203 */
9204TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
9205 auto windowLeft = createForeground();
9206 windowLeft->setFrame({0, 0, 100, 200});
9207 auto windowRight = createForeground();
9208 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009209 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009210 spy->setFrame({0, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009211 mDispatcher->onWindowInfosChanged(
9212 {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009213
9214 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009215 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009216 {50, 50}))
9217 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9218 windowLeft->consumeMotionDown();
9219 spy->consumeMotionDown();
9220
9221 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009222 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009223 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009224 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9225 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009226 .build();
9227 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009228 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009229 InputEventInjectionSync::WAIT_FOR_RESULT))
9230 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9231 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00009232 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009233}
9234
9235/**
9236 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
9237 * the spy should receive the second pointer with ACTION_DOWN.
9238 */
9239TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
9240 auto window = createForeground();
9241 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009242 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009243 spyRight->setFrame({100, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009244 mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009245
9246 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009247 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009248 {50, 50}))
9249 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9250 window->consumeMotionDown();
9251 spyRight->assertNoEvents();
9252
9253 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009254 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009255 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009256 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9257 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009258 .build();
9259 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009260 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009261 InputEventInjectionSync::WAIT_FOR_RESULT))
9262 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00009263 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009264 spyRight->consumeMotionDown();
9265}
9266
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009267/**
9268 * The spy window should not be able to affect whether or not touches are split. Only the foreground
9269 * windows should be allowed to control split touch.
9270 */
9271TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009272 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009273 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009274 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009275 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009276
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009277 auto window = createForeground();
9278 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009279
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009280 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009281
9282 // First finger down, no window touched.
9283 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009284 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009285 {100, 200}))
9286 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9287 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9288 window->assertNoEvents();
9289
9290 // Second finger down on window, the window should receive touch down.
9291 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009292 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009293 .displayId(ADISPLAY_ID_DEFAULT)
9294 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009295 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9296 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009297 .build();
9298 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009299 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009300 InputEventInjectionSync::WAIT_FOR_RESULT))
9301 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9302
9303 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00009304 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009305}
9306
9307/**
9308 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
9309 * do not receive key events.
9310 */
9311TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009312 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009313 spy->setFocusable(false);
9314
9315 auto window = createForeground();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009316 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009317 setFocusedWindow(window);
9318 window->consumeFocusEvent(true);
9319
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009320 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009321 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9322 window->consumeKeyDown(ADISPLAY_ID_NONE);
9323
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009324 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009325 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9326 window->consumeKeyUp(ADISPLAY_ID_NONE);
9327
9328 spy->assertNoEvents();
9329}
9330
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009331using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9332
9333/**
9334 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9335 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9336 */
9337TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9338 auto window = createForeground();
9339 auto spy1 = createSpy();
9340 auto spy2 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009341 mDispatcher->onWindowInfosChanged(
9342 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009343
9344 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009345 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009346 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9347 window->consumeMotionDown();
9348 spy1->consumeMotionDown();
9349 spy2->consumeMotionDown();
9350
9351 // Pilfer pointers from the second spy window.
9352 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9353 spy2->assertNoEvents();
9354 spy1->consumeMotionCancel();
9355 window->consumeMotionCancel();
9356
9357 // The rest of the gesture should only be sent to the second spy window.
9358 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009359 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009360 ADISPLAY_ID_DEFAULT))
9361 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9362 spy2->consumeMotionMove();
9363 spy1->assertNoEvents();
9364 window->assertNoEvents();
9365}
9366
9367/**
9368 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9369 * in the middle of the gesture.
9370 */
9371TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9372 auto window = createForeground();
9373 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009374 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009375
9376 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009377 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009378 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9379 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9380 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9381
9382 window->releaseChannel();
9383
9384 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9385
9386 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009387 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009388 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9389 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9390}
9391
9392/**
9393 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9394 * the spy, but not to any other windows.
9395 */
9396TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9397 auto spy = createSpy();
9398 auto window = createForeground();
9399
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009400 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009401
9402 // First finger down on the window and the spy.
9403 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009404 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009405 {100, 200}))
9406 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9407 spy->consumeMotionDown();
9408 window->consumeMotionDown();
9409
9410 // Spy window pilfers the pointers.
9411 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9412 window->consumeMotionCancel();
9413
9414 // Second finger down on the window and spy, but the window should not receive the pointer down.
9415 const MotionEvent secondFingerDownEvent =
9416 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9417 .displayId(ADISPLAY_ID_DEFAULT)
9418 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009419 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9420 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009421 .build();
9422 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009423 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009424 InputEventInjectionSync::WAIT_FOR_RESULT))
9425 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9426
Harry Cutts33476232023-01-30 19:57:29 +00009427 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009428
9429 // Third finger goes down outside all windows, so injection should fail.
9430 const MotionEvent thirdFingerDownEvent =
9431 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9432 .displayId(ADISPLAY_ID_DEFAULT)
9433 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009434 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9435 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9436 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009437 .build();
9438 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009439 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009440 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009441 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009442
9443 spy->assertNoEvents();
9444 window->assertNoEvents();
9445}
9446
9447/**
9448 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9449 */
9450TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9451 auto spy = createSpy();
9452 spy->setFrame(Rect(0, 0, 100, 100));
9453 auto window = createForeground();
9454 window->setFrame(Rect(0, 0, 200, 200));
9455
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009456 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009457
9458 // First finger down on the window only
9459 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009460 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009461 {150, 150}))
9462 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9463 window->consumeMotionDown();
9464
9465 // Second finger down on the spy and window
9466 const MotionEvent secondFingerDownEvent =
9467 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9468 .displayId(ADISPLAY_ID_DEFAULT)
9469 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009470 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9471 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009472 .build();
9473 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009474 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009475 InputEventInjectionSync::WAIT_FOR_RESULT))
9476 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9477 spy->consumeMotionDown();
9478 window->consumeMotionPointerDown(1);
9479
9480 // Third finger down on the spy and window
9481 const MotionEvent thirdFingerDownEvent =
9482 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9483 .displayId(ADISPLAY_ID_DEFAULT)
9484 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009485 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9486 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9487 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009488 .build();
9489 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009490 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009491 InputEventInjectionSync::WAIT_FOR_RESULT))
9492 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9493 spy->consumeMotionPointerDown(1);
9494 window->consumeMotionPointerDown(2);
9495
9496 // Spy window pilfers the pointers.
9497 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Harry Cutts101ee9b2023-07-06 18:04:14 +00009498 window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9499 window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009500
9501 spy->assertNoEvents();
9502 window->assertNoEvents();
9503}
9504
9505/**
9506 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9507 * other windows should be canceled. If this results in the cancellation of all pointers for some
9508 * window, then that window should receive ACTION_CANCEL.
9509 */
9510TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9511 auto spy = createSpy();
9512 spy->setFrame(Rect(0, 0, 100, 100));
9513 auto window = createForeground();
9514 window->setFrame(Rect(0, 0, 200, 200));
9515
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009516 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009517
9518 // First finger down on both spy and window
9519 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009520 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009521 {10, 10}))
9522 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9523 window->consumeMotionDown();
9524 spy->consumeMotionDown();
9525
9526 // Second finger down on the spy and window
9527 const MotionEvent secondFingerDownEvent =
9528 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9529 .displayId(ADISPLAY_ID_DEFAULT)
9530 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009531 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9532 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009533 .build();
9534 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009535 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009536 InputEventInjectionSync::WAIT_FOR_RESULT))
9537 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9538 spy->consumeMotionPointerDown(1);
9539 window->consumeMotionPointerDown(1);
9540
9541 // Spy window pilfers the pointers.
9542 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9543 window->consumeMotionCancel();
9544
9545 spy->assertNoEvents();
9546 window->assertNoEvents();
9547}
9548
9549/**
9550 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9551 * be sent to other windows
9552 */
9553TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9554 auto spy = createSpy();
9555 spy->setFrame(Rect(0, 0, 100, 100));
9556 auto window = createForeground();
9557 window->setFrame(Rect(0, 0, 200, 200));
9558
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009559 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009560
9561 // First finger down on both window and spy
9562 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009563 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009564 {10, 10}))
9565 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9566 window->consumeMotionDown();
9567 spy->consumeMotionDown();
9568
9569 // Spy window pilfers the pointers.
9570 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9571 window->consumeMotionCancel();
9572
9573 // Second finger down on the window only
9574 const MotionEvent secondFingerDownEvent =
9575 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9576 .displayId(ADISPLAY_ID_DEFAULT)
9577 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009578 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9579 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009580 .build();
9581 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009582 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009583 InputEventInjectionSync::WAIT_FOR_RESULT))
9584 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9585 window->consumeMotionDown();
9586 window->assertNoEvents();
9587
9588 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9589 spy->consumeMotionMove();
9590 spy->assertNoEvents();
9591}
9592
Prabir Pradhand65552b2021-10-07 11:23:50 -07009593class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9594public:
9595 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9596 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9597 std::make_shared<FakeApplicationHandle>();
9598 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009599 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9600 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009601 overlay->setFocusable(false);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009602 overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009603 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009604 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009605 overlay->setTrustedOverlay(true);
9606
9607 std::shared_ptr<FakeApplicationHandle> application =
9608 std::make_shared<FakeApplicationHandle>();
9609 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009610 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9611 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009612 window->setFocusable(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009613 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Prabir Pradhand65552b2021-10-07 11:23:50 -07009614
9615 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009616 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -07009617 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009618 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009619 return {std::move(overlay), std::move(window)};
9620 }
9621
9622 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009623 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009624 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009625 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009626 }
9627
9628 void sendStylusEvent(int32_t action) {
9629 NotifyMotionArgs motionArgs =
9630 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9631 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009632 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009633 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009634 }
9635};
9636
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009637using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9638
9639TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
Siarhei Vishniakouad3b6822023-06-22 14:17:35 -07009640 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009641 ScopedSilentDeath _silentDeath;
9642
Prabir Pradhand65552b2021-10-07 11:23:50 -07009643 auto [overlay, window] = setupStylusOverlayScenario();
9644 overlay->setTrustedOverlay(false);
9645 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009646 ASSERT_DEATH(mDispatcher->onWindowInfosChanged(
9647 {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}),
Prabir Pradhand65552b2021-10-07 11:23:50 -07009648 ".* not a trusted overlay");
9649}
9650
9651TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9652 auto [overlay, window] = setupStylusOverlayScenario();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009653 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -07009654
9655 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9656 overlay->consumeMotionDown();
9657 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9658 overlay->consumeMotionUp();
9659
9660 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9661 window->consumeMotionDown();
9662 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9663 window->consumeMotionUp();
9664
9665 overlay->assertNoEvents();
9666 window->assertNoEvents();
9667}
9668
9669TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9670 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009671 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009672 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -07009673
9674 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9675 overlay->consumeMotionDown();
9676 window->consumeMotionDown();
9677 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9678 overlay->consumeMotionUp();
9679 window->consumeMotionUp();
9680
9681 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9682 window->consumeMotionDown();
9683 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9684 window->consumeMotionUp();
9685
9686 overlay->assertNoEvents();
9687 window->assertNoEvents();
9688}
9689
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009690/**
9691 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9692 * The scenario is as follows:
9693 * - The stylus interceptor overlay is configured as a spy window.
9694 * - The stylus interceptor spy receives the start of a new stylus gesture.
9695 * - It pilfers pointers and then configures itself to no longer be a spy.
9696 * - The stylus interceptor continues to receive the rest of the gesture.
9697 */
9698TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9699 auto [overlay, window] = setupStylusOverlayScenario();
9700 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009701 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009702
9703 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9704 overlay->consumeMotionDown();
9705 window->consumeMotionDown();
9706
9707 // The interceptor pilfers the pointers.
9708 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9709 window->consumeMotionCancel();
9710
9711 // The interceptor configures itself so that it is no longer a spy.
9712 overlay->setSpy(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009713 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009714
9715 // It continues to receive the rest of the stylus gesture.
9716 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9717 overlay->consumeMotionMove();
9718 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9719 overlay->consumeMotionUp();
9720
9721 window->assertNoEvents();
9722}
9723
Prabir Pradhan5735a322022-04-11 17:23:34 +00009724struct User {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009725 gui::Pid mPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009726 gui::Uid mUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +00009727 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9728 std::unique_ptr<InputDispatcher>& mDispatcher;
9729
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009730 User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid)
Prabir Pradhan5735a322022-04-11 17:23:34 +00009731 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9732
9733 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009734 return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009735 ADISPLAY_ID_DEFAULT, {100, 200},
9736 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9737 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9738 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9739 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9740 }
9741
9742 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009743 return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009744 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009745 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009746 mPolicyFlags);
9747 }
9748
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -07009749 sp<FakeWindowHandle> createWindow(const char* name) const {
Prabir Pradhan5735a322022-04-11 17:23:34 +00009750 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9751 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -07009752 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9753 name, ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009754 window->setOwnerInfo(mPid, mUid);
9755 return window;
9756 }
9757};
9758
9759using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9760
9761TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009762 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -07009763 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009764 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009765
9766 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9767 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9768 window->consumeMotionDown();
9769
9770 setFocusedWindow(window);
9771 window->consumeFocusEvent(true);
9772
9773 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9774 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9775 window->consumeKeyDown(ADISPLAY_ID_NONE);
9776}
9777
9778TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009779 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -07009780 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009781 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009782
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009783 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009784 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9785 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9786
9787 setFocusedWindow(window);
9788 window->consumeFocusEvent(true);
9789
9790 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9791 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9792 window->assertNoEvents();
9793}
9794
9795TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009796 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -07009797 auto window = owner.createWindow("Owned window");
9798 auto spy = owner.createWindow("Owned spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +00009799 spy->setSpy(true);
9800 spy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009801 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009802
9803 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9804 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9805 spy->consumeMotionDown();
9806 window->consumeMotionDown();
9807}
9808
9809TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009810 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -07009811 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +00009812
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009813 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -07009814 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +00009815 randosSpy->setSpy(true);
9816 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009817 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009818
9819 // The event is targeted at owner's window, so injection should succeed, but the spy should
9820 // not receive the event.
9821 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9822 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9823 randosSpy->assertNoEvents();
9824 window->consumeMotionDown();
9825}
9826
9827TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009828 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -07009829 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +00009830
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009831 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -07009832 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +00009833 randosSpy->setSpy(true);
9834 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009835 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009836
9837 // A user that has injection permission can inject into any window.
9838 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009839 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009840 ADISPLAY_ID_DEFAULT));
9841 randosSpy->consumeMotionDown();
9842 window->consumeMotionDown();
9843
9844 setFocusedWindow(randosSpy);
9845 randosSpy->consumeFocusEvent(true);
9846
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009847 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher));
Prabir Pradhan5735a322022-04-11 17:23:34 +00009848 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9849 window->assertNoEvents();
9850}
9851
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009852TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009853 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -07009854 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +00009855
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009856 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -07009857 auto randosWindow = rando.createWindow("Rando's window");
Prabir Pradhan5735a322022-04-11 17:23:34 +00009858 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9859 randosWindow->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009860 mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +00009861
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009862 // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids.
Prabir Pradhan5735a322022-04-11 17:23:34 +00009863 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9864 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9865 window->consumeMotionDown();
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07009866 randosWindow->assertNoEvents();
Prabir Pradhan5735a322022-04-11 17:23:34 +00009867}
9868
Garfield Tane84e6f92019-08-29 17:28:41 -07009869} // namespace android::inputdispatcher