blob: 04ed67049d74c77f6804822a4f614e02368396aa [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"
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070018#include "FakeApplicationHandle.h"
Prabir Pradhan81e89fe2024-03-20 21:17:09 +000019#include "FakeInputDispatcherPolicy.h"
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000020#include "FakeInputTracingBackend.h"
Prabir Pradhanc5340732024-03-20 22:53:52 +000021#include "FakeWindows.h"
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000022#include "TestEventMatchers.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080023
Cody Heiner166a5af2023-07-07 12:25:00 -070024#include <NotifyArgsBuilders.h>
Prabir Pradhan5893d362023-11-17 04:30:40 +000025#include <android-base/logging.h>
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070026#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080027#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080028#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070029#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070030#include <binder/Binder.h>
Ameer Armalycff4fa52023-10-04 23:45:11 +000031#include <com_android_input_flags.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000032#include <fcntl.h>
Ameer Armalycff4fa52023-10-04 23:45:11 +000033#include <flag_macros.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080034#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <gtest/gtest.h>
Siarhei Vishniakou3782af62024-03-07 21:56:39 -080036#include <input/BlockingQueue.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100037#include <input/Input.h>
Siarhei Vishniakou0438ca82024-03-12 14:27:25 -070038#include <input/InputConsumer.h>
Siarhei Vishniakouf4043212023-09-18 19:33:03 -070039#include <input/PrintTools.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080041#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100042
Garfield Tan1c7bc862020-01-28 13:24:04 -080043#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080044#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070045#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080046#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080047#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080048
Garfield Tan1c7bc862020-01-28 13:24:04 -080049using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050050using android::gui::FocusRequest;
51using android::gui::TouchOcclusionMode;
52using android::gui::WindowInfo;
53using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080054using android::os::InputEventInjectionResult;
55using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080056
Garfield Tane84e6f92019-08-29 17:28:41 -070057namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080058
Dominik Laskowski2f01d772022-03-23 16:01:29 -070059using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080060using testing::AllOf;
Prabir Pradhan5893d362023-11-17 04:30:40 +000061using testing::Not;
Siarhei Vishniakou85eb5802024-04-01 11:40:59 -070062using testing::Pointee;
63using testing::UnorderedElementsAre;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070064
Siarhei Vishniakouf4043212023-09-18 19:33:03 -070065namespace {
66
Michael Wrightd02c5b62014-02-10 15:10:22 -080067// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000068static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080069
70// An arbitrary device id.
Prabir Pradhan9205e422023-05-16 20:06:13 +000071static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080072static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
Jeff Brownf086ddb2014-02-11 14:28:48 -080074// An arbitrary display id.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070075constexpr ui::LogicalDisplayId DISPLAY_ID = ui::LogicalDisplayId::DEFAULT;
Linnan Li13bf76a2024-05-05 19:18:02 +080076constexpr ui::LogicalDisplayId SECOND_DISPLAY_ID = ui::LogicalDisplayId{1};
Jeff Brownf086ddb2014-02-11 14:28:48 -080077
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000078// Ensure common actions are interchangeable between keys and motions for convenience.
79static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN);
80static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080081static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
82static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
83static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
84static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -070085static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080086static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070087static constexpr int32_t ACTION_SCROLL = AMOTION_EVENT_ACTION_SCROLL;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080088static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080089static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080090/**
91 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
92 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
93 * index 0) is the new pointer going down. The same pointer could have been placed at a different
94 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
95 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
96 * pointer id=0 leaves but the pointer id=1 remains.
97 */
98static constexpr int32_t POINTER_0_DOWN =
99 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800100static constexpr int32_t POINTER_1_DOWN =
101 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +0000102static constexpr int32_t POINTER_2_DOWN =
103 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +0000104static constexpr int32_t POINTER_3_DOWN =
105 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +0000106static constexpr int32_t POINTER_0_UP =
107 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800108static constexpr int32_t POINTER_1_UP =
109 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Harry Cuttsb166c002023-05-09 13:06:05 +0000110static constexpr int32_t POINTER_2_UP =
111 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800112
Antonio Kantek15beb512022-06-13 22:35:41 +0000113// The default pid and uid for the windows created on the secondary display by the test.
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000114static constexpr gui::Pid SECONDARY_WINDOW_PID{1010};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000115static constexpr gui::Uid SECONDARY_WINDOW_UID{1012};
Antonio Kantek15beb512022-06-13 22:35:41 +0000116
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000117// An arbitrary pid of the gesture monitor window
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000118static constexpr gui::Pid MONITOR_PID{2001};
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000119
Linnan Li72352222024-04-12 18:55:57 +0800120static constexpr int EXPECTED_WALLPAPER_FLAGS =
Arthur Hungc539dbb2022-12-08 07:45:36 +0000121 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
122
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800123using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
124
Gang Wang342c9272020-01-13 13:15:04 -0500125/**
126 * Return a DOWN key event with KEYCODE_A.
127 */
128static KeyEvent getTestKeyEvent() {
129 KeyEvent event;
130
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700131 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
132 ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0,
133 AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500134 return event;
135}
136
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -0700137/**
138 * Provide a local override for a flag value. The value is restored when the object of this class
139 * goes out of scope.
140 * This class is not intended to be used directly, because its usage is cumbersome.
141 * Instead, a wrapper macro SCOPED_FLAG_OVERRIDE is provided.
142 */
143class ScopedFlagOverride {
144public:
145 ScopedFlagOverride(std::function<bool()> read, std::function<void(bool)> write, bool value)
146 : mInitialValue(read()), mWriteValue(write) {
147 mWriteValue(value);
148 }
149 ~ScopedFlagOverride() { mWriteValue(mInitialValue); }
150
151private:
152 const bool mInitialValue;
153 std::function<void(bool)> mWriteValue;
154};
155
156typedef bool (*readFlagValueFunction)();
157typedef void (*writeFlagValueFunction)(bool);
158
159/**
160 * Use this macro to locally override a flag value.
161 * Example usage:
162 * SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
163 * Note: this works by creating a local variable in your current scope. Don't call this twice for
164 * the same flag, because the variable names will clash!
165 */
166#define SCOPED_FLAG_OVERRIDE(NAME, VALUE) \
167 readFlagValueFunction read##NAME = com::android::input::flags::NAME; \
168 writeFlagValueFunction write##NAME = com::android::input::flags::NAME; \
169 ScopedFlagOverride override##NAME(read##NAME, write##NAME, (VALUE))
170
Siarhei Vishniakouf4043212023-09-18 19:33:03 -0700171} // namespace
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173// --- InputDispatcherTest ---
174
175class InputDispatcherTest : public testing::Test {
176protected:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000177 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700178 std::unique_ptr<InputDispatcher> mDispatcher;
Prabir Pradhanc5340732024-03-20 22:53:52 +0000179 std::shared_ptr<VerifyingTrace> mVerifyingTrace;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000181 void SetUp() override {
Prabir Pradhanc5340732024-03-20 22:53:52 +0000182 mVerifyingTrace = std::make_shared<VerifyingTrace>();
183 FakeWindowHandle::sOnEventReceivedCallback = [this](const auto& _1, const auto& _2) {
184 handleEventReceivedByWindow(_1, _2);
185 };
186
Prabir Pradhana41d2442023-04-20 21:30:40 +0000187 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000188 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy,
189 std::make_unique<FakeInputTracingBackend>(
Prabir Pradhanc5340732024-03-20 22:53:52 +0000190 mVerifyingTrace));
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700191
Harry Cutts101ee9b2023-07-06 18:04:14 +0000192 mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000193 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700194 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 }
196
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000197 void TearDown() override {
Prabir Pradhanc5340732024-03-20 22:53:52 +0000198 ASSERT_NO_FATAL_FAILURE(mVerifyingTrace->verifyExpectedEventsTraced());
199 FakeWindowHandle::sOnEventReceivedCallback = nullptr;
200
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700201 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000202 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700203 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700205
Prabir Pradhanc5340732024-03-20 22:53:52 +0000206 void handleEventReceivedByWindow(const std::unique_ptr<InputEvent>& event,
207 const gui::WindowInfo& info) {
208 if (!event) {
209 return;
210 }
211
212 switch (event->getType()) {
213 case InputEventType::KEY: {
214 mVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event), info.id);
215 break;
216 }
217 case InputEventType::MOTION: {
218 mVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event),
219 info.id);
220 break;
221 }
222 default:
223 break;
224 }
225 }
226
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700227 /**
228 * Used for debugging when writing the test
229 */
230 void dumpDispatcherState() {
231 std::string dump;
232 mDispatcher->dump(dump);
233 std::stringstream ss(dump);
234 std::string to;
235
236 while (std::getline(ss, to, '\n')) {
237 ALOGE("%s", to.c_str());
238 }
239 }
Vishnu Nair958da932020-08-21 17:12:37 -0700240
Chavi Weingarten847e8512023-03-29 00:26:09 +0000241 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700242 FocusRequest request;
243 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000244 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700245 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Linnan Li13bf76a2024-05-05 19:18:02 +0800246 request.displayId = window->getInfo()->displayId.val();
Vishnu Nair958da932020-08-21 17:12:37 -0700247 mDispatcher->setFocusedWindow(request);
248 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249};
250
Michael Wrightd02c5b62014-02-10 15:10:22 -0800251TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
252 KeyEvent event;
253
254 // Rejects undefined key actions.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700255 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
256 ui::LogicalDisplayId::INVALID, INVALID_HMAC,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000257 /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600258 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800259 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000260 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000261 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262 << "Should reject key events with undefined action.";
263
264 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700265 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
266 ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0,
267 AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800268 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000269 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000270 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271 << "Should reject key events with ACTION_MULTIPLE.";
272}
273
274TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
275 MotionEvent event;
276 PointerProperties pointerProperties[MAX_POINTERS + 1];
277 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800278 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279 pointerProperties[i].clear();
280 pointerProperties[i].id = i;
281 pointerCoords[i].clear();
282 }
283
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800284 // Some constants commonly used below
285 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
286 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
287 constexpr int32_t metaState = AMETA_NONE;
288 constexpr MotionClassification classification = MotionClassification::NONE;
289
chaviw9eaa22c2020-07-01 16:21:27 -0700290 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800292 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000293 /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700294 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700295 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
296 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000297 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800298 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000299 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000300 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800301 << "Should reject motion events with undefined action.";
302
303 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800304 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800305 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
306 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
307 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
308 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000309 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800310 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000311 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000312 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 << "Should reject motion events with pointer down index too large.";
314
Garfield Tanfbe732e2020-01-24 11:26:14 -0800315 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700316 AMOTION_EVENT_ACTION_POINTER_DOWN |
317 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700318 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
319 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700320 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000321 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800322 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000323 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000324 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 << "Should reject motion events with pointer down index too small.";
326
327 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800328 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800329 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
330 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
331 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
332 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000333 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800334 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000335 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000336 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337 << "Should reject motion events with pointer up index too large.";
338
Garfield Tanfbe732e2020-01-24 11:26:14 -0800339 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700340 AMOTION_EVENT_ACTION_POINTER_UP |
341 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700342 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
343 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700344 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000345 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800346 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000347 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000348 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349 << "Should reject motion events with pointer up index too small.";
350
351 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800352 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
353 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700354 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700355 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
356 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000357 /*pointerCount=*/0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800358 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000359 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000360 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361 << "Should reject motion events with 0 pointers.";
362
Garfield Tanfbe732e2020-01-24 11:26:14 -0800363 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
364 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700365 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700366 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
367 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000368 /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800369 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000370 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000371 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372 << "Should reject motion events with more than MAX_POINTERS pointers.";
373
374 // Rejects motion events with invalid pointer ids.
375 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800376 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
377 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700378 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700379 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
380 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000381 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800382 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000383 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000384 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385 << "Should reject motion events with pointer ids less than 0.";
386
387 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800388 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
389 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700390 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700391 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
392 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000393 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800394 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000395 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000396 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
398
399 // Rejects motion events with duplicate pointer ids.
400 pointerProperties[0].id = 1;
401 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800402 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
403 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700404 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700405 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
406 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000407 /*pointerCount=*/2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800408 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000409 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000410 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411 << "Should reject motion events with duplicate pointer ids.";
412}
413
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800414/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
415
416TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
417 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000418 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800419 ASSERT_TRUE(mDispatcher->waitForIdle());
420
421 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
422}
423
424TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Prabir Pradhana8fe7c52023-12-14 22:07:23 +0000425 NotifySwitchArgs args(InputEvent::nextId(), /*eventTime=*/20, /*policyFlags=*/0,
426 /*switchValues=*/1,
Harry Cutts33476232023-01-30 19:57:29 +0000427 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000428 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800429
430 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
431 args.policyFlags |= POLICY_FLAG_TRUSTED;
432 mFakePolicy->assertNotifySwitchWasCalled(args);
433}
434
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -0700435namespace {
436
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700437static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -0700438
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000439class FakeMonitorReceiver {
440public:
Linnan Li13bf76a2024-05-05 19:18:02 +0800441 FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name,
442 ui::LogicalDisplayId displayId)
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700443 : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {}
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000444
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700445 sp<IBinder> getToken() { return mInputReceiver.getToken(); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000446
Linnan Li13bf76a2024-05-05 19:18:02 +0800447 void consumeKeyDown(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700448 mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
449 expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000450 }
451
452 std::optional<int32_t> receiveEvent() {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800453 const auto [sequenceNum, _] = mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED);
454 return sequenceNum;
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000455 }
456
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700457 void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000458
Linnan Li13bf76a2024-05-05 19:18:02 +0800459 void consumeMotionDown(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700460 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
461 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000462 }
463
Linnan Li13bf76a2024-05-05 19:18:02 +0800464 void consumeMotionMove(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700465 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
466 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000467 }
468
Linnan Li13bf76a2024-05-05 19:18:02 +0800469 void consumeMotionUp(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700470 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
471 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000472 }
473
Linnan Li13bf76a2024-05-05 19:18:02 +0800474 void consumeMotionCancel(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700475 mInputReceiver.consumeMotionEvent(
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000476 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
477 WithDisplayId(expectedDisplayId),
478 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
479 }
480
481 void consumeMotionPointerDown(int32_t pointerIdx) {
482 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
483 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700484 mInputReceiver.consumeEvent(InputEventType::MOTION, action, ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700485 /*expectedFlags=*/0);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000486 }
487
488 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700489 mInputReceiver.consumeMotionEvent(matcher);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000490 }
491
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800492 std::unique_ptr<MotionEvent> consumeMotion() { return mInputReceiver.consumeMotion(); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000493
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -0800494 void assertNoEvents() { mInputReceiver.assertNoEvents(CONSUME_TIMEOUT_NO_EVENT_EXPECTED); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000495
496private:
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700497 FakeInputReceiver mInputReceiver;
Prabir Pradhan112b1ad2023-09-21 09:53:53 +0000498};
499
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800500static InputEventInjectionResult injectKey(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700501 InputDispatcher& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700502 ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800503 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -0800504 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000505 bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {},
Prabir Pradhan5735a322022-04-11 17:23:34 +0000506 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800507 KeyEvent event;
508 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
509
510 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800511 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +0000512 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
513 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +0800514
Prabir Pradhan93f342c2021-03-11 15:05:30 -0800515 if (!allowKeyRepeat) {
516 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
517 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800518 // Inject event until dispatch out.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700519 return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +0800520}
521
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700522static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) {
523 InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700524 injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
525 ui::LogicalDisplayId::INVALID, InputEventInjectionSync::WAIT_FOR_RESULT,
526 CONSUME_TIMEOUT_NO_EVENT_EXPECTED);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700527 if (result != InputEventInjectionResult::TIMED_OUT) {
528 FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result);
529 }
530}
531
Linnan Li13bf76a2024-05-05 19:18:02 +0800532static InputEventInjectionResult injectKeyDown(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700533 InputDispatcher& dispatcher,
534 ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) {
Harry Cutts33476232023-01-30 19:57:29 +0000535 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700536}
537
Prabir Pradhan93f342c2021-03-11 15:05:30 -0800538// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
539// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
540// has to be woken up to process the repeating key.
Linnan Li13bf76a2024-05-05 19:18:02 +0800541static InputEventInjectionResult injectKeyDownNoRepeat(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700542 InputDispatcher& dispatcher,
543 ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) {
Harry Cutts33476232023-01-30 19:57:29 +0000544 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -0800545 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +0000546 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -0800547}
548
Linnan Li13bf76a2024-05-05 19:18:02 +0800549static InputEventInjectionResult injectKeyUp(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700550 InputDispatcher& dispatcher,
551 ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) {
Harry Cutts33476232023-01-30 19:57:29 +0000552 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700553}
554
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800555static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700556 InputDispatcher& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -0700557 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000558 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000559 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700560 return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
561 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -0700562}
563
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800564static InputEventInjectionResult injectMotionEvent(
Linnan Li13bf76a2024-05-05 19:18:02 +0800565 InputDispatcher& dispatcher, int32_t action, int32_t source, ui::LogicalDisplayId displayId,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700566 const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700567 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700568 AMOTION_EVENT_INVALID_CURSOR_POSITION},
569 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800570 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000571 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000572 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -0700573 MotionEventBuilder motionBuilder =
574 MotionEventBuilder(action, source)
575 .displayId(displayId)
576 .eventTime(eventTime)
577 .rawXCursorPosition(cursorPosition.x)
578 .rawYCursorPosition(cursorPosition.y)
579 .pointer(
580 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
581 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
582 motionBuilder.downTime(eventTime);
583 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800584
585 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -0700586 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
587 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +0800588}
589
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700590static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source,
Linnan Li13bf76a2024-05-05 19:18:02 +0800591 ui::LogicalDisplayId displayId,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700592 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700593 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -0700594}
595
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700596static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source,
Linnan Li13bf76a2024-05-05 19:18:02 +0800597 ui::LogicalDisplayId displayId,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800598 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700599 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +0000600}
601
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700602static NotifyKeyArgs generateKeyArgs(
603 int32_t action, ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) {
Jackal Guof9696682018-10-05 12:23:23 +0800604 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
605 // Define a valid key event.
Prabir Pradhana8fe7c52023-12-14 22:07:23 +0000606 NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID,
607 AINPUT_SOURCE_KEYBOARD, displayId, POLICY_FLAG_PASS_TO_USER, action,
608 /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +0800609
610 return args;
611}
612
Prabir Pradhan678438e2023-04-13 19:32:51 +0000613[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
Linnan Li13bf76a2024-05-05 19:18:02 +0800614 ui::LogicalDisplayId displayId,
Prabir Pradhan678438e2023-04-13 19:32:51 +0000615 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -0800616 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -0700617 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
618 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
619 }
620
chaviwd1c23182019-12-20 18:44:56 -0800621 PointerProperties pointerProperties[pointerCount];
622 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +0800623
chaviwd1c23182019-12-20 18:44:56 -0800624 for (size_t i = 0; i < pointerCount; i++) {
625 pointerProperties[i].clear();
626 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700627 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +0800628
chaviwd1c23182019-12-20 18:44:56 -0800629 pointerCoords[i].clear();
630 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
631 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
632 }
Jackal Guof9696682018-10-05 12:23:23 +0800633
634 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
635 // Define a valid motion event.
Prabir Pradhana8fe7c52023-12-14 22:07:23 +0000636 NotifyMotionArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, source,
637 displayId, POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0,
638 /*flags=*/0, AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -0800639 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000640 pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0,
Garfield Tan00f511d2019-06-12 16:55:40 -0700641 AMOTION_EVENT_INVALID_CURSOR_POSITION,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000642 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{});
Jackal Guof9696682018-10-05 12:23:23 +0800643
644 return args;
645}
646
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800647static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
648 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
649}
650
Linnan Li13bf76a2024-05-05 19:18:02 +0800651static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
652 ui::LogicalDisplayId displayId) {
chaviwd1c23182019-12-20 18:44:56 -0800653 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
654}
655
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000656static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
657 const PointerCaptureRequest& request) {
Prabir Pradhana8fe7c52023-12-14 22:07:23 +0000658 return NotifyPointerCaptureChangedArgs(InputEvent::nextId(), systemTime(SYSTEM_TIME_MONOTONIC),
659 request);
Prabir Pradhan99987712020-11-10 18:43:05 -0800660}
661
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -0700662} // namespace
663
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800664/**
665 * When a window unexpectedly disposes of its input channel, policy should be notified about the
666 * broken channel.
667 */
668TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
669 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700670 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
671 "Window that breaks its input channel",
672 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800673
Siarhei Vishniakouc41de372023-07-20 13:14:26 -0700674 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800675
676 // Window closes its channel, but the window remains.
677 window->destroyReceiver();
678 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
679}
680
Arthur Hungb92218b2018-08-14 12:00:21 +0800681TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -0700682 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700683 sp<FakeWindowHandle> window =
684 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
685 ui::LogicalDisplayId::DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800686
Siarhei Vishniakouc41de372023-07-20 13:14:26 -0700687 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700689 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
690 ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800691 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +0800692
693 // Window should receive motion event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700694 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800695}
696
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -0800697using InputDispatcherDeathTest = InputDispatcherTest;
698
699/**
700 * When 'onWindowInfosChanged' arguments contain a duplicate entry for the same window, dispatcher
701 * should crash.
702 */
703TEST_F(InputDispatcherDeathTest, DuplicateWindowInfosAbortDispatcher) {
704 testing::GTEST_FLAG(death_test_style) = "threadsafe";
705 ScopedSilentDeath _silentDeath;
706
707 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700708 sp<FakeWindowHandle> window =
709 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
710 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -0800711 ASSERT_DEATH(mDispatcher->onWindowInfosChanged(
712 {{*window->getInfo(), *window->getInfo()}, {}, 0, 0}),
713 "Incorrect WindowInfosUpdate provided");
714}
715
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -0700716TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
717 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700718 sp<FakeWindowHandle> window =
719 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
720 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -0700721
Siarhei Vishniakouc41de372023-07-20 13:14:26 -0700722 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -0700723 // Inject a MotionEvent to an unknown display.
724 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700725 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
726 ui::LogicalDisplayId::INVALID))
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -0700727 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
728
729 // Window should receive motion event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700730 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -0700731}
732
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -0700733/**
Siarhei Vishniakouc41de372023-07-20 13:14:26 -0700734 * Calling onWindowInfosChanged once should not cause any issues.
735 * This test serves as a sanity check for the next test, where onWindowInfosChanged is
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -0700736 * called twice.
737 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -0800738TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -0700739 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700740 sp<FakeWindowHandle> window =
741 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
742 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -0700743 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -0700744
Siarhei Vishniakouc41de372023-07-20 13:14:26 -0700745 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800746 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700747 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
748 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800749 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -0700750
751 // Window should receive motion event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700752 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -0700753}
754
755/**
Siarhei Vishniakouc41de372023-07-20 13:14:26 -0700756 * Calling onWindowInfosChanged twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -0700757 */
758TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -0700759 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700760 sp<FakeWindowHandle> window =
761 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
762 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -0700763 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -0700764
Siarhei Vishniakouc41de372023-07-20 13:14:26 -0700765 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
766 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800767 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700768 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
769 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800770 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -0700771
772 // Window should receive motion event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700773 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -0700774}
775
Arthur Hungb92218b2018-08-14 12:00:21 +0800776// The foreground window should receive the first touch down event.
777TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -0700778 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700779 sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top",
780 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000781 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700782 sp<FakeWindowHandle>::make(application, mDispatcher, "Second",
783 ui::LogicalDisplayId::DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800784
Siarhei Vishniakouc41de372023-07-20 13:14:26 -0700785 mDispatcher->onWindowInfosChanged(
786 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800787 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700788 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
789 ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800790 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +0800791
792 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700793 windowTop->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800794 windowSecond->assertNoEvents();
795}
796
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000797/**
798 * Two windows: A top window, and a wallpaper behind the window.
799 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
800 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800801 * 1. foregroundWindow <-- dup touch to wallpaper
802 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000803 */
804TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
805 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
806 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700807 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground",
808 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800809 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000810 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700811 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper",
812 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800813 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000814
Siarhei Vishniakouc41de372023-07-20 13:14:26 -0700815 mDispatcher->onWindowInfosChanged(
816 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000817 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -0800818 injectMotionEvent(*mDispatcher,
819 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
820 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(200))
821 .build()))
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000822 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
823
824 // Both foreground window and its wallpaper should receive the touch down
825 foregroundWindow->consumeMotionDown();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700826 wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000827
828 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -0800829 injectMotionEvent(*mDispatcher,
830 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
831 .pointer(PointerBuilder(0, ToolType::FINGER).x(110).y(200))
832 .build()))
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000833 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
834
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -0800835 foregroundWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700836 wallpaperWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000837
838 // Now the foreground window goes away, but the wallpaper stays
Siarhei Vishniakouc41de372023-07-20 13:14:26 -0700839 mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000840 foregroundWindow->consumeMotionCancel();
841 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700842 wallpaperWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000843}
844
845/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -0800846 * Two fingers down on the window, and lift off the first finger.
847 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
848 * contains a single pointer.
849 */
850TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
851 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700852 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
853 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -0800854
Siarhei Vishniakouc41de372023-07-20 13:14:26 -0700855 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -0800856 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +0000857 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
858 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
859 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -0800860 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +0000861 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
862 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
863 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
864 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -0800865 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +0000866 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
867 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
868 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
869 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -0800870 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
871 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
872 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
873
874 // Remove the window. The gesture should be canceled
Siarhei Vishniakouc41de372023-07-20 13:14:26 -0700875 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -0800876 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
877 window->consumeMotionEvent(
878 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
879}
880
881/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -0800882 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
883 * with the following differences:
884 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
885 * clean up the connection.
886 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
887 * Ensure that there's no crash in the dispatcher.
888 */
889TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
890 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
891 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700892 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground",
893 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800894 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -0800895 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700896 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper",
897 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800898 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -0800899
Siarhei Vishniakouc41de372023-07-20 13:14:26 -0700900 mDispatcher->onWindowInfosChanged(
901 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2b030972021-11-18 10:01:27 -0800902 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700903 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
904 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Siarhei Vishniakou2b030972021-11-18 10:01:27 -0800905 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
906
907 // Both foreground window and its wallpaper should receive the touch down
908 foregroundWindow->consumeMotionDown();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700909 wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -0800910
911 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700912 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700913 ui::LogicalDisplayId::DEFAULT, {110, 200}))
Siarhei Vishniakou2b030972021-11-18 10:01:27 -0800914 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
915
916 foregroundWindow->consumeMotionMove();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700917 wallpaperWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -0800918
919 // Wallpaper closes its channel, but the window remains.
920 wallpaperWindow->destroyReceiver();
921 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
922
923 // Now the foreground window goes away, but the wallpaper stays, even though its channel
924 // is no longer valid.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -0700925 mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2b030972021-11-18 10:01:27 -0800926 foregroundWindow->consumeMotionCancel();
927}
928
Linnan Li72352222024-04-12 18:55:57 +0800929/**
930 * Two windows: left and right, and a separate wallpaper window underneath each. Device A sends a
931 * down event to the left window. Device B sends a down event to the right window. Next, the right
932 * window disappears. Both the right window and its wallpaper window should receive cancel event.
933 * The left window and its wallpaper window should not receive any events.
934 */
935TEST_F(InputDispatcherTest, MultiDeviceDisappearingWindowWithWallpaperWindows) {
936 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
937 sp<FakeWindowHandle> leftForegroundWindow =
938 sp<FakeWindowHandle>::make(application, mDispatcher, "Left foreground window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700939 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +0800940 leftForegroundWindow->setFrame(Rect(0, 0, 100, 100));
941 leftForegroundWindow->setDupTouchToWallpaper(true);
942 sp<FakeWindowHandle> leftWallpaperWindow =
943 sp<FakeWindowHandle>::make(application, mDispatcher, "Left wallpaper window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700944 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +0800945 leftWallpaperWindow->setFrame(Rect(0, 0, 100, 100));
946 leftWallpaperWindow->setIsWallpaper(true);
947
948 sp<FakeWindowHandle> rightForegroundWindow =
949 sp<FakeWindowHandle>::make(application, mDispatcher, "Right foreground window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700950 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +0800951 rightForegroundWindow->setFrame(Rect(100, 0, 200, 100));
952 rightForegroundWindow->setDupTouchToWallpaper(true);
953 sp<FakeWindowHandle> rightWallpaperWindow =
954 sp<FakeWindowHandle>::make(application, mDispatcher, "Right wallpaper window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700955 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +0800956 rightWallpaperWindow->setFrame(Rect(100, 0, 200, 100));
957 rightWallpaperWindow->setIsWallpaper(true);
958
959 mDispatcher->onWindowInfosChanged(
960 {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(),
961 *rightForegroundWindow->getInfo(), *rightWallpaperWindow->getInfo()},
962 {},
963 0,
964 0});
965
966 const DeviceId deviceA = 9;
967 const DeviceId deviceB = 3;
968 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
969 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
970 .deviceId(deviceA)
971 .build());
972 leftForegroundWindow->consumeMotionEvent(
973 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA)));
974 leftWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN),
975 WithDeviceId(deviceA),
976 WithFlags(EXPECTED_WALLPAPER_FLAGS)));
977
978 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
979 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
980 .deviceId(deviceB)
981 .build());
982 rightForegroundWindow->consumeMotionEvent(
983 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB)));
984 rightWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN),
985 WithDeviceId(deviceB),
986 WithFlags(EXPECTED_WALLPAPER_FLAGS)));
987
988 // Now right foreground window disappears, but right wallpaper window remains.
989 mDispatcher->onWindowInfosChanged(
990 {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(),
991 *rightWallpaperWindow->getInfo()},
992 {},
993 0,
994 0});
995
996 // Left foreground window and left wallpaper window still exist, and should not receive any
997 // events.
998 leftForegroundWindow->assertNoEvents();
999 leftWallpaperWindow->assertNoEvents();
1000 // Since right foreground window disappeared, right wallpaper window and right foreground window
1001 // should receive cancel events.
1002 rightForegroundWindow->consumeMotionEvent(
1003 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB)));
1004 rightWallpaperWindow->consumeMotionEvent(
1005 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB),
1006 WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_CANCELED)));
1007}
1008
1009/**
1010 * Three windows arranged horizontally and without any overlap. Every window has a
1011 * wallpaper window underneath. The middle window also has SLIPPERY flag.
1012 * Device A sends a down event to the left window. Device B sends a down event to the middle window.
1013 * Next, device B sends move event to the right window. Touch for device B should slip from the
1014 * middle window to the right window. Also, the right wallpaper window should receive a down event.
1015 * The middle window and its wallpaper window should receive a cancel event. The left window should
1016 * not receive any events. If device B continues to report events, the right window and its
1017 * wallpaper window should receive remaining events.
1018 */
1019TEST_F(InputDispatcherTest, MultiDeviceSlipperyTouchWithWallpaperWindow) {
1020 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1021 sp<FakeWindowHandle> leftForegroundWindow =
1022 sp<FakeWindowHandle>::make(application, mDispatcher, "Left foreground window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001023 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +08001024 leftForegroundWindow->setFrame(Rect(0, 0, 100, 100));
1025 leftForegroundWindow->setDupTouchToWallpaper(true);
1026 sp<FakeWindowHandle> leftWallpaperWindow =
1027 sp<FakeWindowHandle>::make(application, mDispatcher, "Left wallpaper window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001028 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +08001029 leftWallpaperWindow->setFrame(Rect(0, 0, 100, 100));
1030 leftWallpaperWindow->setIsWallpaper(true);
1031
1032 sp<FakeWindowHandle> middleForegroundWindow =
1033 sp<FakeWindowHandle>::make(application, mDispatcher, "Middle foreground window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001034 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +08001035 middleForegroundWindow->setFrame(Rect(100, 0, 200, 100));
1036 middleForegroundWindow->setDupTouchToWallpaper(true);
1037 middleForegroundWindow->setSlippery(true);
1038 sp<FakeWindowHandle> middleWallpaperWindow =
1039 sp<FakeWindowHandle>::make(application, mDispatcher, "Middle wallpaper window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001040 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +08001041 middleWallpaperWindow->setFrame(Rect(100, 0, 200, 100));
1042 middleWallpaperWindow->setIsWallpaper(true);
1043
1044 sp<FakeWindowHandle> rightForegroundWindow =
1045 sp<FakeWindowHandle>::make(application, mDispatcher, "Right foreground window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001046 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +08001047 rightForegroundWindow->setFrame(Rect(200, 0, 300, 100));
1048 rightForegroundWindow->setDupTouchToWallpaper(true);
1049 sp<FakeWindowHandle> rightWallpaperWindow =
1050 sp<FakeWindowHandle>::make(application, mDispatcher, "Right wallpaper window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001051 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +08001052 rightWallpaperWindow->setFrame(Rect(200, 0, 300, 100));
1053 rightWallpaperWindow->setIsWallpaper(true);
1054
1055 mDispatcher->onWindowInfosChanged(
1056 {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(),
1057 *middleForegroundWindow->getInfo(), *middleWallpaperWindow->getInfo(),
1058 *rightForegroundWindow->getInfo(), *rightWallpaperWindow->getInfo()},
1059 {},
1060 0,
1061 0});
1062
1063 const DeviceId deviceA = 9;
1064 const DeviceId deviceB = 3;
1065 // Device A sends a DOWN event to the left window
1066 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1067 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
1068 .deviceId(deviceA)
1069 .build());
1070 leftForegroundWindow->consumeMotionEvent(
1071 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA)));
1072 leftWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN),
1073 WithDeviceId(deviceA),
1074 WithFlags(EXPECTED_WALLPAPER_FLAGS)));
1075 // Device B sends a DOWN event to the middle window
1076 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1077 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
1078 .deviceId(deviceB)
1079 .build());
1080 middleForegroundWindow->consumeMotionEvent(
1081 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB)));
1082 middleWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN),
1083 WithDeviceId(deviceB),
1084 WithFlags(EXPECTED_WALLPAPER_FLAGS)));
1085 // Move the events of device B to the top of the right window.
1086 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1087 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50))
1088 .deviceId(deviceB)
1089 .build());
1090 middleForegroundWindow->consumeMotionEvent(
1091 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB)));
1092 middleWallpaperWindow->consumeMotionEvent(
1093 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB),
1094 WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_CANCELED)));
1095 rightForegroundWindow->consumeMotionEvent(
1096 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB)));
1097 rightWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN),
1098 WithDeviceId(deviceB),
1099 WithFlags(EXPECTED_WALLPAPER_FLAGS)));
1100 // Make sure the window on the right can receive the remaining events.
1101 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1102 .pointer(PointerBuilder(0, ToolType::FINGER).x(251).y(51))
1103 .deviceId(deviceB)
1104 .build());
1105 leftForegroundWindow->assertNoEvents();
1106 leftWallpaperWindow->assertNoEvents();
1107 middleForegroundWindow->assertNoEvents();
1108 middleWallpaperWindow->assertNoEvents();
1109 rightForegroundWindow->consumeMotionEvent(
1110 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB)));
1111 rightWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE),
1112 WithDeviceId(deviceB),
1113 WithFlags(EXPECTED_WALLPAPER_FLAGS)));
1114}
1115
1116/**
1117 * Similar to the test above, we have three windows, they are arranged horizontally and without any
1118 * overlap, and every window has a wallpaper window. The middle window is a simple window, without
1119 * any special flags. Device A reports a down event that lands in left window. Device B sends a down
1120 * event to the middle window and then touch is transferred from the middle window to the right
1121 * window. The right window and its wallpaper window should receive a down event. The middle window
1122 * and its wallpaper window should receive a cancel event. The left window should not receive any
1123 * events. Subsequent events reported by device B should go to the right window and its wallpaper.
1124 */
1125TEST_F(InputDispatcherTest, MultiDeviceTouchTransferWithWallpaperWindows) {
1126 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1127 sp<FakeWindowHandle> leftForegroundWindow =
1128 sp<FakeWindowHandle>::make(application, mDispatcher, "Left foreground window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001129 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +08001130 leftForegroundWindow->setFrame(Rect(0, 0, 100, 100));
1131 leftForegroundWindow->setDupTouchToWallpaper(true);
1132 sp<FakeWindowHandle> leftWallpaperWindow =
1133 sp<FakeWindowHandle>::make(application, mDispatcher, "Left wallpaper window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001134 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +08001135 leftWallpaperWindow->setFrame(Rect(0, 0, 100, 100));
1136 leftWallpaperWindow->setIsWallpaper(true);
1137
1138 sp<FakeWindowHandle> middleForegroundWindow =
1139 sp<FakeWindowHandle>::make(application, mDispatcher, "Middle foreground window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001140 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +08001141 middleForegroundWindow->setFrame(Rect(100, 0, 200, 100));
1142 middleForegroundWindow->setDupTouchToWallpaper(true);
1143 sp<FakeWindowHandle> middleWallpaperWindow =
1144 sp<FakeWindowHandle>::make(application, mDispatcher, "Middle wallpaper window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001145 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +08001146 middleWallpaperWindow->setFrame(Rect(100, 0, 200, 100));
1147 middleWallpaperWindow->setIsWallpaper(true);
1148
1149 sp<FakeWindowHandle> rightForegroundWindow =
1150 sp<FakeWindowHandle>::make(application, mDispatcher, "Right foreground window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001151 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +08001152 rightForegroundWindow->setFrame(Rect(200, 0, 300, 100));
1153 rightForegroundWindow->setDupTouchToWallpaper(true);
1154 sp<FakeWindowHandle> rightWallpaperWindow =
1155 sp<FakeWindowHandle>::make(application, mDispatcher, "Right wallpaper window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001156 ui::LogicalDisplayId::DEFAULT);
Linnan Li72352222024-04-12 18:55:57 +08001157 rightWallpaperWindow->setFrame(Rect(200, 0, 300, 100));
1158 rightWallpaperWindow->setIsWallpaper(true);
1159
1160 mDispatcher->onWindowInfosChanged(
1161 {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(),
1162 *middleForegroundWindow->getInfo(), *middleWallpaperWindow->getInfo(),
1163 *rightForegroundWindow->getInfo(), *rightWallpaperWindow->getInfo()},
1164 {},
1165 0,
1166 0});
1167
1168 const DeviceId deviceA = 9;
1169 const DeviceId deviceB = 3;
1170 // Device A touch down on the left window
1171 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1172 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
1173 .deviceId(deviceA)
1174 .build());
1175 leftForegroundWindow->consumeMotionEvent(
1176 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA)));
1177 leftWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN),
1178 WithDeviceId(deviceA),
1179 WithFlags(EXPECTED_WALLPAPER_FLAGS)));
1180 // Device B touch down on the middle window
1181 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1182 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
1183 .deviceId(deviceB)
1184 .build());
1185 middleForegroundWindow->consumeMotionEvent(
1186 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB)));
1187 middleWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN),
1188 WithDeviceId(deviceB),
1189 WithFlags(EXPECTED_WALLPAPER_FLAGS)));
1190
1191 // Transfer touch from the middle window to the right window.
1192 ASSERT_TRUE(mDispatcher->transferTouchGesture(middleForegroundWindow->getToken(),
1193 rightForegroundWindow->getToken()));
1194
1195 middleForegroundWindow->consumeMotionEvent(
1196 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB)));
1197 middleWallpaperWindow->consumeMotionEvent(
1198 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB),
1199 WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_CANCELED)));
1200 rightForegroundWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN),
1201 WithDeviceId(deviceB),
1202 WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)));
1203 rightWallpaperWindow->consumeMotionEvent(
1204 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB),
1205 WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)));
1206
1207 // Make sure the right window can receive the remaining events.
1208 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1209 .pointer(PointerBuilder(0, ToolType::FINGER).x(251).y(51))
1210 .deviceId(deviceB)
1211 .build());
1212 leftForegroundWindow->assertNoEvents();
1213 leftWallpaperWindow->assertNoEvents();
1214 middleForegroundWindow->assertNoEvents();
1215 middleWallpaperWindow->assertNoEvents();
1216 rightForegroundWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE),
1217 WithDeviceId(deviceB),
1218 WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)));
1219 rightWallpaperWindow->consumeMotionEvent(
1220 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB),
1221 WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)));
1222}
1223
Arthur Hungc539dbb2022-12-08 07:45:36 +00001224class ShouldSplitTouchFixture : public InputDispatcherTest,
1225 public ::testing::WithParamInterface<bool> {};
1226INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1227 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001228/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001229 * A single window that receives touch (on top), and a wallpaper window underneath it.
1230 * The top window gets a multitouch gesture.
1231 * Ensure that wallpaper gets the same gesture.
1232 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00001233TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001234 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001235 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001236 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground",
1237 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001238 foregroundWindow->setDupTouchToWallpaper(true);
1239 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001240
1241 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001242 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper",
1243 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001244 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001245
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001246 mDispatcher->onWindowInfosChanged(
1247 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001248
1249 // Touch down on top window
1250 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001251 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
1252 ui::LogicalDisplayId::DEFAULT, {100, 100}))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001253 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1254
1255 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00001256 foregroundWindow->consumeMotionDown();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001257 wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001258
1259 // Second finger down on the top window
1260 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001261 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001262 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001263 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1264 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001265 .build();
1266 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001267 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001268 InputEventInjectionSync::WAIT_FOR_RESULT))
1269 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00001270 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001271 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ui::LogicalDisplayId::DEFAULT,
Linnan Li72352222024-04-12 18:55:57 +08001272 EXPECTED_WALLPAPER_FLAGS);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001273
1274 const MotionEvent secondFingerUpEvent =
1275 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001276 .displayId(ui::LogicalDisplayId::DEFAULT)
Arthur Hungc539dbb2022-12-08 07:45:36 +00001277 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001278 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1279 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001280 .build();
1281 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001282 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00001283 InputEventInjectionSync::WAIT_FOR_RESULT))
1284 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan33cfc6d2024-06-11 20:17:44 +00001285 foregroundWindow->consumeMotionPointerUp(/*pointerIdx=*/0,
1286 WithDisplayId(ui::LogicalDisplayId::DEFAULT));
1287 wallpaperWindow->consumeMotionPointerUp(/*pointerIdx=*/0,
1288 AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT),
1289 WithFlags(EXPECTED_WALLPAPER_FLAGS)));
Arthur Hungc539dbb2022-12-08 07:45:36 +00001290
1291 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001292 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001293 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
1294 AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001295 .displayId(ui::LogicalDisplayId::DEFAULT)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001296 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts101ee9b2023-07-06 18:04:14 +00001297 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001298 .x(100)
1299 .y(100))
1300 .build(),
1301 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001302 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001303 foregroundWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
1304 wallpaperWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001305}
1306
1307/**
1308 * Two windows: a window on the left and window on the right.
1309 * A third window, wallpaper, is behind both windows, and spans both top windows.
1310 * The first touch down goes to the left window. A second pointer touches down on the right window.
1311 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1312 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1313 * ACTION_POINTER_DOWN(1).
1314 */
1315TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1316 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001317 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
1318 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001319 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001320 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001321
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001322 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
1323 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001324 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001325 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001326
1327 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001328 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper",
1329 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001330 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001331 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001332
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001333 mDispatcher->onWindowInfosChanged(
1334 {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()},
1335 {},
1336 0,
1337 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001338
1339 // Touch down on left window
1340 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001341 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
1342 ui::LogicalDisplayId::DEFAULT, {100, 100}))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001343 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1344
1345 // Both foreground window and its wallpaper should receive the touch down
1346 leftWindow->consumeMotionDown();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001347 wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001348
1349 // Second finger down on the right window
1350 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001351 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001352 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001353 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1354 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001355 .build();
1356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001357 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001358 InputEventInjectionSync::WAIT_FOR_RESULT))
1359 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1360
1361 leftWindow->consumeMotionMove();
1362 // Since the touch is split, right window gets ACTION_DOWN
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001363 rightWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
1364 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ui::LogicalDisplayId::DEFAULT,
Linnan Li72352222024-04-12 18:55:57 +08001365 EXPECTED_WALLPAPER_FLAGS);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001366
1367 // Now, leftWindow, which received the first finger, disappears.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001368 mDispatcher->onWindowInfosChanged(
1369 {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001370 leftWindow->consumeMotionCancel();
1371 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001372 wallpaperWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001373
1374 // The pointer that's still down on the right window moves, and goes to the right window only.
1375 // As far as the dispatcher's concerned though, both pointers are still present.
1376 const MotionEvent secondFingerMoveEvent =
1377 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1378 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001379 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1380 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001381 .build();
1382 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001383 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001384 InputEventInjectionSync::WAIT_FOR_RESULT));
1385 rightWindow->consumeMotionMove();
1386
1387 leftWindow->assertNoEvents();
1388 rightWindow->assertNoEvents();
1389 wallpaperWindow->assertNoEvents();
1390}
1391
Arthur Hungc539dbb2022-12-08 07:45:36 +00001392/**
1393 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
1394 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
1395 * The right window should receive ACTION_DOWN.
1396 */
1397TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00001398 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001399 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
1400 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001401 leftWindow->setFrame(Rect(0, 0, 200, 200));
1402 leftWindow->setDupTouchToWallpaper(true);
1403 leftWindow->setSlippery(true);
1404
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001405 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
1406 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001407 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00001408
1409 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001410 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper",
1411 ui::LogicalDisplayId::DEFAULT);
Arthur Hung74c248d2022-11-23 07:09:59 +00001412 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00001413
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001414 mDispatcher->onWindowInfosChanged(
1415 {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()},
1416 {},
1417 0,
1418 0});
Arthur Hung74c248d2022-11-23 07:09:59 +00001419
Arthur Hungc539dbb2022-12-08 07:45:36 +00001420 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00001421 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001422 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
1423 ui::LogicalDisplayId::DEFAULT, {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00001424 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00001425
1426 // Both foreground window and its wallpaper should receive the touch down
1427 leftWindow->consumeMotionDown();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001428 wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Arthur Hung74c248d2022-11-23 07:09:59 +00001429
Arthur Hungc539dbb2022-12-08 07:45:36 +00001430 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00001431 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001432 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001433 ui::LogicalDisplayId::DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00001434 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1435
Arthur Hungc539dbb2022-12-08 07:45:36 +00001436 leftWindow->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001437 rightWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
1438 wallpaperWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Arthur Hung74c248d2022-11-23 07:09:59 +00001439}
1440
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001441/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001442 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
1443 * interactive, it might stop sending this flag.
1444 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
1445 * to have a consistent input stream.
1446 *
1447 * Test procedure:
1448 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
1449 * DOWN (new gesture).
1450 *
1451 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
1452 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
1453 *
1454 * We technically just need a single window here, but we are using two windows (spy on top and a
1455 * regular window below) to emulate the actual situation where it happens on the device.
1456 */
1457TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
1458 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001459 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
1460 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001461 spyWindow->setFrame(Rect(0, 0, 200, 200));
1462 spyWindow->setTrustedOverlay(true);
1463 spyWindow->setSpy(true);
1464
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001465 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
1466 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001467 window->setFrame(Rect(0, 0, 200, 200));
1468
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001469 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001470 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001471
1472 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001473 mDispatcher->notifyMotion(
1474 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1475 .deviceId(touchDeviceId)
1476 .policyFlags(DEFAULT_POLICY_FLAGS)
1477 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1478 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001479
Prabir Pradhan678438e2023-04-13 19:32:51 +00001480 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1481 .deviceId(touchDeviceId)
1482 .policyFlags(DEFAULT_POLICY_FLAGS)
1483 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1484 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
1485 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001486 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
1487 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1488 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
1489 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1490
1491 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00001492 mDispatcher->notifyMotion(
1493 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
1494 .deviceId(touchDeviceId)
1495 .policyFlags(0)
1496 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1497 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
1498 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001499 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
1500 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
1501
1502 // We don't need to reset the device to reproduce the issue, but the reset event typically
1503 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00001504 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001505
1506 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00001507 mDispatcher->notifyMotion(
1508 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1509 .deviceId(touchDeviceId)
1510 .policyFlags(DEFAULT_POLICY_FLAGS)
1511 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1512 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001513 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
1514 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
1515
1516 // No more events
1517 spyWindow->assertNoEvents();
1518 window->assertNoEvents();
1519}
1520
1521/**
Linnan Li907ae732023-09-05 17:14:21 +08001522 * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers.
1523 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
1524 * interactive, it might stop sending this flag.
1525 * We've already ensured the consistency of the touch event in this case, and we should also ensure
1526 * the consistency of the hover event in this case.
1527 *
1528 * Test procedure:
1529 * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT
1530 * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT
1531 *
1532 * We expect to receive two full streams of hover events.
1533 */
1534TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) {
1535 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1536
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001537 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
1538 ui::LogicalDisplayId::DEFAULT);
Linnan Li907ae732023-09-05 17:14:21 +08001539 window->setFrame(Rect(0, 0, 300, 300));
1540
1541 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
1542
1543 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
1544 .policyFlags(DEFAULT_POLICY_FLAGS)
1545 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101))
1546 .build());
1547 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
1548
1549 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
1550 .policyFlags(DEFAULT_POLICY_FLAGS)
1551 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
1552 .build());
1553 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
1554
1555 // Send hover exit without the default policy flags.
1556 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
1557 .policyFlags(0)
1558 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
1559 .build());
1560
1561 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
1562
1563 // Send a simple hover event stream, ensure dispatcher not crashed and window can receive
1564 // right event.
1565 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
1566 .policyFlags(DEFAULT_POLICY_FLAGS)
1567 .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201))
1568 .build());
1569 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
1570
1571 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
1572 .policyFlags(DEFAULT_POLICY_FLAGS)
1573 .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202))
1574 .build());
1575 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
1576
1577 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
1578 .policyFlags(DEFAULT_POLICY_FLAGS)
1579 .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202))
1580 .build());
1581 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
1582}
1583
1584/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001585 * Two windows: a window on the left and a window on the right.
1586 * Mouse is hovered from the right window into the left window.
1587 * Next, we tap on the left window, where the cursor was last seen.
1588 * The second tap is done onto the right window.
1589 * The mouse and tap are from two different devices.
1590 * We technically don't need to set the downtime / eventtime for these events, but setting these
1591 * explicitly helps during debugging.
1592 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
1593 * In the buggy implementation, a tap on the right window would cause a crash.
1594 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07001595TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap_legacy) {
1596 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
1597
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001598 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001599 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
1600 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001601 leftWindow->setFrame(Rect(0, 0, 200, 200));
1602
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001603 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
1604 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001605 rightWindow->setFrame(Rect(200, 0, 400, 200));
1606
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001607 mDispatcher->onWindowInfosChanged(
1608 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001609 // All times need to start at the current time, otherwise the dispatcher will drop the events as
1610 // stale.
1611 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
1612 const int32_t mouseDeviceId = 6;
1613 const int32_t touchDeviceId = 4;
1614 // Move the cursor from right
1615 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001616 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001617 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1618 AINPUT_SOURCE_MOUSE)
1619 .deviceId(mouseDeviceId)
1620 .downTime(baseTime + 10)
1621 .eventTime(baseTime + 20)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001622 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001623 .build()));
1624 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
1625
1626 // .. to the left window
1627 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001628 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001629 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
1630 AINPUT_SOURCE_MOUSE)
1631 .deviceId(mouseDeviceId)
1632 .downTime(baseTime + 10)
1633 .eventTime(baseTime + 30)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001634 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001635 .build()));
1636 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
1637 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
1638 // Now tap the left window
1639 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001640 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001641 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
1642 AINPUT_SOURCE_TOUCHSCREEN)
1643 .deviceId(touchDeviceId)
1644 .downTime(baseTime + 40)
1645 .eventTime(baseTime + 40)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001646 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001647 .build()));
1648 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
1649 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
1650
1651 // release tap
1652 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001653 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001654 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
1655 AINPUT_SOURCE_TOUCHSCREEN)
1656 .deviceId(touchDeviceId)
1657 .downTime(baseTime + 40)
1658 .eventTime(baseTime + 50)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001659 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001660 .build()));
1661 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
1662
1663 // Tap the window on the right
1664 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001665 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001666 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
1667 AINPUT_SOURCE_TOUCHSCREEN)
1668 .deviceId(touchDeviceId)
1669 .downTime(baseTime + 60)
1670 .eventTime(baseTime + 60)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001671 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001672 .build()));
1673 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
1674
1675 // release tap
1676 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001677 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001678 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
1679 AINPUT_SOURCE_TOUCHSCREEN)
1680 .deviceId(touchDeviceId)
1681 .downTime(baseTime + 60)
1682 .eventTime(baseTime + 70)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001683 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001684 .build()));
1685 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
1686
1687 // No more events
1688 leftWindow->assertNoEvents();
1689 rightWindow->assertNoEvents();
1690}
1691
1692/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07001693 * Two windows: a window on the left and a window on the right.
1694 * Mouse is hovered from the right window into the left window.
1695 * Next, we tap on the left window, where the cursor was last seen.
1696 * The second tap is done onto the right window.
1697 * The mouse and tap are from two different devices.
1698 * We technically don't need to set the downtime / eventtime for these events, but setting these
1699 * explicitly helps during debugging.
1700 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
1701 * In the buggy implementation, a tap on the right window would cause a crash.
1702 */
1703TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
1704 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
1705
1706 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001707 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
1708 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07001709 leftWindow->setFrame(Rect(0, 0, 200, 200));
1710
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001711 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
1712 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07001713 rightWindow->setFrame(Rect(200, 0, 400, 200));
1714
1715 mDispatcher->onWindowInfosChanged(
1716 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
1717 // All times need to start at the current time, otherwise the dispatcher will drop the events as
1718 // stale.
1719 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
1720 const int32_t mouseDeviceId = 6;
1721 const int32_t touchDeviceId = 4;
1722 // Move the cursor from right
1723 mDispatcher->notifyMotion(
1724 MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
1725 .deviceId(mouseDeviceId)
1726 .downTime(baseTime + 10)
1727 .eventTime(baseTime + 20)
1728 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100))
1729 .build());
1730 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
1731
1732 // .. to the left window
1733 mDispatcher->notifyMotion(
1734 MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
1735 .deviceId(mouseDeviceId)
1736 .downTime(baseTime + 10)
1737 .eventTime(baseTime + 30)
1738 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100))
1739 .build());
1740 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
1741 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
1742 // Now tap the left window
1743 mDispatcher->notifyMotion(
1744 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1745 .deviceId(touchDeviceId)
1746 .downTime(baseTime + 40)
1747 .eventTime(baseTime + 40)
1748 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1749 .build());
1750 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
1751
1752 // release tap
1753 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
1754 .deviceId(touchDeviceId)
1755 .downTime(baseTime + 40)
1756 .eventTime(baseTime + 50)
1757 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1758 .build());
1759 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
1760
1761 // Tap the window on the right
1762 mDispatcher->notifyMotion(
1763 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1764 .deviceId(touchDeviceId)
1765 .downTime(baseTime + 60)
1766 .eventTime(baseTime + 60)
1767 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
1768 .build());
1769 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
1770
1771 // release tap
1772 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
1773 .deviceId(touchDeviceId)
1774 .downTime(baseTime + 60)
1775 .eventTime(baseTime + 70)
1776 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
1777 .build());
1778 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
1779
1780 // No more events
1781 leftWindow->assertNoEvents();
1782 rightWindow->assertNoEvents();
1783}
1784
1785/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07001786 * Start hovering in a window. While this hover is still active, make another window appear on top.
1787 * The top, obstructing window has no input channel, so it's not supposed to receive input.
1788 * While the top window is present, the hovering is stopped.
1789 * Later, hovering gets resumed again.
1790 * Ensure that new hover gesture is handled correctly.
1791 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
1792 * to the window that's currently being hovered over.
1793 */
1794TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
1795 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001796 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
1797 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07001798 window->setFrame(Rect(0, 0, 200, 200));
1799
1800 // Only a single window is present at first
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001801 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07001802
1803 // Start hovering in the window
1804 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
1805 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
1806 .build());
1807 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
1808
1809 // Now, an obscuring window appears!
1810 sp<FakeWindowHandle> obscuringWindow =
1811 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001812 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00001813 /*createInputChannel=*/false);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07001814 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
1815 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
1816 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
1817 obscuringWindow->setNoInputChannel(true);
1818 obscuringWindow->setFocusable(false);
1819 obscuringWindow->setAlpha(1.0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001820 mDispatcher->onWindowInfosChanged(
1821 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07001822
1823 // While this new obscuring window is present, the hovering is stopped
1824 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
1825 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
1826 .build());
1827 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
1828
1829 // Now the obscuring window goes away.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001830 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07001831
1832 // And a new hover gesture starts.
1833 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
1834 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
1835 .build());
1836 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
1837}
1838
1839/**
1840 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
1841 * the obscuring window.
1842 */
1843TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
1844 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001845 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
1846 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07001847 window->setFrame(Rect(0, 0, 200, 200));
1848
1849 // Only a single window is present at first
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001850 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07001851
1852 // Start hovering in the window
1853 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
1854 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
1855 .build());
1856 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
1857
1858 // Now, an obscuring window appears!
1859 sp<FakeWindowHandle> obscuringWindow =
1860 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001861 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00001862 /*createInputChannel=*/false);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07001863 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
1864 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
1865 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
1866 obscuringWindow->setNoInputChannel(true);
1867 obscuringWindow->setFocusable(false);
1868 obscuringWindow->setAlpha(1.0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001869 mDispatcher->onWindowInfosChanged(
1870 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07001871
1872 // While this new obscuring window is present, the hovering continues. The event can't go to the
1873 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
1874 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
1875 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
1876 .build());
1877 obscuringWindow->assertNoEvents();
1878 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
1879
1880 // Now the obscuring window goes away.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001881 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07001882
1883 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
1884 // so it should generate a HOVER_ENTER
1885 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
1886 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
1887 .build());
1888 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
1889
1890 // Now the MOVE should be getting dispatched normally
1891 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
1892 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
1893 .build());
1894 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
1895}
1896
1897/**
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07001898 * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll
1899 * events are delivered to the window.
1900 */
1901TEST_F(InputDispatcherTest, HoverMoveAndScroll) {
1902 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001903 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
1904 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07001905 window->setFrame(Rect(0, 0, 200, 200));
1906 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
1907
1908 // Start hovering in the window
1909 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
1910 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
1911 .build());
1912 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
1913
1914 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
1915 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
1916 .build());
1917 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
1918
1919 // Scroll with the mouse
1920 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE)
1921 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
1922 .build());
1923 window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL));
1924}
1925
1926using InputDispatcherMultiDeviceTest = InputDispatcherTest;
1927
1928/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07001929 * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that
1930 * touch is dropped, because stylus should be preferred over touch.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07001931 */
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07001932TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) {
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07001933 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07001934 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001935 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
1936 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07001937 window->setFrame(Rect(0, 0, 200, 200));
1938
1939 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
1940
1941 constexpr int32_t touchDeviceId = 4;
1942 constexpr int32_t stylusDeviceId = 2;
1943
1944 // Stylus down
1945 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
1946 .deviceId(stylusDeviceId)
1947 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
1948 .build());
1949 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
1950
1951 // Touch down
1952 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1953 .deviceId(touchDeviceId)
1954 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
1955 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07001956
1957 // Touch move
1958 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1959 .deviceId(touchDeviceId)
1960 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
1961 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07001962 // Touch is ignored because stylus is already down
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07001963
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07001964 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07001965 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
1966 .deviceId(stylusDeviceId)
1967 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
1968 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07001969 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
1970 WithCoords(101, 111)));
1971
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07001972 window->assertNoEvents();
1973}
1974
1975/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07001976 * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that
1977 * touch is not dropped, because multiple devices are allowed to be active in the same window.
1978 */
1979TEST_F(InputDispatcherMultiDeviceTest, StylusDownDoesNotBlockTouchDown) {
1980 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
1981 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001982 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
1983 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07001984 window->setFrame(Rect(0, 0, 200, 200));
1985
1986 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
1987
1988 constexpr int32_t touchDeviceId = 4;
1989 constexpr int32_t stylusDeviceId = 2;
1990
1991 // Stylus down
1992 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
1993 .deviceId(stylusDeviceId)
1994 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
1995 .build());
1996 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
1997
1998 // Touch down
1999 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2000 .deviceId(touchDeviceId)
2001 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2002 .build());
2003 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2004
2005 // Touch move
2006 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2007 .deviceId(touchDeviceId)
2008 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2009 .build());
2010 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2011
2012 // Stylus move
2013 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2014 .deviceId(stylusDeviceId)
2015 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2016 .build());
2017 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2018 WithCoords(101, 111)));
2019
2020 window->assertNoEvents();
2021}
2022
2023/**
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002024 * One window and one spy window. Stylus down on the window. Next, touch from another device goes
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002025 * down. Ensure that touch is dropped, because stylus should be preferred over touch.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002026 * Similar test as above, but with added SPY window.
2027 */
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002028TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) {
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002029 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002030 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002031 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
2032 ui::LogicalDisplayId::DEFAULT);
2033 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
2034 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002035 spyWindow->setFrame(Rect(0, 0, 200, 200));
2036 spyWindow->setTrustedOverlay(true);
2037 spyWindow->setSpy(true);
2038 window->setFrame(Rect(0, 0, 200, 200));
2039
2040 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
2041
2042 constexpr int32_t touchDeviceId = 4;
2043 constexpr int32_t stylusDeviceId = 2;
2044
2045 // Stylus down
2046 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2047 .deviceId(stylusDeviceId)
2048 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2049 .build());
2050 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2051 spyWindow->consumeMotionEvent(
2052 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2053
2054 // Touch down
2055 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2056 .deviceId(touchDeviceId)
2057 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2058 .build());
2059
2060 // Touch move
2061 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2062 .deviceId(touchDeviceId)
2063 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2064 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002065
2066 // Touch is ignored because stylus is already down
2067
2068 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002069 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2070 .deviceId(stylusDeviceId)
2071 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2072 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002073 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2074 WithCoords(101, 111)));
2075 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2076 WithCoords(101, 111)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002077
2078 window->assertNoEvents();
2079 spyWindow->assertNoEvents();
2080}
2081
2082/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002083 * One window and one spy window. Stylus down on the window. Next, touch from another device goes
2084 * down. Ensure that touch is not dropped, because multiple devices can be active at the same time.
2085 * Similar test as above, but with added SPY window.
2086 */
2087TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyDoesNotBlockTouchDown) {
2088 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
2089 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002090 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
2091 ui::LogicalDisplayId::DEFAULT);
2092 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
2093 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002094 spyWindow->setFrame(Rect(0, 0, 200, 200));
2095 spyWindow->setTrustedOverlay(true);
2096 spyWindow->setSpy(true);
2097 window->setFrame(Rect(0, 0, 200, 200));
2098
2099 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
2100
2101 constexpr int32_t touchDeviceId = 4;
2102 constexpr int32_t stylusDeviceId = 2;
2103
2104 // Stylus down
2105 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2106 .deviceId(stylusDeviceId)
2107 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2108 .build());
2109 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2110 spyWindow->consumeMotionEvent(
2111 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2112
2113 // Touch down
2114 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2115 .deviceId(touchDeviceId)
2116 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2117 .build());
2118 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2119 spyWindow->consumeMotionEvent(
2120 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2121
2122 // Touch move
2123 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2124 .deviceId(touchDeviceId)
2125 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2126 .build());
2127 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2128 spyWindow->consumeMotionEvent(
2129 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2130
2131 // Subsequent stylus movements are delivered correctly
2132 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2133 .deviceId(stylusDeviceId)
2134 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2135 .build());
2136 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2137 WithCoords(101, 111)));
2138 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2139 WithCoords(101, 111)));
2140
2141 window->assertNoEvents();
2142 spyWindow->assertNoEvents();
2143}
2144
2145/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002146 * One window. Stylus hover on the window. Next, touch from another device goes down. Ensure that
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002147 * touch is dropped, because stylus hover takes precedence.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002148 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002149TEST_F(InputDispatcherMultiDeviceTest, StylusHoverBlocksTouchDown) {
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002150 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002151 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002152 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
2153 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002154 window->setFrame(Rect(0, 0, 200, 200));
2155
2156 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2157
2158 constexpr int32_t touchDeviceId = 4;
2159 constexpr int32_t stylusDeviceId = 2;
2160
2161 // Stylus down on the window
2162 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2163 .deviceId(stylusDeviceId)
2164 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2165 .build());
2166 window->consumeMotionEvent(
2167 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
2168
2169 // Touch down on window
2170 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2171 .deviceId(touchDeviceId)
2172 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2173 .build());
2174 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2175 .deviceId(touchDeviceId)
2176 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2177 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002178
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002179 // Touch is ignored because stylus is hovering
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002180
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002181 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002182 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2183 .deviceId(stylusDeviceId)
2184 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2185 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002186 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE),
2187 WithDeviceId(stylusDeviceId), WithCoords(101, 111)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002188
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002189 // and subsequent touches continue to be ignored
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002190 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2191 .deviceId(touchDeviceId)
2192 .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147))
2193 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002194 window->assertNoEvents();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002195}
2196
2197/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002198 * One window. Stylus hover on the window. Next, touch from another device goes down. Ensure that
2199 * touch is not dropped, because stylus hover and touch can be both active at the same time.
2200 */
2201TEST_F(InputDispatcherMultiDeviceTest, StylusHoverDoesNotBlockTouchDown) {
2202 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
2203 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002204 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
2205 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002206 window->setFrame(Rect(0, 0, 200, 200));
2207
2208 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2209
2210 constexpr int32_t touchDeviceId = 4;
2211 constexpr int32_t stylusDeviceId = 2;
2212
2213 // Stylus down on the window
2214 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2215 .deviceId(stylusDeviceId)
2216 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2217 .build());
2218 window->consumeMotionEvent(
2219 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
2220
2221 // Touch down on window
2222 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2223 .deviceId(touchDeviceId)
2224 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2225 .build());
2226 // Touch move on window
2227 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2228 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2229 .deviceId(touchDeviceId)
2230 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2231 .build());
2232 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2233
2234 // Subsequent stylus movements are delivered correctly
2235 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2236 .deviceId(stylusDeviceId)
2237 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2238 .build());
2239 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE),
2240 WithDeviceId(stylusDeviceId), WithCoords(101, 111)));
2241
2242 // and subsequent touches continue to work
2243 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2244 .deviceId(touchDeviceId)
2245 .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147))
2246 .build());
2247 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2248 window->assertNoEvents();
2249}
2250
2251/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002252 * One window. Touch down on the window. Then, stylus hover on the window from another device.
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002253 * Ensure that touch is canceled, because stylus hover should take precedence.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002254 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002255TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusHover) {
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002256 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002257 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002258 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
2259 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002260 window->setFrame(Rect(0, 0, 200, 200));
2261
2262 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2263
2264 constexpr int32_t touchDeviceId = 4;
2265 constexpr int32_t stylusDeviceId = 2;
2266
2267 // Touch down on window
2268 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2269 .deviceId(touchDeviceId)
2270 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2271 .build());
2272 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2273 .deviceId(touchDeviceId)
2274 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2275 .build());
2276 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2277 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2278
2279 // Stylus hover on the window
2280 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2281 .deviceId(stylusDeviceId)
2282 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2283 .build());
2284 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2285 .deviceId(stylusDeviceId)
2286 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2287 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002288 // Stylus hover movement causes touch to be canceled
2289 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
2290 WithCoords(141, 146)));
2291 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
2292 WithDeviceId(stylusDeviceId), WithCoords(100, 110)));
2293 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE),
2294 WithDeviceId(stylusDeviceId), WithCoords(101, 111)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002295
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002296 // Subsequent touch movements are ignored
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002297 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2298 .deviceId(touchDeviceId)
2299 .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147))
2300 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002301
2302 window->assertNoEvents();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002303}
2304
2305/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002306 * One window. Touch down on the window. Then, stylus hover on the window from another device.
2307 * Ensure that touch is not canceled, because stylus hover can be active at the same time as touch.
2308 */
2309TEST_F(InputDispatcherMultiDeviceTest, TouchIsNotCanceledByStylusHover) {
2310 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
2311 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002312 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
2313 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002314 window->setFrame(Rect(0, 0, 200, 200));
2315
2316 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2317
2318 constexpr int32_t touchDeviceId = 4;
2319 constexpr int32_t stylusDeviceId = 2;
2320
2321 // Touch down on window
2322 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2323 .deviceId(touchDeviceId)
2324 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2325 .build());
2326 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2327 .deviceId(touchDeviceId)
2328 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2329 .build());
2330 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2331 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2332
2333 // Stylus hover on the window
2334 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2335 .deviceId(stylusDeviceId)
2336 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2337 .build());
2338 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2339 .deviceId(stylusDeviceId)
2340 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2341 .build());
2342 // Stylus hover movement is received normally
2343 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
2344 WithDeviceId(stylusDeviceId), WithCoords(100, 110)));
2345 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE),
2346 WithDeviceId(stylusDeviceId), WithCoords(101, 111)));
2347
2348 // Subsequent touch movements also work
2349 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2350 .deviceId(touchDeviceId)
2351 .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147))
2352 .build());
2353 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId),
2354 WithCoords(142, 147)));
2355
2356 window->assertNoEvents();
2357}
2358
2359/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002360 * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that
2361 * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should
2362 * become active.
2363 */
2364TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) {
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002365 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002366 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002367 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
2368 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002369 window->setFrame(Rect(0, 0, 200, 200));
2370
2371 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2372
2373 constexpr int32_t stylusDeviceId1 = 3;
2374 constexpr int32_t stylusDeviceId2 = 5;
2375
2376 // Touch down on window
2377 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2378 .deviceId(stylusDeviceId1)
2379 .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100))
2380 .build());
2381 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2382 .deviceId(stylusDeviceId1)
2383 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101))
2384 .build());
2385 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1)));
2386 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1)));
2387
2388 // Second stylus down
2389 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2390 .deviceId(stylusDeviceId2)
2391 .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10))
2392 .build());
2393 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2394 .deviceId(stylusDeviceId2)
2395 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11))
2396 .build());
2397
2398 // First stylus is canceled, second one takes over.
2399 window->consumeMotionEvent(
2400 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1)));
2401 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2)));
2402 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2)));
2403
2404 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2405 .deviceId(stylusDeviceId1)
2406 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
2407 .build());
2408 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002409 window->assertNoEvents();
2410}
2411
2412/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002413 * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that
2414 * both stylus devices can function simultaneously.
2415 */
2416TEST_F(InputDispatcherMultiDeviceTest, TwoStylusDevicesActiveAtTheSameTime) {
2417 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
2418 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002419 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
2420 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002421 window->setFrame(Rect(0, 0, 200, 200));
2422
2423 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2424
2425 constexpr int32_t stylusDeviceId1 = 3;
2426 constexpr int32_t stylusDeviceId2 = 5;
2427
2428 // Touch down on window
2429 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2430 .deviceId(stylusDeviceId1)
2431 .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100))
2432 .build());
2433 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2434 .deviceId(stylusDeviceId1)
2435 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101))
2436 .build());
2437 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1)));
2438 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1)));
2439
2440 // Second stylus down
2441 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2442 .deviceId(stylusDeviceId2)
2443 .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10))
2444 .build());
2445 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2446 .deviceId(stylusDeviceId2)
2447 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11))
2448 .build());
2449 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2)));
2450 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2)));
2451
2452 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2453 .deviceId(stylusDeviceId1)
2454 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
2455 .build());
2456 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1)));
2457 window->assertNoEvents();
2458}
2459
2460/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002461 * One window. Touch down on the window. Then, stylus down on the window from another device.
2462 * Ensure that is canceled, because stylus down should be preferred over touch.
2463 */
2464TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) {
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002465 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002466 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002467 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
2468 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002469 window->setFrame(Rect(0, 0, 200, 200));
2470
2471 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2472
2473 constexpr int32_t touchDeviceId = 4;
2474 constexpr int32_t stylusDeviceId = 2;
2475
2476 // Touch down on window
2477 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2478 .deviceId(touchDeviceId)
2479 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2480 .build());
2481 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2482 .deviceId(touchDeviceId)
2483 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2484 .build());
2485 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2486 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2487
2488 // Stylus down on the window
2489 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2490 .deviceId(stylusDeviceId)
2491 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2492 .build());
2493 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2494 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2495
2496 // Subsequent stylus movements are delivered correctly
2497 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2498 .deviceId(stylusDeviceId)
2499 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2500 .build());
2501 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2502 WithCoords(101, 111)));
2503}
2504
2505/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002506 * One window. Touch down on the window. Then, stylus down on the window from another device.
2507 * Ensure that both touch and stylus are functioning independently.
2508 */
2509TEST_F(InputDispatcherMultiDeviceTest, TouchIsNotCanceledByStylusDown) {
2510 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
2511 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002512 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
2513 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002514 window->setFrame(Rect(0, 0, 200, 200));
2515
2516 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2517
2518 constexpr int32_t touchDeviceId = 4;
2519 constexpr int32_t stylusDeviceId = 2;
2520
2521 // Touch down on window
2522 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2523 .deviceId(touchDeviceId)
2524 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2525 .build());
2526 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2527 .deviceId(touchDeviceId)
2528 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2529 .build());
2530 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2531 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2532
2533 // Stylus down on the window
2534 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2535 .deviceId(stylusDeviceId)
2536 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2537 .build());
2538 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2539
2540 // Subsequent stylus movements are delivered correctly
2541 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2542 .deviceId(stylusDeviceId)
2543 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2544 .build());
2545 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2546 WithCoords(101, 111)));
2547
2548 // Touch continues to work too
2549 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2550 .deviceId(touchDeviceId)
2551 .pointer(PointerBuilder(0, ToolType::FINGER).x(148).y(149))
2552 .build());
2553 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2554}
2555
2556/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002557 * Two windows: a window on the left and a window on the right.
2558 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2559 * down. Then, on the left window, also place second touch pointer down.
2560 * This test tries to reproduce a crash.
2561 * In the buggy implementation, second pointer down on the left window would cause a crash.
2562 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002563TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch_legacy) {
2564 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002565 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002566 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
2567 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002568 leftWindow->setFrame(Rect(0, 0, 200, 200));
2569
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002570 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
2571 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002572 rightWindow->setFrame(Rect(200, 0, 400, 200));
2573
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002574 mDispatcher->onWindowInfosChanged(
2575 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002576
2577 const int32_t touchDeviceId = 4;
2578 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002579
2580 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002581 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2582 .deviceId(mouseDeviceId)
2583 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2584 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002585 leftWindow->consumeMotionEvent(
2586 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2587
2588 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002589 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2590 .deviceId(mouseDeviceId)
2591 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2592 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2593 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002594
2595 leftWindow->consumeMotionEvent(
2596 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2597 leftWindow->consumeMotionEvent(
2598 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2599
Prabir Pradhan678438e2023-04-13 19:32:51 +00002600 mDispatcher->notifyMotion(
2601 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2602 .deviceId(mouseDeviceId)
2603 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2604 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2605 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2606 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002607 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2608
2609 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002610 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2611 .deviceId(touchDeviceId)
2612 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2613 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002614 leftWindow->assertNoEvents();
2615
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002616 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2617
2618 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002619 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2620 .deviceId(touchDeviceId)
2621 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2622 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2623 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002624 // Since this is now a new splittable pointer going down on the left window, and it's coming
2625 // from a different device, the current gesture in the left window (pointer down) should first
2626 // be canceled.
2627 leftWindow->consumeMotionEvent(
2628 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002629 leftWindow->consumeMotionEvent(
2630 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2631 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2632 // current implementation.
2633 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2634 rightWindow->consumeMotionEvent(
2635 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2636
2637 leftWindow->assertNoEvents();
2638 rightWindow->assertNoEvents();
2639}
2640
2641/**
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002642 * Two windows: a window on the left and a window on the right.
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002643 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2644 * down. Then, on the left window, also place second touch pointer down.
2645 * This test tries to reproduce a crash.
2646 * In the buggy implementation, second pointer down on the left window would cause a crash.
2647 */
2648TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) {
2649 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
2650 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002651 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
2652 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002653 leftWindow->setFrame(Rect(0, 0, 200, 200));
2654
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002655 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
2656 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002657 rightWindow->setFrame(Rect(200, 0, 400, 200));
2658
2659 mDispatcher->onWindowInfosChanged(
2660 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
2661
2662 const int32_t touchDeviceId = 4;
2663 const int32_t mouseDeviceId = 6;
2664
2665 // Start hovering over the left window
2666 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2667 .deviceId(mouseDeviceId)
2668 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2669 .build());
2670 leftWindow->consumeMotionEvent(
2671 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2672
2673 // Mouse down on left window
2674 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2675 .deviceId(mouseDeviceId)
2676 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2677 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2678 .build());
2679
2680 leftWindow->consumeMotionEvent(
2681 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2682 leftWindow->consumeMotionEvent(
2683 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2684
2685 mDispatcher->notifyMotion(
2686 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2687 .deviceId(mouseDeviceId)
2688 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2689 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2690 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2691 .build());
2692 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2693
2694 // First touch pointer down on right window
2695 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2696 .deviceId(touchDeviceId)
2697 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2698 .build());
2699 leftWindow->assertNoEvents();
2700
2701 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2702
2703 // Second touch pointer down on left window
2704 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2705 .deviceId(touchDeviceId)
2706 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2707 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2708 .build());
2709 // Since this is now a new splittable pointer going down on the left window, and it's coming
2710 // from a different device, it will be split and delivered to left window separately.
2711 leftWindow->consumeMotionEvent(
2712 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2713 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2714 // current implementation.
2715 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2716 rightWindow->consumeMotionEvent(
2717 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2718
2719 leftWindow->assertNoEvents();
2720 rightWindow->assertNoEvents();
2721}
2722
2723/**
2724 * Two windows: a window on the left and a window on the right.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002725 * Mouse is hovered on the left window and stylus is hovered on the right window.
2726 */
2727TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) {
2728 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002729 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
2730 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002731 leftWindow->setFrame(Rect(0, 0, 200, 200));
2732
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002733 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
2734 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002735 rightWindow->setFrame(Rect(200, 0, 400, 200));
2736
2737 mDispatcher->onWindowInfosChanged(
2738 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
2739
2740 const int32_t stylusDeviceId = 3;
2741 const int32_t mouseDeviceId = 6;
2742
2743 // Start hovering over the left window
2744 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2745 .deviceId(mouseDeviceId)
2746 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
2747 .build());
2748 leftWindow->consumeMotionEvent(
2749 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2750
2751 // Stylus hovered on right window
2752 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2753 .deviceId(stylusDeviceId)
2754 .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100))
2755 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002756 rightWindow->consumeMotionEvent(
2757 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
2758
2759 // Subsequent HOVER_MOVE events are dispatched correctly.
2760 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
2761 .deviceId(mouseDeviceId)
2762 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
2763 .build());
2764 leftWindow->consumeMotionEvent(
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002765 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002766
2767 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2768 .deviceId(stylusDeviceId)
2769 .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110))
2770 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002771 rightWindow->consumeMotionEvent(
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002772 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002773
2774 leftWindow->assertNoEvents();
2775 rightWindow->assertNoEvents();
2776}
2777
2778/**
2779 * Three windows: a window on the left and a window on the right.
2780 * And a spy window that's positioned above all of them.
2781 * Stylus down on the left window and remains down. Touch goes down on the right and remains down.
2782 * Check the stream that's received by the spy.
2783 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002784TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy_legacy) {
2785 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002786 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2787
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002788 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
2789 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002790 spyWindow->setFrame(Rect(0, 0, 400, 400));
2791 spyWindow->setTrustedOverlay(true);
2792 spyWindow->setSpy(true);
2793
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002794 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
2795 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002796 leftWindow->setFrame(Rect(0, 0, 200, 200));
2797
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002798 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
2799 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002800
2801 rightWindow->setFrame(Rect(200, 0, 400, 200));
2802
2803 mDispatcher->onWindowInfosChanged(
2804 {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
2805
2806 const int32_t stylusDeviceId = 1;
2807 const int32_t touchDeviceId = 2;
2808
2809 // Stylus down on the left window
2810 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2811 .deviceId(stylusDeviceId)
2812 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2813 .build());
2814 leftWindow->consumeMotionEvent(
2815 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2816 spyWindow->consumeMotionEvent(
2817 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2818
2819 // Touch down on the right window
2820 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2821 .deviceId(touchDeviceId)
2822 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2823 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002824 leftWindow->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002825 rightWindow->consumeMotionEvent(
2826 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002827
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002828 // Spy window does not receive touch events, because stylus events take precedence, and it
2829 // already has an active stylus gesture.
2830
2831 // Stylus movements continue. They should be delivered to the left window and to the spy window
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002832 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2833 .deviceId(stylusDeviceId)
2834 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2835 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002836 leftWindow->consumeMotionEvent(
2837 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
2838 spyWindow->consumeMotionEvent(
2839 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002840
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002841 // Further MOVE events keep going to the right window only
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002842 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2843 .deviceId(touchDeviceId)
2844 .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110))
2845 .build());
2846 rightWindow->consumeMotionEvent(
2847 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002848
2849 spyWindow->assertNoEvents();
2850 leftWindow->assertNoEvents();
2851 rightWindow->assertNoEvents();
2852}
2853
2854/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002855 * Three windows: a window on the left and a window on the right.
2856 * And a spy window that's positioned above all of them.
2857 * Stylus down on the left window and remains down. Touch goes down on the right and remains down.
2858 * Check the stream that's received by the spy.
2859 */
2860TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) {
2861 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
2862 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2863
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002864 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
2865 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002866 spyWindow->setFrame(Rect(0, 0, 400, 400));
2867 spyWindow->setTrustedOverlay(true);
2868 spyWindow->setSpy(true);
2869
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002870 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
2871 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002872 leftWindow->setFrame(Rect(0, 0, 200, 200));
2873
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002874 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
2875 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002876
2877 rightWindow->setFrame(Rect(200, 0, 400, 200));
2878
2879 mDispatcher->onWindowInfosChanged(
2880 {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
2881
2882 const int32_t stylusDeviceId = 1;
2883 const int32_t touchDeviceId = 2;
2884
2885 // Stylus down on the left window
2886 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2887 .deviceId(stylusDeviceId)
2888 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2889 .build());
2890 leftWindow->consumeMotionEvent(
2891 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2892 spyWindow->consumeMotionEvent(
2893 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2894
2895 // Touch down on the right window
2896 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2897 .deviceId(touchDeviceId)
2898 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2899 .build());
2900 leftWindow->assertNoEvents();
2901 rightWindow->consumeMotionEvent(
2902 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2903 spyWindow->consumeMotionEvent(
2904 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2905
2906 // Stylus movements continue. They should be delivered to the left window and to the spy window
2907 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2908 .deviceId(stylusDeviceId)
2909 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2910 .build());
2911 leftWindow->consumeMotionEvent(
2912 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
2913 spyWindow->consumeMotionEvent(
2914 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
2915
2916 // Further touch MOVE events keep going to the right window and to the spy
2917 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2918 .deviceId(touchDeviceId)
2919 .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110))
2920 .build());
2921 rightWindow->consumeMotionEvent(
2922 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2923 spyWindow->consumeMotionEvent(
2924 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2925
2926 spyWindow->assertNoEvents();
2927 leftWindow->assertNoEvents();
2928 rightWindow->assertNoEvents();
2929}
2930
2931/**
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002932 * Three windows: a window on the left, a window on the right, and a spy window positioned above
2933 * both.
2934 * Check hover in left window and touch down in the right window.
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002935 * At first, spy should receive hover. Spy shouldn't receive touch while stylus is hovering.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002936 * At the same time, left and right should be getting independent streams of hovering and touch,
2937 * respectively.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002938 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002939TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlocksTouchWithSpy) {
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07002940 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002941 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2942
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002943 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
2944 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002945 spyWindow->setFrame(Rect(0, 0, 400, 400));
2946 spyWindow->setTrustedOverlay(true);
2947 spyWindow->setSpy(true);
2948
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002949 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
2950 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002951 leftWindow->setFrame(Rect(0, 0, 200, 200));
2952
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002953 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
2954 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002955 rightWindow->setFrame(Rect(200, 0, 400, 200));
2956
2957 mDispatcher->onWindowInfosChanged(
2958 {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
2959
2960 const int32_t stylusDeviceId = 1;
2961 const int32_t touchDeviceId = 2;
2962
2963 // Stylus hover on the left window
2964 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2965 .deviceId(stylusDeviceId)
2966 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2967 .build());
2968 leftWindow->consumeMotionEvent(
2969 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
2970 spyWindow->consumeMotionEvent(
2971 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
2972
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002973 // Touch down on the right window. Spy doesn't receive this touch because it already has
2974 // stylus hovering there.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002975 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2976 .deviceId(touchDeviceId)
2977 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2978 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002979 leftWindow->assertNoEvents();
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002980 spyWindow->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002981 rightWindow->consumeMotionEvent(
2982 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2983
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002984 // Stylus movements continue. They should be delivered to the left window and the spy.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002985 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2986 .deviceId(stylusDeviceId)
2987 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2988 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002989 leftWindow->consumeMotionEvent(
2990 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002991 spyWindow->consumeMotionEvent(
2992 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002993
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002994 // Touch movements continue. They should be delivered to the right window only
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002995 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2996 .deviceId(touchDeviceId)
2997 .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101))
2998 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002999 rightWindow->consumeMotionEvent(
3000 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
3001
3002 spyWindow->assertNoEvents();
3003 leftWindow->assertNoEvents();
3004 rightWindow->assertNoEvents();
3005}
3006
3007/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003008 * Three windows: a window on the left, a window on the right, and a spy window positioned above
3009 * both.
3010 * Check hover in left window and touch down in the right window.
3011 * At first, spy should receive hover. Next, spy should receive touch.
3012 * At the same time, left and right should be getting independent streams of hovering and touch,
3013 * respectively.
3014 */
3015TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverDoesNotBlockTouchWithSpy) {
3016 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
3017 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3018
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003019 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
3020 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003021 spyWindow->setFrame(Rect(0, 0, 400, 400));
3022 spyWindow->setTrustedOverlay(true);
3023 spyWindow->setSpy(true);
3024
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003025 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
3026 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003027 leftWindow->setFrame(Rect(0, 0, 200, 200));
3028
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003029 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
3030 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003031 rightWindow->setFrame(Rect(200, 0, 400, 200));
3032
3033 mDispatcher->onWindowInfosChanged(
3034 {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
3035
3036 const int32_t stylusDeviceId = 1;
3037 const int32_t touchDeviceId = 2;
3038
3039 // Stylus hover on the left window
3040 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3041 .deviceId(stylusDeviceId)
3042 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
3043 .build());
3044 leftWindow->consumeMotionEvent(
3045 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3046 spyWindow->consumeMotionEvent(
3047 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3048
3049 // Touch down on the right window.
3050 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3051 .deviceId(touchDeviceId)
3052 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3053 .build());
3054 leftWindow->assertNoEvents();
3055 spyWindow->consumeMotionEvent(
3056 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3057 rightWindow->consumeMotionEvent(
3058 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3059
3060 // Stylus movements continue. They should be delivered to the left window and the spy.
3061 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
3062 .deviceId(stylusDeviceId)
3063 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
3064 .build());
3065 leftWindow->consumeMotionEvent(
3066 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
3067 spyWindow->consumeMotionEvent(
3068 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
3069
3070 // Touch movements continue. They should be delivered to the right window and the spy
3071 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3072 .deviceId(touchDeviceId)
3073 .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101))
3074 .build());
3075 rightWindow->consumeMotionEvent(
3076 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
3077 spyWindow->consumeMotionEvent(
3078 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
3079
3080 spyWindow->assertNoEvents();
3081 leftWindow->assertNoEvents();
3082 rightWindow->assertNoEvents();
3083}
3084
3085/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003086 * On a single window, use two different devices: mouse and touch.
3087 * Touch happens first, with two pointers going down, and then the first pointer leaving.
3088 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
3089 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
3090 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
3091 * represent a new gesture.
3092 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003093TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown_legacy) {
3094 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003095 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003096 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
3097 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003098 window->setFrame(Rect(0, 0, 400, 400));
3099
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003100 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003101
3102 const int32_t touchDeviceId = 4;
3103 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003104
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003105 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003106 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3107 .deviceId(touchDeviceId)
3108 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3109 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003110 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003111 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3112 .deviceId(touchDeviceId)
3113 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3114 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3115 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003116 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003117 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
3118 .deviceId(touchDeviceId)
3119 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3120 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3121 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003122 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3123 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3124 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
3125
3126 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00003127 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3128 .deviceId(mouseDeviceId)
3129 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3130 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
3131 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003132
3133 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08003134 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003135 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3136
Prabir Pradhan678438e2023-04-13 19:32:51 +00003137 mDispatcher->notifyMotion(
3138 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3139 .deviceId(mouseDeviceId)
3140 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3141 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3142 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
3143 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003144 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3145
3146 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003147 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3148 .deviceId(touchDeviceId)
3149 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3150 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3151 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003152 // Since we already canceled this touch gesture, it will be ignored until a completely new
3153 // gesture is started. This is easier to implement than trying to keep track of the new pointer
3154 // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN.
3155 // However, mouse movements should continue to work.
3156 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3157 .deviceId(mouseDeviceId)
3158 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3159 .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110))
3160 .build());
3161 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId)));
3162
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003163 window->assertNoEvents();
3164}
3165
3166/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003167 * On a single window, use two different devices: mouse and touch.
3168 * Touch happens first, with two pointers going down, and then the first pointer leaving.
3169 * Mouse is clicked next, which should not interfere with the touch stream.
3170 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is also
3171 * delivered correctly.
3172 */
3173TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) {
3174 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
3175 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003176 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
3177 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003178 window->setFrame(Rect(0, 0, 400, 400));
3179
3180 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
3181
3182 const int32_t touchDeviceId = 4;
3183 const int32_t mouseDeviceId = 6;
3184
3185 // First touch pointer down
3186 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3187 .deviceId(touchDeviceId)
3188 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3189 .build());
3190 // Second touch pointer down
3191 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3192 .deviceId(touchDeviceId)
3193 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3194 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3195 .build());
3196 // First touch pointer lifts. The second one remains down
3197 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
3198 .deviceId(touchDeviceId)
3199 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3200 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3201 .build());
3202 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3203 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3204 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
3205
3206 // Mouse down
3207 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3208 .deviceId(mouseDeviceId)
3209 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3210 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
3211 .build());
3212
3213 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3214
3215 mDispatcher->notifyMotion(
3216 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3217 .deviceId(mouseDeviceId)
3218 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3219 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3220 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
3221 .build());
3222 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3223
3224 // Second touch pointer down.
3225 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3226 .deviceId(touchDeviceId)
3227 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3228 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3229 .build());
3230 window->consumeMotionEvent(AllOf(WithMotionAction(POINTER_0_DOWN), WithDeviceId(touchDeviceId),
3231 WithPointerCount(2u)));
3232
3233 // Mouse movements should continue to work
3234 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3235 .deviceId(mouseDeviceId)
3236 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3237 .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110))
3238 .build());
3239 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId)));
3240
3241 window->assertNoEvents();
3242}
3243
3244/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003245 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
3246 * the injected event.
3247 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003248TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent_legacy) {
3249 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003250 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003251 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
3252 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003253 window->setFrame(Rect(0, 0, 400, 400));
3254
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003255 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003256
3257 const int32_t touchDeviceId = 4;
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003258 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
3259 // completion.
3260 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003261 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003262 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3263 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003264 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003265 .build()));
3266 window->consumeMotionEvent(
3267 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
3268
3269 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
3270 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003271 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3272 .deviceId(touchDeviceId)
3273 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3274 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003275
3276 window->consumeMotionEvent(
3277 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
3278 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3279}
3280
3281/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003282 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event runs
3283 * parallel to the injected event.
3284 */
3285TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) {
3286 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
3287 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003288 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
3289 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003290 window->setFrame(Rect(0, 0, 400, 400));
3291
3292 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
3293
3294 const int32_t touchDeviceId = 4;
3295 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
3296 // completion.
3297 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3298 injectMotionEvent(*mDispatcher,
3299 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3300 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
3301 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
3302 .build()));
3303 window->consumeMotionEvent(
3304 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
3305
3306 // Now a real touch comes. The injected pointer will remain, and the new gesture will also be
3307 // allowed through.
3308 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3309 .deviceId(touchDeviceId)
3310 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3311 .build());
3312 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3313}
3314
3315/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003316 * This test is similar to the test above, but the sequence of injected events is different.
3317 *
3318 * Two windows: a window on the left and a window on the right.
3319 * Mouse is hovered over the left window.
3320 * Next, we tap on the left window, where the cursor was last seen.
3321 *
3322 * After that, we inject one finger down onto the right window, and then a second finger down onto
3323 * the left window.
3324 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
3325 * window (first), and then another on the left window (second).
3326 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
3327 * In the buggy implementation, second finger down on the left window would cause a crash.
3328 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003329TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch_legacy) {
3330 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003331 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003332 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
3333 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003334 leftWindow->setFrame(Rect(0, 0, 200, 200));
3335
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003336 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
3337 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003338 rightWindow->setFrame(Rect(200, 0, 400, 200));
3339
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003340 mDispatcher->onWindowInfosChanged(
3341 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003342
3343 const int32_t mouseDeviceId = 6;
3344 const int32_t touchDeviceId = 4;
3345 // Hover over the left window. Keep the cursor there.
3346 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003347 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003348 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3349 AINPUT_SOURCE_MOUSE)
3350 .deviceId(mouseDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003351 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003352 .build()));
3353 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3354
3355 // Tap on left window
3356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003357 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003358 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3359 AINPUT_SOURCE_TOUCHSCREEN)
3360 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003361 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003362 .build()));
3363
3364 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003365 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003366 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3367 AINPUT_SOURCE_TOUCHSCREEN)
3368 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003369 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003370 .build()));
3371 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3372 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3373 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
3374
3375 // First finger down on right window
3376 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003377 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003378 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3379 AINPUT_SOURCE_TOUCHSCREEN)
3380 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003381 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003382 .build()));
3383 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3384
3385 // Second finger down on the left window
3386 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003387 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003388 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3389 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003390 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3391 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003392 .build()));
3393 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3394 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
3395
3396 // No more events
3397 leftWindow->assertNoEvents();
3398 rightWindow->assertNoEvents();
3399}
3400
3401/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003402 * This test is similar to the test above, but the sequence of injected events is different.
3403 *
3404 * Two windows: a window on the left and a window on the right.
3405 * Mouse is hovered over the left window.
3406 * Next, we tap on the left window, where the cursor was last seen.
3407 *
3408 * After that, we send one finger down onto the right window, and then a second finger down onto
3409 * the left window.
3410 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
3411 * window (first), and then another on the left window (second).
3412 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
3413 * In the buggy implementation, second finger down on the left window would cause a crash.
3414 */
3415TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) {
3416 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
3417 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003418 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
3419 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003420 leftWindow->setFrame(Rect(0, 0, 200, 200));
3421
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003422 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
3423 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003424 rightWindow->setFrame(Rect(200, 0, 400, 200));
3425
3426 mDispatcher->onWindowInfosChanged(
3427 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
3428
3429 const int32_t mouseDeviceId = 6;
3430 const int32_t touchDeviceId = 4;
3431 // Hover over the left window. Keep the cursor there.
3432 mDispatcher->notifyMotion(
3433 MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3434 .deviceId(mouseDeviceId)
3435 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
3436 .build());
3437 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3438
3439 // Tap on left window
3440 mDispatcher->notifyMotion(
3441 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3442 .deviceId(touchDeviceId)
3443 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3444 .build());
3445
3446 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
3447 .deviceId(touchDeviceId)
3448 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3449 .build());
3450 leftWindow->consumeMotionEvent(
3451 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithDeviceId(touchDeviceId)));
3452 leftWindow->consumeMotionEvent(
3453 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithDeviceId(touchDeviceId)));
3454
3455 // First finger down on right window
3456 mDispatcher->notifyMotion(
3457 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3458 .deviceId(touchDeviceId)
3459 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3460 .build());
3461 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3462
3463 // Second finger down on the left window
3464 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3465 .deviceId(touchDeviceId)
3466 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3467 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
3468 .build());
3469 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3470 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
3471
3472 // No more events
3473 leftWindow->assertNoEvents();
3474 rightWindow->assertNoEvents();
3475}
3476
3477/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003478 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
3479 * While the touch is down, new hover events from the stylus device should be ignored. After the
3480 * touch is gone, stylus hovering should start working again.
3481 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003482TEST_F(InputDispatcherMultiDeviceTest, StylusHoverIgnoresTouchTap) {
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003483 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003484 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003485 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
3486 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003487 window->setFrame(Rect(0, 0, 200, 200));
3488
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003489 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003490
3491 const int32_t stylusDeviceId = 5;
3492 const int32_t touchDeviceId = 4;
3493 // Start hovering with stylus
3494 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003495 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003496 MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003497 .deviceId(stylusDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003498 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003499 .build()));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003500 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003501
3502 // Finger down on the window
3503 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003504 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003505 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003506 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003507 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003508 .build()));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003509 // The touch device should be ignored!
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003510
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003511 // Continue hovering with stylus.
3512 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003513 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003514 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3515 AINPUT_SOURCE_STYLUS)
3516 .deviceId(stylusDeviceId)
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07003517 .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003518 .build()));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003519 // Hovers continue to work
3520 window->consumeMotionEvent(
3521 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003522
3523 // Lift up the finger
3524 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003525 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003526 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3527 AINPUT_SOURCE_TOUCHSCREEN)
3528 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003529 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003530 .build()));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003531
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003532 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003533 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003534 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3535 AINPUT_SOURCE_STYLUS)
3536 .deviceId(stylusDeviceId)
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07003537 .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003538 .build()));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003539 window->consumeMotionEvent(
3540 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003541 window->assertNoEvents();
3542}
3543
3544/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003545 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
3546 * While the touch is down, hovering from the stylus is not affected. After the touch is gone,
3547 * check that the stylus hovering continues to work.
3548 */
3549TEST_F(InputDispatcherMultiDeviceTest, StylusHoverWithTouchTap) {
3550 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
3551 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003552 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
3553 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003554 window->setFrame(Rect(0, 0, 200, 200));
3555
3556 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
3557
3558 const int32_t stylusDeviceId = 5;
3559 const int32_t touchDeviceId = 4;
3560 // Start hovering with stylus
3561 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3562 .deviceId(stylusDeviceId)
3563 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3564 .build());
3565 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3566
3567 // Finger down on the window
3568 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3569 .deviceId(touchDeviceId)
3570 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3571 .build());
3572 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3573
3574 // Continue hovering with stylus.
3575 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
3576 .deviceId(stylusDeviceId)
3577 .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60))
3578 .build());
3579 // Hovers continue to work
3580 window->consumeMotionEvent(
3581 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
3582
3583 // Lift up the finger
3584 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
3585 .deviceId(touchDeviceId)
3586 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3587 .build());
3588 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDeviceId(touchDeviceId)));
3589
3590 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
3591 .deviceId(stylusDeviceId)
3592 .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70))
3593 .build());
3594 window->consumeMotionEvent(
3595 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
3596 window->assertNoEvents();
3597}
3598
3599/**
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -07003600 * If stylus is down anywhere on the screen, then touches should not be delivered to windows that
3601 * have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH.
3602 *
3603 * Two windows: one on the left and one on the right.
3604 * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config.
3605 * Stylus down on the left window, and then touch down on the right window.
3606 * Check that the right window doesn't get touches while the stylus is down on the left window.
3607 */
3608TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusDownBlocksTouch) {
3609 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3610 sp<FakeWindowHandle> leftWindow =
3611 sp<FakeWindowHandle>::make(application, mDispatcher, "Left window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003612 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -07003613 leftWindow->setFrame(Rect(0, 0, 100, 100));
3614
3615 sp<FakeWindowHandle> sbtRightWindow =
3616 sp<FakeWindowHandle>::make(application, mDispatcher,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003617 "Stylus blocks touch (right) window",
3618 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -07003619 sbtRightWindow->setFrame(Rect(100, 100, 200, 200));
3620 sbtRightWindow->setGlobalStylusBlocksTouch(true);
3621
3622 mDispatcher->onWindowInfosChanged(
3623 {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0});
3624
3625 const int32_t stylusDeviceId = 5;
3626 const int32_t touchDeviceId = 4;
3627
3628 // Stylus down in the left window
3629 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
3630 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52))
3631 .deviceId(stylusDeviceId)
3632 .build());
3633 leftWindow->consumeMotionEvent(
3634 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
3635
3636 // Finger tap on the right window
3637 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3638 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3639 .deviceId(touchDeviceId)
3640 .build());
3641 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
3642 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3643 .deviceId(touchDeviceId)
3644 .build());
3645
3646 // The touch should be blocked, because stylus is down somewhere else on screen!
3647 sbtRightWindow->assertNoEvents();
3648
3649 // Continue stylus motion, and ensure it's not impacted.
3650 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
3651 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3652 .deviceId(stylusDeviceId)
3653 .build());
3654 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
3655 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3656 .deviceId(stylusDeviceId)
3657 .build());
3658 leftWindow->consumeMotionEvent(
3659 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
3660 leftWindow->consumeMotionEvent(
3661 AllOf(WithMotionAction(ACTION_UP), WithDeviceId(stylusDeviceId)));
3662
3663 // Now that the stylus gesture is done, touches should be getting delivered correctly.
3664 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3665 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153))
3666 .deviceId(touchDeviceId)
3667 .build());
3668 sbtRightWindow->consumeMotionEvent(
3669 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3670}
3671
3672/**
3673 * If stylus is hovering anywhere on the screen, then touches should not be delivered to windows
3674 * that have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH.
3675 *
3676 * Two windows: one on the left and one on the right.
3677 * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config.
3678 * Stylus hover on the left window, and then touch down on the right window.
3679 * Check that the right window doesn't get touches while the stylus is hovering on the left window.
3680 */
3681TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusHoverBlocksTouch) {
3682 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3683 sp<FakeWindowHandle> leftWindow =
3684 sp<FakeWindowHandle>::make(application, mDispatcher, "Left window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003685 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -07003686 leftWindow->setFrame(Rect(0, 0, 100, 100));
3687
3688 sp<FakeWindowHandle> sbtRightWindow =
3689 sp<FakeWindowHandle>::make(application, mDispatcher,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003690 "Stylus blocks touch (right) window",
3691 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -07003692 sbtRightWindow->setFrame(Rect(100, 100, 200, 200));
3693 sbtRightWindow->setGlobalStylusBlocksTouch(true);
3694
3695 mDispatcher->onWindowInfosChanged(
3696 {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0});
3697
3698 const int32_t stylusDeviceId = 5;
3699 const int32_t touchDeviceId = 4;
3700
3701 // Stylus hover in the left window
3702 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3703 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52))
3704 .deviceId(stylusDeviceId)
3705 .build());
3706 leftWindow->consumeMotionEvent(
3707 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3708
3709 // Finger tap on the right window
3710 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3711 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3712 .deviceId(touchDeviceId)
3713 .build());
3714 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
3715 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3716 .deviceId(touchDeviceId)
3717 .build());
3718
3719 // The touch should be blocked, because stylus is hovering somewhere else on screen!
3720 sbtRightWindow->assertNoEvents();
3721
3722 // Continue stylus motion, and ensure it's not impacted.
3723 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
3724 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3725 .deviceId(stylusDeviceId)
3726 .build());
3727 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3728 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3729 .deviceId(stylusDeviceId)
3730 .build());
3731 leftWindow->consumeMotionEvent(
3732 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
3733 leftWindow->consumeMotionEvent(
3734 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId)));
3735
3736 // Now that the stylus gesture is done, touches should be getting delivered correctly.
3737 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3738 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153))
3739 .deviceId(touchDeviceId)
3740 .build());
3741 sbtRightWindow->consumeMotionEvent(
3742 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3743}
3744
3745/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003746 * A spy window above a window with no input channel.
3747 * Start hovering with a stylus device, and then tap with it.
3748 * Ensure spy window receives the entire sequence.
3749 */
3750TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
3751 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003752 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
3753 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003754 spyWindow->setFrame(Rect(0, 0, 200, 200));
3755 spyWindow->setTrustedOverlay(true);
3756 spyWindow->setSpy(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003757 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
3758 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003759 window->setNoInputChannel(true);
3760 window->setFrame(Rect(0, 0, 200, 200));
3761
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003762 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003763
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003764 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00003765 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3766 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3767 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003768 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3769 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00003770 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3771 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3772 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003773 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3774
3775 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003776 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
3777 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3778 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003779 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3780
3781 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00003782 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
3783 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3784 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003785 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
3786
3787 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00003788 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3789 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3790 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003791 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3792 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00003793 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3794 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3795 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003796 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3797
3798 // No more events
3799 spyWindow->assertNoEvents();
3800 window->assertNoEvents();
3801}
3802
3803/**
Siarhei Vishniakou6b71b632023-10-27 21:34:46 -07003804 * A stale stylus HOVER_EXIT event is injected. Since it's a stale event, it should generally be
3805 * rejected. But since we already have an ongoing gesture, this event should be processed.
3806 * This prevents inconsistent events being handled inside the dispatcher.
3807 */
3808TEST_F(InputDispatcherTest, StaleStylusHoverGestureIsComplete) {
3809 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3810
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003811 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
3812 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou6b71b632023-10-27 21:34:46 -07003813 window->setFrame(Rect(0, 0, 200, 200));
3814
3815 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
3816
3817 // Start hovering with stylus
3818 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3819 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3820 .build());
3821 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3822
3823 NotifyMotionArgs hoverExit = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3824 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3825 .build();
3826 // Make this 'hoverExit' event stale
3827 mFakePolicy->setStaleEventTimeout(100ms);
3828 std::this_thread::sleep_for(100ms);
3829
3830 // It shouldn't be dropped by the dispatcher, even though it's stale.
3831 mDispatcher->notifyMotion(hoverExit);
3832 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3833
3834 // Stylus starts hovering again! There should be no crash.
3835 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3836 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(51))
3837 .build());
3838 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3839}
3840
3841/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003842 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
3843 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
3844 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
3845 * While the mouse is down, new move events from the touch device should be ignored.
3846 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003847TEST_F(InputDispatcherTest, TouchPilferAndMouseMove_legacy) {
3848 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003849 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003850 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
3851 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003852 spyWindow->setFrame(Rect(0, 0, 200, 200));
3853 spyWindow->setTrustedOverlay(true);
3854 spyWindow->setSpy(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003855 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
3856 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003857 window->setFrame(Rect(0, 0, 200, 200));
3858
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003859 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003860
3861 const int32_t mouseDeviceId = 7;
3862 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003863
3864 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00003865 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3866 .deviceId(mouseDeviceId)
3867 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3868 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003869 spyWindow->consumeMotionEvent(
3870 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3871 window->consumeMotionEvent(
3872 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3873
3874 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00003875 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3876 .deviceId(touchDeviceId)
3877 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3878 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003879 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3880 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3881 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3882 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3883
Prabir Pradhan678438e2023-04-13 19:32:51 +00003884 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3885 .deviceId(touchDeviceId)
3886 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
3887 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003888 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3889 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3890
3891 // Pilfer the stream
3892 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3893 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
3894
Prabir Pradhan678438e2023-04-13 19:32:51 +00003895 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3896 .deviceId(touchDeviceId)
3897 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
3898 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003899 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3900
3901 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003902 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3903 .deviceId(mouseDeviceId)
3904 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3905 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3906 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003907
3908 spyWindow->consumeMotionEvent(
3909 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
3910 spyWindow->consumeMotionEvent(
3911 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3912 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3913
Prabir Pradhan678438e2023-04-13 19:32:51 +00003914 mDispatcher->notifyMotion(
3915 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3916 .deviceId(mouseDeviceId)
3917 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3918 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3919 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3920 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003921 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3922 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3923
3924 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003925 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3926 .deviceId(mouseDeviceId)
3927 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3928 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
3929 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003930 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3931 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3932
3933 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003934 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3935 .deviceId(touchDeviceId)
3936 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
3937 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003938
3939 // No more events
3940 spyWindow->assertNoEvents();
3941 window->assertNoEvents();
3942}
3943
3944/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003945 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
3946 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
3947 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
3948 * While the mouse is down, new move events from the touch device should continue to work.
3949 */
3950TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
3951 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
3952 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003953 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
3954 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003955 spyWindow->setFrame(Rect(0, 0, 200, 200));
3956 spyWindow->setTrustedOverlay(true);
3957 spyWindow->setSpy(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003958 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
3959 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07003960 window->setFrame(Rect(0, 0, 200, 200));
3961
3962 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
3963
3964 const int32_t mouseDeviceId = 7;
3965 const int32_t touchDeviceId = 4;
3966
3967 // Hover a bit with mouse first
3968 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3969 .deviceId(mouseDeviceId)
3970 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3971 .build());
3972 spyWindow->consumeMotionEvent(
3973 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3974 window->consumeMotionEvent(
3975 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3976
3977 // Start touching
3978 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3979 .deviceId(touchDeviceId)
3980 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3981 .build());
3982
3983 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3984 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3985
3986 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3987 .deviceId(touchDeviceId)
3988 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
3989 .build());
3990 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3991 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3992
3993 // Pilfer the stream
3994 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3995 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
3996 // Hover is not pilfered! Only touch.
3997
3998 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3999 .deviceId(touchDeviceId)
4000 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
4001 .build());
4002 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
4003
4004 // Mouse down
4005 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4006 .deviceId(mouseDeviceId)
4007 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4008 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
4009 .build());
4010
4011 spyWindow->consumeMotionEvent(
4012 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
4013 spyWindow->consumeMotionEvent(
4014 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
4015 window->consumeMotionEvent(
4016 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
4017 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
4018
4019 mDispatcher->notifyMotion(
4020 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
4021 .deviceId(mouseDeviceId)
4022 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4023 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
4024 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
4025 .build());
4026 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
4027 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
4028
4029 // Mouse move!
4030 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4031 .deviceId(mouseDeviceId)
4032 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4033 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
4034 .build());
4035 spyWindow->consumeMotionEvent(
4036 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId)));
4037 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId)));
4038
4039 // Touch move!
4040 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
4041 .deviceId(touchDeviceId)
4042 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
4043 .build());
4044 spyWindow->consumeMotionEvent(
4045 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
4046
4047 // No more events
4048 spyWindow->assertNoEvents();
4049 window->assertNoEvents();
4050}
4051
4052/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004053 * On the display, have a single window, and also an area where there's no window.
4054 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
4055 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
4056 */
4057TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
4058 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4059 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004060 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004061
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004062 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004063
4064 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00004065 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004066
4067 mDispatcher->waitForIdle();
4068 window->assertNoEvents();
4069
4070 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00004071 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004072 mDispatcher->waitForIdle();
4073 window->consumeMotionDown();
4074}
4075
4076/**
4077 * Same test as above, but instead of touching the empty space, the first touch goes to
4078 * non-touchable window.
4079 */
4080TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
4081 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4082 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004083 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004084 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
4085 window1->setTouchable(false);
4086 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004087 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004088 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
4089
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004090 mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004091
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004092 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004093 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004094
4095 mDispatcher->waitForIdle();
4096 window1->assertNoEvents();
4097 window2->assertNoEvents();
4098
4099 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00004100 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004101 mDispatcher->waitForIdle();
4102 window2->consumeMotionDown();
4103}
4104
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004105/**
4106 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
4107 * to the event time of the first ACTION_DOWN sent to the particular window.
4108 */
4109TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
4110 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4111 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004112 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004113 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
4114 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004115 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004116 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
4117
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004118 mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0});
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004119
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004120 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004121 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004122 mDispatcher->waitForIdle();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004123
Prabir Pradhan9d5f9ce2024-01-24 00:03:41 +00004124 const std::unique_ptr<MotionEvent> firstDown =
4125 window1->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN)));
4126 ASSERT_EQ(firstDown->getDownTime(), firstDown->getEventTime());
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004127 window2->assertNoEvents();
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004128
4129 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00004130 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004131 mDispatcher->waitForIdle();
Prabir Pradhan9d5f9ce2024-01-24 00:03:41 +00004132
4133 const std::unique_ptr<MotionEvent> secondDown =
4134 window2->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN)));
4135 ASSERT_EQ(secondDown->getDownTime(), secondDown->getEventTime());
4136 ASSERT_NE(firstDown->getDownTime(), secondDown->getDownTime());
4137 // We currently send MOVE events to all windows receiving a split touch when there is any change
4138 // in the touch state, even when none of the pointers in the split window actually moved.
4139 // Document this behavior in the test.
4140 window1->consumeMotionMove();
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004141
4142 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004143 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004144 mDispatcher->waitForIdle();
Prabir Pradhan9d5f9ce2024-01-24 00:03:41 +00004145
4146 window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime()));
4147 window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime()));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004148
4149 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004150 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004151 mDispatcher->waitForIdle();
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004152
Prabir Pradhan9d5f9ce2024-01-24 00:03:41 +00004153 window2->consumeMotionEvent(
4154 AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(secondDown->getDownTime())));
4155 window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime()));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004156
4157 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004158 mDispatcher->notifyMotion(
4159 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004160 mDispatcher->waitForIdle();
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004161
Prabir Pradhan9d5f9ce2024-01-24 00:03:41 +00004162 window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime()));
4163 window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime()));
4164
4165 // Now add new touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004166 mDispatcher->notifyMotion(
4167 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004168 mDispatcher->waitForIdle();
Prabir Pradhan9d5f9ce2024-01-24 00:03:41 +00004169
4170 window1->consumeMotionEvent(
4171 AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(firstDown->getDownTime())));
4172 window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime()));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004173}
4174
Siarhei Vishniakouac42d242024-06-28 10:43:53 -07004175/**
4176 * When events are not split, the downTime should be adjusted such that the downTime corresponds
4177 * to the event time of the first ACTION_DOWN. If a new window appears, it should not affect
4178 * the event routing because the first window prevents splitting.
4179 */
4180TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTimeForNewWindow) {
4181 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4182 sp<FakeWindowHandle> window1 =
4183 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
4184 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
4185 window1->setPreventSplitting(true);
4186
4187 sp<FakeWindowHandle> window2 =
4188 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
4189 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
4190
4191 mDispatcher->onWindowInfosChanged({{*window1->getInfo()}, {}, 0, 0});
4192
4193 // Touch down on the first window
4194 NotifyMotionArgs downArgs = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4195 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4196 .build();
4197 mDispatcher->notifyMotion(downArgs);
4198
4199 window1->consumeMotionEvent(
4200 AllOf(WithMotionAction(ACTION_DOWN), WithDownTime(downArgs.downTime)));
4201
4202 // Second window is added
4203 mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0});
4204
4205 // Now touch down on the window with another pointer
4206 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4207 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4208 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
4209 .downTime(downArgs.downTime)
4210 .build());
4211 window1->consumeMotionPointerDown(1, AllOf(WithDownTime(downArgs.downTime)));
4212
4213 // Finish the gesture
4214 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
4215 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4216 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
4217 .downTime(downArgs.downTime)
4218 .build());
4219 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4220 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4221 .downTime(downArgs.downTime)
4222 .build());
4223 window1->consumeMotionPointerUp(1, AllOf(WithDownTime(downArgs.downTime)));
4224 window1->consumeMotionEvent(
4225 AllOf(WithMotionAction(ACTION_UP), WithDownTime(downArgs.downTime)));
4226 window2->assertNoEvents();
4227}
4228
4229/**
4230 * When splitting touch events, the downTime should be adjusted such that the downTime corresponds
4231 * to the event time of the first ACTION_DOWN sent to the new window.
4232 * If a new window that does not support split appears on the screen and gets touched with the
4233 * second finger, it should not get any events because it doesn't want split touches. At the same
4234 * time, the first window should not get the pointer_down event because it supports split touches
4235 * (and the touch occurred outside of the bounds of window1).
4236 */
4237TEST_F(InputDispatcherTest, SplitTouchesDropsEventForNonSplittableSecondWindow) {
4238 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4239 sp<FakeWindowHandle> window1 =
4240 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
4241 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
4242
4243 sp<FakeWindowHandle> window2 =
4244 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
4245 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
4246
4247 mDispatcher->onWindowInfosChanged({{*window1->getInfo()}, {}, 0, 0});
4248
4249 // Touch down on the first window
4250 NotifyMotionArgs downArgs = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4251 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4252 .build();
4253 mDispatcher->notifyMotion(downArgs);
4254
4255 window1->consumeMotionEvent(
4256 AllOf(WithMotionAction(ACTION_DOWN), WithDownTime(downArgs.downTime)));
4257
4258 // Second window is added
4259 window2->setPreventSplitting(true);
4260 mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0});
4261
4262 // Now touch down on the window with another pointer
4263 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4264 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4265 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
4266 .downTime(downArgs.downTime)
4267 .build());
4268 // Event is dropped because window2 doesn't support split touch, and window1 does.
4269
4270 // Complete the gesture
4271 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
4272 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4273 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
4274 .downTime(downArgs.downTime)
4275 .build());
4276 // A redundant MOVE event is generated that doesn't carry any new information
4277 window1->consumeMotionEvent(
4278 AllOf(WithMotionAction(ACTION_MOVE), WithDownTime(downArgs.downTime)));
4279 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4280 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4281 .downTime(downArgs.downTime)
4282 .build());
4283
4284 window1->consumeMotionEvent(
4285 AllOf(WithMotionAction(ACTION_UP), WithDownTime(downArgs.downTime)));
4286 window1->assertNoEvents();
4287 window2->assertNoEvents();
4288}
4289
Garfield Tandf26e862020-07-01 20:18:19 -07004290TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07004291 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004292 sp<FakeWindowHandle> windowLeft = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
4293 ui::LogicalDisplayId::DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07004294 windowLeft->setFrame(Rect(0, 0, 600, 800));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004295 sp<FakeWindowHandle> windowRight = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
4296 ui::LogicalDisplayId::DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07004297 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07004298
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004299 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Garfield Tandf26e862020-07-01 20:18:19 -07004300
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004301 mDispatcher->onWindowInfosChanged(
4302 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07004303
4304 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004305 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004306 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004307 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4308 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004309 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004310 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004311 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07004312
4313 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004314 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004315 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004316 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4317 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004318 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004319 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004320 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
4321 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07004322
4323 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004324 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004325 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004326 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4327 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004328 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004329 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004330 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
4331 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07004332
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004333 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004334 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004335 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
4336 AINPUT_SOURCE_MOUSE)
4337 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4338 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004339 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004340 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004341 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07004342
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004343 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004344 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004345 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
4346 AINPUT_SOURCE_MOUSE)
4347 .buttonState(0)
4348 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004349 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004350 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004351 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07004352
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004353 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004354 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004355 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
4356 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004357 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004358 .build()));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004359 windowLeft->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07004360
4361 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004362 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004363 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004364 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4365 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004366 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004367 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004368 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004369
4370 // No more events
4371 windowLeft->assertNoEvents();
4372 windowRight->assertNoEvents();
4373}
4374
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004375/**
4376 * Put two fingers down (and don't release them) and click the mouse button.
4377 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
4378 * currently active gesture should be canceled, and the new one should proceed.
4379 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004380TEST_F(InputDispatcherTest, TwoPointersDownMouseClick_legacy) {
4381 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004382 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004383 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4384 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004385 window->setFrame(Rect(0, 0, 600, 800));
4386
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004387 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004388
4389 const int32_t touchDeviceId = 4;
4390 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004391
4392 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00004393 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4394 .deviceId(touchDeviceId)
4395 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4396 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004397
Prabir Pradhan678438e2023-04-13 19:32:51 +00004398 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4399 .deviceId(touchDeviceId)
4400 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4401 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
4402 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004403 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4404 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4405
4406 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00004407 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4408 .deviceId(mouseDeviceId)
4409 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4410 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4411 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004412 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
4413 WithPointerCount(2u)));
4414 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
4415
Prabir Pradhan678438e2023-04-13 19:32:51 +00004416 mDispatcher->notifyMotion(
4417 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
4418 .deviceId(mouseDeviceId)
4419 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4420 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
4421 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4422 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004423 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
4424
4425 // Try to send more touch events while the mouse is down. Since it's a continuation of an
4426 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004427 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
4428 .deviceId(touchDeviceId)
4429 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
4430 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
4431 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004432 window->assertNoEvents();
4433}
4434
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004435/**
4436 * Put two fingers down (and don't release them) and click the mouse button.
4437 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
4438 * currently active gesture should not be canceled, and the new one should proceed in parallel.
4439 */
4440TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
4441 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
4442 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004443 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4444 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004445 window->setFrame(Rect(0, 0, 600, 800));
4446
4447 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
4448
4449 const int32_t touchDeviceId = 4;
4450 const int32_t mouseDeviceId = 6;
4451
4452 // Two pointers down
4453 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4454 .deviceId(touchDeviceId)
4455 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4456 .build());
4457
4458 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4459 .deviceId(touchDeviceId)
4460 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4461 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
4462 .build());
4463 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4464 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4465
4466 // Send a series of mouse events for a mouse click
4467 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4468 .deviceId(mouseDeviceId)
4469 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4470 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4471 .build());
4472 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
4473
4474 mDispatcher->notifyMotion(
4475 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
4476 .deviceId(mouseDeviceId)
4477 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4478 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
4479 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4480 .build());
4481 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
4482
4483 // Try to send more touch events while the mouse is down. Since it's a continuation of an
4484 // already active gesture, it should be sent normally.
4485 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
4486 .deviceId(touchDeviceId)
4487 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
4488 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
4489 .build());
4490 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
4491 window->assertNoEvents();
4492}
4493
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004494TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
4495 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4496
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004497 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
4498 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004499 spyWindow->setFrame(Rect(0, 0, 600, 800));
4500 spyWindow->setTrustedOverlay(true);
4501 spyWindow->setSpy(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004502 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4503 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004504 window->setFrame(Rect(0, 0, 600, 800));
4505
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004506 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004507 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004508
4509 // Send mouse cursor to the window
4510 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004511 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004512 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4513 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004514 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004515 .build()));
4516
4517 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4518 WithSource(AINPUT_SOURCE_MOUSE)));
4519 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4520 WithSource(AINPUT_SOURCE_MOUSE)));
4521
4522 window->assertNoEvents();
4523 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07004524}
4525
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004526TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows_legacy) {
4527 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004528 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4529
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004530 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
4531 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004532 spyWindow->setFrame(Rect(0, 0, 600, 800));
4533 spyWindow->setTrustedOverlay(true);
4534 spyWindow->setSpy(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004535 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4536 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004537 window->setFrame(Rect(0, 0, 600, 800));
4538
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004539 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004540 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004541
4542 // Send mouse cursor to the window
4543 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004544 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004545 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4546 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004547 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004548 .build()));
4549
4550 // Move mouse cursor
4551 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004552 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004553 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4554 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004555 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004556 .build()));
4557
4558 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4559 WithSource(AINPUT_SOURCE_MOUSE)));
4560 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4561 WithSource(AINPUT_SOURCE_MOUSE)));
4562 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4563 WithSource(AINPUT_SOURCE_MOUSE)));
4564 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4565 WithSource(AINPUT_SOURCE_MOUSE)));
4566 // Touch down on the window
4567 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004568 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004569 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
4570 AINPUT_SOURCE_TOUCHSCREEN)
4571 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004572 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004573 .build()));
4574 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4575 WithSource(AINPUT_SOURCE_MOUSE)));
4576 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4577 WithSource(AINPUT_SOURCE_MOUSE)));
4578 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4579 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4580 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4581 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4582
4583 // pilfer the motion, retaining the gesture on the spy window.
4584 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
4585 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4586 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4587
4588 // Touch UP on the window
4589 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004590 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004591 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
4592 AINPUT_SOURCE_TOUCHSCREEN)
4593 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004594 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004595 .build()));
4596 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4597 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4598
4599 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
4600 // to send a new gesture. It should again go to both windows (spy and the window below), just
4601 // like the first gesture did, before pilfering. The window configuration has not changed.
4602
4603 // One more tap - DOWN
4604 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004605 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004606 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
4607 AINPUT_SOURCE_TOUCHSCREEN)
4608 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004609 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004610 .build()));
4611 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4612 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4613 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4614 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4615
4616 // Touch UP on the window
4617 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004618 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004619 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
4620 AINPUT_SOURCE_TOUCHSCREEN)
4621 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004622 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004623 .build()));
4624 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4625 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4626 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4627 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4628
4629 window->assertNoEvents();
4630 spyWindow->assertNoEvents();
4631}
4632
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004633TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
4634 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
4635 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4636
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004637 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
4638 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004639 spyWindow->setFrame(Rect(0, 0, 600, 800));
4640 spyWindow->setTrustedOverlay(true);
4641 spyWindow->setSpy(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004642 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4643 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004644 window->setFrame(Rect(0, 0, 600, 800));
4645
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004646 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004647 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
4648
4649 // Send mouse cursor to the window
4650 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4651 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
4652 .build());
4653
4654 // Move mouse cursor
4655 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
4656 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
4657 .build());
4658
4659 window->consumeMotionEvent(
4660 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithSource(AINPUT_SOURCE_MOUSE)));
4661 spyWindow->consumeMotionEvent(
4662 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithSource(AINPUT_SOURCE_MOUSE)));
4663 window->consumeMotionEvent(
4664 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
4665 spyWindow->consumeMotionEvent(
4666 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
4667 // Touch down on the window
4668 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4669 .deviceId(SECOND_DEVICE_ID)
4670 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
4671 .build());
4672 window->consumeMotionEvent(
4673 AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4674 spyWindow->consumeMotionEvent(
4675 AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4676
4677 // pilfer the motion, retaining the gesture on the spy window.
4678 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
4679 window->consumeMotionEvent(
4680 AllOf(WithMotionAction(ACTION_CANCEL), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4681 // Mouse hover is not pilfered
4682
4683 // Touch UP on the window
4684 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4685 .deviceId(SECOND_DEVICE_ID)
4686 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
4687 .build());
4688 spyWindow->consumeMotionEvent(
4689 AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4690
4691 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
4692 // to send a new gesture. It should again go to both windows (spy and the window below), just
4693 // like the first gesture did, before pilfering. The window configuration has not changed.
4694
4695 // One more tap - DOWN
4696 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4697 .deviceId(SECOND_DEVICE_ID)
4698 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
4699 .build());
4700 window->consumeMotionEvent(
4701 AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4702 spyWindow->consumeMotionEvent(
4703 AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4704
4705 // Touch UP on the window
4706 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4707 .deviceId(SECOND_DEVICE_ID)
4708 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
4709 .build());
4710 window->consumeMotionEvent(
4711 AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4712 spyWindow->consumeMotionEvent(
4713 AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4714
4715 // Mouse movement continues normally as well
4716 // Move mouse cursor
4717 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
4718 .pointer(PointerBuilder(0, ToolType::MOUSE).x(120).y(130))
4719 .build());
4720 window->consumeMotionEvent(
4721 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
4722 spyWindow->consumeMotionEvent(
4723 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
4724
4725 window->assertNoEvents();
4726 spyWindow->assertNoEvents();
4727}
4728
Garfield Tandf26e862020-07-01 20:18:19 -07004729// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
4730// directly in this test.
4731TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07004732 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004733 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4734 ui::LogicalDisplayId::DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07004735 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07004736
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004737 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Garfield Tandf26e862020-07-01 20:18:19 -07004738
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004739 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07004740
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004741 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004742 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004743 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4744 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004745 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004746 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004747 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07004748 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004749 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004750 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004751 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4752 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004753 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004754 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004755 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
4756 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07004757
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004758 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004759 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004760 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
4761 AINPUT_SOURCE_MOUSE)
4762 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4763 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004764 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004765 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004766 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07004767
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004768 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004769 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004770 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
4771 AINPUT_SOURCE_MOUSE)
4772 .buttonState(0)
4773 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004774 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004775 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004776 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07004777
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004778 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004779 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004780 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
4781 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004782 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004783 .build()));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004784 window->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07004785
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07004786 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
4787 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
4788 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004789 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004790 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
4791 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004792 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004793 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004794 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07004795}
4796
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004797/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004798 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
4799 * is generated.
4800 */
4801TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
4802 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004803 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4804 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004805 window->setFrame(Rect(0, 0, 1200, 800));
4806
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004807 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004808
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004809 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004810
4811 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004812 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004813 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4814 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004815 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004816 .build()));
4817 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4818
4819 // Remove the window, but keep the channel.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004820 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004821 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
4822}
4823
4824/**
Daniel Norman7487dfa2023-08-02 16:39:45 -07004825 * Test that invalid HOVER events sent by accessibility do not cause a fatal crash.
4826 */
Ameer Armalycff4fa52023-10-04 23:45:11 +00004827TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash,
4828 REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags,
4829 a11y_crash_on_inconsistent_event_stream))) {
Daniel Norman7487dfa2023-08-02 16:39:45 -07004830 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004831 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4832 ui::LogicalDisplayId::DEFAULT);
Daniel Norman7487dfa2023-08-02 16:39:45 -07004833 window->setFrame(Rect(0, 0, 1200, 800));
4834
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004835 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Daniel Norman7487dfa2023-08-02 16:39:45 -07004836
4837 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
4838
4839 MotionEventBuilder hoverEnterBuilder =
4840 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4841 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4842 .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
4843 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4844 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
4845 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4846 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
4847 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4848 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4849}
4850
4851/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004852 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
4853 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004854TEST_F(InputDispatcherTest, TouchDownAfterMouseHover_legacy) {
4855 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004856 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004857 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4858 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004859 window->setFrame(Rect(0, 0, 100, 100));
4860
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004861 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004862
4863 const int32_t mouseDeviceId = 7;
4864 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004865
4866 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00004867 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4868 .deviceId(mouseDeviceId)
4869 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
4870 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004871 window->consumeMotionEvent(
4872 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
4873
4874 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00004875 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4876 .deviceId(touchDeviceId)
4877 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4878 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004879
4880 window->consumeMotionEvent(
4881 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
4882 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
4883}
4884
4885/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004886 * If mouse is hovering when the touch goes down, the hovering should not be stopped.
4887 */
4888TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
4889 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
4890 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004891 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4892 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004893 window->setFrame(Rect(0, 0, 100, 100));
4894
4895 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
4896
4897 const int32_t mouseDeviceId = 7;
4898 const int32_t touchDeviceId = 4;
4899
4900 // Start hovering with the mouse
4901 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4902 .deviceId(mouseDeviceId)
4903 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
4904 .build());
4905 window->consumeMotionEvent(
4906 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
4907
4908 // Touch goes down
4909 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4910 .deviceId(touchDeviceId)
4911 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4912 .build());
4913 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
4914}
4915
4916/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004917 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004918 * The tap causes a HOVER_EXIT event to be generated because the current event
4919 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004920 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004921TEST_F(InputDispatcherTest, MouseHoverAndTouchTap_legacy) {
4922 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004923 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004924 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4925 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004926 window->setFrame(Rect(0, 0, 100, 100));
4927
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004928 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004929 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
4930 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
4931 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004932 ASSERT_NO_FATAL_FAILURE(
4933 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4934 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004935
4936 // Tap on the window
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004937 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4938 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4939 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004940 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004941 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4942 WithSource(AINPUT_SOURCE_MOUSE))));
4943
4944 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004945 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4946 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
4947
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004948 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4949 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4950 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004951 ASSERT_NO_FATAL_FAILURE(
4952 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4953 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
4954}
4955
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004956/**
4957 * Send a mouse hover event followed by a tap from touchscreen.
4958 * The tap causes a HOVER_EXIT event to be generated because the current event
4959 * stream's source has been switched.
4960 */
4961TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
4962 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
4963 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004964 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4965 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004966 window->setFrame(Rect(0, 0, 100, 100));
4967
4968 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
4969 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
4970 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
4971 .build());
4972
4973 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4974 WithSource(AINPUT_SOURCE_MOUSE)));
4975
4976 // Tap on the window
4977 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4978 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4979 .build());
4980
4981 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4982 WithSource(AINPUT_SOURCE_MOUSE)));
4983
4984 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4985 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4986
4987 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4988 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4989 .build());
4990
4991 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4992 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4993}
4994
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004995TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
4996 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4997 sp<FakeWindowHandle> windowDefaultDisplay =
4998 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004999 ui::LogicalDisplayId::DEFAULT);
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02005000 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
5001 sp<FakeWindowHandle> windowSecondDisplay =
5002 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
5003 SECOND_DISPLAY_ID);
5004 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
5005
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005006 mDispatcher->onWindowInfosChanged(
5007 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02005008
5009 // Set cursor position in window in default display and check that hover enter and move
5010 // events are generated.
5011 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005012 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02005013 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
5014 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005015 .displayId(ui::LogicalDisplayId::DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005016 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02005017 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08005018 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02005019
5020 // Remove all windows in secondary display and check that no event happens on window in
5021 // primary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005022 mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0});
5023
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02005024 windowDefaultDisplay->assertNoEvents();
5025
5026 // Move cursor position in window in default display and check that only hover move
5027 // event is generated and not hover enter event.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005028 mDispatcher->onWindowInfosChanged(
5029 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02005030 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005031 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02005032 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
5033 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005034 .displayId(ui::LogicalDisplayId::DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005035 .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02005036 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08005037 windowDefaultDisplay->consumeMotionEvent(
5038 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5039 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02005040 windowDefaultDisplay->assertNoEvents();
5041}
5042
Garfield Tan00f511d2019-06-12 16:55:40 -07005043TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07005044 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07005045
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005046 sp<FakeWindowHandle> windowLeft = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
5047 ui::LogicalDisplayId::DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07005048 windowLeft->setFrame(Rect(0, 0, 600, 800));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005049 sp<FakeWindowHandle> windowRight = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
5050 ui::LogicalDisplayId::DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07005051 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07005052
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005053 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Garfield Tan00f511d2019-06-12 16:55:40 -07005054
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005055 mDispatcher->onWindowInfosChanged(
5056 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tan00f511d2019-06-12 16:55:40 -07005057
5058 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
5059 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005060 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005061 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005062 ui::LogicalDisplayId::DEFAULT, {610, 400}, {599, 400}));
5063 windowLeft->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07005064 windowRight->assertNoEvents();
5065}
5066
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005067TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07005068 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005069 sp<FakeWindowHandle> window =
5070 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
5071 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07005072 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005073
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005074 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005075 setFocusedWindow(window);
5076
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005077 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005078
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005079 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005080
5081 // Window should receive key down event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005082 window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005083
5084 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
5085 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005086 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005087 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT, AKEY_EVENT_FLAG_CANCELED);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005088}
5089
5090TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07005091 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005092 sp<FakeWindowHandle> window =
5093 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
5094 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005095
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005096 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005097
Prabir Pradhan678438e2023-04-13 19:32:51 +00005098 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005099 AINPUT_SOURCE_TOUCHSCREEN,
5100 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005101
5102 // Window should receive motion down event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005103 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005104
5105 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
5106 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005107 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005108 window->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005109 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08005110}
5111
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07005112TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
5113 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005114 sp<FakeWindowHandle> window =
5115 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
5116 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07005117
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005118 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07005119
5120 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
5121 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
5122 .build());
5123
5124 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
5125
5126 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
5127 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
5128 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
5129
5130 // After the device has been reset, a new hovering stream can be sent to the window
5131 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
5132 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
5133 .build());
5134 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
5135}
5136
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005137TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
5138 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005139 sp<FakeWindowHandle> window =
5140 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
5141 ui::LogicalDisplayId::DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005142 window->setFocusable(true);
5143
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005144 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005145 setFocusedWindow(window);
5146
5147 window->consumeFocusEvent(true);
5148
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005149 const NotifyKeyArgs keyArgs =
5150 generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005151 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
5152 const nsecs_t injectTime = keyArgs.eventTime;
5153 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005154 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005155 // The dispatching time should be always greater than or equal to intercept key timeout.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005156 window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005157 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
5158 std::chrono::nanoseconds(interceptKeyTimeout).count());
5159}
5160
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07005161/**
5162 * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set.
5163 */
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005164TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
5165 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005166 sp<FakeWindowHandle> window =
5167 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
5168 ui::LogicalDisplayId::DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005169 window->setFocusable(true);
5170
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005171 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005172 setFocusedWindow(window);
5173
5174 window->consumeFocusEvent(true);
5175
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005176 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
5177 window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07005178
5179 // Set a value that's significantly larger than the default consumption timeout. If the
5180 // implementation is correct, the actual value doesn't matter; it won't slow down the test.
5181 mFakePolicy->setInterceptKeyTimeout(600ms);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005182 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07005183 // Window should receive key event immediately when same key up.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005184 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005185}
5186
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005187/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08005188 * Two windows. First is a regular window. Second does not overlap with the first, and has
5189 * WATCH_OUTSIDE_TOUCH.
5190 * Both windows are owned by the same UID.
5191 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
5192 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
5193 */
5194TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
5195 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005196 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
5197 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08005198 window->setFrame(Rect{0, 0, 100, 100});
5199
5200 sp<FakeWindowHandle> outsideWindow =
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07005201 sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005202 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08005203 outsideWindow->setFrame(Rect{100, 100, 200, 200});
5204 outsideWindow->setWatchOutsideTouch(true);
5205 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005206 mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08005207
5208 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005209 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005210 AINPUT_SOURCE_TOUCHSCREEN,
5211 ui::LogicalDisplayId::DEFAULT, {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08005212 window->consumeMotionDown();
5213 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
5214 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
5215 outsideWindow->consumeMotionEvent(
5216 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
Prabir Pradhan502a7252023-12-01 16:11:24 +00005217
5218 // Ensure outsideWindow doesn't get any more events for the gesture.
5219 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005220 ui::LogicalDisplayId::DEFAULT, {PointF{51, 51}}));
Prabir Pradhan502a7252023-12-01 16:11:24 +00005221 window->consumeMotionMove();
5222 outsideWindow->assertNoEvents();
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08005223}
5224
5225/**
Linnan Liccf6ce32024-04-11 20:32:13 +08005226 * Three windows:
5227 * - Left window
5228 * - Right window
5229 * - Outside window(watch for ACTION_OUTSIDE events)
5230 * The windows "left" and "outside" share the same owner, the window "right" has a different owner,
5231 * In order to allow the outside window can receive the ACTION_OUTSIDE events, the outside window is
5232 * positioned above the "left" and "right" windows, and it doesn't overlap with them.
5233 *
5234 * First, device A report a down event landed in the right window, the outside window can receive
5235 * an ACTION_OUTSIDE event that with zeroed coordinates, the device B report a down event landed
5236 * in the left window, the outside window can receive an ACTION_OUTSIDE event the with valid
5237 * coordinates, after these, device A and device B continue report MOVE event, the right and left
5238 * window can receive it, but outside window event can't receive it.
5239 */
5240TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinatesWhenMultiDevice) {
5241 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5242 sp<FakeWindowHandle> leftWindow =
5243 sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005244 ui::LogicalDisplayId::DEFAULT);
Linnan Liccf6ce32024-04-11 20:32:13 +08005245 leftWindow->setFrame(Rect{0, 0, 100, 100});
5246 leftWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
5247
5248 sp<FakeWindowHandle> outsideWindow =
5249 sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005250 ui::LogicalDisplayId::DEFAULT);
Linnan Liccf6ce32024-04-11 20:32:13 +08005251 outsideWindow->setFrame(Rect{100, 100, 200, 200});
5252 outsideWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
5253 outsideWindow->setWatchOutsideTouch(true);
5254
5255 std::shared_ptr<FakeApplicationHandle> anotherApplication =
5256 std::make_shared<FakeApplicationHandle>();
5257 sp<FakeWindowHandle> rightWindow =
5258 sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Right Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005259 ui::LogicalDisplayId::DEFAULT);
Linnan Liccf6ce32024-04-11 20:32:13 +08005260 rightWindow->setFrame(Rect{100, 0, 200, 100});
5261 rightWindow->setOwnerInfo(gui::Pid{2}, gui::Uid{202});
5262
5263 // OutsideWindow must be above left window and right window to receive ACTION_OUTSIDE events
5264 // when left window or right window is tapped
5265 mDispatcher->onWindowInfosChanged(
5266 {{*outsideWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()},
5267 {},
5268 0,
5269 0});
5270
5271 const DeviceId deviceA = 9;
5272 const DeviceId deviceB = 3;
5273
5274 // Tap on right window use device A
5275 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5276 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5277 .deviceId(deviceA)
5278 .build());
5279 leftWindow->assertNoEvents();
5280 rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA)));
5281 // Right window is belonged to another owner, so outsideWindow should receive ACTION_OUTSIDE
5282 // with zeroed coords.
5283 outsideWindow->consumeMotionEvent(
5284 AllOf(WithMotionAction(ACTION_OUTSIDE), WithDeviceId(deviceA), WithCoords(0, 0)));
5285
5286 // Tap on left window use device B
5287 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5288 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5289 .deviceId(deviceB)
5290 .build());
5291 leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB)));
5292 rightWindow->assertNoEvents();
5293 // Because new gesture down on the left window that has the same owner with outside Window, the
5294 // outside Window should receive the ACTION_OUTSIDE with coords.
5295 outsideWindow->consumeMotionEvent(
5296 AllOf(WithMotionAction(ACTION_OUTSIDE), WithDeviceId(deviceB), WithCoords(-50, -50)));
5297
5298 // Ensure that windows that can only accept outside do not receive remaining gestures
5299 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5300 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51))
5301 .deviceId(deviceA)
5302 .build());
5303 leftWindow->assertNoEvents();
5304 rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceA)));
5305
5306 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5307 .pointer(PointerBuilder(0, ToolType::FINGER).x(51).y(51))
5308 .deviceId(deviceB)
5309 .build());
5310 leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB)));
5311 rightWindow->assertNoEvents();
5312 outsideWindow->assertNoEvents();
5313}
5314
5315/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005316 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
5317 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
5318 * ACTION_OUTSIDE event is sent per gesture.
5319 */
5320TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
5321 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
5322 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005323 sp<FakeWindowHandle> window =
5324 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5325 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005326 window->setWatchOutsideTouch(true);
5327 window->setFrame(Rect{0, 0, 100, 100});
5328 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005329 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005330 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005331 secondWindow->setFrame(Rect{100, 100, 200, 200});
5332 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005333 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005334 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005335 thirdWindow->setFrame(Rect{200, 200, 300, 300});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005336 mDispatcher->onWindowInfosChanged(
5337 {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0});
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005338
5339 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005340 mDispatcher->notifyMotion(
5341 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5342 ui::LogicalDisplayId::DEFAULT, {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005343 window->assertNoEvents();
5344 secondWindow->assertNoEvents();
5345
5346 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
5347 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005348 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005349 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +00005350 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08005351 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
5352 window->consumeMotionEvent(
5353 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005354 secondWindow->consumeMotionDown();
5355 thirdWindow->assertNoEvents();
5356
5357 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
5358 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005359 mDispatcher->notifyMotion(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005360 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5361 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +00005362 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005363 window->assertNoEvents();
5364 secondWindow->consumeMotionMove();
5365 thirdWindow->consumeMotionDown();
5366}
5367
Prabir Pradhan814fe082022-07-22 20:22:18 +00005368TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
5369 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005370 sp<FakeWindowHandle> window =
5371 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
5372 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00005373 window->setFocusable(true);
5374
Patrick Williamsd828f302023-04-28 17:52:08 -05005375 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00005376 setFocusedWindow(window);
5377
5378 window->consumeFocusEvent(true);
5379
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005380 const NotifyKeyArgs keyDown =
5381 generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT);
5382 const NotifyKeyArgs keyUp =
5383 generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005384 mDispatcher->notifyKey(keyDown);
5385 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00005386
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005387 window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
5388 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00005389
5390 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05005391 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00005392
5393 window->consumeFocusEvent(false);
5394
Prabir Pradhan678438e2023-04-13 19:32:51 +00005395 mDispatcher->notifyKey(keyDown);
5396 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00005397 window->assertNoEvents();
5398}
5399
Arthur Hung96483742022-11-15 03:30:48 +00005400TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
5401 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005402 sp<FakeWindowHandle> window =
5403 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
5404 ui::LogicalDisplayId::DEFAULT);
Arthur Hung96483742022-11-15 03:30:48 +00005405 // Ensure window is non-split and have some transform.
5406 window->setPreventSplitting(true);
5407 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05005408 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00005409
5410 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005411 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
5412 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hung96483742022-11-15 03:30:48 +00005413 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005414 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung96483742022-11-15 03:30:48 +00005415
5416 const MotionEvent secondFingerDownEvent =
5417 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005418 .displayId(ui::LogicalDisplayId::DEFAULT)
Arthur Hung96483742022-11-15 03:30:48 +00005419 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005420 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
5421 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00005422 .build();
5423 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005424 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung96483742022-11-15 03:30:48 +00005425 InputEventInjectionSync::WAIT_FOR_RESULT))
5426 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5427
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005428 std::unique_ptr<MotionEvent> event = window->consumeMotionEvent();
5429 ASSERT_NE(nullptr, event);
5430 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
5431 EXPECT_EQ(70, event->getX(0)); // 50 + 20
5432 EXPECT_EQ(90, event->getY(0)); // 50 + 40
5433 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
5434 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
Arthur Hung96483742022-11-15 03:30:48 +00005435}
5436
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07005437/**
5438 * Two windows: a splittable and a non-splittable.
5439 * The non-splittable window shouldn't receive any "incomplete" gestures.
5440 * Send the first pointer to the splittable window, and then touch the non-splittable window.
5441 * The second pointer should be dropped because the initial window is splittable, so it won't get
5442 * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any
5443 * "incomplete" gestures.
5444 */
5445TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) {
5446 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5447 sp<FakeWindowHandle> leftWindow =
5448 sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005449 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07005450 leftWindow->setPreventSplitting(false);
5451 leftWindow->setFrame(Rect(0, 0, 100, 100));
5452 sp<FakeWindowHandle> rightWindow =
5453 sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005454 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07005455 rightWindow->setPreventSplitting(true);
5456 rightWindow->setFrame(Rect(100, 100, 200, 200));
5457 mDispatcher->onWindowInfosChanged(
5458 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
5459
5460 // Touch down on left, splittable window
5461 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5462 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5463 .build());
5464 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
5465
5466 mDispatcher->notifyMotion(
5467 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5468 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
5469 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
5470 .build());
5471 leftWindow->assertNoEvents();
5472 rightWindow->assertNoEvents();
5473}
5474
Harry Cuttsb166c002023-05-09 13:06:05 +00005475TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
5476 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005477 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
5478 ui::LogicalDisplayId::DEFAULT);
Harry Cuttsb166c002023-05-09 13:06:05 +00005479 window->setFrame(Rect(0, 0, 400, 400));
5480 sp<FakeWindowHandle> trustedOverlay =
5481 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005482 ui::LogicalDisplayId::DEFAULT);
Harry Cuttsb166c002023-05-09 13:06:05 +00005483 trustedOverlay->setSpy(true);
5484 trustedOverlay->setTrustedOverlay(true);
5485
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005486 mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00005487
5488 // Start a three-finger touchpad swipe
5489 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
5490 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
5491 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5492 .build());
5493 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
5494 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
5495 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
5496 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5497 .build());
5498 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
5499 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
5500 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
5501 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
5502 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5503 .build());
5504
5505 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
5506 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
5507 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
5508
5509 // Move the swipe a bit
5510 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
5511 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5512 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
5513 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
5514 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5515 .build());
5516
5517 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5518
5519 // End the swipe
5520 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
5521 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5522 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
5523 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
5524 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5525 .build());
5526 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
5527 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5528 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
5529 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5530 .build());
5531 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
5532 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5533 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5534 .build());
5535
5536 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
5537 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
5538 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
5539
5540 window->assertNoEvents();
5541}
5542
5543TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
5544 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005545 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
5546 ui::LogicalDisplayId::DEFAULT);
Harry Cuttsb166c002023-05-09 13:06:05 +00005547 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005548 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00005549
5550 // Start a three-finger touchpad swipe
5551 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
5552 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
5553 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5554 .build());
5555 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
5556 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
5557 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
5558 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5559 .build());
5560 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
5561 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
5562 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
5563 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
5564 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5565 .build());
5566
5567 // Move the swipe a bit
5568 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
5569 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5570 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
5571 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
5572 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5573 .build());
5574
5575 // End the swipe
5576 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
5577 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5578 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
5579 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
5580 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5581 .build());
5582 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
5583 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5584 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
5585 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5586 .build());
5587 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
5588 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5589 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5590 .build());
5591
5592 window->assertNoEvents();
5593}
5594
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005595/**
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005596 * Send a two-pointer gesture to a single window. The window's orientation changes in response to
5597 * the first pointer.
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00005598 * Ensure that the second pointer and the subsequent gesture is correctly delivered to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005599 */
5600TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) {
5601 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005602 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
5603 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005604 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07005605 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005606
5607 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
5608 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5609 .downTime(baseTime + 10)
5610 .eventTime(baseTime + 10)
5611 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
5612 .build());
5613
5614 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
5615
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005616 // Change the transform so that the orientation is now different from original.
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07005617 window->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005618
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07005619 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005620
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005621 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5622 .downTime(baseTime + 10)
5623 .eventTime(baseTime + 30)
5624 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
5625 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
5626 .build());
5627
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00005628 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
5629
5630 // Finish the gesture and start a new one. Ensure all events are sent to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005631 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
5632 .downTime(baseTime + 10)
5633 .eventTime(baseTime + 40)
5634 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
5635 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
5636 .build());
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00005637
5638 window->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
5639
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005640 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
5641 .downTime(baseTime + 10)
5642 .eventTime(baseTime + 50)
5643 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
5644 .build());
5645
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00005646 window->consumeMotionEvent(WithMotionAction(ACTION_UP));
5647
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005648 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5649 .downTime(baseTime + 60)
5650 .eventTime(baseTime + 60)
5651 .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40))
5652 .build());
5653
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07005654 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005655}
5656
5657/**
Hu Guo771a7692023-09-17 20:51:08 +08005658 * When there are multiple screens, such as screen projection to TV or screen recording, if the
5659 * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and
5660 * its coordinates should be converted by the transform of the windows of target screen.
5661 */
5662TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) {
5663 // This case will create a window and a spy window on the default display and mirror
5664 // window on the second display. cancel event is sent through spy window pilferPointers
5665 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5666
5667 sp<FakeWindowHandle> spyWindowDefaultDisplay =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005668 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
5669 ui::LogicalDisplayId::DEFAULT);
Hu Guo771a7692023-09-17 20:51:08 +08005670 spyWindowDefaultDisplay->setTrustedOverlay(true);
5671 spyWindowDefaultDisplay->setSpy(true);
5672
5673 sp<FakeWindowHandle> windowDefaultDisplay =
5674 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005675 ui::LogicalDisplayId::DEFAULT);
Hu Guo771a7692023-09-17 20:51:08 +08005676 windowDefaultDisplay->setWindowTransform(1, 0, 0, 1);
5677
5678 sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID);
5679 windowSecondDisplay->setWindowTransform(2, 0, 0, 2);
5680
5681 // Add the windows to the dispatcher
5682 mDispatcher->onWindowInfosChanged(
5683 {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(),
5684 *windowSecondDisplay->getInfo()},
5685 {},
5686 0,
5687 0});
5688
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005689 // Send down to ui::LogicalDisplayId::DEFAULT
Hu Guo771a7692023-09-17 20:51:08 +08005690 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005691 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
5692 ui::LogicalDisplayId::DEFAULT, {100, 100}))
Hu Guo771a7692023-09-17 20:51:08 +08005693 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5694
5695 spyWindowDefaultDisplay->consumeMotionDown();
5696 windowDefaultDisplay->consumeMotionDown();
5697
5698 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken()));
5699
5700 // windowDefaultDisplay gets cancel
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005701 std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent();
5702 ASSERT_NE(nullptr, event);
5703 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction());
Hu Guo771a7692023-09-17 20:51:08 +08005704
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005705 // The cancel event is sent to windowDefaultDisplay of the ui::LogicalDisplayId::DEFAULT
5706 // display, so the coordinates of the cancel are converted by windowDefaultDisplay's transform,
5707 // the x and y coordinates are both 100, otherwise if the cancel event is sent to
5708 // windowSecondDisplay of SECOND_DISPLAY_ID, the x and y coordinates are 200
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005709 EXPECT_EQ(100, event->getX(0));
5710 EXPECT_EQ(100, event->getY(0));
Hu Guo771a7692023-09-17 20:51:08 +08005711}
5712
5713/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005714 * Ensure the correct coordinate spaces are used by InputDispatcher.
5715 *
5716 * InputDispatcher works in the display space, so its coordinate system is relative to the display
5717 * panel. Windows get events in the window space, and get raw coordinates in the logical display
5718 * space.
5719 */
5720class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
5721public:
5722 void SetUp() override {
5723 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005724 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005725 }
5726
Linnan Li13bf76a2024-05-05 19:18:02 +08005727 void addDisplayInfo(ui::LogicalDisplayId displayId, const ui::Transform& transform) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005728 gui::DisplayInfo info;
5729 info.displayId = displayId;
5730 info.transform = transform;
5731 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05005732 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005733 }
5734
5735 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
5736 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05005737 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005738 }
5739
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005740 void removeAllWindowsAndDisplays() {
5741 mDisplayInfos.clear();
5742 mWindowInfos.clear();
5743 }
5744
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005745 // Set up a test scenario where the display has a scaled projection and there are two windows
5746 // on the display.
5747 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
5748 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
5749 // respectively.
5750 ui::Transform displayTransform;
5751 displayTransform.set(2, 0, 0, 4);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005752 addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005753
5754 std::shared_ptr<FakeApplicationHandle> application =
5755 std::make_shared<FakeApplicationHandle>();
5756
5757 // Add two windows to the display. Their frames are represented in the display space.
5758 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005759 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005760 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005761 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
5762 addWindow(firstWindow);
5763
5764 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005765 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005766 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005767 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
5768 addWindow(secondWindow);
5769 return {std::move(firstWindow), std::move(secondWindow)};
5770 }
5771
5772private:
5773 std::vector<gui::DisplayInfo> mDisplayInfos;
5774 std::vector<gui::WindowInfo> mWindowInfos;
5775};
5776
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005777TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005778 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5779 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005780 // selected so that if the hit test was performed with the point and the bounds being in
5781 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005782 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005783 AINPUT_SOURCE_TOUCHSCREEN,
5784 ui::LogicalDisplayId::DEFAULT, {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005785
5786 firstWindow->consumeMotionDown();
5787 secondWindow->assertNoEvents();
5788}
5789
5790// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
5791// the event should be treated as being in the logical display space.
5792TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
5793 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5794 // Send down to the first window. The point is represented in the logical display space. The
5795 // point is selected so that if the hit test was done in logical display space, then it would
5796 // end up in the incorrect window.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005797 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005798 PointF{75 * 2, 55 * 4});
5799
5800 firstWindow->consumeMotionDown();
5801 secondWindow->assertNoEvents();
5802}
5803
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005804// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
5805// event should be treated as being in the logical display space.
5806TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
5807 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5808
5809 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5810 ui::Transform injectedEventTransform;
5811 injectedEventTransform.set(matrix);
5812 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
5813 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
5814
5815 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005816 .displayId(ui::LogicalDisplayId::DEFAULT)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005817 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005818 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005819 .x(untransformedPoint.x)
5820 .y(untransformedPoint.y))
5821 .build();
5822 event.transform(matrix);
5823
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005824 injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT,
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005825 InputEventInjectionSync::WAIT_FOR_RESULT);
5826
5827 firstWindow->consumeMotionDown();
5828 secondWindow->assertNoEvents();
5829}
5830
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005831TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
5832 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5833
5834 // Send down to the second window.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005835 mDispatcher->notifyMotion(
5836 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5837 ui::LogicalDisplayId::DEFAULT, {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005838
5839 firstWindow->assertNoEvents();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005840 std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent();
5841 ASSERT_NE(nullptr, event);
5842 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005843
5844 // Ensure that the events from the "getRaw" API are in logical display coordinates.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005845 EXPECT_EQ(300, event->getRawX(0));
5846 EXPECT_EQ(880, event->getRawY(0));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005847
5848 // Ensure that the x and y values are in the window's coordinate space.
5849 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
5850 // the logical display space. This will be the origin of the window space.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005851 EXPECT_EQ(100, event->getX(0));
5852 EXPECT_EQ(80, event->getY(0));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005853}
5854
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005855TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) {
5856 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5857 // The monitor will always receive events in the logical display's coordinate space, because
5858 // it does not have a window.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005859 FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ui::LogicalDisplayId::DEFAULT};
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005860
5861 // Send down to the first window.
5862 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005863 ui::LogicalDisplayId::DEFAULT, {PointF{50, 100}}));
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005864 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5865 monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5866
5867 // Second pointer goes down on second window.
5868 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005869 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005870 {PointF{50, 100}, PointF{150, 220}}));
5871 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80)));
5872 const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}},
5873 {1, PointF{300, 880}}};
5874 monitor.consumeMotionEvent(
5875 AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers)));
5876
5877 mDispatcher->cancelCurrentTouch();
5878
5879 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400)));
5880 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80)));
5881 monitor.consumeMotionEvent(
5882 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers)));
5883}
5884
Prabir Pradhan1c29a092023-09-21 10:29:29 +00005885TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) {
5886 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5887
5888 // Send down to the first window.
5889 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005890 ui::LogicalDisplayId::DEFAULT, {PointF{50, 100}}));
Prabir Pradhan1c29a092023-09-21 10:29:29 +00005891 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5892
5893 // The pointer is transferred to the second window, and the second window receives it in the
5894 // correct coordinate space.
Prabir Pradhan367f3432024-02-13 23:05:58 +00005895 mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken());
Prabir Pradhan1c29a092023-09-21 10:29:29 +00005896 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400)));
5897 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400)));
5898}
5899
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005900TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) {
5901 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5902
5903 // Send hover move to the second window, and ensure it shows up as hover enter.
5904 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005905 ui::LogicalDisplayId::DEFAULT,
5906 {PointF{150, 220}}));
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005907 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5908 WithCoords(100, 80), WithRawCoords(300, 880)));
5909
5910 // Touch down at the same location and ensure a hover exit is synthesized.
5911 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005912 ui::LogicalDisplayId::DEFAULT,
5913 {PointF{150, 220}}));
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005914 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5915 WithRawCoords(300, 880)));
5916 secondWindow->consumeMotionEvent(
5917 AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880)));
5918 secondWindow->assertNoEvents();
5919 firstWindow->assertNoEvents();
5920}
5921
Prabir Pradhan453ae732023-10-13 14:30:14 +00005922// Same as above, but while the window is being mirrored.
5923TEST_F(InputDispatcherDisplayProjectionTest,
5924 SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) {
5925 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5926
5927 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5928 ui::Transform secondDisplayTransform;
5929 secondDisplayTransform.set(matrix);
5930 addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform);
5931
5932 sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID);
5933 secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4);
5934 addWindow(secondWindowClone);
5935
5936 // Send hover move to the second window, and ensure it shows up as hover enter.
5937 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005938 ui::LogicalDisplayId::DEFAULT,
5939 {PointF{150, 220}}));
Prabir Pradhan453ae732023-10-13 14:30:14 +00005940 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5941 WithCoords(100, 80), WithRawCoords(300, 880)));
5942
5943 // Touch down at the same location and ensure a hover exit is synthesized for the correct
5944 // display.
5945 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005946 ui::LogicalDisplayId::DEFAULT,
5947 {PointF{150, 220}}));
Prabir Pradhan453ae732023-10-13 14:30:14 +00005948 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5949 WithRawCoords(300, 880)));
5950 secondWindow->consumeMotionEvent(
5951 AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880)));
5952 secondWindow->assertNoEvents();
5953 firstWindow->assertNoEvents();
5954}
5955
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005956TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) {
5957 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5958
5959 // Send hover enter to second window
5960 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005961 ui::LogicalDisplayId::DEFAULT,
5962 {PointF{150, 220}}));
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005963 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5964 WithCoords(100, 80), WithRawCoords(300, 880)));
5965
5966 mDispatcher->cancelCurrentTouch();
5967
5968 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5969 WithRawCoords(300, 880)));
5970 secondWindow->assertNoEvents();
5971 firstWindow->assertNoEvents();
5972}
5973
Prabir Pradhan453ae732023-10-13 14:30:14 +00005974// Same as above, but while the window is being mirrored.
Prabir Pradhan16463382023-10-12 23:03:19 +00005975TEST_F(InputDispatcherDisplayProjectionTest,
5976 SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) {
5977 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5978
5979 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5980 ui::Transform secondDisplayTransform;
5981 secondDisplayTransform.set(matrix);
5982 addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform);
5983
5984 sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID);
5985 secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4);
5986 addWindow(secondWindowClone);
5987
5988 // Send hover enter to second window
5989 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005990 ui::LogicalDisplayId::DEFAULT,
5991 {PointF{150, 220}}));
Prabir Pradhan16463382023-10-12 23:03:19 +00005992 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5993 WithCoords(100, 80), WithRawCoords(300, 880),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005994 WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Prabir Pradhan16463382023-10-12 23:03:19 +00005995
5996 mDispatcher->cancelCurrentTouch();
5997
5998 // Ensure the cancelation happens with the correct displayId and the correct coordinates.
5999 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
6000 WithRawCoords(300, 880),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006001 WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Prabir Pradhan16463382023-10-12 23:03:19 +00006002 secondWindow->assertNoEvents();
6003 firstWindow->assertNoEvents();
6004}
6005
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00006006/** Ensure consistent behavior of InputDispatcher in all orientations. */
6007class InputDispatcherDisplayOrientationFixture
6008 : public InputDispatcherDisplayProjectionTest,
6009 public ::testing::WithParamInterface<ui::Rotation> {};
6010
6011// This test verifies the touchable region of a window for all rotations of the display by tapping
6012// in different locations on the display, specifically points close to the four corners of a
6013// window.
6014TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
6015 constexpr static int32_t displayWidth = 400;
6016 constexpr static int32_t displayHeight = 800;
6017
6018 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6019
6020 const auto rotation = GetParam();
6021
6022 // Set up the display with the specified rotation.
6023 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
6024 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
6025 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
6026 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
6027 logicalDisplayWidth, logicalDisplayHeight);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006028 addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform);
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00006029
6030 // Create a window with its bounds determined in the logical display.
6031 const Rect frameInLogicalDisplay(100, 100, 200, 300);
6032 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006033 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
6034 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00006035 window->setFrame(frameInDisplay, displayTransform);
6036 addWindow(window);
6037
6038 // The following points in logical display space should be inside the window.
6039 static const std::array<vec2, 4> insidePoints{
6040 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
6041 for (const auto pointInsideWindow : insidePoints) {
6042 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
6043 const PointF pointInDisplaySpace{p.x, p.y};
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006044 mDispatcher->notifyMotion(
6045 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6046 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00006047 window->consumeMotionDown();
6048
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006049 mDispatcher->notifyMotion(
6050 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6051 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00006052 window->consumeMotionUp();
6053 }
6054
6055 // The following points in logical display space should be outside the window.
6056 static const std::array<vec2, 5> outsidePoints{
6057 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
6058 for (const auto pointOutsideWindow : outsidePoints) {
6059 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
6060 const PointF pointInDisplaySpace{p.x, p.y};
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006061 mDispatcher->notifyMotion(
6062 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6063 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00006064
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006065 mDispatcher->notifyMotion(
6066 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6067 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00006068 }
6069 window->assertNoEvents();
6070}
6071
Linnan Li5e5645e2024-03-05 14:43:05 +00006072// This test verifies the occlusion detection for all rotations of the display by tapping
6073// in different locations on the display, specifically points close to the four corners of a
6074// window.
6075TEST_P(InputDispatcherDisplayOrientationFixture, BlockUntrustClickInDifferentOrientations) {
6076 constexpr static int32_t displayWidth = 400;
6077 constexpr static int32_t displayHeight = 800;
6078
6079 std::shared_ptr<FakeApplicationHandle> untrustedWindowApplication =
6080 std::make_shared<FakeApplicationHandle>();
6081 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6082
6083 const auto rotation = GetParam();
6084
6085 // Set up the display with the specified rotation.
6086 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
6087 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
6088 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
6089 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
6090 logicalDisplayWidth, logicalDisplayHeight);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006091 addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform);
Linnan Li5e5645e2024-03-05 14:43:05 +00006092
6093 // Create a window that not trusted.
6094 const Rect untrustedWindowFrameInLogicalDisplay(100, 100, 200, 300);
6095
6096 const Rect untrustedWindowFrameInDisplay =
6097 displayTransform.inverse().transform(untrustedWindowFrameInLogicalDisplay);
6098
6099 sp<FakeWindowHandle> untrustedWindow =
6100 sp<FakeWindowHandle>::make(untrustedWindowApplication, mDispatcher, "UntrustedWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006101 ui::LogicalDisplayId::DEFAULT);
Linnan Li5e5645e2024-03-05 14:43:05 +00006102 untrustedWindow->setFrame(untrustedWindowFrameInDisplay, displayTransform);
6103 untrustedWindow->setTrustedOverlay(false);
6104 untrustedWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
6105 untrustedWindow->setTouchable(false);
6106 untrustedWindow->setAlpha(1.0f);
6107 untrustedWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
6108 addWindow(untrustedWindow);
6109
6110 // Create a simple app window below the untrusted window.
6111 const Rect simpleAppWindowFrameInLogicalDisplay(0, 0, 300, 600);
6112 const Rect simpleAppWindowFrameInDisplay =
6113 displayTransform.inverse().transform(simpleAppWindowFrameInLogicalDisplay);
6114
6115 sp<FakeWindowHandle> simpleAppWindow =
6116 sp<FakeWindowHandle>::make(application, mDispatcher, "SimpleAppWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006117 ui::LogicalDisplayId::DEFAULT);
Linnan Li5e5645e2024-03-05 14:43:05 +00006118 simpleAppWindow->setFrame(simpleAppWindowFrameInDisplay, displayTransform);
6119 simpleAppWindow->setOwnerInfo(gui::Pid{2}, gui::Uid{202});
6120 addWindow(simpleAppWindow);
6121
6122 // The following points in logical display space should be inside the untrusted window, so
6123 // the simple window could not receive events that coordinate is these point.
6124 static const std::array<vec2, 4> untrustedPoints{
6125 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
6126
6127 for (const auto untrustedPoint : untrustedPoints) {
6128 const vec2 p = displayTransform.inverse().transform(untrustedPoint);
6129 const PointF pointInDisplaySpace{p.x, p.y};
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006130 mDispatcher->notifyMotion(
6131 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6132 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
6133 mDispatcher->notifyMotion(
6134 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6135 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
Linnan Li5e5645e2024-03-05 14:43:05 +00006136 }
6137 untrustedWindow->assertNoEvents();
6138 simpleAppWindow->assertNoEvents();
6139 // The following points in logical display space should be outside the untrusted window, so
6140 // the simple window should receive events that coordinate is these point.
6141 static const std::array<vec2, 5> trustedPoints{
6142 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
6143 for (const auto trustedPoint : trustedPoints) {
6144 const vec2 p = displayTransform.inverse().transform(trustedPoint);
6145 const PointF pointInDisplaySpace{p.x, p.y};
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006146 mDispatcher->notifyMotion(
6147 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6148 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
6149 simpleAppWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
Linnan Li5e5645e2024-03-05 14:43:05 +00006150 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006151 mDispatcher->notifyMotion(
6152 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6153 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
6154 simpleAppWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
Linnan Li5e5645e2024-03-05 14:43:05 +00006155 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
6156 }
6157 untrustedWindow->assertNoEvents();
6158}
6159
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00006160// Run the precision tests for all rotations.
6161INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
6162 InputDispatcherDisplayOrientationFixture,
6163 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6164 ui::ROTATION_270),
6165 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6166 return ftl::enum_string(testParamInfo.param);
6167 });
6168
Siarhei Vishniakou18050092021-09-01 13:32:49 -07006169using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
6170 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006171
6172class TransferTouchFixture : public InputDispatcherTest,
6173 public ::testing::WithParamInterface<TransferFunction> {};
6174
6175TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07006176 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08006177
6178 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006179 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006180 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006181 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006182 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006183 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006184 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006185 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006186 sp<FakeWindowHandle> wallpaper =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006187 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper",
6188 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006189 wallpaper->setIsWallpaper(true);
Prabir Pradhan65455c72024-02-13 21:46:41 +00006190 // Add the windows to the dispatcher, and ensure the first window is focused
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006191 mDispatcher->onWindowInfosChanged(
6192 {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0});
Prabir Pradhan65455c72024-02-13 21:46:41 +00006193 setFocusedWindow(firstWindow);
6194 firstWindow->consumeFocusEvent(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006195
6196 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006197 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006198 AINPUT_SOURCE_TOUCHSCREEN,
6199 ui::LogicalDisplayId::DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00006200
Svet Ganov5d3bc372020-01-26 23:11:07 -08006201 // Only the first window should get the down event
6202 firstWindow->consumeMotionDown();
6203 secondWindow->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006204 wallpaper->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Prabir Pradhan65455c72024-02-13 21:46:41 +00006205 // Dispatcher reports pointer down outside focus for the wallpaper
6206 mFakePolicy->assertOnPointerDownEquals(wallpaper->getToken());
Svet Ganov5d3bc372020-01-26 23:11:07 -08006207
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006208 // Transfer touch to the second window
6209 TransferFunction f = GetParam();
6210 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
6211 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006212 // The first window gets cancel and the second gets down
6213 firstWindow->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006214 secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
6215 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6216 wallpaper->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Prabir Pradhan65455c72024-02-13 21:46:41 +00006217 // There should not be any changes to the focused window when transferring touch
6218 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertOnPointerDownWasNotCalled());
Svet Ganov5d3bc372020-01-26 23:11:07 -08006219
6220 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006221 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006222 ui::LogicalDisplayId::DEFAULT));
Prabir Pradhan65455c72024-02-13 21:46:41 +00006223 // The first window gets no events and the second gets up
Svet Ganov5d3bc372020-01-26 23:11:07 -08006224 firstWindow->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006225 secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
6226 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006227 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08006228}
6229
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006230/**
Prabir Pradhan367f3432024-02-13 23:05:58 +00006231 * When 'transferTouchGesture' API is invoked, dispatcher needs to find the "best" window to take
6232 * touch from. When we have spy windows, there are several windows to choose from: either spy, or
6233 * the 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006234 * natural to the user.
6235 * In this test, we are sending a pointer to both spy window and first window. We then try to
6236 * transfer touch to the second window. The dispatcher should identify the first window as the
6237 * one that should lose the gesture, and therefore the action should be to move the gesture from
6238 * the first window to the second.
Prabir Pradhan367f3432024-02-13 23:05:58 +00006239 * The main goal here is to test the behaviour of 'transferTouchGesture' API, but it's still valid
6240 * to test the other API, as well.
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006241 */
6242TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
6243 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6244
6245 // Create a couple of windows + a spy window
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006246 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
6247 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006248 spyWindow->setTrustedOverlay(true);
6249 spyWindow->setSpy(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006250 sp<FakeWindowHandle> firstWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "First",
6251 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006252 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006253 sp<FakeWindowHandle>::make(application, mDispatcher, "Second",
6254 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006255
6256 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006257 mDispatcher->onWindowInfosChanged(
6258 {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006259
6260 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006261 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006262 AINPUT_SOURCE_TOUCHSCREEN,
6263 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006264 // Only the first window and spy should get the down event
6265 spyWindow->consumeMotionDown();
6266 firstWindow->consumeMotionDown();
6267
6268 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
Prabir Pradhan367f3432024-02-13 23:05:58 +00006269 // if f === 'transferTouchGesture'.
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006270 TransferFunction f = GetParam();
6271 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
6272 ASSERT_TRUE(success);
6273 // The first window gets cancel and the second gets down
6274 firstWindow->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006275 secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
6276 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006277
6278 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006279 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006280 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006281 // The first window gets no events and the second+spy get up
6282 firstWindow->assertNoEvents();
6283 spyWindow->consumeMotionUp();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006284 secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
6285 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006286}
6287
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006288TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07006289 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08006290
6291 PointF touchPoint = {10, 10};
6292
6293 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006294 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006295 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006296 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08006297 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006298 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006299 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006300 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08006301 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006302
6303 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006304 mDispatcher->onWindowInfosChanged(
6305 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08006306
6307 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006308 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006309 AINPUT_SOURCE_TOUCHSCREEN,
6310 ui::LogicalDisplayId::DEFAULT, {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006311 // Only the first window should get the down event
6312 firstWindow->consumeMotionDown();
6313 secondWindow->assertNoEvents();
6314
6315 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006316 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006317 ui::LogicalDisplayId::DEFAULT,
6318 {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006319 // Only the first window should get the pointer down event
6320 firstWindow->consumeMotionPointerDown(1);
6321 secondWindow->assertNoEvents();
6322
6323 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006324 TransferFunction f = GetParam();
6325 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
6326 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006327 // The first window gets cancel and the second gets down and pointer down
6328 firstWindow->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006329 secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
6330 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6331 secondWindow->consumeMotionPointerDown(1, ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan65455c72024-02-13 21:46:41 +00006332 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006333
6334 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006335 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006336 ui::LogicalDisplayId::DEFAULT,
6337 {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006338 // The first window gets nothing and the second gets pointer up
6339 firstWindow->assertNoEvents();
Prabir Pradhan33cfc6d2024-06-11 20:17:44 +00006340 secondWindow->consumeMotionPointerUp(/*pointerIdx=*/1,
6341 AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT),
6342 WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE),
6343 WithPointerCount(2)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006344
6345 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006346 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006347 ui::LogicalDisplayId::DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006348 // The first window gets nothing and the second gets up
6349 firstWindow->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006350 secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
6351 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006352}
6353
Arthur Hungc539dbb2022-12-08 07:45:36 +00006354TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
6355 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6356
6357 // Create a couple of windows
6358 sp<FakeWindowHandle> firstWindow =
6359 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006360 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006361 firstWindow->setDupTouchToWallpaper(true);
6362 sp<FakeWindowHandle> secondWindow =
6363 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006364 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006365 secondWindow->setDupTouchToWallpaper(true);
6366
6367 sp<FakeWindowHandle> wallpaper1 =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006368 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1",
6369 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006370 wallpaper1->setIsWallpaper(true);
6371
6372 sp<FakeWindowHandle> wallpaper2 =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006373 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2",
6374 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006375 wallpaper2->setIsWallpaper(true);
6376 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006377 mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(),
6378 *secondWindow->getInfo(), *wallpaper2->getInfo()},
6379 {},
6380 0,
6381 0});
Arthur Hungc539dbb2022-12-08 07:45:36 +00006382
6383 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006384 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006385 AINPUT_SOURCE_TOUCHSCREEN,
6386 ui::LogicalDisplayId::DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00006387
6388 // Only the first window should get the down event
6389 firstWindow->consumeMotionDown();
6390 secondWindow->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006391 wallpaper1->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006392 wallpaper2->assertNoEvents();
6393
6394 // Transfer touch focus to the second window
6395 TransferFunction f = GetParam();
6396 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
6397 ASSERT_TRUE(success);
6398
6399 // The first window gets cancel and the second gets down
6400 firstWindow->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006401 secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
6402 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6403 wallpaper1->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
6404 wallpaper2->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
Linnan Li72352222024-04-12 18:55:57 +08006405 EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006406
6407 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006408 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006409 ui::LogicalDisplayId::DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00006410 // The first window gets no events and the second gets up
6411 firstWindow->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006412 secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
6413 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006414 wallpaper1->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006415 wallpaper2->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
Linnan Li72352222024-04-12 18:55:57 +08006416 EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006417}
6418
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006419// For the cases of single pointer touch and two pointers non-split touch, the api's
Prabir Pradhan367f3432024-02-13 23:05:58 +00006420// 'transferTouchGesture' and 'transferTouchOnDisplay' are equivalent in behaviour. They only differ
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006421// for the case where there are multiple pointers split across several windows.
Prabir Pradhan367f3432024-02-13 23:05:58 +00006422INSTANTIATE_TEST_SUITE_P(
6423 InputDispatcherTransferFunctionTests, TransferTouchFixture,
6424 ::testing::Values(
6425 [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> /*ignored*/,
6426 sp<IBinder> destChannelToken) {
6427 return dispatcher->transferTouchOnDisplay(destChannelToken,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006428 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan367f3432024-02-13 23:05:58 +00006429 },
6430 [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> from,
6431 sp<IBinder> to) {
6432 return dispatcher->transferTouchGesture(from, to,
6433 /*isDragAndDrop=*/false);
6434 }));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006435
Prabir Pradhan367f3432024-02-13 23:05:58 +00006436TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07006437 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08006438
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006439 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006440 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006441 ui::LogicalDisplayId::DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006442 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006443
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006444 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006445 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006446 ui::LogicalDisplayId::DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006447 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006448
6449 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006450 mDispatcher->onWindowInfosChanged(
6451 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08006452
6453 PointF pointInFirst = {300, 200};
6454 PointF pointInSecond = {300, 600};
6455
6456 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006457 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006458 AINPUT_SOURCE_TOUCHSCREEN,
6459 ui::LogicalDisplayId::DEFAULT, {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006460 // Only the first window should get the down event
6461 firstWindow->consumeMotionDown();
6462 secondWindow->assertNoEvents();
6463
6464 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006465 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006466 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +00006467 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006468 // The first window gets a move and the second a down
6469 firstWindow->consumeMotionMove();
6470 secondWindow->consumeMotionDown();
6471
Prabir Pradhan367f3432024-02-13 23:05:58 +00006472 // Transfer touch to the second window
6473 mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken());
Svet Ganov5d3bc372020-01-26 23:11:07 -08006474 // The first window gets cancel and the new gets pointer down (it already saw down)
6475 firstWindow->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006476 secondWindow->consumeMotionPointerDown(1, ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan65455c72024-02-13 21:46:41 +00006477 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006478
6479 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006480 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006481 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +00006482 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006483 // The first window gets nothing and the second gets pointer up
6484 firstWindow->assertNoEvents();
Prabir Pradhan33cfc6d2024-06-11 20:17:44 +00006485 secondWindow->consumeMotionPointerUp(/*pointerIdx=*/1,
6486 AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT),
6487 WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE),
6488 WithPointerCount(2)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006489
6490 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006491 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006492 ui::LogicalDisplayId::DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006493 // The first window gets nothing and the second gets up
6494 firstWindow->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006495 secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
6496 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006497}
6498
Prabir Pradhan367f3432024-02-13 23:05:58 +00006499// Same as TransferTouch_TwoPointersSplitTouch, but using 'transferTouchOnDisplay' api.
6500// Unlike 'transferTouchGesture', calling 'transferTouchOnDisplay' when there are two windows
6501// receiving touch is not supported, so the touch should continue on those windows and the
6502// transferred-to window should get nothing.
6503TEST_F(InputDispatcherTest, TransferTouchOnDisplay_TwoPointersSplitTouch) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006504 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6505
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006506 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006507 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006508 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006509 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006510
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006511 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006512 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006513 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006514 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006515
6516 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006517 mDispatcher->onWindowInfosChanged(
6518 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006519
6520 PointF pointInFirst = {300, 200};
6521 PointF pointInSecond = {300, 600};
6522
6523 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006524 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006525 AINPUT_SOURCE_TOUCHSCREEN,
6526 ui::LogicalDisplayId::DEFAULT, {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006527 // Only the first window should get the down event
6528 firstWindow->consumeMotionDown();
6529 secondWindow->assertNoEvents();
6530
6531 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006532 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006533 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +00006534 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006535 // The first window gets a move and the second a down
6536 firstWindow->consumeMotionMove();
6537 secondWindow->consumeMotionDown();
6538
6539 // Transfer touch focus to the second window
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006540 const bool transferred = mDispatcher->transferTouchOnDisplay(secondWindow->getToken(),
6541 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan367f3432024-02-13 23:05:58 +00006542 // The 'transferTouchOnDisplay' call should not succeed, because there are 2 touched windows
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006543 ASSERT_FALSE(transferred);
6544 firstWindow->assertNoEvents();
6545 secondWindow->assertNoEvents();
6546
6547 // The rest of the dispatch should proceed as normal
6548 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006549 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006550 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +00006551 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006552 // The first window gets MOVE and the second gets pointer up
6553 firstWindow->consumeMotionMove();
6554 secondWindow->consumeMotionUp();
6555
6556 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006557 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006558 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006559 // The first window gets nothing and the second gets up
6560 firstWindow->consumeMotionUp();
6561 secondWindow->assertNoEvents();
6562}
6563
Arthur Hungabbb9d82021-09-01 14:52:30 +00006564// This case will create two windows and one mirrored window on the default display and mirror
Prabir Pradhan367f3432024-02-13 23:05:58 +00006565// two windows on the second display. It will test if 'transferTouchGesture' works fine if we put
Arthur Hungabbb9d82021-09-01 14:52:30 +00006566// the windows info of second display before default display.
Prabir Pradhan367f3432024-02-13 23:05:58 +00006567TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00006568 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6569 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006570 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1",
6571 ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006572 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006573 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006574 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2",
6575 ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006576 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006577
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006578 sp<FakeWindowHandle> mirrorWindowInPrimary =
6579 firstWindowInPrimary->clone(ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006580 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006581
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07006582 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006583 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006584
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07006585 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006586 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006587
6588 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006589 mDispatcher->onWindowInfosChanged(
6590 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
6591 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
6592 *secondWindowInPrimary->getInfo()},
6593 {},
6594 0,
6595 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00006596
6597 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006598 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
6599 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00006600 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6601
6602 // Window should receive motion event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006603 firstWindowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006604
Prabir Pradhan367f3432024-02-13 23:05:58 +00006605 // Transfer touch
6606 ASSERT_TRUE(mDispatcher->transferTouchGesture(firstWindowInPrimary->getToken(),
6607 secondWindowInPrimary->getToken()));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006608 // The first window gets cancel.
6609 firstWindowInPrimary->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006610 secondWindowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan65455c72024-02-13 21:46:41 +00006611 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006612
6613 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006614 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006615 ui::LogicalDisplayId::DEFAULT, {150, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00006616 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6617 firstWindowInPrimary->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006618 secondWindowInPrimary->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan65455c72024-02-13 21:46:41 +00006619 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006620
6621 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006622 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Arthur Hungabbb9d82021-09-01 14:52:30 +00006623 {150, 50}))
6624 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6625 firstWindowInPrimary->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006626 secondWindowInPrimary->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
6627 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006628}
6629
Prabir Pradhan367f3432024-02-13 23:05:58 +00006630// Same as TransferTouch_CloneSurface, but this touch on the secondary display and use
6631// 'transferTouchOnDisplay' api.
6632TEST_F(InputDispatcherTest, TransferTouchOnDisplay_CloneSurface) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00006633 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6634 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006635 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1",
6636 ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006637 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006638 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006639 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2",
6640 ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006641 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006642
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006643 sp<FakeWindowHandle> mirrorWindowInPrimary =
6644 firstWindowInPrimary->clone(ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006645 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006646
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07006647 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006648 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006649
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07006650 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006651 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006652
6653 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006654 mDispatcher->onWindowInfosChanged(
6655 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
6656 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
6657 *secondWindowInPrimary->getInfo()},
6658 {},
6659 0,
6660 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00006661
6662 // Touch on second display.
6663 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006664 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
6665 {50, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00006666 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6667
6668 // Window should receive motion event.
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006669 firstWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006670
6671 // Transfer touch focus
Prabir Pradhan367f3432024-02-13 23:05:58 +00006672 ASSERT_TRUE(mDispatcher->transferTouchOnDisplay(secondWindowInSecondary->getToken(),
6673 SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006674
6675 // The first window gets cancel.
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006676 firstWindowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
Prabir Pradhan65455c72024-02-13 21:46:41 +00006677 secondWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID,
6678 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006679
6680 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006681 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungabbb9d82021-09-01 14:52:30 +00006682 SECOND_DISPLAY_ID, {150, 50}))
6683 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006684 firstWindowInSecondary->assertNoEvents();
Prabir Pradhan65455c72024-02-13 21:46:41 +00006685 secondWindowInSecondary->consumeMotionMove(SECOND_DISPLAY_ID,
6686 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006687
6688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006689 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00006690 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006691 firstWindowInSecondary->assertNoEvents();
Prabir Pradhan65455c72024-02-13 21:46:41 +00006692 secondWindowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006693}
6694
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006695TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006696 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006697 sp<FakeWindowHandle> window =
6698 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6699 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006700
Vishnu Nair47074b82020-08-14 11:54:47 -07006701 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006702 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006703 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006704
6705 window->consumeFocusEvent(true);
6706
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006707 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006708
6709 // Window should receive key down event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006710 window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00006711
6712 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00006713 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00006714 mFakePolicy->assertUserActivityPoked();
6715}
6716
6717TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
6718 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006719 sp<FakeWindowHandle> window =
6720 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6721 ui::LogicalDisplayId::DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00006722
6723 window->setDisableUserActivity(true);
6724 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006725 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00006726 setFocusedWindow(window);
6727
6728 window->consumeFocusEvent(true);
6729
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006730 mDispatcher->notifyKey(
6731 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
Josep del Riob3981622023-04-18 15:49:45 +00006732
6733 // Window should receive key down event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006734 window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00006735
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006736 // Should have not poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00006737 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00006738 mFakePolicy->assertUserActivityNotPoked();
6739}
6740
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006741TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceivePolicyConsumedKey) {
Josep del Riob3981622023-04-18 15:49:45 +00006742 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006743 sp<FakeWindowHandle> window =
6744 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6745 ui::LogicalDisplayId::DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00006746
6747 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006748 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00006749 setFocusedWindow(window);
6750
6751 window->consumeFocusEvent(true);
6752
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006753 mFakePolicy->setConsumeKeyBeforeDispatching(true);
6754
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006755 mDispatcher->notifyKey(
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006756 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
Josep del Riob3981622023-04-18 15:49:45 +00006757 mDispatcher->waitForIdle();
6758
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006759 // Key is not passed down
Josep del Riob3981622023-04-18 15:49:45 +00006760 window->assertNoEvents();
6761
6762 // Should have poked user activity
6763 mFakePolicy->assertUserActivityPoked();
6764}
6765
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006766TEST_F(InputDispatcherTest, FocusedWindow_PolicyConsumedKeyIgnoresDisableUserActivity) {
Josep del Riob3981622023-04-18 15:49:45 +00006767 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006768 sp<FakeWindowHandle> window =
6769 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6770 ui::LogicalDisplayId::DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00006771
6772 window->setDisableUserActivity(true);
6773 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006774 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00006775 setFocusedWindow(window);
6776
6777 window->consumeFocusEvent(true);
6778
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006779 mFakePolicy->setConsumeKeyBeforeDispatching(true);
6780
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006781 mDispatcher->notifyKey(
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006782 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
Josep del Riob3981622023-04-18 15:49:45 +00006783 mDispatcher->waitForIdle();
6784
6785 // System key is not passed down
6786 window->assertNoEvents();
6787
6788 // Should have poked user activity
6789 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006790}
6791
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006792class DisableUserActivityInputDispatcherTest : public InputDispatcherTest,
6793 public ::testing::WithParamInterface<bool> {};
6794
6795TEST_P(DisableUserActivityInputDispatcherTest, NotPassedToUserUserActivity) {
6796 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6797 sp<FakeWindowHandle> window =
6798 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6799 ui::LogicalDisplayId::DEFAULT);
6800
6801 window->setDisableUserActivity(GetParam());
6802
6803 window->setFocusable(true);
6804 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
6805 setFocusedWindow(window);
6806
6807 window->consumeFocusEvent(true);
6808
6809 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
6810 .keyCode(AKEYCODE_A)
6811 .policyFlags(0)
6812 .build());
6813 mDispatcher->waitForIdle();
6814
6815 // Key is not passed down
6816 window->assertNoEvents();
6817
6818 // Should not have poked user activity
6819 mFakePolicy->assertUserActivityNotPoked();
6820}
6821
6822INSTANTIATE_TEST_CASE_P(DisableUserActivity, DisableUserActivityInputDispatcherTest,
6823 ::testing::Bool());
6824
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07006825TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
6826 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006827 sp<FakeWindowHandle> window =
6828 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6829 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07006830
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006831 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07006832
6833 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006834 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006835 ui::LogicalDisplayId::DEFAULT, {100, 100}))
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07006836 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6837
6838 window->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006839 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07006840
6841 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00006842 mDispatcher->waitForIdle();
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07006843 mFakePolicy->assertUserActivityPoked();
6844}
6845
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006846TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006847 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006848 sp<FakeWindowHandle> window =
6849 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6850 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006851
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006852 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006853
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006854 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006855 mDispatcher->waitForIdle();
6856
6857 window->assertNoEvents();
6858}
6859
6860// If a window is touchable, but does not have focus, it should receive motion events, but not keys
6861TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07006862 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006863 sp<FakeWindowHandle> window =
6864 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6865 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006866
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006867 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006868
6869 // Send key
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006870 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006871 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00006872 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006873 AINPUT_SOURCE_TOUCHSCREEN,
6874 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006875
6876 // Window should receive only the motion event
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006877 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006878 window->assertNoEvents(); // Key event or focus event will not be received
6879}
6880
arthurhungea3f4fc2020-12-21 23:18:53 +08006881TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
6882 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6883
arthurhungea3f4fc2020-12-21 23:18:53 +08006884 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006885 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006886 ui::LogicalDisplayId::DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08006887 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08006888
arthurhungea3f4fc2020-12-21 23:18:53 +08006889 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006890 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006891 ui::LogicalDisplayId::DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08006892 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08006893
6894 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006895 mDispatcher->onWindowInfosChanged(
6896 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
arthurhungea3f4fc2020-12-21 23:18:53 +08006897
6898 PointF pointInFirst = {300, 200};
6899 PointF pointInSecond = {300, 600};
6900
6901 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006902 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006903 AINPUT_SOURCE_TOUCHSCREEN,
6904 ui::LogicalDisplayId::DEFAULT, {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08006905 // Only the first window should get the down event
6906 firstWindow->consumeMotionDown();
6907 secondWindow->assertNoEvents();
6908
6909 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006910 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006911 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +00006912 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08006913 // The first window gets a move and the second a down
6914 firstWindow->consumeMotionMove();
6915 secondWindow->consumeMotionDown();
6916
6917 // Send pointer cancel to the second window
6918 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006919 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
6920 ui::LogicalDisplayId::DEFAULT, {pointInFirst, pointInSecond});
arthurhungea3f4fc2020-12-21 23:18:53 +08006921 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00006922 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08006923 // The first window gets move and the second gets cancel.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006924 firstWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
6925 secondWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
arthurhungea3f4fc2020-12-21 23:18:53 +08006926
6927 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00006928 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006929 ui::LogicalDisplayId::DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08006930 // The first window gets up and the second gets nothing.
6931 firstWindow->consumeMotionUp();
6932 secondWindow->assertNoEvents();
6933}
6934
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006935TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
6936 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6937
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006938 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
6939 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006940 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006941 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
6942 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
6943 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
6944
Harry Cutts33476232023-01-30 19:57:29 +00006945 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006946 window->assertNoEvents();
6947 mDispatcher->waitForIdle();
6948}
6949
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006950using InputDispatcherMonitorTest = InputDispatcherTest;
6951
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006952/**
6953 * Two entities that receive touch: A window, and a global monitor.
6954 * The touch goes to the window, and then the window disappears.
6955 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
6956 * for the monitor, as well.
6957 * 1. foregroundWindow
6958 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
6959 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006960TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006961 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006962 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground",
6963 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006964
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006965 FakeMonitorReceiver monitor =
6966 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006967
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006968 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006969 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006970 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
6971 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006972 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6973
6974 // Both the foreground window and the global monitor should receive the touch down
6975 window->consumeMotionDown();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006976 monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006977
6978 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006979 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006980 ui::LogicalDisplayId::DEFAULT, {110, 200}))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006981 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6982
6983 window->consumeMotionMove();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006984 monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006985
6986 // Now the foreground window goes away
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006987 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006988 window->consumeMotionCancel();
6989 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
6990
6991 // If more events come in, there will be no more foreground window to send them to. This will
6992 // cause a cancel for the monitor, as well.
6993 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006994 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006995 ui::LogicalDisplayId::DEFAULT, {120, 200}))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006996 << "Injection should fail because the window was removed";
6997 window->assertNoEvents();
6998 // Global monitor now gets the cancel
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006999 monitor.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00007000}
7001
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007002TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07007003 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007004 sp<FakeWindowHandle> window =
7005 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
7006 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007007 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00007008
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007009 FakeMonitorReceiver monitor =
7010 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00007011
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007012 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007013 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7014 ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007015 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007016 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
7017 monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00007018}
7019
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007020TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007021 FakeMonitorReceiver monitor =
7022 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00007023
Chris Yea209fde2020-07-22 13:54:51 -07007024 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007025 sp<FakeWindowHandle> window =
7026 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
7027 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007028 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00007029
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007030 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007031 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7032 ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007033 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007034 monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
7035 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00007036
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007037 // Pilfer pointers from the monitor.
7038 // This should not do anything and the window should continue to receive events.
7039 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00007040
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007041 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007042 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007043 ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007044 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007045
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007046 monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT);
7047 window->consumeMotionMove(ui::LogicalDisplayId::DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00007048}
7049
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007050TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07007051 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007052 sp<FakeWindowHandle> window =
7053 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
7054 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007055 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Evan Rosky84f07f02021-04-16 10:42:42 -07007056 window->setWindowOffset(20, 40);
7057 window->setWindowTransform(0, 1, -1, 0);
7058
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007059 FakeMonitorReceiver monitor =
7060 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07007061
7062 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007063 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7064 ui::LogicalDisplayId::DEFAULT))
Evan Rosky84f07f02021-04-16 10:42:42 -07007065 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007066 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007067 std::unique_ptr<MotionEvent> event = monitor.consumeMotion();
7068 ASSERT_NE(nullptr, event);
Evan Rosky84f07f02021-04-16 10:42:42 -07007069 // Even though window has transform, gesture monitor must not.
7070 ASSERT_EQ(ui::Transform(), event->getTransform());
7071}
7072
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007073TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00007074 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007075 FakeMonitorReceiver monitor =
7076 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00007077
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007078 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007079 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7080 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007081 << "Injection should fail if there is a monitor, but no touchable window";
7082 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00007083}
7084
Linnan Lid8150952024-01-26 18:07:17 +00007085/**
7086 * Two displays
7087 * The first monitor has a foreground window, a monitor
7088 * The second window has only one monitor.
7089 * We first inject a Down event into the first display, this injection should succeed and both
7090 * the foreground window and monitor should receive a down event, then inject a Down event into
7091 * the second display as well, this injection should fail, at this point, the first display
7092 * window and monitor should not receive a cancel or any other event.
7093 * Continue to inject Move and UP events to the first display, the events should be received
7094 * normally by the foreground window and monitor.
7095 */
7096TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) {
7097 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007098 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground",
7099 ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007100
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007101 FakeMonitorReceiver monitor =
7102 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007103 FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
7104
7105 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
7106 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007107 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7108 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Linnan Lid8150952024-01-26 18:07:17 +00007109 << "The down event injected into the first display should succeed";
7110
7111 window->consumeMotionDown();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007112 monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007113
7114 ASSERT_EQ(InputEventInjectionResult::FAILED,
7115 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
7116 {100, 200}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007117 << "The down event injected into the second display should fail since there's no "
7118 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00007119
7120 // Continue to inject event to first display.
7121 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7122 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007123 ui::LogicalDisplayId::DEFAULT, {110, 220}))
Linnan Lid8150952024-01-26 18:07:17 +00007124 << "The move event injected into the first display should succeed";
7125
7126 window->consumeMotionMove();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007127 monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007128
7129 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007130 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Linnan Lid8150952024-01-26 18:07:17 +00007131 {110, 220}))
7132 << "The up event injected into the first display should succeed";
7133
7134 window->consumeMotionUp();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007135 monitor.consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007136
7137 window->assertNoEvents();
7138 monitor.assertNoEvents();
Linnan Lid8150952024-01-26 18:07:17 +00007139 secondMonitor.assertNoEvents();
7140}
7141
7142/**
7143 * Two displays
7144 * There is a monitor and foreground window on each display.
7145 * First, we inject down events into each of the two displays, at this point, the foreground windows
7146 * and monitors on both displays should receive down events.
7147 * At this point, the foreground window of the second display goes away, the gone window should
7148 * receive the cancel event, and the other windows and monitors should not receive any events.
7149 * Inject a move event into the second display. At this point, the injection should fail because
7150 * the second display no longer has a foreground window. At this point, the monitor on the second
7151 * display should receive a cancel event, and any windows or monitors on the first display should
7152 * not receive any events, and any subsequent injection of events into the second display should
7153 * also fail.
7154 * Continue to inject events into the first display, and the events should all be injected
7155 * successfully and received normally.
7156 */
7157TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCancelWhenAnotherDisplayMonitorTouchCanceled) {
7158 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007159 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground",
7160 ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007161 sp<FakeWindowHandle> secondWindow =
7162 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondForeground",
7163 SECOND_DISPLAY_ID);
7164
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007165 FakeMonitorReceiver monitor =
7166 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007167 FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
7168
7169 // There is a foreground window on both displays.
7170 mDispatcher->onWindowInfosChanged({{*window->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
7171 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007172 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7173 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Linnan Lid8150952024-01-26 18:07:17 +00007174 << "The down event injected into the first display should succeed";
7175
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007176 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
7177 monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007178
7179 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7180 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
7181 {100, 200}))
7182 << "The down event injected into the second display should succeed";
7183
Linnan Lid8150952024-01-26 18:07:17 +00007184 secondWindow->consumeMotionDown(SECOND_DISPLAY_ID);
7185 secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID);
7186
7187 // Now second window is gone away.
7188 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
7189
7190 // The gone window should receive a cancel, and the monitor on the second display should not
7191 // receive any events.
Linnan Lid8150952024-01-26 18:07:17 +00007192 secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID);
7193 secondMonitor.assertNoEvents();
7194
7195 ASSERT_EQ(InputEventInjectionResult::FAILED,
7196 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7197 SECOND_DISPLAY_ID, {110, 220}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007198 << "The move event injected into the second display should fail because there's no "
7199 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00007200 // Now the monitor on the second display should receive a cancel event.
7201 secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID);
Linnan Lid8150952024-01-26 18:07:17 +00007202
7203 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7204 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007205 ui::LogicalDisplayId::DEFAULT, {110, 200}))
Linnan Lid8150952024-01-26 18:07:17 +00007206 << "The move event injected into the first display should succeed";
7207
7208 window->consumeMotionMove();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007209 monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007210
7211 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007212 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
7213 {110, 220}))
7214 << "The up event injected into the second display should fail because there's no "
7215 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00007216
7217 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007218 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Linnan Lid8150952024-01-26 18:07:17 +00007219 {110, 220}))
7220 << "The up event injected into the first display should succeed";
7221
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007222 window->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
7223 monitor.consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007224
Linnan Lid8150952024-01-26 18:07:17 +00007225 window->assertNoEvents();
7226 monitor.assertNoEvents();
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007227 secondWindow->assertNoEvents();
7228 secondMonitor.assertNoEvents();
Linnan Lid8150952024-01-26 18:07:17 +00007229}
7230
7231/**
7232 * One display with transform
7233 * There is a foreground window and a monitor on the display
7234 * Inject down event and move event sequentially, the foreground window and monitor can receive down
7235 * event and move event, then let the foreground window go away, the foreground window receives
7236 * cancel event, inject move event again, the monitor receives cancel event, all the events received
7237 * by the monitor should be with the same transform as the display
7238 */
7239TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) {
7240 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007241 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground",
7242 ui::LogicalDisplayId::DEFAULT);
7243 FakeMonitorReceiver monitor =
7244 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007245
7246 ui::Transform transform;
7247 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
7248
7249 gui::DisplayInfo displayInfo;
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007250 displayInfo.displayId = ui::LogicalDisplayId::DEFAULT;
Linnan Lid8150952024-01-26 18:07:17 +00007251 displayInfo.transform = transform;
7252
7253 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
7254
7255 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007256 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7257 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Linnan Lid8150952024-01-26 18:07:17 +00007258 << "The down event injected should succeed";
7259
7260 window->consumeMotionDown();
7261 std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion();
7262 EXPECT_EQ(transform, downMotionEvent->getTransform());
7263 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction());
7264
7265 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7266 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007267 ui::LogicalDisplayId::DEFAULT, {110, 220}))
Linnan Lid8150952024-01-26 18:07:17 +00007268 << "The move event injected should succeed";
7269
7270 window->consumeMotionMove();
7271 std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion();
7272 EXPECT_EQ(transform, moveMotionEvent->getTransform());
7273 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction());
7274
7275 // Let foreground window gone
7276 mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0});
7277
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007278 // Foreground window should receive a cancel event, but not the monitor.
Linnan Lid8150952024-01-26 18:07:17 +00007279 window->consumeMotionCancel();
Linnan Lid8150952024-01-26 18:07:17 +00007280
7281 ASSERT_EQ(InputEventInjectionResult::FAILED,
7282 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007283 ui::LogicalDisplayId::DEFAULT, {110, 220}))
Linnan Lid8150952024-01-26 18:07:17 +00007284 << "The move event injected should failed";
7285 // Now foreground should not receive any events, but monitor should receive a cancel event
7286 // with transform that same as display's display.
Linnan Lid8150952024-01-26 18:07:17 +00007287 std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion();
7288 EXPECT_EQ(transform, cancelMotionEvent->getTransform());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007289 EXPECT_EQ(ui::LogicalDisplayId::DEFAULT, cancelMotionEvent->getDisplayId());
Linnan Lid8150952024-01-26 18:07:17 +00007290 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction());
7291
7292 // Other event inject to this display should fail.
7293 ASSERT_EQ(InputEventInjectionResult::FAILED,
7294 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007295 ui::LogicalDisplayId::DEFAULT, {110, 220}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007296 << "The up event injected should fail because the touched window was removed";
Linnan Lid8150952024-01-26 18:07:17 +00007297 window->assertNoEvents();
7298 monitor.assertNoEvents();
7299}
7300
chaviw81e2bb92019-12-18 15:03:51 -08007301TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07007302 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007303 sp<FakeWindowHandle> window =
7304 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
7305 ui::LogicalDisplayId::DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08007306
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007307 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
chaviw81e2bb92019-12-18 15:03:51 -08007308
7309 NotifyMotionArgs motionArgs =
7310 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007311 ui::LogicalDisplayId::DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08007312
Prabir Pradhan678438e2023-04-13 19:32:51 +00007313 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08007314 // Window should receive motion down event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007315 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08007316
7317 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08007318 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08007319 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
7320 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
7321 motionArgs.pointerCoords[0].getX() - 10);
7322
Prabir Pradhan678438e2023-04-13 19:32:51 +00007323 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007324 window->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08007325}
7326
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007327/**
7328 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
7329 * the device default right away. In the test scenario, we check both the default value,
7330 * and the action of enabling / disabling.
7331 */
7332TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07007333 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007334 sp<FakeWindowHandle> window =
7335 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
7336 ui::LogicalDisplayId::DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08007337 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007338
7339 // Set focused application.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007340 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07007341 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007342
7343 SCOPED_TRACE("Check default value of touch mode");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007344 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007345 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007346 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007347
7348 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07007349 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007350 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00007351 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007352
7353 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08007354 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007355 /*hasPermission=*/true, ui::LogicalDisplayId::DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007356 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07007357 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007358 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007359 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007360 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007361
7362 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07007363 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007364 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00007365 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007366
7367 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08007368 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007369 /*hasPermission=*/true, ui::LogicalDisplayId::DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007370 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07007371 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007372 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007373 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007374 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007375
7376 window->assertNoEvents();
7377}
7378
Gang Wange9087892020-01-07 12:17:14 -05007379TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07007380 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007381 sp<FakeWindowHandle> window =
7382 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
7383 ui::LogicalDisplayId::DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05007384
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007385 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07007386 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05007387
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007388 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007389 setFocusedWindow(window);
7390
Harry Cutts33476232023-01-30 19:57:29 +00007391 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05007392
Prabir Pradhan678438e2023-04-13 19:32:51 +00007393 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
7394 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05007395
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007396 std::unique_ptr<KeyEvent> event = window->consumeKey();
7397 ASSERT_NE(event, nullptr);
7398 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
Gang Wange9087892020-01-07 12:17:14 -05007399 ASSERT_NE(verified, nullptr);
7400 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
7401
7402 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
7403 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
7404 ASSERT_EQ(keyArgs.source, verified->source);
7405 ASSERT_EQ(keyArgs.displayId, verified->displayId);
7406
7407 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
7408
7409 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05007410 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08007411 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05007412 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
7413 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
7414 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
7415 ASSERT_EQ(0, verifiedKey.repeatCount);
7416}
7417
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007418TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07007419 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007420 sp<FakeWindowHandle> window =
7421 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
7422 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007423
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007424 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007425
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07007426 ui::Transform transform;
7427 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
7428
7429 gui::DisplayInfo displayInfo;
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007430 displayInfo.displayId = ui::LogicalDisplayId::DEFAULT;
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07007431 displayInfo.transform = transform;
7432
Patrick Williamsd828f302023-04-28 17:52:08 -05007433 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007434
Prabir Pradhan678438e2023-04-13 19:32:51 +00007435 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007436 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007437 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007438 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007439
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007440 std::unique_ptr<MotionEvent> event = window->consumeMotionEvent();
7441 ASSERT_NE(nullptr, event);
7442 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007443 ASSERT_NE(verified, nullptr);
7444 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
7445
7446 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
7447 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
7448 EXPECT_EQ(motionArgs.source, verified->source);
7449 EXPECT_EQ(motionArgs.displayId, verified->displayId);
7450
7451 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
7452
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07007453 const vec2 rawXY =
7454 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
7455 motionArgs.pointerCoords[0].getXYValue());
7456 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
7457 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007458 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007459 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08007460 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007461 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
7462 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
7463}
7464
chaviw09c8d2d2020-08-24 15:48:26 -07007465/**
7466 * Ensure that separate calls to sign the same data are generating the same key.
7467 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
7468 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
7469 * tests.
7470 */
7471TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
7472 KeyEvent event = getTestKeyEvent();
7473 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
7474
7475 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
7476 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
7477 ASSERT_EQ(hmac1, hmac2);
7478}
7479
7480/**
7481 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
7482 */
7483TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
7484 KeyEvent event = getTestKeyEvent();
7485 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
7486 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
7487
7488 verifiedEvent.deviceId += 1;
7489 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7490
7491 verifiedEvent.source += 1;
7492 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7493
7494 verifiedEvent.eventTimeNanos += 1;
7495 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7496
Linnan Li13bf76a2024-05-05 19:18:02 +08007497 verifiedEvent.displayId = ui::LogicalDisplayId{verifiedEvent.displayId.val() + 1};
chaviw09c8d2d2020-08-24 15:48:26 -07007498 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7499
7500 verifiedEvent.action += 1;
7501 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7502
7503 verifiedEvent.downTimeNanos += 1;
7504 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7505
7506 verifiedEvent.flags += 1;
7507 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7508
7509 verifiedEvent.keyCode += 1;
7510 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7511
7512 verifiedEvent.scanCode += 1;
7513 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7514
7515 verifiedEvent.metaState += 1;
7516 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7517
7518 verifiedEvent.repeatCount += 1;
7519 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7520}
7521
Vishnu Nair958da932020-08-21 17:12:37 -07007522TEST_F(InputDispatcherTest, SetFocusedWindow) {
7523 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007524 sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top",
7525 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007526 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007527 sp<FakeWindowHandle>::make(application, mDispatcher, "Second",
7528 ui::LogicalDisplayId::DEFAULT);
7529 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair958da932020-08-21 17:12:37 -07007530
7531 // Top window is also focusable but is not granted focus.
7532 windowTop->setFocusable(true);
7533 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007534 mDispatcher->onWindowInfosChanged(
7535 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007536 setFocusedWindow(windowSecond);
7537
7538 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007539 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007540 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007541
7542 // Focused window should receive event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007543 windowSecond->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -07007544 windowTop->assertNoEvents();
7545}
7546
7547TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
7548 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007549 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow",
7550 ui::LogicalDisplayId::DEFAULT);
7551 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair958da932020-08-21 17:12:37 -07007552
7553 window->setFocusable(true);
7554 // Release channel for window is no longer valid.
7555 window->releaseChannel();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007556 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007557 setFocusedWindow(window);
7558
7559 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007560 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07007561
7562 // window channel is invalid, so it should not receive any input event.
7563 window->assertNoEvents();
7564}
7565
7566TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
7567 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007568 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow",
7569 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007570 window->setFocusable(false);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007571 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair958da932020-08-21 17:12:37 -07007572
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007573 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007574 setFocusedWindow(window);
7575
7576 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007577 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07007578
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007579 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07007580 window->assertNoEvents();
7581}
7582
7583TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
7584 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007585 sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top",
7586 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007587 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007588 sp<FakeWindowHandle>::make(application, mDispatcher, "Second",
7589 ui::LogicalDisplayId::DEFAULT);
7590 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair958da932020-08-21 17:12:37 -07007591
7592 windowTop->setFocusable(true);
7593 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007594 mDispatcher->onWindowInfosChanged(
7595 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007596 setFocusedWindow(windowTop);
7597 windowTop->consumeFocusEvent(true);
7598
Chavi Weingarten847e8512023-03-29 00:26:09 +00007599 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007600 mDispatcher->onWindowInfosChanged(
7601 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007602 windowSecond->consumeFocusEvent(true);
7603 windowTop->consumeFocusEvent(false);
7604
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007605 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007606 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007607
7608 // Focused window should receive event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007609 windowSecond->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -07007610}
7611
Chavi Weingarten847e8512023-03-29 00:26:09 +00007612TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07007613 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007614 sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top",
7615 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007616 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007617 sp<FakeWindowHandle>::make(application, mDispatcher, "Second",
7618 ui::LogicalDisplayId::DEFAULT);
7619 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair958da932020-08-21 17:12:37 -07007620
7621 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00007622 windowSecond->setFocusable(false);
7623 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007624 mDispatcher->onWindowInfosChanged(
7625 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Chavi Weingarten847e8512023-03-29 00:26:09 +00007626 setFocusedWindow(windowTop);
7627 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07007628
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007629 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Chavi Weingarten847e8512023-03-29 00:26:09 +00007630 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007631
7632 // Event should be dropped.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007633 windowTop->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -07007634 windowSecond->assertNoEvents();
7635}
7636
7637TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
7638 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007639 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow",
7640 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007641 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007642 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007643 ui::LogicalDisplayId::DEFAULT);
7644 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair958da932020-08-21 17:12:37 -07007645
7646 window->setFocusable(true);
7647 previousFocusedWindow->setFocusable(true);
7648 window->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007649 mDispatcher->onWindowInfosChanged(
7650 {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007651 setFocusedWindow(previousFocusedWindow);
7652 previousFocusedWindow->consumeFocusEvent(true);
7653
7654 // Requesting focus on invisible window takes focus from currently focused window.
7655 setFocusedWindow(window);
7656 previousFocusedWindow->consumeFocusEvent(false);
7657
7658 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007659 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007660 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007661 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007662
7663 // Window does not get focus event or key down.
7664 window->assertNoEvents();
7665
7666 // Window becomes visible.
7667 window->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007668 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007669
7670 // Window receives focus event.
7671 window->consumeFocusEvent(true);
7672 // Focused window receives key down.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007673 window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007674}
7675
Vishnu Nair599f1412021-06-21 10:39:58 -07007676TEST_F(InputDispatcherTest, DisplayRemoved) {
7677 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007678 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "window",
7679 ui::LogicalDisplayId::DEFAULT);
7680 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair599f1412021-06-21 10:39:58 -07007681
7682 // window is granted focus.
7683 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007684 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair599f1412021-06-21 10:39:58 -07007685 setFocusedWindow(window);
7686 window->consumeFocusEvent(true);
7687
7688 // When a display is removed window loses focus.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007689 mDispatcher->displayRemoved(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07007690 window->consumeFocusEvent(false);
7691}
7692
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007693/**
7694 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
7695 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
7696 * of the 'slipperyEnterWindow'.
7697 *
7698 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
7699 * a way so that the touched location is no longer covered by the top window.
7700 *
7701 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
7702 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
7703 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
7704 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
7705 * with ACTION_DOWN).
7706 * Thus, the touch has been transferred from the top window into the bottom window, because the top
7707 * window moved itself away from the touched location and had Flag::SLIPPERY.
7708 *
7709 * Even though the top window moved away from the touched location, it is still obscuring the bottom
7710 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
7711 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
7712 *
7713 * In this test, we ensure that the event received by the bottom window has
7714 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
7715 */
7716TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00007717 constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007718 constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1};
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007719
7720 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007721 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007722
7723 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007724 sp<FakeWindowHandle>::make(application, mDispatcher, "Top",
7725 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007726 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007727 // Make sure this one overlaps the bottom window
7728 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
7729 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
7730 // one. Windows with the same owner are not considered to be occluding each other.
7731 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
7732
7733 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007734 sp<FakeWindowHandle>::make(application, mDispatcher, "Second",
7735 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007736 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
7737
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007738 mDispatcher->onWindowInfosChanged(
7739 {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007740
7741 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00007742 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007743 AINPUT_SOURCE_TOUCHSCREEN,
7744 ui::LogicalDisplayId::DEFAULT, {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007745 slipperyExitWindow->consumeMotionDown();
7746 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007747 mDispatcher->onWindowInfosChanged(
7748 {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007749
Prabir Pradhan678438e2023-04-13 19:32:51 +00007750 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007751 AINPUT_SOURCE_TOUCHSCREEN,
7752 ui::LogicalDisplayId::DEFAULT, {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007753
7754 slipperyExitWindow->consumeMotionCancel();
7755
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007756 slipperyEnterWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007757 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
7758}
7759
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07007760/**
7761 * Two windows, one on the left and another on the right. The left window is slippery. The right
7762 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
7763 * touch moves from the left window into the right window, the gesture should continue to go to the
7764 * left window. Touch shouldn't slip because the right window can't receive touches. This test
7765 * reproduces a crash.
7766 */
7767TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
7768 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7769
7770 sp<FakeWindowHandle> leftSlipperyWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007771 sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
7772 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07007773 leftSlipperyWindow->setSlippery(true);
7774 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
7775
7776 sp<FakeWindowHandle> rightDropTouchesWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007777 sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
7778 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07007779 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
7780 rightDropTouchesWindow->setDropInput(true);
7781
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007782 mDispatcher->onWindowInfosChanged(
7783 {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07007784
7785 // Start touch in the left window
7786 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7787 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
7788 .build());
7789 leftSlipperyWindow->consumeMotionDown();
7790
7791 // And move it into the right window
7792 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7793 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
7794 .build());
7795
7796 // Since the right window isn't eligible to receive input, touch does not slip.
7797 // The left window continues to receive the gesture.
7798 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
7799 rightDropTouchesWindow->assertNoEvents();
7800}
7801
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07007802/**
7803 * A single window is on screen first. Touch is injected into that window. Next, a second window
7804 * appears. Since the first window is slippery, touch will move from the first window to the second.
7805 */
7806TEST_F(InputDispatcherTest, InjectedTouchSlips) {
7807 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7808 sp<FakeWindowHandle> originalWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007809 sp<FakeWindowHandle>::make(application, mDispatcher, "Original",
7810 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07007811 originalWindow->setFrame(Rect(0, 0, 200, 200));
7812 originalWindow->setSlippery(true);
7813
7814 sp<FakeWindowHandle> appearingWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007815 sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing",
7816 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07007817 appearingWindow->setFrame(Rect(0, 0, 200, 200));
7818
7819 mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0});
7820
7821 // Touch down on the original window
7822 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7823 injectMotionEvent(*mDispatcher,
7824 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7825 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
7826 .build()));
7827 originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
7828
7829 // Now, a new window appears. This could be, for example, a notification shade that appears
7830 // after user starts to drag down on the launcher window.
7831 mDispatcher->onWindowInfosChanged(
7832 {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0});
7833 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7834 injectMotionEvent(*mDispatcher,
7835 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7836 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110))
7837 .build()));
7838 originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
7839 appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
7840 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7841 injectMotionEvent(*mDispatcher,
7842 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7843 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
7844 .build()));
7845 appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
7846
7847 originalWindow->assertNoEvents();
7848 appearingWindow->assertNoEvents();
7849}
7850
Linnan Li49b2b202024-04-12 12:46:40 +08007851/**
7852 * Three windows:
7853 * - left window, which has FLAG_SLIPPERY, so it supports slippery exit
7854 * - right window
7855 * - spy window
7856 * The three windows do not overlap.
7857 *
7858 * We have two devices reporting events:
7859 * - Device A reports ACTION_DOWN, which lands in the left window
7860 * - Device B reports ACTION_DOWN, which lands in the spy window.
7861 * - Now, device B reports ACTION_MOVE events which move to the right window.
7862 *
7863 * The right window should not receive any events because the spy window is not a foreground window,
7864 * and also it does not support slippery touches.
7865 */
7866TEST_F(InputDispatcherTest, MultiDeviceSpyWindowSlipTest) {
7867 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7868 sp<FakeWindowHandle> leftWindow =
7869 sp<FakeWindowHandle>::make(application, mDispatcher, "Left window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007870 ui::LogicalDisplayId::DEFAULT);
Linnan Li49b2b202024-04-12 12:46:40 +08007871 leftWindow->setFrame(Rect(0, 0, 100, 100));
7872 leftWindow->setSlippery(true);
7873
7874 sp<FakeWindowHandle> rightWindow =
7875 sp<FakeWindowHandle>::make(application, mDispatcher, "Right window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007876 ui::LogicalDisplayId::DEFAULT);
Linnan Li49b2b202024-04-12 12:46:40 +08007877 rightWindow->setFrame(Rect(100, 0, 200, 100));
7878
7879 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007880 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window",
7881 ui::LogicalDisplayId::DEFAULT);
Linnan Li49b2b202024-04-12 12:46:40 +08007882 spyWindow->setFrame(Rect(200, 0, 300, 100));
7883 spyWindow->setSpy(true);
7884 spyWindow->setTrustedOverlay(true);
7885
7886 mDispatcher->onWindowInfosChanged(
7887 {{*leftWindow->getInfo(), *rightWindow->getInfo(), *spyWindow->getInfo()}, {}, 0, 0});
7888
7889 const DeviceId deviceA = 9;
7890 const DeviceId deviceB = 3;
7891
7892 // Tap on left window with device A
7893 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7894 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
7895 .deviceId(deviceA)
7896 .build());
7897 leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA)));
7898
7899 // Tap on spy window with device B
7900 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7901 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50))
7902 .deviceId(deviceB)
7903 .build());
7904 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB)));
7905
7906 // Move to right window with device B. Touches should not slip to the right window, because spy
7907 // window is not a foreground window, and it does not have FLAG_SLIPPERY
7908 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7909 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
7910 .deviceId(deviceB)
7911 .build());
7912 leftWindow->assertNoEvents();
7913 rightWindow->assertNoEvents();
7914 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB)));
7915}
7916
7917/**
7918 * Three windows arranged horizontally and without any overlap.
7919 * The left and right windows have FLAG_SLIPPERY. The middle window does not have any special flags.
7920 *
7921 * We have two devices reporting events:
7922 * - Device A reports ACTION_DOWN which lands in the left window
7923 * - Device B reports ACTION_DOWN which lands in the right window
7924 * - Device B reports ACTION_MOVE that shifts to the middle window.
7925 * This should cause touches for Device B to slip from the right window to the middle window.
7926 * The right window should receive ACTION_CANCEL for device B and the
7927 * middle window should receive down event for Device B.
7928 * If device B reports more ACTION_MOVE events, the middle window should receive remaining events.
7929 */
7930TEST_F(InputDispatcherTest, MultiDeviceSlipperyWindowTest) {
Siarhei Vishniakoudd56df12024-05-20 14:56:38 -07007931 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
Linnan Li49b2b202024-04-12 12:46:40 +08007932 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7933 sp<FakeWindowHandle> leftWindow =
7934 sp<FakeWindowHandle>::make(application, mDispatcher, "Left window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007935 ui::LogicalDisplayId::DEFAULT);
Linnan Li49b2b202024-04-12 12:46:40 +08007936 leftWindow->setFrame(Rect(0, 0, 100, 100));
7937 leftWindow->setSlippery(true);
7938
7939 sp<FakeWindowHandle> middleWindow =
7940 sp<FakeWindowHandle>::make(application, mDispatcher, "middle window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007941 ui::LogicalDisplayId::DEFAULT);
Linnan Li49b2b202024-04-12 12:46:40 +08007942 middleWindow->setFrame(Rect(100, 0, 200, 100));
7943
7944 sp<FakeWindowHandle> rightWindow =
7945 sp<FakeWindowHandle>::make(application, mDispatcher, "Right window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007946 ui::LogicalDisplayId::DEFAULT);
Linnan Li49b2b202024-04-12 12:46:40 +08007947 rightWindow->setFrame(Rect(200, 0, 300, 100));
7948 rightWindow->setSlippery(true);
7949
7950 mDispatcher->onWindowInfosChanged(
7951 {{*leftWindow->getInfo(), *middleWindow->getInfo(), *rightWindow->getInfo()},
7952 {},
7953 0,
7954 0});
7955
7956 const DeviceId deviceA = 9;
7957 const DeviceId deviceB = 3;
7958
7959 // Tap on left window with device A
7960 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7961 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
7962 .deviceId(deviceA)
7963 .build());
7964 leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA)));
7965
7966 // Tap on right window with device B
7967 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7968 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50))
7969 .deviceId(deviceB)
7970 .build());
7971 rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB)));
7972
7973 // Move to middle window with device B. Touches should slip to middle window, because right
7974 // window is a foreground window that's associated with device B and has FLAG_SLIPPERY.
7975 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7976 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
7977 .deviceId(deviceB)
7978 .build());
7979 rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB)));
7980 middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB)));
7981
7982 // Move to middle window with device A. Touches should slip to middle window, because left
7983 // window is a foreground window that's associated with device A and has FLAG_SLIPPERY.
7984 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7985 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
7986 .deviceId(deviceA)
7987 .build());
7988 leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceA)));
7989 middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA)));
7990
7991 // Ensure that middle window can receive the remaining move events.
7992 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7993 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51))
7994 .deviceId(deviceB)
7995 .build());
7996 leftWindow->assertNoEvents();
7997 middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB)));
7998 rightWindow->assertNoEvents();
7999}
8000
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008001TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008002 using Uid = gui::Uid;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008003 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
8004
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008005 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
8006 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008007 leftWindow->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00008008 leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008009
8010 sp<FakeWindowHandle> rightSpy =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008011 sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy",
8012 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008013 rightSpy->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00008014 rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008015 rightSpy->setSpy(true);
8016 rightSpy->setTrustedOverlay(true);
8017
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008018 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
8019 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008020 rightWindow->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00008021 rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008022
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008023 mDispatcher->onWindowInfosChanged(
8024 {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008025
8026 // Touch in the left window
8027 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8028 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8029 .build());
8030 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown());
8031 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008032 ASSERT_NO_FATAL_FAILURE(
8033 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008034
8035 // Touch another finger over the right windows
8036 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8037 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8038 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
8039 .build());
8040 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown());
8041 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown());
8042 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove());
8043 mDispatcher->waitForIdle();
8044 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008045 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID,
8046 {Uid{101}, Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008047
8048 // Release finger over left window. The UP actions are not treated as device interaction.
8049 // The windows that did not receive the UP pointer will receive MOVE events, but since this
8050 // is part of the UP action, we do not treat this as device interaction.
8051 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
8052 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8053 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
8054 .build());
8055 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp());
8056 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
8057 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
8058 mDispatcher->waitForIdle();
8059 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
8060
8061 // Move remaining finger
8062 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8063 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
8064 .build());
8065 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
8066 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
8067 mDispatcher->waitForIdle();
8068 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008069 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008070
8071 // Release all fingers
8072 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
8073 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
8074 .build());
8075 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp());
8076 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp());
8077 mDispatcher->waitForIdle();
8078 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
8079}
8080
8081TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) {
8082 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
8083
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008084 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
8085 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008086 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00008087 window->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008088
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008089 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008090 setFocusedWindow(window);
8091 ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true));
8092
8093 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008094 ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008095 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00008096 ASSERT_NO_FATAL_FAILURE(
8097 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008098
8099 // The UP actions are not treated as device interaction.
8100 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008101 ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00008102 mDispatcher->waitForIdle();
8103 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
8104}
8105
Prabir Pradhan5893d362023-11-17 04:30:40 +00008106TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) {
8107 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
8108
8109 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008110 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan5893d362023-11-17 04:30:40 +00008111 left->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008112 sp<FakeWindowHandle> right =
8113 sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window",
8114 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan5893d362023-11-17 04:30:40 +00008115 right->setFrame(Rect(100, 0, 200, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008116 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window",
8117 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan5893d362023-11-17 04:30:40 +00008118 spy->setFrame(Rect(0, 0, 200, 100));
8119 spy->setTrustedOverlay(true);
8120 spy->setSpy(true);
8121
8122 mDispatcher->onWindowInfosChanged(
8123 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
8124
8125 // Send hover move to the left window, and ensure hover enter is synthesized with a new eventId.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008126 NotifyMotionArgs notifyArgs =
8127 generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
8128 ui::LogicalDisplayId::DEFAULT, {PointF{50, 50}});
Prabir Pradhan5893d362023-11-17 04:30:40 +00008129 mDispatcher->notifyMotion(notifyArgs);
8130
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008131 std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent(
Prabir Pradhan5893d362023-11-17 04:30:40 +00008132 AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)),
8133 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008134 ASSERT_NE(nullptr, leftEnter);
Prabir Pradhan5893d362023-11-17 04:30:40 +00008135 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
8136 Not(WithEventId(notifyArgs.id)),
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008137 Not(WithEventId(leftEnter->getId())),
Prabir Pradhan5893d362023-11-17 04:30:40 +00008138 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
8139
8140 // Send move to the right window, and ensure hover exit and enter are synthesized with new ids.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008141 notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
8142 ui::LogicalDisplayId::DEFAULT, {PointF{150, 50}});
Prabir Pradhan5893d362023-11-17 04:30:40 +00008143 mDispatcher->notifyMotion(notifyArgs);
8144
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008145 std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent(
Prabir Pradhan5893d362023-11-17 04:30:40 +00008146 AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)),
8147 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008148 ASSERT_NE(nullptr, leftExit);
Prabir Pradhan5893d362023-11-17 04:30:40 +00008149 right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
8150 Not(WithEventId(notifyArgs.id)),
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008151 Not(WithEventId(leftExit->getId())),
Prabir Pradhan5893d362023-11-17 04:30:40 +00008152 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
8153
8154 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id)));
8155}
8156
Linnan Liccf6ce32024-04-11 20:32:13 +08008157/**
8158 * When a device reports a DOWN event, which lands in a window that supports splits, and then the
8159 * device then reports a POINTER_DOWN, which lands in the location of a non-existing window, then
8160 * the previous window should receive this event and not be dropped.
8161 */
8162TEST_F(InputDispatcherMultiDeviceTest, SingleDevicePointerDownEventRetentionWithoutWindowTarget) {
8163 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008164 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
8165 ui::LogicalDisplayId::DEFAULT);
Linnan Liccf6ce32024-04-11 20:32:13 +08008166 window->setFrame(Rect(0, 0, 100, 100));
8167 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
8168
8169 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8170 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8171 .build());
8172
8173 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN)));
8174
8175 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8176 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8177 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
8178 .build());
8179
8180 window->consumeMotionEvent(AllOf(WithMotionAction(POINTER_1_DOWN)));
8181}
8182
8183/**
8184 * When deviceA reports a DOWN event, which lands in a window that supports splits, and then deviceB
8185 * also reports a DOWN event, which lands in the location of a non-existing window, then the
8186 * previous window should receive deviceB's event and it should be dropped.
8187 */
8188TEST_F(InputDispatcherMultiDeviceTest, SecondDeviceDownEventDroppedWithoutWindowTarget) {
8189 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008190 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
8191 ui::LogicalDisplayId::DEFAULT);
Linnan Liccf6ce32024-04-11 20:32:13 +08008192 window->setFrame(Rect(0, 0, 100, 100));
8193 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
8194
8195 const DeviceId deviceA = 9;
8196 const DeviceId deviceB = 3;
8197
8198 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8199 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8200 .deviceId(deviceA)
8201 .build());
8202 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA)));
8203
8204 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8205 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
8206 .deviceId(deviceB)
8207 .build());
8208 window->assertNoEvents();
8209}
8210
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00008211class InputDispatcherFallbackKeyTest : public InputDispatcherTest {
8212protected:
8213 std::shared_ptr<FakeApplicationHandle> mApp;
8214 sp<FakeWindowHandle> mWindow;
8215
8216 virtual void SetUp() override {
8217 InputDispatcherTest::SetUp();
8218
8219 mApp = std::make_shared<FakeApplicationHandle>();
8220
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008221 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window",
8222 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00008223 mWindow->setFrame(Rect(0, 0, 100, 100));
8224
8225 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
8226 setFocusedWindow(mWindow);
8227 ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true));
8228 }
8229
8230 void setFallback(int32_t keycode) {
8231 mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) {
8232 return KeyEventBuilder(event).keyCode(keycode).build();
8233 });
8234 }
8235
8236 void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008237 std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled);
8238 ASSERT_NE(nullptr, event);
8239 ASSERT_THAT(*event, matcher);
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00008240 }
8241};
8242
8243TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) {
8244 mDispatcher->notifyKey(
8245 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8246 consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A)));
8247 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
8248}
8249
8250TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) {
8251 mDispatcher->notifyKey(
8252 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8253 consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A)));
8254 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8255}
8256
8257TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) {
8258 mDispatcher->notifyKey(
8259 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8260
8261 // Do not handle this key event.
8262 consumeKey(/*handled=*/false,
8263 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8264 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8265
8266 // Since the policy did not request any fallback to be generated, ensure there are no events.
8267 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
8268}
8269
8270TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) {
8271 setFallback(AKEYCODE_B);
8272 mDispatcher->notifyKey(
8273 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8274
8275 // Do not handle this key event.
8276 consumeKey(/*handled=*/false,
8277 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8278
8279 // Since the key was not handled, ensure the fallback event was dispatched instead.
8280 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8281 consumeKey(/*handled=*/true,
8282 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8283 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8284
8285 // Release the original key, and ensure the fallback key is also released.
8286 mDispatcher->notifyKey(
8287 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8288 consumeKey(/*handled=*/false,
8289 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8290 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8291 consumeKey(/*handled=*/true,
8292 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
8293 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8294
8295 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
8296 mWindow->assertNoEvents();
8297}
8298
8299TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) {
8300 setFallback(AKEYCODE_B);
8301 mDispatcher->notifyKey(
8302 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8303
8304 // Do not handle this key event, but handle the fallback.
8305 consumeKey(/*handled=*/false,
8306 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8307 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8308 consumeKey(/*handled=*/true,
8309 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8310 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8311
8312 // Release the original key, and ensure the fallback key is also released.
8313 mDispatcher->notifyKey(
8314 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8315 // But this time, the app handles the original key.
8316 consumeKey(/*handled=*/true,
8317 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8318 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8319 // Ensure the fallback key is canceled.
8320 consumeKey(/*handled=*/true,
8321 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
8322 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
8323
8324 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
8325 mWindow->assertNoEvents();
8326}
8327
8328TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) {
8329 setFallback(AKEYCODE_B);
8330 mDispatcher->notifyKey(
8331 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8332
8333 // Do not handle this key event.
8334 consumeKey(/*handled=*/false,
8335 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8336 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8337 // App does not handle the fallback either, so ensure another fallback is not generated.
8338 setFallback(AKEYCODE_C);
8339 consumeKey(/*handled=*/false,
8340 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8341 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8342
8343 // Release the original key, and ensure the fallback key is also released.
8344 setFallback(AKEYCODE_B);
8345 mDispatcher->notifyKey(
8346 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8347 consumeKey(/*handled=*/false,
8348 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8349 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8350 consumeKey(/*handled=*/false,
8351 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
8352 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8353
8354 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
8355 mWindow->assertNoEvents();
8356}
8357
8358TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) {
8359 setFallback(AKEYCODE_B);
8360 mDispatcher->notifyKey(
8361 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8362
8363 // Do not handle this key event, so fallback is generated.
8364 consumeKey(/*handled=*/false,
8365 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8366 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8367 consumeKey(/*handled=*/true,
8368 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8369 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8370
8371 // Release the original key, but assume the policy is misbehaving and it
8372 // generates an inconsistent fallback to the one from the DOWN event.
8373 setFallback(AKEYCODE_C);
8374 mDispatcher->notifyKey(
8375 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8376 consumeKey(/*handled=*/false,
8377 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8378 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8379 // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency.
8380 consumeKey(/*handled=*/true,
8381 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
8382 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
8383
8384 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
8385 mWindow->assertNoEvents();
8386}
8387
8388TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) {
8389 setFallback(AKEYCODE_B);
8390 mDispatcher->notifyKey(
8391 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8392
8393 // Do not handle this key event, so fallback is generated.
8394 consumeKey(/*handled=*/false,
8395 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8396 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8397 consumeKey(/*handled=*/true,
8398 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8399 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8400
8401 // The original key is canceled.
8402 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD)
8403 .keyCode(AKEYCODE_A)
8404 .addFlag(AKEY_EVENT_FLAG_CANCELED)
8405 .build());
8406 consumeKey(/*handled=*/false,
8407 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A),
8408 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
8409 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8410 // Ensure the fallback key is also canceled due to the original key being canceled.
8411 consumeKey(/*handled=*/true,
8412 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
8413 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
8414
8415 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
8416 mWindow->assertNoEvents();
8417}
8418
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00008419TEST_F(InputDispatcherFallbackKeyTest, InputChannelRemovedDuringPolicyCall) {
Prabir Pradhanb13da8f2024-01-09 23:10:13 +00008420 setFallback(AKEYCODE_B);
8421 mDispatcher->notifyKey(
8422 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8423
8424 // Do not handle this key event.
8425 consumeKey(/*handled=*/false,
8426 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8427 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8428 consumeKey(/*handled=*/true,
8429 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8430 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8431
8432 mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) {
8433 // When the unhandled key is reported to the policy next, remove the input channel.
8434 mDispatcher->removeInputChannel(mWindow->getToken());
8435 return KeyEventBuilder(event).keyCode(AKEYCODE_B).build();
8436 });
8437 // Release the original key, and let the app now handle the previously unhandled key.
8438 // This should result in the previously generated fallback key to be cancelled.
8439 // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified
8440 // of the UP event for consistency. The Dispatcher calls into the policy from its own thread
8441 // without holding the lock, because it need to synchronously fetch the fallback key. While in
8442 // the policy call, we will now remove the input channel. Once the policy call returns, the
8443 // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does
8444 // not cause any crashes.
8445 mDispatcher->notifyKey(
8446 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8447 consumeKey(/*handled=*/true,
8448 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8449 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8450}
8451
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00008452TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) {
8453 setFallback(AKEYCODE_B);
8454 mDispatcher->notifyKey(
8455 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8456
8457 // Do not handle this key event.
8458 consumeKey(/*handled=*/false,
8459 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8460 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8461 consumeKey(/*handled=*/true,
8462 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8463 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8464
8465 mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) {
8466 // When the unhandled key is reported to the policy next, remove the window.
8467 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
8468 return KeyEventBuilder(event).keyCode(AKEYCODE_B).build();
8469 });
8470 // Release the original key, which the app will not handle. When this unhandled key is reported
8471 // to the policy, the window will be removed.
8472 mDispatcher->notifyKey(
8473 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8474 consumeKey(/*handled=*/false,
8475 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8476 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8477
8478 // Since the window was removed, it loses focus, and the channel state will be reset.
8479 consumeKey(/*handled=*/true,
8480 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
8481 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
8482 mWindow->consumeFocusEvent(false);
8483 mWindow->assertNoEvents();
8484}
8485
8486TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedWhileAwaitingFinishedSignal) {
8487 setFallback(AKEYCODE_B);
8488 mDispatcher->notifyKey(
8489 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8490
8491 // Do not handle this key event.
8492 consumeKey(/*handled=*/false,
8493 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8494 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8495 const auto [seq, event] = mWindow->receiveEvent();
8496 ASSERT_TRUE(seq.has_value() && event != nullptr) << "Failed to receive fallback event";
8497 ASSERT_EQ(event->getType(), InputEventType::KEY);
8498 ASSERT_THAT(static_cast<const KeyEvent&>(*event),
8499 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8500 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8501
8502 // Remove the window now, which should generate a cancellations and make the window lose focus.
8503 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
8504 consumeKey(/*handled=*/true,
8505 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A),
8506 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
8507 consumeKey(/*handled=*/true,
8508 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
8509 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
8510 mWindow->consumeFocusEvent(false);
8511
8512 // Finish the event by reporting it as handled.
8513 mWindow->finishEvent(*seq);
8514 mWindow->assertNoEvents();
8515}
8516
Garfield Tan1c7bc862020-01-28 13:24:04 -08008517class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
8518protected:
Siarhei Vishniakoufa2a0492023-11-14 13:13:18 -08008519 static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms;
8520 static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms;
Garfield Tan1c7bc862020-01-28 13:24:04 -08008521
Chris Yea209fde2020-07-22 13:54:51 -07008522 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08008523 sp<FakeWindowHandle> mWindow;
8524
8525 virtual void SetUp() override {
Prabir Pradhandae52792023-12-15 07:36:40 +00008526 InputDispatcherTest::SetUp();
Garfield Tan1c7bc862020-01-28 13:24:04 -08008527
Prabir Pradhandae52792023-12-15 07:36:40 +00008528 mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008529 setUpWindow();
8530 }
8531
8532 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07008533 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008534 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window",
8535 ui::LogicalDisplayId::DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008536
Vishnu Nair47074b82020-08-14 11:54:47 -07008537 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008538 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008539 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008540 mWindow->consumeFocusEvent(true);
8541 }
8542
Chris Ye2ad95392020-09-01 13:44:44 -07008543 void sendAndConsumeKeyDown(int32_t deviceId) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008544 NotifyKeyArgs keyArgs =
8545 generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07008546 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08008547 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00008548 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008549
8550 // Window should receive key down event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008551 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008552 }
8553
8554 void expectKeyRepeatOnce(int32_t repeatCount) {
8555 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008556 mWindow->consumeKeyEvent(
8557 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount)));
Garfield Tan1c7bc862020-01-28 13:24:04 -08008558 }
8559
Chris Ye2ad95392020-09-01 13:44:44 -07008560 void sendAndConsumeKeyUp(int32_t deviceId) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008561 NotifyKeyArgs keyArgs =
8562 generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07008563 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08008564 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00008565 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008566
8567 // Window should receive key down event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008568 mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00008569 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008570 }
Hu Guofe3c8f12023-09-22 17:20:15 +08008571
8572 void injectKeyRepeat(int32_t repeatCount) {
8573 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008574 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, repeatCount,
8575 ui::LogicalDisplayId::DEFAULT))
Hu Guofe3c8f12023-09-22 17:20:15 +08008576 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8577 }
Garfield Tan1c7bc862020-01-28 13:24:04 -08008578};
8579
8580TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00008581 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07008582 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
8583 expectKeyRepeatOnce(repeatCount);
8584 }
8585}
8586
8587TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00008588 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07008589 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
8590 expectKeyRepeatOnce(repeatCount);
8591 }
Harry Cutts33476232023-01-30 19:57:29 +00008592 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07008593 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08008594 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
8595 expectKeyRepeatOnce(repeatCount);
8596 }
8597}
8598
8599TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00008600 sendAndConsumeKeyDown(/*deviceId=*/1);
8601 expectKeyRepeatOnce(/*repeatCount=*/1);
8602 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07008603 mWindow->assertNoEvents();
8604}
8605
8606TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00008607 sendAndConsumeKeyDown(/*deviceId=*/1);
8608 expectKeyRepeatOnce(/*repeatCount=*/1);
8609 sendAndConsumeKeyDown(/*deviceId=*/2);
8610 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07008611 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00008612 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07008613 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00008614 expectKeyRepeatOnce(/*repeatCount=*/2);
8615 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07008616 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00008617 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07008618 mWindow->assertNoEvents();
8619}
8620
8621TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00008622 sendAndConsumeKeyDown(/*deviceId=*/1);
8623 expectKeyRepeatOnce(/*repeatCount=*/1);
8624 sendAndConsumeKeyDown(/*deviceId=*/2);
8625 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07008626 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00008627 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07008628 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08008629 mWindow->assertNoEvents();
8630}
8631
liushenxiang42232912021-05-21 20:24:09 +08008632TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
8633 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00008634 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00008635 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008636 mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT,
liushenxiang42232912021-05-21 20:24:09 +08008637 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
8638 mWindow->assertNoEvents();
8639}
8640
Garfield Tan1c7bc862020-01-28 13:24:04 -08008641TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00008642 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00008643 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008644 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008645 std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey();
8646 ASSERT_NE(nullptr, repeatEvent);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008647 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008648 IdGenerator::getSource(repeatEvent->getId()));
Garfield Tan1c7bc862020-01-28 13:24:04 -08008649 }
8650}
8651
8652TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00008653 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00008654 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008655
8656 std::unordered_set<int32_t> idSet;
8657 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008658 std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey();
8659 ASSERT_NE(nullptr, repeatEvent);
8660 int32_t id = repeatEvent->getId();
Garfield Tan1c7bc862020-01-28 13:24:04 -08008661 EXPECT_EQ(idSet.end(), idSet.find(id));
8662 idSet.insert(id);
8663 }
8664}
8665
Hu Guofe3c8f12023-09-22 17:20:15 +08008666TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_CorrectRepeatCountWhenInjectKeyRepeat) {
8667 injectKeyRepeat(0);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008668 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Hu Guofe3c8f12023-09-22 17:20:15 +08008669 for (int32_t repeatCount = 1; repeatCount <= 2; ++repeatCount) {
8670 expectKeyRepeatOnce(repeatCount);
8671 }
8672 injectKeyRepeat(1);
8673 // Expect repeatCount to be 3 instead of 1
8674 expectKeyRepeatOnce(3);
8675}
8676
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008677/* Test InputDispatcher for MultiDisplay */
8678class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
8679public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07008680 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008681 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08008682
Chris Yea209fde2020-07-22 13:54:51 -07008683 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008684 windowInPrimary = sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1",
8685 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008686
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008687 // Set focus window for primary display, but focused display would be second one.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008688 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07008689 windowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008690 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
8691
Vishnu Nair958da932020-08-21 17:12:37 -07008692 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008693 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08008694
Chris Yea209fde2020-07-22 13:54:51 -07008695 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008696 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008697 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008698 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008699 // Set focus display to second one.
8700 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
Arpit Singhb65e2bd2024-06-03 09:48:16 +00008701 mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID);
8702
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008703 // Set focus window for second display.
8704 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07008705 windowInSecondary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008706 mDispatcher->onWindowInfosChanged(
8707 {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008708 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008709 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008710 }
8711
Prabir Pradhan3608aad2019-10-02 17:08:26 -07008712 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008713 InputDispatcherTest::TearDown();
8714
Chris Yea209fde2020-07-22 13:54:51 -07008715 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008716 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07008717 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008718 windowInSecondary.clear();
8719 }
8720
8721protected:
Chris Yea209fde2020-07-22 13:54:51 -07008722 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008723 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07008724 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008725 sp<FakeWindowHandle> windowInSecondary;
8726};
8727
8728TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
8729 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008731 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8732 ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008733 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008734 windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08008735 windowInSecondary->assertNoEvents();
8736
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008737 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008738 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008739 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008740 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08008741 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08008742 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08008743}
8744
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008745TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08008746 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08008747 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008748 injectKeyDownNoRepeat(*mDispatcher, ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008749 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008750 windowInPrimary->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08008751 windowInSecondary->assertNoEvents();
8752
8753 // Test inject a key down without display id specified.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008754 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008755 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08008756 windowInPrimary->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008757 windowInSecondary->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Arthur Hungb92218b2018-08-14 12:00:21 +08008758
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008759 // Remove all windows in secondary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008760 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
Arthur Hungb92218b2018-08-14 12:00:21 +08008761
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008762 // Old focus should receive a cancel event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008763 windowInSecondary->consumeKeyUp(ui::LogicalDisplayId::INVALID, AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08008764
8765 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008766 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Arthur Hungb92218b2018-08-14 12:00:21 +08008767 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008768 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08008769 windowInSecondary->assertNoEvents();
8770}
8771
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008772// Test per-display input monitors for motion event.
8773TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08008774 FakeMonitorReceiver monitorInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008775 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08008776 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00008777 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008778
8779 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008780 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008781 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8782 ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008783 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008784 windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
8785 monitorInPrimary.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008786 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08008787 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008788
8789 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008790 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008791 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008792 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008793 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08008794 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08008795 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08008796 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008797
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08008798 // Lift up the touch from the second display
8799 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008800 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08008801 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8802 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
8803 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
8804
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008805 // Test inject a non-pointer motion event.
8806 // If specific a display, it will dispatch to the focused window of particular display,
8807 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008808 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008809 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL,
8810 ui::LogicalDisplayId::INVALID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008811 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008812 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08008813 monitorInPrimary.assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008814 windowInSecondary->consumeMotionDown(ui::LogicalDisplayId::INVALID);
8815 monitorInSecondary.consumeMotionDown(ui::LogicalDisplayId::INVALID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008816}
8817
8818// Test per-display input monitors for key event.
8819TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008820 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08008821 FakeMonitorReceiver monitorInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008822 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08008823 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00008824 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008825
8826 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008827 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008828 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008829 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08008830 monitorInPrimary.assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008831 windowInSecondary->consumeKeyDown(ui::LogicalDisplayId::INVALID);
8832 monitorInSecondary.consumeKeyDown(ui::LogicalDisplayId::INVALID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008833}
8834
Vishnu Nair958da932020-08-21 17:12:37 -07008835TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
8836 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008837 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2",
8838 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07008839 secondWindowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008840 mDispatcher->onWindowInfosChanged(
8841 {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(),
8842 *windowInSecondary->getInfo()},
8843 {},
8844 0,
8845 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008846 setFocusedWindow(secondWindowInPrimary);
8847 windowInPrimary->consumeFocusEvent(false);
8848 secondWindowInPrimary->consumeFocusEvent(true);
8849
8850 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008851 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008852 injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008853 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07008854 windowInPrimary->assertNoEvents();
8855 windowInSecondary->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008856 secondWindowInPrimary->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07008857}
8858
Arthur Hungdfd528e2021-12-08 13:23:04 +00008859TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
8860 FakeMonitorReceiver monitorInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008861 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Arthur Hungdfd528e2021-12-08 13:23:04 +00008862 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00008863 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hungdfd528e2021-12-08 13:23:04 +00008864
8865 // Test touch down on primary display.
8866 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008867 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8868 ui::LogicalDisplayId::DEFAULT))
Arthur Hungdfd528e2021-12-08 13:23:04 +00008869 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008870 windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
8871 monitorInPrimary.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hungdfd528e2021-12-08 13:23:04 +00008872
8873 // Test touch down on second display.
8874 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008875 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungdfd528e2021-12-08 13:23:04 +00008876 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8877 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
8878 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
8879
8880 // Trigger cancel touch.
8881 mDispatcher->cancelCurrentTouch();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008882 windowInPrimary->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT);
8883 monitorInPrimary.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT);
Arthur Hungdfd528e2021-12-08 13:23:04 +00008884 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
8885 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
8886
8887 // Test inject a move motion event, no window/monitor should receive the event.
8888 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008889 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008890 ui::LogicalDisplayId::DEFAULT, {110, 200}))
Arthur Hungdfd528e2021-12-08 13:23:04 +00008891 << "Inject motion event should return InputEventInjectionResult::FAILED";
8892 windowInPrimary->assertNoEvents();
8893 monitorInPrimary.assertNoEvents();
8894
8895 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008896 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00008897 SECOND_DISPLAY_ID, {110, 200}))
8898 << "Inject motion event should return InputEventInjectionResult::FAILED";
8899 windowInSecondary->assertNoEvents();
8900 monitorInSecondary.assertNoEvents();
8901}
8902
Hu Guocb134f12023-12-23 13:42:44 +00008903/**
8904 * Send a key to the primary display and to the secondary display.
8905 * Then cause the key on the primary display to be canceled by sending in a stale key.
8906 * Ensure that the key on the primary display is canceled, and that the key on the secondary display
8907 * does not get canceled.
8908 */
8909TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) {
8910 // Send a key down on primary display
8911 mDispatcher->notifyKey(
8912 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008913 .displayId(ui::LogicalDisplayId::DEFAULT)
Hu Guocb134f12023-12-23 13:42:44 +00008914 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8915 .build());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008916 windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN),
8917 WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Hu Guocb134f12023-12-23 13:42:44 +00008918 windowInSecondary->assertNoEvents();
8919
8920 // Send a key down on second display
8921 mDispatcher->notifyKey(
8922 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
8923 .displayId(SECOND_DISPLAY_ID)
8924 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8925 .build());
8926 windowInSecondary->consumeKeyEvent(
8927 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID)));
8928 windowInPrimary->assertNoEvents();
8929
8930 // Send a valid key up event on primary display that will be dropped because it is stale
8931 NotifyKeyArgs staleKeyUp =
8932 KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008933 .displayId(ui::LogicalDisplayId::DEFAULT)
Hu Guocb134f12023-12-23 13:42:44 +00008934 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8935 .build();
8936 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms;
8937 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
8938 std::this_thread::sleep_for(STALE_EVENT_TIMEOUT);
8939 mDispatcher->notifyKey(staleKeyUp);
8940
8941 // Only the key gesture corresponding to the dropped event should receive the cancel event.
8942 // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not
8943 // receive any events.
8944 windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008945 WithDisplayId(ui::LogicalDisplayId::DEFAULT),
Hu Guocb134f12023-12-23 13:42:44 +00008946 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
8947 windowInSecondary->assertNoEvents();
8948}
8949
8950/**
8951 * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events.
8952 */
8953TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) {
8954 // Send touch down on primary display.
8955 mDispatcher->notifyMotion(
8956 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8957 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008958 .displayId(ui::LogicalDisplayId::DEFAULT)
Hu Guocb134f12023-12-23 13:42:44 +00008959 .build());
8960 windowInPrimary->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008961 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Hu Guocb134f12023-12-23 13:42:44 +00008962 windowInSecondary->assertNoEvents();
8963
8964 // Send touch down on second display.
8965 mDispatcher->notifyMotion(
8966 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8967 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8968 .displayId(SECOND_DISPLAY_ID)
8969 .build());
8970 windowInPrimary->assertNoEvents();
8971 windowInSecondary->consumeMotionEvent(
8972 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID)));
8973
8974 // inject a valid MotionEvent on primary display that will be stale when it arrives.
8975 NotifyMotionArgs staleMotionUp =
8976 MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008977 .displayId(ui::LogicalDisplayId::DEFAULT)
Hu Guocb134f12023-12-23 13:42:44 +00008978 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8979 .build();
8980 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms;
8981 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
8982 std::this_thread::sleep_for(STALE_EVENT_TIMEOUT);
8983 mDispatcher->notifyMotion(staleMotionUp);
8984
8985 // For stale motion events, we let the gesture to complete. This behaviour is different from key
8986 // events, where we would cancel the current keys instead.
8987 windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP));
8988 windowInSecondary->assertNoEvents();
8989}
8990
Jackal Guof9696682018-10-05 12:23:23 +08008991class InputFilterTest : public InputDispatcherTest {
8992protected:
Linnan Li13bf76a2024-05-05 19:18:02 +08008993 void testNotifyMotion(ui::LogicalDisplayId displayId, bool expectToBeFiltered,
Prabir Pradhan81420cc2021-09-06 10:28:50 -07008994 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08008995 NotifyMotionArgs motionArgs;
8996
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008997 motionArgs =
8998 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00008999 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10009000 motionArgs =
9001 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00009002 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08009003 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08009004 if (expectToBeFiltered) {
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009005 const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue());
Prabir Pradhan81420cc2021-09-06 10:28:50 -07009006 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08009007 } else {
9008 mFakePolicy->assertFilterInputEventWasNotCalled();
9009 }
9010 }
9011
9012 void testNotifyKey(bool expectToBeFiltered) {
9013 NotifyKeyArgs keyArgs;
9014
9015 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00009016 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08009017 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00009018 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08009019 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08009020
9021 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08009022 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08009023 } else {
9024 mFakePolicy->assertFilterInputEventWasNotCalled();
9025 }
9026 }
9027};
9028
9029// Test InputFilter for MotionEvent
9030TEST_F(InputFilterTest, MotionEvent_InputFilter) {
9031 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009032 testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/false);
Harry Cutts101ee9b2023-07-06 18:04:14 +00009033 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08009034
9035 // Enable InputFilter
9036 mDispatcher->setInputFilterEnabled(true);
9037 // Test touch on both primary and second display, and check if both events are filtered.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009038 testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/true);
Harry Cutts101ee9b2023-07-06 18:04:14 +00009039 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08009040
9041 // Disable InputFilter
9042 mDispatcher->setInputFilterEnabled(false);
9043 // Test touch on both primary and second display, and check if both events aren't filtered.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009044 testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/false);
Harry Cutts101ee9b2023-07-06 18:04:14 +00009045 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08009046}
9047
9048// Test InputFilter for KeyEvent
9049TEST_F(InputFilterTest, KeyEvent_InputFilter) {
9050 // Since the InputFilter is disabled by default, check if key event aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00009051 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08009052
9053 // Enable InputFilter
9054 mDispatcher->setInputFilterEnabled(true);
9055 // Send a key event, and check if it is filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00009056 testNotifyKey(/*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08009057
9058 // Disable InputFilter
9059 mDispatcher->setInputFilterEnabled(false);
9060 // Send a key event, and check if it isn't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00009061 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08009062}
9063
Prabir Pradhan81420cc2021-09-06 10:28:50 -07009064// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
9065// logical display coordinate space.
9066TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
9067 ui::Transform firstDisplayTransform;
9068 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
9069 ui::Transform secondDisplayTransform;
9070 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
9071
9072 std::vector<gui::DisplayInfo> displayInfos(2);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009073 displayInfos[0].displayId = ui::LogicalDisplayId::DEFAULT;
Prabir Pradhan81420cc2021-09-06 10:28:50 -07009074 displayInfos[0].transform = firstDisplayTransform;
9075 displayInfos[1].displayId = SECOND_DISPLAY_ID;
9076 displayInfos[1].transform = secondDisplayTransform;
9077
Patrick Williamsd828f302023-04-28 17:52:08 -05009078 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07009079
9080 // Enable InputFilter
9081 mDispatcher->setInputFilterEnabled(true);
9082
9083 // Ensure the correct transforms are used for the displays.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009084 testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/true,
9085 firstDisplayTransform);
Harry Cutts101ee9b2023-07-06 18:04:14 +00009086 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform);
Prabir Pradhan81420cc2021-09-06 10:28:50 -07009087}
9088
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009089class InputFilterInjectionPolicyTest : public InputDispatcherTest {
9090protected:
9091 virtual void SetUp() override {
9092 InputDispatcherTest::SetUp();
9093
9094 /**
9095 * We don't need to enable input filter to test the injected event policy, but we enabled it
9096 * here to make the tests more realistic, since this policy only matters when inputfilter is
9097 * on.
9098 */
9099 mDispatcher->setInputFilterEnabled(true);
9100
9101 std::shared_ptr<InputApplicationHandle> application =
9102 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009103 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009104 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009105
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009106 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009107 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009108 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009109 setFocusedWindow(mWindow);
9110 mWindow->consumeFocusEvent(true);
9111 }
9112
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009113 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
9114 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009115 KeyEvent event;
9116
9117 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
9118 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009119 ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0,
9120 AKEYCODE_A, KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009121 const int32_t additionalPolicyFlags =
9122 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
9123 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00009124 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00009125 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009126 policyFlags | additionalPolicyFlags));
9127
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009128 mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags)));
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009129 }
9130
9131 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
9132 int32_t flags) {
9133 MotionEvent event;
9134 PointerProperties pointerProperties[1];
9135 PointerCoords pointerCoords[1];
9136 pointerProperties[0].clear();
9137 pointerProperties[0].id = 0;
9138 pointerCoords[0].clear();
9139 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
9140 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
9141
9142 ui::Transform identityTransform;
9143 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
9144 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
9145 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
9146 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
9147 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07009148 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07009149 eventTime,
Harry Cutts101ee9b2023-07-06 18:04:14 +00009150 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009151
9152 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
9153 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00009154 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00009155 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009156 policyFlags | additionalPolicyFlags));
9157
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009158 mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId)));
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009159 }
9160
9161private:
9162 sp<FakeWindowHandle> mWindow;
9163};
9164
9165TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009166 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
9167 // filter. Without it, the event will no different from a regularly injected event, and the
9168 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00009169 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
9170 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009171}
9172
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009173TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009174 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00009175 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009176 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
9177}
9178
9179TEST_F(InputFilterInjectionPolicyTest,
9180 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
9181 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00009182 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009183 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009184}
9185
9186TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00009187 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
9188 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009189}
9190
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009191class InputDispatcherUserActivityPokeTests : public InputDispatcherTest {
9192protected:
9193 virtual void SetUp() override {
9194 InputDispatcherTest::SetUp();
9195
9196 std::shared_ptr<FakeApplicationHandle> application =
9197 std::make_shared<FakeApplicationHandle>();
9198 application->setDispatchingTimeout(100ms);
9199 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009200 ui::LogicalDisplayId::DEFAULT);
Yeabkal Wubshit222d83d2024-01-24 18:00:09 +00009201 mWindow->setFrame(Rect(0, 0, 100, 100));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009202 mWindow->setDispatchingTimeout(100ms);
9203 mWindow->setFocusable(true);
9204
9205 // Set focused application.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009206 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009207
9208 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
9209 setFocusedWindow(mWindow);
9210 mWindow->consumeFocusEvent(true);
9211 }
9212
Linnan Li13bf76a2024-05-05 19:18:02 +08009213 void notifyAndConsumeMotion(int32_t action, uint32_t source, ui::LogicalDisplayId displayId,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009214 nsecs_t eventTime) {
9215 mDispatcher->notifyMotion(MotionArgsBuilder(action, source)
9216 .displayId(displayId)
9217 .eventTime(eventTime)
9218 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
9219 .build());
9220 mWindow->consumeMotionEvent(WithMotionAction(action));
9221 }
9222
9223private:
9224 sp<FakeWindowHandle> mWindow;
9225};
9226
9227TEST_F_WITH_FLAGS(
9228 InputDispatcherUserActivityPokeTests, MinPokeTimeObserved,
9229 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
9230 rate_limit_user_activity_poke_in_dispatcher))) {
9231 mDispatcher->setMinTimeBetweenUserActivityPokes(50ms);
9232
9233 // First event of type TOUCH. Should poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009234 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009235 milliseconds_to_nanoseconds(50));
9236 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009237 {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH,
9238 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009239
9240 // 80ns > 50ns has passed since previous TOUCH event. Should poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009241 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009242 milliseconds_to_nanoseconds(130));
9243 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009244 {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH,
9245 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009246
9247 // First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event).
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009248 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
9249 ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(135));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009250 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009251 {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER,
9252 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009253
9254 // Within 50ns of previous TOUCH event. Should NOT poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009255 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009256 milliseconds_to_nanoseconds(140));
9257 mFakePolicy->assertUserActivityNotPoked();
9258
9259 // Within 50ns of previous OTHER event. Should NOT poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009260 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
9261 ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(150));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009262 mFakePolicy->assertUserActivityNotPoked();
9263
9264 // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke.
9265 // Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009266 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009267 milliseconds_to_nanoseconds(160));
9268 mFakePolicy->assertUserActivityNotPoked();
9269
9270 // 65ns > 50ns has passed since previous OTHER event. Should poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009271 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
9272 ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(200));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009273 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009274 {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER,
9275 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009276
9277 // 170ns > 50ns has passed since previous TOUCH event. Should poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009278 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009279 milliseconds_to_nanoseconds(300));
9280 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009281 {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH,
9282 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009283
9284 // Assert that there's no more user activity poke event.
9285 mFakePolicy->assertUserActivityNotPoked();
9286}
9287
9288TEST_F_WITH_FLAGS(
9289 InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed,
9290 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
9291 rate_limit_user_activity_poke_in_dispatcher))) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009292 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009293 milliseconds_to_nanoseconds(200));
9294 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009295 {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH,
9296 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009297
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009298 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009299 milliseconds_to_nanoseconds(280));
9300 mFakePolicy->assertUserActivityNotPoked();
9301
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009302 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009303 milliseconds_to_nanoseconds(340));
9304 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009305 {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH,
9306 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009307}
9308
9309TEST_F_WITH_FLAGS(
9310 InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting,
9311 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
9312 rate_limit_user_activity_poke_in_dispatcher))) {
9313 mDispatcher->setMinTimeBetweenUserActivityPokes(0ms);
9314
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009315 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
9316 20);
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009317 mFakePolicy->assertUserActivityPoked();
9318
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009319 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
9320 30);
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009321 mFakePolicy->assertUserActivityPoked();
9322}
9323
chaviwfd6d3512019-03-25 13:23:49 -07009324class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07009325 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07009326 InputDispatcherTest::SetUp();
9327
Chris Yea209fde2020-07-22 13:54:51 -07009328 std::shared_ptr<FakeApplicationHandle> application =
9329 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009330 mUnfocusedWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Top",
9331 ui::LogicalDisplayId::DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07009332 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07009333
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009334 mFocusedWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Second",
9335 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08009336 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07009337
9338 // Set focused application.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009339 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07009340 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07009341
9342 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009343 mDispatcher->onWindowInfosChanged(
9344 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009345 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01009346 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07009347 }
9348
Prabir Pradhan3608aad2019-10-02 17:08:26 -07009349 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07009350 InputDispatcherTest::TearDown();
9351
9352 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08009353 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07009354 }
9355
9356protected:
9357 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08009358 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07009359 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07009360};
9361
9362// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
9363// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
9364// the onPointerDownOutsideFocus callback.
9365TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009366 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009367 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9368 ui::LogicalDisplayId::DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009369 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07009370 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07009371
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08009372 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07009373 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
9374}
9375
9376// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
9377// DOWN on the window that doesn't have focus. Ensure no window received the
9378// onPointerDownOutsideFocus callback.
9379TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009380 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009381 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009382 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009383 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07009384 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07009385
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08009386 ASSERT_TRUE(mDispatcher->waitForIdle());
9387 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07009388}
9389
9390// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
9391// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
9392TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08009393 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009394 injectKeyDownNoRepeat(*mDispatcher, ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009395 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009396 mFocusedWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07009397
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08009398 ASSERT_TRUE(mDispatcher->waitForIdle());
9399 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07009400}
9401
9402// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
9403// DOWN on the window that already has focus. Ensure no window received the
9404// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10009405TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009406 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009407 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9408 ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009409 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07009410 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07009411
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08009412 ASSERT_TRUE(mDispatcher->waitForIdle());
9413 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07009414}
9415
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08009416// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
9417// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
9418TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
9419 const MotionEvent event =
9420 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
9421 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009422 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08009423 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
9424 .build();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009425 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08009426 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009427 mUnfocusedWindow->consumeAnyMotionDown(ui::LogicalDisplayId::DEFAULT,
9428 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08009429
9430 ASSERT_TRUE(mDispatcher->waitForIdle());
9431 mFakePolicy->assertOnPointerDownWasNotCalled();
9432 // Ensure that the unfocused window did not receive any FOCUS events.
9433 mUnfocusedWindow->assertNoEvents();
9434}
9435
chaviwaf87b3e2019-10-01 16:59:28 -07009436// These tests ensures we can send touch events to a single client when there are multiple input
9437// windows that point to the same client token.
9438class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
9439 virtual void SetUp() override {
9440 InputDispatcherTest::SetUp();
9441
Chris Yea209fde2020-07-22 13:54:51 -07009442 std::shared_ptr<FakeApplicationHandle> application =
9443 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009444 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009445 ui::LogicalDisplayId::DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07009446 mWindow1->setFrame(Rect(0, 0, 100, 100));
9447
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009448 mWindow2 = mWindow1->clone(ui::LogicalDisplayId::DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07009449 mWindow2->setFrame(Rect(100, 100, 200, 200));
9450
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009451 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07009452 }
9453
9454protected:
9455 sp<FakeWindowHandle> mWindow1;
9456 sp<FakeWindowHandle> mWindow2;
9457
9458 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05009459 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07009460 vec2 vals = windowInfo->transform.transform(point.x, point.y);
9461 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07009462 }
9463
9464 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
9465 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01009466 const std::string name = window->getName();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009467 std::unique_ptr<MotionEvent> motionEvent =
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009468 window->consumeMotionEvent(WithMotionAction(expectedAction));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009469 ASSERT_NE(nullptr, motionEvent);
9470 ASSERT_EQ(points.size(), motionEvent->getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07009471
9472 for (size_t i = 0; i < points.size(); i++) {
9473 float expectedX = points[i].x;
9474 float expectedY = points[i].y;
9475
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009476 EXPECT_EQ(expectedX, motionEvent->getX(i))
chaviwaf87b3e2019-10-01 16:59:28 -07009477 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009478 << ", got " << motionEvent->getX(i);
9479 EXPECT_EQ(expectedY, motionEvent->getY(i))
chaviwaf87b3e2019-10-01 16:59:28 -07009480 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009481 << ", got " << motionEvent->getY(i);
chaviwaf87b3e2019-10-01 16:59:28 -07009482 }
9483 }
chaviw9eaa22c2020-07-01 16:21:27 -07009484
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009485 void touchAndAssertPositions(sp<FakeWindowHandle> touchedWindow, int32_t action,
9486 const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07009487 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009488 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009489 ui::LogicalDisplayId::DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07009490
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009491 consumeMotionEvent(touchedWindow, action, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07009492 }
chaviwaf87b3e2019-10-01 16:59:28 -07009493};
9494
9495TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
9496 // Touch Window 1
9497 PointF touchedPoint = {10, 10};
9498 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009499 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009500
9501 // Release touch on Window 1
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009502 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009503
9504 // Touch Window 2
9505 touchedPoint = {150, 150};
9506 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009507 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009508}
9509
chaviw9eaa22c2020-07-01 16:21:27 -07009510TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
9511 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07009512 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009513 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07009514
9515 // Touch Window 1
9516 PointF touchedPoint = {10, 10};
9517 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009518 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009519 // Release touch on Window 1
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009520 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009521
9522 // Touch Window 2
9523 touchedPoint = {150, 150};
9524 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009525 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
9526 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009527
chaviw9eaa22c2020-07-01 16:21:27 -07009528 // Update the transform so rotation is set
9529 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009530 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07009531 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009532 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009533}
9534
chaviw9eaa22c2020-07-01 16:21:27 -07009535TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009536 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009537 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009538
9539 // Touch Window 1
9540 std::vector<PointF> touchedPoints = {PointF{10, 10}};
9541 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009542 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009543
9544 // Touch Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009545 // Since this is part of the same touch gesture that has already been dispatched to Window 1,
9546 // the touch stream from Window 2 will be merged with the stream in Window 1. The merged stream
9547 // will continue to be dispatched through Window 1.
chaviw9eaa22c2020-07-01 16:21:27 -07009548 touchedPoints.push_back(PointF{150, 150});
9549 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009550 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009551
chaviw9eaa22c2020-07-01 16:21:27 -07009552 // Release Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009553 touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07009554 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009555
chaviw9eaa22c2020-07-01 16:21:27 -07009556 // Update the transform so rotation is set for Window 2
9557 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009558 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07009559 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009560 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009561}
9562
chaviw9eaa22c2020-07-01 16:21:27 -07009563TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009564 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009565 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009566
9567 // Touch Window 1
9568 std::vector<PointF> touchedPoints = {PointF{10, 10}};
9569 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009570 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009571
9572 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07009573 touchedPoints.push_back(PointF{150, 150});
9574 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009575
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009576 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009577
9578 // Move both windows
9579 touchedPoints = {{20, 20}, {175, 175}};
9580 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
9581 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
9582
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009583 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009584
chaviw9eaa22c2020-07-01 16:21:27 -07009585 // Release Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009586 touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07009587 expectedPoints.pop_back();
9588
9589 // Touch Window 2
9590 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009591 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07009592 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009593 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07009594
9595 // Move both windows
9596 touchedPoints = {{20, 20}, {175, 175}};
9597 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
9598 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
9599
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009600 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009601}
9602
9603TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
9604 mWindow1->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009605 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009606
9607 // Touch Window 1
9608 std::vector<PointF> touchedPoints = {PointF{10, 10}};
9609 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009610 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009611
9612 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07009613 touchedPoints.push_back(PointF{150, 150});
9614 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009615
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009616 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009617
9618 // Move both windows
9619 touchedPoints = {{20, 20}, {175, 175}};
9620 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
9621 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
9622
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009623 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009624}
9625
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07009626/**
9627 * When one of the windows is slippery, the touch should not slip into the other window with the
9628 * same input channel.
9629 */
9630TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
9631 mWindow1->setSlippery(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009632 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07009633
9634 // Touch down in window 1
9635 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009636 ui::LogicalDisplayId::DEFAULT, {{50, 50}}));
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07009637 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
9638
9639 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
9640 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
9641 // getting generated.
9642 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009643 ui::LogicalDisplayId::DEFAULT, {{150, 150}}));
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07009644
9645 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
9646}
9647
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009648/**
9649 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
9650 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
9651 * that the pointer is hovering over may have a different transform.
9652 */
9653TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009654 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009655
9656 // Start hover in window 1
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07009657 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN)
9658 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
9659 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009660 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
9661 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009662 // Move hover to window 2.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07009663 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
9664 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
9665 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009666 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009667 consumeMotionEvent(mWindow2, ACTION_HOVER_ENTER,
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009668 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
9669}
9670
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009671class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
9672 virtual void SetUp() override {
9673 InputDispatcherTest::SetUp();
9674
Chris Yea209fde2020-07-22 13:54:51 -07009675 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009676 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009677 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009678 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009679 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009680 mWindow->setDispatchingTimeout(100ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07009681 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009682
9683 // Set focused application.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009684 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009685
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009686 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009687 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009688 mWindow->consumeFocusEvent(true);
9689 }
9690
9691 virtual void TearDown() override {
9692 InputDispatcherTest::TearDown();
9693 mWindow.clear();
9694 }
9695
9696protected:
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009697 static constexpr std::chrono::duration SPY_TIMEOUT = 200ms;
Chris Yea209fde2020-07-22 13:54:51 -07009698 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009699 sp<FakeWindowHandle> mWindow;
9700 static constexpr PointF WINDOW_LOCATION = {20, 20};
9701
9702 void tapOnWindow() {
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08009703 const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER)
9704 .x(WINDOW_LOCATION.x)
9705 .y(WINDOW_LOCATION.y);
9706 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9707 .pointer(touchingPointer)
9708 .build());
9709 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
9710 .pointer(touchingPointer)
9711 .build());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009712 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009713
9714 sp<FakeWindowHandle> addSpyWindow() {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009715 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy",
9716 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009717 spy->setTrustedOverlay(true);
9718 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009719 spy->setSpy(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009720 spy->setDispatchingTimeout(SPY_TIMEOUT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009721 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009722 return spy;
9723 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009724};
9725
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009726// Send a tap and respond, which should not cause an ANR.
9727TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
9728 tapOnWindow();
9729 mWindow->consumeMotionDown();
9730 mWindow->consumeMotionUp();
9731 ASSERT_TRUE(mDispatcher->waitForIdle());
9732 mFakePolicy->assertNotifyAnrWasNotCalled();
9733}
9734
9735// Send a regular key and respond, which should not cause an ANR.
9736TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009737 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009738 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009739 ASSERT_TRUE(mDispatcher->waitForIdle());
9740 mFakePolicy->assertNotifyAnrWasNotCalled();
9741}
9742
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05009743TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
9744 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009745 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05009746 mWindow->consumeFocusEvent(false);
9747
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009748 InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009749 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
9750 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
9751 CONSUME_TIMEOUT_EVENT_EXPECTED,
Harry Cutts33476232023-01-30 19:57:29 +00009752 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009753 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05009754 // Key will not go to window because we have no focused window.
9755 // The 'no focused window' ANR timer should start instead.
9756
9757 // Now, the focused application goes away.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009758 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, nullptr);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05009759 // The key should get dropped and there should be no ANR.
9760
9761 ASSERT_TRUE(mDispatcher->waitForIdle());
9762 mFakePolicy->assertNotifyAnrWasNotCalled();
9763}
9764
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009765// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009766// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
9767// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009768TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009769 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009770 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9771 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009772
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009773 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009774 ASSERT_TRUE(sequenceNum);
9775 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009776 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009777
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009778 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009779 mWindow->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009780 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009781 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009782 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009783}
9784
9785// Send a key to the app and have the app not respond right away.
9786TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
9787 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009788 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009789 const auto [sequenceNum, _] = mWindow->receiveEvent();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009790 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009791 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009792 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009793 ASSERT_TRUE(mDispatcher->waitForIdle());
9794}
9795
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009796// We have a focused application, but no focused window
9797TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07009798 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009799 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009800 mWindow->consumeFocusEvent(false);
9801
9802 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009803 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009804 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9805 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009806 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
9807 mDispatcher->waitForIdle();
9808 mFakePolicy->assertNotifyAnrWasNotCalled();
9809
9810 // Once a focused event arrives, we get an ANR for this application
9811 // We specify the injection timeout to be smaller than the application timeout, to ensure that
9812 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009813 const InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009814 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
9815 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::WAIT_FOR_RESULT, 50ms,
Linnan Li13bf76a2024-05-05 19:18:02 +08009816 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009817 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009818 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07009819 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009820 ASSERT_TRUE(mDispatcher->waitForIdle());
9821}
9822
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009823/**
9824 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
9825 * there will not be an ANR.
9826 */
9827TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
9828 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009829 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009830 mWindow->consumeFocusEvent(false);
9831
9832 KeyEvent event;
Siarhei Vishniakoua7333112023-10-27 13:33:29 -07009833 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
9834 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009835 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
9836 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
9837
9838 // Define a valid key down event that is stale (too old).
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009839 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
9840 ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN,
9841 /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime,
9842 eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009843
Hu Guofe3c8f12023-09-22 17:20:15 +08009844 const int32_t policyFlags =
9845 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009846
9847 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00009848 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009849 InputEventInjectionSync::WAIT_FOR_RESULT,
9850 INJECT_EVENT_TIMEOUT, policyFlags);
9851 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
9852 << "Injection should fail because the event is stale";
9853
9854 ASSERT_TRUE(mDispatcher->waitForIdle());
9855 mFakePolicy->assertNotifyAnrWasNotCalled();
9856 mWindow->assertNoEvents();
9857}
9858
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009859// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009860// Make sure that we don't notify policy twice about the same ANR.
9861TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07009862 const std::chrono::duration appTimeout = 400ms;
9863 mApplication->setDispatchingTimeout(appTimeout);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009864 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication);
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07009865
Vishnu Nair47074b82020-08-14 11:54:47 -07009866 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009867 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009868 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009869
9870 // Once a focused event arrives, we get an ANR for this application
9871 // We specify the injection timeout to be smaller than the application timeout, to ensure that
9872 // injection times out (instead of failing).
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07009873 const std::chrono::duration eventInjectionTimeout = 100ms;
9874 ASSERT_LT(eventInjectionTimeout, appTimeout);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009875 const InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009876 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
9877 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::WAIT_FOR_RESULT,
9878 eventInjectionTimeout,
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07009879 /*allowKeyRepeat=*/false);
9880 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result)
9881 << "result=" << ftl::enum_string(result);
9882 // We already waited for 'eventInjectionTimeout`, because the countdown started when the event
9883 // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait.
9884 std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout;
9885 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009886
Vishnu Naire4df8752022-09-08 09:17:55 -07009887 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009888 // ANR should not be raised again. It is up to policy to do that if it desires.
9889 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009890
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009891 // If we now get a focused window, the ANR should stop, but the policy handles that via
9892 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009893 ASSERT_TRUE(mDispatcher->waitForIdle());
9894}
9895
9896// We have a focused application, but no focused window
9897TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07009898 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009899 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009900 mWindow->consumeFocusEvent(false);
9901
9902 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009903 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009904
Vishnu Naire4df8752022-09-08 09:17:55 -07009905 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
9906 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009907
9908 // Future focused events get dropped right away
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009909 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009910 ASSERT_TRUE(mDispatcher->waitForIdle());
9911 mWindow->assertNoEvents();
9912}
9913
9914/**
9915 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
9916 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
9917 * If we process 1 of the events, but ANR on the second event with the same timestamp,
9918 * the ANR mechanism should still work.
9919 *
9920 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
9921 * DOWN event, while not responding on the second one.
9922 */
9923TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
9924 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009925 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009926 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009927 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9928 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009929 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009930
9931 // Now send ACTION_UP, with identical timestamp
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009932 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009933 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009934 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9935 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009936 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009937
9938 // We have now sent down and up. Let's consume first event and then ANR on the second.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009939 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009940 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009941 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009942}
9943
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009944// A spy window can receive an ANR
9945TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
9946 sp<FakeWindowHandle> spy = addSpyWindow();
9947
9948 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009949 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9950 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009951 mWindow->consumeMotionDown();
9952
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009953 const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009954 ASSERT_TRUE(sequenceNum);
9955 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009956 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009957
9958 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009959 spy->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009960 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009961 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009962 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009963}
9964
9965// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009966// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009967TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
9968 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009969
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009970 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009971 injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT));
9972 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
9973 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9974 injectKeyUp(*mDispatcher, ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009975
9976 // Stuck on the ACTION_UP
9977 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009978 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009979
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009980 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009981 tapOnWindow();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009982 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
9983 spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009984
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009985 mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); // still the previous motion
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009986 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08009987 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009988 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009989 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009990}
9991
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009992// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009993// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009994TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
9995 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009996
9997 tapOnWindow();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009998 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
9999 spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010000
10001 mWindow->consumeMotionDown();
10002 // Stuck on the ACTION_UP
10003 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -080010004 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010005
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010006 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010007 tapOnWindow();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010008 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
10009 spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010010
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010011 mWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); // still the previous motion
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010012 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -080010013 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010014 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010015 spy->assertNoEvents();
10016}
10017
10018TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010019 mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010020
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010021 FakeMonitorReceiver monitor =
10022 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010023
10024 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010025 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10026 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010027
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010028 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010029 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
10030 ASSERT_TRUE(consumeSeq);
10031
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010032 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(),
10033 MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010034
10035 monitor.finishEvent(*consumeSeq);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010036 monitor.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010037
10038 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -080010039 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010040}
10041
10042// If a window is unresponsive, then you get anr. if the window later catches up and starts to
10043// process events, you don't get an anr. When the window later becomes unresponsive again, you
10044// get an ANR again.
10045// 1. tap -> block on ACTION_UP -> receive ANR
10046// 2. consume all pending events (= queue becomes healthy again)
10047// 3. tap again -> block on ACTION_UP again -> receive ANR second time
10048TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
10049 tapOnWindow();
10050
10051 mWindow->consumeMotionDown();
10052 // Block on ACTION_UP
10053 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -080010054 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010055 mWindow->consumeMotionUp(); // Now the connection should be healthy again
10056 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -080010057 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010058 mWindow->assertNoEvents();
10059
10060 tapOnWindow();
10061 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -080010062 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010063 mWindow->consumeMotionUp();
10064
10065 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -080010066 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010067 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010068 mWindow->assertNoEvents();
10069}
10070
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010071// If a connection remains unresponsive for a while, make sure policy is only notified once about
10072// it.
10073TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010074 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010075 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10076 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010077
10078 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -080010079 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010080 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010081 // 'notifyConnectionUnresponsive' should only be called once per connection
10082 mFakePolicy->assertNotifyAnrWasNotCalled();
10083 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010084 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080010085 mWindow->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010086 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010087 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010088 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -080010089 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010090 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010091}
10092
10093/**
10094 * 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 -070010095 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010096 */
10097TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010098 // The timeouts in this test are established by relying on the fact that the "key waiting for
10099 // events timeout" is equal to 500ms.
10100 ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010101 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010102 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010103
10104 tapOnWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010105 const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010106 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010107 const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010108 ASSERT_TRUE(upSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010109
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010110 // Don't finish the events yet, and send a key
10111 mDispatcher->notifyKey(
10112 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
10113 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
10114 .build());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010115 // Key will not be sent to the window, yet, because the window is still processing events
10116 // and the key remains pending, waiting for the touch events to be processed
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010117 // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR.
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010118 mWindow->assertNoEvents(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010119
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010120 std::this_thread::sleep_for(400ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010121 // if we wait long enough though, dispatcher will give up, and still send the key
10122 // to the focused window, even though we have not yet finished the motion event
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010123 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010124 mWindow->finishEvent(*downSequenceNum);
10125 mWindow->finishEvent(*upSequenceNum);
10126}
10127
10128/**
10129 * If a window is processing a motion event, and then a key event comes in, the key event should
10130 * not go to the focused window until the motion is processed.
10131 * If then a new motion comes in, then the pending key event should be going to the currently
10132 * focused window right away.
10133 */
10134TEST_F(InputDispatcherSingleWindowAnr,
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010135 PendingKey_IsDeliveredWhileMotionIsProcessingAndNewTouchComesIn) {
10136 // The timeouts in this test are established by relying on the fact that the "key waiting for
10137 // events timeout" is equal to 500ms.
10138 ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010139 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010140 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010141
10142 tapOnWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010143 const auto& [downSequenceNum, _] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010144 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010145 const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010146 ASSERT_TRUE(upSequenceNum);
10147 // Don't finish the events yet, and send a key
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -080010148 mDispatcher->notifyKey(
10149 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
10150 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
10151 .build());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010152 // At this point, key is still pending, and should not be sent to the application yet.
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010153 mWindow->assertNoEvents(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010154
10155 // Now tap down again. It should cause the pending key to go to the focused window right away.
10156 tapOnWindow();
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010157 // Now that we tapped, we should receive the key immediately.
10158 // Since there's still room for slowness, we use 200ms, which is much less than
10159 // the "key waiting for events' timeout of 500ms minus the already waited 100ms duration.
10160 std::unique_ptr<InputEvent> keyEvent = mWindow->consume(200ms);
10161 ASSERT_NE(nullptr, keyEvent);
10162 ASSERT_EQ(InputEventType::KEY, keyEvent->getType());
10163 ASSERT_THAT(static_cast<KeyEvent&>(*keyEvent), WithKeyAction(AKEY_EVENT_ACTION_DOWN));
10164 // it doesn't matter that we haven't ack'd the other events yet. We can finish events in any
10165 // order.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010166 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
10167 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -080010168 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
10169 mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010170 mWindow->assertNoEvents();
10171}
10172
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -070010173/**
10174 * Send an event to the app and have the app not respond right away.
10175 * When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
10176 * So InputDispatcher will enqueue ACTION_CANCEL event as well.
10177 * At some point, the window becomes responsive again.
10178 * Ensure that subsequent events get dropped, and the next gesture is delivered.
10179 */
10180TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) {
10181 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10182 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
10183 .build());
10184
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010185 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -070010186 ASSERT_TRUE(sequenceNum);
10187 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
10188 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
10189
10190 mWindow->finishEvent(*sequenceNum);
10191 mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
10192 ASSERT_TRUE(mDispatcher->waitForIdle());
10193 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
10194
10195 // Now that the window is responsive, let's continue the gesture.
10196 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
10197 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
10198 .build());
10199
10200 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10201 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
10202 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
10203 .build());
10204
10205 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
10206 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
10207 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
10208 .build());
10209 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
10210 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
10211 .build());
10212 // We already canceled this pointer, so the window shouldn't get any new events.
10213 mWindow->assertNoEvents();
10214
10215 // Start another one.
10216 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10217 .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15))
10218 .build());
10219 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
10220}
10221
Prabir Pradhanfc364722024-02-08 17:51:20 +000010222// Send an event to the app and have the app not respond right away. Then remove the app window.
10223// When the window is removed, the dispatcher will cancel the events for that window.
10224// So InputDispatcher will enqueue ACTION_CANCEL event as well.
10225TEST_F(InputDispatcherSingleWindowAnr, AnrAfterWindowRemoval) {
10226 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010227 AINPUT_SOURCE_TOUCHSCREEN,
10228 ui::LogicalDisplayId::DEFAULT, {WINDOW_LOCATION}));
Prabir Pradhanfc364722024-02-08 17:51:20 +000010229
10230 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
10231 ASSERT_TRUE(sequenceNum);
10232
10233 // Remove the window, but the input channel should remain alive.
10234 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
10235
10236 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
10237 // Since the window was removed, Dispatcher does not know the PID associated with the window
10238 // anymore, so the policy is notified without the PID.
10239 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken(),
10240 /*pid=*/std::nullopt);
10241
10242 mWindow->finishEvent(*sequenceNum);
10243 // The cancellation was generated when the window was removed, along with the focus event.
10244 mWindow->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010245 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Prabir Pradhanfc364722024-02-08 17:51:20 +000010246 mWindow->consumeFocusEvent(false);
10247 ASSERT_TRUE(mDispatcher->waitForIdle());
10248 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt);
10249}
10250
10251// Send an event to the app and have the app not respond right away. Wait for the policy to be
10252// notified of the unresponsive window, then remove the app window.
10253TEST_F(InputDispatcherSingleWindowAnr, AnrFollowedByWindowRemoval) {
10254 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010255 AINPUT_SOURCE_TOUCHSCREEN,
10256 ui::LogicalDisplayId::DEFAULT, {WINDOW_LOCATION}));
Prabir Pradhanfc364722024-02-08 17:51:20 +000010257
10258 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
10259 ASSERT_TRUE(sequenceNum);
10260 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
10261 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
10262
10263 // Remove the window, but the input channel should remain alive.
10264 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
10265
10266 mWindow->finishEvent(*sequenceNum);
10267 // The cancellation was generated during the ANR, and the window lost focus when it was removed.
10268 mWindow->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010269 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Prabir Pradhanfc364722024-02-08 17:51:20 +000010270 mWindow->consumeFocusEvent(false);
10271 ASSERT_TRUE(mDispatcher->waitForIdle());
10272 // Since the window was removed, Dispatcher does not know the PID associated with the window
10273 // becoming responsive, so the policy is notified without the PID.
10274 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt);
10275}
10276
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010277class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
10278 virtual void SetUp() override {
10279 InputDispatcherTest::SetUp();
10280
Chris Yea209fde2020-07-22 13:54:51 -070010281 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010282 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010283 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010284 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010285 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010286 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010287 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010288
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010289 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010290 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010291 mFocusedWindow->setDispatchingTimeout(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010292 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010293
10294 // Set focused application.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010295 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -070010296 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010297
10298 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010299 mDispatcher->onWindowInfosChanged(
10300 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010301 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010302 mFocusedWindow->consumeFocusEvent(true);
10303 }
10304
10305 virtual void TearDown() override {
10306 InputDispatcherTest::TearDown();
10307
10308 mUnfocusedWindow.clear();
10309 mFocusedWindow.clear();
10310 }
10311
10312protected:
Chris Yea209fde2020-07-22 13:54:51 -070010313 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010314 sp<FakeWindowHandle> mUnfocusedWindow;
10315 sp<FakeWindowHandle> mFocusedWindow;
10316 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
10317 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
10318 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
10319
10320 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
10321
10322 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
10323
10324private:
10325 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010326 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010327 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10328 ui::LogicalDisplayId::DEFAULT, location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010329 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010330 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10331 ui::LogicalDisplayId::DEFAULT, location));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010332 }
10333};
10334
10335// If we have 2 windows that are both unresponsive, the one with the shortest timeout
10336// should be ANR'd first.
10337TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010338 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010339 injectMotionEvent(*mDispatcher,
10340 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10341 AINPUT_SOURCE_TOUCHSCREEN)
10342 .pointer(PointerBuilder(0, ToolType::FINGER)
10343 .x(FOCUSED_WINDOW_LOCATION.x)
10344 .y(FOCUSED_WINDOW_LOCATION.y))
10345 .build()));
10346 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10347 injectMotionEvent(*mDispatcher,
10348 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
10349 AINPUT_SOURCE_TOUCHSCREEN)
10350 .pointer(PointerBuilder(0, ToolType::FINGER)
10351 .x(FOCUSED_WINDOW_LOCATION.x)
10352 .y(FOCUSED_WINDOW_LOCATION.y))
10353 .build()));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010354 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010355 mFocusedWindow->consumeMotionUp();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010356 mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010357 // We consumed all events, so no ANR
10358 ASSERT_TRUE(mDispatcher->waitForIdle());
10359 mFakePolicy->assertNotifyAnrWasNotCalled();
10360
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010361 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010362 injectMotionEvent(*mDispatcher,
10363 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10364 AINPUT_SOURCE_TOUCHSCREEN)
10365 .pointer(PointerBuilder(0, ToolType::FINGER)
10366 .x(FOCUSED_WINDOW_LOCATION.x)
10367 .y(FOCUSED_WINDOW_LOCATION.y))
10368 .build()));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010369 const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010370 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010371
10372 const std::chrono::duration timeout =
10373 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -080010374 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010375
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010376 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010377 mFocusedWindow->consumeMotionDown();
10378 // This cancel is generated because the connection was unresponsive
10379 mFocusedWindow->consumeMotionCancel();
10380 mFocusedWindow->assertNoEvents();
10381 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010382 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -080010383 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
10384 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010385 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010386}
10387
10388// If we have 2 windows with identical timeouts that are both unresponsive,
10389// it doesn't matter which order they should have ANR.
10390// But we should receive ANR for both.
10391TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
10392 // Set the timeout for unfocused window to match the focused window
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010393 mUnfocusedWindow->setDispatchingTimeout(
10394 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010395 mDispatcher->onWindowInfosChanged(
10396 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010397
10398 tapOnFocusedWindow();
10399 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010400 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010401 std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken(
10402 mFocusedWindow->getDispatchingTimeout(
10403 DISPATCHING_TIMEOUT)),
10404 mFakePolicy->getUnresponsiveWindowToken(0ms)};
10405
10406 ASSERT_THAT(anrConnectionTokens,
10407 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
10408 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010409
10410 ASSERT_TRUE(mDispatcher->waitForIdle());
10411 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010412
10413 mFocusedWindow->consumeMotionDown();
10414 mFocusedWindow->consumeMotionUp();
10415 mUnfocusedWindow->consumeMotionOutside();
10416
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010417 std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(),
10418 mFakePolicy->getResponsiveWindowToken()};
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010419
10420 // Both applications should be marked as responsive, in any order
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010421 ASSERT_THAT(responsiveTokens,
10422 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
10423 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010424 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010425}
10426
10427// If a window is already not responding, the second tap on the same window should be ignored.
10428// We should also log an error to account for the dropped event (not tested here).
10429// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
10430TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
10431 tapOnFocusedWindow();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010432 mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010433 // Receive the events, but don't respond
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010434 const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010435 ASSERT_TRUE(downEventSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010436 const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010437 ASSERT_TRUE(upEventSequenceNum);
10438 const std::chrono::duration timeout =
10439 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -080010440 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010441
10442 // Tap once again
10443 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010444 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010445 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10446 ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010447 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010448 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010449 FOCUSED_WINDOW_LOCATION));
10450 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
10451 // valid touch target
10452 mUnfocusedWindow->assertNoEvents();
10453
10454 // Consume the first tap
10455 mFocusedWindow->finishEvent(*downEventSequenceNum);
10456 mFocusedWindow->finishEvent(*upEventSequenceNum);
10457 ASSERT_TRUE(mDispatcher->waitForIdle());
10458 // The second tap did not go to the focused window
10459 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010460 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -080010461 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
10462 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010463 mFakePolicy->assertNotifyAnrWasNotCalled();
10464}
10465
10466// If you tap outside of all windows, there will not be ANR
10467TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010468 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010469 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10470 ui::LogicalDisplayId::DEFAULT, LOCATION_OUTSIDE_ALL_WINDOWS));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010471 ASSERT_TRUE(mDispatcher->waitForIdle());
10472 mFakePolicy->assertNotifyAnrWasNotCalled();
10473}
10474
10475// Since the focused window is paused, tapping on it should not produce any events
10476TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
10477 mFocusedWindow->setPaused(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010478 mDispatcher->onWindowInfosChanged(
10479 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010480
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010481 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010482 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10483 ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010484
10485 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
10486 ASSERT_TRUE(mDispatcher->waitForIdle());
10487 // Should not ANR because the window is paused, and touches shouldn't go to it
10488 mFakePolicy->assertNotifyAnrWasNotCalled();
10489
10490 mFocusedWindow->assertNoEvents();
10491 mUnfocusedWindow->assertNoEvents();
10492}
10493
10494/**
10495 * 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 -070010496 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010497 * If a different window becomes focused at this time, the key should go to that window instead.
10498 *
10499 * Warning!!!
10500 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
10501 * and the injection timeout that we specify when injecting the key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010502 * We must have the injection timeout (100ms) be smaller than
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010503 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
10504 *
10505 * If that value changes, this test should also change.
10506 */
10507TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
10508 // Set a long ANR timeout to prevent it from triggering
10509 mFocusedWindow->setDispatchingTimeout(2s);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010510 mDispatcher->onWindowInfosChanged(
10511 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010512
10513 tapOnUnfocusedWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010514 const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010515 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010516 const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010517 ASSERT_TRUE(upSequenceNum);
10518 // Don't finish the events yet, and send a key
10519 // Injection will succeed because we will eventually give up and send the key to the focused
10520 // window even if motions are still being processed.
10521
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010522 InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010523 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
10524 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
Linnan Li13bf76a2024-05-05 19:18:02 +080010525 /*injectionTimeout=*/100ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010526 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010527 // Key will not be sent to the window, yet, because the window is still processing events
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010528 // and the key remains pending, waiting for the touch events to be processed.
10529 // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED
10530 // under the hood.
10531 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
10532 mFocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010533
10534 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -070010535 mFocusedWindow->setFocusable(false);
10536 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010537 mDispatcher->onWindowInfosChanged(
10538 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010539 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010540
10541 // Focus events should precede the key events
10542 mUnfocusedWindow->consumeFocusEvent(true);
10543 mFocusedWindow->consumeFocusEvent(false);
10544
10545 // Finish the tap events, which should unblock dispatcher
10546 mUnfocusedWindow->finishEvent(*downSequenceNum);
10547 mUnfocusedWindow->finishEvent(*upSequenceNum);
10548
10549 // Now that all queues are cleared and no backlog in the connections, the key event
10550 // can finally go to the newly focused "mUnfocusedWindow".
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010551 mUnfocusedWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010552 mFocusedWindow->assertNoEvents();
10553 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010554 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010555}
10556
10557// When the touch stream is split across 2 windows, and one of them does not respond,
10558// then ANR should be raised and the touch should be canceled for the unresponsive window.
10559// The other window should not be affected by that.
10560TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
10561 // Touch Window 1
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010562 mDispatcher->notifyMotion(
10563 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
10564 ui::LogicalDisplayId::DEFAULT, {FOCUSED_WINDOW_LOCATION}));
10565 mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010566
10567 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +000010568 mDispatcher->notifyMotion(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010569 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
10570 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +000010571 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010572
10573 const std::chrono::duration timeout =
10574 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -080010575 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010576
10577 mUnfocusedWindow->consumeMotionDown();
10578 mFocusedWindow->consumeMotionDown();
10579 // Focused window may or may not receive ACTION_MOVE
10580 // But it should definitely receive ACTION_CANCEL due to the ANR
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010581 const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010582 ASSERT_TRUE(moveOrCancelSequenceNum);
10583 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
10584 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -070010585 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010586 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
10587 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
10588 mFocusedWindow->consumeMotionCancel();
10589 } else {
10590 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
10591 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010592 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -080010593 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
10594 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010595
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010596 mUnfocusedWindow->assertNoEvents();
10597 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010598 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010599}
10600
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010601/**
10602 * If we have no focused window, and a key comes in, we start the ANR timer.
10603 * The focused application should add a focused window before the timer runs out to prevent ANR.
10604 *
10605 * If the user touches another application during this time, the key should be dropped.
10606 * Next, if a new focused window comes in, without toggling the focused application,
10607 * then no ANR should occur.
10608 *
10609 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
10610 * but in some cases the policy may not update the focused application.
10611 */
10612TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
10613 std::shared_ptr<FakeApplicationHandle> focusedApplication =
10614 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouc033dfb2023-10-03 10:45:16 -070010615 focusedApplication->setDispatchingTimeout(300ms);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010616 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, focusedApplication);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010617 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
10618 mFocusedWindow->setFocusable(false);
10619
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010620 mDispatcher->onWindowInfosChanged(
10621 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010622 mFocusedWindow->consumeFocusEvent(false);
10623
10624 // Send a key. The ANR timer should start because there is no focused window.
10625 // 'focusedApplication' will get blamed if this timer completes.
10626 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010627 InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010628 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
10629 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
Linnan Li13bf76a2024-05-05 19:18:02 +080010630 /*injectionTimeout=*/100ms,
Harry Cutts33476232023-01-30 19:57:29 +000010631 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010632 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010633
10634 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
10635 // then the injected touches won't cause the focused event to get dropped.
10636 // The dispatcher only checks for whether the queue should be pruned upon queueing.
10637 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
10638 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
10639 // For this test, it means that the key would get delivered to the window once it becomes
10640 // focused.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010641 std::this_thread::sleep_for(100ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010642
10643 // Touch unfocused window. This should force the pending key to get dropped.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010644 mDispatcher->notifyMotion(
10645 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
10646 ui::LogicalDisplayId::DEFAULT, {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010647
10648 // We do not consume the motion right away, because that would require dispatcher to first
10649 // process (== drop) the key event, and by that time, ANR will be raised.
10650 // Set the focused window first.
10651 mFocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010652 mDispatcher->onWindowInfosChanged(
10653 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010654 setFocusedWindow(mFocusedWindow);
10655 mFocusedWindow->consumeFocusEvent(true);
10656 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
10657 // to another application. This could be a bug / behaviour in the policy.
10658
10659 mUnfocusedWindow->consumeMotionDown();
10660
10661 ASSERT_TRUE(mDispatcher->waitForIdle());
10662 // Should not ANR because we actually have a focused window. It was just added too slowly.
10663 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
10664}
10665
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010666/**
10667 * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having
10668 * an inconsistent event stream inside the dispatcher. In this test, we make sure that the
10669 * dispatcher doesn't prune pointer events incorrectly.
10670 *
10671 * This test reproduces a crash in InputDispatcher.
10672 * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur.
10673 *
10674 * Keep the currently focused application (mApplication), and have no focused window.
10675 * We set up two additional windows:
10676 * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the
10677 * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application
10678 * window. This window is not focusable, but is touchable.
10679 *
10680 * We first touch the navigation bar, which causes it to inject a key. Since there's no focused
10681 * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now
10682 * blocked. The dispatcher is waiting for 'mApplication' to add a focused window.
10683 *
10684 * Now, we touch "Another window". This window is owned by a different application than
10685 * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused
10686 * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start
10687 * dropping the events from its queue. Ensure that no crash occurs.
10688 *
10689 * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale.
10690 * This does not affect the test running time.
10691 */
10692TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) {
10693 std::shared_ptr<FakeApplicationHandle> systemUiApplication =
10694 std::make_shared<FakeApplicationHandle>();
10695 systemUiApplication->setDispatchingTimeout(3000ms);
10696 mFakePolicy->setStaleEventTimeout(3000ms);
10697 sp<FakeWindowHandle> navigationBar =
10698 sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010699 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010700 navigationBar->setFocusable(false);
10701 navigationBar->setWatchOutsideTouch(true);
10702 navigationBar->setFrame(Rect(0, 0, 100, 100));
10703
10704 mApplication->setDispatchingTimeout(3000ms);
10705 // 'mApplication' is already focused, but we call it again here to make it explicit.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010706 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication);
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010707
10708 std::shared_ptr<FakeApplicationHandle> anotherApplication =
10709 std::make_shared<FakeApplicationHandle>();
10710 sp<FakeWindowHandle> appWindow =
10711 sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010712 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010713 appWindow->setFocusable(false);
10714 appWindow->setFrame(Rect(100, 100, 200, 200));
10715
10716 mDispatcher->onWindowInfosChanged(
10717 {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0});
10718 // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus
10719 mFocusedWindow->consumeFocusEvent(false);
10720
10721 // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher
10722 // in response.
10723 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10724 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
10725 .build());
10726 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
10727
10728 // Key will not be sent anywhere because we have no focused window. It will remain pending.
10729 // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is.
10730 InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010731 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
10732 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
Linnan Li13bf76a2024-05-05 19:18:02 +080010733 /*injectionTimeout=*/100ms,
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010734 /*allowKeyRepeat=*/false);
10735 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
10736
10737 // Finish the gesture - lift up finger and inject ACTION_UP key event
10738 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
10739 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
10740 .build());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010741 result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0,
10742 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
Linnan Li13bf76a2024-05-05 19:18:02 +080010743 /*injectionTimeout=*/100ms,
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010744 /*allowKeyRepeat=*/false);
10745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
10746 // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be
10747 // getting any events yet.
10748 navigationBar->assertNoEvents();
10749
10750 // Now touch "Another window". This touch is going to a different application than the one we
10751 // are waiting for (which is 'mApplication').
10752 // This should cause the dispatcher to drop the pending focus-dispatched events (like the key
10753 // trying to be injected) and to continue processing the rest of the events in the original
10754 // order.
10755 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10756 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
10757 .build());
10758 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP));
10759 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE));
10760 appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
10761
10762 appWindow->assertNoEvents();
10763 navigationBar->assertNoEvents();
10764}
10765
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010766// These tests ensure we cannot send touch events to a window that's positioned behind a window
10767// that has feature NO_INPUT_CHANNEL.
10768// Layout:
10769// Top (closest to user)
10770// mNoInputWindow (above all windows)
10771// mBottomWindow
10772// Bottom (furthest from user)
10773class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
10774 virtual void SetUp() override {
10775 InputDispatcherTest::SetUp();
10776
10777 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010778 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
10779 "Window without input channel",
10780 ui::LogicalDisplayId::DEFAULT,
10781 /*createInputChannel=*/false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010782 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010783 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
10784 // It's perfectly valid for this window to not have an associated input channel
10785
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010786 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010787 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010788 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
10789
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010790 mDispatcher->onWindowInfosChanged(
10791 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010792 }
10793
10794protected:
10795 std::shared_ptr<FakeApplicationHandle> mApplication;
10796 sp<FakeWindowHandle> mNoInputWindow;
10797 sp<FakeWindowHandle> mBottomWindow;
10798};
10799
10800TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
10801 PointF touchedPoint = {10, 10};
10802
Prabir Pradhan678438e2023-04-13 19:32:51 +000010803 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010804 AINPUT_SOURCE_TOUCHSCREEN,
10805 ui::LogicalDisplayId::DEFAULT, {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010806
10807 mNoInputWindow->assertNoEvents();
10808 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
10809 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
10810 // and therefore should prevent mBottomWindow from receiving touches
10811 mBottomWindow->assertNoEvents();
10812}
10813
10814/**
10815 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
10816 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
10817 */
10818TEST_F(InputDispatcherMultiWindowOcclusionTests,
10819 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010820 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
10821 "Window with input channel and NO_INPUT_CHANNEL",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010822 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010823
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010824 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010825 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010826 mDispatcher->onWindowInfosChanged(
10827 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010828
10829 PointF touchedPoint = {10, 10};
10830
Prabir Pradhan678438e2023-04-13 19:32:51 +000010831 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010832 AINPUT_SOURCE_TOUCHSCREEN,
10833 ui::LogicalDisplayId::DEFAULT, {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010834
10835 mNoInputWindow->assertNoEvents();
10836 mBottomWindow->assertNoEvents();
10837}
10838
Vishnu Nair958da932020-08-21 17:12:37 -070010839class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
10840protected:
10841 std::shared_ptr<FakeApplicationHandle> mApp;
10842 sp<FakeWindowHandle> mWindow;
10843 sp<FakeWindowHandle> mMirror;
10844
10845 virtual void SetUp() override {
10846 InputDispatcherTest::SetUp();
10847 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010848 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow",
10849 ui::LogicalDisplayId::DEFAULT);
10850 mMirror = mWindow->clone(ui::LogicalDisplayId::DEFAULT);
10851 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp);
Vishnu Nair958da932020-08-21 17:12:37 -070010852 mWindow->setFocusable(true);
10853 mMirror->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010854 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010855 }
10856};
10857
10858TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
10859 // Request focus on a mirrored window
10860 setFocusedWindow(mMirror);
10861
10862 // window gets focused
10863 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010864 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010865 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010866 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010867}
10868
10869// A focused & mirrored window remains focused only if the window and its mirror are both
10870// focusable.
10871TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
10872 setFocusedWindow(mMirror);
10873
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +000010874 // window gets focused because it is above the mirror
Vishnu Nair958da932020-08-21 17:12:37 -070010875 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010876 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010877 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010878 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010879 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010880 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010881 mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010882
10883 mMirror->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010884 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010885
10886 // window loses focus since one of the windows associated with the token in not focusable
10887 mWindow->consumeFocusEvent(false);
10888
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010889 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010890 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -070010891 mWindow->assertNoEvents();
10892}
10893
10894// A focused & mirrored window remains focused until the window and its mirror both become
10895// invisible.
10896TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
10897 setFocusedWindow(mMirror);
10898
10899 // window gets focused
10900 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010901 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010902 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010903 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010904 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010905 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010906 mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010907
10908 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010909 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010910
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010911 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010912 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010913 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010914 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010915 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010916 mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010917
10918 mWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010919 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010920
10921 // window loses focus only after all windows associated with the token become invisible.
10922 mWindow->consumeFocusEvent(false);
10923
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010924 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010925 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -070010926 mWindow->assertNoEvents();
10927}
10928
10929// A focused & mirrored window remains focused until both windows are removed.
10930TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
10931 setFocusedWindow(mMirror);
10932
10933 // window gets focused
10934 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010935 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010936 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010937 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010938 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010939 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010940 mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010941
10942 // single window is removed but the window token remains focused
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010943 mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010944
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010945 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010946 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010947 mMirror->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010948 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010949 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010950 mMirror->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010951
10952 // Both windows are removed
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010953 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010954 mWindow->consumeFocusEvent(false);
10955
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010956 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010957 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -070010958 mWindow->assertNoEvents();
10959}
10960
10961// Focus request can be pending until one window becomes visible.
10962TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
10963 // Request focus on an invisible mirror.
10964 mWindow->setVisible(false);
10965 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010966 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010967 setFocusedWindow(mMirror);
10968
10969 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010970 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010971 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010972 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -070010973
10974 mMirror->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010975 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010976
10977 // window gets focused
10978 mWindow->consumeFocusEvent(true);
10979 // window gets the pending key event
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010980 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -070010981}
Prabir Pradhan99987712020-11-10 18:43:05 -080010982
10983class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
10984protected:
10985 std::shared_ptr<FakeApplicationHandle> mApp;
10986 sp<FakeWindowHandle> mWindow;
10987 sp<FakeWindowHandle> mSecondWindow;
10988
10989 void SetUp() override {
10990 InputDispatcherTest::SetUp();
10991 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010992 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow",
10993 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -080010994 mWindow->setFocusable(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010995 mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2",
10996 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -080010997 mSecondWindow->setFocusable(true);
10998
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010999 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011000 mDispatcher->onWindowInfosChanged(
11001 {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan99987712020-11-10 18:43:05 -080011002
11003 setFocusedWindow(mWindow);
11004 mWindow->consumeFocusEvent(true);
11005 }
11006
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000011007 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000011008 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -080011009 }
11010
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000011011 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
11012 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -080011013 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Hiroki Sato25040232024-02-22 17:21:22 +090011014 auto request = mFakePolicy->assertSetPointerCaptureCalled(window, enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000011015 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -080011016 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000011017 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -080011018 }
11019};
11020
11021TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
11022 // Ensure that capture cannot be obtained for unfocused windows.
11023 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
11024 mFakePolicy->assertSetPointerCaptureNotCalled();
11025 mSecondWindow->assertNoEvents();
11026
11027 // Ensure that capture can be enabled from the focus window.
11028 requestAndVerifyPointerCapture(mWindow, true);
11029
11030 // Ensure that capture cannot be disabled from a window that does not have capture.
11031 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
11032 mFakePolicy->assertSetPointerCaptureNotCalled();
11033
11034 // Ensure that capture can be disabled from the window with capture.
11035 requestAndVerifyPointerCapture(mWindow, false);
11036}
11037
11038TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000011039 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -080011040
11041 setFocusedWindow(mSecondWindow);
11042
11043 // Ensure that the capture disabled event was sent first.
11044 mWindow->consumeCaptureEvent(false);
11045 mWindow->consumeFocusEvent(false);
11046 mSecondWindow->consumeFocusEvent(true);
Hiroki Sato25040232024-02-22 17:21:22 +090011047 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan99987712020-11-10 18:43:05 -080011048
11049 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000011050 notifyPointerCaptureChanged({});
11051 notifyPointerCaptureChanged(request);
11052 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -080011053 mWindow->assertNoEvents();
11054 mSecondWindow->assertNoEvents();
11055 mFakePolicy->assertSetPointerCaptureNotCalled();
11056}
11057
11058TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000011059 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -080011060
11061 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000011062 notifyPointerCaptureChanged({});
11063 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -080011064
11065 // Ensure that Pointer Capture is disabled.
Hiroki Sato25040232024-02-22 17:21:22 +090011066 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan99987712020-11-10 18:43:05 -080011067 mWindow->consumeCaptureEvent(false);
11068 mWindow->assertNoEvents();
11069}
11070
Prabir Pradhan167e6d92021-02-04 16:18:17 -080011071TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
11072 requestAndVerifyPointerCapture(mWindow, true);
11073
11074 // The first window loses focus.
11075 setFocusedWindow(mSecondWindow);
Hiroki Sato25040232024-02-22 17:21:22 +090011076 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -080011077 mWindow->consumeCaptureEvent(false);
11078
11079 // Request Pointer Capture from the second window before the notification from InputReader
11080 // arrives.
11081 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Hiroki Sato25040232024-02-22 17:21:22 +090011082 auto request = mFakePolicy->assertSetPointerCaptureCalled(mSecondWindow, true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -080011083
11084 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000011085 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -080011086
11087 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000011088 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -080011089
11090 mSecondWindow->consumeFocusEvent(true);
11091 mSecondWindow->consumeCaptureEvent(true);
11092}
11093
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000011094TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
11095 // App repeatedly enables and disables capture.
11096 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
Hiroki Sato25040232024-02-22 17:21:22 +090011097 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000011098 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
Hiroki Sato25040232024-02-22 17:21:22 +090011099 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000011100 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
Hiroki Sato25040232024-02-22 17:21:22 +090011101 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000011102
11103 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
11104 // first request is now stale, this should do nothing.
11105 notifyPointerCaptureChanged(firstRequest);
11106 mWindow->assertNoEvents();
11107
11108 // InputReader notifies that the second request was enabled.
11109 notifyPointerCaptureChanged(secondRequest);
11110 mWindow->consumeCaptureEvent(true);
11111}
11112
Prabir Pradhan7092e262022-05-03 16:51:09 +000011113TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
11114 requestAndVerifyPointerCapture(mWindow, true);
11115
11116 // App toggles pointer capture off and on.
11117 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
Hiroki Sato25040232024-02-22 17:21:22 +090011118 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan7092e262022-05-03 16:51:09 +000011119
11120 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
Hiroki Sato25040232024-02-22 17:21:22 +090011121 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true);
Prabir Pradhan7092e262022-05-03 16:51:09 +000011122
11123 // InputReader notifies that the latest "enable" request was processed, while skipping over the
11124 // preceding "disable" request.
11125 notifyPointerCaptureChanged(enableRequest);
11126
11127 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
11128 // any notifications.
11129 mWindow->assertNoEvents();
11130}
11131
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011132/**
11133 * One window. Hover mouse in the window, and then start capture. Make sure that the relative
11134 * mouse movements don't affect the previous mouse hovering state.
11135 * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no
11136 * HOVER_MOVE events).
11137 */
11138TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) {
11139 // Mouse hover on the window
11140 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
11141 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
11142 .build());
11143 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
11144 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
11145 .build());
11146
11147 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER)));
11148 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE)));
11149
11150 // Start pointer capture
11151 requestAndVerifyPointerCapture(mWindow, true);
11152
11153 // Send some relative mouse movements and receive them in the window.
11154 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE)
11155 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11))
11156 .build());
11157 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11),
11158 WithSource(AINPUT_SOURCE_MOUSE_RELATIVE)));
11159
11160 // Stop pointer capture
11161 requestAndVerifyPointerCapture(mWindow, false);
11162
11163 // Continue hovering on the window
11164 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
11165 .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115))
11166 .build());
11167 mWindow->consumeMotionEvent(
11168 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
11169
11170 mWindow->assertNoEvents();
11171}
11172
Prabir Pradhan9d3d5612024-06-06 20:34:26 +000011173TEST_F(InputDispatcherPointerCaptureTests, MultiDisplayPointerCapture) {
11174 // The default display is the focused display to begin with.
11175 requestAndVerifyPointerCapture(mWindow, true);
11176
11177 // Move the second window to a second display, make it the focused window on that display.
11178 mSecondWindow->editInfo()->displayId = SECOND_DISPLAY_ID;
11179 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
11180 setFocusedWindow(mSecondWindow);
11181 mSecondWindow->consumeFocusEvent(true);
11182
11183 mWindow->assertNoEvents();
11184
11185 // The second window cannot gain capture because it is not on the focused display.
11186 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
11187 mFakePolicy->assertSetPointerCaptureNotCalled();
11188 mSecondWindow->assertNoEvents();
11189
11190 // Make the second display the focused display.
11191 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
Arpit Singhb65e2bd2024-06-03 09:48:16 +000011192 mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID);
Prabir Pradhan9d3d5612024-06-06 20:34:26 +000011193
11194 // This causes the first window to lose pointer capture, and it's unable to request capture.
11195 mWindow->consumeCaptureEvent(false);
11196 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
11197
11198 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
11199 mFakePolicy->assertSetPointerCaptureNotCalled();
11200
11201 // The second window is now able to gain pointer capture successfully.
11202 requestAndVerifyPointerCapture(mSecondWindow, true);
11203}
11204
Hiroki Sato25040232024-02-22 17:21:22 +090011205using InputDispatcherPointerCaptureDeathTest = InputDispatcherPointerCaptureTests;
11206
11207TEST_F(InputDispatcherPointerCaptureDeathTest,
11208 NotifyPointerCaptureChangedWithWrongTokenAbortsDispatcher) {
11209 testing::GTEST_FLAG(death_test_style) = "threadsafe";
11210 ScopedSilentDeath _silentDeath;
11211
11212 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
11213 auto request = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true);
11214
11215 // Dispatch a pointer changed event with a wrong token.
11216 request.window = mSecondWindow->getToken();
11217 ASSERT_DEATH(
11218 {
11219 notifyPointerCaptureChanged(request);
11220 mSecondWindow->consumeCaptureEvent(true);
11221 },
11222 "Unexpected requested window for Pointer Capture.");
11223}
11224
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011225class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
11226protected:
11227 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +000011228
11229 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
11230 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
11231
11232 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
11233 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
11234
11235 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
11236 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
11237 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
11238 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
11239 MAXIMUM_OBSCURING_OPACITY);
11240
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011241 static constexpr gui::Uid TOUCHED_APP_UID{10001};
11242 static constexpr gui::Uid APP_B_UID{10002};
11243 static constexpr gui::Uid APP_C_UID{10003};
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011244
11245 sp<FakeWindowHandle> mTouchWindow;
11246
11247 virtual void SetUp() override {
11248 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011249 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011250 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
11251 }
11252
11253 virtual void TearDown() override {
11254 InputDispatcherTest::TearDown();
11255 mTouchWindow.clear();
11256 }
11257
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011258 sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode,
chaviw3277faf2021-05-19 16:45:23 -050011259 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011260 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011261 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011262 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011263 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011264 return window;
11265 }
11266
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011267 sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011268 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
11269 sp<FakeWindowHandle> window =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011270 sp<FakeWindowHandle>::make(app, mDispatcher, name, ui::LogicalDisplayId::DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011271 // Generate an arbitrary PID based on the UID
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011272 window->setOwnerInfo(gui::Pid{static_cast<pid_t>(1777 + (uid.val() % 10000))}, uid);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011273 return window;
11274 }
11275
11276 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000011277 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011278 AINPUT_SOURCE_TOUCHSCREEN,
11279 ui::LogicalDisplayId::DEFAULT, points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011280 }
11281};
11282
11283TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011284 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011285 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011286 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011287
11288 touch();
11289
11290 mTouchWindow->assertNoEvents();
11291}
11292
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011293TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +000011294 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
11295 const sp<FakeWindowHandle>& w =
11296 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011297 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000011298
11299 touch();
11300
11301 mTouchWindow->assertNoEvents();
11302}
11303
11304TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011305 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
11306 const sp<FakeWindowHandle>& w =
11307 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011308 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011309
11310 touch();
11311
11312 w->assertNoEvents();
11313}
11314
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011315TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011316 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011317 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011318
11319 touch();
11320
11321 mTouchWindow->consumeAnyMotionDown();
11322}
11323
11324TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011325 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011326 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011327 w->setFrame(Rect(0, 0, 50, 50));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011328 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011329
11330 touch({PointF{100, 100}});
11331
11332 mTouchWindow->consumeAnyMotionDown();
11333}
11334
11335TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011336 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011337 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011338 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011339
11340 touch();
11341
11342 mTouchWindow->consumeAnyMotionDown();
11343}
11344
11345TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
11346 const sp<FakeWindowHandle>& w =
11347 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011348 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011349
11350 touch();
11351
11352 mTouchWindow->consumeAnyMotionDown();
11353}
11354
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011355TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
11356 const sp<FakeWindowHandle>& w =
11357 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011358 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011359
11360 touch();
11361
11362 w->assertNoEvents();
11363}
11364
11365/**
11366 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
11367 * inside) while letting them pass-through. Note that even though touch passes through the occluding
11368 * window, the occluding window will still receive ACTION_OUTSIDE event.
11369 */
11370TEST_F(InputDispatcherUntrustedTouchesTest,
11371 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
11372 const sp<FakeWindowHandle>& w =
11373 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011374 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011375 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011376
11377 touch();
11378
11379 w->consumeMotionOutside();
11380}
11381
11382TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
11383 const sp<FakeWindowHandle>& w =
11384 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011385 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011386 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011387
11388 touch();
11389
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011390 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011391}
11392
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011393TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011394 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011395 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11396 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011397 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011398
11399 touch();
11400
11401 mTouchWindow->consumeAnyMotionDown();
11402}
11403
11404TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
11405 const sp<FakeWindowHandle>& w =
11406 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11407 MAXIMUM_OBSCURING_OPACITY);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011408 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011409
11410 touch();
11411
11412 mTouchWindow->consumeAnyMotionDown();
11413}
11414
11415TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011416 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011417 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11418 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011419 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011420
11421 touch();
11422
11423 mTouchWindow->assertNoEvents();
11424}
11425
11426TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
11427 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
11428 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011429 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
11430 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011431 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011432 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
11433 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011434 mDispatcher->onWindowInfosChanged(
11435 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011436
11437 touch();
11438
11439 mTouchWindow->assertNoEvents();
11440}
11441
11442TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
11443 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
11444 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011445 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
11446 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011447 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011448 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
11449 OPACITY_FAR_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011450 mDispatcher->onWindowInfosChanged(
11451 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011452
11453 touch();
11454
11455 mTouchWindow->consumeAnyMotionDown();
11456}
11457
11458TEST_F(InputDispatcherUntrustedTouchesTest,
11459 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
11460 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011461 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11462 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011463 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011464 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
11465 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011466 mDispatcher->onWindowInfosChanged(
11467 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011468
11469 touch();
11470
11471 mTouchWindow->consumeAnyMotionDown();
11472}
11473
11474TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
11475 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011476 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11477 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011478 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011479 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
11480 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011481 mDispatcher->onWindowInfosChanged(
11482 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011483
11484 touch();
11485
11486 mTouchWindow->assertNoEvents();
11487}
11488
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011489TEST_F(InputDispatcherUntrustedTouchesTest,
11490 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
11491 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011492 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
11493 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011494 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011495 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11496 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011497 mDispatcher->onWindowInfosChanged(
11498 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011499
11500 touch();
11501
11502 mTouchWindow->assertNoEvents();
11503}
11504
11505TEST_F(InputDispatcherUntrustedTouchesTest,
11506 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
11507 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011508 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
11509 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011510 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011511 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11512 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011513 mDispatcher->onWindowInfosChanged(
11514 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011515
11516 touch();
11517
11518 mTouchWindow->consumeAnyMotionDown();
11519}
11520
11521TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
11522 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011523 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
11524 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011525 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011526
11527 touch();
11528
11529 mTouchWindow->consumeAnyMotionDown();
11530}
11531
11532TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
11533 const sp<FakeWindowHandle>& w =
11534 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011535 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011536
11537 touch();
11538
11539 mTouchWindow->consumeAnyMotionDown();
11540}
11541
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000011542TEST_F(InputDispatcherUntrustedTouchesTest,
11543 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
11544 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
11545 const sp<FakeWindowHandle>& w =
11546 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011547 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000011548
11549 touch();
11550
11551 mTouchWindow->assertNoEvents();
11552}
11553
11554TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
11555 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
11556 const sp<FakeWindowHandle>& w =
11557 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011558 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000011559
11560 touch();
11561
11562 mTouchWindow->consumeAnyMotionDown();
11563}
11564
11565TEST_F(InputDispatcherUntrustedTouchesTest,
11566 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
11567 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
11568 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011569 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11570 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011571 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000011572
11573 touch();
11574
11575 mTouchWindow->consumeAnyMotionDown();
11576}
11577
11578TEST_F(InputDispatcherUntrustedTouchesTest,
11579 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
11580 const sp<FakeWindowHandle>& w1 =
11581 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
11582 OPACITY_BELOW_THRESHOLD);
11583 const sp<FakeWindowHandle>& w2 =
11584 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11585 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011586 mDispatcher->onWindowInfosChanged(
11587 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000011588
11589 touch();
11590
11591 mTouchWindow->assertNoEvents();
11592}
11593
11594/**
11595 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
11596 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
11597 * (which alone would result in allowing touches) does not affect the blocking behavior.
11598 */
11599TEST_F(InputDispatcherUntrustedTouchesTest,
11600 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
11601 const sp<FakeWindowHandle>& wB =
11602 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
11603 OPACITY_BELOW_THRESHOLD);
11604 const sp<FakeWindowHandle>& wC =
11605 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
11606 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011607 mDispatcher->onWindowInfosChanged(
11608 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000011609
11610 touch();
11611
11612 mTouchWindow->assertNoEvents();
11613}
11614
11615/**
11616 * This test is testing that a window from a different UID but with same application token doesn't
11617 * block the touch. Apps can share the application token for close UI collaboration for example.
11618 */
11619TEST_F(InputDispatcherUntrustedTouchesTest,
11620 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
11621 const sp<FakeWindowHandle>& w =
11622 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
11623 w->setApplicationToken(mTouchWindow->getApplicationToken());
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011624 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000011625
11626 touch();
11627
11628 mTouchWindow->consumeAnyMotionDown();
11629}
11630
arthurhungb89ccb02020-12-30 16:19:01 +080011631class InputDispatcherDragTests : public InputDispatcherTest {
11632protected:
11633 std::shared_ptr<FakeApplicationHandle> mApp;
11634 sp<FakeWindowHandle> mWindow;
11635 sp<FakeWindowHandle> mSecondWindow;
11636 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011637 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011638 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
11639 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +080011640
11641 void SetUp() override {
11642 InputDispatcherTest::SetUp();
11643 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011644 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow",
11645 ui::LogicalDisplayId::DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +080011646 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +080011647
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011648 mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2",
11649 ui::LogicalDisplayId::DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +080011650 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +080011651
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011652 mSpyWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow",
11653 ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011654 mSpyWindow->setSpy(true);
11655 mSpyWindow->setTrustedOverlay(true);
11656 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
11657
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011658 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011659 mDispatcher->onWindowInfosChanged(
11660 {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()},
11661 {},
11662 0,
11663 0});
arthurhungb89ccb02020-12-30 16:19:01 +080011664 }
11665
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011666 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
11667 switch (fromSource) {
11668 case AINPUT_SOURCE_TOUCHSCREEN:
11669 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011670 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011671 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011672 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11673 break;
11674 case AINPUT_SOURCE_STYLUS:
11675 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011676 injectMotionEvent(*mDispatcher,
11677 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
11678 AINPUT_SOURCE_STYLUS)
11679 .buttonState(
11680 AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
11681 .pointer(PointerBuilder(0, ToolType::STYLUS)
11682 .x(50)
11683 .y(50))
11684 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011685 break;
11686 case AINPUT_SOURCE_MOUSE:
11687 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011688 injectMotionEvent(*mDispatcher,
11689 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
11690 AINPUT_SOURCE_MOUSE)
11691 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
11692 .pointer(PointerBuilder(MOUSE_POINTER_ID,
11693 ToolType::MOUSE)
11694 .x(50)
11695 .y(50))
11696 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011697 break;
11698 default:
11699 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
11700 }
arthurhungb89ccb02020-12-30 16:19:01 +080011701
11702 // Window should receive motion event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011703 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011704 // Spy window should also receive motion event
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011705 mSpyWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000011706 }
11707
11708 // Start performing drag, we will create a drag window and transfer touch to it.
11709 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
11710 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011711 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +000011712 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011713 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +000011714 }
arthurhungb89ccb02020-12-30 16:19:01 +080011715
11716 // The drag window covers the entire display
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011717 mDragWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow",
11718 ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011719 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011720 mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(),
11721 *mWindow->getInfo(), *mSecondWindow->getInfo()},
11722 {},
11723 0,
11724 0});
arthurhungb89ccb02020-12-30 16:19:01 +080011725
11726 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +000011727 bool transferred =
Prabir Pradhan367f3432024-02-13 23:05:58 +000011728 mDispatcher->transferTouchGesture(mWindow->getToken(), mDragWindow->getToken(),
11729 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +000011730 if (transferred) {
11731 mWindow->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011732 mDragWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
11733 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung54745652022-04-20 07:17:41 +000011734 }
11735 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +080011736 }
11737};
11738
11739TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011740 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +080011741
11742 // Move on window.
11743 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011744 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011745 ui::LogicalDisplayId::DEFAULT, {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080011746 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011747 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11748 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080011749 mWindow->consumeDragEvent(false, 50, 50);
11750 mSecondWindow->assertNoEvents();
11751
11752 // Move to another window.
11753 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011754 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011755 ui::LogicalDisplayId::DEFAULT, {150, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080011756 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011757 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11758 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080011759 mWindow->consumeDragEvent(true, 150, 50);
11760 mSecondWindow->consumeDragEvent(false, 50, 50);
11761
11762 // Move back to original window.
11763 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011764 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011765 ui::LogicalDisplayId::DEFAULT, {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080011766 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011767 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11768 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080011769 mWindow->consumeDragEvent(false, 50, 50);
11770 mSecondWindow->consumeDragEvent(true, -50, 50);
11771
11772 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011773 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011774 {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080011775 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011776 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080011777 mWindow->assertNoEvents();
11778 mSecondWindow->assertNoEvents();
11779}
11780
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011781TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011782 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011783
11784 // No cancel event after drag start
11785 mSpyWindow->assertNoEvents();
11786
11787 const MotionEvent secondFingerDownEvent =
11788 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11789 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011790 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11791 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011792 .build();
11793 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011794 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011795 InputEventInjectionSync::WAIT_FOR_RESULT))
11796 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11797
11798 // Receives cancel for first pointer after next pointer down
11799 mSpyWindow->consumeMotionCancel();
11800 mSpyWindow->consumeMotionDown();
11801
11802 mSpyWindow->assertNoEvents();
11803}
11804
arthurhungf452d0b2021-01-06 00:19:52 +080011805TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011806 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +080011807
11808 // Move on window.
11809 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011810 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011811 ui::LogicalDisplayId::DEFAULT, {50, 50}))
arthurhungf452d0b2021-01-06 00:19:52 +080011812 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011813 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11814 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungf452d0b2021-01-06 00:19:52 +080011815 mWindow->consumeDragEvent(false, 50, 50);
11816 mSecondWindow->assertNoEvents();
11817
11818 // Move to another window.
11819 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011820 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011821 ui::LogicalDisplayId::DEFAULT, {150, 50}))
arthurhungf452d0b2021-01-06 00:19:52 +080011822 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011823 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11824 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungf452d0b2021-01-06 00:19:52 +080011825 mWindow->consumeDragEvent(true, 150, 50);
11826 mSecondWindow->consumeDragEvent(false, 50, 50);
11827
11828 // drop to another window.
11829 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011830 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
arthurhungf452d0b2021-01-06 00:19:52 +080011831 {150, 50}))
11832 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011833 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070011834 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhungf452d0b2021-01-06 00:19:52 +080011835 mWindow->assertNoEvents();
11836 mSecondWindow->assertNoEvents();
11837}
11838
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011839TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) {
11840 startDrag();
11841
11842 // No cancel event after drag start
11843 mSpyWindow->assertNoEvents();
11844
11845 const MotionEvent secondFingerDownEvent =
11846 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11847 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
11848 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11849 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
11850 .build();
11851 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
11852 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
11853 InputEventInjectionSync::WAIT_FOR_RESULT))
11854 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11855
11856 // Receives cancel for first pointer after next pointer down
11857 mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -080011858 mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1})));
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011859 mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
11860
11861 mSpyWindow->assertNoEvents();
11862
11863 // Spy window calls pilfer pointers
11864 EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken()));
11865 mDragWindow->assertNoEvents();
11866
11867 const MotionEvent firstFingerMoveEvent =
Siarhei Vishniakou96a15572023-12-18 10:47:52 -080011868 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011869 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
11870 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60))
11871 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
11872 .build();
11873 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou96a15572023-12-18 10:47:52 -080011874 injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011875 InputEventInjectionSync::WAIT_FOR_RESULT))
11876 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11877
11878 // Drag window should still receive the new event
Prabir Pradhan65455c72024-02-13 21:46:41 +000011879 mDragWindow->consumeMotionEvent(
11880 AllOf(WithMotionAction(ACTION_MOVE), WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)));
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011881 mDragWindow->assertNoEvents();
11882}
11883
arthurhung6d4bed92021-03-17 11:59:33 +080011884TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011885 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +080011886
11887 // Move on window and keep button pressed.
11888 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011889 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080011890 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
11891 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011892 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080011893 .build()))
11894 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011895 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11896 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhung6d4bed92021-03-17 11:59:33 +080011897 mWindow->consumeDragEvent(false, 50, 50);
11898 mSecondWindow->assertNoEvents();
11899
11900 // Move to another window and release button, expect to drop item.
11901 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011902 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080011903 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
11904 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011905 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080011906 .build()))
11907 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011908 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11909 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhung6d4bed92021-03-17 11:59:33 +080011910 mWindow->assertNoEvents();
11911 mSecondWindow->assertNoEvents();
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070011912 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhung6d4bed92021-03-17 11:59:33 +080011913
11914 // nothing to the window.
11915 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011916 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080011917 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
11918 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011919 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080011920 .build()))
11921 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011922 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhung6d4bed92021-03-17 11:59:33 +080011923 mWindow->assertNoEvents();
11924 mSecondWindow->assertNoEvents();
11925}
11926
Arthur Hung54745652022-04-20 07:17:41 +000011927TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011928 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +080011929
11930 // Set second window invisible.
11931 mSecondWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011932 mDispatcher->onWindowInfosChanged(
11933 {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Arthur Hung6d0571e2021-04-09 20:18:16 +080011934
11935 // Move on window.
11936 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011937 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011938 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hung6d0571e2021-04-09 20:18:16 +080011939 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011940 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11941 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung6d0571e2021-04-09 20:18:16 +080011942 mWindow->consumeDragEvent(false, 50, 50);
11943 mSecondWindow->assertNoEvents();
11944
11945 // Move to another window.
11946 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011947 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011948 ui::LogicalDisplayId::DEFAULT, {150, 50}))
Arthur Hung6d0571e2021-04-09 20:18:16 +080011949 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011950 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11951 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung6d0571e2021-04-09 20:18:16 +080011952 mWindow->consumeDragEvent(true, 150, 50);
11953 mSecondWindow->assertNoEvents();
11954
11955 // drop to another window.
11956 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011957 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Arthur Hung6d0571e2021-04-09 20:18:16 +080011958 {150, 50}))
11959 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011960 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070011961 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
Arthur Hung6d0571e2021-04-09 20:18:16 +080011962 mWindow->assertNoEvents();
11963 mSecondWindow->assertNoEvents();
11964}
11965
Arthur Hung54745652022-04-20 07:17:41 +000011966TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011967 // Ensure window could track pointerIds if it didn't support split touch.
11968 mWindow->setPreventSplitting(true);
11969
Arthur Hung54745652022-04-20 07:17:41 +000011970 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011971 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
11972 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hung54745652022-04-20 07:17:41 +000011973 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011974 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000011975
11976 const MotionEvent secondFingerDownEvent =
11977 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011978 .displayId(ui::LogicalDisplayId::DEFAULT)
Arthur Hung54745652022-04-20 07:17:41 +000011979 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011980 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11981 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000011982 .build();
11983 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011984 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000011985 InputEventInjectionSync::WAIT_FOR_RESULT))
11986 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000011987 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +000011988
11989 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011990 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +000011991}
11992
11993TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
11994 // First down on second window.
11995 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011996 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
11997 ui::LogicalDisplayId::DEFAULT, {150, 50}))
Arthur Hung54745652022-04-20 07:17:41 +000011998 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11999
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012000 mSecondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000012001
12002 // Second down on first window.
12003 const MotionEvent secondFingerDownEvent =
12004 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012005 .displayId(ui::LogicalDisplayId::DEFAULT)
Arthur Hung54745652022-04-20 07:17:41 +000012006 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012007 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
12008 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000012009 .build();
12010 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012011 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000012012 InputEventInjectionSync::WAIT_FOR_RESULT))
12013 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012014 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
12015 mSecondWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000012016
12017 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012018 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +000012019
12020 // Move on window.
12021 const MotionEvent secondFingerMoveEvent =
12022 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
12023 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012024 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
12025 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000012026 .build();
12027 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012028 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000012029 InputEventInjectionSync::WAIT_FOR_RESULT));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012030 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
12031 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung54745652022-04-20 07:17:41 +000012032 mWindow->consumeDragEvent(false, 50, 50);
12033 mSecondWindow->consumeMotionMove();
12034
12035 // Release the drag pointer should perform drop.
12036 const MotionEvent secondFingerUpEvent =
12037 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
12038 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012039 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
12040 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000012041 .build();
12042 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012043 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000012044 InputEventInjectionSync::WAIT_FOR_RESULT));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012045 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070012046 mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken());
Arthur Hung54745652022-04-20 07:17:41 +000012047 mWindow->assertNoEvents();
12048 mSecondWindow->consumeMotionMove();
12049}
12050
Arthur Hung3915c1f2022-05-31 07:17:17 +000012051TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012052 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +000012053
12054 // Update window of second display.
12055 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070012056 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012057 mDispatcher->onWindowInfosChanged(
12058 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
12059 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
12060 {},
12061 0,
12062 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +000012063
12064 // Let second display has a touch state.
12065 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012066 injectMotionEvent(*mDispatcher,
Arthur Hung3915c1f2022-05-31 07:17:17 +000012067 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
12068 AINPUT_SOURCE_TOUCHSCREEN)
12069 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012070 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +000012071 .build()));
Prabir Pradhan7662a8d2023-12-15 01:58:14 +000012072 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +000012073 // Update window again.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012074 mDispatcher->onWindowInfosChanged(
12075 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
12076 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
12077 {},
12078 0,
12079 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +000012080
12081 // Move on window.
12082 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012083 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012084 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hung3915c1f2022-05-31 07:17:17 +000012085 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012086 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
12087 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung3915c1f2022-05-31 07:17:17 +000012088 mWindow->consumeDragEvent(false, 50, 50);
12089 mSecondWindow->assertNoEvents();
12090
12091 // Move to another window.
12092 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012093 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012094 ui::LogicalDisplayId::DEFAULT, {150, 50}))
Arthur Hung3915c1f2022-05-31 07:17:17 +000012095 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012096 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
12097 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung3915c1f2022-05-31 07:17:17 +000012098 mWindow->consumeDragEvent(true, 150, 50);
12099 mSecondWindow->consumeDragEvent(false, 50, 50);
12100
12101 // drop to another window.
12102 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012103 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Arthur Hung3915c1f2022-05-31 07:17:17 +000012104 {150, 50}))
12105 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012106 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070012107 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hung3915c1f2022-05-31 07:17:17 +000012108 mWindow->assertNoEvents();
12109 mSecondWindow->assertNoEvents();
12110}
12111
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012112TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
12113 startDrag(true, AINPUT_SOURCE_MOUSE);
12114 // Move on window.
12115 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012116 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012117 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
12118 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012119 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012120 .x(50)
12121 .y(50))
12122 .build()))
12123 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012124 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
12125 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012126 mWindow->consumeDragEvent(false, 50, 50);
12127 mSecondWindow->assertNoEvents();
12128
12129 // Move to another window.
12130 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012131 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012132 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
12133 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012134 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012135 .x(150)
12136 .y(50))
12137 .build()))
12138 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012139 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
12140 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012141 mWindow->consumeDragEvent(true, 150, 50);
12142 mSecondWindow->consumeDragEvent(false, 50, 50);
12143
12144 // drop to another window.
12145 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012146 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012147 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
12148 .buttonState(0)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012149 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012150 .x(150)
12151 .y(50))
12152 .build()))
12153 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012154 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070012155 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012156 mWindow->assertNoEvents();
12157 mSecondWindow->assertNoEvents();
12158}
12159
Linnan Li5af92f92023-07-14 14:36:22 +080012160/**
12161 * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag
12162 * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash.
12163 */
12164TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) {
12165 // Down on second window
12166 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012167 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12168 ui::LogicalDisplayId::DEFAULT, {150, 50}))
Linnan Li5af92f92023-07-14 14:36:22 +080012169 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12170
12171 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown());
12172 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown());
12173
12174 // Down on first window
12175 const MotionEvent secondFingerDownEvent =
12176 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012177 .displayId(ui::LogicalDisplayId::DEFAULT)
Linnan Li5af92f92023-07-14 14:36:22 +080012178 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
12179 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
12180 .build();
12181 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
12182 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
12183 InputEventInjectionSync::WAIT_FOR_RESULT))
12184 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12185 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
12186 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove());
12187 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1));
12188
12189 // Start drag on first window
12190 ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN));
12191
12192 // Trigger cancel
12193 mDispatcher->cancelCurrentTouch();
12194 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012195 ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan65455c72024-02-13 21:46:41 +000012196 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE));
Linnan Li5af92f92023-07-14 14:36:22 +080012197 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel());
12198
12199 ASSERT_TRUE(mDispatcher->waitForIdle());
12200 // The D&D finished with nullptr
12201 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
12202
12203 // Remove drag window
12204 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
12205
12206 // Inject a simple gesture, ensure dispatcher not crashed
12207 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012208 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12209 ui::LogicalDisplayId::DEFAULT, PointF{50, 50}))
Linnan Li5af92f92023-07-14 14:36:22 +080012210 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12211 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
12212
12213 const MotionEvent moveEvent =
12214 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012215 .displayId(ui::LogicalDisplayId::DEFAULT)
Linnan Li5af92f92023-07-14 14:36:22 +080012216 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12217 .build();
12218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
12219 injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT,
12220 InputEventInjectionSync::WAIT_FOR_RESULT))
12221 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12222 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove());
12223
12224 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012225 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Linnan Li5af92f92023-07-14 14:36:22 +080012226 {50, 50}))
12227 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12228 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp());
12229}
12230
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000012231TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) {
12232 // Start hovering over the window.
12233 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
12234 injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012235 ui::LogicalDisplayId::DEFAULT, {50, 50}));
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000012236
12237 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)));
12238 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)));
12239
12240 ASSERT_FALSE(startDrag(/*sendDown=*/false))
12241 << "Drag and drop should not work with a hovering pointer";
12242}
12243
Vishnu Nair062a8672021-09-03 16:07:44 -070012244class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
12245
12246TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
12247 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012248 sp<FakeWindowHandle> window =
12249 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
12250 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012251 window->setDropInput(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012252 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair062a8672021-09-03 16:07:44 -070012253 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012254 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012255 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000012256 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070012257
12258 // With the flag set, window should not get any input
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012259 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012260 window->assertNoEvents();
12261
Prabir Pradhan678438e2023-04-13 19:32:51 +000012262 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012263 AINPUT_SOURCE_TOUCHSCREEN,
12264 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070012265 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012266 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -080012267 mDispatcher->waitForIdle();
Vishnu Nair062a8672021-09-03 16:07:44 -070012268 window->assertNoEvents();
12269
12270 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012271 window->setDropInput(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012272 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012273
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012274 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT));
12275 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012276
Prabir Pradhan678438e2023-04-13 19:32:51 +000012277 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012278 AINPUT_SOURCE_TOUCHSCREEN,
12279 ui::LogicalDisplayId::DEFAULT));
12280 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012281 window->assertNoEvents();
12282}
12283
12284TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
12285 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
12286 std::make_shared<FakeApplicationHandle>();
12287 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070012288 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012289 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012290 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012291 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012292 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070012293 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012294 sp<FakeWindowHandle> window =
12295 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
12296 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012297 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012298 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012299 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair062a8672021-09-03 16:07:44 -070012300 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012301 mDispatcher->onWindowInfosChanged(
12302 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012303 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000012304 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070012305
12306 // With the flag set, window should not get any input
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012307 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012308 window->assertNoEvents();
12309
Prabir Pradhan678438e2023-04-13 19:32:51 +000012310 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012311 AINPUT_SOURCE_TOUCHSCREEN,
12312 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070012313 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012314 ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012315 window->assertNoEvents();
12316
12317 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012318 window->setDropInputIfObscured(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012319 mDispatcher->onWindowInfosChanged(
12320 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012321
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012322 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT));
12323 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012324
Prabir Pradhan678438e2023-04-13 19:32:51 +000012325 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012326 AINPUT_SOURCE_TOUCHSCREEN,
12327 ui::LogicalDisplayId::DEFAULT));
12328 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
12329 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
Vishnu Nair062a8672021-09-03 16:07:44 -070012330 window->assertNoEvents();
12331}
12332
12333TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
12334 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
12335 std::make_shared<FakeApplicationHandle>();
12336 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070012337 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012338 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012339 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012340 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012341 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070012342 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012343 sp<FakeWindowHandle> window =
12344 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
12345 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012346 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012347 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012348 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair062a8672021-09-03 16:07:44 -070012349 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012350 mDispatcher->onWindowInfosChanged(
12351 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012352 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000012353 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070012354
12355 // With the flag set, window should not get any input
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012356 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012357 window->assertNoEvents();
12358
Prabir Pradhan678438e2023-04-13 19:32:51 +000012359 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012360 AINPUT_SOURCE_TOUCHSCREEN,
12361 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070012362 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012363 ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012364 window->assertNoEvents();
12365
12366 // When the window is no longer obscured because it went on top, it should get input
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012367 mDispatcher->onWindowInfosChanged(
12368 {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012369
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012370 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT));
12371 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012372
Prabir Pradhan678438e2023-04-13 19:32:51 +000012373 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012374 AINPUT_SOURCE_TOUCHSCREEN,
12375 ui::LogicalDisplayId::DEFAULT));
12376 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012377 window->assertNoEvents();
12378}
12379
Antonio Kantekf16f2832021-09-28 04:39:20 +000012380class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
12381protected:
12382 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +000012383 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +000012384 sp<FakeWindowHandle> mWindow;
12385 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +000012386 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +000012387
12388 void SetUp() override {
12389 InputDispatcherTest::SetUp();
12390
12391 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +000012392 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012393 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow",
12394 ui::LogicalDisplayId::DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012395 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000012396 setFocusedWindow(mWindow);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012397 mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2",
12398 ui::LogicalDisplayId::DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012399 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +000012400 mThirdWindow =
12401 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
12402 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
12403 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012404
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012405 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012406 mDispatcher->onWindowInfosChanged(
12407 {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()},
12408 {},
12409 0,
12410 0});
Antonio Kantek15beb512022-06-13 22:35:41 +000012411 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012412 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000012413
Antonio Kantek15beb512022-06-13 22:35:41 +000012414 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +000012415 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012416 WINDOW_UID, /*hasPermission=*/true,
12417 ui::LogicalDisplayId::DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -070012418 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
12419 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000012420 mThirdWindow->assertNoEvents();
12421 }
12422
12423 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
12424 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +000012425 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +000012426 SECOND_DISPLAY_ID)) {
12427 mWindow->assertNoEvents();
12428 mSecondWindow->assertNoEvents();
12429 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -070012430 }
Antonio Kantekf16f2832021-09-28 04:39:20 +000012431 }
12432
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012433 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Antonio Kantek15beb512022-06-13 22:35:41 +000012434 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -070012435 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012436 ui::LogicalDisplayId::DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000012437 mWindow->consumeTouchModeEvent(inTouchMode);
12438 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000012439 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +000012440 }
12441};
12442
Antonio Kantek26defcf2022-02-08 01:12:27 +000012443TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080012444 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +000012445 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
12446 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000012447 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012448}
12449
Antonio Kantek26defcf2022-02-08 01:12:27 +000012450TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
12451 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012452 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000012453 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012454 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek26defcf2022-02-08 01:12:27 +000012455 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000012456 ownerUid, /*hasPermission=*/false,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012457 ui::LogicalDisplayId::DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +000012458 mWindow->assertNoEvents();
12459 mSecondWindow->assertNoEvents();
12460}
12461
12462TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
12463 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012464 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000012465 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012466 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek15beb512022-06-13 22:35:41 +000012467 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000012468 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000012469}
12470
Antonio Kantekf16f2832021-09-28 04:39:20 +000012471TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080012472 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +000012473 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
12474 windowInfo.ownerPid, windowInfo.ownerUid,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012475 /*hasPermission=*/true,
12476 ui::LogicalDisplayId::DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000012477 mWindow->assertNoEvents();
12478 mSecondWindow->assertNoEvents();
12479}
12480
Antonio Kantek15beb512022-06-13 22:35:41 +000012481TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
12482 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
12483 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
12484 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000012485 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +000012486 mWindow->assertNoEvents();
12487 mSecondWindow->assertNoEvents();
12488 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
12489}
12490
Antonio Kantek48710e42022-03-24 14:19:30 -070012491TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
12492 // Interact with the window first.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012493 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012494 injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT))
Antonio Kantek48710e42022-03-24 14:19:30 -070012495 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012496 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Antonio Kantek48710e42022-03-24 14:19:30 -070012497
12498 // Then remove focus.
12499 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012500 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Antonio Kantek48710e42022-03-24 14:19:30 -070012501
12502 // Assert that caller can switch touch mode by owning one of the last interacted window.
12503 const WindowInfo& windowInfo = *mWindow->getInfo();
12504 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
12505 windowInfo.ownerPid, windowInfo.ownerUid,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012506 /*hasPermission=*/false,
12507 ui::LogicalDisplayId::DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -070012508}
12509
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012510class InputDispatcherSpyWindowTest : public InputDispatcherTest {
12511public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012512 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012513 std::shared_ptr<FakeApplicationHandle> application =
12514 std::make_shared<FakeApplicationHandle>();
12515 std::string name = "Fake Spy ";
12516 name += std::to_string(mSpyCount++);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012517 sp<FakeWindowHandle> spy =
12518 sp<FakeWindowHandle>::make(application, mDispatcher, name.c_str(),
12519 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012520 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -080012521 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012522 return spy;
12523 }
12524
12525 sp<FakeWindowHandle> createForeground() {
12526 std::shared_ptr<FakeApplicationHandle> application =
12527 std::make_shared<FakeApplicationHandle>();
12528 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070012529 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012530 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012531 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012532 return window;
12533 }
12534
12535private:
12536 int mSpyCount{0};
12537};
12538
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080012539using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012540/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -080012541 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
12542 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080012543TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
Siarhei Vishniakou21f77bd2023-07-18 17:51:35 -070012544 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080012545 ScopedSilentDeath _silentDeath;
12546
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012547 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -080012548 spy->setTrustedOverlay(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012549 ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}),
Prabir Pradhan5c85e052021-12-22 02:27:12 -080012550 ".* not a trusted overlay");
12551}
12552
12553/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012554 * Input injection into a display with a spy window but no foreground windows should succeed.
12555 */
12556TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012557 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012558 mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012559
12560 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012561 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12562 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012563 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012564 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012565}
12566
12567/**
12568 * Verify the order in which different input windows receive events. The touched foreground window
12569 * (if there is one) should always receive the event first. When there are multiple spy windows, the
12570 * spy windows will receive the event according to their Z-order, where the top-most spy window will
12571 * receive events before ones belows it.
12572 *
12573 * Here, we set up a scenario with four windows in the following Z order from the top:
12574 * spy1, spy2, window, spy3.
12575 * We then inject an event and verify that the foreground "window" receives it first, followed by
12576 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
12577 * window.
12578 */
12579TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
12580 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012581 auto spy1 = createSpy();
12582 auto spy2 = createSpy();
12583 auto spy3 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012584 mDispatcher->onWindowInfosChanged(
12585 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012586 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
12587 const size_t numChannels = channels.size();
12588
Michael Wright8e9a8562022-02-09 13:44:29 +000012589 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012590 if (!epollFd.ok()) {
12591 FAIL() << "Failed to create epoll fd";
12592 }
12593
12594 for (size_t i = 0; i < numChannels; i++) {
12595 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
12596 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
12597 FAIL() << "Failed to add fd to epoll";
12598 }
12599 }
12600
12601 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012602 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12603 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012604 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12605
12606 std::vector<size_t> eventOrder;
12607 std::vector<struct epoll_event> events(numChannels);
12608 for (;;) {
12609 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
12610 (100ms).count());
12611 if (nFds < 0) {
12612 FAIL() << "Failed to call epoll_wait";
12613 }
12614 if (nFds == 0) {
12615 break; // epoll_wait timed out
12616 }
12617 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -070012618 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -070012619 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012620 channels[i]->consumeMotionDown();
12621 }
12622 }
12623
12624 // Verify the order in which the events were received.
12625 EXPECT_EQ(3u, eventOrder.size());
12626 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
12627 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
12628 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
12629}
12630
12631/**
12632 * A spy window using the NOT_TOUCHABLE flag does not receive events.
12633 */
12634TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
12635 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012636 auto spy = createSpy();
12637 spy->setTouchable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012638 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012639
12640 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012641 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12642 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012643 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012644 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012645 spy->assertNoEvents();
12646}
12647
12648/**
12649 * A spy window will only receive gestures that originate within its touchable region. Gestures that
12650 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
12651 * to the window.
12652 */
12653TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
12654 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012655 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012656 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012657 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012658
12659 // Inject an event outside the spy window's touchable region.
12660 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012661 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12662 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012663 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12664 window->consumeMotionDown();
12665 spy->assertNoEvents();
12666 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012667 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12668 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012669 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12670 window->consumeMotionUp();
12671 spy->assertNoEvents();
12672
12673 // Inject an event inside the spy window's touchable region.
12674 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012675 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12676 ui::LogicalDisplayId::DEFAULT, {5, 10}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012677 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12678 window->consumeMotionDown();
12679 spy->consumeMotionDown();
12680}
12681
12682/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012683 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080012684 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012685 */
12686TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
12687 auto window = createForeground();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012688 window->setOwnerInfo(gui::Pid{12}, gui::Uid{34});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012689 auto spy = createSpy();
12690 spy->setWatchOutsideTouch(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012691 spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012692 spy->setFrame(Rect{0, 0, 20, 20});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012693 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012694
12695 // Inject an event outside the spy window's frame and touchable region.
12696 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012697 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12698 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012699 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12700 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080012701 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012702}
12703
12704/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012705 * Even when a spy window spans over multiple foreground windows, the spy should receive all
12706 * pointers that are down within its bounds.
12707 */
12708TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
12709 auto windowLeft = createForeground();
12710 windowLeft->setFrame({0, 0, 100, 200});
12711 auto windowRight = createForeground();
12712 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012713 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012714 spy->setFrame({0, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012715 mDispatcher->onWindowInfosChanged(
12716 {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012717
12718 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012719 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12720 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012721 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12722 windowLeft->consumeMotionDown();
12723 spy->consumeMotionDown();
12724
12725 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080012726 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012727 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012728 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12729 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012730 .build();
12731 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012732 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012733 InputEventInjectionSync::WAIT_FOR_RESULT))
12734 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12735 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +000012736 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012737}
12738
12739/**
12740 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
12741 * the spy should receive the second pointer with ACTION_DOWN.
12742 */
12743TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
12744 auto window = createForeground();
12745 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012746 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012747 spyRight->setFrame({100, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012748 mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012749
12750 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012751 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12752 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012753 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12754 window->consumeMotionDown();
12755 spyRight->assertNoEvents();
12756
12757 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080012758 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012759 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012760 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12761 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012762 .build();
12763 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012764 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012765 InputEventInjectionSync::WAIT_FOR_RESULT))
12766 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000012767 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012768 spyRight->consumeMotionDown();
12769}
12770
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012771/**
12772 * The spy window should not be able to affect whether or not touches are split. Only the foreground
12773 * windows should be allowed to control split touch.
12774 */
12775TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080012776 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012777 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012778 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080012779 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012780
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012781 auto window = createForeground();
12782 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012783
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012784 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012785
12786 // First finger down, no window touched.
12787 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012788 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12789 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012790 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012791 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012792 window->assertNoEvents();
12793
12794 // Second finger down on window, the window should receive touch down.
12795 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080012796 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012797 .displayId(ui::LogicalDisplayId::DEFAULT)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012798 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012799 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
12800 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012801 .build();
12802 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012803 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012804 InputEventInjectionSync::WAIT_FOR_RESULT))
12805 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12806
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012807 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +000012808 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012809}
12810
12811/**
12812 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
12813 * do not receive key events.
12814 */
12815TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012816 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012817 spy->setFocusable(false);
12818
12819 auto window = createForeground();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012820 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012821 setFocusedWindow(window);
12822 window->consumeFocusEvent(true);
12823
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012824 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012825 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012826 window->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012827
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012828 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012829 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012830 window->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012831
12832 spy->assertNoEvents();
12833}
12834
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012835using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
12836
12837/**
12838 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
12839 * are currently sent to any other windows - including other spy windows - will also be cancelled.
12840 */
12841TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
12842 auto window = createForeground();
12843 auto spy1 = createSpy();
12844 auto spy2 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012845 mDispatcher->onWindowInfosChanged(
12846 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012847
12848 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012849 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12850 ui::LogicalDisplayId::DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012851 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12852 window->consumeMotionDown();
12853 spy1->consumeMotionDown();
12854 spy2->consumeMotionDown();
12855
12856 // Pilfer pointers from the second spy window.
12857 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
12858 spy2->assertNoEvents();
12859 spy1->consumeMotionCancel();
12860 window->consumeMotionCancel();
12861
12862 // The rest of the gesture should only be sent to the second spy window.
12863 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012864 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012865 ui::LogicalDisplayId::DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012866 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12867 spy2->consumeMotionMove();
12868 spy1->assertNoEvents();
12869 window->assertNoEvents();
12870}
12871
12872/**
12873 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
12874 * in the middle of the gesture.
12875 */
12876TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
12877 auto window = createForeground();
12878 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012879 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012880
12881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012882 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12883 ui::LogicalDisplayId::DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012884 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012885 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
12886 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012887
12888 window->releaseChannel();
12889
12890 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
12891
12892 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012893 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12894 ui::LogicalDisplayId::DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012895 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012896 spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012897}
12898
12899/**
12900 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
12901 * the spy, but not to any other windows.
12902 */
12903TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
12904 auto spy = createSpy();
12905 auto window = createForeground();
12906
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012907 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012908
12909 // First finger down on the window and the spy.
12910 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012911 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12912 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012913 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12914 spy->consumeMotionDown();
12915 window->consumeMotionDown();
12916
12917 // Spy window pilfers the pointers.
12918 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
12919 window->consumeMotionCancel();
12920
12921 // Second finger down on the window and spy, but the window should not receive the pointer down.
12922 const MotionEvent secondFingerDownEvent =
12923 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012924 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012925 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012926 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
12927 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012928 .build();
12929 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012930 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012931 InputEventInjectionSync::WAIT_FOR_RESULT))
12932 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12933
Harry Cutts33476232023-01-30 19:57:29 +000012934 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012935
12936 // Third finger goes down outside all windows, so injection should fail.
12937 const MotionEvent thirdFingerDownEvent =
12938 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012939 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012940 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012941 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
12942 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
12943 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012944 .build();
12945 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012946 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012947 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080012948 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012949
12950 spy->assertNoEvents();
12951 window->assertNoEvents();
12952}
12953
12954/**
12955 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
12956 */
12957TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
12958 auto spy = createSpy();
12959 spy->setFrame(Rect(0, 0, 100, 100));
12960 auto window = createForeground();
12961 window->setFrame(Rect(0, 0, 200, 200));
12962
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012963 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012964
12965 // First finger down on the window only
12966 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012967 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12968 ui::LogicalDisplayId::DEFAULT, {150, 150}))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012969 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12970 window->consumeMotionDown();
12971
12972 // Second finger down on the spy and window
12973 const MotionEvent secondFingerDownEvent =
12974 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012975 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012976 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012977 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
12978 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012979 .build();
12980 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012981 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012982 InputEventInjectionSync::WAIT_FOR_RESULT))
12983 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12984 spy->consumeMotionDown();
12985 window->consumeMotionPointerDown(1);
12986
12987 // Third finger down on the spy and window
12988 const MotionEvent thirdFingerDownEvent =
12989 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012990 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012991 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012992 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
12993 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
12994 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012995 .build();
12996 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012997 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012998 InputEventInjectionSync::WAIT_FOR_RESULT))
12999 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
13000 spy->consumeMotionPointerDown(1);
13001 window->consumeMotionPointerDown(2);
13002
13003 // Spy window pilfers the pointers.
13004 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Prabir Pradhan33cfc6d2024-06-11 20:17:44 +000013005 window->consumeMotionPointerUp(/*pointerIdx=*/2,
13006 AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT),
13007 WithFlags(AMOTION_EVENT_FLAG_CANCELED),
13008 WithPointerCount(3)));
13009 window->consumeMotionPointerUp(/*pointerIdx=*/1,
13010 AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT),
13011 WithFlags(AMOTION_EVENT_FLAG_CANCELED),
13012 WithPointerCount(2)));
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000013013
13014 spy->assertNoEvents();
13015 window->assertNoEvents();
13016}
13017
13018/**
13019 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
13020 * other windows should be canceled. If this results in the cancellation of all pointers for some
13021 * window, then that window should receive ACTION_CANCEL.
13022 */
13023TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
13024 auto spy = createSpy();
13025 spy->setFrame(Rect(0, 0, 100, 100));
13026 auto window = createForeground();
13027 window->setFrame(Rect(0, 0, 200, 200));
13028
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013029 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000013030
13031 // First finger down on both spy and window
13032 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013033 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
13034 ui::LogicalDisplayId::DEFAULT, {10, 10}))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000013035 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
13036 window->consumeMotionDown();
13037 spy->consumeMotionDown();
13038
13039 // Second finger down on the spy and window
13040 const MotionEvent secondFingerDownEvent =
13041 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013042 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000013043 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070013044 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
13045 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000013046 .build();
13047 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070013048 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000013049 InputEventInjectionSync::WAIT_FOR_RESULT))
13050 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
13051 spy->consumeMotionPointerDown(1);
13052 window->consumeMotionPointerDown(1);
13053
13054 // Spy window pilfers the pointers.
13055 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
13056 window->consumeMotionCancel();
13057
13058 spy->assertNoEvents();
13059 window->assertNoEvents();
13060}
13061
13062/**
13063 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
13064 * be sent to other windows
13065 */
13066TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
13067 auto spy = createSpy();
13068 spy->setFrame(Rect(0, 0, 100, 100));
13069 auto window = createForeground();
13070 window->setFrame(Rect(0, 0, 200, 200));
13071
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013072 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000013073
13074 // First finger down on both window and spy
13075 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013076 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
13077 ui::LogicalDisplayId::DEFAULT, {10, 10}))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000013078 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
13079 window->consumeMotionDown();
13080 spy->consumeMotionDown();
13081
13082 // Spy window pilfers the pointers.
13083 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
13084 window->consumeMotionCancel();
13085
13086 // Second finger down on the window only
13087 const MotionEvent secondFingerDownEvent =
13088 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013089 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000013090 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070013091 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
13092 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000013093 .build();
13094 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070013095 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000013096 InputEventInjectionSync::WAIT_FOR_RESULT))
13097 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
13098 window->consumeMotionDown();
13099 window->assertNoEvents();
13100
13101 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
13102 spy->consumeMotionMove();
13103 spy->assertNoEvents();
13104}
13105
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070013106/**
13107 * A window on the left and a window on the right. Also, a spy window that's above all of the
13108 * windows, and spanning both left and right windows.
13109 * Send simultaneous motion streams from two different devices, one to the left window, and another
13110 * to the right window.
13111 * Pilfer from spy window.
13112 * Check that the pilfering only affects the pointers that are actually being received by the spy.
13113 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013114TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer_legacy) {
13115 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070013116 sp<FakeWindowHandle> spy = createSpy();
13117 spy->setFrame(Rect(0, 0, 200, 200));
13118 sp<FakeWindowHandle> leftWindow = createForeground();
13119 leftWindow->setFrame(Rect(0, 0, 100, 100));
13120
13121 sp<FakeWindowHandle> rightWindow = createForeground();
13122 rightWindow->setFrame(Rect(100, 0, 200, 100));
13123
13124 constexpr int32_t stylusDeviceId = 1;
13125 constexpr int32_t touchDeviceId = 2;
13126
13127 mDispatcher->onWindowInfosChanged(
13128 {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
13129
13130 // Stylus down on left window and spy
13131 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
13132 .deviceId(stylusDeviceId)
13133 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
13134 .build());
13135 leftWindow->consumeMotionEvent(
13136 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
13137 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
13138
13139 // Finger down on right window and spy - but spy already has stylus
13140 mDispatcher->notifyMotion(
13141 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
13142 .deviceId(touchDeviceId)
13143 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
13144 .build());
13145 rightWindow->consumeMotionEvent(
13146 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070013147 spy->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070013148
13149 // Act: pilfer from spy. Spy is currently receiving touch events.
13150 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070013151 leftWindow->consumeMotionEvent(
13152 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070013153 rightWindow->consumeMotionEvent(
13154 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
13155
13156 // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus
13157 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
13158 .deviceId(stylusDeviceId)
13159 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52))
13160 .build());
13161 mDispatcher->notifyMotion(
13162 MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
13163 .deviceId(touchDeviceId)
13164 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52))
13165 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070013166 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070013167
13168 spy->assertNoEvents();
13169 leftWindow->assertNoEvents();
13170 rightWindow->assertNoEvents();
13171}
13172
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013173/**
13174 * A window on the left and a window on the right. Also, a spy window that's above all of the
13175 * windows, and spanning both left and right windows.
13176 * Send simultaneous motion streams from two different devices, one to the left window, and another
13177 * to the right window.
13178 * Pilfer from spy window.
13179 * Check that the pilfering affects all of the pointers that are actually being received by the spy.
13180 * The spy should receive both the touch and the stylus events after pilfer.
13181 */
13182TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) {
13183 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
13184 sp<FakeWindowHandle> spy = createSpy();
13185 spy->setFrame(Rect(0, 0, 200, 200));
13186 sp<FakeWindowHandle> leftWindow = createForeground();
13187 leftWindow->setFrame(Rect(0, 0, 100, 100));
13188
13189 sp<FakeWindowHandle> rightWindow = createForeground();
13190 rightWindow->setFrame(Rect(100, 0, 200, 100));
13191
13192 constexpr int32_t stylusDeviceId = 1;
13193 constexpr int32_t touchDeviceId = 2;
13194
13195 mDispatcher->onWindowInfosChanged(
13196 {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
13197
13198 // Stylus down on left window and spy
13199 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
13200 .deviceId(stylusDeviceId)
13201 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
13202 .build());
13203 leftWindow->consumeMotionEvent(
13204 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
13205 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
13206
13207 // Finger down on right window and spy
13208 mDispatcher->notifyMotion(
13209 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
13210 .deviceId(touchDeviceId)
13211 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
13212 .build());
13213 rightWindow->consumeMotionEvent(
13214 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
13215 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
13216
13217 // Act: pilfer from spy. Spy is currently receiving touch events.
13218 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
13219 leftWindow->consumeMotionEvent(
13220 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId)));
13221 rightWindow->consumeMotionEvent(
13222 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
13223
13224 // Continue movements from both stylus and touch. Touch and stylus will be delivered to spy
Siarhei Vishniakou92bca1c2024-04-01 14:06:59 -070013225 // Instead of sending the two MOVE events for each input device together, and then receiving
13226 // them both, process them one at at time. InputConsumer is always in the batching mode, which
13227 // means that the two MOVE events will be initially put into a batch. Once the events are
13228 // batched, the 'consume' call may result in any of the MOVE events to be sent first (depending
13229 // on the implementation of InputConsumer), which would mean that the order of the received
13230 // events could be different depending on whether there are 1 or 2 events pending in the
13231 // InputChannel at the time the test calls 'consume'. To make assertions simpler here, and to
13232 // avoid this confusing behaviour, send and receive each MOVE event separately.
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013233 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
13234 .deviceId(stylusDeviceId)
13235 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52))
13236 .build());
Siarhei Vishniakou92bca1c2024-04-01 14:06:59 -070013237 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013238 mDispatcher->notifyMotion(
13239 MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
13240 .deviceId(touchDeviceId)
13241 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52))
13242 .build());
Siarhei Vishniakou92bca1c2024-04-01 14:06:59 -070013243 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013244
13245 spy->assertNoEvents();
13246 leftWindow->assertNoEvents();
13247 rightWindow->assertNoEvents();
13248}
13249
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000013250TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) {
13251 auto window = createForeground();
13252 auto spy = createSpy();
13253 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
13254
13255 mDispatcher->notifyMotion(
13256 MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
13257 .deviceId(1)
13258 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200))
13259 .build());
13260 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13261 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13262
13263 // Pilfer pointers from the spy window should fail.
13264 EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken()));
13265 spy->assertNoEvents();
13266 window->assertNoEvents();
13267}
13268
Prabir Pradhand65552b2021-10-07 11:23:50 -070013269class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
13270public:
13271 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
13272 std::shared_ptr<FakeApplicationHandle> overlayApplication =
13273 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013274 sp<FakeWindowHandle> overlay = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
13275 "Stylus interceptor window",
13276 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013277 overlay->setFocusable(false);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013278 overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080013279 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080013280 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013281 overlay->setTrustedOverlay(true);
13282
13283 std::shared_ptr<FakeApplicationHandle> application =
13284 std::make_shared<FakeApplicationHandle>();
13285 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070013286 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013287 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013288 window->setFocusable(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013289 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Prabir Pradhand65552b2021-10-07 11:23:50 -070013290
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013291 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013292 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070013293 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000013294 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013295 return {std::move(overlay), std::move(window)};
13296 }
13297
13298 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000013299 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -070013300 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013301 ui::LogicalDisplayId::DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -070013302 }
13303
13304 void sendStylusEvent(int32_t action) {
13305 NotifyMotionArgs motionArgs =
13306 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013307 ui::LogicalDisplayId::DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070013308 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +000013309 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013310 }
13311};
13312
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080013313using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
13314
13315TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
Siarhei Vishniakouad3b6822023-06-22 14:17:35 -070013316 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080013317 ScopedSilentDeath _silentDeath;
13318
Prabir Pradhand65552b2021-10-07 11:23:50 -070013319 auto [overlay, window] = setupStylusOverlayScenario();
13320 overlay->setTrustedOverlay(false);
13321 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013322 ASSERT_DEATH(mDispatcher->onWindowInfosChanged(
13323 {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}),
Prabir Pradhand65552b2021-10-07 11:23:50 -070013324 ".* not a trusted overlay");
13325}
13326
13327TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
13328 auto [overlay, window] = setupStylusOverlayScenario();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013329 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070013330
13331 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
13332 overlay->consumeMotionDown();
13333 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
13334 overlay->consumeMotionUp();
13335
13336 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
13337 window->consumeMotionDown();
13338 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
13339 window->consumeMotionUp();
13340
13341 overlay->assertNoEvents();
13342 window->assertNoEvents();
13343}
13344
13345TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
13346 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -080013347 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013348 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070013349
13350 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
13351 overlay->consumeMotionDown();
13352 window->consumeMotionDown();
13353 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
13354 overlay->consumeMotionUp();
13355 window->consumeMotionUp();
13356
13357 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
13358 window->consumeMotionDown();
13359 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
13360 window->consumeMotionUp();
13361
13362 overlay->assertNoEvents();
13363 window->assertNoEvents();
13364}
13365
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000013366/**
13367 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
13368 * The scenario is as follows:
13369 * - The stylus interceptor overlay is configured as a spy window.
13370 * - The stylus interceptor spy receives the start of a new stylus gesture.
13371 * - It pilfers pointers and then configures itself to no longer be a spy.
13372 * - The stylus interceptor continues to receive the rest of the gesture.
13373 */
13374TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
13375 auto [overlay, window] = setupStylusOverlayScenario();
13376 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013377 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000013378
13379 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
13380 overlay->consumeMotionDown();
13381 window->consumeMotionDown();
13382
13383 // The interceptor pilfers the pointers.
13384 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
13385 window->consumeMotionCancel();
13386
13387 // The interceptor configures itself so that it is no longer a spy.
13388 overlay->setSpy(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013389 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000013390
13391 // It continues to receive the rest of the stylus gesture.
13392 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
13393 overlay->consumeMotionMove();
13394 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
13395 overlay->consumeMotionUp();
13396
13397 window->assertNoEvents();
13398}
13399
Prabir Pradhan5735a322022-04-11 17:23:34 +000013400struct User {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013401 gui::Pid mPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000013402 gui::Uid mUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +000013403 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
13404 std::unique_ptr<InputDispatcher>& mDispatcher;
13405
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013406 User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid)
Prabir Pradhan5735a322022-04-11 17:23:34 +000013407 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
13408
13409 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070013410 return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013411 ui::LogicalDisplayId::DEFAULT, {100, 200},
Prabir Pradhan5735a322022-04-11 17:23:34 +000013412 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
13413 AMOTION_EVENT_INVALID_CURSOR_POSITION},
13414 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
13415 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
13416 }
13417
13418 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Linnan Li13bf76a2024-05-05 19:18:02 +080013419 return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013420 ui::LogicalDisplayId::INVALID,
Prabir Pradhan5735a322022-04-11 17:23:34 +000013421 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +000013422 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +000013423 mPolicyFlags);
13424 }
13425
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013426 sp<FakeWindowHandle> createWindow(const char* name) const {
Prabir Pradhan5735a322022-04-11 17:23:34 +000013427 std::shared_ptr<FakeApplicationHandle> overlayApplication =
13428 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013429 sp<FakeWindowHandle> window =
13430 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, name,
13431 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +000013432 window->setOwnerInfo(mPid, mUid);
13433 return window;
13434 }
13435};
13436
13437using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
13438
13439TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013440 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013441 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013442 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013443
13444 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13445 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13446 window->consumeMotionDown();
13447
13448 setFocusedWindow(window);
13449 window->consumeFocusEvent(true);
13450
13451 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13452 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013453 window->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Prabir Pradhan5735a322022-04-11 17:23:34 +000013454}
13455
13456TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013457 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013458 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013459 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013460
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013461 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013462 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
13463 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13464
13465 setFocusedWindow(window);
13466 window->consumeFocusEvent(true);
13467
13468 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
13469 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
13470 window->assertNoEvents();
13471}
13472
13473TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013474 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013475 auto window = owner.createWindow("Owned window");
13476 auto spy = owner.createWindow("Owned spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013477 spy->setSpy(true);
13478 spy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013479 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013480
13481 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13482 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13483 spy->consumeMotionDown();
13484 window->consumeMotionDown();
13485}
13486
13487TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013488 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013489 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013490
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013491 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013492 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013493 randosSpy->setSpy(true);
13494 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013495 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013496
13497 // The event is targeted at owner's window, so injection should succeed, but the spy should
13498 // not receive the event.
13499 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13500 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13501 randosSpy->assertNoEvents();
13502 window->consumeMotionDown();
13503}
13504
13505TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013506 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013507 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013508
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013509 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013510 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013511 randosSpy->setSpy(true);
13512 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013513 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013514
13515 // A user that has injection permission can inject into any window.
13516 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070013517 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013518 ui::LogicalDisplayId::DEFAULT));
Prabir Pradhan5735a322022-04-11 17:23:34 +000013519 randosSpy->consumeMotionDown();
13520 window->consumeMotionDown();
13521
13522 setFocusedWindow(randosSpy);
13523 randosSpy->consumeFocusEvent(true);
13524
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070013525 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013526 randosSpy->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Prabir Pradhan5735a322022-04-11 17:23:34 +000013527 window->assertNoEvents();
13528}
13529
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070013530TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013531 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013532 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013533
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013534 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013535 auto randosWindow = rando.createWindow("Rando's window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013536 randosWindow->setFrame(Rect{-10, -10, -5, -5});
13537 randosWindow->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013538 mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013539
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070013540 // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids.
Prabir Pradhan5735a322022-04-11 17:23:34 +000013541 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13542 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13543 window->consumeMotionDown();
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070013544 randosWindow->assertNoEvents();
Prabir Pradhan5735a322022-04-11 17:23:34 +000013545}
13546
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013547using InputDispatcherPointerInWindowTest = InputDispatcherTest;
13548
13549TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) {
13550 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
13551
13552 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013553 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013554 left->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013555 sp<FakeWindowHandle> right =
13556 sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window",
13557 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013558 right->setFrame(Rect(100, 0, 200, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013559 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window",
13560 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013561 spy->setFrame(Rect(0, 0, 200, 100));
13562 spy->setTrustedOverlay(true);
13563 spy->setSpy(true);
13564
13565 mDispatcher->onWindowInfosChanged(
13566 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
13567
13568 // Hover into the left window.
13569 mDispatcher->notifyMotion(
13570 MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
13571 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50))
13572 .build());
13573
13574 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13575 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13576
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013577 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13578 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013579 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013580 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13581 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013582 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013583 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13584 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013585 /*pointerId=*/0));
13586
13587 // Hover move to the right window.
13588 mDispatcher->notifyMotion(
13589 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
13590 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50))
13591 .build());
13592
13593 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13594 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13595 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
13596
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013597 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13598 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013599 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013600 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13601 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013602 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013603 ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13604 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013605 /*pointerId=*/0));
13606
13607 // Stop hovering.
13608 mDispatcher->notifyMotion(
13609 MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
13610 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50))
13611 .build());
13612
13613 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13614 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13615
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013616 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13617 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013618 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013619 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13620 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013621 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013622 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13623 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013624 /*pointerId=*/0));
13625}
13626
13627TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) {
13628 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
13629
13630 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013631 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013632 left->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013633 sp<FakeWindowHandle> right =
13634 sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window",
13635 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013636 right->setFrame(Rect(100, 0, 200, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013637 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window",
13638 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013639 spy->setFrame(Rect(0, 0, 200, 100));
13640 spy->setTrustedOverlay(true);
13641 spy->setSpy(true);
13642
13643 mDispatcher->onWindowInfosChanged(
13644 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
13645
13646 // First pointer down on left window.
13647 mDispatcher->notifyMotion(
13648 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
13649 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
13650 .build());
13651
13652 left->consumeMotionDown();
13653 spy->consumeMotionDown();
13654
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013655 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13656 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013657 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013658 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13659 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013660 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013661 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13662 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013663 /*pointerId=*/0));
13664
13665 // Second pointer down on right window.
13666 mDispatcher->notifyMotion(
13667 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
13668 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
13669 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
13670 .build());
13671
13672 left->consumeMotionMove();
13673 right->consumeMotionDown();
13674 spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
13675
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013676 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13677 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013678 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013679 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13680 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013681 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013682 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13683 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013684 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013685 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13686 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013687 /*pointerId=*/1));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013688 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13689 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013690 /*pointerId=*/1));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013691 ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13692 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013693 /*pointerId=*/1));
13694
13695 // Second pointer up.
13696 mDispatcher->notifyMotion(
13697 MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
13698 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
13699 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
13700 .build());
13701
13702 left->consumeMotionMove();
13703 right->consumeMotionUp();
13704 spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
13705
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013706 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13707 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013708 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013709 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13710 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013711 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013712 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13713 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013714 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013715 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13716 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013717 /*pointerId=*/1));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013718 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13719 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013720 /*pointerId=*/1));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013721 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13722 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013723 /*pointerId=*/1));
13724
13725 // First pointer up.
13726 mDispatcher->notifyMotion(
13727 MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
13728 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
13729 .build());
13730
13731 left->consumeMotionUp();
13732 spy->consumeMotionUp();
13733
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013734 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13735 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013736 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013737 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13738 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013739 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013740 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13741 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013742 /*pointerId=*/0));
13743}
13744
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013745TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse_legacy) {
13746 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013747 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
13748
13749 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013750 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013751 left->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013752 sp<FakeWindowHandle> right =
13753 sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window",
13754 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013755 right->setFrame(Rect(100, 0, 200, 100));
13756
13757 mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0});
13758
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013759 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13760 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013761 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013762 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13763 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013764 /*pointerId=*/0));
13765
13766 // Hover move into the window.
13767 mDispatcher->notifyMotion(
13768 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13769 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50))
13770 .rawXCursorPosition(50)
13771 .rawYCursorPosition(50)
13772 .deviceId(DEVICE_ID)
13773 .build());
13774
13775 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13776
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013777 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13778 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013779 /*pointerId=*/0));
13780
13781 // Move the mouse with another device. This cancels the hovering pointer from the first device.
13782 mDispatcher->notifyMotion(
13783 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13784 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50))
13785 .rawXCursorPosition(51)
13786 .rawYCursorPosition(50)
13787 .deviceId(SECOND_DEVICE_ID)
13788 .build());
13789
13790 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13791 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13792
13793 // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets
13794 // a HOVER_EXIT from the first device.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013795 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13796 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013797 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013798 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013799 SECOND_DEVICE_ID,
13800 /*pointerId=*/0));
13801
13802 // Move the mouse outside the window. Document the current behavior, where the window does not
13803 // receive HOVER_EXIT even though the mouse left the window.
13804 mDispatcher->notifyMotion(
13805 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13806 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50))
13807 .rawXCursorPosition(150)
13808 .rawYCursorPosition(50)
13809 .deviceId(SECOND_DEVICE_ID)
13810 .build());
13811
13812 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13813 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013814 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13815 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013816 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013817 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013818 SECOND_DEVICE_ID,
13819 /*pointerId=*/0));
13820}
13821
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013822/**
13823 * TODO(b/313689709) - correctly support multiple mouse devices, because they should be controlling
13824 * the same cursor, and therefore have a shared motion event stream.
13825 */
13826TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) {
13827 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
13828 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
13829
13830 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013831 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013832 left->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013833 sp<FakeWindowHandle> right =
13834 sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window",
13835 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013836 right->setFrame(Rect(100, 0, 200, 100));
13837
13838 mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0});
13839
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013840 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13841 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013842 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013843 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13844 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013845 /*pointerId=*/0));
13846
13847 // Hover move into the window.
13848 mDispatcher->notifyMotion(
13849 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13850 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50))
13851 .rawXCursorPosition(50)
13852 .rawYCursorPosition(50)
13853 .deviceId(DEVICE_ID)
13854 .build());
13855
13856 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13857
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013858 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13859 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013860 /*pointerId=*/0));
13861
13862 // Move the mouse with another device
13863 mDispatcher->notifyMotion(
13864 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13865 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50))
13866 .rawXCursorPosition(51)
13867 .rawYCursorPosition(50)
13868 .deviceId(SECOND_DEVICE_ID)
13869 .build());
13870 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13871
13872 // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets
13873 // a HOVER_EXIT from the first device.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013874 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13875 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013876 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013877 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013878 SECOND_DEVICE_ID,
13879 /*pointerId=*/0));
13880
13881 // Move the mouse outside the window. Document the current behavior, where the window does not
13882 // receive HOVER_EXIT even though the mouse left the window.
13883 mDispatcher->notifyMotion(
13884 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13885 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50))
13886 .rawXCursorPosition(150)
13887 .rawYCursorPosition(50)
13888 .deviceId(SECOND_DEVICE_ID)
13889 .build());
13890
13891 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013892 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13893 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013894 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013895 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013896 SECOND_DEVICE_ID,
13897 /*pointerId=*/0));
13898}
13899
Arpit Singhb65e2bd2024-06-03 09:48:16 +000013900TEST_F(InputDispatcherTest, FocusedDisplayChangeIsNotified) {
13901 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
13902 mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID);
13903}
13904
Garfield Tane84e6f92019-08-29 17:28:41 -070013905} // namespace android::inputdispatcher