blob: 5eab6be07429735c545b03aa940356b16498c939 [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
Garfield Tandf26e862020-07-01 20:18:19 -07004175TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07004176 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004177 sp<FakeWindowHandle> windowLeft = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
4178 ui::LogicalDisplayId::DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07004179 windowLeft->setFrame(Rect(0, 0, 600, 800));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004180 sp<FakeWindowHandle> windowRight = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
4181 ui::LogicalDisplayId::DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07004182 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07004183
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004184 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Garfield Tandf26e862020-07-01 20:18:19 -07004185
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004186 mDispatcher->onWindowInfosChanged(
4187 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07004188
4189 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004190 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004191 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004192 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4193 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004194 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004195 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004196 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07004197
4198 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004199 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004200 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004201 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4202 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004203 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004204 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004205 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
4206 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07004207
4208 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004209 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004210 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004211 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4212 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004213 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004214 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004215 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
4216 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07004217
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004219 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004220 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
4221 AINPUT_SOURCE_MOUSE)
4222 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4223 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004224 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004225 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004226 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07004227
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004229 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004230 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
4231 AINPUT_SOURCE_MOUSE)
4232 .buttonState(0)
4233 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004234 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004235 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004236 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07004237
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004238 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004239 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004240 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
4241 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004242 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004243 .build()));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004244 windowLeft->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07004245
4246 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004247 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004248 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004249 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4250 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004251 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004252 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004253 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004254
4255 // No more events
4256 windowLeft->assertNoEvents();
4257 windowRight->assertNoEvents();
4258}
4259
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004260/**
4261 * Put two fingers down (and don't release them) and click the mouse button.
4262 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
4263 * currently active gesture should be canceled, and the new one should proceed.
4264 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004265TEST_F(InputDispatcherTest, TwoPointersDownMouseClick_legacy) {
4266 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004267 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004268 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4269 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004270 window->setFrame(Rect(0, 0, 600, 800));
4271
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004272 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004273
4274 const int32_t touchDeviceId = 4;
4275 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004276
4277 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00004278 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4279 .deviceId(touchDeviceId)
4280 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4281 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004282
Prabir Pradhan678438e2023-04-13 19:32:51 +00004283 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4284 .deviceId(touchDeviceId)
4285 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4286 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
4287 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004288 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4289 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4290
4291 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00004292 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4293 .deviceId(mouseDeviceId)
4294 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4295 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4296 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004297 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
4298 WithPointerCount(2u)));
4299 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
4300
Prabir Pradhan678438e2023-04-13 19:32:51 +00004301 mDispatcher->notifyMotion(
4302 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
4303 .deviceId(mouseDeviceId)
4304 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4305 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
4306 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4307 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004308 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
4309
4310 // Try to send more touch events while the mouse is down. Since it's a continuation of an
4311 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004312 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
4313 .deviceId(touchDeviceId)
4314 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
4315 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
4316 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004317 window->assertNoEvents();
4318}
4319
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004320/**
4321 * Put two fingers down (and don't release them) and click the mouse button.
4322 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
4323 * currently active gesture should not be canceled, and the new one should proceed in parallel.
4324 */
4325TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
4326 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
4327 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004328 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4329 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004330 window->setFrame(Rect(0, 0, 600, 800));
4331
4332 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
4333
4334 const int32_t touchDeviceId = 4;
4335 const int32_t mouseDeviceId = 6;
4336
4337 // Two pointers down
4338 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4339 .deviceId(touchDeviceId)
4340 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4341 .build());
4342
4343 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4344 .deviceId(touchDeviceId)
4345 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4346 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
4347 .build());
4348 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4349 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4350
4351 // Send a series of mouse events for a mouse click
4352 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4353 .deviceId(mouseDeviceId)
4354 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4355 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4356 .build());
4357 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
4358
4359 mDispatcher->notifyMotion(
4360 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
4361 .deviceId(mouseDeviceId)
4362 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4363 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
4364 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4365 .build());
4366 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
4367
4368 // Try to send more touch events while the mouse is down. Since it's a continuation of an
4369 // already active gesture, it should be sent normally.
4370 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
4371 .deviceId(touchDeviceId)
4372 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
4373 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
4374 .build());
4375 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
4376 window->assertNoEvents();
4377}
4378
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004379TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
4380 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4381
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004382 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
4383 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004384 spyWindow->setFrame(Rect(0, 0, 600, 800));
4385 spyWindow->setTrustedOverlay(true);
4386 spyWindow->setSpy(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004387 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4388 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004389 window->setFrame(Rect(0, 0, 600, 800));
4390
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004391 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004392 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004393
4394 // Send mouse cursor to the window
4395 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004396 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004397 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4398 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004399 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004400 .build()));
4401
4402 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4403 WithSource(AINPUT_SOURCE_MOUSE)));
4404 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4405 WithSource(AINPUT_SOURCE_MOUSE)));
4406
4407 window->assertNoEvents();
4408 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07004409}
4410
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004411TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows_legacy) {
4412 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004413 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4414
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004415 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
4416 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004417 spyWindow->setFrame(Rect(0, 0, 600, 800));
4418 spyWindow->setTrustedOverlay(true);
4419 spyWindow->setSpy(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004420 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4421 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004422 window->setFrame(Rect(0, 0, 600, 800));
4423
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004424 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004425 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004426
4427 // Send mouse cursor to the window
4428 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004429 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004430 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4431 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004432 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004433 .build()));
4434
4435 // Move mouse cursor
4436 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004437 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004438 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4439 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004440 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004441 .build()));
4442
4443 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4444 WithSource(AINPUT_SOURCE_MOUSE)));
4445 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4446 WithSource(AINPUT_SOURCE_MOUSE)));
4447 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4448 WithSource(AINPUT_SOURCE_MOUSE)));
4449 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4450 WithSource(AINPUT_SOURCE_MOUSE)));
4451 // Touch down on the window
4452 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004453 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004454 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
4455 AINPUT_SOURCE_TOUCHSCREEN)
4456 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004457 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004458 .build()));
4459 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4460 WithSource(AINPUT_SOURCE_MOUSE)));
4461 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4462 WithSource(AINPUT_SOURCE_MOUSE)));
4463 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4464 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4465 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4466 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4467
4468 // pilfer the motion, retaining the gesture on the spy window.
4469 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
4470 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4471 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4472
4473 // Touch UP on the window
4474 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004475 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004476 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
4477 AINPUT_SOURCE_TOUCHSCREEN)
4478 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004479 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004480 .build()));
4481 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4482 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4483
4484 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
4485 // to send a new gesture. It should again go to both windows (spy and the window below), just
4486 // like the first gesture did, before pilfering. The window configuration has not changed.
4487
4488 // One more tap - DOWN
4489 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004490 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004491 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
4492 AINPUT_SOURCE_TOUCHSCREEN)
4493 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004494 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004495 .build()));
4496 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4497 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4498 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4499 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4500
4501 // Touch UP on the window
4502 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004503 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004504 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
4505 AINPUT_SOURCE_TOUCHSCREEN)
4506 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004507 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004508 .build()));
4509 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4510 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4511 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4512 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4513
4514 window->assertNoEvents();
4515 spyWindow->assertNoEvents();
4516}
4517
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004518TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
4519 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
4520 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4521
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004522 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
4523 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004524 spyWindow->setFrame(Rect(0, 0, 600, 800));
4525 spyWindow->setTrustedOverlay(true);
4526 spyWindow->setSpy(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004527 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4528 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004529 window->setFrame(Rect(0, 0, 600, 800));
4530
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004531 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004532 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
4533
4534 // Send mouse cursor to the window
4535 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4536 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
4537 .build());
4538
4539 // Move mouse cursor
4540 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
4541 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
4542 .build());
4543
4544 window->consumeMotionEvent(
4545 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithSource(AINPUT_SOURCE_MOUSE)));
4546 spyWindow->consumeMotionEvent(
4547 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithSource(AINPUT_SOURCE_MOUSE)));
4548 window->consumeMotionEvent(
4549 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
4550 spyWindow->consumeMotionEvent(
4551 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
4552 // Touch down on the window
4553 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4554 .deviceId(SECOND_DEVICE_ID)
4555 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
4556 .build());
4557 window->consumeMotionEvent(
4558 AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4559 spyWindow->consumeMotionEvent(
4560 AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4561
4562 // pilfer the motion, retaining the gesture on the spy window.
4563 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
4564 window->consumeMotionEvent(
4565 AllOf(WithMotionAction(ACTION_CANCEL), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4566 // Mouse hover is not pilfered
4567
4568 // Touch UP on the window
4569 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4570 .deviceId(SECOND_DEVICE_ID)
4571 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
4572 .build());
4573 spyWindow->consumeMotionEvent(
4574 AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4575
4576 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
4577 // to send a new gesture. It should again go to both windows (spy and the window below), just
4578 // like the first gesture did, before pilfering. The window configuration has not changed.
4579
4580 // One more tap - DOWN
4581 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4582 .deviceId(SECOND_DEVICE_ID)
4583 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
4584 .build());
4585 window->consumeMotionEvent(
4586 AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4587 spyWindow->consumeMotionEvent(
4588 AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4589
4590 // Touch UP on the window
4591 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4592 .deviceId(SECOND_DEVICE_ID)
4593 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
4594 .build());
4595 window->consumeMotionEvent(
4596 AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4597 spyWindow->consumeMotionEvent(
4598 AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4599
4600 // Mouse movement continues normally as well
4601 // Move mouse cursor
4602 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
4603 .pointer(PointerBuilder(0, ToolType::MOUSE).x(120).y(130))
4604 .build());
4605 window->consumeMotionEvent(
4606 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
4607 spyWindow->consumeMotionEvent(
4608 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
4609
4610 window->assertNoEvents();
4611 spyWindow->assertNoEvents();
4612}
4613
Garfield Tandf26e862020-07-01 20:18:19 -07004614// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
4615// directly in this test.
4616TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07004617 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004618 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4619 ui::LogicalDisplayId::DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07004620 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07004621
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004622 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Garfield Tandf26e862020-07-01 20:18:19 -07004623
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004624 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07004625
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004626 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004627 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004628 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4629 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004630 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004631 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004632 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07004633 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004634 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004635 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004636 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4637 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004638 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004639 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004640 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
4641 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07004642
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004643 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004644 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004645 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
4646 AINPUT_SOURCE_MOUSE)
4647 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4648 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004649 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004650 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004651 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07004652
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004653 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004654 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004655 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
4656 AINPUT_SOURCE_MOUSE)
4657 .buttonState(0)
4658 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004659 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004660 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004661 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07004662
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004663 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004664 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004665 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
4666 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004667 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004668 .build()));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004669 window->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07004670
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07004671 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
4672 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
4673 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004674 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004675 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
4676 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004677 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004678 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004679 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07004680}
4681
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004682/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004683 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
4684 * is generated.
4685 */
4686TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
4687 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004688 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4689 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004690 window->setFrame(Rect(0, 0, 1200, 800));
4691
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004692 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004693
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004694 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004695
4696 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004697 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004698 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4699 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004700 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004701 .build()));
4702 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4703
4704 // Remove the window, but keep the channel.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004705 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004706 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
4707}
4708
4709/**
Daniel Norman7487dfa2023-08-02 16:39:45 -07004710 * Test that invalid HOVER events sent by accessibility do not cause a fatal crash.
4711 */
Ameer Armalycff4fa52023-10-04 23:45:11 +00004712TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash,
4713 REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags,
4714 a11y_crash_on_inconsistent_event_stream))) {
Daniel Norman7487dfa2023-08-02 16:39:45 -07004715 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004716 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4717 ui::LogicalDisplayId::DEFAULT);
Daniel Norman7487dfa2023-08-02 16:39:45 -07004718 window->setFrame(Rect(0, 0, 1200, 800));
4719
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004720 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Daniel Norman7487dfa2023-08-02 16:39:45 -07004721
4722 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
4723
4724 MotionEventBuilder hoverEnterBuilder =
4725 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4726 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4727 .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
4728 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4729 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
4730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4731 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
4732 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4733 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4734}
4735
4736/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004737 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
4738 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004739TEST_F(InputDispatcherTest, TouchDownAfterMouseHover_legacy) {
4740 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004741 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004742 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4743 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004744 window->setFrame(Rect(0, 0, 100, 100));
4745
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004746 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004747
4748 const int32_t mouseDeviceId = 7;
4749 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004750
4751 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00004752 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4753 .deviceId(mouseDeviceId)
4754 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
4755 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004756 window->consumeMotionEvent(
4757 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
4758
4759 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00004760 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4761 .deviceId(touchDeviceId)
4762 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4763 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004764
4765 window->consumeMotionEvent(
4766 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
4767 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
4768}
4769
4770/**
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004771 * If mouse is hovering when the touch goes down, the hovering should not be stopped.
4772 */
4773TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
4774 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
4775 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004776 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4777 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004778 window->setFrame(Rect(0, 0, 100, 100));
4779
4780 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
4781
4782 const int32_t mouseDeviceId = 7;
4783 const int32_t touchDeviceId = 4;
4784
4785 // Start hovering with the mouse
4786 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4787 .deviceId(mouseDeviceId)
4788 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
4789 .build());
4790 window->consumeMotionEvent(
4791 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
4792
4793 // Touch goes down
4794 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4795 .deviceId(touchDeviceId)
4796 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4797 .build());
4798 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
4799}
4800
4801/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004802 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004803 * The tap causes a HOVER_EXIT event to be generated because the current event
4804 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004805 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004806TEST_F(InputDispatcherTest, MouseHoverAndTouchTap_legacy) {
4807 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004808 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004809 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4810 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004811 window->setFrame(Rect(0, 0, 100, 100));
4812
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004813 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004814 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
4815 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
4816 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004817 ASSERT_NO_FATAL_FAILURE(
4818 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4819 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004820
4821 // Tap on the window
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004822 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4823 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4824 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004825 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004826 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4827 WithSource(AINPUT_SOURCE_MOUSE))));
4828
4829 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004830 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4831 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
4832
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004833 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4834 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4835 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004836 ASSERT_NO_FATAL_FAILURE(
4837 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4838 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
4839}
4840
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004841/**
4842 * Send a mouse hover event followed by a tap from touchscreen.
4843 * The tap causes a HOVER_EXIT event to be generated because the current event
4844 * stream's source has been switched.
4845 */
4846TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
4847 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
4848 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004849 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
4850 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004851 window->setFrame(Rect(0, 0, 100, 100));
4852
4853 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
4854 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
4855 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
4856 .build());
4857
4858 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4859 WithSource(AINPUT_SOURCE_MOUSE)));
4860
4861 // Tap on the window
4862 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4863 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4864 .build());
4865
4866 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4867 WithSource(AINPUT_SOURCE_MOUSE)));
4868
4869 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4870 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4871
4872 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4873 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4874 .build());
4875
4876 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4877 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4878}
4879
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004880TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
4881 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4882 sp<FakeWindowHandle> windowDefaultDisplay =
4883 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004884 ui::LogicalDisplayId::DEFAULT);
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004885 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
4886 sp<FakeWindowHandle> windowSecondDisplay =
4887 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
4888 SECOND_DISPLAY_ID);
4889 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
4890
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004891 mDispatcher->onWindowInfosChanged(
4892 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004893
4894 // Set cursor position in window in default display and check that hover enter and move
4895 // events are generated.
4896 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004897 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004898 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4899 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004900 .displayId(ui::LogicalDisplayId::DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004901 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004902 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004903 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004904
4905 // Remove all windows in secondary display and check that no event happens on window in
4906 // primary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004907 mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0});
4908
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004909 windowDefaultDisplay->assertNoEvents();
4910
4911 // Move cursor position in window in default display and check that only hover move
4912 // event is generated and not hover enter event.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004913 mDispatcher->onWindowInfosChanged(
4914 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004915 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004916 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004917 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4918 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004919 .displayId(ui::LogicalDisplayId::DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004920 .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004921 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004922 windowDefaultDisplay->consumeMotionEvent(
4923 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4924 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004925 windowDefaultDisplay->assertNoEvents();
4926}
4927
Garfield Tan00f511d2019-06-12 16:55:40 -07004928TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07004929 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07004930
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004931 sp<FakeWindowHandle> windowLeft = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
4932 ui::LogicalDisplayId::DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004933 windowLeft->setFrame(Rect(0, 0, 600, 800));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004934 sp<FakeWindowHandle> windowRight = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
4935 ui::LogicalDisplayId::DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004936 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07004937
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004938 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Garfield Tan00f511d2019-06-12 16:55:40 -07004939
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004940 mDispatcher->onWindowInfosChanged(
4941 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tan00f511d2019-06-12 16:55:40 -07004942
4943 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
4944 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004945 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004946 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004947 ui::LogicalDisplayId::DEFAULT, {610, 400}, {599, 400}));
4948 windowLeft->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004949 windowRight->assertNoEvents();
4950}
4951
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004952TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07004953 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004954 sp<FakeWindowHandle> window =
4955 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
4956 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07004957 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004958
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004959 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07004960 setFocusedWindow(window);
4961
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004962 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004963
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004964 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004965
4966 // Window should receive key down event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004967 window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004968
4969 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
4970 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004971 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004972 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT, AKEY_EVENT_FLAG_CANCELED);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004973}
4974
4975TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07004976 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004977 sp<FakeWindowHandle> window =
4978 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
4979 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004980
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004981 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004982
Prabir Pradhan678438e2023-04-13 19:32:51 +00004983 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004984 AINPUT_SOURCE_TOUCHSCREEN,
4985 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004986
4987 // Window should receive motion down event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004988 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004989
4990 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
4991 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004992 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004993 window->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004994 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004995}
4996
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07004997TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
4998 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004999 sp<FakeWindowHandle> window =
5000 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
5001 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07005002
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005003 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07005004
5005 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
5006 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
5007 .build());
5008
5009 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
5010
5011 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
5012 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
5013 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
5014
5015 // After the device has been reset, a new hovering stream can be sent to the window
5016 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
5017 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
5018 .build());
5019 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
5020}
5021
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005022TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
5023 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005024 sp<FakeWindowHandle> window =
5025 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
5026 ui::LogicalDisplayId::DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005027 window->setFocusable(true);
5028
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005029 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005030 setFocusedWindow(window);
5031
5032 window->consumeFocusEvent(true);
5033
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005034 const NotifyKeyArgs keyArgs =
5035 generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005036 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
5037 const nsecs_t injectTime = keyArgs.eventTime;
5038 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005039 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005040 // The dispatching time should be always greater than or equal to intercept key timeout.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005041 window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005042 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
5043 std::chrono::nanoseconds(interceptKeyTimeout).count());
5044}
5045
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07005046/**
5047 * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set.
5048 */
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005049TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
5050 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005051 sp<FakeWindowHandle> window =
5052 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
5053 ui::LogicalDisplayId::DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005054 window->setFocusable(true);
5055
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005056 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005057 setFocusedWindow(window);
5058
5059 window->consumeFocusEvent(true);
5060
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005061 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
5062 window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07005063
5064 // Set a value that's significantly larger than the default consumption timeout. If the
5065 // implementation is correct, the actual value doesn't matter; it won't slow down the test.
5066 mFakePolicy->setInterceptKeyTimeout(600ms);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005067 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07005068 // Window should receive key event immediately when same key up.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005069 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08005070}
5071
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005072/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08005073 * Two windows. First is a regular window. Second does not overlap with the first, and has
5074 * WATCH_OUTSIDE_TOUCH.
5075 * Both windows are owned by the same UID.
5076 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
5077 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
5078 */
5079TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
5080 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005081 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
5082 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08005083 window->setFrame(Rect{0, 0, 100, 100});
5084
5085 sp<FakeWindowHandle> outsideWindow =
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07005086 sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005087 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08005088 outsideWindow->setFrame(Rect{100, 100, 200, 200});
5089 outsideWindow->setWatchOutsideTouch(true);
5090 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005091 mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08005092
5093 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005094 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005095 AINPUT_SOURCE_TOUCHSCREEN,
5096 ui::LogicalDisplayId::DEFAULT, {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08005097 window->consumeMotionDown();
5098 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
5099 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
5100 outsideWindow->consumeMotionEvent(
5101 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
Prabir Pradhan502a7252023-12-01 16:11:24 +00005102
5103 // Ensure outsideWindow doesn't get any more events for the gesture.
5104 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005105 ui::LogicalDisplayId::DEFAULT, {PointF{51, 51}}));
Prabir Pradhan502a7252023-12-01 16:11:24 +00005106 window->consumeMotionMove();
5107 outsideWindow->assertNoEvents();
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08005108}
5109
5110/**
Linnan Liccf6ce32024-04-11 20:32:13 +08005111 * Three windows:
5112 * - Left window
5113 * - Right window
5114 * - Outside window(watch for ACTION_OUTSIDE events)
5115 * The windows "left" and "outside" share the same owner, the window "right" has a different owner,
5116 * In order to allow the outside window can receive the ACTION_OUTSIDE events, the outside window is
5117 * positioned above the "left" and "right" windows, and it doesn't overlap with them.
5118 *
5119 * First, device A report a down event landed in the right window, the outside window can receive
5120 * an ACTION_OUTSIDE event that with zeroed coordinates, the device B report a down event landed
5121 * in the left window, the outside window can receive an ACTION_OUTSIDE event the with valid
5122 * coordinates, after these, device A and device B continue report MOVE event, the right and left
5123 * window can receive it, but outside window event can't receive it.
5124 */
5125TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinatesWhenMultiDevice) {
5126 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5127 sp<FakeWindowHandle> leftWindow =
5128 sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005129 ui::LogicalDisplayId::DEFAULT);
Linnan Liccf6ce32024-04-11 20:32:13 +08005130 leftWindow->setFrame(Rect{0, 0, 100, 100});
5131 leftWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
5132
5133 sp<FakeWindowHandle> outsideWindow =
5134 sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005135 ui::LogicalDisplayId::DEFAULT);
Linnan Liccf6ce32024-04-11 20:32:13 +08005136 outsideWindow->setFrame(Rect{100, 100, 200, 200});
5137 outsideWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
5138 outsideWindow->setWatchOutsideTouch(true);
5139
5140 std::shared_ptr<FakeApplicationHandle> anotherApplication =
5141 std::make_shared<FakeApplicationHandle>();
5142 sp<FakeWindowHandle> rightWindow =
5143 sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Right Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005144 ui::LogicalDisplayId::DEFAULT);
Linnan Liccf6ce32024-04-11 20:32:13 +08005145 rightWindow->setFrame(Rect{100, 0, 200, 100});
5146 rightWindow->setOwnerInfo(gui::Pid{2}, gui::Uid{202});
5147
5148 // OutsideWindow must be above left window and right window to receive ACTION_OUTSIDE events
5149 // when left window or right window is tapped
5150 mDispatcher->onWindowInfosChanged(
5151 {{*outsideWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()},
5152 {},
5153 0,
5154 0});
5155
5156 const DeviceId deviceA = 9;
5157 const DeviceId deviceB = 3;
5158
5159 // Tap on right window use device A
5160 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5161 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5162 .deviceId(deviceA)
5163 .build());
5164 leftWindow->assertNoEvents();
5165 rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA)));
5166 // Right window is belonged to another owner, so outsideWindow should receive ACTION_OUTSIDE
5167 // with zeroed coords.
5168 outsideWindow->consumeMotionEvent(
5169 AllOf(WithMotionAction(ACTION_OUTSIDE), WithDeviceId(deviceA), WithCoords(0, 0)));
5170
5171 // Tap on left window use device B
5172 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5173 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5174 .deviceId(deviceB)
5175 .build());
5176 leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB)));
5177 rightWindow->assertNoEvents();
5178 // Because new gesture down on the left window that has the same owner with outside Window, the
5179 // outside Window should receive the ACTION_OUTSIDE with coords.
5180 outsideWindow->consumeMotionEvent(
5181 AllOf(WithMotionAction(ACTION_OUTSIDE), WithDeviceId(deviceB), WithCoords(-50, -50)));
5182
5183 // Ensure that windows that can only accept outside do not receive remaining gestures
5184 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5185 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51))
5186 .deviceId(deviceA)
5187 .build());
5188 leftWindow->assertNoEvents();
5189 rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceA)));
5190
5191 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5192 .pointer(PointerBuilder(0, ToolType::FINGER).x(51).y(51))
5193 .deviceId(deviceB)
5194 .build());
5195 leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB)));
5196 rightWindow->assertNoEvents();
5197 outsideWindow->assertNoEvents();
5198}
5199
5200/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005201 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
5202 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
5203 * ACTION_OUTSIDE event is sent per gesture.
5204 */
5205TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
5206 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
5207 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005208 sp<FakeWindowHandle> window =
5209 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5210 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005211 window->setWatchOutsideTouch(true);
5212 window->setFrame(Rect{0, 0, 100, 100});
5213 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005214 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005215 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005216 secondWindow->setFrame(Rect{100, 100, 200, 200});
5217 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005218 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005219 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005220 thirdWindow->setFrame(Rect{200, 200, 300, 300});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005221 mDispatcher->onWindowInfosChanged(
5222 {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0});
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005223
5224 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005225 mDispatcher->notifyMotion(
5226 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5227 ui::LogicalDisplayId::DEFAULT, {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005228 window->assertNoEvents();
5229 secondWindow->assertNoEvents();
5230
5231 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
5232 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005233 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005234 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +00005235 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08005236 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
5237 window->consumeMotionEvent(
5238 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005239 secondWindow->consumeMotionDown();
5240 thirdWindow->assertNoEvents();
5241
5242 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
5243 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005244 mDispatcher->notifyMotion(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005245 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5246 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +00005247 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005248 window->assertNoEvents();
5249 secondWindow->consumeMotionMove();
5250 thirdWindow->consumeMotionDown();
5251}
5252
Prabir Pradhan814fe082022-07-22 20:22:18 +00005253TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
5254 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005255 sp<FakeWindowHandle> window =
5256 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
5257 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00005258 window->setFocusable(true);
5259
Patrick Williamsd828f302023-04-28 17:52:08 -05005260 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00005261 setFocusedWindow(window);
5262
5263 window->consumeFocusEvent(true);
5264
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005265 const NotifyKeyArgs keyDown =
5266 generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT);
5267 const NotifyKeyArgs keyUp =
5268 generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005269 mDispatcher->notifyKey(keyDown);
5270 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00005271
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005272 window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
5273 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00005274
5275 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05005276 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00005277
5278 window->consumeFocusEvent(false);
5279
Prabir Pradhan678438e2023-04-13 19:32:51 +00005280 mDispatcher->notifyKey(keyDown);
5281 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00005282 window->assertNoEvents();
5283}
5284
Arthur Hung96483742022-11-15 03:30:48 +00005285TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
5286 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005287 sp<FakeWindowHandle> window =
5288 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
5289 ui::LogicalDisplayId::DEFAULT);
Arthur Hung96483742022-11-15 03:30:48 +00005290 // Ensure window is non-split and have some transform.
5291 window->setPreventSplitting(true);
5292 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05005293 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00005294
5295 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005296 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
5297 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hung96483742022-11-15 03:30:48 +00005298 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005299 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung96483742022-11-15 03:30:48 +00005300
5301 const MotionEvent secondFingerDownEvent =
5302 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005303 .displayId(ui::LogicalDisplayId::DEFAULT)
Arthur Hung96483742022-11-15 03:30:48 +00005304 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005305 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
5306 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00005307 .build();
5308 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005309 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung96483742022-11-15 03:30:48 +00005310 InputEventInjectionSync::WAIT_FOR_RESULT))
5311 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5312
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005313 std::unique_ptr<MotionEvent> event = window->consumeMotionEvent();
5314 ASSERT_NE(nullptr, event);
5315 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
5316 EXPECT_EQ(70, event->getX(0)); // 50 + 20
5317 EXPECT_EQ(90, event->getY(0)); // 50 + 40
5318 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
5319 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
Arthur Hung96483742022-11-15 03:30:48 +00005320}
5321
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07005322/**
5323 * Two windows: a splittable and a non-splittable.
5324 * The non-splittable window shouldn't receive any "incomplete" gestures.
5325 * Send the first pointer to the splittable window, and then touch the non-splittable window.
5326 * The second pointer should be dropped because the initial window is splittable, so it won't get
5327 * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any
5328 * "incomplete" gestures.
5329 */
5330TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) {
5331 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5332 sp<FakeWindowHandle> leftWindow =
5333 sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005334 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07005335 leftWindow->setPreventSplitting(false);
5336 leftWindow->setFrame(Rect(0, 0, 100, 100));
5337 sp<FakeWindowHandle> rightWindow =
5338 sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005339 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07005340 rightWindow->setPreventSplitting(true);
5341 rightWindow->setFrame(Rect(100, 100, 200, 200));
5342 mDispatcher->onWindowInfosChanged(
5343 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
5344
5345 // Touch down on left, splittable window
5346 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5347 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5348 .build());
5349 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
5350
5351 mDispatcher->notifyMotion(
5352 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5353 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
5354 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
5355 .build());
5356 leftWindow->assertNoEvents();
5357 rightWindow->assertNoEvents();
5358}
5359
Harry Cuttsb166c002023-05-09 13:06:05 +00005360TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
5361 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005362 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
5363 ui::LogicalDisplayId::DEFAULT);
Harry Cuttsb166c002023-05-09 13:06:05 +00005364 window->setFrame(Rect(0, 0, 400, 400));
5365 sp<FakeWindowHandle> trustedOverlay =
5366 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005367 ui::LogicalDisplayId::DEFAULT);
Harry Cuttsb166c002023-05-09 13:06:05 +00005368 trustedOverlay->setSpy(true);
5369 trustedOverlay->setTrustedOverlay(true);
5370
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005371 mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00005372
5373 // Start a three-finger touchpad swipe
5374 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
5375 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
5376 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5377 .build());
5378 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
5379 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
5380 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
5381 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5382 .build());
5383 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
5384 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
5385 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
5386 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
5387 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5388 .build());
5389
5390 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
5391 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
5392 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
5393
5394 // Move the swipe a bit
5395 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
5396 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5397 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
5398 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
5399 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5400 .build());
5401
5402 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5403
5404 // End the swipe
5405 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
5406 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5407 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
5408 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
5409 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5410 .build());
5411 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
5412 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5413 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
5414 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5415 .build());
5416 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
5417 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5418 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5419 .build());
5420
5421 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
5422 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
5423 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
5424
5425 window->assertNoEvents();
5426}
5427
5428TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
5429 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005430 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
5431 ui::LogicalDisplayId::DEFAULT);
Harry Cuttsb166c002023-05-09 13:06:05 +00005432 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005433 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00005434
5435 // Start a three-finger touchpad swipe
5436 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
5437 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
5438 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5439 .build());
5440 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
5441 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
5442 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
5443 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5444 .build());
5445 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
5446 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
5447 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
5448 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
5449 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5450 .build());
5451
5452 // Move the swipe a bit
5453 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
5454 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5455 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
5456 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
5457 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5458 .build());
5459
5460 // End the swipe
5461 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
5462 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5463 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
5464 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
5465 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5466 .build());
5467 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
5468 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5469 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
5470 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5471 .build());
5472 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
5473 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
5474 .classification(MotionClassification::MULTI_FINGER_SWIPE)
5475 .build());
5476
5477 window->assertNoEvents();
5478}
5479
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00005480/**
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005481 * Send a two-pointer gesture to a single window. The window's orientation changes in response to
5482 * the first pointer.
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00005483 * Ensure that the second pointer and the subsequent gesture is correctly delivered to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005484 */
5485TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) {
5486 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005487 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
5488 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005489 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07005490 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005491
5492 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
5493 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5494 .downTime(baseTime + 10)
5495 .eventTime(baseTime + 10)
5496 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
5497 .build());
5498
5499 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
5500
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005501 // Change the transform so that the orientation is now different from original.
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07005502 window->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005503
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07005504 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005505
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005506 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5507 .downTime(baseTime + 10)
5508 .eventTime(baseTime + 30)
5509 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
5510 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
5511 .build());
5512
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00005513 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
5514
5515 // Finish the gesture and start a new one. Ensure all events are sent to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005516 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
5517 .downTime(baseTime + 10)
5518 .eventTime(baseTime + 40)
5519 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
5520 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
5521 .build());
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00005522
5523 window->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
5524
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005525 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
5526 .downTime(baseTime + 10)
5527 .eventTime(baseTime + 50)
5528 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
5529 .build());
5530
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00005531 window->consumeMotionEvent(WithMotionAction(ACTION_UP));
5532
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005533 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5534 .downTime(baseTime + 60)
5535 .eventTime(baseTime + 60)
5536 .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40))
5537 .build());
5538
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07005539 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005540}
5541
5542/**
Hu Guo771a7692023-09-17 20:51:08 +08005543 * When there are multiple screens, such as screen projection to TV or screen recording, if the
5544 * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and
5545 * its coordinates should be converted by the transform of the windows of target screen.
5546 */
5547TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) {
5548 // This case will create a window and a spy window on the default display and mirror
5549 // window on the second display. cancel event is sent through spy window pilferPointers
5550 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5551
5552 sp<FakeWindowHandle> spyWindowDefaultDisplay =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005553 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
5554 ui::LogicalDisplayId::DEFAULT);
Hu Guo771a7692023-09-17 20:51:08 +08005555 spyWindowDefaultDisplay->setTrustedOverlay(true);
5556 spyWindowDefaultDisplay->setSpy(true);
5557
5558 sp<FakeWindowHandle> windowDefaultDisplay =
5559 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005560 ui::LogicalDisplayId::DEFAULT);
Hu Guo771a7692023-09-17 20:51:08 +08005561 windowDefaultDisplay->setWindowTransform(1, 0, 0, 1);
5562
5563 sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID);
5564 windowSecondDisplay->setWindowTransform(2, 0, 0, 2);
5565
5566 // Add the windows to the dispatcher
5567 mDispatcher->onWindowInfosChanged(
5568 {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(),
5569 *windowSecondDisplay->getInfo()},
5570 {},
5571 0,
5572 0});
5573
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005574 // Send down to ui::LogicalDisplayId::DEFAULT
Hu Guo771a7692023-09-17 20:51:08 +08005575 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005576 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
5577 ui::LogicalDisplayId::DEFAULT, {100, 100}))
Hu Guo771a7692023-09-17 20:51:08 +08005578 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5579
5580 spyWindowDefaultDisplay->consumeMotionDown();
5581 windowDefaultDisplay->consumeMotionDown();
5582
5583 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken()));
5584
5585 // windowDefaultDisplay gets cancel
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005586 std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent();
5587 ASSERT_NE(nullptr, event);
5588 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction());
Hu Guo771a7692023-09-17 20:51:08 +08005589
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005590 // The cancel event is sent to windowDefaultDisplay of the ui::LogicalDisplayId::DEFAULT
5591 // display, so the coordinates of the cancel are converted by windowDefaultDisplay's transform,
5592 // the x and y coordinates are both 100, otherwise if the cancel event is sent to
5593 // windowSecondDisplay of SECOND_DISPLAY_ID, the x and y coordinates are 200
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005594 EXPECT_EQ(100, event->getX(0));
5595 EXPECT_EQ(100, event->getY(0));
Hu Guo771a7692023-09-17 20:51:08 +08005596}
5597
5598/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005599 * Ensure the correct coordinate spaces are used by InputDispatcher.
5600 *
5601 * InputDispatcher works in the display space, so its coordinate system is relative to the display
5602 * panel. Windows get events in the window space, and get raw coordinates in the logical display
5603 * space.
5604 */
5605class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
5606public:
5607 void SetUp() override {
5608 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005609 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005610 }
5611
Linnan Li13bf76a2024-05-05 19:18:02 +08005612 void addDisplayInfo(ui::LogicalDisplayId displayId, const ui::Transform& transform) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005613 gui::DisplayInfo info;
5614 info.displayId = displayId;
5615 info.transform = transform;
5616 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05005617 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005618 }
5619
5620 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
5621 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05005622 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005623 }
5624
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005625 void removeAllWindowsAndDisplays() {
5626 mDisplayInfos.clear();
5627 mWindowInfos.clear();
5628 }
5629
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005630 // Set up a test scenario where the display has a scaled projection and there are two windows
5631 // on the display.
5632 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
5633 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
5634 // respectively.
5635 ui::Transform displayTransform;
5636 displayTransform.set(2, 0, 0, 4);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005637 addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005638
5639 std::shared_ptr<FakeApplicationHandle> application =
5640 std::make_shared<FakeApplicationHandle>();
5641
5642 // Add two windows to the display. Their frames are represented in the display space.
5643 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005644 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005645 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005646 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
5647 addWindow(firstWindow);
5648
5649 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005650 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005651 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005652 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
5653 addWindow(secondWindow);
5654 return {std::move(firstWindow), std::move(secondWindow)};
5655 }
5656
5657private:
5658 std::vector<gui::DisplayInfo> mDisplayInfos;
5659 std::vector<gui::WindowInfo> mWindowInfos;
5660};
5661
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005662TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005663 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5664 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005665 // selected so that if the hit test was performed with the point and the bounds being in
5666 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005667 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005668 AINPUT_SOURCE_TOUCHSCREEN,
5669 ui::LogicalDisplayId::DEFAULT, {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005670
5671 firstWindow->consumeMotionDown();
5672 secondWindow->assertNoEvents();
5673}
5674
5675// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
5676// the event should be treated as being in the logical display space.
5677TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
5678 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5679 // Send down to the first window. The point is represented in the logical display space. The
5680 // point is selected so that if the hit test was done in logical display space, then it would
5681 // end up in the incorrect window.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005682 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005683 PointF{75 * 2, 55 * 4});
5684
5685 firstWindow->consumeMotionDown();
5686 secondWindow->assertNoEvents();
5687}
5688
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005689// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
5690// event should be treated as being in the logical display space.
5691TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
5692 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5693
5694 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5695 ui::Transform injectedEventTransform;
5696 injectedEventTransform.set(matrix);
5697 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
5698 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
5699
5700 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005701 .displayId(ui::LogicalDisplayId::DEFAULT)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005702 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005703 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005704 .x(untransformedPoint.x)
5705 .y(untransformedPoint.y))
5706 .build();
5707 event.transform(matrix);
5708
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005709 injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT,
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005710 InputEventInjectionSync::WAIT_FOR_RESULT);
5711
5712 firstWindow->consumeMotionDown();
5713 secondWindow->assertNoEvents();
5714}
5715
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005716TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
5717 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5718
5719 // Send down to the second window.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005720 mDispatcher->notifyMotion(
5721 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5722 ui::LogicalDisplayId::DEFAULT, {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005723
5724 firstWindow->assertNoEvents();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005725 std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent();
5726 ASSERT_NE(nullptr, event);
5727 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005728
5729 // Ensure that the events from the "getRaw" API are in logical display coordinates.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005730 EXPECT_EQ(300, event->getRawX(0));
5731 EXPECT_EQ(880, event->getRawY(0));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005732
5733 // Ensure that the x and y values are in the window's coordinate space.
5734 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
5735 // the logical display space. This will be the origin of the window space.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005736 EXPECT_EQ(100, event->getX(0));
5737 EXPECT_EQ(80, event->getY(0));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005738}
5739
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005740TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) {
5741 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5742 // The monitor will always receive events in the logical display's coordinate space, because
5743 // it does not have a window.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005744 FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ui::LogicalDisplayId::DEFAULT};
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005745
5746 // Send down to the first window.
5747 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005748 ui::LogicalDisplayId::DEFAULT, {PointF{50, 100}}));
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005749 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5750 monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5751
5752 // Second pointer goes down on second window.
5753 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005754 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005755 {PointF{50, 100}, PointF{150, 220}}));
5756 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80)));
5757 const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}},
5758 {1, PointF{300, 880}}};
5759 monitor.consumeMotionEvent(
5760 AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers)));
5761
5762 mDispatcher->cancelCurrentTouch();
5763
5764 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400)));
5765 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80)));
5766 monitor.consumeMotionEvent(
5767 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers)));
5768}
5769
Prabir Pradhan1c29a092023-09-21 10:29:29 +00005770TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) {
5771 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5772
5773 // Send down to the first window.
5774 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005775 ui::LogicalDisplayId::DEFAULT, {PointF{50, 100}}));
Prabir Pradhan1c29a092023-09-21 10:29:29 +00005776 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5777
5778 // The pointer is transferred to the second window, and the second window receives it in the
5779 // correct coordinate space.
Prabir Pradhan367f3432024-02-13 23:05:58 +00005780 mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken());
Prabir Pradhan1c29a092023-09-21 10:29:29 +00005781 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400)));
5782 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400)));
5783}
5784
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005785TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) {
5786 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5787
5788 // Send hover move to the second window, and ensure it shows up as hover enter.
5789 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005790 ui::LogicalDisplayId::DEFAULT,
5791 {PointF{150, 220}}));
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005792 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5793 WithCoords(100, 80), WithRawCoords(300, 880)));
5794
5795 // Touch down at the same location and ensure a hover exit is synthesized.
5796 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005797 ui::LogicalDisplayId::DEFAULT,
5798 {PointF{150, 220}}));
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005799 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5800 WithRawCoords(300, 880)));
5801 secondWindow->consumeMotionEvent(
5802 AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880)));
5803 secondWindow->assertNoEvents();
5804 firstWindow->assertNoEvents();
5805}
5806
Prabir Pradhan453ae732023-10-13 14:30:14 +00005807// Same as above, but while the window is being mirrored.
5808TEST_F(InputDispatcherDisplayProjectionTest,
5809 SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) {
5810 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5811
5812 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5813 ui::Transform secondDisplayTransform;
5814 secondDisplayTransform.set(matrix);
5815 addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform);
5816
5817 sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID);
5818 secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4);
5819 addWindow(secondWindowClone);
5820
5821 // Send hover move to the second window, and ensure it shows up as hover enter.
5822 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005823 ui::LogicalDisplayId::DEFAULT,
5824 {PointF{150, 220}}));
Prabir Pradhan453ae732023-10-13 14:30:14 +00005825 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5826 WithCoords(100, 80), WithRawCoords(300, 880)));
5827
5828 // Touch down at the same location and ensure a hover exit is synthesized for the correct
5829 // display.
5830 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005831 ui::LogicalDisplayId::DEFAULT,
5832 {PointF{150, 220}}));
Prabir Pradhan453ae732023-10-13 14:30:14 +00005833 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5834 WithRawCoords(300, 880)));
5835 secondWindow->consumeMotionEvent(
5836 AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880)));
5837 secondWindow->assertNoEvents();
5838 firstWindow->assertNoEvents();
5839}
5840
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005841TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) {
5842 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5843
5844 // Send hover enter to second window
5845 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005846 ui::LogicalDisplayId::DEFAULT,
5847 {PointF{150, 220}}));
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005848 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5849 WithCoords(100, 80), WithRawCoords(300, 880)));
5850
5851 mDispatcher->cancelCurrentTouch();
5852
5853 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5854 WithRawCoords(300, 880)));
5855 secondWindow->assertNoEvents();
5856 firstWindow->assertNoEvents();
5857}
5858
Prabir Pradhan453ae732023-10-13 14:30:14 +00005859// Same as above, but while the window is being mirrored.
Prabir Pradhan16463382023-10-12 23:03:19 +00005860TEST_F(InputDispatcherDisplayProjectionTest,
5861 SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) {
5862 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5863
5864 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5865 ui::Transform secondDisplayTransform;
5866 secondDisplayTransform.set(matrix);
5867 addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform);
5868
5869 sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID);
5870 secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4);
5871 addWindow(secondWindowClone);
5872
5873 // Send hover enter to second window
5874 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005875 ui::LogicalDisplayId::DEFAULT,
5876 {PointF{150, 220}}));
Prabir Pradhan16463382023-10-12 23:03:19 +00005877 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5878 WithCoords(100, 80), WithRawCoords(300, 880),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005879 WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Prabir Pradhan16463382023-10-12 23:03:19 +00005880
5881 mDispatcher->cancelCurrentTouch();
5882
5883 // Ensure the cancelation happens with the correct displayId and the correct coordinates.
5884 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5885 WithRawCoords(300, 880),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005886 WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Prabir Pradhan16463382023-10-12 23:03:19 +00005887 secondWindow->assertNoEvents();
5888 firstWindow->assertNoEvents();
5889}
5890
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005891/** Ensure consistent behavior of InputDispatcher in all orientations. */
5892class InputDispatcherDisplayOrientationFixture
5893 : public InputDispatcherDisplayProjectionTest,
5894 public ::testing::WithParamInterface<ui::Rotation> {};
5895
5896// This test verifies the touchable region of a window for all rotations of the display by tapping
5897// in different locations on the display, specifically points close to the four corners of a
5898// window.
5899TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
5900 constexpr static int32_t displayWidth = 400;
5901 constexpr static int32_t displayHeight = 800;
5902
5903 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5904
5905 const auto rotation = GetParam();
5906
5907 // Set up the display with the specified rotation.
5908 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
5909 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
5910 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
5911 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
5912 logicalDisplayWidth, logicalDisplayHeight);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005913 addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform);
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005914
5915 // Create a window with its bounds determined in the logical display.
5916 const Rect frameInLogicalDisplay(100, 100, 200, 300);
5917 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005918 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
5919 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005920 window->setFrame(frameInDisplay, displayTransform);
5921 addWindow(window);
5922
5923 // The following points in logical display space should be inside the window.
5924 static const std::array<vec2, 4> insidePoints{
5925 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
5926 for (const auto pointInsideWindow : insidePoints) {
5927 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
5928 const PointF pointInDisplaySpace{p.x, p.y};
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005929 mDispatcher->notifyMotion(
5930 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5931 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005932 window->consumeMotionDown();
5933
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005934 mDispatcher->notifyMotion(
5935 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5936 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005937 window->consumeMotionUp();
5938 }
5939
5940 // The following points in logical display space should be outside the window.
5941 static const std::array<vec2, 5> outsidePoints{
5942 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
5943 for (const auto pointOutsideWindow : outsidePoints) {
5944 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
5945 const PointF pointInDisplaySpace{p.x, p.y};
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005946 mDispatcher->notifyMotion(
5947 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5948 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005949
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005950 mDispatcher->notifyMotion(
5951 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5952 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005953 }
5954 window->assertNoEvents();
5955}
5956
Linnan Li5e5645e2024-03-05 14:43:05 +00005957// This test verifies the occlusion detection for all rotations of the display by tapping
5958// in different locations on the display, specifically points close to the four corners of a
5959// window.
5960TEST_P(InputDispatcherDisplayOrientationFixture, BlockUntrustClickInDifferentOrientations) {
5961 constexpr static int32_t displayWidth = 400;
5962 constexpr static int32_t displayHeight = 800;
5963
5964 std::shared_ptr<FakeApplicationHandle> untrustedWindowApplication =
5965 std::make_shared<FakeApplicationHandle>();
5966 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5967
5968 const auto rotation = GetParam();
5969
5970 // Set up the display with the specified rotation.
5971 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
5972 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
5973 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
5974 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
5975 logicalDisplayWidth, logicalDisplayHeight);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005976 addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform);
Linnan Li5e5645e2024-03-05 14:43:05 +00005977
5978 // Create a window that not trusted.
5979 const Rect untrustedWindowFrameInLogicalDisplay(100, 100, 200, 300);
5980
5981 const Rect untrustedWindowFrameInDisplay =
5982 displayTransform.inverse().transform(untrustedWindowFrameInLogicalDisplay);
5983
5984 sp<FakeWindowHandle> untrustedWindow =
5985 sp<FakeWindowHandle>::make(untrustedWindowApplication, mDispatcher, "UntrustedWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005986 ui::LogicalDisplayId::DEFAULT);
Linnan Li5e5645e2024-03-05 14:43:05 +00005987 untrustedWindow->setFrame(untrustedWindowFrameInDisplay, displayTransform);
5988 untrustedWindow->setTrustedOverlay(false);
5989 untrustedWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
5990 untrustedWindow->setTouchable(false);
5991 untrustedWindow->setAlpha(1.0f);
5992 untrustedWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
5993 addWindow(untrustedWindow);
5994
5995 // Create a simple app window below the untrusted window.
5996 const Rect simpleAppWindowFrameInLogicalDisplay(0, 0, 300, 600);
5997 const Rect simpleAppWindowFrameInDisplay =
5998 displayTransform.inverse().transform(simpleAppWindowFrameInLogicalDisplay);
5999
6000 sp<FakeWindowHandle> simpleAppWindow =
6001 sp<FakeWindowHandle>::make(application, mDispatcher, "SimpleAppWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006002 ui::LogicalDisplayId::DEFAULT);
Linnan Li5e5645e2024-03-05 14:43:05 +00006003 simpleAppWindow->setFrame(simpleAppWindowFrameInDisplay, displayTransform);
6004 simpleAppWindow->setOwnerInfo(gui::Pid{2}, gui::Uid{202});
6005 addWindow(simpleAppWindow);
6006
6007 // The following points in logical display space should be inside the untrusted window, so
6008 // the simple window could not receive events that coordinate is these point.
6009 static const std::array<vec2, 4> untrustedPoints{
6010 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
6011
6012 for (const auto untrustedPoint : untrustedPoints) {
6013 const vec2 p = displayTransform.inverse().transform(untrustedPoint);
6014 const PointF pointInDisplaySpace{p.x, p.y};
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006015 mDispatcher->notifyMotion(
6016 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6017 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
6018 mDispatcher->notifyMotion(
6019 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6020 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
Linnan Li5e5645e2024-03-05 14:43:05 +00006021 }
6022 untrustedWindow->assertNoEvents();
6023 simpleAppWindow->assertNoEvents();
6024 // The following points in logical display space should be outside the untrusted window, so
6025 // the simple window should receive events that coordinate is these point.
6026 static const std::array<vec2, 5> trustedPoints{
6027 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
6028 for (const auto trustedPoint : trustedPoints) {
6029 const vec2 p = displayTransform.inverse().transform(trustedPoint);
6030 const PointF pointInDisplaySpace{p.x, p.y};
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006031 mDispatcher->notifyMotion(
6032 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6033 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
6034 simpleAppWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
Linnan Li5e5645e2024-03-05 14:43:05 +00006035 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006036 mDispatcher->notifyMotion(
6037 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6038 ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace}));
6039 simpleAppWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
Linnan Li5e5645e2024-03-05 14:43:05 +00006040 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
6041 }
6042 untrustedWindow->assertNoEvents();
6043}
6044
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00006045// Run the precision tests for all rotations.
6046INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
6047 InputDispatcherDisplayOrientationFixture,
6048 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6049 ui::ROTATION_270),
6050 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6051 return ftl::enum_string(testParamInfo.param);
6052 });
6053
Siarhei Vishniakou18050092021-09-01 13:32:49 -07006054using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
6055 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006056
6057class TransferTouchFixture : public InputDispatcherTest,
6058 public ::testing::WithParamInterface<TransferFunction> {};
6059
6060TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07006061 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08006062
6063 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006064 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006065 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006066 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006067 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006068 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006069 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006070 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006071 sp<FakeWindowHandle> wallpaper =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006072 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper",
6073 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006074 wallpaper->setIsWallpaper(true);
Prabir Pradhan65455c72024-02-13 21:46:41 +00006075 // Add the windows to the dispatcher, and ensure the first window is focused
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006076 mDispatcher->onWindowInfosChanged(
6077 {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0});
Prabir Pradhan65455c72024-02-13 21:46:41 +00006078 setFocusedWindow(firstWindow);
6079 firstWindow->consumeFocusEvent(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006080
6081 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006082 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006083 AINPUT_SOURCE_TOUCHSCREEN,
6084 ui::LogicalDisplayId::DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00006085
Svet Ganov5d3bc372020-01-26 23:11:07 -08006086 // Only the first window should get the down event
6087 firstWindow->consumeMotionDown();
6088 secondWindow->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006089 wallpaper->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Prabir Pradhan65455c72024-02-13 21:46:41 +00006090 // Dispatcher reports pointer down outside focus for the wallpaper
6091 mFakePolicy->assertOnPointerDownEquals(wallpaper->getToken());
Svet Ganov5d3bc372020-01-26 23:11:07 -08006092
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006093 // Transfer touch to the second window
6094 TransferFunction f = GetParam();
6095 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
6096 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006097 // The first window gets cancel and the second gets down
6098 firstWindow->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006099 secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
6100 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6101 wallpaper->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Prabir Pradhan65455c72024-02-13 21:46:41 +00006102 // There should not be any changes to the focused window when transferring touch
6103 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertOnPointerDownWasNotCalled());
Svet Ganov5d3bc372020-01-26 23:11:07 -08006104
6105 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006106 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006107 ui::LogicalDisplayId::DEFAULT));
Prabir Pradhan65455c72024-02-13 21:46:41 +00006108 // The first window gets no events and the second gets up
Svet Ganov5d3bc372020-01-26 23:11:07 -08006109 firstWindow->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006110 secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
6111 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006112 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08006113}
6114
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006115/**
Prabir Pradhan367f3432024-02-13 23:05:58 +00006116 * When 'transferTouchGesture' API is invoked, dispatcher needs to find the "best" window to take
6117 * touch from. When we have spy windows, there are several windows to choose from: either spy, or
6118 * the 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006119 * natural to the user.
6120 * In this test, we are sending a pointer to both spy window and first window. We then try to
6121 * transfer touch to the second window. The dispatcher should identify the first window as the
6122 * one that should lose the gesture, and therefore the action should be to move the gesture from
6123 * the first window to the second.
Prabir Pradhan367f3432024-02-13 23:05:58 +00006124 * The main goal here is to test the behaviour of 'transferTouchGesture' API, but it's still valid
6125 * to test the other API, as well.
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006126 */
6127TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
6128 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6129
6130 // Create a couple of windows + a spy window
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006131 sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy",
6132 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006133 spyWindow->setTrustedOverlay(true);
6134 spyWindow->setSpy(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006135 sp<FakeWindowHandle> firstWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "First",
6136 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006137 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006138 sp<FakeWindowHandle>::make(application, mDispatcher, "Second",
6139 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006140
6141 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006142 mDispatcher->onWindowInfosChanged(
6143 {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006144
6145 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006146 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006147 AINPUT_SOURCE_TOUCHSCREEN,
6148 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006149 // Only the first window and spy should get the down event
6150 spyWindow->consumeMotionDown();
6151 firstWindow->consumeMotionDown();
6152
6153 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
Prabir Pradhan367f3432024-02-13 23:05:58 +00006154 // if f === 'transferTouchGesture'.
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006155 TransferFunction f = GetParam();
6156 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
6157 ASSERT_TRUE(success);
6158 // The first window gets cancel and the second gets down
6159 firstWindow->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006160 secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
6161 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006162
6163 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006164 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006165 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006166 // The first window gets no events and the second+spy get up
6167 firstWindow->assertNoEvents();
6168 spyWindow->consumeMotionUp();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006169 secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
6170 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07006171}
6172
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006173TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07006174 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08006175
6176 PointF touchPoint = {10, 10};
6177
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);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08006182 firstWindow->setPreventSplitting(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);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08006186 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006187
6188 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006189 mDispatcher->onWindowInfosChanged(
6190 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08006191
6192 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006193 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006194 AINPUT_SOURCE_TOUCHSCREEN,
6195 ui::LogicalDisplayId::DEFAULT, {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006196 // Only the first window should get the down event
6197 firstWindow->consumeMotionDown();
6198 secondWindow->assertNoEvents();
6199
6200 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006201 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006202 ui::LogicalDisplayId::DEFAULT,
6203 {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006204 // Only the first window should get the pointer down event
6205 firstWindow->consumeMotionPointerDown(1);
6206 secondWindow->assertNoEvents();
6207
6208 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006209 TransferFunction f = GetParam();
6210 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 and pointer 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 secondWindow->consumeMotionPointerDown(1, ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan65455c72024-02-13 21:46:41 +00006217 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006218
6219 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006220 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006221 ui::LogicalDisplayId::DEFAULT,
6222 {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006223 // The first window gets nothing and the second gets pointer up
6224 firstWindow->assertNoEvents();
Prabir Pradhan33cfc6d2024-06-11 20:17:44 +00006225 secondWindow->consumeMotionPointerUp(/*pointerIdx=*/1,
6226 AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT),
6227 WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE),
6228 WithPointerCount(2)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006229
6230 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006231 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006232 ui::LogicalDisplayId::DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006233 // The first window gets nothing and the second gets up
6234 firstWindow->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006235 secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
6236 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006237}
6238
Arthur Hungc539dbb2022-12-08 07:45:36 +00006239TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
6240 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6241
6242 // Create a couple of windows
6243 sp<FakeWindowHandle> firstWindow =
6244 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006245 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006246 firstWindow->setDupTouchToWallpaper(true);
6247 sp<FakeWindowHandle> secondWindow =
6248 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006249 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006250 secondWindow->setDupTouchToWallpaper(true);
6251
6252 sp<FakeWindowHandle> wallpaper1 =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006253 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1",
6254 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006255 wallpaper1->setIsWallpaper(true);
6256
6257 sp<FakeWindowHandle> wallpaper2 =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006258 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2",
6259 ui::LogicalDisplayId::DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006260 wallpaper2->setIsWallpaper(true);
6261 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006262 mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(),
6263 *secondWindow->getInfo(), *wallpaper2->getInfo()},
6264 {},
6265 0,
6266 0});
Arthur Hungc539dbb2022-12-08 07:45:36 +00006267
6268 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006269 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006270 AINPUT_SOURCE_TOUCHSCREEN,
6271 ui::LogicalDisplayId::DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00006272
6273 // Only the first window should get the down event
6274 firstWindow->consumeMotionDown();
6275 secondWindow->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006276 wallpaper1->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006277 wallpaper2->assertNoEvents();
6278
6279 // Transfer touch focus to the second window
6280 TransferFunction f = GetParam();
6281 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
6282 ASSERT_TRUE(success);
6283
6284 // The first window gets cancel and the second gets down
6285 firstWindow->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006286 secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
6287 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6288 wallpaper1->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS);
6289 wallpaper2->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
Linnan Li72352222024-04-12 18:55:57 +08006290 EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006291
6292 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006293 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006294 ui::LogicalDisplayId::DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00006295 // The first window gets no events and the second gets up
6296 firstWindow->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006297 secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
6298 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006299 wallpaper1->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006300 wallpaper2->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
Linnan Li72352222024-04-12 18:55:57 +08006301 EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006302}
6303
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006304// For the cases of single pointer touch and two pointers non-split touch, the api's
Prabir Pradhan367f3432024-02-13 23:05:58 +00006305// 'transferTouchGesture' and 'transferTouchOnDisplay' are equivalent in behaviour. They only differ
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006306// for the case where there are multiple pointers split across several windows.
Prabir Pradhan367f3432024-02-13 23:05:58 +00006307INSTANTIATE_TEST_SUITE_P(
6308 InputDispatcherTransferFunctionTests, TransferTouchFixture,
6309 ::testing::Values(
6310 [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> /*ignored*/,
6311 sp<IBinder> destChannelToken) {
6312 return dispatcher->transferTouchOnDisplay(destChannelToken,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006313 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan367f3432024-02-13 23:05:58 +00006314 },
6315 [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> from,
6316 sp<IBinder> to) {
6317 return dispatcher->transferTouchGesture(from, to,
6318 /*isDragAndDrop=*/false);
6319 }));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006320
Prabir Pradhan367f3432024-02-13 23:05:58 +00006321TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07006322 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08006323
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006324 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006325 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006326 ui::LogicalDisplayId::DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006327 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006328
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006329 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006330 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006331 ui::LogicalDisplayId::DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006332 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006333
6334 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006335 mDispatcher->onWindowInfosChanged(
6336 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08006337
6338 PointF pointInFirst = {300, 200};
6339 PointF pointInSecond = {300, 600};
6340
6341 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006342 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006343 AINPUT_SOURCE_TOUCHSCREEN,
6344 ui::LogicalDisplayId::DEFAULT, {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006345 // Only the first window should get the down event
6346 firstWindow->consumeMotionDown();
6347 secondWindow->assertNoEvents();
6348
6349 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006350 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006351 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +00006352 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006353 // The first window gets a move and the second a down
6354 firstWindow->consumeMotionMove();
6355 secondWindow->consumeMotionDown();
6356
Prabir Pradhan367f3432024-02-13 23:05:58 +00006357 // Transfer touch to the second window
6358 mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken());
Svet Ganov5d3bc372020-01-26 23:11:07 -08006359 // The first window gets cancel and the new gets pointer down (it already saw down)
6360 firstWindow->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006361 secondWindow->consumeMotionPointerDown(1, ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan65455c72024-02-13 21:46:41 +00006362 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006363
6364 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006365 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006366 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +00006367 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006368 // The first window gets nothing and the second gets pointer up
6369 firstWindow->assertNoEvents();
Prabir Pradhan33cfc6d2024-06-11 20:17:44 +00006370 secondWindow->consumeMotionPointerUp(/*pointerIdx=*/1,
6371 AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT),
6372 WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE),
6373 WithPointerCount(2)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006374
6375 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006376 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006377 ui::LogicalDisplayId::DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08006378 // The first window gets nothing and the second gets up
6379 firstWindow->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006380 secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
6381 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Svet Ganov5d3bc372020-01-26 23:11:07 -08006382}
6383
Prabir Pradhan367f3432024-02-13 23:05:58 +00006384// Same as TransferTouch_TwoPointersSplitTouch, but using 'transferTouchOnDisplay' api.
6385// Unlike 'transferTouchGesture', calling 'transferTouchOnDisplay' when there are two windows
6386// receiving touch is not supported, so the touch should continue on those windows and the
6387// transferred-to window should get nothing.
6388TEST_F(InputDispatcherTest, TransferTouchOnDisplay_TwoPointersSplitTouch) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006389 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6390
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006391 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006392 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006393 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006394 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006395
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006396 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006397 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006398 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006399 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006400
6401 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006402 mDispatcher->onWindowInfosChanged(
6403 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006404
6405 PointF pointInFirst = {300, 200};
6406 PointF pointInSecond = {300, 600};
6407
6408 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006409 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006410 AINPUT_SOURCE_TOUCHSCREEN,
6411 ui::LogicalDisplayId::DEFAULT, {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006412 // Only the first window should get the down event
6413 firstWindow->consumeMotionDown();
6414 secondWindow->assertNoEvents();
6415
6416 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006417 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006418 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +00006419 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006420 // The first window gets a move and the second a down
6421 firstWindow->consumeMotionMove();
6422 secondWindow->consumeMotionDown();
6423
6424 // Transfer touch focus to the second window
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006425 const bool transferred = mDispatcher->transferTouchOnDisplay(secondWindow->getToken(),
6426 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan367f3432024-02-13 23:05:58 +00006427 // The 'transferTouchOnDisplay' call should not succeed, because there are 2 touched windows
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006428 ASSERT_FALSE(transferred);
6429 firstWindow->assertNoEvents();
6430 secondWindow->assertNoEvents();
6431
6432 // The rest of the dispatch should proceed as normal
6433 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006434 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006435 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +00006436 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006437 // The first window gets MOVE and the second gets pointer up
6438 firstWindow->consumeMotionMove();
6439 secondWindow->consumeMotionUp();
6440
6441 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006442 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006443 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00006444 // The first window gets nothing and the second gets up
6445 firstWindow->consumeMotionUp();
6446 secondWindow->assertNoEvents();
6447}
6448
Arthur Hungabbb9d82021-09-01 14:52:30 +00006449// This case will create two windows and one mirrored window on the default display and mirror
Prabir Pradhan367f3432024-02-13 23:05:58 +00006450// two windows on the second display. It will test if 'transferTouchGesture' works fine if we put
Arthur Hungabbb9d82021-09-01 14:52:30 +00006451// the windows info of second display before default display.
Prabir Pradhan367f3432024-02-13 23:05:58 +00006452TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00006453 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6454 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006455 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1",
6456 ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006457 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006458 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006459 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2",
6460 ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006461 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006462
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006463 sp<FakeWindowHandle> mirrorWindowInPrimary =
6464 firstWindowInPrimary->clone(ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006465 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006466
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07006467 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006468 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006469
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07006470 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006471 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006472
6473 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006474 mDispatcher->onWindowInfosChanged(
6475 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
6476 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
6477 *secondWindowInPrimary->getInfo()},
6478 {},
6479 0,
6480 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00006481
6482 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006483 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
6484 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00006485 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6486
6487 // Window should receive motion event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006488 firstWindowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006489
Prabir Pradhan367f3432024-02-13 23:05:58 +00006490 // Transfer touch
6491 ASSERT_TRUE(mDispatcher->transferTouchGesture(firstWindowInPrimary->getToken(),
6492 secondWindowInPrimary->getToken()));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006493 // The first window gets cancel.
6494 firstWindowInPrimary->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006495 secondWindowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan65455c72024-02-13 21:46:41 +00006496 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006497
6498 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006499 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006500 ui::LogicalDisplayId::DEFAULT, {150, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00006501 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6502 firstWindowInPrimary->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006503 secondWindowInPrimary->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan65455c72024-02-13 21:46:41 +00006504 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006505
6506 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006507 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Arthur Hungabbb9d82021-09-01 14:52:30 +00006508 {150, 50}))
6509 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6510 firstWindowInPrimary->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006511 secondWindowInPrimary->consumeMotionUp(ui::LogicalDisplayId::DEFAULT,
6512 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006513}
6514
Prabir Pradhan367f3432024-02-13 23:05:58 +00006515// Same as TransferTouch_CloneSurface, but this touch on the secondary display and use
6516// 'transferTouchOnDisplay' api.
6517TEST_F(InputDispatcherTest, TransferTouchOnDisplay_CloneSurface) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00006518 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6519 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006520 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1",
6521 ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006522 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006523 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006524 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2",
6525 ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006526 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006527
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006528 sp<FakeWindowHandle> mirrorWindowInPrimary =
6529 firstWindowInPrimary->clone(ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006530 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006531
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07006532 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006533 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006534
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07006535 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006536 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006537
6538 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006539 mDispatcher->onWindowInfosChanged(
6540 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
6541 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
6542 *secondWindowInPrimary->getInfo()},
6543 {},
6544 0,
6545 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00006546
6547 // Touch on second display.
6548 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006549 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
6550 {50, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00006551 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6552
6553 // Window should receive motion event.
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006554 firstWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006555
6556 // Transfer touch focus
Prabir Pradhan367f3432024-02-13 23:05:58 +00006557 ASSERT_TRUE(mDispatcher->transferTouchOnDisplay(secondWindowInSecondary->getToken(),
6558 SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00006559
6560 // The first window gets cancel.
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006561 firstWindowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
Prabir Pradhan65455c72024-02-13 21:46:41 +00006562 secondWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID,
6563 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006564
6565 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006566 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungabbb9d82021-09-01 14:52:30 +00006567 SECOND_DISPLAY_ID, {150, 50}))
6568 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006569 firstWindowInSecondary->assertNoEvents();
Prabir Pradhan65455c72024-02-13 21:46:41 +00006570 secondWindowInSecondary->consumeMotionMove(SECOND_DISPLAY_ID,
6571 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006572
6573 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006574 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00006575 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006576 firstWindowInSecondary->assertNoEvents();
Prabir Pradhan65455c72024-02-13 21:46:41 +00006577 secondWindowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00006578}
6579
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006580TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006581 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006582 sp<FakeWindowHandle> window =
6583 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6584 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006585
Vishnu Nair47074b82020-08-14 11:54:47 -07006586 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006587 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006588 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006589
6590 window->consumeFocusEvent(true);
6591
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006592 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006593
6594 // Window should receive key down event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006595 window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00006596
6597 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00006598 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00006599 mFakePolicy->assertUserActivityPoked();
6600}
6601
6602TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
6603 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006604 sp<FakeWindowHandle> window =
6605 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6606 ui::LogicalDisplayId::DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00006607
6608 window->setDisableUserActivity(true);
6609 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006610 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00006611 setFocusedWindow(window);
6612
6613 window->consumeFocusEvent(true);
6614
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006615 mDispatcher->notifyKey(
6616 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
Josep del Riob3981622023-04-18 15:49:45 +00006617
6618 // Window should receive key down event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006619 window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00006620
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006621 // Should have not poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00006622 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00006623 mFakePolicy->assertUserActivityNotPoked();
6624}
6625
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006626TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceivePolicyConsumedKey) {
Josep del Riob3981622023-04-18 15:49:45 +00006627 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006628 sp<FakeWindowHandle> window =
6629 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6630 ui::LogicalDisplayId::DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00006631
6632 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006633 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00006634 setFocusedWindow(window);
6635
6636 window->consumeFocusEvent(true);
6637
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006638 mFakePolicy->setConsumeKeyBeforeDispatching(true);
6639
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006640 mDispatcher->notifyKey(
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006641 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
Josep del Riob3981622023-04-18 15:49:45 +00006642 mDispatcher->waitForIdle();
6643
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006644 // Key is not passed down
Josep del Riob3981622023-04-18 15:49:45 +00006645 window->assertNoEvents();
6646
6647 // Should have poked user activity
6648 mFakePolicy->assertUserActivityPoked();
6649}
6650
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006651TEST_F(InputDispatcherTest, FocusedWindow_PolicyConsumedKeyIgnoresDisableUserActivity) {
Josep del Riob3981622023-04-18 15:49:45 +00006652 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006653 sp<FakeWindowHandle> window =
6654 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6655 ui::LogicalDisplayId::DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00006656
6657 window->setDisableUserActivity(true);
6658 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006659 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00006660 setFocusedWindow(window);
6661
6662 window->consumeFocusEvent(true);
6663
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006664 mFakePolicy->setConsumeKeyBeforeDispatching(true);
6665
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006666 mDispatcher->notifyKey(
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006667 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
Josep del Riob3981622023-04-18 15:49:45 +00006668 mDispatcher->waitForIdle();
6669
6670 // System key is not passed down
6671 window->assertNoEvents();
6672
6673 // Should have poked user activity
6674 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006675}
6676
Josep del Rioc8fdedb2024-05-21 13:32:43 +00006677class DisableUserActivityInputDispatcherTest : public InputDispatcherTest,
6678 public ::testing::WithParamInterface<bool> {};
6679
6680TEST_P(DisableUserActivityInputDispatcherTest, NotPassedToUserUserActivity) {
6681 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6682 sp<FakeWindowHandle> window =
6683 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6684 ui::LogicalDisplayId::DEFAULT);
6685
6686 window->setDisableUserActivity(GetParam());
6687
6688 window->setFocusable(true);
6689 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
6690 setFocusedWindow(window);
6691
6692 window->consumeFocusEvent(true);
6693
6694 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
6695 .keyCode(AKEYCODE_A)
6696 .policyFlags(0)
6697 .build());
6698 mDispatcher->waitForIdle();
6699
6700 // Key is not passed down
6701 window->assertNoEvents();
6702
6703 // Should not have poked user activity
6704 mFakePolicy->assertUserActivityNotPoked();
6705}
6706
6707INSTANTIATE_TEST_CASE_P(DisableUserActivity, DisableUserActivityInputDispatcherTest,
6708 ::testing::Bool());
6709
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07006710TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
6711 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006712 sp<FakeWindowHandle> window =
6713 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6714 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07006715
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006716 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07006717
6718 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006719 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006720 ui::LogicalDisplayId::DEFAULT, {100, 100}))
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07006721 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6722
6723 window->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006724 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07006725
6726 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00006727 mDispatcher->waitForIdle();
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07006728 mFakePolicy->assertUserActivityPoked();
6729}
6730
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006731TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006732 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006733 sp<FakeWindowHandle> window =
6734 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6735 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006736
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006737 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006738
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006739 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006740 mDispatcher->waitForIdle();
6741
6742 window->assertNoEvents();
6743}
6744
6745// If a window is touchable, but does not have focus, it should receive motion events, but not keys
6746TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07006747 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006748 sp<FakeWindowHandle> window =
6749 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6750 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006751
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006752 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006753
6754 // Send key
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006755 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006756 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00006757 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006758 AINPUT_SOURCE_TOUCHSCREEN,
6759 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006760
6761 // Window should receive only the motion event
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006762 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006763 window->assertNoEvents(); // Key event or focus event will not be received
6764}
6765
arthurhungea3f4fc2020-12-21 23:18:53 +08006766TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
6767 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6768
arthurhungea3f4fc2020-12-21 23:18:53 +08006769 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006770 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006771 ui::LogicalDisplayId::DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08006772 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08006773
arthurhungea3f4fc2020-12-21 23:18:53 +08006774 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006775 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006776 ui::LogicalDisplayId::DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08006777 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08006778
6779 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006780 mDispatcher->onWindowInfosChanged(
6781 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
arthurhungea3f4fc2020-12-21 23:18:53 +08006782
6783 PointF pointInFirst = {300, 200};
6784 PointF pointInSecond = {300, 600};
6785
6786 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006787 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006788 AINPUT_SOURCE_TOUCHSCREEN,
6789 ui::LogicalDisplayId::DEFAULT, {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08006790 // Only the first window should get the down event
6791 firstWindow->consumeMotionDown();
6792 secondWindow->assertNoEvents();
6793
6794 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006795 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006796 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +00006797 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08006798 // The first window gets a move and the second a down
6799 firstWindow->consumeMotionMove();
6800 secondWindow->consumeMotionDown();
6801
6802 // Send pointer cancel to the second window
6803 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006804 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
6805 ui::LogicalDisplayId::DEFAULT, {pointInFirst, pointInSecond});
arthurhungea3f4fc2020-12-21 23:18:53 +08006806 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00006807 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08006808 // The first window gets move and the second gets cancel.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006809 firstWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
6810 secondWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
arthurhungea3f4fc2020-12-21 23:18:53 +08006811
6812 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00006813 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006814 ui::LogicalDisplayId::DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08006815 // The first window gets up and the second gets nothing.
6816 firstWindow->consumeMotionUp();
6817 secondWindow->assertNoEvents();
6818}
6819
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006820TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
6821 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6822
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006823 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
6824 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006825 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006826 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
6827 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
6828 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
6829
Harry Cutts33476232023-01-30 19:57:29 +00006830 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006831 window->assertNoEvents();
6832 mDispatcher->waitForIdle();
6833}
6834
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006835using InputDispatcherMonitorTest = InputDispatcherTest;
6836
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006837/**
6838 * Two entities that receive touch: A window, and a global monitor.
6839 * The touch goes to the window, and then the window disappears.
6840 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
6841 * for the monitor, as well.
6842 * 1. foregroundWindow
6843 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
6844 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006845TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006846 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006847 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground",
6848 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006849
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006850 FakeMonitorReceiver monitor =
6851 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006852
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006853 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006854 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006855 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
6856 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006857 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6858
6859 // Both the foreground window and the global monitor should receive the touch down
6860 window->consumeMotionDown();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006861 monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006862
6863 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006864 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006865 ui::LogicalDisplayId::DEFAULT, {110, 200}))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006866 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6867
6868 window->consumeMotionMove();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006869 monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006870
6871 // Now the foreground window goes away
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006872 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006873 window->consumeMotionCancel();
6874 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
6875
6876 // If more events come in, there will be no more foreground window to send them to. This will
6877 // cause a cancel for the monitor, as well.
6878 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006879 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006880 ui::LogicalDisplayId::DEFAULT, {120, 200}))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006881 << "Injection should fail because the window was removed";
6882 window->assertNoEvents();
6883 // Global monitor now gets the cancel
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006884 monitor.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006885}
6886
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006887TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07006888 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006889 sp<FakeWindowHandle> window =
6890 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6891 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006892 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00006893
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006894 FakeMonitorReceiver monitor =
6895 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006896
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006897 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006898 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
6899 ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006900 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006901 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
6902 monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006903}
6904
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006905TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006906 FakeMonitorReceiver monitor =
6907 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006908
Chris Yea209fde2020-07-22 13:54:51 -07006909 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006910 sp<FakeWindowHandle> window =
6911 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6912 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006913 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00006914
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006915 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006916 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
6917 ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006918 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006919 monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
6920 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006921
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006922 // Pilfer pointers from the monitor.
6923 // This should not do anything and the window should continue to receive events.
6924 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00006925
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006926 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006927 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006928 ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006929 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006930
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006931 monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT);
6932 window->consumeMotionMove(ui::LogicalDisplayId::DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006933}
6934
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006935TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07006936 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006937 sp<FakeWindowHandle> window =
6938 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
6939 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006940 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Evan Rosky84f07f02021-04-16 10:42:42 -07006941 window->setWindowOffset(20, 40);
6942 window->setWindowTransform(0, 1, -1, 0);
6943
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006944 FakeMonitorReceiver monitor =
6945 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07006946
6947 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006948 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
6949 ui::LogicalDisplayId::DEFAULT))
Evan Rosky84f07f02021-04-16 10:42:42 -07006950 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006951 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08006952 std::unique_ptr<MotionEvent> event = monitor.consumeMotion();
6953 ASSERT_NE(nullptr, event);
Evan Rosky84f07f02021-04-16 10:42:42 -07006954 // Even though window has transform, gesture monitor must not.
6955 ASSERT_EQ(ui::Transform(), event->getTransform());
6956}
6957
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006958TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00006959 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006960 FakeMonitorReceiver monitor =
6961 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00006962
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006963 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006964 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
6965 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006966 << "Injection should fail if there is a monitor, but no touchable window";
6967 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00006968}
6969
Linnan Lid8150952024-01-26 18:07:17 +00006970/**
6971 * Two displays
6972 * The first monitor has a foreground window, a monitor
6973 * The second window has only one monitor.
6974 * We first inject a Down event into the first display, this injection should succeed and both
6975 * the foreground window and monitor should receive a down event, then inject a Down event into
6976 * the second display as well, this injection should fail, at this point, the first display
6977 * window and monitor should not receive a cancel or any other event.
6978 * Continue to inject Move and UP events to the first display, the events should be received
6979 * normally by the foreground window and monitor.
6980 */
6981TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) {
6982 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006983 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground",
6984 ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006985
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006986 FakeMonitorReceiver monitor =
6987 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006988 FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
6989
6990 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
6991 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006992 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
6993 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Linnan Lid8150952024-01-26 18:07:17 +00006994 << "The down event injected into the first display should succeed";
6995
6996 window->consumeMotionDown();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006997 monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006998
6999 ASSERT_EQ(InputEventInjectionResult::FAILED,
7000 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
7001 {100, 200}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007002 << "The down event injected into the second display should fail since there's no "
7003 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00007004
7005 // Continue to inject event to first display.
7006 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7007 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007008 ui::LogicalDisplayId::DEFAULT, {110, 220}))
Linnan Lid8150952024-01-26 18:07:17 +00007009 << "The move event injected into the first display should succeed";
7010
7011 window->consumeMotionMove();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007012 monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007013
7014 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007015 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Linnan Lid8150952024-01-26 18:07:17 +00007016 {110, 220}))
7017 << "The up event injected into the first display should succeed";
7018
7019 window->consumeMotionUp();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007020 monitor.consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007021
7022 window->assertNoEvents();
7023 monitor.assertNoEvents();
Linnan Lid8150952024-01-26 18:07:17 +00007024 secondMonitor.assertNoEvents();
7025}
7026
7027/**
7028 * Two displays
7029 * There is a monitor and foreground window on each display.
7030 * First, we inject down events into each of the two displays, at this point, the foreground windows
7031 * and monitors on both displays should receive down events.
7032 * At this point, the foreground window of the second display goes away, the gone window should
7033 * receive the cancel event, and the other windows and monitors should not receive any events.
7034 * Inject a move event into the second display. At this point, the injection should fail because
7035 * the second display no longer has a foreground window. At this point, the monitor on the second
7036 * display should receive a cancel event, and any windows or monitors on the first display should
7037 * not receive any events, and any subsequent injection of events into the second display should
7038 * also fail.
7039 * Continue to inject events into the first display, and the events should all be injected
7040 * successfully and received normally.
7041 */
7042TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCancelWhenAnotherDisplayMonitorTouchCanceled) {
7043 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007044 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground",
7045 ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007046 sp<FakeWindowHandle> secondWindow =
7047 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondForeground",
7048 SECOND_DISPLAY_ID);
7049
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007050 FakeMonitorReceiver monitor =
7051 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007052 FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
7053
7054 // There is a foreground window on both displays.
7055 mDispatcher->onWindowInfosChanged({{*window->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
7056 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007057 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7058 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Linnan Lid8150952024-01-26 18:07:17 +00007059 << "The down event injected into the first display should succeed";
7060
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007061 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
7062 monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007063
7064 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7065 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
7066 {100, 200}))
7067 << "The down event injected into the second display should succeed";
7068
Linnan Lid8150952024-01-26 18:07:17 +00007069 secondWindow->consumeMotionDown(SECOND_DISPLAY_ID);
7070 secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID);
7071
7072 // Now second window is gone away.
7073 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
7074
7075 // The gone window should receive a cancel, and the monitor on the second display should not
7076 // receive any events.
Linnan Lid8150952024-01-26 18:07:17 +00007077 secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID);
7078 secondMonitor.assertNoEvents();
7079
7080 ASSERT_EQ(InputEventInjectionResult::FAILED,
7081 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7082 SECOND_DISPLAY_ID, {110, 220}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007083 << "The move event injected into the second display should fail because there's no "
7084 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00007085 // Now the monitor on the second display should receive a cancel event.
7086 secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID);
Linnan Lid8150952024-01-26 18:07:17 +00007087
7088 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7089 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007090 ui::LogicalDisplayId::DEFAULT, {110, 200}))
Linnan Lid8150952024-01-26 18:07:17 +00007091 << "The move event injected into the first display should succeed";
7092
7093 window->consumeMotionMove();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007094 monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007095
7096 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007097 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
7098 {110, 220}))
7099 << "The up event injected into the second display should fail because there's no "
7100 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00007101
7102 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007103 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Linnan Lid8150952024-01-26 18:07:17 +00007104 {110, 220}))
7105 << "The up event injected into the first display should succeed";
7106
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007107 window->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
7108 monitor.consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007109
Linnan Lid8150952024-01-26 18:07:17 +00007110 window->assertNoEvents();
7111 monitor.assertNoEvents();
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007112 secondWindow->assertNoEvents();
7113 secondMonitor.assertNoEvents();
Linnan Lid8150952024-01-26 18:07:17 +00007114}
7115
7116/**
7117 * One display with transform
7118 * There is a foreground window and a monitor on the display
7119 * Inject down event and move event sequentially, the foreground window and monitor can receive down
7120 * event and move event, then let the foreground window go away, the foreground window receives
7121 * cancel event, inject move event again, the monitor receives cancel event, all the events received
7122 * by the monitor should be with the same transform as the display
7123 */
7124TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) {
7125 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007126 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground",
7127 ui::LogicalDisplayId::DEFAULT);
7128 FakeMonitorReceiver monitor =
7129 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00007130
7131 ui::Transform transform;
7132 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
7133
7134 gui::DisplayInfo displayInfo;
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007135 displayInfo.displayId = ui::LogicalDisplayId::DEFAULT;
Linnan Lid8150952024-01-26 18:07:17 +00007136 displayInfo.transform = transform;
7137
7138 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
7139
7140 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007141 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
7142 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Linnan Lid8150952024-01-26 18:07:17 +00007143 << "The down event injected should succeed";
7144
7145 window->consumeMotionDown();
7146 std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion();
7147 EXPECT_EQ(transform, downMotionEvent->getTransform());
7148 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction());
7149
7150 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7151 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007152 ui::LogicalDisplayId::DEFAULT, {110, 220}))
Linnan Lid8150952024-01-26 18:07:17 +00007153 << "The move event injected should succeed";
7154
7155 window->consumeMotionMove();
7156 std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion();
7157 EXPECT_EQ(transform, moveMotionEvent->getTransform());
7158 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction());
7159
7160 // Let foreground window gone
7161 mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0});
7162
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007163 // Foreground window should receive a cancel event, but not the monitor.
Linnan Lid8150952024-01-26 18:07:17 +00007164 window->consumeMotionCancel();
Linnan Lid8150952024-01-26 18:07:17 +00007165
7166 ASSERT_EQ(InputEventInjectionResult::FAILED,
7167 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007168 ui::LogicalDisplayId::DEFAULT, {110, 220}))
Linnan Lid8150952024-01-26 18:07:17 +00007169 << "The move event injected should failed";
7170 // Now foreground should not receive any events, but monitor should receive a cancel event
7171 // with transform that same as display's display.
Linnan Lid8150952024-01-26 18:07:17 +00007172 std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion();
7173 EXPECT_EQ(transform, cancelMotionEvent->getTransform());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007174 EXPECT_EQ(ui::LogicalDisplayId::DEFAULT, cancelMotionEvent->getDisplayId());
Linnan Lid8150952024-01-26 18:07:17 +00007175 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction());
7176
7177 // Other event inject to this display should fail.
7178 ASSERT_EQ(InputEventInjectionResult::FAILED,
7179 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007180 ui::LogicalDisplayId::DEFAULT, {110, 220}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00007181 << "The up event injected should fail because the touched window was removed";
Linnan Lid8150952024-01-26 18:07:17 +00007182 window->assertNoEvents();
7183 monitor.assertNoEvents();
7184}
7185
chaviw81e2bb92019-12-18 15:03:51 -08007186TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07007187 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007188 sp<FakeWindowHandle> window =
7189 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
7190 ui::LogicalDisplayId::DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08007191
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007192 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
chaviw81e2bb92019-12-18 15:03:51 -08007193
7194 NotifyMotionArgs motionArgs =
7195 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007196 ui::LogicalDisplayId::DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08007197
Prabir Pradhan678438e2023-04-13 19:32:51 +00007198 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08007199 // Window should receive motion down event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007200 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08007201
7202 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08007203 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08007204 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
7205 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
7206 motionArgs.pointerCoords[0].getX() - 10);
7207
Prabir Pradhan678438e2023-04-13 19:32:51 +00007208 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007209 window->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08007210}
7211
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007212/**
7213 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
7214 * the device default right away. In the test scenario, we check both the default value,
7215 * and the action of enabling / disabling.
7216 */
7217TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07007218 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007219 sp<FakeWindowHandle> window =
7220 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
7221 ui::LogicalDisplayId::DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08007222 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007223
7224 // Set focused application.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007225 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07007226 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007227
7228 SCOPED_TRACE("Check default value of touch mode");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007229 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007230 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007231 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007232
7233 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07007234 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007235 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00007236 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007237
7238 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08007239 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007240 /*hasPermission=*/true, ui::LogicalDisplayId::DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007241 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07007242 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007243 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007244 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007245 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007246
7247 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07007248 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007249 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00007250 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007251
7252 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08007253 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007254 /*hasPermission=*/true, ui::LogicalDisplayId::DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00007255 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07007256 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007257 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007258 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00007259 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007260
7261 window->assertNoEvents();
7262}
7263
Gang Wange9087892020-01-07 12:17:14 -05007264TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07007265 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007266 sp<FakeWindowHandle> window =
7267 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
7268 ui::LogicalDisplayId::DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05007269
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007270 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07007271 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05007272
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007273 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007274 setFocusedWindow(window);
7275
Harry Cutts33476232023-01-30 19:57:29 +00007276 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05007277
Prabir Pradhan678438e2023-04-13 19:32:51 +00007278 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
7279 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05007280
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007281 std::unique_ptr<KeyEvent> event = window->consumeKey();
7282 ASSERT_NE(event, nullptr);
7283 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
Gang Wange9087892020-01-07 12:17:14 -05007284 ASSERT_NE(verified, nullptr);
7285 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
7286
7287 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
7288 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
7289 ASSERT_EQ(keyArgs.source, verified->source);
7290 ASSERT_EQ(keyArgs.displayId, verified->displayId);
7291
7292 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
7293
7294 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05007295 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08007296 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05007297 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
7298 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
7299 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
7300 ASSERT_EQ(0, verifiedKey.repeatCount);
7301}
7302
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007303TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07007304 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007305 sp<FakeWindowHandle> window =
7306 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
7307 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007308
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007309 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007310
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07007311 ui::Transform transform;
7312 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
7313
7314 gui::DisplayInfo displayInfo;
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007315 displayInfo.displayId = ui::LogicalDisplayId::DEFAULT;
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07007316 displayInfo.transform = transform;
7317
Patrick Williamsd828f302023-04-28 17:52:08 -05007318 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007319
Prabir Pradhan678438e2023-04-13 19:32:51 +00007320 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007321 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007322 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007323 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007324
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007325 std::unique_ptr<MotionEvent> event = window->consumeMotionEvent();
7326 ASSERT_NE(nullptr, event);
7327 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007328 ASSERT_NE(verified, nullptr);
7329 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
7330
7331 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
7332 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
7333 EXPECT_EQ(motionArgs.source, verified->source);
7334 EXPECT_EQ(motionArgs.displayId, verified->displayId);
7335
7336 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
7337
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07007338 const vec2 rawXY =
7339 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
7340 motionArgs.pointerCoords[0].getXYValue());
7341 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
7342 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007343 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007344 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08007345 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08007346 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
7347 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
7348}
7349
chaviw09c8d2d2020-08-24 15:48:26 -07007350/**
7351 * Ensure that separate calls to sign the same data are generating the same key.
7352 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
7353 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
7354 * tests.
7355 */
7356TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
7357 KeyEvent event = getTestKeyEvent();
7358 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
7359
7360 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
7361 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
7362 ASSERT_EQ(hmac1, hmac2);
7363}
7364
7365/**
7366 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
7367 */
7368TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
7369 KeyEvent event = getTestKeyEvent();
7370 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
7371 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
7372
7373 verifiedEvent.deviceId += 1;
7374 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7375
7376 verifiedEvent.source += 1;
7377 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7378
7379 verifiedEvent.eventTimeNanos += 1;
7380 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7381
Linnan Li13bf76a2024-05-05 19:18:02 +08007382 verifiedEvent.displayId = ui::LogicalDisplayId{verifiedEvent.displayId.val() + 1};
chaviw09c8d2d2020-08-24 15:48:26 -07007383 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7384
7385 verifiedEvent.action += 1;
7386 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7387
7388 verifiedEvent.downTimeNanos += 1;
7389 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7390
7391 verifiedEvent.flags += 1;
7392 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7393
7394 verifiedEvent.keyCode += 1;
7395 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7396
7397 verifiedEvent.scanCode += 1;
7398 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7399
7400 verifiedEvent.metaState += 1;
7401 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7402
7403 verifiedEvent.repeatCount += 1;
7404 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
7405}
7406
Vishnu Nair958da932020-08-21 17:12:37 -07007407TEST_F(InputDispatcherTest, SetFocusedWindow) {
7408 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007409 sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top",
7410 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007411 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007412 sp<FakeWindowHandle>::make(application, mDispatcher, "Second",
7413 ui::LogicalDisplayId::DEFAULT);
7414 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair958da932020-08-21 17:12:37 -07007415
7416 // Top window is also focusable but is not granted focus.
7417 windowTop->setFocusable(true);
7418 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007419 mDispatcher->onWindowInfosChanged(
7420 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007421 setFocusedWindow(windowSecond);
7422
7423 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007425 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007426
7427 // Focused window should receive event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007428 windowSecond->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -07007429 windowTop->assertNoEvents();
7430}
7431
7432TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
7433 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007434 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow",
7435 ui::LogicalDisplayId::DEFAULT);
7436 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair958da932020-08-21 17:12:37 -07007437
7438 window->setFocusable(true);
7439 // Release channel for window is no longer valid.
7440 window->releaseChannel();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007441 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007442 setFocusedWindow(window);
7443
7444 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007445 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07007446
7447 // window channel is invalid, so it should not receive any input event.
7448 window->assertNoEvents();
7449}
7450
7451TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
7452 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007453 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow",
7454 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007455 window->setFocusable(false);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007456 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair958da932020-08-21 17:12:37 -07007457
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007458 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007459 setFocusedWindow(window);
7460
7461 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007462 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07007463
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08007464 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07007465 window->assertNoEvents();
7466}
7467
7468TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
7469 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007470 sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top",
7471 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007472 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007473 sp<FakeWindowHandle>::make(application, mDispatcher, "Second",
7474 ui::LogicalDisplayId::DEFAULT);
7475 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair958da932020-08-21 17:12:37 -07007476
7477 windowTop->setFocusable(true);
7478 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007479 mDispatcher->onWindowInfosChanged(
7480 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007481 setFocusedWindow(windowTop);
7482 windowTop->consumeFocusEvent(true);
7483
Chavi Weingarten847e8512023-03-29 00:26:09 +00007484 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007485 mDispatcher->onWindowInfosChanged(
7486 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007487 windowSecond->consumeFocusEvent(true);
7488 windowTop->consumeFocusEvent(false);
7489
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007490 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007491 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007492
7493 // Focused window should receive event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007494 windowSecond->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -07007495}
7496
Chavi Weingarten847e8512023-03-29 00:26:09 +00007497TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07007498 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007499 sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top",
7500 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007501 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007502 sp<FakeWindowHandle>::make(application, mDispatcher, "Second",
7503 ui::LogicalDisplayId::DEFAULT);
7504 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair958da932020-08-21 17:12:37 -07007505
7506 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00007507 windowSecond->setFocusable(false);
7508 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007509 mDispatcher->onWindowInfosChanged(
7510 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Chavi Weingarten847e8512023-03-29 00:26:09 +00007511 setFocusedWindow(windowTop);
7512 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07007513
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007514 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Chavi Weingarten847e8512023-03-29 00:26:09 +00007515 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007516
7517 // Event should be dropped.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007518 windowTop->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -07007519 windowSecond->assertNoEvents();
7520}
7521
7522TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
7523 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007524 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow",
7525 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007526 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007527 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007528 ui::LogicalDisplayId::DEFAULT);
7529 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair958da932020-08-21 17:12:37 -07007530
7531 window->setFocusable(true);
7532 previousFocusedWindow->setFocusable(true);
7533 window->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007534 mDispatcher->onWindowInfosChanged(
7535 {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007536 setFocusedWindow(previousFocusedWindow);
7537 previousFocusedWindow->consumeFocusEvent(true);
7538
7539 // Requesting focus on invisible window takes focus from currently focused window.
7540 setFocusedWindow(window);
7541 previousFocusedWindow->consumeFocusEvent(false);
7542
7543 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007544 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007545 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007546 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007547
7548 // Window does not get focus event or key down.
7549 window->assertNoEvents();
7550
7551 // Window becomes visible.
7552 window->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007553 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007554
7555 // Window receives focus event.
7556 window->consumeFocusEvent(true);
7557 // Focused window receives key down.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007558 window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007559}
7560
Vishnu Nair599f1412021-06-21 10:39:58 -07007561TEST_F(InputDispatcherTest, DisplayRemoved) {
7562 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007563 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "window",
7564 ui::LogicalDisplayId::DEFAULT);
7565 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair599f1412021-06-21 10:39:58 -07007566
7567 // window is granted focus.
7568 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007569 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair599f1412021-06-21 10:39:58 -07007570 setFocusedWindow(window);
7571 window->consumeFocusEvent(true);
7572
7573 // When a display is removed window loses focus.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007574 mDispatcher->displayRemoved(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07007575 window->consumeFocusEvent(false);
7576}
7577
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007578/**
7579 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
7580 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
7581 * of the 'slipperyEnterWindow'.
7582 *
7583 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
7584 * a way so that the touched location is no longer covered by the top window.
7585 *
7586 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
7587 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
7588 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
7589 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
7590 * with ACTION_DOWN).
7591 * Thus, the touch has been transferred from the top window into the bottom window, because the top
7592 * window moved itself away from the touched location and had Flag::SLIPPERY.
7593 *
7594 * Even though the top window moved away from the touched location, it is still obscuring the bottom
7595 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
7596 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
7597 *
7598 * In this test, we ensure that the event received by the bottom window has
7599 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
7600 */
7601TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00007602 constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007603 constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1};
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007604
7605 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007606 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007607
7608 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007609 sp<FakeWindowHandle>::make(application, mDispatcher, "Top",
7610 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007611 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007612 // Make sure this one overlaps the bottom window
7613 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
7614 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
7615 // one. Windows with the same owner are not considered to be occluding each other.
7616 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
7617
7618 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007619 sp<FakeWindowHandle>::make(application, mDispatcher, "Second",
7620 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007621 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
7622
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007623 mDispatcher->onWindowInfosChanged(
7624 {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007625
7626 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00007627 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007628 AINPUT_SOURCE_TOUCHSCREEN,
7629 ui::LogicalDisplayId::DEFAULT, {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007630 slipperyExitWindow->consumeMotionDown();
7631 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007632 mDispatcher->onWindowInfosChanged(
7633 {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007634
Prabir Pradhan678438e2023-04-13 19:32:51 +00007635 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007636 AINPUT_SOURCE_TOUCHSCREEN,
7637 ui::LogicalDisplayId::DEFAULT, {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007638
7639 slipperyExitWindow->consumeMotionCancel();
7640
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007641 slipperyEnterWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007642 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
7643}
7644
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07007645/**
7646 * Two windows, one on the left and another on the right. The left window is slippery. The right
7647 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
7648 * touch moves from the left window into the right window, the gesture should continue to go to the
7649 * left window. Touch shouldn't slip because the right window can't receive touches. This test
7650 * reproduces a crash.
7651 */
7652TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
7653 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7654
7655 sp<FakeWindowHandle> leftSlipperyWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007656 sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
7657 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07007658 leftSlipperyWindow->setSlippery(true);
7659 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
7660
7661 sp<FakeWindowHandle> rightDropTouchesWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007662 sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
7663 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07007664 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
7665 rightDropTouchesWindow->setDropInput(true);
7666
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007667 mDispatcher->onWindowInfosChanged(
7668 {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07007669
7670 // Start touch in the left window
7671 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7672 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
7673 .build());
7674 leftSlipperyWindow->consumeMotionDown();
7675
7676 // And move it into the right window
7677 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7678 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
7679 .build());
7680
7681 // Since the right window isn't eligible to receive input, touch does not slip.
7682 // The left window continues to receive the gesture.
7683 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
7684 rightDropTouchesWindow->assertNoEvents();
7685}
7686
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07007687/**
7688 * A single window is on screen first. Touch is injected into that window. Next, a second window
7689 * appears. Since the first window is slippery, touch will move from the first window to the second.
7690 */
7691TEST_F(InputDispatcherTest, InjectedTouchSlips) {
7692 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7693 sp<FakeWindowHandle> originalWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007694 sp<FakeWindowHandle>::make(application, mDispatcher, "Original",
7695 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07007696 originalWindow->setFrame(Rect(0, 0, 200, 200));
7697 originalWindow->setSlippery(true);
7698
7699 sp<FakeWindowHandle> appearingWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007700 sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing",
7701 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07007702 appearingWindow->setFrame(Rect(0, 0, 200, 200));
7703
7704 mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0});
7705
7706 // Touch down on the original window
7707 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7708 injectMotionEvent(*mDispatcher,
7709 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7710 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
7711 .build()));
7712 originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
7713
7714 // Now, a new window appears. This could be, for example, a notification shade that appears
7715 // after user starts to drag down on the launcher window.
7716 mDispatcher->onWindowInfosChanged(
7717 {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0});
7718 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7719 injectMotionEvent(*mDispatcher,
7720 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7721 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110))
7722 .build()));
7723 originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
7724 appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
7725 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7726 injectMotionEvent(*mDispatcher,
7727 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7728 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
7729 .build()));
7730 appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
7731
7732 originalWindow->assertNoEvents();
7733 appearingWindow->assertNoEvents();
7734}
7735
Linnan Li49b2b202024-04-12 12:46:40 +08007736/**
7737 * Three windows:
7738 * - left window, which has FLAG_SLIPPERY, so it supports slippery exit
7739 * - right window
7740 * - spy window
7741 * The three windows do not overlap.
7742 *
7743 * We have two devices reporting events:
7744 * - Device A reports ACTION_DOWN, which lands in the left window
7745 * - Device B reports ACTION_DOWN, which lands in the spy window.
7746 * - Now, device B reports ACTION_MOVE events which move to the right window.
7747 *
7748 * The right window should not receive any events because the spy window is not a foreground window,
7749 * and also it does not support slippery touches.
7750 */
7751TEST_F(InputDispatcherTest, MultiDeviceSpyWindowSlipTest) {
7752 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7753 sp<FakeWindowHandle> leftWindow =
7754 sp<FakeWindowHandle>::make(application, mDispatcher, "Left window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007755 ui::LogicalDisplayId::DEFAULT);
Linnan Li49b2b202024-04-12 12:46:40 +08007756 leftWindow->setFrame(Rect(0, 0, 100, 100));
7757 leftWindow->setSlippery(true);
7758
7759 sp<FakeWindowHandle> rightWindow =
7760 sp<FakeWindowHandle>::make(application, mDispatcher, "Right window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007761 ui::LogicalDisplayId::DEFAULT);
Linnan Li49b2b202024-04-12 12:46:40 +08007762 rightWindow->setFrame(Rect(100, 0, 200, 100));
7763
7764 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007765 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window",
7766 ui::LogicalDisplayId::DEFAULT);
Linnan Li49b2b202024-04-12 12:46:40 +08007767 spyWindow->setFrame(Rect(200, 0, 300, 100));
7768 spyWindow->setSpy(true);
7769 spyWindow->setTrustedOverlay(true);
7770
7771 mDispatcher->onWindowInfosChanged(
7772 {{*leftWindow->getInfo(), *rightWindow->getInfo(), *spyWindow->getInfo()}, {}, 0, 0});
7773
7774 const DeviceId deviceA = 9;
7775 const DeviceId deviceB = 3;
7776
7777 // Tap on left window with device A
7778 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7779 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
7780 .deviceId(deviceA)
7781 .build());
7782 leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA)));
7783
7784 // Tap on spy window with device B
7785 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7786 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50))
7787 .deviceId(deviceB)
7788 .build());
7789 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB)));
7790
7791 // Move to right window with device B. Touches should not slip to the right window, because spy
7792 // window is not a foreground window, and it does not have FLAG_SLIPPERY
7793 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7794 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
7795 .deviceId(deviceB)
7796 .build());
7797 leftWindow->assertNoEvents();
7798 rightWindow->assertNoEvents();
7799 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB)));
7800}
7801
7802/**
7803 * Three windows arranged horizontally and without any overlap.
7804 * The left and right windows have FLAG_SLIPPERY. The middle window does not have any special flags.
7805 *
7806 * We have two devices reporting events:
7807 * - Device A reports ACTION_DOWN which lands in the left window
7808 * - Device B reports ACTION_DOWN which lands in the right window
7809 * - Device B reports ACTION_MOVE that shifts to the middle window.
7810 * This should cause touches for Device B to slip from the right window to the middle window.
7811 * The right window should receive ACTION_CANCEL for device B and the
7812 * middle window should receive down event for Device B.
7813 * If device B reports more ACTION_MOVE events, the middle window should receive remaining events.
7814 */
7815TEST_F(InputDispatcherTest, MultiDeviceSlipperyWindowTest) {
Siarhei Vishniakoudd56df12024-05-20 14:56:38 -07007816 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
Linnan Li49b2b202024-04-12 12:46:40 +08007817 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7818 sp<FakeWindowHandle> leftWindow =
7819 sp<FakeWindowHandle>::make(application, mDispatcher, "Left window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007820 ui::LogicalDisplayId::DEFAULT);
Linnan Li49b2b202024-04-12 12:46:40 +08007821 leftWindow->setFrame(Rect(0, 0, 100, 100));
7822 leftWindow->setSlippery(true);
7823
7824 sp<FakeWindowHandle> middleWindow =
7825 sp<FakeWindowHandle>::make(application, mDispatcher, "middle window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007826 ui::LogicalDisplayId::DEFAULT);
Linnan Li49b2b202024-04-12 12:46:40 +08007827 middleWindow->setFrame(Rect(100, 0, 200, 100));
7828
7829 sp<FakeWindowHandle> rightWindow =
7830 sp<FakeWindowHandle>::make(application, mDispatcher, "Right window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007831 ui::LogicalDisplayId::DEFAULT);
Linnan Li49b2b202024-04-12 12:46:40 +08007832 rightWindow->setFrame(Rect(200, 0, 300, 100));
7833 rightWindow->setSlippery(true);
7834
7835 mDispatcher->onWindowInfosChanged(
7836 {{*leftWindow->getInfo(), *middleWindow->getInfo(), *rightWindow->getInfo()},
7837 {},
7838 0,
7839 0});
7840
7841 const DeviceId deviceA = 9;
7842 const DeviceId deviceB = 3;
7843
7844 // Tap on left window with device A
7845 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7846 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
7847 .deviceId(deviceA)
7848 .build());
7849 leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA)));
7850
7851 // Tap on right window with device B
7852 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7853 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50))
7854 .deviceId(deviceB)
7855 .build());
7856 rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB)));
7857
7858 // Move to middle window with device B. Touches should slip to middle window, because right
7859 // window is a foreground window that's associated with device B and has FLAG_SLIPPERY.
7860 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7861 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
7862 .deviceId(deviceB)
7863 .build());
7864 rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB)));
7865 middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB)));
7866
7867 // Move to middle window with device A. Touches should slip to middle window, because left
7868 // window is a foreground window that's associated with device A and has FLAG_SLIPPERY.
7869 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7870 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
7871 .deviceId(deviceA)
7872 .build());
7873 leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceA)));
7874 middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA)));
7875
7876 // Ensure that middle window can receive the remaining move events.
7877 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7878 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51))
7879 .deviceId(deviceB)
7880 .build());
7881 leftWindow->assertNoEvents();
7882 middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB)));
7883 rightWindow->assertNoEvents();
7884}
7885
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007886TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007887 using Uid = gui::Uid;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007888 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7889
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007890 sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left",
7891 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007892 leftWindow->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00007893 leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007894
7895 sp<FakeWindowHandle> rightSpy =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007896 sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy",
7897 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007898 rightSpy->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00007899 rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007900 rightSpy->setSpy(true);
7901 rightSpy->setTrustedOverlay(true);
7902
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007903 sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right",
7904 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007905 rightWindow->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00007906 rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007907
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007908 mDispatcher->onWindowInfosChanged(
7909 {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007910
7911 // Touch in the left window
7912 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7913 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
7914 .build());
7915 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown());
7916 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007917 ASSERT_NO_FATAL_FAILURE(
7918 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007919
7920 // Touch another finger over the right windows
7921 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7922 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
7923 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
7924 .build());
7925 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown());
7926 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown());
7927 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove());
7928 mDispatcher->waitForIdle();
7929 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007930 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID,
7931 {Uid{101}, Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007932
7933 // Release finger over left window. The UP actions are not treated as device interaction.
7934 // The windows that did not receive the UP pointer will receive MOVE events, but since this
7935 // is part of the UP action, we do not treat this as device interaction.
7936 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
7937 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
7938 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
7939 .build());
7940 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp());
7941 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
7942 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
7943 mDispatcher->waitForIdle();
7944 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7945
7946 // Move remaining finger
7947 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7948 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
7949 .build());
7950 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
7951 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
7952 mDispatcher->waitForIdle();
7953 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007954 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007955
7956 // Release all fingers
7957 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
7958 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
7959 .build());
7960 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp());
7961 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp());
7962 mDispatcher->waitForIdle();
7963 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7964}
7965
7966TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) {
7967 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7968
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007969 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
7970 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007971 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00007972 window->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007973
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007974 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007975 setFocusedWindow(window);
7976 ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true));
7977
7978 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007979 ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007980 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007981 ASSERT_NO_FATAL_FAILURE(
7982 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007983
7984 // The UP actions are not treated as device interaction.
7985 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007986 ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007987 mDispatcher->waitForIdle();
7988 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7989}
7990
Prabir Pradhan5893d362023-11-17 04:30:40 +00007991TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) {
7992 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7993
7994 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007995 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan5893d362023-11-17 04:30:40 +00007996 left->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07007997 sp<FakeWindowHandle> right =
7998 sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window",
7999 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan5893d362023-11-17 04:30:40 +00008000 right->setFrame(Rect(100, 0, 200, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008001 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window",
8002 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan5893d362023-11-17 04:30:40 +00008003 spy->setFrame(Rect(0, 0, 200, 100));
8004 spy->setTrustedOverlay(true);
8005 spy->setSpy(true);
8006
8007 mDispatcher->onWindowInfosChanged(
8008 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
8009
8010 // Send hover move to the left window, and ensure hover enter is synthesized with a new eventId.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008011 NotifyMotionArgs notifyArgs =
8012 generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
8013 ui::LogicalDisplayId::DEFAULT, {PointF{50, 50}});
Prabir Pradhan5893d362023-11-17 04:30:40 +00008014 mDispatcher->notifyMotion(notifyArgs);
8015
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008016 std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent(
Prabir Pradhan5893d362023-11-17 04:30:40 +00008017 AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)),
8018 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008019 ASSERT_NE(nullptr, leftEnter);
Prabir Pradhan5893d362023-11-17 04:30:40 +00008020 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
8021 Not(WithEventId(notifyArgs.id)),
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008022 Not(WithEventId(leftEnter->getId())),
Prabir Pradhan5893d362023-11-17 04:30:40 +00008023 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
8024
8025 // Send move to the right window, and ensure hover exit and enter are synthesized with new ids.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008026 notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
8027 ui::LogicalDisplayId::DEFAULT, {PointF{150, 50}});
Prabir Pradhan5893d362023-11-17 04:30:40 +00008028 mDispatcher->notifyMotion(notifyArgs);
8029
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008030 std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent(
Prabir Pradhan5893d362023-11-17 04:30:40 +00008031 AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)),
8032 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008033 ASSERT_NE(nullptr, leftExit);
Prabir Pradhan5893d362023-11-17 04:30:40 +00008034 right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
8035 Not(WithEventId(notifyArgs.id)),
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008036 Not(WithEventId(leftExit->getId())),
Prabir Pradhan5893d362023-11-17 04:30:40 +00008037 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
8038
8039 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id)));
8040}
8041
Linnan Liccf6ce32024-04-11 20:32:13 +08008042/**
8043 * When a device reports a DOWN event, which lands in a window that supports splits, and then the
8044 * device then reports a POINTER_DOWN, which lands in the location of a non-existing window, then
8045 * the previous window should receive this event and not be dropped.
8046 */
8047TEST_F(InputDispatcherMultiDeviceTest, SingleDevicePointerDownEventRetentionWithoutWindowTarget) {
8048 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008049 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
8050 ui::LogicalDisplayId::DEFAULT);
Linnan Liccf6ce32024-04-11 20:32:13 +08008051 window->setFrame(Rect(0, 0, 100, 100));
8052 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
8053
8054 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8055 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8056 .build());
8057
8058 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN)));
8059
8060 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8061 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8062 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
8063 .build());
8064
8065 window->consumeMotionEvent(AllOf(WithMotionAction(POINTER_1_DOWN)));
8066}
8067
8068/**
8069 * When deviceA reports a DOWN event, which lands in a window that supports splits, and then deviceB
8070 * also reports a DOWN event, which lands in the location of a non-existing window, then the
8071 * previous window should receive deviceB's event and it should be dropped.
8072 */
8073TEST_F(InputDispatcherMultiDeviceTest, SecondDeviceDownEventDroppedWithoutWindowTarget) {
8074 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008075 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window",
8076 ui::LogicalDisplayId::DEFAULT);
Linnan Liccf6ce32024-04-11 20:32:13 +08008077 window->setFrame(Rect(0, 0, 100, 100));
8078 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
8079
8080 const DeviceId deviceA = 9;
8081 const DeviceId deviceB = 3;
8082
8083 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8084 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8085 .deviceId(deviceA)
8086 .build());
8087 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA)));
8088
8089 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8090 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
8091 .deviceId(deviceB)
8092 .build());
8093 window->assertNoEvents();
8094}
8095
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00008096class InputDispatcherFallbackKeyTest : public InputDispatcherTest {
8097protected:
8098 std::shared_ptr<FakeApplicationHandle> mApp;
8099 sp<FakeWindowHandle> mWindow;
8100
8101 virtual void SetUp() override {
8102 InputDispatcherTest::SetUp();
8103
8104 mApp = std::make_shared<FakeApplicationHandle>();
8105
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008106 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window",
8107 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00008108 mWindow->setFrame(Rect(0, 0, 100, 100));
8109
8110 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
8111 setFocusedWindow(mWindow);
8112 ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true));
8113 }
8114
8115 void setFallback(int32_t keycode) {
8116 mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) {
8117 return KeyEventBuilder(event).keyCode(keycode).build();
8118 });
8119 }
8120
8121 void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008122 std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled);
8123 ASSERT_NE(nullptr, event);
8124 ASSERT_THAT(*event, matcher);
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00008125 }
8126};
8127
8128TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) {
8129 mDispatcher->notifyKey(
8130 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8131 consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A)));
8132 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
8133}
8134
8135TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) {
8136 mDispatcher->notifyKey(
8137 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8138 consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A)));
8139 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8140}
8141
8142TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) {
8143 mDispatcher->notifyKey(
8144 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8145
8146 // Do not handle this key event.
8147 consumeKey(/*handled=*/false,
8148 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8149 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8150
8151 // Since the policy did not request any fallback to be generated, ensure there are no events.
8152 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
8153}
8154
8155TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) {
8156 setFallback(AKEYCODE_B);
8157 mDispatcher->notifyKey(
8158 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8159
8160 // Do not handle this key event.
8161 consumeKey(/*handled=*/false,
8162 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8163
8164 // Since the key was not handled, ensure the fallback event was dispatched instead.
8165 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8166 consumeKey(/*handled=*/true,
8167 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8168 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8169
8170 // Release the original key, and ensure the fallback key is also released.
8171 mDispatcher->notifyKey(
8172 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8173 consumeKey(/*handled=*/false,
8174 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8175 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8176 consumeKey(/*handled=*/true,
8177 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
8178 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8179
8180 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
8181 mWindow->assertNoEvents();
8182}
8183
8184TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) {
8185 setFallback(AKEYCODE_B);
8186 mDispatcher->notifyKey(
8187 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8188
8189 // Do not handle this key event, but handle the fallback.
8190 consumeKey(/*handled=*/false,
8191 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8192 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8193 consumeKey(/*handled=*/true,
8194 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8195 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8196
8197 // Release the original key, and ensure the fallback key is also released.
8198 mDispatcher->notifyKey(
8199 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8200 // But this time, the app handles the original key.
8201 consumeKey(/*handled=*/true,
8202 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8203 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8204 // Ensure the fallback key is canceled.
8205 consumeKey(/*handled=*/true,
8206 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
8207 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
8208
8209 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
8210 mWindow->assertNoEvents();
8211}
8212
8213TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) {
8214 setFallback(AKEYCODE_B);
8215 mDispatcher->notifyKey(
8216 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8217
8218 // Do not handle this key event.
8219 consumeKey(/*handled=*/false,
8220 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8221 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8222 // App does not handle the fallback either, so ensure another fallback is not generated.
8223 setFallback(AKEYCODE_C);
8224 consumeKey(/*handled=*/false,
8225 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8226 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8227
8228 // Release the original key, and ensure the fallback key is also released.
8229 setFallback(AKEYCODE_B);
8230 mDispatcher->notifyKey(
8231 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8232 consumeKey(/*handled=*/false,
8233 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8234 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8235 consumeKey(/*handled=*/false,
8236 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
8237 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8238
8239 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
8240 mWindow->assertNoEvents();
8241}
8242
8243TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) {
8244 setFallback(AKEYCODE_B);
8245 mDispatcher->notifyKey(
8246 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8247
8248 // Do not handle this key event, so fallback is generated.
8249 consumeKey(/*handled=*/false,
8250 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8251 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8252 consumeKey(/*handled=*/true,
8253 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8254 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8255
8256 // Release the original key, but assume the policy is misbehaving and it
8257 // generates an inconsistent fallback to the one from the DOWN event.
8258 setFallback(AKEYCODE_C);
8259 mDispatcher->notifyKey(
8260 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8261 consumeKey(/*handled=*/false,
8262 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8263 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8264 // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency.
8265 consumeKey(/*handled=*/true,
8266 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
8267 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
8268
8269 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
8270 mWindow->assertNoEvents();
8271}
8272
8273TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) {
8274 setFallback(AKEYCODE_B);
8275 mDispatcher->notifyKey(
8276 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8277
8278 // Do not handle this key event, so fallback is generated.
8279 consumeKey(/*handled=*/false,
8280 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8281 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8282 consumeKey(/*handled=*/true,
8283 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8284 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8285
8286 // The original key is canceled.
8287 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD)
8288 .keyCode(AKEYCODE_A)
8289 .addFlag(AKEY_EVENT_FLAG_CANCELED)
8290 .build());
8291 consumeKey(/*handled=*/false,
8292 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A),
8293 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
8294 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8295 // Ensure the fallback key is also canceled due to the original key being canceled.
8296 consumeKey(/*handled=*/true,
8297 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
8298 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
8299
8300 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
8301 mWindow->assertNoEvents();
8302}
8303
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00008304TEST_F(InputDispatcherFallbackKeyTest, InputChannelRemovedDuringPolicyCall) {
Prabir Pradhanb13da8f2024-01-09 23:10:13 +00008305 setFallback(AKEYCODE_B);
8306 mDispatcher->notifyKey(
8307 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8308
8309 // Do not handle this key event.
8310 consumeKey(/*handled=*/false,
8311 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8312 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8313 consumeKey(/*handled=*/true,
8314 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8315 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8316
8317 mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) {
8318 // When the unhandled key is reported to the policy next, remove the input channel.
8319 mDispatcher->removeInputChannel(mWindow->getToken());
8320 return KeyEventBuilder(event).keyCode(AKEYCODE_B).build();
8321 });
8322 // Release the original key, and let the app now handle the previously unhandled key.
8323 // This should result in the previously generated fallback key to be cancelled.
8324 // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified
8325 // of the UP event for consistency. The Dispatcher calls into the policy from its own thread
8326 // without holding the lock, because it need to synchronously fetch the fallback key. While in
8327 // the policy call, we will now remove the input channel. Once the policy call returns, the
8328 // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does
8329 // not cause any crashes.
8330 mDispatcher->notifyKey(
8331 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8332 consumeKey(/*handled=*/true,
8333 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8334 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8335}
8336
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00008337TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) {
8338 setFallback(AKEYCODE_B);
8339 mDispatcher->notifyKey(
8340 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8341
8342 // Do not handle this key event.
8343 consumeKey(/*handled=*/false,
8344 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8345 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8346 consumeKey(/*handled=*/true,
8347 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8348 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8349
8350 mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) {
8351 // When the unhandled key is reported to the policy next, remove the window.
8352 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
8353 return KeyEventBuilder(event).keyCode(AKEYCODE_B).build();
8354 });
8355 // Release the original key, which the app will not handle. When this unhandled key is reported
8356 // to the policy, the window will be removed.
8357 mDispatcher->notifyKey(
8358 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8359 consumeKey(/*handled=*/false,
8360 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8361 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8362
8363 // Since the window was removed, it loses focus, and the channel state will be reset.
8364 consumeKey(/*handled=*/true,
8365 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
8366 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
8367 mWindow->consumeFocusEvent(false);
8368 mWindow->assertNoEvents();
8369}
8370
8371TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedWhileAwaitingFinishedSignal) {
8372 setFallback(AKEYCODE_B);
8373 mDispatcher->notifyKey(
8374 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
8375
8376 // Do not handle this key event.
8377 consumeKey(/*handled=*/false,
8378 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
8379 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
8380 const auto [seq, event] = mWindow->receiveEvent();
8381 ASSERT_TRUE(seq.has_value() && event != nullptr) << "Failed to receive fallback event";
8382 ASSERT_EQ(event->getType(), InputEventType::KEY);
8383 ASSERT_THAT(static_cast<const KeyEvent&>(*event),
8384 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
8385 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
8386
8387 // Remove the window now, which should generate a cancellations and make the window lose focus.
8388 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
8389 consumeKey(/*handled=*/true,
8390 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A),
8391 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
8392 consumeKey(/*handled=*/true,
8393 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
8394 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
8395 mWindow->consumeFocusEvent(false);
8396
8397 // Finish the event by reporting it as handled.
8398 mWindow->finishEvent(*seq);
8399 mWindow->assertNoEvents();
8400}
8401
Garfield Tan1c7bc862020-01-28 13:24:04 -08008402class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
8403protected:
Siarhei Vishniakoufa2a0492023-11-14 13:13:18 -08008404 static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms;
8405 static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms;
Garfield Tan1c7bc862020-01-28 13:24:04 -08008406
Chris Yea209fde2020-07-22 13:54:51 -07008407 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08008408 sp<FakeWindowHandle> mWindow;
8409
8410 virtual void SetUp() override {
Prabir Pradhandae52792023-12-15 07:36:40 +00008411 InputDispatcherTest::SetUp();
Garfield Tan1c7bc862020-01-28 13:24:04 -08008412
Prabir Pradhandae52792023-12-15 07:36:40 +00008413 mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008414 setUpWindow();
8415 }
8416
8417 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07008418 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008419 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window",
8420 ui::LogicalDisplayId::DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008421
Vishnu Nair47074b82020-08-14 11:54:47 -07008422 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008423 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008424 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008425 mWindow->consumeFocusEvent(true);
8426 }
8427
Chris Ye2ad95392020-09-01 13:44:44 -07008428 void sendAndConsumeKeyDown(int32_t deviceId) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008429 NotifyKeyArgs keyArgs =
8430 generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07008431 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08008432 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00008433 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008434
8435 // Window should receive key down event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008436 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008437 }
8438
8439 void expectKeyRepeatOnce(int32_t repeatCount) {
8440 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008441 mWindow->consumeKeyEvent(
8442 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount)));
Garfield Tan1c7bc862020-01-28 13:24:04 -08008443 }
8444
Chris Ye2ad95392020-09-01 13:44:44 -07008445 void sendAndConsumeKeyUp(int32_t deviceId) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008446 NotifyKeyArgs keyArgs =
8447 generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07008448 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08008449 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00008450 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008451
8452 // Window should receive key down event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008453 mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00008454 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008455 }
Hu Guofe3c8f12023-09-22 17:20:15 +08008456
8457 void injectKeyRepeat(int32_t repeatCount) {
8458 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008459 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, repeatCount,
8460 ui::LogicalDisplayId::DEFAULT))
Hu Guofe3c8f12023-09-22 17:20:15 +08008461 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8462 }
Garfield Tan1c7bc862020-01-28 13:24:04 -08008463};
8464
8465TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00008466 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07008467 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
8468 expectKeyRepeatOnce(repeatCount);
8469 }
8470}
8471
8472TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00008473 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07008474 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
8475 expectKeyRepeatOnce(repeatCount);
8476 }
Harry Cutts33476232023-01-30 19:57:29 +00008477 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07008478 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08008479 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
8480 expectKeyRepeatOnce(repeatCount);
8481 }
8482}
8483
8484TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00008485 sendAndConsumeKeyDown(/*deviceId=*/1);
8486 expectKeyRepeatOnce(/*repeatCount=*/1);
8487 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07008488 mWindow->assertNoEvents();
8489}
8490
8491TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00008492 sendAndConsumeKeyDown(/*deviceId=*/1);
8493 expectKeyRepeatOnce(/*repeatCount=*/1);
8494 sendAndConsumeKeyDown(/*deviceId=*/2);
8495 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07008496 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00008497 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07008498 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00008499 expectKeyRepeatOnce(/*repeatCount=*/2);
8500 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07008501 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00008502 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07008503 mWindow->assertNoEvents();
8504}
8505
8506TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00008507 sendAndConsumeKeyDown(/*deviceId=*/1);
8508 expectKeyRepeatOnce(/*repeatCount=*/1);
8509 sendAndConsumeKeyDown(/*deviceId=*/2);
8510 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07008511 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00008512 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07008513 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08008514 mWindow->assertNoEvents();
8515}
8516
liushenxiang42232912021-05-21 20:24:09 +08008517TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
8518 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00008519 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00008520 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008521 mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT,
liushenxiang42232912021-05-21 20:24:09 +08008522 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
8523 mWindow->assertNoEvents();
8524}
8525
Garfield Tan1c7bc862020-01-28 13:24:04 -08008526TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00008527 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00008528 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008529 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008530 std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey();
8531 ASSERT_NE(nullptr, repeatEvent);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008532 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008533 IdGenerator::getSource(repeatEvent->getId()));
Garfield Tan1c7bc862020-01-28 13:24:04 -08008534 }
8535}
8536
8537TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00008538 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00008539 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08008540
8541 std::unordered_set<int32_t> idSet;
8542 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008543 std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey();
8544 ASSERT_NE(nullptr, repeatEvent);
8545 int32_t id = repeatEvent->getId();
Garfield Tan1c7bc862020-01-28 13:24:04 -08008546 EXPECT_EQ(idSet.end(), idSet.find(id));
8547 idSet.insert(id);
8548 }
8549}
8550
Hu Guofe3c8f12023-09-22 17:20:15 +08008551TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_CorrectRepeatCountWhenInjectKeyRepeat) {
8552 injectKeyRepeat(0);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008553 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Hu Guofe3c8f12023-09-22 17:20:15 +08008554 for (int32_t repeatCount = 1; repeatCount <= 2; ++repeatCount) {
8555 expectKeyRepeatOnce(repeatCount);
8556 }
8557 injectKeyRepeat(1);
8558 // Expect repeatCount to be 3 instead of 1
8559 expectKeyRepeatOnce(3);
8560}
8561
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008562/* Test InputDispatcher for MultiDisplay */
8563class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
8564public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07008565 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008566 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08008567
Chris Yea209fde2020-07-22 13:54:51 -07008568 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008569 windowInPrimary = sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1",
8570 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008571
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008572 // Set focus window for primary display, but focused display would be second one.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008573 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07008574 windowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008575 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
8576
Vishnu Nair958da932020-08-21 17:12:37 -07008577 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008578 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08008579
Chris Yea209fde2020-07-22 13:54:51 -07008580 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008581 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008582 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008583 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008584 // Set focus display to second one.
8585 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
Arpit Singhb65e2bd2024-06-03 09:48:16 +00008586 mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID);
8587
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008588 // Set focus window for second display.
8589 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07008590 windowInSecondary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008591 mDispatcher->onWindowInfosChanged(
8592 {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008593 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008594 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008595 }
8596
Prabir Pradhan3608aad2019-10-02 17:08:26 -07008597 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008598 InputDispatcherTest::TearDown();
8599
Chris Yea209fde2020-07-22 13:54:51 -07008600 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008601 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07008602 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008603 windowInSecondary.clear();
8604 }
8605
8606protected:
Chris Yea209fde2020-07-22 13:54:51 -07008607 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008608 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07008609 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008610 sp<FakeWindowHandle> windowInSecondary;
8611};
8612
8613TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
8614 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008615 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008616 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8617 ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008618 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008619 windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08008620 windowInSecondary->assertNoEvents();
8621
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008622 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008623 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008624 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008625 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08008626 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08008627 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08008628}
8629
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008630TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08008631 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08008632 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008633 injectKeyDownNoRepeat(*mDispatcher, ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008634 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008635 windowInPrimary->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08008636 windowInSecondary->assertNoEvents();
8637
8638 // Test inject a key down without display id specified.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008639 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008640 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08008641 windowInPrimary->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008642 windowInSecondary->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Arthur Hungb92218b2018-08-14 12:00:21 +08008643
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008644 // Remove all windows in secondary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008645 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
Arthur Hungb92218b2018-08-14 12:00:21 +08008646
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008647 // Old focus should receive a cancel event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008648 windowInSecondary->consumeKeyUp(ui::LogicalDisplayId::INVALID, AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08008649
8650 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008651 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Arthur Hungb92218b2018-08-14 12:00:21 +08008652 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008653 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08008654 windowInSecondary->assertNoEvents();
8655}
8656
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008657// Test per-display input monitors for motion event.
8658TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08008659 FakeMonitorReceiver monitorInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008660 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08008661 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00008662 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008663
8664 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008665 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008666 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8667 ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008668 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008669 windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
8670 monitorInPrimary.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008671 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08008672 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008673
8674 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008675 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008676 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008677 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008678 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08008679 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08008680 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08008681 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008682
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08008683 // Lift up the touch from the second display
8684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008685 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08008686 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8687 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
8688 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
8689
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008690 // Test inject a non-pointer motion event.
8691 // If specific a display, it will dispatch to the focused window of particular display,
8692 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008693 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008694 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL,
8695 ui::LogicalDisplayId::INVALID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008696 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008697 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08008698 monitorInPrimary.assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008699 windowInSecondary->consumeMotionDown(ui::LogicalDisplayId::INVALID);
8700 monitorInSecondary.consumeMotionDown(ui::LogicalDisplayId::INVALID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008701}
8702
8703// Test per-display input monitors for key event.
8704TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008705 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08008706 FakeMonitorReceiver monitorInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008707 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08008708 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00008709 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008710
8711 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008712 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008713 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008714 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08008715 monitorInPrimary.assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008716 windowInSecondary->consumeKeyDown(ui::LogicalDisplayId::INVALID);
8717 monitorInSecondary.consumeKeyDown(ui::LogicalDisplayId::INVALID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008718}
8719
Vishnu Nair958da932020-08-21 17:12:37 -07008720TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
8721 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008722 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2",
8723 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07008724 secondWindowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008725 mDispatcher->onWindowInfosChanged(
8726 {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(),
8727 *windowInSecondary->getInfo()},
8728 {},
8729 0,
8730 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008731 setFocusedWindow(secondWindowInPrimary);
8732 windowInPrimary->consumeFocusEvent(false);
8733 secondWindowInPrimary->consumeFocusEvent(true);
8734
8735 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008736 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008737 injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008738 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07008739 windowInPrimary->assertNoEvents();
8740 windowInSecondary->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008741 secondWindowInPrimary->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07008742}
8743
Arthur Hungdfd528e2021-12-08 13:23:04 +00008744TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
8745 FakeMonitorReceiver monitorInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008746 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Arthur Hungdfd528e2021-12-08 13:23:04 +00008747 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00008748 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hungdfd528e2021-12-08 13:23:04 +00008749
8750 // Test touch down on primary display.
8751 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008752 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8753 ui::LogicalDisplayId::DEFAULT))
Arthur Hungdfd528e2021-12-08 13:23:04 +00008754 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008755 windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
8756 monitorInPrimary.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hungdfd528e2021-12-08 13:23:04 +00008757
8758 // Test touch down on second display.
8759 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008760 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungdfd528e2021-12-08 13:23:04 +00008761 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8762 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
8763 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
8764
8765 // Trigger cancel touch.
8766 mDispatcher->cancelCurrentTouch();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008767 windowInPrimary->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT);
8768 monitorInPrimary.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT);
Arthur Hungdfd528e2021-12-08 13:23:04 +00008769 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
8770 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
8771
8772 // Test inject a move motion event, no window/monitor should receive the event.
8773 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008774 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008775 ui::LogicalDisplayId::DEFAULT, {110, 200}))
Arthur Hungdfd528e2021-12-08 13:23:04 +00008776 << "Inject motion event should return InputEventInjectionResult::FAILED";
8777 windowInPrimary->assertNoEvents();
8778 monitorInPrimary.assertNoEvents();
8779
8780 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008781 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00008782 SECOND_DISPLAY_ID, {110, 200}))
8783 << "Inject motion event should return InputEventInjectionResult::FAILED";
8784 windowInSecondary->assertNoEvents();
8785 monitorInSecondary.assertNoEvents();
8786}
8787
Hu Guocb134f12023-12-23 13:42:44 +00008788/**
8789 * Send a key to the primary display and to the secondary display.
8790 * Then cause the key on the primary display to be canceled by sending in a stale key.
8791 * Ensure that the key on the primary display is canceled, and that the key on the secondary display
8792 * does not get canceled.
8793 */
8794TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) {
8795 // Send a key down on primary display
8796 mDispatcher->notifyKey(
8797 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008798 .displayId(ui::LogicalDisplayId::DEFAULT)
Hu Guocb134f12023-12-23 13:42:44 +00008799 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8800 .build());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008801 windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN),
8802 WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Hu Guocb134f12023-12-23 13:42:44 +00008803 windowInSecondary->assertNoEvents();
8804
8805 // Send a key down on second display
8806 mDispatcher->notifyKey(
8807 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
8808 .displayId(SECOND_DISPLAY_ID)
8809 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8810 .build());
8811 windowInSecondary->consumeKeyEvent(
8812 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID)));
8813 windowInPrimary->assertNoEvents();
8814
8815 // Send a valid key up event on primary display that will be dropped because it is stale
8816 NotifyKeyArgs staleKeyUp =
8817 KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008818 .displayId(ui::LogicalDisplayId::DEFAULT)
Hu Guocb134f12023-12-23 13:42:44 +00008819 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8820 .build();
8821 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms;
8822 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
8823 std::this_thread::sleep_for(STALE_EVENT_TIMEOUT);
8824 mDispatcher->notifyKey(staleKeyUp);
8825
8826 // Only the key gesture corresponding to the dropped event should receive the cancel event.
8827 // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not
8828 // receive any events.
8829 windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008830 WithDisplayId(ui::LogicalDisplayId::DEFAULT),
Hu Guocb134f12023-12-23 13:42:44 +00008831 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
8832 windowInSecondary->assertNoEvents();
8833}
8834
8835/**
8836 * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events.
8837 */
8838TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) {
8839 // Send touch down on primary display.
8840 mDispatcher->notifyMotion(
8841 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8842 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008843 .displayId(ui::LogicalDisplayId::DEFAULT)
Hu Guocb134f12023-12-23 13:42:44 +00008844 .build());
8845 windowInPrimary->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008846 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Hu Guocb134f12023-12-23 13:42:44 +00008847 windowInSecondary->assertNoEvents();
8848
8849 // Send touch down on second display.
8850 mDispatcher->notifyMotion(
8851 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8852 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8853 .displayId(SECOND_DISPLAY_ID)
8854 .build());
8855 windowInPrimary->assertNoEvents();
8856 windowInSecondary->consumeMotionEvent(
8857 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID)));
8858
8859 // inject a valid MotionEvent on primary display that will be stale when it arrives.
8860 NotifyMotionArgs staleMotionUp =
8861 MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008862 .displayId(ui::LogicalDisplayId::DEFAULT)
Hu Guocb134f12023-12-23 13:42:44 +00008863 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8864 .build();
8865 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms;
8866 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
8867 std::this_thread::sleep_for(STALE_EVENT_TIMEOUT);
8868 mDispatcher->notifyMotion(staleMotionUp);
8869
8870 // For stale motion events, we let the gesture to complete. This behaviour is different from key
8871 // events, where we would cancel the current keys instead.
8872 windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP));
8873 windowInSecondary->assertNoEvents();
8874}
8875
Jackal Guof9696682018-10-05 12:23:23 +08008876class InputFilterTest : public InputDispatcherTest {
8877protected:
Linnan Li13bf76a2024-05-05 19:18:02 +08008878 void testNotifyMotion(ui::LogicalDisplayId displayId, bool expectToBeFiltered,
Prabir Pradhan81420cc2021-09-06 10:28:50 -07008879 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08008880 NotifyMotionArgs motionArgs;
8881
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008882 motionArgs =
8883 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00008884 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008885 motionArgs =
8886 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00008887 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008888 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08008889 if (expectToBeFiltered) {
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008890 const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue());
Prabir Pradhan81420cc2021-09-06 10:28:50 -07008891 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08008892 } else {
8893 mFakePolicy->assertFilterInputEventWasNotCalled();
8894 }
8895 }
8896
8897 void testNotifyKey(bool expectToBeFiltered) {
8898 NotifyKeyArgs keyArgs;
8899
8900 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00008901 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08008902 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00008903 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008904 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08008905
8906 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08008907 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08008908 } else {
8909 mFakePolicy->assertFilterInputEventWasNotCalled();
8910 }
8911 }
8912};
8913
8914// Test InputFilter for MotionEvent
8915TEST_F(InputFilterTest, MotionEvent_InputFilter) {
8916 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008917 testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/false);
Harry Cutts101ee9b2023-07-06 18:04:14 +00008918 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08008919
8920 // Enable InputFilter
8921 mDispatcher->setInputFilterEnabled(true);
8922 // Test touch on both primary and second display, and check if both events are filtered.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008923 testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/true);
Harry Cutts101ee9b2023-07-06 18:04:14 +00008924 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08008925
8926 // Disable InputFilter
8927 mDispatcher->setInputFilterEnabled(false);
8928 // Test touch on both primary and second display, and check if both events aren't filtered.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008929 testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/false);
Harry Cutts101ee9b2023-07-06 18:04:14 +00008930 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08008931}
8932
8933// Test InputFilter for KeyEvent
8934TEST_F(InputFilterTest, KeyEvent_InputFilter) {
8935 // Since the InputFilter is disabled by default, check if key event aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00008936 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08008937
8938 // Enable InputFilter
8939 mDispatcher->setInputFilterEnabled(true);
8940 // Send a key event, and check if it is filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00008941 testNotifyKey(/*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08008942
8943 // Disable InputFilter
8944 mDispatcher->setInputFilterEnabled(false);
8945 // Send a key event, and check if it isn't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00008946 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08008947}
8948
Prabir Pradhan81420cc2021-09-06 10:28:50 -07008949// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
8950// logical display coordinate space.
8951TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
8952 ui::Transform firstDisplayTransform;
8953 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
8954 ui::Transform secondDisplayTransform;
8955 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
8956
8957 std::vector<gui::DisplayInfo> displayInfos(2);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008958 displayInfos[0].displayId = ui::LogicalDisplayId::DEFAULT;
Prabir Pradhan81420cc2021-09-06 10:28:50 -07008959 displayInfos[0].transform = firstDisplayTransform;
8960 displayInfos[1].displayId = SECOND_DISPLAY_ID;
8961 displayInfos[1].transform = secondDisplayTransform;
8962
Patrick Williamsd828f302023-04-28 17:52:08 -05008963 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07008964
8965 // Enable InputFilter
8966 mDispatcher->setInputFilterEnabled(true);
8967
8968 // Ensure the correct transforms are used for the displays.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008969 testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/true,
8970 firstDisplayTransform);
Harry Cutts101ee9b2023-07-06 18:04:14 +00008971 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform);
Prabir Pradhan81420cc2021-09-06 10:28:50 -07008972}
8973
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008974class InputFilterInjectionPolicyTest : public InputDispatcherTest {
8975protected:
8976 virtual void SetUp() override {
8977 InputDispatcherTest::SetUp();
8978
8979 /**
8980 * We don't need to enable input filter to test the injected event policy, but we enabled it
8981 * here to make the tests more realistic, since this policy only matters when inputfilter is
8982 * on.
8983 */
8984 mDispatcher->setInputFilterEnabled(true);
8985
8986 std::shared_ptr<InputApplicationHandle> application =
8987 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008988 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008989 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008990
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008991 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008992 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008993 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008994 setFocusedWindow(mWindow);
8995 mWindow->consumeFocusEvent(true);
8996 }
8997
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008998 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
8999 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009000 KeyEvent event;
9001
9002 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
9003 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009004 ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0,
9005 AKEYCODE_A, KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009006 const int32_t additionalPolicyFlags =
9007 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
9008 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00009009 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00009010 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009011 policyFlags | additionalPolicyFlags));
9012
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009013 mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags)));
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009014 }
9015
9016 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
9017 int32_t flags) {
9018 MotionEvent event;
9019 PointerProperties pointerProperties[1];
9020 PointerCoords pointerCoords[1];
9021 pointerProperties[0].clear();
9022 pointerProperties[0].id = 0;
9023 pointerCoords[0].clear();
9024 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
9025 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
9026
9027 ui::Transform identityTransform;
9028 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
9029 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
9030 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
9031 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
9032 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07009033 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07009034 eventTime,
Harry Cutts101ee9b2023-07-06 18:04:14 +00009035 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009036
9037 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
9038 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00009039 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00009040 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009041 policyFlags | additionalPolicyFlags));
9042
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009043 mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId)));
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009044 }
9045
9046private:
9047 sp<FakeWindowHandle> mWindow;
9048};
9049
9050TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009051 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
9052 // filter. Without it, the event will no different from a regularly injected event, and the
9053 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00009054 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
9055 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009056}
9057
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009058TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009059 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00009060 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009061 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
9062}
9063
9064TEST_F(InputFilterInjectionPolicyTest,
9065 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
9066 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00009067 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009068 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009069}
9070
9071TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00009072 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
9073 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009074}
9075
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009076class InputDispatcherUserActivityPokeTests : public InputDispatcherTest {
9077protected:
9078 virtual void SetUp() override {
9079 InputDispatcherTest::SetUp();
9080
9081 std::shared_ptr<FakeApplicationHandle> application =
9082 std::make_shared<FakeApplicationHandle>();
9083 application->setDispatchingTimeout(100ms);
9084 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009085 ui::LogicalDisplayId::DEFAULT);
Yeabkal Wubshit222d83d2024-01-24 18:00:09 +00009086 mWindow->setFrame(Rect(0, 0, 100, 100));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009087 mWindow->setDispatchingTimeout(100ms);
9088 mWindow->setFocusable(true);
9089
9090 // Set focused application.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009091 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009092
9093 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
9094 setFocusedWindow(mWindow);
9095 mWindow->consumeFocusEvent(true);
9096 }
9097
Linnan Li13bf76a2024-05-05 19:18:02 +08009098 void notifyAndConsumeMotion(int32_t action, uint32_t source, ui::LogicalDisplayId displayId,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009099 nsecs_t eventTime) {
9100 mDispatcher->notifyMotion(MotionArgsBuilder(action, source)
9101 .displayId(displayId)
9102 .eventTime(eventTime)
9103 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
9104 .build());
9105 mWindow->consumeMotionEvent(WithMotionAction(action));
9106 }
9107
9108private:
9109 sp<FakeWindowHandle> mWindow;
9110};
9111
9112TEST_F_WITH_FLAGS(
9113 InputDispatcherUserActivityPokeTests, MinPokeTimeObserved,
9114 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
9115 rate_limit_user_activity_poke_in_dispatcher))) {
9116 mDispatcher->setMinTimeBetweenUserActivityPokes(50ms);
9117
9118 // First event of type TOUCH. Should poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009119 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009120 milliseconds_to_nanoseconds(50));
9121 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009122 {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH,
9123 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009124
9125 // 80ns > 50ns has passed since previous TOUCH event. Should poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009126 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009127 milliseconds_to_nanoseconds(130));
9128 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009129 {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH,
9130 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009131
9132 // First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event).
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009133 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
9134 ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(135));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009135 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009136 {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER,
9137 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009138
9139 // Within 50ns of previous TOUCH event. Should NOT poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009140 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009141 milliseconds_to_nanoseconds(140));
9142 mFakePolicy->assertUserActivityNotPoked();
9143
9144 // Within 50ns of previous OTHER event. Should NOT poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009145 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
9146 ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(150));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009147 mFakePolicy->assertUserActivityNotPoked();
9148
9149 // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke.
9150 // Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009151 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009152 milliseconds_to_nanoseconds(160));
9153 mFakePolicy->assertUserActivityNotPoked();
9154
9155 // 65ns > 50ns has passed since previous OTHER event. Should poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009156 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
9157 ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(200));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009158 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009159 {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER,
9160 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009161
9162 // 170ns > 50ns has passed since previous TOUCH event. Should poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009163 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009164 milliseconds_to_nanoseconds(300));
9165 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009166 {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH,
9167 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009168
9169 // Assert that there's no more user activity poke event.
9170 mFakePolicy->assertUserActivityNotPoked();
9171}
9172
9173TEST_F_WITH_FLAGS(
9174 InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed,
9175 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
9176 rate_limit_user_activity_poke_in_dispatcher))) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009177 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009178 milliseconds_to_nanoseconds(200));
9179 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009180 {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH,
9181 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009182
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009183 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009184 milliseconds_to_nanoseconds(280));
9185 mFakePolicy->assertUserActivityNotPoked();
9186
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009187 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009188 milliseconds_to_nanoseconds(340));
9189 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009190 {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH,
9191 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009192}
9193
9194TEST_F_WITH_FLAGS(
9195 InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting,
9196 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
9197 rate_limit_user_activity_poke_in_dispatcher))) {
9198 mDispatcher->setMinTimeBetweenUserActivityPokes(0ms);
9199
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009200 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
9201 20);
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009202 mFakePolicy->assertUserActivityPoked();
9203
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009204 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
9205 30);
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009206 mFakePolicy->assertUserActivityPoked();
9207}
9208
chaviwfd6d3512019-03-25 13:23:49 -07009209class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07009210 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07009211 InputDispatcherTest::SetUp();
9212
Chris Yea209fde2020-07-22 13:54:51 -07009213 std::shared_ptr<FakeApplicationHandle> application =
9214 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009215 mUnfocusedWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Top",
9216 ui::LogicalDisplayId::DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07009217 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07009218
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009219 mFocusedWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Second",
9220 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08009221 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07009222
9223 // Set focused application.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009224 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07009225 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07009226
9227 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009228 mDispatcher->onWindowInfosChanged(
9229 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009230 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01009231 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07009232 }
9233
Prabir Pradhan3608aad2019-10-02 17:08:26 -07009234 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07009235 InputDispatcherTest::TearDown();
9236
9237 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08009238 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07009239 }
9240
9241protected:
9242 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08009243 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07009244 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07009245};
9246
9247// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
9248// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
9249// the onPointerDownOutsideFocus callback.
9250TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009251 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009252 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9253 ui::LogicalDisplayId::DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009254 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07009255 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07009256
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08009257 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07009258 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
9259}
9260
9261// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
9262// DOWN on the window that doesn't have focus. Ensure no window received the
9263// onPointerDownOutsideFocus callback.
9264TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009265 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009266 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009267 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009268 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07009269 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07009270
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08009271 ASSERT_TRUE(mDispatcher->waitForIdle());
9272 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07009273}
9274
9275// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
9276// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
9277TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08009278 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009279 injectKeyDownNoRepeat(*mDispatcher, ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009280 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009281 mFocusedWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07009282
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08009283 ASSERT_TRUE(mDispatcher->waitForIdle());
9284 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07009285}
9286
9287// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
9288// DOWN on the window that already has focus. Ensure no window received the
9289// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10009290TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009291 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009292 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9293 ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009294 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07009295 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07009296
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08009297 ASSERT_TRUE(mDispatcher->waitForIdle());
9298 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07009299}
9300
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08009301// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
9302// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
9303TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
9304 const MotionEvent event =
9305 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
9306 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009307 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08009308 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
9309 .build();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009310 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08009311 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009312 mUnfocusedWindow->consumeAnyMotionDown(ui::LogicalDisplayId::DEFAULT,
9313 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08009314
9315 ASSERT_TRUE(mDispatcher->waitForIdle());
9316 mFakePolicy->assertOnPointerDownWasNotCalled();
9317 // Ensure that the unfocused window did not receive any FOCUS events.
9318 mUnfocusedWindow->assertNoEvents();
9319}
9320
chaviwaf87b3e2019-10-01 16:59:28 -07009321// These tests ensures we can send touch events to a single client when there are multiple input
9322// windows that point to the same client token.
9323class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
9324 virtual void SetUp() override {
9325 InputDispatcherTest::SetUp();
9326
Chris Yea209fde2020-07-22 13:54:51 -07009327 std::shared_ptr<FakeApplicationHandle> application =
9328 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009329 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009330 ui::LogicalDisplayId::DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07009331 mWindow1->setFrame(Rect(0, 0, 100, 100));
9332
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009333 mWindow2 = mWindow1->clone(ui::LogicalDisplayId::DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07009334 mWindow2->setFrame(Rect(100, 100, 200, 200));
9335
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009336 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07009337 }
9338
9339protected:
9340 sp<FakeWindowHandle> mWindow1;
9341 sp<FakeWindowHandle> mWindow2;
9342
9343 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05009344 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07009345 vec2 vals = windowInfo->transform.transform(point.x, point.y);
9346 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07009347 }
9348
9349 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
9350 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01009351 const std::string name = window->getName();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009352 std::unique_ptr<MotionEvent> motionEvent =
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009353 window->consumeMotionEvent(WithMotionAction(expectedAction));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009354 ASSERT_NE(nullptr, motionEvent);
9355 ASSERT_EQ(points.size(), motionEvent->getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07009356
9357 for (size_t i = 0; i < points.size(); i++) {
9358 float expectedX = points[i].x;
9359 float expectedY = points[i].y;
9360
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009361 EXPECT_EQ(expectedX, motionEvent->getX(i))
chaviwaf87b3e2019-10-01 16:59:28 -07009362 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009363 << ", got " << motionEvent->getX(i);
9364 EXPECT_EQ(expectedY, motionEvent->getY(i))
chaviwaf87b3e2019-10-01 16:59:28 -07009365 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009366 << ", got " << motionEvent->getY(i);
chaviwaf87b3e2019-10-01 16:59:28 -07009367 }
9368 }
chaviw9eaa22c2020-07-01 16:21:27 -07009369
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009370 void touchAndAssertPositions(sp<FakeWindowHandle> touchedWindow, int32_t action,
9371 const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07009372 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009373 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009374 ui::LogicalDisplayId::DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07009375
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009376 consumeMotionEvent(touchedWindow, action, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07009377 }
chaviwaf87b3e2019-10-01 16:59:28 -07009378};
9379
9380TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
9381 // Touch Window 1
9382 PointF touchedPoint = {10, 10};
9383 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009384 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009385
9386 // Release touch on Window 1
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009387 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009388
9389 // Touch Window 2
9390 touchedPoint = {150, 150};
9391 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009392 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009393}
9394
chaviw9eaa22c2020-07-01 16:21:27 -07009395TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
9396 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07009397 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009398 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07009399
9400 // Touch Window 1
9401 PointF touchedPoint = {10, 10};
9402 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009403 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009404 // Release touch on Window 1
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009405 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009406
9407 // Touch Window 2
9408 touchedPoint = {150, 150};
9409 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009410 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
9411 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009412
chaviw9eaa22c2020-07-01 16:21:27 -07009413 // Update the transform so rotation is set
9414 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009415 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07009416 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009417 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009418}
9419
chaviw9eaa22c2020-07-01 16:21:27 -07009420TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009421 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009422 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009423
9424 // Touch Window 1
9425 std::vector<PointF> touchedPoints = {PointF{10, 10}};
9426 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009427 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009428
9429 // Touch Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009430 // Since this is part of the same touch gesture that has already been dispatched to Window 1,
9431 // the touch stream from Window 2 will be merged with the stream in Window 1. The merged stream
9432 // will continue to be dispatched through Window 1.
chaviw9eaa22c2020-07-01 16:21:27 -07009433 touchedPoints.push_back(PointF{150, 150});
9434 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009435 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009436
chaviw9eaa22c2020-07-01 16:21:27 -07009437 // Release Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009438 touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07009439 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009440
chaviw9eaa22c2020-07-01 16:21:27 -07009441 // Update the transform so rotation is set for Window 2
9442 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009443 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07009444 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009445 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009446}
9447
chaviw9eaa22c2020-07-01 16:21:27 -07009448TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009449 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009450 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009451
9452 // Touch Window 1
9453 std::vector<PointF> touchedPoints = {PointF{10, 10}};
9454 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009455 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009456
9457 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07009458 touchedPoints.push_back(PointF{150, 150});
9459 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009460
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009461 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009462
9463 // Move both windows
9464 touchedPoints = {{20, 20}, {175, 175}};
9465 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
9466 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
9467
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009468 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009469
chaviw9eaa22c2020-07-01 16:21:27 -07009470 // Release Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009471 touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07009472 expectedPoints.pop_back();
9473
9474 // Touch Window 2
9475 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009476 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07009477 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009478 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07009479
9480 // Move both windows
9481 touchedPoints = {{20, 20}, {175, 175}};
9482 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
9483 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
9484
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009485 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009486}
9487
9488TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
9489 mWindow1->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009490 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009491
9492 // Touch Window 1
9493 std::vector<PointF> touchedPoints = {PointF{10, 10}};
9494 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009495 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009496
9497 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07009498 touchedPoints.push_back(PointF{150, 150});
9499 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009500
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009501 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009502
9503 // Move both windows
9504 touchedPoints = {{20, 20}, {175, 175}};
9505 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
9506 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
9507
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009508 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009509}
9510
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07009511/**
9512 * When one of the windows is slippery, the touch should not slip into the other window with the
9513 * same input channel.
9514 */
9515TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
9516 mWindow1->setSlippery(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009517 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07009518
9519 // Touch down in window 1
9520 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009521 ui::LogicalDisplayId::DEFAULT, {{50, 50}}));
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07009522 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
9523
9524 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
9525 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
9526 // getting generated.
9527 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009528 ui::LogicalDisplayId::DEFAULT, {{150, 150}}));
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07009529
9530 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
9531}
9532
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009533/**
9534 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
9535 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
9536 * that the pointer is hovering over may have a different transform.
9537 */
9538TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009539 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009540
9541 // Start hover in window 1
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07009542 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN)
9543 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
9544 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009545 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
9546 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009547 // Move hover to window 2.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07009548 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
9549 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
9550 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009551 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009552 consumeMotionEvent(mWindow2, ACTION_HOVER_ENTER,
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009553 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
9554}
9555
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009556class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
9557 virtual void SetUp() override {
9558 InputDispatcherTest::SetUp();
9559
Chris Yea209fde2020-07-22 13:54:51 -07009560 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009561 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009562 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009563 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009564 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009565 mWindow->setDispatchingTimeout(100ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07009566 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009567
9568 // Set focused application.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009569 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009570
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009571 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009572 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009573 mWindow->consumeFocusEvent(true);
9574 }
9575
9576 virtual void TearDown() override {
9577 InputDispatcherTest::TearDown();
9578 mWindow.clear();
9579 }
9580
9581protected:
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009582 static constexpr std::chrono::duration SPY_TIMEOUT = 200ms;
Chris Yea209fde2020-07-22 13:54:51 -07009583 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009584 sp<FakeWindowHandle> mWindow;
9585 static constexpr PointF WINDOW_LOCATION = {20, 20};
9586
9587 void tapOnWindow() {
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08009588 const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER)
9589 .x(WINDOW_LOCATION.x)
9590 .y(WINDOW_LOCATION.y);
9591 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9592 .pointer(touchingPointer)
9593 .build());
9594 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
9595 .pointer(touchingPointer)
9596 .build());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009597 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009598
9599 sp<FakeWindowHandle> addSpyWindow() {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009600 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy",
9601 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009602 spy->setTrustedOverlay(true);
9603 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009604 spy->setSpy(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009605 spy->setDispatchingTimeout(SPY_TIMEOUT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009606 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009607 return spy;
9608 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009609};
9610
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009611// Send a tap and respond, which should not cause an ANR.
9612TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
9613 tapOnWindow();
9614 mWindow->consumeMotionDown();
9615 mWindow->consumeMotionUp();
9616 ASSERT_TRUE(mDispatcher->waitForIdle());
9617 mFakePolicy->assertNotifyAnrWasNotCalled();
9618}
9619
9620// Send a regular key and respond, which should not cause an ANR.
9621TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009622 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009623 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009624 ASSERT_TRUE(mDispatcher->waitForIdle());
9625 mFakePolicy->assertNotifyAnrWasNotCalled();
9626}
9627
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05009628TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
9629 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009630 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05009631 mWindow->consumeFocusEvent(false);
9632
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009633 InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009634 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
9635 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
9636 CONSUME_TIMEOUT_EVENT_EXPECTED,
Harry Cutts33476232023-01-30 19:57:29 +00009637 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009638 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05009639 // Key will not go to window because we have no focused window.
9640 // The 'no focused window' ANR timer should start instead.
9641
9642 // Now, the focused application goes away.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009643 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, nullptr);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05009644 // The key should get dropped and there should be no ANR.
9645
9646 ASSERT_TRUE(mDispatcher->waitForIdle());
9647 mFakePolicy->assertNotifyAnrWasNotCalled();
9648}
9649
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009650// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009651// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
9652// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009653TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009654 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009655 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9656 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009657
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009658 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009659 ASSERT_TRUE(sequenceNum);
9660 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009661 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009662
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009663 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009664 mWindow->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009665 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009666 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009667 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009668}
9669
9670// Send a key to the app and have the app not respond right away.
9671TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
9672 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009673 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009674 const auto [sequenceNum, _] = mWindow->receiveEvent();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009675 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009676 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009677 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009678 ASSERT_TRUE(mDispatcher->waitForIdle());
9679}
9680
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009681// We have a focused application, but no focused window
9682TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07009683 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009684 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009685 mWindow->consumeFocusEvent(false);
9686
9687 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009689 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9690 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009691 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
9692 mDispatcher->waitForIdle();
9693 mFakePolicy->assertNotifyAnrWasNotCalled();
9694
9695 // Once a focused event arrives, we get an ANR for this application
9696 // We specify the injection timeout to be smaller than the application timeout, to ensure that
9697 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009698 const InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009699 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
9700 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::WAIT_FOR_RESULT, 50ms,
Linnan Li13bf76a2024-05-05 19:18:02 +08009701 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009702 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009703 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07009704 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009705 ASSERT_TRUE(mDispatcher->waitForIdle());
9706}
9707
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009708/**
9709 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
9710 * there will not be an ANR.
9711 */
9712TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
9713 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009714 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009715 mWindow->consumeFocusEvent(false);
9716
9717 KeyEvent event;
Siarhei Vishniakoua7333112023-10-27 13:33:29 -07009718 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
9719 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009720 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
9721 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
9722
9723 // Define a valid key down event that is stale (too old).
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009724 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
9725 ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN,
9726 /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime,
9727 eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009728
Hu Guofe3c8f12023-09-22 17:20:15 +08009729 const int32_t policyFlags =
9730 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009731
9732 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00009733 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009734 InputEventInjectionSync::WAIT_FOR_RESULT,
9735 INJECT_EVENT_TIMEOUT, policyFlags);
9736 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
9737 << "Injection should fail because the event is stale";
9738
9739 ASSERT_TRUE(mDispatcher->waitForIdle());
9740 mFakePolicy->assertNotifyAnrWasNotCalled();
9741 mWindow->assertNoEvents();
9742}
9743
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009744// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009745// Make sure that we don't notify policy twice about the same ANR.
9746TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07009747 const std::chrono::duration appTimeout = 400ms;
9748 mApplication->setDispatchingTimeout(appTimeout);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009749 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication);
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07009750
Vishnu Nair47074b82020-08-14 11:54:47 -07009751 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009752 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009753 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009754
9755 // Once a focused event arrives, we get an ANR for this application
9756 // We specify the injection timeout to be smaller than the application timeout, to ensure that
9757 // injection times out (instead of failing).
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07009758 const std::chrono::duration eventInjectionTimeout = 100ms;
9759 ASSERT_LT(eventInjectionTimeout, appTimeout);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009760 const InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009761 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
9762 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::WAIT_FOR_RESULT,
9763 eventInjectionTimeout,
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07009764 /*allowKeyRepeat=*/false);
9765 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result)
9766 << "result=" << ftl::enum_string(result);
9767 // We already waited for 'eventInjectionTimeout`, because the countdown started when the event
9768 // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait.
9769 std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout;
9770 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009771
Vishnu Naire4df8752022-09-08 09:17:55 -07009772 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009773 // ANR should not be raised again. It is up to policy to do that if it desires.
9774 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009775
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009776 // If we now get a focused window, the ANR should stop, but the policy handles that via
9777 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009778 ASSERT_TRUE(mDispatcher->waitForIdle());
9779}
9780
9781// We have a focused application, but no focused window
9782TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07009783 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009784 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009785 mWindow->consumeFocusEvent(false);
9786
9787 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009788 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009789
Vishnu Naire4df8752022-09-08 09:17:55 -07009790 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
9791 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009792
9793 // Future focused events get dropped right away
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009794 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009795 ASSERT_TRUE(mDispatcher->waitForIdle());
9796 mWindow->assertNoEvents();
9797}
9798
9799/**
9800 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
9801 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
9802 * If we process 1 of the events, but ANR on the second event with the same timestamp,
9803 * the ANR mechanism should still work.
9804 *
9805 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
9806 * DOWN event, while not responding on the second one.
9807 */
9808TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
9809 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009810 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009811 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009812 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9813 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009814 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009815
9816 // Now send ACTION_UP, with identical timestamp
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009817 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009818 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009819 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9820 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009821 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009822
9823 // 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 -07009824 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009825 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009826 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009827}
9828
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009829// A spy window can receive an ANR
9830TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
9831 sp<FakeWindowHandle> spy = addSpyWindow();
9832
9833 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009834 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9835 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009836 mWindow->consumeMotionDown();
9837
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009838 const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009839 ASSERT_TRUE(sequenceNum);
9840 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009841 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009842
9843 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009844 spy->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009845 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009846 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009847 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009848}
9849
9850// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009851// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009852TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
9853 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009854
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009855 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009856 injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT));
9857 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
9858 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9859 injectKeyUp(*mDispatcher, ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009860
9861 // Stuck on the ACTION_UP
9862 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009863 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009864
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009865 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009866 tapOnWindow();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009867 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
9868 spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009869
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009870 mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); // still the previous motion
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009871 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08009872 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009873 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009874 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009875}
9876
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009877// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009878// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009879TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
9880 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009881
9882 tapOnWindow();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009883 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
9884 spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009885
9886 mWindow->consumeMotionDown();
9887 // Stuck on the ACTION_UP
9888 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009889 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009890
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009891 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009892 tapOnWindow();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009893 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
9894 spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009895
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009896 mWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); // still the previous motion
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009897 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08009898 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009899 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009900 spy->assertNoEvents();
9901}
9902
9903TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009904 mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009905
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009906 FakeMonitorReceiver monitor =
9907 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009908
9909 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009910 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9911 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009912
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009913 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009914 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
9915 ASSERT_TRUE(consumeSeq);
9916
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009917 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(),
9918 MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009919
9920 monitor.finishEvent(*consumeSeq);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009921 monitor.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009922
9923 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009924 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009925}
9926
9927// If a window is unresponsive, then you get anr. if the window later catches up and starts to
9928// process events, you don't get an anr. When the window later becomes unresponsive again, you
9929// get an ANR again.
9930// 1. tap -> block on ACTION_UP -> receive ANR
9931// 2. consume all pending events (= queue becomes healthy again)
9932// 3. tap again -> block on ACTION_UP again -> receive ANR second time
9933TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
9934 tapOnWindow();
9935
9936 mWindow->consumeMotionDown();
9937 // Block on ACTION_UP
9938 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009939 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009940 mWindow->consumeMotionUp(); // Now the connection should be healthy again
9941 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08009942 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009943 mWindow->assertNoEvents();
9944
9945 tapOnWindow();
9946 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08009947 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009948 mWindow->consumeMotionUp();
9949
9950 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08009951 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009952 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009953 mWindow->assertNoEvents();
9954}
9955
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009956// If a connection remains unresponsive for a while, make sure policy is only notified once about
9957// it.
9958TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009959 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009960 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9961 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009962
9963 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009964 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009965 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009966 // 'notifyConnectionUnresponsive' should only be called once per connection
9967 mFakePolicy->assertNotifyAnrWasNotCalled();
9968 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009969 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009970 mWindow->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009971 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009972 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009973 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08009974 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009975 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009976}
9977
9978/**
9979 * 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 -07009980 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009981 */
9982TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08009983 // The timeouts in this test are established by relying on the fact that the "key waiting for
9984 // events timeout" is equal to 500ms.
9985 ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009986 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009987 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009988
9989 tapOnWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009990 const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009991 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009992 const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009993 ASSERT_TRUE(upSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009994
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08009995 // Don't finish the events yet, and send a key
9996 mDispatcher->notifyKey(
9997 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
9998 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
9999 .build());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010000 // Key will not be sent to the window, yet, because the window is still processing events
10001 // and the key remains pending, waiting for the touch events to be processed
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010002 // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR.
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010003 mWindow->assertNoEvents(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010004
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010005 std::this_thread::sleep_for(400ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010006 // if we wait long enough though, dispatcher will give up, and still send the key
10007 // to the focused window, even though we have not yet finished the motion event
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010008 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010009 mWindow->finishEvent(*downSequenceNum);
10010 mWindow->finishEvent(*upSequenceNum);
10011}
10012
10013/**
10014 * If a window is processing a motion event, and then a key event comes in, the key event should
10015 * not go to the focused window until the motion is processed.
10016 * If then a new motion comes in, then the pending key event should be going to the currently
10017 * focused window right away.
10018 */
10019TEST_F(InputDispatcherSingleWindowAnr,
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010020 PendingKey_IsDeliveredWhileMotionIsProcessingAndNewTouchComesIn) {
10021 // The timeouts in this test are established by relying on the fact that the "key waiting for
10022 // events timeout" is equal to 500ms.
10023 ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010024 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010025 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010026
10027 tapOnWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010028 const auto& [downSequenceNum, _] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010029 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010030 const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010031 ASSERT_TRUE(upSequenceNum);
10032 // Don't finish the events yet, and send a key
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -080010033 mDispatcher->notifyKey(
10034 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
10035 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
10036 .build());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010037 // At this point, key is still pending, and should not be sent to the application yet.
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010038 mWindow->assertNoEvents(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010039
10040 // Now tap down again. It should cause the pending key to go to the focused window right away.
10041 tapOnWindow();
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010042 // Now that we tapped, we should receive the key immediately.
10043 // Since there's still room for slowness, we use 200ms, which is much less than
10044 // the "key waiting for events' timeout of 500ms minus the already waited 100ms duration.
10045 std::unique_ptr<InputEvent> keyEvent = mWindow->consume(200ms);
10046 ASSERT_NE(nullptr, keyEvent);
10047 ASSERT_EQ(InputEventType::KEY, keyEvent->getType());
10048 ASSERT_THAT(static_cast<KeyEvent&>(*keyEvent), WithKeyAction(AKEY_EVENT_ACTION_DOWN));
10049 // it doesn't matter that we haven't ack'd the other events yet. We can finish events in any
10050 // order.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010051 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
10052 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -080010053 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
10054 mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010055 mWindow->assertNoEvents();
10056}
10057
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -070010058/**
10059 * Send an event to the app and have the app not respond right away.
10060 * When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
10061 * So InputDispatcher will enqueue ACTION_CANCEL event as well.
10062 * At some point, the window becomes responsive again.
10063 * Ensure that subsequent events get dropped, and the next gesture is delivered.
10064 */
10065TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) {
10066 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10067 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
10068 .build());
10069
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010070 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -070010071 ASSERT_TRUE(sequenceNum);
10072 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
10073 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
10074
10075 mWindow->finishEvent(*sequenceNum);
10076 mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
10077 ASSERT_TRUE(mDispatcher->waitForIdle());
10078 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
10079
10080 // Now that the window is responsive, let's continue the gesture.
10081 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
10082 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
10083 .build());
10084
10085 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10086 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
10087 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
10088 .build());
10089
10090 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
10091 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
10092 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
10093 .build());
10094 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
10095 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
10096 .build());
10097 // We already canceled this pointer, so the window shouldn't get any new events.
10098 mWindow->assertNoEvents();
10099
10100 // Start another one.
10101 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10102 .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15))
10103 .build());
10104 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
10105}
10106
Prabir Pradhanfc364722024-02-08 17:51:20 +000010107// Send an event to the app and have the app not respond right away. Then remove the app window.
10108// When the window is removed, the dispatcher will cancel the events for that window.
10109// So InputDispatcher will enqueue ACTION_CANCEL event as well.
10110TEST_F(InputDispatcherSingleWindowAnr, AnrAfterWindowRemoval) {
10111 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010112 AINPUT_SOURCE_TOUCHSCREEN,
10113 ui::LogicalDisplayId::DEFAULT, {WINDOW_LOCATION}));
Prabir Pradhanfc364722024-02-08 17:51:20 +000010114
10115 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
10116 ASSERT_TRUE(sequenceNum);
10117
10118 // Remove the window, but the input channel should remain alive.
10119 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
10120
10121 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
10122 // Since the window was removed, Dispatcher does not know the PID associated with the window
10123 // anymore, so the policy is notified without the PID.
10124 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken(),
10125 /*pid=*/std::nullopt);
10126
10127 mWindow->finishEvent(*sequenceNum);
10128 // The cancellation was generated when the window was removed, along with the focus event.
10129 mWindow->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010130 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Prabir Pradhanfc364722024-02-08 17:51:20 +000010131 mWindow->consumeFocusEvent(false);
10132 ASSERT_TRUE(mDispatcher->waitForIdle());
10133 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt);
10134}
10135
10136// Send an event to the app and have the app not respond right away. Wait for the policy to be
10137// notified of the unresponsive window, then remove the app window.
10138TEST_F(InputDispatcherSingleWindowAnr, AnrFollowedByWindowRemoval) {
10139 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010140 AINPUT_SOURCE_TOUCHSCREEN,
10141 ui::LogicalDisplayId::DEFAULT, {WINDOW_LOCATION}));
Prabir Pradhanfc364722024-02-08 17:51:20 +000010142
10143 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
10144 ASSERT_TRUE(sequenceNum);
10145 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
10146 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
10147
10148 // Remove the window, but the input channel should remain alive.
10149 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
10150
10151 mWindow->finishEvent(*sequenceNum);
10152 // The cancellation was generated during the ANR, and the window lost focus when it was removed.
10153 mWindow->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010154 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Prabir Pradhanfc364722024-02-08 17:51:20 +000010155 mWindow->consumeFocusEvent(false);
10156 ASSERT_TRUE(mDispatcher->waitForIdle());
10157 // Since the window was removed, Dispatcher does not know the PID associated with the window
10158 // becoming responsive, so the policy is notified without the PID.
10159 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt);
10160}
10161
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010162class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
10163 virtual void SetUp() override {
10164 InputDispatcherTest::SetUp();
10165
Chris Yea209fde2020-07-22 13:54:51 -070010166 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010167 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010168 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010169 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010170 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010171 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010172 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010173
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010174 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010175 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010176 mFocusedWindow->setDispatchingTimeout(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010177 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010178
10179 // Set focused application.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010180 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -070010181 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010182
10183 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010184 mDispatcher->onWindowInfosChanged(
10185 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010186 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010187 mFocusedWindow->consumeFocusEvent(true);
10188 }
10189
10190 virtual void TearDown() override {
10191 InputDispatcherTest::TearDown();
10192
10193 mUnfocusedWindow.clear();
10194 mFocusedWindow.clear();
10195 }
10196
10197protected:
Chris Yea209fde2020-07-22 13:54:51 -070010198 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010199 sp<FakeWindowHandle> mUnfocusedWindow;
10200 sp<FakeWindowHandle> mFocusedWindow;
10201 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
10202 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
10203 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
10204
10205 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
10206
10207 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
10208
10209private:
10210 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010211 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010212 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10213 ui::LogicalDisplayId::DEFAULT, location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010214 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010215 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10216 ui::LogicalDisplayId::DEFAULT, location));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010217 }
10218};
10219
10220// If we have 2 windows that are both unresponsive, the one with the shortest timeout
10221// should be ANR'd first.
10222TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010223 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010224 injectMotionEvent(*mDispatcher,
10225 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10226 AINPUT_SOURCE_TOUCHSCREEN)
10227 .pointer(PointerBuilder(0, ToolType::FINGER)
10228 .x(FOCUSED_WINDOW_LOCATION.x)
10229 .y(FOCUSED_WINDOW_LOCATION.y))
10230 .build()));
10231 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10232 injectMotionEvent(*mDispatcher,
10233 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
10234 AINPUT_SOURCE_TOUCHSCREEN)
10235 .pointer(PointerBuilder(0, ToolType::FINGER)
10236 .x(FOCUSED_WINDOW_LOCATION.x)
10237 .y(FOCUSED_WINDOW_LOCATION.y))
10238 .build()));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010239 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010240 mFocusedWindow->consumeMotionUp();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010241 mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010242 // We consumed all events, so no ANR
10243 ASSERT_TRUE(mDispatcher->waitForIdle());
10244 mFakePolicy->assertNotifyAnrWasNotCalled();
10245
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010246 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010247 injectMotionEvent(*mDispatcher,
10248 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10249 AINPUT_SOURCE_TOUCHSCREEN)
10250 .pointer(PointerBuilder(0, ToolType::FINGER)
10251 .x(FOCUSED_WINDOW_LOCATION.x)
10252 .y(FOCUSED_WINDOW_LOCATION.y))
10253 .build()));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010254 const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010255 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010256
10257 const std::chrono::duration timeout =
10258 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -080010259 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010260
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010261 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010262 mFocusedWindow->consumeMotionDown();
10263 // This cancel is generated because the connection was unresponsive
10264 mFocusedWindow->consumeMotionCancel();
10265 mFocusedWindow->assertNoEvents();
10266 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010267 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -080010268 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
10269 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010270 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010271}
10272
10273// If we have 2 windows with identical timeouts that are both unresponsive,
10274// it doesn't matter which order they should have ANR.
10275// But we should receive ANR for both.
10276TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
10277 // Set the timeout for unfocused window to match the focused window
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010278 mUnfocusedWindow->setDispatchingTimeout(
10279 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010280 mDispatcher->onWindowInfosChanged(
10281 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010282
10283 tapOnFocusedWindow();
10284 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010285 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010286 std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken(
10287 mFocusedWindow->getDispatchingTimeout(
10288 DISPATCHING_TIMEOUT)),
10289 mFakePolicy->getUnresponsiveWindowToken(0ms)};
10290
10291 ASSERT_THAT(anrConnectionTokens,
10292 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
10293 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010294
10295 ASSERT_TRUE(mDispatcher->waitForIdle());
10296 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010297
10298 mFocusedWindow->consumeMotionDown();
10299 mFocusedWindow->consumeMotionUp();
10300 mUnfocusedWindow->consumeMotionOutside();
10301
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010302 std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(),
10303 mFakePolicy->getResponsiveWindowToken()};
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010304
10305 // Both applications should be marked as responsive, in any order
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010306 ASSERT_THAT(responsiveTokens,
10307 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
10308 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010309 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010310}
10311
10312// If a window is already not responding, the second tap on the same window should be ignored.
10313// We should also log an error to account for the dropped event (not tested here).
10314// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
10315TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
10316 tapOnFocusedWindow();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010317 mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010318 // Receive the events, but don't respond
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010319 const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010320 ASSERT_TRUE(downEventSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010321 const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010322 ASSERT_TRUE(upEventSequenceNum);
10323 const std::chrono::duration timeout =
10324 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -080010325 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010326
10327 // Tap once again
10328 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010329 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010330 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10331 ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010332 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010333 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010334 FOCUSED_WINDOW_LOCATION));
10335 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
10336 // valid touch target
10337 mUnfocusedWindow->assertNoEvents();
10338
10339 // Consume the first tap
10340 mFocusedWindow->finishEvent(*downEventSequenceNum);
10341 mFocusedWindow->finishEvent(*upEventSequenceNum);
10342 ASSERT_TRUE(mDispatcher->waitForIdle());
10343 // The second tap did not go to the focused window
10344 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010345 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -080010346 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
10347 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010348 mFakePolicy->assertNotifyAnrWasNotCalled();
10349}
10350
10351// If you tap outside of all windows, there will not be ANR
10352TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010353 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010354 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10355 ui::LogicalDisplayId::DEFAULT, LOCATION_OUTSIDE_ALL_WINDOWS));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010356 ASSERT_TRUE(mDispatcher->waitForIdle());
10357 mFakePolicy->assertNotifyAnrWasNotCalled();
10358}
10359
10360// Since the focused window is paused, tapping on it should not produce any events
10361TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
10362 mFocusedWindow->setPaused(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010363 mDispatcher->onWindowInfosChanged(
10364 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010365
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010366 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010367 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10368 ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010369
10370 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
10371 ASSERT_TRUE(mDispatcher->waitForIdle());
10372 // Should not ANR because the window is paused, and touches shouldn't go to it
10373 mFakePolicy->assertNotifyAnrWasNotCalled();
10374
10375 mFocusedWindow->assertNoEvents();
10376 mUnfocusedWindow->assertNoEvents();
10377}
10378
10379/**
10380 * 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 -070010381 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010382 * If a different window becomes focused at this time, the key should go to that window instead.
10383 *
10384 * Warning!!!
10385 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
10386 * and the injection timeout that we specify when injecting the key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010387 * We must have the injection timeout (100ms) be smaller than
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010388 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
10389 *
10390 * If that value changes, this test should also change.
10391 */
10392TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
10393 // Set a long ANR timeout to prevent it from triggering
10394 mFocusedWindow->setDispatchingTimeout(2s);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010395 mDispatcher->onWindowInfosChanged(
10396 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010397
10398 tapOnUnfocusedWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010399 const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010400 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010401 const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010402 ASSERT_TRUE(upSequenceNum);
10403 // Don't finish the events yet, and send a key
10404 // Injection will succeed because we will eventually give up and send the key to the focused
10405 // window even if motions are still being processed.
10406
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010407 InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010408 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
10409 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
Linnan Li13bf76a2024-05-05 19:18:02 +080010410 /*injectionTimeout=*/100ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010411 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010412 // Key will not be sent to the window, yet, because the window is still processing events
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010413 // and the key remains pending, waiting for the touch events to be processed.
10414 // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED
10415 // under the hood.
10416 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
10417 mFocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010418
10419 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -070010420 mFocusedWindow->setFocusable(false);
10421 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010422 mDispatcher->onWindowInfosChanged(
10423 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010424 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010425
10426 // Focus events should precede the key events
10427 mUnfocusedWindow->consumeFocusEvent(true);
10428 mFocusedWindow->consumeFocusEvent(false);
10429
10430 // Finish the tap events, which should unblock dispatcher
10431 mUnfocusedWindow->finishEvent(*downSequenceNum);
10432 mUnfocusedWindow->finishEvent(*upSequenceNum);
10433
10434 // Now that all queues are cleared and no backlog in the connections, the key event
10435 // can finally go to the newly focused "mUnfocusedWindow".
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010436 mUnfocusedWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010437 mFocusedWindow->assertNoEvents();
10438 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010439 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010440}
10441
10442// When the touch stream is split across 2 windows, and one of them does not respond,
10443// then ANR should be raised and the touch should be canceled for the unresponsive window.
10444// The other window should not be affected by that.
10445TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
10446 // Touch Window 1
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010447 mDispatcher->notifyMotion(
10448 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
10449 ui::LogicalDisplayId::DEFAULT, {FOCUSED_WINDOW_LOCATION}));
10450 mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010451
10452 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +000010453 mDispatcher->notifyMotion(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010454 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
10455 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +000010456 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010457
10458 const std::chrono::duration timeout =
10459 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -080010460 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010461
10462 mUnfocusedWindow->consumeMotionDown();
10463 mFocusedWindow->consumeMotionDown();
10464 // Focused window may or may not receive ACTION_MOVE
10465 // But it should definitely receive ACTION_CANCEL due to the ANR
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010466 const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010467 ASSERT_TRUE(moveOrCancelSequenceNum);
10468 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
10469 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -070010470 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010471 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
10472 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
10473 mFocusedWindow->consumeMotionCancel();
10474 } else {
10475 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
10476 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010477 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -080010478 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
10479 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010480
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010481 mUnfocusedWindow->assertNoEvents();
10482 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010483 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010484}
10485
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010486/**
10487 * If we have no focused window, and a key comes in, we start the ANR timer.
10488 * The focused application should add a focused window before the timer runs out to prevent ANR.
10489 *
10490 * If the user touches another application during this time, the key should be dropped.
10491 * Next, if a new focused window comes in, without toggling the focused application,
10492 * then no ANR should occur.
10493 *
10494 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
10495 * but in some cases the policy may not update the focused application.
10496 */
10497TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
10498 std::shared_ptr<FakeApplicationHandle> focusedApplication =
10499 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouc033dfb2023-10-03 10:45:16 -070010500 focusedApplication->setDispatchingTimeout(300ms);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010501 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, focusedApplication);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010502 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
10503 mFocusedWindow->setFocusable(false);
10504
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010505 mDispatcher->onWindowInfosChanged(
10506 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010507 mFocusedWindow->consumeFocusEvent(false);
10508
10509 // Send a key. The ANR timer should start because there is no focused window.
10510 // 'focusedApplication' will get blamed if this timer completes.
10511 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010512 InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010513 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
10514 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
Linnan Li13bf76a2024-05-05 19:18:02 +080010515 /*injectionTimeout=*/100ms,
Harry Cutts33476232023-01-30 19:57:29 +000010516 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010517 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010518
10519 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
10520 // then the injected touches won't cause the focused event to get dropped.
10521 // The dispatcher only checks for whether the queue should be pruned upon queueing.
10522 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
10523 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
10524 // For this test, it means that the key would get delivered to the window once it becomes
10525 // focused.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010526 std::this_thread::sleep_for(100ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010527
10528 // Touch unfocused window. This should force the pending key to get dropped.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010529 mDispatcher->notifyMotion(
10530 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
10531 ui::LogicalDisplayId::DEFAULT, {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010532
10533 // We do not consume the motion right away, because that would require dispatcher to first
10534 // process (== drop) the key event, and by that time, ANR will be raised.
10535 // Set the focused window first.
10536 mFocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010537 mDispatcher->onWindowInfosChanged(
10538 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010539 setFocusedWindow(mFocusedWindow);
10540 mFocusedWindow->consumeFocusEvent(true);
10541 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
10542 // to another application. This could be a bug / behaviour in the policy.
10543
10544 mUnfocusedWindow->consumeMotionDown();
10545
10546 ASSERT_TRUE(mDispatcher->waitForIdle());
10547 // Should not ANR because we actually have a focused window. It was just added too slowly.
10548 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
10549}
10550
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010551/**
10552 * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having
10553 * an inconsistent event stream inside the dispatcher. In this test, we make sure that the
10554 * dispatcher doesn't prune pointer events incorrectly.
10555 *
10556 * This test reproduces a crash in InputDispatcher.
10557 * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur.
10558 *
10559 * Keep the currently focused application (mApplication), and have no focused window.
10560 * We set up two additional windows:
10561 * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the
10562 * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application
10563 * window. This window is not focusable, but is touchable.
10564 *
10565 * We first touch the navigation bar, which causes it to inject a key. Since there's no focused
10566 * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now
10567 * blocked. The dispatcher is waiting for 'mApplication' to add a focused window.
10568 *
10569 * Now, we touch "Another window". This window is owned by a different application than
10570 * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused
10571 * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start
10572 * dropping the events from its queue. Ensure that no crash occurs.
10573 *
10574 * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale.
10575 * This does not affect the test running time.
10576 */
10577TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) {
10578 std::shared_ptr<FakeApplicationHandle> systemUiApplication =
10579 std::make_shared<FakeApplicationHandle>();
10580 systemUiApplication->setDispatchingTimeout(3000ms);
10581 mFakePolicy->setStaleEventTimeout(3000ms);
10582 sp<FakeWindowHandle> navigationBar =
10583 sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010584 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010585 navigationBar->setFocusable(false);
10586 navigationBar->setWatchOutsideTouch(true);
10587 navigationBar->setFrame(Rect(0, 0, 100, 100));
10588
10589 mApplication->setDispatchingTimeout(3000ms);
10590 // 'mApplication' is already focused, but we call it again here to make it explicit.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010591 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication);
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010592
10593 std::shared_ptr<FakeApplicationHandle> anotherApplication =
10594 std::make_shared<FakeApplicationHandle>();
10595 sp<FakeWindowHandle> appWindow =
10596 sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010597 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010598 appWindow->setFocusable(false);
10599 appWindow->setFrame(Rect(100, 100, 200, 200));
10600
10601 mDispatcher->onWindowInfosChanged(
10602 {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0});
10603 // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus
10604 mFocusedWindow->consumeFocusEvent(false);
10605
10606 // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher
10607 // in response.
10608 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10609 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
10610 .build());
10611 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
10612
10613 // Key will not be sent anywhere because we have no focused window. It will remain pending.
10614 // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is.
10615 InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010616 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
10617 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
Linnan Li13bf76a2024-05-05 19:18:02 +080010618 /*injectionTimeout=*/100ms,
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010619 /*allowKeyRepeat=*/false);
10620 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
10621
10622 // Finish the gesture - lift up finger and inject ACTION_UP key event
10623 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
10624 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
10625 .build());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010626 result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0,
10627 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
Linnan Li13bf76a2024-05-05 19:18:02 +080010628 /*injectionTimeout=*/100ms,
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010629 /*allowKeyRepeat=*/false);
10630 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
10631 // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be
10632 // getting any events yet.
10633 navigationBar->assertNoEvents();
10634
10635 // Now touch "Another window". This touch is going to a different application than the one we
10636 // are waiting for (which is 'mApplication').
10637 // This should cause the dispatcher to drop the pending focus-dispatched events (like the key
10638 // trying to be injected) and to continue processing the rest of the events in the original
10639 // order.
10640 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10641 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
10642 .build());
10643 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP));
10644 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE));
10645 appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
10646
10647 appWindow->assertNoEvents();
10648 navigationBar->assertNoEvents();
10649}
10650
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010651// These tests ensure we cannot send touch events to a window that's positioned behind a window
10652// that has feature NO_INPUT_CHANNEL.
10653// Layout:
10654// Top (closest to user)
10655// mNoInputWindow (above all windows)
10656// mBottomWindow
10657// Bottom (furthest from user)
10658class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
10659 virtual void SetUp() override {
10660 InputDispatcherTest::SetUp();
10661
10662 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010663 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
10664 "Window without input channel",
10665 ui::LogicalDisplayId::DEFAULT,
10666 /*createInputChannel=*/false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010667 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010668 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
10669 // It's perfectly valid for this window to not have an associated input channel
10670
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010671 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010672 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010673 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
10674
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010675 mDispatcher->onWindowInfosChanged(
10676 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010677 }
10678
10679protected:
10680 std::shared_ptr<FakeApplicationHandle> mApplication;
10681 sp<FakeWindowHandle> mNoInputWindow;
10682 sp<FakeWindowHandle> mBottomWindow;
10683};
10684
10685TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
10686 PointF touchedPoint = {10, 10};
10687
Prabir Pradhan678438e2023-04-13 19:32:51 +000010688 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010689 AINPUT_SOURCE_TOUCHSCREEN,
10690 ui::LogicalDisplayId::DEFAULT, {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010691
10692 mNoInputWindow->assertNoEvents();
10693 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
10694 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
10695 // and therefore should prevent mBottomWindow from receiving touches
10696 mBottomWindow->assertNoEvents();
10697}
10698
10699/**
10700 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
10701 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
10702 */
10703TEST_F(InputDispatcherMultiWindowOcclusionTests,
10704 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010705 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
10706 "Window with input channel and NO_INPUT_CHANNEL",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010707 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010708
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010709 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010710 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010711 mDispatcher->onWindowInfosChanged(
10712 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010713
10714 PointF touchedPoint = {10, 10};
10715
Prabir Pradhan678438e2023-04-13 19:32:51 +000010716 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010717 AINPUT_SOURCE_TOUCHSCREEN,
10718 ui::LogicalDisplayId::DEFAULT, {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010719
10720 mNoInputWindow->assertNoEvents();
10721 mBottomWindow->assertNoEvents();
10722}
10723
Vishnu Nair958da932020-08-21 17:12:37 -070010724class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
10725protected:
10726 std::shared_ptr<FakeApplicationHandle> mApp;
10727 sp<FakeWindowHandle> mWindow;
10728 sp<FakeWindowHandle> mMirror;
10729
10730 virtual void SetUp() override {
10731 InputDispatcherTest::SetUp();
10732 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010733 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow",
10734 ui::LogicalDisplayId::DEFAULT);
10735 mMirror = mWindow->clone(ui::LogicalDisplayId::DEFAULT);
10736 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp);
Vishnu Nair958da932020-08-21 17:12:37 -070010737 mWindow->setFocusable(true);
10738 mMirror->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010739 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010740 }
10741};
10742
10743TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
10744 // Request focus on a mirrored window
10745 setFocusedWindow(mMirror);
10746
10747 // window gets focused
10748 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010749 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010750 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010751 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010752}
10753
10754// A focused & mirrored window remains focused only if the window and its mirror are both
10755// focusable.
10756TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
10757 setFocusedWindow(mMirror);
10758
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +000010759 // window gets focused because it is above the mirror
Vishnu Nair958da932020-08-21 17:12:37 -070010760 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010761 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010762 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010763 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010764 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010765 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010766 mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010767
10768 mMirror->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010769 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010770
10771 // window loses focus since one of the windows associated with the token in not focusable
10772 mWindow->consumeFocusEvent(false);
10773
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010774 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010775 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -070010776 mWindow->assertNoEvents();
10777}
10778
10779// A focused & mirrored window remains focused until the window and its mirror both become
10780// invisible.
10781TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
10782 setFocusedWindow(mMirror);
10783
10784 // window gets focused
10785 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010786 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010787 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010788 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010789 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010790 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010791 mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010792
10793 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010794 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010795
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010796 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010797 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010798 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010799 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010800 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010801 mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010802
10803 mWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010804 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010805
10806 // window loses focus only after all windows associated with the token become invisible.
10807 mWindow->consumeFocusEvent(false);
10808
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010809 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010810 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -070010811 mWindow->assertNoEvents();
10812}
10813
10814// A focused & mirrored window remains focused until both windows are removed.
10815TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
10816 setFocusedWindow(mMirror);
10817
10818 // window gets focused
10819 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010820 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010821 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010822 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010823 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010824 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010825 mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010826
10827 // single window is removed but the window token remains focused
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010828 mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010829
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010830 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010831 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010832 mMirror->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010833 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010834 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010835 mMirror->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010836
10837 // Both windows are removed
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010838 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010839 mWindow->consumeFocusEvent(false);
10840
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010841 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010842 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -070010843 mWindow->assertNoEvents();
10844}
10845
10846// Focus request can be pending until one window becomes visible.
10847TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
10848 // Request focus on an invisible mirror.
10849 mWindow->setVisible(false);
10850 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010851 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010852 setFocusedWindow(mMirror);
10853
10854 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010855 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010856 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010857 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -070010858
10859 mMirror->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010860 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010861
10862 // window gets focused
10863 mWindow->consumeFocusEvent(true);
10864 // window gets the pending key event
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010865 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -070010866}
Prabir Pradhan99987712020-11-10 18:43:05 -080010867
10868class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
10869protected:
10870 std::shared_ptr<FakeApplicationHandle> mApp;
10871 sp<FakeWindowHandle> mWindow;
10872 sp<FakeWindowHandle> mSecondWindow;
10873
10874 void SetUp() override {
10875 InputDispatcherTest::SetUp();
10876 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010877 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow",
10878 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -080010879 mWindow->setFocusable(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010880 mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2",
10881 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -080010882 mSecondWindow->setFocusable(true);
10883
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010884 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010885 mDispatcher->onWindowInfosChanged(
10886 {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan99987712020-11-10 18:43:05 -080010887
10888 setFocusedWindow(mWindow);
10889 mWindow->consumeFocusEvent(true);
10890 }
10891
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010892 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000010893 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -080010894 }
10895
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010896 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
10897 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -080010898 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Hiroki Sato25040232024-02-22 17:21:22 +090010899 auto request = mFakePolicy->assertSetPointerCaptureCalled(window, enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010900 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -080010901 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010902 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -080010903 }
10904};
10905
10906TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
10907 // Ensure that capture cannot be obtained for unfocused windows.
10908 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
10909 mFakePolicy->assertSetPointerCaptureNotCalled();
10910 mSecondWindow->assertNoEvents();
10911
10912 // Ensure that capture can be enabled from the focus window.
10913 requestAndVerifyPointerCapture(mWindow, true);
10914
10915 // Ensure that capture cannot be disabled from a window that does not have capture.
10916 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
10917 mFakePolicy->assertSetPointerCaptureNotCalled();
10918
10919 // Ensure that capture can be disabled from the window with capture.
10920 requestAndVerifyPointerCapture(mWindow, false);
10921}
10922
10923TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010924 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -080010925
10926 setFocusedWindow(mSecondWindow);
10927
10928 // Ensure that the capture disabled event was sent first.
10929 mWindow->consumeCaptureEvent(false);
10930 mWindow->consumeFocusEvent(false);
10931 mSecondWindow->consumeFocusEvent(true);
Hiroki Sato25040232024-02-22 17:21:22 +090010932 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan99987712020-11-10 18:43:05 -080010933
10934 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010935 notifyPointerCaptureChanged({});
10936 notifyPointerCaptureChanged(request);
10937 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -080010938 mWindow->assertNoEvents();
10939 mSecondWindow->assertNoEvents();
10940 mFakePolicy->assertSetPointerCaptureNotCalled();
10941}
10942
10943TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010944 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -080010945
10946 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010947 notifyPointerCaptureChanged({});
10948 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -080010949
10950 // Ensure that Pointer Capture is disabled.
Hiroki Sato25040232024-02-22 17:21:22 +090010951 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan99987712020-11-10 18:43:05 -080010952 mWindow->consumeCaptureEvent(false);
10953 mWindow->assertNoEvents();
10954}
10955
Prabir Pradhan167e6d92021-02-04 16:18:17 -080010956TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
10957 requestAndVerifyPointerCapture(mWindow, true);
10958
10959 // The first window loses focus.
10960 setFocusedWindow(mSecondWindow);
Hiroki Sato25040232024-02-22 17:21:22 +090010961 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -080010962 mWindow->consumeCaptureEvent(false);
10963
10964 // Request Pointer Capture from the second window before the notification from InputReader
10965 // arrives.
10966 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Hiroki Sato25040232024-02-22 17:21:22 +090010967 auto request = mFakePolicy->assertSetPointerCaptureCalled(mSecondWindow, true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -080010968
10969 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010970 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -080010971
10972 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010973 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -080010974
10975 mSecondWindow->consumeFocusEvent(true);
10976 mSecondWindow->consumeCaptureEvent(true);
10977}
10978
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010979TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
10980 // App repeatedly enables and disables capture.
10981 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
Hiroki Sato25040232024-02-22 17:21:22 +090010982 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010983 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
Hiroki Sato25040232024-02-22 17:21:22 +090010984 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010985 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
Hiroki Sato25040232024-02-22 17:21:22 +090010986 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010987
10988 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
10989 // first request is now stale, this should do nothing.
10990 notifyPointerCaptureChanged(firstRequest);
10991 mWindow->assertNoEvents();
10992
10993 // InputReader notifies that the second request was enabled.
10994 notifyPointerCaptureChanged(secondRequest);
10995 mWindow->consumeCaptureEvent(true);
10996}
10997
Prabir Pradhan7092e262022-05-03 16:51:09 +000010998TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
10999 requestAndVerifyPointerCapture(mWindow, true);
11000
11001 // App toggles pointer capture off and on.
11002 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
Hiroki Sato25040232024-02-22 17:21:22 +090011003 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan7092e262022-05-03 16:51:09 +000011004
11005 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
Hiroki Sato25040232024-02-22 17:21:22 +090011006 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true);
Prabir Pradhan7092e262022-05-03 16:51:09 +000011007
11008 // InputReader notifies that the latest "enable" request was processed, while skipping over the
11009 // preceding "disable" request.
11010 notifyPointerCaptureChanged(enableRequest);
11011
11012 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
11013 // any notifications.
11014 mWindow->assertNoEvents();
11015}
11016
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011017/**
11018 * One window. Hover mouse in the window, and then start capture. Make sure that the relative
11019 * mouse movements don't affect the previous mouse hovering state.
11020 * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no
11021 * HOVER_MOVE events).
11022 */
11023TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) {
11024 // Mouse hover on the window
11025 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
11026 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
11027 .build());
11028 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
11029 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
11030 .build());
11031
11032 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER)));
11033 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE)));
11034
11035 // Start pointer capture
11036 requestAndVerifyPointerCapture(mWindow, true);
11037
11038 // Send some relative mouse movements and receive them in the window.
11039 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE)
11040 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11))
11041 .build());
11042 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11),
11043 WithSource(AINPUT_SOURCE_MOUSE_RELATIVE)));
11044
11045 // Stop pointer capture
11046 requestAndVerifyPointerCapture(mWindow, false);
11047
11048 // Continue hovering on the window
11049 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
11050 .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115))
11051 .build());
11052 mWindow->consumeMotionEvent(
11053 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
11054
11055 mWindow->assertNoEvents();
11056}
11057
Prabir Pradhan9d3d5612024-06-06 20:34:26 +000011058TEST_F(InputDispatcherPointerCaptureTests, MultiDisplayPointerCapture) {
11059 // The default display is the focused display to begin with.
11060 requestAndVerifyPointerCapture(mWindow, true);
11061
11062 // Move the second window to a second display, make it the focused window on that display.
11063 mSecondWindow->editInfo()->displayId = SECOND_DISPLAY_ID;
11064 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
11065 setFocusedWindow(mSecondWindow);
11066 mSecondWindow->consumeFocusEvent(true);
11067
11068 mWindow->assertNoEvents();
11069
11070 // The second window cannot gain capture because it is not on the focused display.
11071 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
11072 mFakePolicy->assertSetPointerCaptureNotCalled();
11073 mSecondWindow->assertNoEvents();
11074
11075 // Make the second display the focused display.
11076 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
Arpit Singhb65e2bd2024-06-03 09:48:16 +000011077 mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID);
Prabir Pradhan9d3d5612024-06-06 20:34:26 +000011078
11079 // This causes the first window to lose pointer capture, and it's unable to request capture.
11080 mWindow->consumeCaptureEvent(false);
11081 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
11082
11083 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
11084 mFakePolicy->assertSetPointerCaptureNotCalled();
11085
11086 // The second window is now able to gain pointer capture successfully.
11087 requestAndVerifyPointerCapture(mSecondWindow, true);
11088}
11089
Hiroki Sato25040232024-02-22 17:21:22 +090011090using InputDispatcherPointerCaptureDeathTest = InputDispatcherPointerCaptureTests;
11091
11092TEST_F(InputDispatcherPointerCaptureDeathTest,
11093 NotifyPointerCaptureChangedWithWrongTokenAbortsDispatcher) {
11094 testing::GTEST_FLAG(death_test_style) = "threadsafe";
11095 ScopedSilentDeath _silentDeath;
11096
11097 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
11098 auto request = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true);
11099
11100 // Dispatch a pointer changed event with a wrong token.
11101 request.window = mSecondWindow->getToken();
11102 ASSERT_DEATH(
11103 {
11104 notifyPointerCaptureChanged(request);
11105 mSecondWindow->consumeCaptureEvent(true);
11106 },
11107 "Unexpected requested window for Pointer Capture.");
11108}
11109
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011110class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
11111protected:
11112 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +000011113
11114 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
11115 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
11116
11117 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
11118 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
11119
11120 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
11121 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
11122 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
11123 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
11124 MAXIMUM_OBSCURING_OPACITY);
11125
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011126 static constexpr gui::Uid TOUCHED_APP_UID{10001};
11127 static constexpr gui::Uid APP_B_UID{10002};
11128 static constexpr gui::Uid APP_C_UID{10003};
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011129
11130 sp<FakeWindowHandle> mTouchWindow;
11131
11132 virtual void SetUp() override {
11133 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011134 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011135 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
11136 }
11137
11138 virtual void TearDown() override {
11139 InputDispatcherTest::TearDown();
11140 mTouchWindow.clear();
11141 }
11142
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011143 sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode,
chaviw3277faf2021-05-19 16:45:23 -050011144 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011145 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011146 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011147 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011148 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011149 return window;
11150 }
11151
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011152 sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011153 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
11154 sp<FakeWindowHandle> window =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011155 sp<FakeWindowHandle>::make(app, mDispatcher, name, ui::LogicalDisplayId::DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011156 // Generate an arbitrary PID based on the UID
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011157 window->setOwnerInfo(gui::Pid{static_cast<pid_t>(1777 + (uid.val() % 10000))}, uid);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011158 return window;
11159 }
11160
11161 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000011162 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011163 AINPUT_SOURCE_TOUCHSCREEN,
11164 ui::LogicalDisplayId::DEFAULT, points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011165 }
11166};
11167
11168TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011169 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011170 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011171 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011172
11173 touch();
11174
11175 mTouchWindow->assertNoEvents();
11176}
11177
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011178TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +000011179 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
11180 const sp<FakeWindowHandle>& w =
11181 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011182 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000011183
11184 touch();
11185
11186 mTouchWindow->assertNoEvents();
11187}
11188
11189TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011190 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
11191 const sp<FakeWindowHandle>& w =
11192 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011193 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011194
11195 touch();
11196
11197 w->assertNoEvents();
11198}
11199
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011200TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011201 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011202 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011203
11204 touch();
11205
11206 mTouchWindow->consumeAnyMotionDown();
11207}
11208
11209TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011210 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011211 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011212 w->setFrame(Rect(0, 0, 50, 50));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011213 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011214
11215 touch({PointF{100, 100}});
11216
11217 mTouchWindow->consumeAnyMotionDown();
11218}
11219
11220TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011221 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011222 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011223 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011224
11225 touch();
11226
11227 mTouchWindow->consumeAnyMotionDown();
11228}
11229
11230TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
11231 const sp<FakeWindowHandle>& w =
11232 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011233 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011234
11235 touch();
11236
11237 mTouchWindow->consumeAnyMotionDown();
11238}
11239
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011240TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
11241 const sp<FakeWindowHandle>& w =
11242 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011243 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011244
11245 touch();
11246
11247 w->assertNoEvents();
11248}
11249
11250/**
11251 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
11252 * inside) while letting them pass-through. Note that even though touch passes through the occluding
11253 * window, the occluding window will still receive ACTION_OUTSIDE event.
11254 */
11255TEST_F(InputDispatcherUntrustedTouchesTest,
11256 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
11257 const sp<FakeWindowHandle>& w =
11258 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011259 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011260 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011261
11262 touch();
11263
11264 w->consumeMotionOutside();
11265}
11266
11267TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
11268 const sp<FakeWindowHandle>& w =
11269 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011270 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011271 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011272
11273 touch();
11274
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011275 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011276}
11277
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011278TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011279 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011280 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11281 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011282 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011283
11284 touch();
11285
11286 mTouchWindow->consumeAnyMotionDown();
11287}
11288
11289TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
11290 const sp<FakeWindowHandle>& w =
11291 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11292 MAXIMUM_OBSCURING_OPACITY);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011293 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011294
11295 touch();
11296
11297 mTouchWindow->consumeAnyMotionDown();
11298}
11299
11300TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011301 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011302 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11303 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011304 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011305
11306 touch();
11307
11308 mTouchWindow->assertNoEvents();
11309}
11310
11311TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
11312 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
11313 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011314 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
11315 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011316 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011317 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
11318 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011319 mDispatcher->onWindowInfosChanged(
11320 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011321
11322 touch();
11323
11324 mTouchWindow->assertNoEvents();
11325}
11326
11327TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
11328 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
11329 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011330 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
11331 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011332 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011333 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
11334 OPACITY_FAR_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011335 mDispatcher->onWindowInfosChanged(
11336 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011337
11338 touch();
11339
11340 mTouchWindow->consumeAnyMotionDown();
11341}
11342
11343TEST_F(InputDispatcherUntrustedTouchesTest,
11344 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
11345 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011346 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11347 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011348 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011349 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
11350 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011351 mDispatcher->onWindowInfosChanged(
11352 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011353
11354 touch();
11355
11356 mTouchWindow->consumeAnyMotionDown();
11357}
11358
11359TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
11360 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011361 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11362 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011363 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011364 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
11365 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011366 mDispatcher->onWindowInfosChanged(
11367 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011368
11369 touch();
11370
11371 mTouchWindow->assertNoEvents();
11372}
11373
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011374TEST_F(InputDispatcherUntrustedTouchesTest,
11375 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
11376 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011377 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
11378 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011379 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011380 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11381 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011382 mDispatcher->onWindowInfosChanged(
11383 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011384
11385 touch();
11386
11387 mTouchWindow->assertNoEvents();
11388}
11389
11390TEST_F(InputDispatcherUntrustedTouchesTest,
11391 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
11392 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011393 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
11394 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011395 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011396 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11397 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011398 mDispatcher->onWindowInfosChanged(
11399 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011400
11401 touch();
11402
11403 mTouchWindow->consumeAnyMotionDown();
11404}
11405
11406TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
11407 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011408 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
11409 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011410 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011411
11412 touch();
11413
11414 mTouchWindow->consumeAnyMotionDown();
11415}
11416
11417TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
11418 const sp<FakeWindowHandle>& w =
11419 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011420 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011421
11422 touch();
11423
11424 mTouchWindow->consumeAnyMotionDown();
11425}
11426
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000011427TEST_F(InputDispatcherUntrustedTouchesTest,
11428 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
11429 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
11430 const sp<FakeWindowHandle>& w =
11431 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011432 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000011433
11434 touch();
11435
11436 mTouchWindow->assertNoEvents();
11437}
11438
11439TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
11440 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
11441 const sp<FakeWindowHandle>& w =
11442 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011443 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000011444
11445 touch();
11446
11447 mTouchWindow->consumeAnyMotionDown();
11448}
11449
11450TEST_F(InputDispatcherUntrustedTouchesTest,
11451 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
11452 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
11453 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011454 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11455 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011456 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000011457
11458 touch();
11459
11460 mTouchWindow->consumeAnyMotionDown();
11461}
11462
11463TEST_F(InputDispatcherUntrustedTouchesTest,
11464 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
11465 const sp<FakeWindowHandle>& w1 =
11466 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
11467 OPACITY_BELOW_THRESHOLD);
11468 const sp<FakeWindowHandle>& w2 =
11469 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11470 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011471 mDispatcher->onWindowInfosChanged(
11472 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000011473
11474 touch();
11475
11476 mTouchWindow->assertNoEvents();
11477}
11478
11479/**
11480 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
11481 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
11482 * (which alone would result in allowing touches) does not affect the blocking behavior.
11483 */
11484TEST_F(InputDispatcherUntrustedTouchesTest,
11485 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
11486 const sp<FakeWindowHandle>& wB =
11487 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
11488 OPACITY_BELOW_THRESHOLD);
11489 const sp<FakeWindowHandle>& wC =
11490 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
11491 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011492 mDispatcher->onWindowInfosChanged(
11493 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000011494
11495 touch();
11496
11497 mTouchWindow->assertNoEvents();
11498}
11499
11500/**
11501 * This test is testing that a window from a different UID but with same application token doesn't
11502 * block the touch. Apps can share the application token for close UI collaboration for example.
11503 */
11504TEST_F(InputDispatcherUntrustedTouchesTest,
11505 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
11506 const sp<FakeWindowHandle>& w =
11507 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
11508 w->setApplicationToken(mTouchWindow->getApplicationToken());
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011509 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000011510
11511 touch();
11512
11513 mTouchWindow->consumeAnyMotionDown();
11514}
11515
arthurhungb89ccb02020-12-30 16:19:01 +080011516class InputDispatcherDragTests : public InputDispatcherTest {
11517protected:
11518 std::shared_ptr<FakeApplicationHandle> mApp;
11519 sp<FakeWindowHandle> mWindow;
11520 sp<FakeWindowHandle> mSecondWindow;
11521 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011522 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011523 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
11524 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +080011525
11526 void SetUp() override {
11527 InputDispatcherTest::SetUp();
11528 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011529 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow",
11530 ui::LogicalDisplayId::DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +080011531 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +080011532
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011533 mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2",
11534 ui::LogicalDisplayId::DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +080011535 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +080011536
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011537 mSpyWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow",
11538 ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011539 mSpyWindow->setSpy(true);
11540 mSpyWindow->setTrustedOverlay(true);
11541 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
11542
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011543 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011544 mDispatcher->onWindowInfosChanged(
11545 {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()},
11546 {},
11547 0,
11548 0});
arthurhungb89ccb02020-12-30 16:19:01 +080011549 }
11550
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011551 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
11552 switch (fromSource) {
11553 case AINPUT_SOURCE_TOUCHSCREEN:
11554 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011555 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011556 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011557 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11558 break;
11559 case AINPUT_SOURCE_STYLUS:
11560 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011561 injectMotionEvent(*mDispatcher,
11562 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
11563 AINPUT_SOURCE_STYLUS)
11564 .buttonState(
11565 AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
11566 .pointer(PointerBuilder(0, ToolType::STYLUS)
11567 .x(50)
11568 .y(50))
11569 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011570 break;
11571 case AINPUT_SOURCE_MOUSE:
11572 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011573 injectMotionEvent(*mDispatcher,
11574 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
11575 AINPUT_SOURCE_MOUSE)
11576 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
11577 .pointer(PointerBuilder(MOUSE_POINTER_ID,
11578 ToolType::MOUSE)
11579 .x(50)
11580 .y(50))
11581 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011582 break;
11583 default:
11584 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
11585 }
arthurhungb89ccb02020-12-30 16:19:01 +080011586
11587 // Window should receive motion event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011588 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011589 // Spy window should also receive motion event
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011590 mSpyWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000011591 }
11592
11593 // Start performing drag, we will create a drag window and transfer touch to it.
11594 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
11595 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011596 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +000011597 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011598 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +000011599 }
arthurhungb89ccb02020-12-30 16:19:01 +080011600
11601 // The drag window covers the entire display
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011602 mDragWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow",
11603 ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011604 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011605 mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(),
11606 *mWindow->getInfo(), *mSecondWindow->getInfo()},
11607 {},
11608 0,
11609 0});
arthurhungb89ccb02020-12-30 16:19:01 +080011610
11611 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +000011612 bool transferred =
Prabir Pradhan367f3432024-02-13 23:05:58 +000011613 mDispatcher->transferTouchGesture(mWindow->getToken(), mDragWindow->getToken(),
11614 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +000011615 if (transferred) {
11616 mWindow->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011617 mDragWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
11618 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung54745652022-04-20 07:17:41 +000011619 }
11620 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +080011621 }
11622};
11623
11624TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011625 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +080011626
11627 // Move on window.
11628 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011629 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011630 ui::LogicalDisplayId::DEFAULT, {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080011631 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011632 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11633 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080011634 mWindow->consumeDragEvent(false, 50, 50);
11635 mSecondWindow->assertNoEvents();
11636
11637 // Move to another window.
11638 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011639 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011640 ui::LogicalDisplayId::DEFAULT, {150, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080011641 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011642 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11643 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080011644 mWindow->consumeDragEvent(true, 150, 50);
11645 mSecondWindow->consumeDragEvent(false, 50, 50);
11646
11647 // Move back to original window.
11648 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011649 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011650 ui::LogicalDisplayId::DEFAULT, {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080011651 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011652 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11653 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080011654 mWindow->consumeDragEvent(false, 50, 50);
11655 mSecondWindow->consumeDragEvent(true, -50, 50);
11656
11657 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011658 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011659 {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080011660 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011661 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080011662 mWindow->assertNoEvents();
11663 mSecondWindow->assertNoEvents();
11664}
11665
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011666TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011667 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011668
11669 // No cancel event after drag start
11670 mSpyWindow->assertNoEvents();
11671
11672 const MotionEvent secondFingerDownEvent =
11673 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11674 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011675 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11676 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011677 .build();
11678 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011679 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011680 InputEventInjectionSync::WAIT_FOR_RESULT))
11681 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11682
11683 // Receives cancel for first pointer after next pointer down
11684 mSpyWindow->consumeMotionCancel();
11685 mSpyWindow->consumeMotionDown();
11686
11687 mSpyWindow->assertNoEvents();
11688}
11689
arthurhungf452d0b2021-01-06 00:19:52 +080011690TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011691 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +080011692
11693 // Move on window.
11694 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011695 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011696 ui::LogicalDisplayId::DEFAULT, {50, 50}))
arthurhungf452d0b2021-01-06 00:19:52 +080011697 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011698 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11699 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungf452d0b2021-01-06 00:19:52 +080011700 mWindow->consumeDragEvent(false, 50, 50);
11701 mSecondWindow->assertNoEvents();
11702
11703 // Move to another window.
11704 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011705 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011706 ui::LogicalDisplayId::DEFAULT, {150, 50}))
arthurhungf452d0b2021-01-06 00:19:52 +080011707 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011708 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11709 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungf452d0b2021-01-06 00:19:52 +080011710 mWindow->consumeDragEvent(true, 150, 50);
11711 mSecondWindow->consumeDragEvent(false, 50, 50);
11712
11713 // drop to another window.
11714 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011715 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
arthurhungf452d0b2021-01-06 00:19:52 +080011716 {150, 50}))
11717 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011718 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070011719 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhungf452d0b2021-01-06 00:19:52 +080011720 mWindow->assertNoEvents();
11721 mSecondWindow->assertNoEvents();
11722}
11723
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011724TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) {
11725 startDrag();
11726
11727 // No cancel event after drag start
11728 mSpyWindow->assertNoEvents();
11729
11730 const MotionEvent secondFingerDownEvent =
11731 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11732 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
11733 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11734 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
11735 .build();
11736 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
11737 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
11738 InputEventInjectionSync::WAIT_FOR_RESULT))
11739 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11740
11741 // Receives cancel for first pointer after next pointer down
11742 mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -080011743 mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1})));
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011744 mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
11745
11746 mSpyWindow->assertNoEvents();
11747
11748 // Spy window calls pilfer pointers
11749 EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken()));
11750 mDragWindow->assertNoEvents();
11751
11752 const MotionEvent firstFingerMoveEvent =
Siarhei Vishniakou96a15572023-12-18 10:47:52 -080011753 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011754 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
11755 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60))
11756 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
11757 .build();
11758 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou96a15572023-12-18 10:47:52 -080011759 injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011760 InputEventInjectionSync::WAIT_FOR_RESULT))
11761 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11762
11763 // Drag window should still receive the new event
Prabir Pradhan65455c72024-02-13 21:46:41 +000011764 mDragWindow->consumeMotionEvent(
11765 AllOf(WithMotionAction(ACTION_MOVE), WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)));
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011766 mDragWindow->assertNoEvents();
11767}
11768
arthurhung6d4bed92021-03-17 11:59:33 +080011769TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011770 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +080011771
11772 // Move on window and keep button pressed.
11773 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011774 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080011775 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
11776 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011777 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080011778 .build()))
11779 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011780 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11781 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhung6d4bed92021-03-17 11:59:33 +080011782 mWindow->consumeDragEvent(false, 50, 50);
11783 mSecondWindow->assertNoEvents();
11784
11785 // Move to another window and release button, expect to drop item.
11786 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011787 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080011788 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
11789 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011790 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080011791 .build()))
11792 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011793 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11794 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhung6d4bed92021-03-17 11:59:33 +080011795 mWindow->assertNoEvents();
11796 mSecondWindow->assertNoEvents();
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070011797 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhung6d4bed92021-03-17 11:59:33 +080011798
11799 // nothing to the window.
11800 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011801 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080011802 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
11803 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011804 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080011805 .build()))
11806 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011807 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhung6d4bed92021-03-17 11:59:33 +080011808 mWindow->assertNoEvents();
11809 mSecondWindow->assertNoEvents();
11810}
11811
Arthur Hung54745652022-04-20 07:17:41 +000011812TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011813 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +080011814
11815 // Set second window invisible.
11816 mSecondWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011817 mDispatcher->onWindowInfosChanged(
11818 {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Arthur Hung6d0571e2021-04-09 20:18:16 +080011819
11820 // Move on window.
11821 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011822 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011823 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hung6d0571e2021-04-09 20:18:16 +080011824 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011825 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11826 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung6d0571e2021-04-09 20:18:16 +080011827 mWindow->consumeDragEvent(false, 50, 50);
11828 mSecondWindow->assertNoEvents();
11829
11830 // Move to another window.
11831 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011832 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011833 ui::LogicalDisplayId::DEFAULT, {150, 50}))
Arthur Hung6d0571e2021-04-09 20:18:16 +080011834 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011835 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11836 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung6d0571e2021-04-09 20:18:16 +080011837 mWindow->consumeDragEvent(true, 150, 50);
11838 mSecondWindow->assertNoEvents();
11839
11840 // drop to another window.
11841 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011842 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Arthur Hung6d0571e2021-04-09 20:18:16 +080011843 {150, 50}))
11844 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011845 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070011846 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
Arthur Hung6d0571e2021-04-09 20:18:16 +080011847 mWindow->assertNoEvents();
11848 mSecondWindow->assertNoEvents();
11849}
11850
Arthur Hung54745652022-04-20 07:17:41 +000011851TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011852 // Ensure window could track pointerIds if it didn't support split touch.
11853 mWindow->setPreventSplitting(true);
11854
Arthur Hung54745652022-04-20 07:17:41 +000011855 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011856 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
11857 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hung54745652022-04-20 07:17:41 +000011858 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011859 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000011860
11861 const MotionEvent secondFingerDownEvent =
11862 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011863 .displayId(ui::LogicalDisplayId::DEFAULT)
Arthur Hung54745652022-04-20 07:17:41 +000011864 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011865 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11866 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000011867 .build();
11868 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011869 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000011870 InputEventInjectionSync::WAIT_FOR_RESULT))
11871 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000011872 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +000011873
11874 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011875 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +000011876}
11877
11878TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
11879 // First down on second window.
11880 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011881 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
11882 ui::LogicalDisplayId::DEFAULT, {150, 50}))
Arthur Hung54745652022-04-20 07:17:41 +000011883 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11884
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011885 mSecondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000011886
11887 // Second down on first window.
11888 const MotionEvent secondFingerDownEvent =
11889 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011890 .displayId(ui::LogicalDisplayId::DEFAULT)
Arthur Hung54745652022-04-20 07:17:41 +000011891 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011892 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
11893 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000011894 .build();
11895 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011896 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000011897 InputEventInjectionSync::WAIT_FOR_RESULT))
11898 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011899 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
11900 mSecondWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000011901
11902 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011903 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +000011904
11905 // Move on window.
11906 const MotionEvent secondFingerMoveEvent =
11907 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
11908 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011909 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
11910 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000011911 .build();
11912 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011913 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000011914 InputEventInjectionSync::WAIT_FOR_RESULT));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011915 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11916 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung54745652022-04-20 07:17:41 +000011917 mWindow->consumeDragEvent(false, 50, 50);
11918 mSecondWindow->consumeMotionMove();
11919
11920 // Release the drag pointer should perform drop.
11921 const MotionEvent secondFingerUpEvent =
11922 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
11923 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011924 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
11925 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000011926 .build();
11927 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011928 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000011929 InputEventInjectionSync::WAIT_FOR_RESULT));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011930 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070011931 mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken());
Arthur Hung54745652022-04-20 07:17:41 +000011932 mWindow->assertNoEvents();
11933 mSecondWindow->consumeMotionMove();
11934}
11935
Arthur Hung3915c1f2022-05-31 07:17:17 +000011936TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011937 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +000011938
11939 // Update window of second display.
11940 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011941 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011942 mDispatcher->onWindowInfosChanged(
11943 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
11944 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
11945 {},
11946 0,
11947 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +000011948
11949 // Let second display has a touch state.
11950 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011951 injectMotionEvent(*mDispatcher,
Arthur Hung3915c1f2022-05-31 07:17:17 +000011952 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
11953 AINPUT_SOURCE_TOUCHSCREEN)
11954 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011955 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +000011956 .build()));
Prabir Pradhan7662a8d2023-12-15 01:58:14 +000011957 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +000011958 // Update window again.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011959 mDispatcher->onWindowInfosChanged(
11960 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
11961 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
11962 {},
11963 0,
11964 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +000011965
11966 // Move on window.
11967 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011968 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011969 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hung3915c1f2022-05-31 07:17:17 +000011970 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011971 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11972 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung3915c1f2022-05-31 07:17:17 +000011973 mWindow->consumeDragEvent(false, 50, 50);
11974 mSecondWindow->assertNoEvents();
11975
11976 // Move to another window.
11977 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011978 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011979 ui::LogicalDisplayId::DEFAULT, {150, 50}))
Arthur Hung3915c1f2022-05-31 07:17:17 +000011980 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011981 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11982 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung3915c1f2022-05-31 07:17:17 +000011983 mWindow->consumeDragEvent(true, 150, 50);
11984 mSecondWindow->consumeDragEvent(false, 50, 50);
11985
11986 // drop to another window.
11987 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011988 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Arthur Hung3915c1f2022-05-31 07:17:17 +000011989 {150, 50}))
11990 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011991 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070011992 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hung3915c1f2022-05-31 07:17:17 +000011993 mWindow->assertNoEvents();
11994 mSecondWindow->assertNoEvents();
11995}
11996
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011997TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
11998 startDrag(true, AINPUT_SOURCE_MOUSE);
11999 // Move on window.
12000 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012001 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012002 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
12003 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012004 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012005 .x(50)
12006 .y(50))
12007 .build()))
12008 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012009 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
12010 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012011 mWindow->consumeDragEvent(false, 50, 50);
12012 mSecondWindow->assertNoEvents();
12013
12014 // Move to another window.
12015 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012016 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012017 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
12018 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012019 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012020 .x(150)
12021 .y(50))
12022 .build()))
12023 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012024 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
12025 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012026 mWindow->consumeDragEvent(true, 150, 50);
12027 mSecondWindow->consumeDragEvent(false, 50, 50);
12028
12029 // drop to another window.
12030 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012031 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012032 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
12033 .buttonState(0)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012034 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012035 .x(150)
12036 .y(50))
12037 .build()))
12038 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012039 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070012040 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012041 mWindow->assertNoEvents();
12042 mSecondWindow->assertNoEvents();
12043}
12044
Linnan Li5af92f92023-07-14 14:36:22 +080012045/**
12046 * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag
12047 * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash.
12048 */
12049TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) {
12050 // Down on second window
12051 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012052 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12053 ui::LogicalDisplayId::DEFAULT, {150, 50}))
Linnan Li5af92f92023-07-14 14:36:22 +080012054 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12055
12056 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown());
12057 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown());
12058
12059 // Down on first window
12060 const MotionEvent secondFingerDownEvent =
12061 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012062 .displayId(ui::LogicalDisplayId::DEFAULT)
Linnan Li5af92f92023-07-14 14:36:22 +080012063 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
12064 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
12065 .build();
12066 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
12067 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
12068 InputEventInjectionSync::WAIT_FOR_RESULT))
12069 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12070 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
12071 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove());
12072 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1));
12073
12074 // Start drag on first window
12075 ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN));
12076
12077 // Trigger cancel
12078 mDispatcher->cancelCurrentTouch();
12079 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012080 ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan65455c72024-02-13 21:46:41 +000012081 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE));
Linnan Li5af92f92023-07-14 14:36:22 +080012082 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel());
12083
12084 ASSERT_TRUE(mDispatcher->waitForIdle());
12085 // The D&D finished with nullptr
12086 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
12087
12088 // Remove drag window
12089 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
12090
12091 // Inject a simple gesture, ensure dispatcher not crashed
12092 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012093 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12094 ui::LogicalDisplayId::DEFAULT, PointF{50, 50}))
Linnan Li5af92f92023-07-14 14:36:22 +080012095 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12096 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
12097
12098 const MotionEvent moveEvent =
12099 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012100 .displayId(ui::LogicalDisplayId::DEFAULT)
Linnan Li5af92f92023-07-14 14:36:22 +080012101 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12102 .build();
12103 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
12104 injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT,
12105 InputEventInjectionSync::WAIT_FOR_RESULT))
12106 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12107 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove());
12108
12109 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012110 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Linnan Li5af92f92023-07-14 14:36:22 +080012111 {50, 50}))
12112 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12113 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp());
12114}
12115
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000012116TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) {
12117 // Start hovering over the window.
12118 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
12119 injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012120 ui::LogicalDisplayId::DEFAULT, {50, 50}));
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000012121
12122 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)));
12123 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)));
12124
12125 ASSERT_FALSE(startDrag(/*sendDown=*/false))
12126 << "Drag and drop should not work with a hovering pointer";
12127}
12128
Vishnu Nair062a8672021-09-03 16:07:44 -070012129class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
12130
12131TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
12132 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012133 sp<FakeWindowHandle> window =
12134 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
12135 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012136 window->setDropInput(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012137 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair062a8672021-09-03 16:07:44 -070012138 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012139 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012140 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000012141 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070012142
12143 // With the flag set, window should not get any input
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012144 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012145 window->assertNoEvents();
12146
Prabir Pradhan678438e2023-04-13 19:32:51 +000012147 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012148 AINPUT_SOURCE_TOUCHSCREEN,
12149 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070012150 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012151 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -080012152 mDispatcher->waitForIdle();
Vishnu Nair062a8672021-09-03 16:07:44 -070012153 window->assertNoEvents();
12154
12155 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012156 window->setDropInput(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012157 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012158
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012159 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT));
12160 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012161
Prabir Pradhan678438e2023-04-13 19:32:51 +000012162 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012163 AINPUT_SOURCE_TOUCHSCREEN,
12164 ui::LogicalDisplayId::DEFAULT));
12165 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012166 window->assertNoEvents();
12167}
12168
12169TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
12170 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
12171 std::make_shared<FakeApplicationHandle>();
12172 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070012173 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012174 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012175 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012176 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012177 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070012178 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012179 sp<FakeWindowHandle> window =
12180 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
12181 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012182 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012183 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012184 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair062a8672021-09-03 16:07:44 -070012185 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012186 mDispatcher->onWindowInfosChanged(
12187 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012188 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000012189 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070012190
12191 // With the flag set, window should not get any input
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012192 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012193 window->assertNoEvents();
12194
Prabir Pradhan678438e2023-04-13 19:32:51 +000012195 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012196 AINPUT_SOURCE_TOUCHSCREEN,
12197 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070012198 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012199 ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012200 window->assertNoEvents();
12201
12202 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012203 window->setDropInputIfObscured(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012204 mDispatcher->onWindowInfosChanged(
12205 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012206
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012207 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT));
12208 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012209
Prabir Pradhan678438e2023-04-13 19:32:51 +000012210 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012211 AINPUT_SOURCE_TOUCHSCREEN,
12212 ui::LogicalDisplayId::DEFAULT));
12213 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
12214 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
Vishnu Nair062a8672021-09-03 16:07:44 -070012215 window->assertNoEvents();
12216}
12217
12218TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
12219 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
12220 std::make_shared<FakeApplicationHandle>();
12221 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070012222 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012223 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012224 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012225 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012226 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070012227 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012228 sp<FakeWindowHandle> window =
12229 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
12230 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012231 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012232 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012233 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair062a8672021-09-03 16:07:44 -070012234 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012235 mDispatcher->onWindowInfosChanged(
12236 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012237 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000012238 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070012239
12240 // With the flag set, window should not get any input
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012241 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012242 window->assertNoEvents();
12243
Prabir Pradhan678438e2023-04-13 19:32:51 +000012244 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012245 AINPUT_SOURCE_TOUCHSCREEN,
12246 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070012247 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012248 ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012249 window->assertNoEvents();
12250
12251 // When the window is no longer obscured because it went on top, it should get input
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012252 mDispatcher->onWindowInfosChanged(
12253 {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012254
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012255 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT));
12256 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012257
Prabir Pradhan678438e2023-04-13 19:32:51 +000012258 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012259 AINPUT_SOURCE_TOUCHSCREEN,
12260 ui::LogicalDisplayId::DEFAULT));
12261 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012262 window->assertNoEvents();
12263}
12264
Antonio Kantekf16f2832021-09-28 04:39:20 +000012265class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
12266protected:
12267 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +000012268 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +000012269 sp<FakeWindowHandle> mWindow;
12270 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +000012271 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +000012272
12273 void SetUp() override {
12274 InputDispatcherTest::SetUp();
12275
12276 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +000012277 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012278 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow",
12279 ui::LogicalDisplayId::DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012280 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000012281 setFocusedWindow(mWindow);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012282 mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2",
12283 ui::LogicalDisplayId::DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012284 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +000012285 mThirdWindow =
12286 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
12287 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
12288 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012289
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012290 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012291 mDispatcher->onWindowInfosChanged(
12292 {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()},
12293 {},
12294 0,
12295 0});
Antonio Kantek15beb512022-06-13 22:35:41 +000012296 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012297 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000012298
Antonio Kantek15beb512022-06-13 22:35:41 +000012299 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +000012300 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012301 WINDOW_UID, /*hasPermission=*/true,
12302 ui::LogicalDisplayId::DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -070012303 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
12304 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000012305 mThirdWindow->assertNoEvents();
12306 }
12307
12308 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
12309 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +000012310 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +000012311 SECOND_DISPLAY_ID)) {
12312 mWindow->assertNoEvents();
12313 mSecondWindow->assertNoEvents();
12314 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -070012315 }
Antonio Kantekf16f2832021-09-28 04:39:20 +000012316 }
12317
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012318 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Antonio Kantek15beb512022-06-13 22:35:41 +000012319 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -070012320 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012321 ui::LogicalDisplayId::DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000012322 mWindow->consumeTouchModeEvent(inTouchMode);
12323 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000012324 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +000012325 }
12326};
12327
Antonio Kantek26defcf2022-02-08 01:12:27 +000012328TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080012329 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +000012330 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
12331 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000012332 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012333}
12334
Antonio Kantek26defcf2022-02-08 01:12:27 +000012335TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
12336 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012337 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000012338 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012339 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek26defcf2022-02-08 01:12:27 +000012340 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000012341 ownerUid, /*hasPermission=*/false,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012342 ui::LogicalDisplayId::DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +000012343 mWindow->assertNoEvents();
12344 mSecondWindow->assertNoEvents();
12345}
12346
12347TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
12348 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012349 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000012350 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012351 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek15beb512022-06-13 22:35:41 +000012352 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000012353 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000012354}
12355
Antonio Kantekf16f2832021-09-28 04:39:20 +000012356TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080012357 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +000012358 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
12359 windowInfo.ownerPid, windowInfo.ownerUid,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012360 /*hasPermission=*/true,
12361 ui::LogicalDisplayId::DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000012362 mWindow->assertNoEvents();
12363 mSecondWindow->assertNoEvents();
12364}
12365
Antonio Kantek15beb512022-06-13 22:35:41 +000012366TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
12367 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
12368 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
12369 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000012370 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +000012371 mWindow->assertNoEvents();
12372 mSecondWindow->assertNoEvents();
12373 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
12374}
12375
Antonio Kantek48710e42022-03-24 14:19:30 -070012376TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
12377 // Interact with the window first.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012378 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012379 injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT))
Antonio Kantek48710e42022-03-24 14:19:30 -070012380 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012381 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Antonio Kantek48710e42022-03-24 14:19:30 -070012382
12383 // Then remove focus.
12384 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012385 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Antonio Kantek48710e42022-03-24 14:19:30 -070012386
12387 // Assert that caller can switch touch mode by owning one of the last interacted window.
12388 const WindowInfo& windowInfo = *mWindow->getInfo();
12389 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
12390 windowInfo.ownerPid, windowInfo.ownerUid,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012391 /*hasPermission=*/false,
12392 ui::LogicalDisplayId::DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -070012393}
12394
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012395class InputDispatcherSpyWindowTest : public InputDispatcherTest {
12396public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012397 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012398 std::shared_ptr<FakeApplicationHandle> application =
12399 std::make_shared<FakeApplicationHandle>();
12400 std::string name = "Fake Spy ";
12401 name += std::to_string(mSpyCount++);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012402 sp<FakeWindowHandle> spy =
12403 sp<FakeWindowHandle>::make(application, mDispatcher, name.c_str(),
12404 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012405 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -080012406 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012407 return spy;
12408 }
12409
12410 sp<FakeWindowHandle> createForeground() {
12411 std::shared_ptr<FakeApplicationHandle> application =
12412 std::make_shared<FakeApplicationHandle>();
12413 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070012414 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012415 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012416 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012417 return window;
12418 }
12419
12420private:
12421 int mSpyCount{0};
12422};
12423
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080012424using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012425/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -080012426 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
12427 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080012428TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
Siarhei Vishniakou21f77bd2023-07-18 17:51:35 -070012429 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080012430 ScopedSilentDeath _silentDeath;
12431
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012432 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -080012433 spy->setTrustedOverlay(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012434 ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}),
Prabir Pradhan5c85e052021-12-22 02:27:12 -080012435 ".* not a trusted overlay");
12436}
12437
12438/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012439 * Input injection into a display with a spy window but no foreground windows should succeed.
12440 */
12441TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012442 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012443 mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012444
12445 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012446 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12447 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012448 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012449 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012450}
12451
12452/**
12453 * Verify the order in which different input windows receive events. The touched foreground window
12454 * (if there is one) should always receive the event first. When there are multiple spy windows, the
12455 * spy windows will receive the event according to their Z-order, where the top-most spy window will
12456 * receive events before ones belows it.
12457 *
12458 * Here, we set up a scenario with four windows in the following Z order from the top:
12459 * spy1, spy2, window, spy3.
12460 * We then inject an event and verify that the foreground "window" receives it first, followed by
12461 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
12462 * window.
12463 */
12464TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
12465 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012466 auto spy1 = createSpy();
12467 auto spy2 = createSpy();
12468 auto spy3 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012469 mDispatcher->onWindowInfosChanged(
12470 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012471 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
12472 const size_t numChannels = channels.size();
12473
Michael Wright8e9a8562022-02-09 13:44:29 +000012474 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012475 if (!epollFd.ok()) {
12476 FAIL() << "Failed to create epoll fd";
12477 }
12478
12479 for (size_t i = 0; i < numChannels; i++) {
12480 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
12481 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
12482 FAIL() << "Failed to add fd to epoll";
12483 }
12484 }
12485
12486 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012487 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12488 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012489 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12490
12491 std::vector<size_t> eventOrder;
12492 std::vector<struct epoll_event> events(numChannels);
12493 for (;;) {
12494 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
12495 (100ms).count());
12496 if (nFds < 0) {
12497 FAIL() << "Failed to call epoll_wait";
12498 }
12499 if (nFds == 0) {
12500 break; // epoll_wait timed out
12501 }
12502 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -070012503 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -070012504 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012505 channels[i]->consumeMotionDown();
12506 }
12507 }
12508
12509 // Verify the order in which the events were received.
12510 EXPECT_EQ(3u, eventOrder.size());
12511 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
12512 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
12513 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
12514}
12515
12516/**
12517 * A spy window using the NOT_TOUCHABLE flag does not receive events.
12518 */
12519TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
12520 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012521 auto spy = createSpy();
12522 spy->setTouchable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012523 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012524
12525 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012526 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12527 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012528 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012529 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012530 spy->assertNoEvents();
12531}
12532
12533/**
12534 * A spy window will only receive gestures that originate within its touchable region. Gestures that
12535 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
12536 * to the window.
12537 */
12538TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
12539 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012540 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012541 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012542 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012543
12544 // Inject an event outside the spy window's touchable region.
12545 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012546 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12547 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012548 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12549 window->consumeMotionDown();
12550 spy->assertNoEvents();
12551 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012552 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12553 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012554 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12555 window->consumeMotionUp();
12556 spy->assertNoEvents();
12557
12558 // Inject an event inside the spy window's touchable region.
12559 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012560 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12561 ui::LogicalDisplayId::DEFAULT, {5, 10}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012562 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12563 window->consumeMotionDown();
12564 spy->consumeMotionDown();
12565}
12566
12567/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012568 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080012569 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012570 */
12571TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
12572 auto window = createForeground();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012573 window->setOwnerInfo(gui::Pid{12}, gui::Uid{34});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012574 auto spy = createSpy();
12575 spy->setWatchOutsideTouch(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012576 spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012577 spy->setFrame(Rect{0, 0, 20, 20});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012578 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012579
12580 // Inject an event outside the spy window's frame and touchable region.
12581 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012582 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12583 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012584 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12585 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080012586 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012587}
12588
12589/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012590 * Even when a spy window spans over multiple foreground windows, the spy should receive all
12591 * pointers that are down within its bounds.
12592 */
12593TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
12594 auto windowLeft = createForeground();
12595 windowLeft->setFrame({0, 0, 100, 200});
12596 auto windowRight = createForeground();
12597 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012598 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012599 spy->setFrame({0, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012600 mDispatcher->onWindowInfosChanged(
12601 {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012602
12603 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012604 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12605 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012606 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12607 windowLeft->consumeMotionDown();
12608 spy->consumeMotionDown();
12609
12610 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080012611 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012612 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012613 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12614 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012615 .build();
12616 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012617 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012618 InputEventInjectionSync::WAIT_FOR_RESULT))
12619 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12620 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +000012621 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012622}
12623
12624/**
12625 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
12626 * the spy should receive the second pointer with ACTION_DOWN.
12627 */
12628TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
12629 auto window = createForeground();
12630 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012631 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012632 spyRight->setFrame({100, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012633 mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012634
12635 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012636 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12637 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012638 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12639 window->consumeMotionDown();
12640 spyRight->assertNoEvents();
12641
12642 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080012643 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012644 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012645 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12646 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012647 .build();
12648 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012649 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012650 InputEventInjectionSync::WAIT_FOR_RESULT))
12651 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000012652 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012653 spyRight->consumeMotionDown();
12654}
12655
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012656/**
12657 * The spy window should not be able to affect whether or not touches are split. Only the foreground
12658 * windows should be allowed to control split touch.
12659 */
12660TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080012661 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012662 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012663 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080012664 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012665
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012666 auto window = createForeground();
12667 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012668
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012669 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012670
12671 // First finger down, no window touched.
12672 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012673 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12674 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012675 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012676 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012677 window->assertNoEvents();
12678
12679 // Second finger down on window, the window should receive touch down.
12680 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080012681 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012682 .displayId(ui::LogicalDisplayId::DEFAULT)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012683 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012684 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
12685 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012686 .build();
12687 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012688 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012689 InputEventInjectionSync::WAIT_FOR_RESULT))
12690 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12691
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012692 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +000012693 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012694}
12695
12696/**
12697 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
12698 * do not receive key events.
12699 */
12700TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012701 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012702 spy->setFocusable(false);
12703
12704 auto window = createForeground();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012705 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012706 setFocusedWindow(window);
12707 window->consumeFocusEvent(true);
12708
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012709 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012710 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012711 window->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012712
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012713 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012714 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012715 window->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012716
12717 spy->assertNoEvents();
12718}
12719
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012720using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
12721
12722/**
12723 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
12724 * are currently sent to any other windows - including other spy windows - will also be cancelled.
12725 */
12726TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
12727 auto window = createForeground();
12728 auto spy1 = createSpy();
12729 auto spy2 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012730 mDispatcher->onWindowInfosChanged(
12731 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012732
12733 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012734 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12735 ui::LogicalDisplayId::DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012736 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12737 window->consumeMotionDown();
12738 spy1->consumeMotionDown();
12739 spy2->consumeMotionDown();
12740
12741 // Pilfer pointers from the second spy window.
12742 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
12743 spy2->assertNoEvents();
12744 spy1->consumeMotionCancel();
12745 window->consumeMotionCancel();
12746
12747 // The rest of the gesture should only be sent to the second spy window.
12748 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012749 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012750 ui::LogicalDisplayId::DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012751 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12752 spy2->consumeMotionMove();
12753 spy1->assertNoEvents();
12754 window->assertNoEvents();
12755}
12756
12757/**
12758 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
12759 * in the middle of the gesture.
12760 */
12761TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
12762 auto window = createForeground();
12763 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012764 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012765
12766 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012767 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12768 ui::LogicalDisplayId::DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012769 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012770 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
12771 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012772
12773 window->releaseChannel();
12774
12775 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
12776
12777 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012778 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12779 ui::LogicalDisplayId::DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012780 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012781 spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012782}
12783
12784/**
12785 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
12786 * the spy, but not to any other windows.
12787 */
12788TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
12789 auto spy = createSpy();
12790 auto window = createForeground();
12791
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012792 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012793
12794 // First finger down on the window and the spy.
12795 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012796 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12797 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012798 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12799 spy->consumeMotionDown();
12800 window->consumeMotionDown();
12801
12802 // Spy window pilfers the pointers.
12803 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
12804 window->consumeMotionCancel();
12805
12806 // Second finger down on the window and spy, but the window should not receive the pointer down.
12807 const MotionEvent secondFingerDownEvent =
12808 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012809 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012810 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012811 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
12812 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012813 .build();
12814 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012815 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012816 InputEventInjectionSync::WAIT_FOR_RESULT))
12817 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12818
Harry Cutts33476232023-01-30 19:57:29 +000012819 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012820
12821 // Third finger goes down outside all windows, so injection should fail.
12822 const MotionEvent thirdFingerDownEvent =
12823 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012824 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012825 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012826 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
12827 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
12828 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012829 .build();
12830 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012831 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012832 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080012833 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012834
12835 spy->assertNoEvents();
12836 window->assertNoEvents();
12837}
12838
12839/**
12840 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
12841 */
12842TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
12843 auto spy = createSpy();
12844 spy->setFrame(Rect(0, 0, 100, 100));
12845 auto window = createForeground();
12846 window->setFrame(Rect(0, 0, 200, 200));
12847
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012848 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012849
12850 // First finger down on the window only
12851 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012852 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12853 ui::LogicalDisplayId::DEFAULT, {150, 150}))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012854 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12855 window->consumeMotionDown();
12856
12857 // Second finger down on the spy and window
12858 const MotionEvent secondFingerDownEvent =
12859 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012860 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012861 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012862 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
12863 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012864 .build();
12865 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012866 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012867 InputEventInjectionSync::WAIT_FOR_RESULT))
12868 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12869 spy->consumeMotionDown();
12870 window->consumeMotionPointerDown(1);
12871
12872 // Third finger down on the spy and window
12873 const MotionEvent thirdFingerDownEvent =
12874 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012875 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012876 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012877 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
12878 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
12879 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012880 .build();
12881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012882 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012883 InputEventInjectionSync::WAIT_FOR_RESULT))
12884 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12885 spy->consumeMotionPointerDown(1);
12886 window->consumeMotionPointerDown(2);
12887
12888 // Spy window pilfers the pointers.
12889 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Prabir Pradhan33cfc6d2024-06-11 20:17:44 +000012890 window->consumeMotionPointerUp(/*pointerIdx=*/2,
12891 AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT),
12892 WithFlags(AMOTION_EVENT_FLAG_CANCELED),
12893 WithPointerCount(3)));
12894 window->consumeMotionPointerUp(/*pointerIdx=*/1,
12895 AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT),
12896 WithFlags(AMOTION_EVENT_FLAG_CANCELED),
12897 WithPointerCount(2)));
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012898
12899 spy->assertNoEvents();
12900 window->assertNoEvents();
12901}
12902
12903/**
12904 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
12905 * other windows should be canceled. If this results in the cancellation of all pointers for some
12906 * window, then that window should receive ACTION_CANCEL.
12907 */
12908TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
12909 auto spy = createSpy();
12910 spy->setFrame(Rect(0, 0, 100, 100));
12911 auto window = createForeground();
12912 window->setFrame(Rect(0, 0, 200, 200));
12913
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012914 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012915
12916 // First finger down on both spy and window
12917 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012918 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12919 ui::LogicalDisplayId::DEFAULT, {10, 10}))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012920 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12921 window->consumeMotionDown();
12922 spy->consumeMotionDown();
12923
12924 // Second finger down on the spy and window
12925 const MotionEvent secondFingerDownEvent =
12926 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012927 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012928 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012929 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
12930 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012931 .build();
12932 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012933 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012934 InputEventInjectionSync::WAIT_FOR_RESULT))
12935 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12936 spy->consumeMotionPointerDown(1);
12937 window->consumeMotionPointerDown(1);
12938
12939 // Spy window pilfers the pointers.
12940 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
12941 window->consumeMotionCancel();
12942
12943 spy->assertNoEvents();
12944 window->assertNoEvents();
12945}
12946
12947/**
12948 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
12949 * be sent to other windows
12950 */
12951TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
12952 auto spy = createSpy();
12953 spy->setFrame(Rect(0, 0, 100, 100));
12954 auto window = createForeground();
12955 window->setFrame(Rect(0, 0, 200, 200));
12956
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012957 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012958
12959 // First finger down on both window and spy
12960 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012961 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12962 ui::LogicalDisplayId::DEFAULT, {10, 10}))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012963 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12964 window->consumeMotionDown();
12965 spy->consumeMotionDown();
12966
12967 // Spy window pilfers the pointers.
12968 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
12969 window->consumeMotionCancel();
12970
12971 // Second finger down on the window only
12972 const MotionEvent secondFingerDownEvent =
12973 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012974 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012975 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012976 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
12977 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012978 .build();
12979 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012980 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012981 InputEventInjectionSync::WAIT_FOR_RESULT))
12982 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12983 window->consumeMotionDown();
12984 window->assertNoEvents();
12985
12986 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
12987 spy->consumeMotionMove();
12988 spy->assertNoEvents();
12989}
12990
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070012991/**
12992 * A window on the left and a window on the right. Also, a spy window that's above all of the
12993 * windows, and spanning both left and right windows.
12994 * Send simultaneous motion streams from two different devices, one to the left window, and another
12995 * to the right window.
12996 * Pilfer from spy window.
12997 * Check that the pilfering only affects the pointers that are actually being received by the spy.
12998 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070012999TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer_legacy) {
13000 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070013001 sp<FakeWindowHandle> spy = createSpy();
13002 spy->setFrame(Rect(0, 0, 200, 200));
13003 sp<FakeWindowHandle> leftWindow = createForeground();
13004 leftWindow->setFrame(Rect(0, 0, 100, 100));
13005
13006 sp<FakeWindowHandle> rightWindow = createForeground();
13007 rightWindow->setFrame(Rect(100, 0, 200, 100));
13008
13009 constexpr int32_t stylusDeviceId = 1;
13010 constexpr int32_t touchDeviceId = 2;
13011
13012 mDispatcher->onWindowInfosChanged(
13013 {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
13014
13015 // Stylus down on left window and spy
13016 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
13017 .deviceId(stylusDeviceId)
13018 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
13019 .build());
13020 leftWindow->consumeMotionEvent(
13021 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
13022 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
13023
13024 // Finger down on right window and spy - but spy already has stylus
13025 mDispatcher->notifyMotion(
13026 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
13027 .deviceId(touchDeviceId)
13028 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
13029 .build());
13030 rightWindow->consumeMotionEvent(
13031 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070013032 spy->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070013033
13034 // Act: pilfer from spy. Spy is currently receiving touch events.
13035 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070013036 leftWindow->consumeMotionEvent(
13037 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070013038 rightWindow->consumeMotionEvent(
13039 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
13040
13041 // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus
13042 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
13043 .deviceId(stylusDeviceId)
13044 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52))
13045 .build());
13046 mDispatcher->notifyMotion(
13047 MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
13048 .deviceId(touchDeviceId)
13049 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52))
13050 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070013051 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070013052
13053 spy->assertNoEvents();
13054 leftWindow->assertNoEvents();
13055 rightWindow->assertNoEvents();
13056}
13057
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013058/**
13059 * A window on the left and a window on the right. Also, a spy window that's above all of the
13060 * windows, and spanning both left and right windows.
13061 * Send simultaneous motion streams from two different devices, one to the left window, and another
13062 * to the right window.
13063 * Pilfer from spy window.
13064 * Check that the pilfering affects all of the pointers that are actually being received by the spy.
13065 * The spy should receive both the touch and the stylus events after pilfer.
13066 */
13067TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) {
13068 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
13069 sp<FakeWindowHandle> spy = createSpy();
13070 spy->setFrame(Rect(0, 0, 200, 200));
13071 sp<FakeWindowHandle> leftWindow = createForeground();
13072 leftWindow->setFrame(Rect(0, 0, 100, 100));
13073
13074 sp<FakeWindowHandle> rightWindow = createForeground();
13075 rightWindow->setFrame(Rect(100, 0, 200, 100));
13076
13077 constexpr int32_t stylusDeviceId = 1;
13078 constexpr int32_t touchDeviceId = 2;
13079
13080 mDispatcher->onWindowInfosChanged(
13081 {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
13082
13083 // Stylus down on left window and spy
13084 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
13085 .deviceId(stylusDeviceId)
13086 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
13087 .build());
13088 leftWindow->consumeMotionEvent(
13089 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
13090 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
13091
13092 // Finger down on right window and spy
13093 mDispatcher->notifyMotion(
13094 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
13095 .deviceId(touchDeviceId)
13096 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
13097 .build());
13098 rightWindow->consumeMotionEvent(
13099 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
13100 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
13101
13102 // Act: pilfer from spy. Spy is currently receiving touch events.
13103 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
13104 leftWindow->consumeMotionEvent(
13105 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId)));
13106 rightWindow->consumeMotionEvent(
13107 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
13108
13109 // Continue movements from both stylus and touch. Touch and stylus will be delivered to spy
Siarhei Vishniakou92bca1c2024-04-01 14:06:59 -070013110 // Instead of sending the two MOVE events for each input device together, and then receiving
13111 // them both, process them one at at time. InputConsumer is always in the batching mode, which
13112 // means that the two MOVE events will be initially put into a batch. Once the events are
13113 // batched, the 'consume' call may result in any of the MOVE events to be sent first (depending
13114 // on the implementation of InputConsumer), which would mean that the order of the received
13115 // events could be different depending on whether there are 1 or 2 events pending in the
13116 // InputChannel at the time the test calls 'consume'. To make assertions simpler here, and to
13117 // avoid this confusing behaviour, send and receive each MOVE event separately.
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013118 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
13119 .deviceId(stylusDeviceId)
13120 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52))
13121 .build());
Siarhei Vishniakou92bca1c2024-04-01 14:06:59 -070013122 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013123 mDispatcher->notifyMotion(
13124 MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
13125 .deviceId(touchDeviceId)
13126 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52))
13127 .build());
Siarhei Vishniakou92bca1c2024-04-01 14:06:59 -070013128 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013129
13130 spy->assertNoEvents();
13131 leftWindow->assertNoEvents();
13132 rightWindow->assertNoEvents();
13133}
13134
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000013135TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) {
13136 auto window = createForeground();
13137 auto spy = createSpy();
13138 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
13139
13140 mDispatcher->notifyMotion(
13141 MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
13142 .deviceId(1)
13143 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200))
13144 .build());
13145 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13146 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13147
13148 // Pilfer pointers from the spy window should fail.
13149 EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken()));
13150 spy->assertNoEvents();
13151 window->assertNoEvents();
13152}
13153
Prabir Pradhand65552b2021-10-07 11:23:50 -070013154class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
13155public:
13156 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
13157 std::shared_ptr<FakeApplicationHandle> overlayApplication =
13158 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013159 sp<FakeWindowHandle> overlay = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
13160 "Stylus interceptor window",
13161 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013162 overlay->setFocusable(false);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013163 overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080013164 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080013165 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013166 overlay->setTrustedOverlay(true);
13167
13168 std::shared_ptr<FakeApplicationHandle> application =
13169 std::make_shared<FakeApplicationHandle>();
13170 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070013171 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013172 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013173 window->setFocusable(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013174 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Prabir Pradhand65552b2021-10-07 11:23:50 -070013175
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013176 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013177 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070013178 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000013179 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013180 return {std::move(overlay), std::move(window)};
13181 }
13182
13183 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000013184 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -070013185 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013186 ui::LogicalDisplayId::DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -070013187 }
13188
13189 void sendStylusEvent(int32_t action) {
13190 NotifyMotionArgs motionArgs =
13191 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013192 ui::LogicalDisplayId::DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070013193 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +000013194 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013195 }
13196};
13197
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080013198using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
13199
13200TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
Siarhei Vishniakouad3b6822023-06-22 14:17:35 -070013201 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080013202 ScopedSilentDeath _silentDeath;
13203
Prabir Pradhand65552b2021-10-07 11:23:50 -070013204 auto [overlay, window] = setupStylusOverlayScenario();
13205 overlay->setTrustedOverlay(false);
13206 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013207 ASSERT_DEATH(mDispatcher->onWindowInfosChanged(
13208 {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}),
Prabir Pradhand65552b2021-10-07 11:23:50 -070013209 ".* not a trusted overlay");
13210}
13211
13212TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
13213 auto [overlay, window] = setupStylusOverlayScenario();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013214 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070013215
13216 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
13217 overlay->consumeMotionDown();
13218 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
13219 overlay->consumeMotionUp();
13220
13221 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
13222 window->consumeMotionDown();
13223 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
13224 window->consumeMotionUp();
13225
13226 overlay->assertNoEvents();
13227 window->assertNoEvents();
13228}
13229
13230TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
13231 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -080013232 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013233 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070013234
13235 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
13236 overlay->consumeMotionDown();
13237 window->consumeMotionDown();
13238 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
13239 overlay->consumeMotionUp();
13240 window->consumeMotionUp();
13241
13242 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
13243 window->consumeMotionDown();
13244 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
13245 window->consumeMotionUp();
13246
13247 overlay->assertNoEvents();
13248 window->assertNoEvents();
13249}
13250
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000013251/**
13252 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
13253 * The scenario is as follows:
13254 * - The stylus interceptor overlay is configured as a spy window.
13255 * - The stylus interceptor spy receives the start of a new stylus gesture.
13256 * - It pilfers pointers and then configures itself to no longer be a spy.
13257 * - The stylus interceptor continues to receive the rest of the gesture.
13258 */
13259TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
13260 auto [overlay, window] = setupStylusOverlayScenario();
13261 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013262 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000013263
13264 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
13265 overlay->consumeMotionDown();
13266 window->consumeMotionDown();
13267
13268 // The interceptor pilfers the pointers.
13269 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
13270 window->consumeMotionCancel();
13271
13272 // The interceptor configures itself so that it is no longer a spy.
13273 overlay->setSpy(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013274 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000013275
13276 // It continues to receive the rest of the stylus gesture.
13277 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
13278 overlay->consumeMotionMove();
13279 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
13280 overlay->consumeMotionUp();
13281
13282 window->assertNoEvents();
13283}
13284
Prabir Pradhan5735a322022-04-11 17:23:34 +000013285struct User {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013286 gui::Pid mPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000013287 gui::Uid mUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +000013288 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
13289 std::unique_ptr<InputDispatcher>& mDispatcher;
13290
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013291 User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid)
Prabir Pradhan5735a322022-04-11 17:23:34 +000013292 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
13293
13294 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070013295 return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013296 ui::LogicalDisplayId::DEFAULT, {100, 200},
Prabir Pradhan5735a322022-04-11 17:23:34 +000013297 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
13298 AMOTION_EVENT_INVALID_CURSOR_POSITION},
13299 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
13300 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
13301 }
13302
13303 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Linnan Li13bf76a2024-05-05 19:18:02 +080013304 return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013305 ui::LogicalDisplayId::INVALID,
Prabir Pradhan5735a322022-04-11 17:23:34 +000013306 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +000013307 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +000013308 mPolicyFlags);
13309 }
13310
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013311 sp<FakeWindowHandle> createWindow(const char* name) const {
Prabir Pradhan5735a322022-04-11 17:23:34 +000013312 std::shared_ptr<FakeApplicationHandle> overlayApplication =
13313 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013314 sp<FakeWindowHandle> window =
13315 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, name,
13316 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +000013317 window->setOwnerInfo(mPid, mUid);
13318 return window;
13319 }
13320};
13321
13322using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
13323
13324TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013325 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013326 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013327 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013328
13329 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13330 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13331 window->consumeMotionDown();
13332
13333 setFocusedWindow(window);
13334 window->consumeFocusEvent(true);
13335
13336 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13337 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013338 window->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Prabir Pradhan5735a322022-04-11 17:23:34 +000013339}
13340
13341TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013342 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013343 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013344 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013345
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013346 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013347 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
13348 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13349
13350 setFocusedWindow(window);
13351 window->consumeFocusEvent(true);
13352
13353 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
13354 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
13355 window->assertNoEvents();
13356}
13357
13358TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013359 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013360 auto window = owner.createWindow("Owned window");
13361 auto spy = owner.createWindow("Owned spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013362 spy->setSpy(true);
13363 spy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013364 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013365
13366 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13367 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13368 spy->consumeMotionDown();
13369 window->consumeMotionDown();
13370}
13371
13372TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013373 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013374 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013375
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013376 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013377 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013378 randosSpy->setSpy(true);
13379 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013380 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013381
13382 // The event is targeted at owner's window, so injection should succeed, but the spy should
13383 // not receive the event.
13384 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13385 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13386 randosSpy->assertNoEvents();
13387 window->consumeMotionDown();
13388}
13389
13390TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013391 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013392 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013393
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013394 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013395 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013396 randosSpy->setSpy(true);
13397 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013398 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013399
13400 // A user that has injection permission can inject into any window.
13401 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070013402 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013403 ui::LogicalDisplayId::DEFAULT));
Prabir Pradhan5735a322022-04-11 17:23:34 +000013404 randosSpy->consumeMotionDown();
13405 window->consumeMotionDown();
13406
13407 setFocusedWindow(randosSpy);
13408 randosSpy->consumeFocusEvent(true);
13409
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070013410 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013411 randosSpy->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Prabir Pradhan5735a322022-04-11 17:23:34 +000013412 window->assertNoEvents();
13413}
13414
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070013415TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013416 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013417 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013418
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013419 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013420 auto randosWindow = rando.createWindow("Rando's window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013421 randosWindow->setFrame(Rect{-10, -10, -5, -5});
13422 randosWindow->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013423 mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013424
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070013425 // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids.
Prabir Pradhan5735a322022-04-11 17:23:34 +000013426 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13427 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13428 window->consumeMotionDown();
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070013429 randosWindow->assertNoEvents();
Prabir Pradhan5735a322022-04-11 17:23:34 +000013430}
13431
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013432using InputDispatcherPointerInWindowTest = InputDispatcherTest;
13433
13434TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) {
13435 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
13436
13437 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013438 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013439 left->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013440 sp<FakeWindowHandle> right =
13441 sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window",
13442 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013443 right->setFrame(Rect(100, 0, 200, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013444 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window",
13445 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013446 spy->setFrame(Rect(0, 0, 200, 100));
13447 spy->setTrustedOverlay(true);
13448 spy->setSpy(true);
13449
13450 mDispatcher->onWindowInfosChanged(
13451 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
13452
13453 // Hover into the left window.
13454 mDispatcher->notifyMotion(
13455 MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
13456 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50))
13457 .build());
13458
13459 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13460 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13461
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013462 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13463 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013464 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013465 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13466 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013467 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013468 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13469 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013470 /*pointerId=*/0));
13471
13472 // Hover move to the right window.
13473 mDispatcher->notifyMotion(
13474 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
13475 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50))
13476 .build());
13477
13478 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13479 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13480 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
13481
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013482 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13483 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013484 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013485 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13486 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013487 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013488 ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13489 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013490 /*pointerId=*/0));
13491
13492 // Stop hovering.
13493 mDispatcher->notifyMotion(
13494 MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
13495 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50))
13496 .build());
13497
13498 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13499 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13500
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013501 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13502 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013503 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013504 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13505 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013506 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013507 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13508 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013509 /*pointerId=*/0));
13510}
13511
13512TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) {
13513 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
13514
13515 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013516 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013517 left->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013518 sp<FakeWindowHandle> right =
13519 sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window",
13520 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013521 right->setFrame(Rect(100, 0, 200, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013522 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window",
13523 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013524 spy->setFrame(Rect(0, 0, 200, 100));
13525 spy->setTrustedOverlay(true);
13526 spy->setSpy(true);
13527
13528 mDispatcher->onWindowInfosChanged(
13529 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
13530
13531 // First pointer down on left window.
13532 mDispatcher->notifyMotion(
13533 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
13534 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
13535 .build());
13536
13537 left->consumeMotionDown();
13538 spy->consumeMotionDown();
13539
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013540 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13541 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013542 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013543 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13544 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013545 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013546 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13547 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013548 /*pointerId=*/0));
13549
13550 // Second pointer down on right window.
13551 mDispatcher->notifyMotion(
13552 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
13553 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
13554 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
13555 .build());
13556
13557 left->consumeMotionMove();
13558 right->consumeMotionDown();
13559 spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
13560
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013561 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13562 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013563 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013564 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13565 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013566 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013567 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13568 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013569 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013570 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13571 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013572 /*pointerId=*/1));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013573 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13574 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013575 /*pointerId=*/1));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013576 ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13577 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013578 /*pointerId=*/1));
13579
13580 // Second pointer up.
13581 mDispatcher->notifyMotion(
13582 MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
13583 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
13584 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
13585 .build());
13586
13587 left->consumeMotionMove();
13588 right->consumeMotionUp();
13589 spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
13590
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013591 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13592 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013593 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013594 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13595 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013596 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013597 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->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_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13601 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013602 /*pointerId=*/1));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013603 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13604 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013605 /*pointerId=*/1));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013606 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13607 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013608 /*pointerId=*/1));
13609
13610 // First pointer up.
13611 mDispatcher->notifyMotion(
13612 MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
13613 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
13614 .build());
13615
13616 left->consumeMotionUp();
13617 spy->consumeMotionUp();
13618
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013619 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->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(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13623 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013624 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013625 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13626 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013627 /*pointerId=*/0));
13628}
13629
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013630TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse_legacy) {
13631 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013632 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
13633
13634 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013635 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013636 left->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013637 sp<FakeWindowHandle> right =
13638 sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window",
13639 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013640 right->setFrame(Rect(100, 0, 200, 100));
13641
13642 mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0});
13643
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013644 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13645 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013646 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013647 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13648 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013649 /*pointerId=*/0));
13650
13651 // Hover move into the window.
13652 mDispatcher->notifyMotion(
13653 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13654 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50))
13655 .rawXCursorPosition(50)
13656 .rawYCursorPosition(50)
13657 .deviceId(DEVICE_ID)
13658 .build());
13659
13660 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13661
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013662 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13663 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013664 /*pointerId=*/0));
13665
13666 // Move the mouse with another device. This cancels the hovering pointer from the first device.
13667 mDispatcher->notifyMotion(
13668 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13669 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50))
13670 .rawXCursorPosition(51)
13671 .rawYCursorPosition(50)
13672 .deviceId(SECOND_DEVICE_ID)
13673 .build());
13674
13675 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13676 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13677
13678 // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets
13679 // a HOVER_EXIT from the first device.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013680 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13681 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013682 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013683 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013684 SECOND_DEVICE_ID,
13685 /*pointerId=*/0));
13686
13687 // Move the mouse outside the window. Document the current behavior, where the window does not
13688 // receive HOVER_EXIT even though the mouse left the window.
13689 mDispatcher->notifyMotion(
13690 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13691 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50))
13692 .rawXCursorPosition(150)
13693 .rawYCursorPosition(50)
13694 .deviceId(SECOND_DEVICE_ID)
13695 .build());
13696
13697 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13698 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013699 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13700 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013701 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013702 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013703 SECOND_DEVICE_ID,
13704 /*pointerId=*/0));
13705}
13706
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013707/**
13708 * TODO(b/313689709) - correctly support multiple mouse devices, because they should be controlling
13709 * the same cursor, and therefore have a shared motion event stream.
13710 */
13711TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) {
13712 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
13713 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
13714
13715 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013716 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013717 left->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013718 sp<FakeWindowHandle> right =
13719 sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window",
13720 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013721 right->setFrame(Rect(100, 0, 200, 100));
13722
13723 mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0});
13724
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013725 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13726 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013727 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013728 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13729 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013730 /*pointerId=*/0));
13731
13732 // Hover move into the window.
13733 mDispatcher->notifyMotion(
13734 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13735 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50))
13736 .rawXCursorPosition(50)
13737 .rawYCursorPosition(50)
13738 .deviceId(DEVICE_ID)
13739 .build());
13740
13741 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13742
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013743 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13744 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013745 /*pointerId=*/0));
13746
13747 // Move the mouse with another device
13748 mDispatcher->notifyMotion(
13749 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13750 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50))
13751 .rawXCursorPosition(51)
13752 .rawYCursorPosition(50)
13753 .deviceId(SECOND_DEVICE_ID)
13754 .build());
13755 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13756
13757 // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets
13758 // a HOVER_EXIT from the first device.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013759 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13760 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013761 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013762 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013763 SECOND_DEVICE_ID,
13764 /*pointerId=*/0));
13765
13766 // Move the mouse outside the window. Document the current behavior, where the window does not
13767 // receive HOVER_EXIT even though the mouse left the window.
13768 mDispatcher->notifyMotion(
13769 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13770 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50))
13771 .rawXCursorPosition(150)
13772 .rawYCursorPosition(50)
13773 .deviceId(SECOND_DEVICE_ID)
13774 .build());
13775
13776 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013777 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13778 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013779 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013780 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013781 SECOND_DEVICE_ID,
13782 /*pointerId=*/0));
13783}
13784
Arpit Singhb65e2bd2024-06-03 09:48:16 +000013785TEST_F(InputDispatcherTest, FocusedDisplayChangeIsNotified) {
13786 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
13787 mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID);
13788}
13789
Garfield Tane84e6f92019-08-29 17:28:41 -070013790} // namespace android::inputdispatcher