blob: 244deab767bad11903a83ddcdbab3c50db756f89 [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);
8586 // Set focus window for second display.
8587 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07008588 windowInSecondary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008589 mDispatcher->onWindowInfosChanged(
8590 {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008591 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008592 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008593 }
8594
Prabir Pradhan3608aad2019-10-02 17:08:26 -07008595 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008596 InputDispatcherTest::TearDown();
8597
Chris Yea209fde2020-07-22 13:54:51 -07008598 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008599 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07008600 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008601 windowInSecondary.clear();
8602 }
8603
8604protected:
Chris Yea209fde2020-07-22 13:54:51 -07008605 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008606 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07008607 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008608 sp<FakeWindowHandle> windowInSecondary;
8609};
8610
8611TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
8612 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008613 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008614 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8615 ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008616 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008617 windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08008618 windowInSecondary->assertNoEvents();
8619
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008620 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008621 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008622 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008623 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08008624 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08008625 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08008626}
8627
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008628TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08008629 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08008630 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008631 injectKeyDownNoRepeat(*mDispatcher, ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008632 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008633 windowInPrimary->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08008634 windowInSecondary->assertNoEvents();
8635
8636 // Test inject a key down without display id specified.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008637 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008638 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08008639 windowInPrimary->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008640 windowInSecondary->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Arthur Hungb92218b2018-08-14 12:00:21 +08008641
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008642 // Remove all windows in secondary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008643 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
Arthur Hungb92218b2018-08-14 12:00:21 +08008644
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008645 // Old focus should receive a cancel event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008646 windowInSecondary->consumeKeyUp(ui::LogicalDisplayId::INVALID, AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08008647
8648 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008649 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Arthur Hungb92218b2018-08-14 12:00:21 +08008650 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008651 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08008652 windowInSecondary->assertNoEvents();
8653}
8654
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008655// Test per-display input monitors for motion event.
8656TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08008657 FakeMonitorReceiver monitorInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008658 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08008659 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00008660 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008661
8662 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008663 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008664 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8665 ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008666 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008667 windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
8668 monitorInPrimary.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008669 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08008670 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008671
8672 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008673 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008674 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008675 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008676 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08008677 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08008678 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08008679 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008680
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08008681 // Lift up the touch from the second display
8682 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008683 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08008684 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8685 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
8686 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
8687
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008688 // Test inject a non-pointer motion event.
8689 // If specific a display, it will dispatch to the focused window of particular display,
8690 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008691 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008692 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL,
8693 ui::LogicalDisplayId::INVALID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008694 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008695 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08008696 monitorInPrimary.assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008697 windowInSecondary->consumeMotionDown(ui::LogicalDisplayId::INVALID);
8698 monitorInSecondary.consumeMotionDown(ui::LogicalDisplayId::INVALID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008699}
8700
8701// Test per-display input monitors for key event.
8702TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008703 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08008704 FakeMonitorReceiver monitorInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008705 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08008706 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00008707 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008708
8709 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008710 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008711 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008712 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08008713 monitorInPrimary.assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008714 windowInSecondary->consumeKeyDown(ui::LogicalDisplayId::INVALID);
8715 monitorInSecondary.consumeKeyDown(ui::LogicalDisplayId::INVALID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08008716}
8717
Vishnu Nair958da932020-08-21 17:12:37 -07008718TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
8719 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008720 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2",
8721 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07008722 secondWindowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008723 mDispatcher->onWindowInfosChanged(
8724 {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(),
8725 *windowInSecondary->getInfo()},
8726 {},
8727 0,
8728 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008729 setFocusedWindow(secondWindowInPrimary);
8730 windowInPrimary->consumeFocusEvent(false);
8731 secondWindowInPrimary->consumeFocusEvent(true);
8732
8733 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008734 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008735 injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008736 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07008737 windowInPrimary->assertNoEvents();
8738 windowInSecondary->assertNoEvents();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008739 secondWindowInPrimary->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07008740}
8741
Arthur Hungdfd528e2021-12-08 13:23:04 +00008742TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
8743 FakeMonitorReceiver monitorInPrimary =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008744 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Arthur Hungdfd528e2021-12-08 13:23:04 +00008745 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00008746 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hungdfd528e2021-12-08 13:23:04 +00008747
8748 // Test touch down on primary display.
8749 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008750 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8751 ui::LogicalDisplayId::DEFAULT))
Arthur Hungdfd528e2021-12-08 13:23:04 +00008752 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008753 windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
8754 monitorInPrimary.consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hungdfd528e2021-12-08 13:23:04 +00008755
8756 // Test touch down on second display.
8757 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008758 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungdfd528e2021-12-08 13:23:04 +00008759 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8760 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
8761 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
8762
8763 // Trigger cancel touch.
8764 mDispatcher->cancelCurrentTouch();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008765 windowInPrimary->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT);
8766 monitorInPrimary.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT);
Arthur Hungdfd528e2021-12-08 13:23:04 +00008767 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
8768 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
8769
8770 // Test inject a move motion event, no window/monitor should receive the event.
8771 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008772 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008773 ui::LogicalDisplayId::DEFAULT, {110, 200}))
Arthur Hungdfd528e2021-12-08 13:23:04 +00008774 << "Inject motion event should return InputEventInjectionResult::FAILED";
8775 windowInPrimary->assertNoEvents();
8776 monitorInPrimary.assertNoEvents();
8777
8778 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008779 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00008780 SECOND_DISPLAY_ID, {110, 200}))
8781 << "Inject motion event should return InputEventInjectionResult::FAILED";
8782 windowInSecondary->assertNoEvents();
8783 monitorInSecondary.assertNoEvents();
8784}
8785
Hu Guocb134f12023-12-23 13:42:44 +00008786/**
8787 * Send a key to the primary display and to the secondary display.
8788 * Then cause the key on the primary display to be canceled by sending in a stale key.
8789 * Ensure that the key on the primary display is canceled, and that the key on the secondary display
8790 * does not get canceled.
8791 */
8792TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) {
8793 // Send a key down on primary display
8794 mDispatcher->notifyKey(
8795 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008796 .displayId(ui::LogicalDisplayId::DEFAULT)
Hu Guocb134f12023-12-23 13:42:44 +00008797 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8798 .build());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008799 windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN),
8800 WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Hu Guocb134f12023-12-23 13:42:44 +00008801 windowInSecondary->assertNoEvents();
8802
8803 // Send a key down on second display
8804 mDispatcher->notifyKey(
8805 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
8806 .displayId(SECOND_DISPLAY_ID)
8807 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8808 .build());
8809 windowInSecondary->consumeKeyEvent(
8810 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID)));
8811 windowInPrimary->assertNoEvents();
8812
8813 // Send a valid key up event on primary display that will be dropped because it is stale
8814 NotifyKeyArgs staleKeyUp =
8815 KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008816 .displayId(ui::LogicalDisplayId::DEFAULT)
Hu Guocb134f12023-12-23 13:42:44 +00008817 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8818 .build();
8819 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms;
8820 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
8821 std::this_thread::sleep_for(STALE_EVENT_TIMEOUT);
8822 mDispatcher->notifyKey(staleKeyUp);
8823
8824 // Only the key gesture corresponding to the dropped event should receive the cancel event.
8825 // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not
8826 // receive any events.
8827 windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008828 WithDisplayId(ui::LogicalDisplayId::DEFAULT),
Hu Guocb134f12023-12-23 13:42:44 +00008829 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
8830 windowInSecondary->assertNoEvents();
8831}
8832
8833/**
8834 * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events.
8835 */
8836TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) {
8837 // Send touch down on primary display.
8838 mDispatcher->notifyMotion(
8839 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8840 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008841 .displayId(ui::LogicalDisplayId::DEFAULT)
Hu Guocb134f12023-12-23 13:42:44 +00008842 .build());
8843 windowInPrimary->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008844 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Hu Guocb134f12023-12-23 13:42:44 +00008845 windowInSecondary->assertNoEvents();
8846
8847 // Send touch down on second display.
8848 mDispatcher->notifyMotion(
8849 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8850 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8851 .displayId(SECOND_DISPLAY_ID)
8852 .build());
8853 windowInPrimary->assertNoEvents();
8854 windowInSecondary->consumeMotionEvent(
8855 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID)));
8856
8857 // inject a valid MotionEvent on primary display that will be stale when it arrives.
8858 NotifyMotionArgs staleMotionUp =
8859 MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008860 .displayId(ui::LogicalDisplayId::DEFAULT)
Hu Guocb134f12023-12-23 13:42:44 +00008861 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
8862 .build();
8863 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms;
8864 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
8865 std::this_thread::sleep_for(STALE_EVENT_TIMEOUT);
8866 mDispatcher->notifyMotion(staleMotionUp);
8867
8868 // For stale motion events, we let the gesture to complete. This behaviour is different from key
8869 // events, where we would cancel the current keys instead.
8870 windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP));
8871 windowInSecondary->assertNoEvents();
8872}
8873
Jackal Guof9696682018-10-05 12:23:23 +08008874class InputFilterTest : public InputDispatcherTest {
8875protected:
Linnan Li13bf76a2024-05-05 19:18:02 +08008876 void testNotifyMotion(ui::LogicalDisplayId displayId, bool expectToBeFiltered,
Prabir Pradhan81420cc2021-09-06 10:28:50 -07008877 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08008878 NotifyMotionArgs motionArgs;
8879
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008880 motionArgs =
8881 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00008882 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008883 motionArgs =
8884 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00008885 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008886 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08008887 if (expectToBeFiltered) {
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008888 const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue());
Prabir Pradhan81420cc2021-09-06 10:28:50 -07008889 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08008890 } else {
8891 mFakePolicy->assertFilterInputEventWasNotCalled();
8892 }
8893 }
8894
8895 void testNotifyKey(bool expectToBeFiltered) {
8896 NotifyKeyArgs keyArgs;
8897
8898 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00008899 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08008900 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00008901 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008902 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08008903
8904 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08008905 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08008906 } else {
8907 mFakePolicy->assertFilterInputEventWasNotCalled();
8908 }
8909 }
8910};
8911
8912// Test InputFilter for MotionEvent
8913TEST_F(InputFilterTest, MotionEvent_InputFilter) {
8914 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008915 testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/false);
Harry Cutts101ee9b2023-07-06 18:04:14 +00008916 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08008917
8918 // Enable InputFilter
8919 mDispatcher->setInputFilterEnabled(true);
8920 // Test touch on both primary and second display, and check if both events are filtered.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008921 testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/true);
Harry Cutts101ee9b2023-07-06 18:04:14 +00008922 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08008923
8924 // Disable InputFilter
8925 mDispatcher->setInputFilterEnabled(false);
8926 // Test touch on both primary and second display, and check if both events aren't filtered.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008927 testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/false);
Harry Cutts101ee9b2023-07-06 18:04:14 +00008928 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08008929}
8930
8931// Test InputFilter for KeyEvent
8932TEST_F(InputFilterTest, KeyEvent_InputFilter) {
8933 // Since the InputFilter is disabled by default, check if key event aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00008934 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08008935
8936 // Enable InputFilter
8937 mDispatcher->setInputFilterEnabled(true);
8938 // Send a key event, and check if it is filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00008939 testNotifyKey(/*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08008940
8941 // Disable InputFilter
8942 mDispatcher->setInputFilterEnabled(false);
8943 // Send a key event, and check if it isn't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00008944 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08008945}
8946
Prabir Pradhan81420cc2021-09-06 10:28:50 -07008947// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
8948// logical display coordinate space.
8949TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
8950 ui::Transform firstDisplayTransform;
8951 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
8952 ui::Transform secondDisplayTransform;
8953 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
8954
8955 std::vector<gui::DisplayInfo> displayInfos(2);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008956 displayInfos[0].displayId = ui::LogicalDisplayId::DEFAULT;
Prabir Pradhan81420cc2021-09-06 10:28:50 -07008957 displayInfos[0].transform = firstDisplayTransform;
8958 displayInfos[1].displayId = SECOND_DISPLAY_ID;
8959 displayInfos[1].transform = secondDisplayTransform;
8960
Patrick Williamsd828f302023-04-28 17:52:08 -05008961 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07008962
8963 // Enable InputFilter
8964 mDispatcher->setInputFilterEnabled(true);
8965
8966 // Ensure the correct transforms are used for the displays.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008967 testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/true,
8968 firstDisplayTransform);
Harry Cutts101ee9b2023-07-06 18:04:14 +00008969 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform);
Prabir Pradhan81420cc2021-09-06 10:28:50 -07008970}
8971
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008972class InputFilterInjectionPolicyTest : public InputDispatcherTest {
8973protected:
8974 virtual void SetUp() override {
8975 InputDispatcherTest::SetUp();
8976
8977 /**
8978 * We don't need to enable input filter to test the injected event policy, but we enabled it
8979 * here to make the tests more realistic, since this policy only matters when inputfilter is
8980 * on.
8981 */
8982 mDispatcher->setInputFilterEnabled(true);
8983
8984 std::shared_ptr<InputApplicationHandle> application =
8985 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008986 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008987 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008988
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008989 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008990 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008991 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008992 setFocusedWindow(mWindow);
8993 mWindow->consumeFocusEvent(true);
8994 }
8995
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008996 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
8997 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008998 KeyEvent event;
8999
9000 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
9001 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009002 ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0,
9003 AKEYCODE_A, KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009004 const int32_t additionalPolicyFlags =
9005 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
9006 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00009007 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00009008 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009009 policyFlags | additionalPolicyFlags));
9010
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009011 mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags)));
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009012 }
9013
9014 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
9015 int32_t flags) {
9016 MotionEvent event;
9017 PointerProperties pointerProperties[1];
9018 PointerCoords pointerCoords[1];
9019 pointerProperties[0].clear();
9020 pointerProperties[0].id = 0;
9021 pointerCoords[0].clear();
9022 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
9023 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
9024
9025 ui::Transform identityTransform;
9026 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
9027 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
9028 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
9029 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
9030 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07009031 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07009032 eventTime,
Harry Cutts101ee9b2023-07-06 18:04:14 +00009033 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009034
9035 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
9036 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00009037 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00009038 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009039 policyFlags | additionalPolicyFlags));
9040
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009041 mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId)));
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009042 }
9043
9044private:
9045 sp<FakeWindowHandle> mWindow;
9046};
9047
9048TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009049 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
9050 // filter. Without it, the event will no different from a regularly injected event, and the
9051 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00009052 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
9053 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009054}
9055
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009056TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009057 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00009058 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009059 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
9060}
9061
9062TEST_F(InputFilterInjectionPolicyTest,
9063 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
9064 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00009065 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00009066 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009067}
9068
9069TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00009070 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
9071 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00009072}
9073
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009074class InputDispatcherUserActivityPokeTests : public InputDispatcherTest {
9075protected:
9076 virtual void SetUp() override {
9077 InputDispatcherTest::SetUp();
9078
9079 std::shared_ptr<FakeApplicationHandle> application =
9080 std::make_shared<FakeApplicationHandle>();
9081 application->setDispatchingTimeout(100ms);
9082 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009083 ui::LogicalDisplayId::DEFAULT);
Yeabkal Wubshit222d83d2024-01-24 18:00:09 +00009084 mWindow->setFrame(Rect(0, 0, 100, 100));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009085 mWindow->setDispatchingTimeout(100ms);
9086 mWindow->setFocusable(true);
9087
9088 // Set focused application.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009089 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009090
9091 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
9092 setFocusedWindow(mWindow);
9093 mWindow->consumeFocusEvent(true);
9094 }
9095
Linnan Li13bf76a2024-05-05 19:18:02 +08009096 void notifyAndConsumeMotion(int32_t action, uint32_t source, ui::LogicalDisplayId displayId,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009097 nsecs_t eventTime) {
9098 mDispatcher->notifyMotion(MotionArgsBuilder(action, source)
9099 .displayId(displayId)
9100 .eventTime(eventTime)
9101 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
9102 .build());
9103 mWindow->consumeMotionEvent(WithMotionAction(action));
9104 }
9105
9106private:
9107 sp<FakeWindowHandle> mWindow;
9108};
9109
9110TEST_F_WITH_FLAGS(
9111 InputDispatcherUserActivityPokeTests, MinPokeTimeObserved,
9112 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
9113 rate_limit_user_activity_poke_in_dispatcher))) {
9114 mDispatcher->setMinTimeBetweenUserActivityPokes(50ms);
9115
9116 // First event of type TOUCH. Should poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009117 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009118 milliseconds_to_nanoseconds(50));
9119 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009120 {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH,
9121 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009122
9123 // 80ns > 50ns has passed since previous TOUCH event. Should poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009124 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009125 milliseconds_to_nanoseconds(130));
9126 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009127 {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH,
9128 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009129
9130 // First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event).
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009131 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
9132 ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(135));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009133 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009134 {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER,
9135 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009136
9137 // Within 50ns of previous TOUCH event. Should NOT poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009138 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009139 milliseconds_to_nanoseconds(140));
9140 mFakePolicy->assertUserActivityNotPoked();
9141
9142 // Within 50ns of previous OTHER event. Should NOT poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009143 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
9144 ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(150));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009145 mFakePolicy->assertUserActivityNotPoked();
9146
9147 // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke.
9148 // Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009149 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009150 milliseconds_to_nanoseconds(160));
9151 mFakePolicy->assertUserActivityNotPoked();
9152
9153 // 65ns > 50ns has passed since previous OTHER event. Should poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009154 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
9155 ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(200));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009156 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009157 {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER,
9158 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009159
9160 // 170ns > 50ns has passed since previous TOUCH event. Should poke.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009161 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009162 milliseconds_to_nanoseconds(300));
9163 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009164 {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH,
9165 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009166
9167 // Assert that there's no more user activity poke event.
9168 mFakePolicy->assertUserActivityNotPoked();
9169}
9170
9171TEST_F_WITH_FLAGS(
9172 InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed,
9173 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
9174 rate_limit_user_activity_poke_in_dispatcher))) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009175 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009176 milliseconds_to_nanoseconds(200));
9177 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009178 {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH,
9179 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009180
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009181 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009182 milliseconds_to_nanoseconds(280));
9183 mFakePolicy->assertUserActivityNotPoked();
9184
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009185 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009186 milliseconds_to_nanoseconds(340));
9187 mFakePolicy->assertUserActivityPoked(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009188 {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH,
9189 ui::LogicalDisplayId::DEFAULT}});
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009190}
9191
9192TEST_F_WITH_FLAGS(
9193 InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting,
9194 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
9195 rate_limit_user_activity_poke_in_dispatcher))) {
9196 mDispatcher->setMinTimeBetweenUserActivityPokes(0ms);
9197
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009198 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
9199 20);
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009200 mFakePolicy->assertUserActivityPoked();
9201
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009202 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
9203 30);
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08009204 mFakePolicy->assertUserActivityPoked();
9205}
9206
chaviwfd6d3512019-03-25 13:23:49 -07009207class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07009208 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07009209 InputDispatcherTest::SetUp();
9210
Chris Yea209fde2020-07-22 13:54:51 -07009211 std::shared_ptr<FakeApplicationHandle> application =
9212 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009213 mUnfocusedWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Top",
9214 ui::LogicalDisplayId::DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07009215 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07009216
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009217 mFocusedWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Second",
9218 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08009219 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07009220
9221 // Set focused application.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009222 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07009223 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07009224
9225 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009226 mDispatcher->onWindowInfosChanged(
9227 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009228 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01009229 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07009230 }
9231
Prabir Pradhan3608aad2019-10-02 17:08:26 -07009232 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07009233 InputDispatcherTest::TearDown();
9234
9235 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08009236 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07009237 }
9238
9239protected:
9240 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08009241 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07009242 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07009243};
9244
9245// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
9246// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
9247// the onPointerDownOutsideFocus callback.
9248TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009249 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009250 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9251 ui::LogicalDisplayId::DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009252 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07009253 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07009254
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08009255 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07009256 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
9257}
9258
9259// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
9260// DOWN on the window that doesn't have focus. Ensure no window received the
9261// onPointerDownOutsideFocus callback.
9262TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009263 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009264 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009265 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009266 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07009267 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07009268
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08009269 ASSERT_TRUE(mDispatcher->waitForIdle());
9270 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07009271}
9272
9273// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
9274// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
9275TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08009276 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009277 injectKeyDownNoRepeat(*mDispatcher, ui::LogicalDisplayId::DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009278 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009279 mFocusedWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07009280
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08009281 ASSERT_TRUE(mDispatcher->waitForIdle());
9282 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07009283}
9284
9285// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
9286// DOWN on the window that already has focus. Ensure no window received the
9287// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10009288TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009289 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009290 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9291 ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009292 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07009293 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07009294
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08009295 ASSERT_TRUE(mDispatcher->waitForIdle());
9296 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07009297}
9298
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08009299// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
9300// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
9301TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
9302 const MotionEvent event =
9303 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
9304 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009305 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08009306 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
9307 .build();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009308 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08009309 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009310 mUnfocusedWindow->consumeAnyMotionDown(ui::LogicalDisplayId::DEFAULT,
9311 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08009312
9313 ASSERT_TRUE(mDispatcher->waitForIdle());
9314 mFakePolicy->assertOnPointerDownWasNotCalled();
9315 // Ensure that the unfocused window did not receive any FOCUS events.
9316 mUnfocusedWindow->assertNoEvents();
9317}
9318
chaviwaf87b3e2019-10-01 16:59:28 -07009319// These tests ensures we can send touch events to a single client when there are multiple input
9320// windows that point to the same client token.
9321class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
9322 virtual void SetUp() override {
9323 InputDispatcherTest::SetUp();
9324
Chris Yea209fde2020-07-22 13:54:51 -07009325 std::shared_ptr<FakeApplicationHandle> application =
9326 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009327 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009328 ui::LogicalDisplayId::DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07009329 mWindow1->setFrame(Rect(0, 0, 100, 100));
9330
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009331 mWindow2 = mWindow1->clone(ui::LogicalDisplayId::DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07009332 mWindow2->setFrame(Rect(100, 100, 200, 200));
9333
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009334 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07009335 }
9336
9337protected:
9338 sp<FakeWindowHandle> mWindow1;
9339 sp<FakeWindowHandle> mWindow2;
9340
9341 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05009342 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07009343 vec2 vals = windowInfo->transform.transform(point.x, point.y);
9344 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07009345 }
9346
9347 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
9348 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01009349 const std::string name = window->getName();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009350 std::unique_ptr<MotionEvent> motionEvent =
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009351 window->consumeMotionEvent(WithMotionAction(expectedAction));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009352 ASSERT_NE(nullptr, motionEvent);
9353 ASSERT_EQ(points.size(), motionEvent->getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07009354
9355 for (size_t i = 0; i < points.size(); i++) {
9356 float expectedX = points[i].x;
9357 float expectedY = points[i].y;
9358
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009359 EXPECT_EQ(expectedX, motionEvent->getX(i))
chaviwaf87b3e2019-10-01 16:59:28 -07009360 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009361 << ", got " << motionEvent->getX(i);
9362 EXPECT_EQ(expectedY, motionEvent->getY(i))
chaviwaf87b3e2019-10-01 16:59:28 -07009363 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009364 << ", got " << motionEvent->getY(i);
chaviwaf87b3e2019-10-01 16:59:28 -07009365 }
9366 }
chaviw9eaa22c2020-07-01 16:21:27 -07009367
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009368 void touchAndAssertPositions(sp<FakeWindowHandle> touchedWindow, int32_t action,
9369 const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07009370 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009371 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009372 ui::LogicalDisplayId::DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07009373
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009374 consumeMotionEvent(touchedWindow, action, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07009375 }
chaviwaf87b3e2019-10-01 16:59:28 -07009376};
9377
9378TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
9379 // Touch Window 1
9380 PointF touchedPoint = {10, 10};
9381 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009382 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009383
9384 // Release touch on Window 1
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009385 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009386
9387 // Touch Window 2
9388 touchedPoint = {150, 150};
9389 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009390 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009391}
9392
chaviw9eaa22c2020-07-01 16:21:27 -07009393TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
9394 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07009395 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009396 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07009397
9398 // Touch Window 1
9399 PointF touchedPoint = {10, 10};
9400 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009401 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009402 // Release touch on Window 1
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009403 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009404
9405 // Touch Window 2
9406 touchedPoint = {150, 150};
9407 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009408 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
9409 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009410
chaviw9eaa22c2020-07-01 16:21:27 -07009411 // Update the transform so rotation is set
9412 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009413 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07009414 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009415 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07009416}
9417
chaviw9eaa22c2020-07-01 16:21:27 -07009418TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009419 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009420 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009421
9422 // Touch Window 1
9423 std::vector<PointF> touchedPoints = {PointF{10, 10}};
9424 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009425 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009426
9427 // Touch Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009428 // Since this is part of the same touch gesture that has already been dispatched to Window 1,
9429 // the touch stream from Window 2 will be merged with the stream in Window 1. The merged stream
9430 // will continue to be dispatched through Window 1.
chaviw9eaa22c2020-07-01 16:21:27 -07009431 touchedPoints.push_back(PointF{150, 150});
9432 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009433 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009434
chaviw9eaa22c2020-07-01 16:21:27 -07009435 // Release Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009436 touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07009437 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009438
chaviw9eaa22c2020-07-01 16:21:27 -07009439 // Update the transform so rotation is set for Window 2
9440 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009441 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07009442 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009443 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009444}
9445
chaviw9eaa22c2020-07-01 16:21:27 -07009446TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009447 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009448 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009449
9450 // Touch Window 1
9451 std::vector<PointF> touchedPoints = {PointF{10, 10}};
9452 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009453 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009454
9455 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07009456 touchedPoints.push_back(PointF{150, 150});
9457 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009458
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009459 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009460
9461 // Move both windows
9462 touchedPoints = {{20, 20}, {175, 175}};
9463 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
9464 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
9465
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009466 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009467
chaviw9eaa22c2020-07-01 16:21:27 -07009468 // Release Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009469 touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07009470 expectedPoints.pop_back();
9471
9472 // Touch Window 2
9473 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009474 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07009475 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009476 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07009477
9478 // Move both windows
9479 touchedPoints = {{20, 20}, {175, 175}};
9480 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
9481 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
9482
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009483 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009484}
9485
9486TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
9487 mWindow1->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009488 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009489
9490 // Touch Window 1
9491 std::vector<PointF> touchedPoints = {PointF{10, 10}};
9492 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009493 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009494
9495 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07009496 touchedPoints.push_back(PointF{150, 150});
9497 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009498
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009499 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009500
9501 // Move both windows
9502 touchedPoints = {{20, 20}, {175, 175}};
9503 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
9504 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
9505
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009506 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00009507}
9508
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07009509/**
9510 * When one of the windows is slippery, the touch should not slip into the other window with the
9511 * same input channel.
9512 */
9513TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
9514 mWindow1->setSlippery(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009515 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07009516
9517 // Touch down in window 1
9518 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009519 ui::LogicalDisplayId::DEFAULT, {{50, 50}}));
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07009520 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
9521
9522 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
9523 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
9524 // getting generated.
9525 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009526 ui::LogicalDisplayId::DEFAULT, {{150, 150}}));
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07009527
9528 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
9529}
9530
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009531/**
9532 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
9533 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
9534 * that the pointer is hovering over may have a different transform.
9535 */
9536TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009537 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009538
9539 // Start hover in window 1
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07009540 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN)
9541 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
9542 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009543 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
9544 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009545 // Move hover to window 2.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07009546 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
9547 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
9548 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009549 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009550 consumeMotionEvent(mWindow2, ACTION_HOVER_ENTER,
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07009551 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
9552}
9553
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009554class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
9555 virtual void SetUp() override {
9556 InputDispatcherTest::SetUp();
9557
Chris Yea209fde2020-07-22 13:54:51 -07009558 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009559 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009560 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009561 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009562 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009563 mWindow->setDispatchingTimeout(100ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07009564 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009565
9566 // Set focused application.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009567 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009568
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009569 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009570 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009571 mWindow->consumeFocusEvent(true);
9572 }
9573
9574 virtual void TearDown() override {
9575 InputDispatcherTest::TearDown();
9576 mWindow.clear();
9577 }
9578
9579protected:
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009580 static constexpr std::chrono::duration SPY_TIMEOUT = 200ms;
Chris Yea209fde2020-07-22 13:54:51 -07009581 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009582 sp<FakeWindowHandle> mWindow;
9583 static constexpr PointF WINDOW_LOCATION = {20, 20};
9584
9585 void tapOnWindow() {
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08009586 const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER)
9587 .x(WINDOW_LOCATION.x)
9588 .y(WINDOW_LOCATION.y);
9589 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9590 .pointer(touchingPointer)
9591 .build());
9592 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
9593 .pointer(touchingPointer)
9594 .build());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009595 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009596
9597 sp<FakeWindowHandle> addSpyWindow() {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009598 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy",
9599 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009600 spy->setTrustedOverlay(true);
9601 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009602 spy->setSpy(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009603 spy->setDispatchingTimeout(SPY_TIMEOUT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009604 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009605 return spy;
9606 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009607};
9608
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009609// Send a tap and respond, which should not cause an ANR.
9610TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
9611 tapOnWindow();
9612 mWindow->consumeMotionDown();
9613 mWindow->consumeMotionUp();
9614 ASSERT_TRUE(mDispatcher->waitForIdle());
9615 mFakePolicy->assertNotifyAnrWasNotCalled();
9616}
9617
9618// Send a regular key and respond, which should not cause an ANR.
9619TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009620 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009621 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009622 ASSERT_TRUE(mDispatcher->waitForIdle());
9623 mFakePolicy->assertNotifyAnrWasNotCalled();
9624}
9625
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05009626TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
9627 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009628 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05009629 mWindow->consumeFocusEvent(false);
9630
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009631 InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009632 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
9633 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
9634 CONSUME_TIMEOUT_EVENT_EXPECTED,
Harry Cutts33476232023-01-30 19:57:29 +00009635 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009636 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05009637 // Key will not go to window because we have no focused window.
9638 // The 'no focused window' ANR timer should start instead.
9639
9640 // Now, the focused application goes away.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009641 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, nullptr);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05009642 // The key should get dropped and there should be no ANR.
9643
9644 ASSERT_TRUE(mDispatcher->waitForIdle());
9645 mFakePolicy->assertNotifyAnrWasNotCalled();
9646}
9647
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009648// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009649// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
9650// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009651TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009652 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009653 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9654 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009655
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009656 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009657 ASSERT_TRUE(sequenceNum);
9658 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009659 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009660
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009661 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009662 mWindow->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009663 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009664 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009665 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009666}
9667
9668// Send a key to the app and have the app not respond right away.
9669TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
9670 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009671 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009672 const auto [sequenceNum, _] = mWindow->receiveEvent();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009673 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009674 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009675 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07009676 ASSERT_TRUE(mDispatcher->waitForIdle());
9677}
9678
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009679// We have a focused application, but no focused window
9680TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07009681 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009682 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009683 mWindow->consumeFocusEvent(false);
9684
9685 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009686 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009687 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9688 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009689 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
9690 mDispatcher->waitForIdle();
9691 mFakePolicy->assertNotifyAnrWasNotCalled();
9692
9693 // Once a focused event arrives, we get an ANR for this application
9694 // We specify the injection timeout to be smaller than the application timeout, to ensure that
9695 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009696 const InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009697 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
9698 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::WAIT_FOR_RESULT, 50ms,
Linnan Li13bf76a2024-05-05 19:18:02 +08009699 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009700 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009701 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07009702 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009703 ASSERT_TRUE(mDispatcher->waitForIdle());
9704}
9705
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009706/**
9707 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
9708 * there will not be an ANR.
9709 */
9710TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
9711 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009712 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009713 mWindow->consumeFocusEvent(false);
9714
9715 KeyEvent event;
Siarhei Vishniakoua7333112023-10-27 13:33:29 -07009716 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
9717 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009718 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
9719 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
9720
9721 // Define a valid key down event that is stale (too old).
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009722 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
9723 ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN,
9724 /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime,
9725 eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009726
Hu Guofe3c8f12023-09-22 17:20:15 +08009727 const int32_t policyFlags =
9728 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009729
9730 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00009731 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08009732 InputEventInjectionSync::WAIT_FOR_RESULT,
9733 INJECT_EVENT_TIMEOUT, policyFlags);
9734 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
9735 << "Injection should fail because the event is stale";
9736
9737 ASSERT_TRUE(mDispatcher->waitForIdle());
9738 mFakePolicy->assertNotifyAnrWasNotCalled();
9739 mWindow->assertNoEvents();
9740}
9741
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009742// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009743// Make sure that we don't notify policy twice about the same ANR.
9744TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07009745 const std::chrono::duration appTimeout = 400ms;
9746 mApplication->setDispatchingTimeout(appTimeout);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009747 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication);
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07009748
Vishnu Nair47074b82020-08-14 11:54:47 -07009749 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009750 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009751 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009752
9753 // Once a focused event arrives, we get an ANR for this application
9754 // We specify the injection timeout to be smaller than the application timeout, to ensure that
9755 // injection times out (instead of failing).
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07009756 const std::chrono::duration eventInjectionTimeout = 100ms;
9757 ASSERT_LT(eventInjectionTimeout, appTimeout);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009758 const InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009759 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
9760 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::WAIT_FOR_RESULT,
9761 eventInjectionTimeout,
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07009762 /*allowKeyRepeat=*/false);
9763 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result)
9764 << "result=" << ftl::enum_string(result);
9765 // We already waited for 'eventInjectionTimeout`, because the countdown started when the event
9766 // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait.
9767 std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout;
9768 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009769
Vishnu Naire4df8752022-09-08 09:17:55 -07009770 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009771 // ANR should not be raised again. It is up to policy to do that if it desires.
9772 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009773
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009774 // If we now get a focused window, the ANR should stop, but the policy handles that via
9775 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009776 ASSERT_TRUE(mDispatcher->waitForIdle());
9777}
9778
9779// We have a focused application, but no focused window
9780TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07009781 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009782 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009783 mWindow->consumeFocusEvent(false);
9784
9785 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009786 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009787
Vishnu Naire4df8752022-09-08 09:17:55 -07009788 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
9789 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009790
9791 // Future focused events get dropped right away
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009792 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009793 ASSERT_TRUE(mDispatcher->waitForIdle());
9794 mWindow->assertNoEvents();
9795}
9796
9797/**
9798 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
9799 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
9800 * If we process 1 of the events, but ANR on the second event with the same timestamp,
9801 * the ANR mechanism should still work.
9802 *
9803 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
9804 * DOWN event, while not responding on the second one.
9805 */
9806TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
9807 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009808 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009809 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009810 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9811 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009812 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009813
9814 // Now send ACTION_UP, with identical timestamp
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009815 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009816 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009817 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9818 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009819 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009820
9821 // 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 -07009822 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009823 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009824 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009825}
9826
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009827// A spy window can receive an ANR
9828TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
9829 sp<FakeWindowHandle> spy = addSpyWindow();
9830
9831 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009832 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9833 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009834 mWindow->consumeMotionDown();
9835
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009836 const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009837 ASSERT_TRUE(sequenceNum);
9838 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009839 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009840
9841 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009842 spy->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009843 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009844 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009845 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009846}
9847
9848// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009849// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009850TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
9851 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009852
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009853 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009854 injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT));
9855 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
9856 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9857 injectKeyUp(*mDispatcher, ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009858
9859 // Stuck on the ACTION_UP
9860 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009861 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009862
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009863 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009864 tapOnWindow();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009865 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
9866 spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009867
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009868 mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); // still the previous motion
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009869 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08009870 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009871 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009872 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009873}
9874
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009875// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009876// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009877TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
9878 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009879
9880 tapOnWindow();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009881 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
9882 spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009883
9884 mWindow->consumeMotionDown();
9885 // Stuck on the ACTION_UP
9886 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009887 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009888
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009889 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009890 tapOnWindow();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009891 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
9892 spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009893
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009894 mWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); // still the previous motion
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009895 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08009896 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009897 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009898 spy->assertNoEvents();
9899}
9900
9901TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009902 mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009903
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009904 FakeMonitorReceiver monitor =
9905 FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009906
9907 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009908 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9909 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009910
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009911 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009912 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
9913 ASSERT_TRUE(consumeSeq);
9914
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009915 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(),
9916 MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009917
9918 monitor.finishEvent(*consumeSeq);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009919 monitor.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009920
9921 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009922 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009923}
9924
9925// If a window is unresponsive, then you get anr. if the window later catches up and starts to
9926// process events, you don't get an anr. When the window later becomes unresponsive again, you
9927// get an ANR again.
9928// 1. tap -> block on ACTION_UP -> receive ANR
9929// 2. consume all pending events (= queue becomes healthy again)
9930// 3. tap again -> block on ACTION_UP again -> receive ANR second time
9931TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
9932 tapOnWindow();
9933
9934 mWindow->consumeMotionDown();
9935 // Block on ACTION_UP
9936 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009937 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009938 mWindow->consumeMotionUp(); // Now the connection should be healthy again
9939 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08009940 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009941 mWindow->assertNoEvents();
9942
9943 tapOnWindow();
9944 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08009945 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009946 mWindow->consumeMotionUp();
9947
9948 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08009949 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009950 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009951 mWindow->assertNoEvents();
9952}
9953
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009954// If a connection remains unresponsive for a while, make sure policy is only notified once about
9955// it.
9956TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009957 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009958 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
9959 ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009960
9961 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009962 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009963 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009964 // 'notifyConnectionUnresponsive' should only be called once per connection
9965 mFakePolicy->assertNotifyAnrWasNotCalled();
9966 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009967 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009968 mWindow->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009969 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009970 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009971 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08009972 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009973 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009974}
9975
9976/**
9977 * 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 -07009978 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009979 */
9980TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08009981 // The timeouts in this test are established by relying on the fact that the "key waiting for
9982 // events timeout" is equal to 500ms.
9983 ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009984 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009985 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009986
9987 tapOnWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009988 const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009989 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009990 const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009991 ASSERT_TRUE(upSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009992
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08009993 // Don't finish the events yet, and send a key
9994 mDispatcher->notifyKey(
9995 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
9996 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
9997 .build());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009998 // Key will not be sent to the window, yet, because the window is still processing events
9999 // and the key remains pending, waiting for the touch events to be processed
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010000 // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR.
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010001 mWindow->assertNoEvents(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010002
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010003 std::this_thread::sleep_for(400ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010004 // if we wait long enough though, dispatcher will give up, and still send the key
10005 // to the focused window, even though we have not yet finished the motion event
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010006 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010007 mWindow->finishEvent(*downSequenceNum);
10008 mWindow->finishEvent(*upSequenceNum);
10009}
10010
10011/**
10012 * If a window is processing a motion event, and then a key event comes in, the key event should
10013 * not go to the focused window until the motion is processed.
10014 * If then a new motion comes in, then the pending key event should be going to the currently
10015 * focused window right away.
10016 */
10017TEST_F(InputDispatcherSingleWindowAnr,
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010018 PendingKey_IsDeliveredWhileMotionIsProcessingAndNewTouchComesIn) {
10019 // The timeouts in this test are established by relying on the fact that the "key waiting for
10020 // events timeout" is equal to 500ms.
10021 ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010022 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010023 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010024
10025 tapOnWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010026 const auto& [downSequenceNum, _] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010027 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010028 const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010029 ASSERT_TRUE(upSequenceNum);
10030 // Don't finish the events yet, and send a key
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -080010031 mDispatcher->notifyKey(
10032 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
10033 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
10034 .build());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010035 // At this point, key is still pending, and should not be sent to the application yet.
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010036 mWindow->assertNoEvents(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010037
10038 // Now tap down again. It should cause the pending key to go to the focused window right away.
10039 tapOnWindow();
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -080010040 // Now that we tapped, we should receive the key immediately.
10041 // Since there's still room for slowness, we use 200ms, which is much less than
10042 // the "key waiting for events' timeout of 500ms minus the already waited 100ms duration.
10043 std::unique_ptr<InputEvent> keyEvent = mWindow->consume(200ms);
10044 ASSERT_NE(nullptr, keyEvent);
10045 ASSERT_EQ(InputEventType::KEY, keyEvent->getType());
10046 ASSERT_THAT(static_cast<KeyEvent&>(*keyEvent), WithKeyAction(AKEY_EVENT_ACTION_DOWN));
10047 // it doesn't matter that we haven't ack'd the other events yet. We can finish events in any
10048 // order.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010049 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
10050 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -080010051 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
10052 mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010053 mWindow->assertNoEvents();
10054}
10055
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -070010056/**
10057 * Send an event to the app and have the app not respond right away.
10058 * When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
10059 * So InputDispatcher will enqueue ACTION_CANCEL event as well.
10060 * At some point, the window becomes responsive again.
10061 * Ensure that subsequent events get dropped, and the next gesture is delivered.
10062 */
10063TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) {
10064 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10065 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
10066 .build());
10067
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010068 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -070010069 ASSERT_TRUE(sequenceNum);
10070 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
10071 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
10072
10073 mWindow->finishEvent(*sequenceNum);
10074 mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
10075 ASSERT_TRUE(mDispatcher->waitForIdle());
10076 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
10077
10078 // Now that the window is responsive, let's continue the gesture.
10079 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
10080 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
10081 .build());
10082
10083 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10084 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
10085 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
10086 .build());
10087
10088 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
10089 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
10090 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
10091 .build());
10092 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
10093 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
10094 .build());
10095 // We already canceled this pointer, so the window shouldn't get any new events.
10096 mWindow->assertNoEvents();
10097
10098 // Start another one.
10099 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10100 .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15))
10101 .build());
10102 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
10103}
10104
Prabir Pradhanfc364722024-02-08 17:51:20 +000010105// Send an event to the app and have the app not respond right away. Then remove the app window.
10106// When the window is removed, the dispatcher will cancel the events for that window.
10107// So InputDispatcher will enqueue ACTION_CANCEL event as well.
10108TEST_F(InputDispatcherSingleWindowAnr, AnrAfterWindowRemoval) {
10109 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010110 AINPUT_SOURCE_TOUCHSCREEN,
10111 ui::LogicalDisplayId::DEFAULT, {WINDOW_LOCATION}));
Prabir Pradhanfc364722024-02-08 17:51:20 +000010112
10113 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
10114 ASSERT_TRUE(sequenceNum);
10115
10116 // Remove the window, but the input channel should remain alive.
10117 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
10118
10119 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
10120 // Since the window was removed, Dispatcher does not know the PID associated with the window
10121 // anymore, so the policy is notified without the PID.
10122 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken(),
10123 /*pid=*/std::nullopt);
10124
10125 mWindow->finishEvent(*sequenceNum);
10126 // The cancellation was generated when the window was removed, along with the focus event.
10127 mWindow->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010128 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Prabir Pradhanfc364722024-02-08 17:51:20 +000010129 mWindow->consumeFocusEvent(false);
10130 ASSERT_TRUE(mDispatcher->waitForIdle());
10131 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt);
10132}
10133
10134// Send an event to the app and have the app not respond right away. Wait for the policy to be
10135// notified of the unresponsive window, then remove the app window.
10136TEST_F(InputDispatcherSingleWindowAnr, AnrFollowedByWindowRemoval) {
10137 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010138 AINPUT_SOURCE_TOUCHSCREEN,
10139 ui::LogicalDisplayId::DEFAULT, {WINDOW_LOCATION}));
Prabir Pradhanfc364722024-02-08 17:51:20 +000010140
10141 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
10142 ASSERT_TRUE(sequenceNum);
10143 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
10144 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
10145
10146 // Remove the window, but the input channel should remain alive.
10147 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
10148
10149 mWindow->finishEvent(*sequenceNum);
10150 // The cancellation was generated during the ANR, and the window lost focus when it was removed.
10151 mWindow->consumeMotionEvent(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010152 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
Prabir Pradhanfc364722024-02-08 17:51:20 +000010153 mWindow->consumeFocusEvent(false);
10154 ASSERT_TRUE(mDispatcher->waitForIdle());
10155 // Since the window was removed, Dispatcher does not know the PID associated with the window
10156 // becoming responsive, so the policy is notified without the PID.
10157 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt);
10158}
10159
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010160class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
10161 virtual void SetUp() override {
10162 InputDispatcherTest::SetUp();
10163
Chris Yea209fde2020-07-22 13:54:51 -070010164 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010165 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010166 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010167 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010168 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010169 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010170 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010171
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010172 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010173 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010174 mFocusedWindow->setDispatchingTimeout(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010175 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010176
10177 // Set focused application.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010178 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -070010179 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010180
10181 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010182 mDispatcher->onWindowInfosChanged(
10183 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010184 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010185 mFocusedWindow->consumeFocusEvent(true);
10186 }
10187
10188 virtual void TearDown() override {
10189 InputDispatcherTest::TearDown();
10190
10191 mUnfocusedWindow.clear();
10192 mFocusedWindow.clear();
10193 }
10194
10195protected:
Chris Yea209fde2020-07-22 13:54:51 -070010196 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010197 sp<FakeWindowHandle> mUnfocusedWindow;
10198 sp<FakeWindowHandle> mFocusedWindow;
10199 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
10200 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
10201 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
10202
10203 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
10204
10205 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
10206
10207private:
10208 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010209 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010210 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10211 ui::LogicalDisplayId::DEFAULT, location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010212 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010213 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10214 ui::LogicalDisplayId::DEFAULT, location));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010215 }
10216};
10217
10218// If we have 2 windows that are both unresponsive, the one with the shortest timeout
10219// should be ANR'd first.
10220TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010221 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010222 injectMotionEvent(*mDispatcher,
10223 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10224 AINPUT_SOURCE_TOUCHSCREEN)
10225 .pointer(PointerBuilder(0, ToolType::FINGER)
10226 .x(FOCUSED_WINDOW_LOCATION.x)
10227 .y(FOCUSED_WINDOW_LOCATION.y))
10228 .build()));
10229 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10230 injectMotionEvent(*mDispatcher,
10231 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
10232 AINPUT_SOURCE_TOUCHSCREEN)
10233 .pointer(PointerBuilder(0, ToolType::FINGER)
10234 .x(FOCUSED_WINDOW_LOCATION.x)
10235 .y(FOCUSED_WINDOW_LOCATION.y))
10236 .build()));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010237 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010238 mFocusedWindow->consumeMotionUp();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010239 mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010240 // We consumed all events, so no ANR
10241 ASSERT_TRUE(mDispatcher->waitForIdle());
10242 mFakePolicy->assertNotifyAnrWasNotCalled();
10243
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010244 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010245 injectMotionEvent(*mDispatcher,
10246 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10247 AINPUT_SOURCE_TOUCHSCREEN)
10248 .pointer(PointerBuilder(0, ToolType::FINGER)
10249 .x(FOCUSED_WINDOW_LOCATION.x)
10250 .y(FOCUSED_WINDOW_LOCATION.y))
10251 .build()));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010252 const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010253 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010254
10255 const std::chrono::duration timeout =
10256 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -080010257 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010258
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010259 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010260 mFocusedWindow->consumeMotionDown();
10261 // This cancel is generated because the connection was unresponsive
10262 mFocusedWindow->consumeMotionCancel();
10263 mFocusedWindow->assertNoEvents();
10264 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010265 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -080010266 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
10267 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010268 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010269}
10270
10271// If we have 2 windows with identical timeouts that are both unresponsive,
10272// it doesn't matter which order they should have ANR.
10273// But we should receive ANR for both.
10274TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
10275 // Set the timeout for unfocused window to match the focused window
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010276 mUnfocusedWindow->setDispatchingTimeout(
10277 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010278 mDispatcher->onWindowInfosChanged(
10279 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010280
10281 tapOnFocusedWindow();
10282 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010283 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010284 std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken(
10285 mFocusedWindow->getDispatchingTimeout(
10286 DISPATCHING_TIMEOUT)),
10287 mFakePolicy->getUnresponsiveWindowToken(0ms)};
10288
10289 ASSERT_THAT(anrConnectionTokens,
10290 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
10291 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010292
10293 ASSERT_TRUE(mDispatcher->waitForIdle());
10294 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010295
10296 mFocusedWindow->consumeMotionDown();
10297 mFocusedWindow->consumeMotionUp();
10298 mUnfocusedWindow->consumeMotionOutside();
10299
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010300 std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(),
10301 mFakePolicy->getResponsiveWindowToken()};
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010302
10303 // Both applications should be marked as responsive, in any order
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -070010304 ASSERT_THAT(responsiveTokens,
10305 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
10306 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010307 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010308}
10309
10310// If a window is already not responding, the second tap on the same window should be ignored.
10311// We should also log an error to account for the dropped event (not tested here).
10312// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
10313TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
10314 tapOnFocusedWindow();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010315 mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010316 // Receive the events, but don't respond
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010317 const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010318 ASSERT_TRUE(downEventSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010319 const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010320 ASSERT_TRUE(upEventSequenceNum);
10321 const std::chrono::duration timeout =
10322 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -080010323 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010324
10325 // Tap once again
10326 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010327 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010328 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10329 ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010330 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010331 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010332 FOCUSED_WINDOW_LOCATION));
10333 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
10334 // valid touch target
10335 mUnfocusedWindow->assertNoEvents();
10336
10337 // Consume the first tap
10338 mFocusedWindow->finishEvent(*downEventSequenceNum);
10339 mFocusedWindow->finishEvent(*upEventSequenceNum);
10340 ASSERT_TRUE(mDispatcher->waitForIdle());
10341 // The second tap did not go to the focused window
10342 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010343 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -080010344 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
10345 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010346 mFakePolicy->assertNotifyAnrWasNotCalled();
10347}
10348
10349// If you tap outside of all windows, there will not be ANR
10350TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010351 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010352 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10353 ui::LogicalDisplayId::DEFAULT, LOCATION_OUTSIDE_ALL_WINDOWS));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010354 ASSERT_TRUE(mDispatcher->waitForIdle());
10355 mFakePolicy->assertNotifyAnrWasNotCalled();
10356}
10357
10358// Since the focused window is paused, tapping on it should not produce any events
10359TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
10360 mFocusedWindow->setPaused(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010361 mDispatcher->onWindowInfosChanged(
10362 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010363
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010364 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010365 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
10366 ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010367
10368 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
10369 ASSERT_TRUE(mDispatcher->waitForIdle());
10370 // Should not ANR because the window is paused, and touches shouldn't go to it
10371 mFakePolicy->assertNotifyAnrWasNotCalled();
10372
10373 mFocusedWindow->assertNoEvents();
10374 mUnfocusedWindow->assertNoEvents();
10375}
10376
10377/**
10378 * 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 -070010379 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010380 * If a different window becomes focused at this time, the key should go to that window instead.
10381 *
10382 * Warning!!!
10383 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
10384 * and the injection timeout that we specify when injecting the key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010385 * We must have the injection timeout (100ms) be smaller than
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010386 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
10387 *
10388 * If that value changes, this test should also change.
10389 */
10390TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
10391 // Set a long ANR timeout to prevent it from triggering
10392 mFocusedWindow->setDispatchingTimeout(2s);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010393 mDispatcher->onWindowInfosChanged(
10394 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010395
10396 tapOnUnfocusedWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010397 const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010398 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010399 const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010400 ASSERT_TRUE(upSequenceNum);
10401 // Don't finish the events yet, and send a key
10402 // Injection will succeed because we will eventually give up and send the key to the focused
10403 // window even if motions are still being processed.
10404
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010405 InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010406 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
10407 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
Linnan Li13bf76a2024-05-05 19:18:02 +080010408 /*injectionTimeout=*/100ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010409 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010410 // Key will not be sent to the window, yet, because the window is still processing events
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010411 // and the key remains pending, waiting for the touch events to be processed.
10412 // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED
10413 // under the hood.
10414 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
10415 mFocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010416
10417 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -070010418 mFocusedWindow->setFocusable(false);
10419 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010420 mDispatcher->onWindowInfosChanged(
10421 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010422 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010423
10424 // Focus events should precede the key events
10425 mUnfocusedWindow->consumeFocusEvent(true);
10426 mFocusedWindow->consumeFocusEvent(false);
10427
10428 // Finish the tap events, which should unblock dispatcher
10429 mUnfocusedWindow->finishEvent(*downSequenceNum);
10430 mUnfocusedWindow->finishEvent(*upSequenceNum);
10431
10432 // Now that all queues are cleared and no backlog in the connections, the key event
10433 // can finally go to the newly focused "mUnfocusedWindow".
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010434 mUnfocusedWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010435 mFocusedWindow->assertNoEvents();
10436 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010437 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010438}
10439
10440// When the touch stream is split across 2 windows, and one of them does not respond,
10441// then ANR should be raised and the touch should be canceled for the unresponsive window.
10442// The other window should not be affected by that.
10443TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
10444 // Touch Window 1
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010445 mDispatcher->notifyMotion(
10446 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
10447 ui::LogicalDisplayId::DEFAULT, {FOCUSED_WINDOW_LOCATION}));
10448 mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010449
10450 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +000010451 mDispatcher->notifyMotion(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010452 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
10453 ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan678438e2023-04-13 19:32:51 +000010454 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010455
10456 const std::chrono::duration timeout =
10457 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -080010458 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010459
10460 mUnfocusedWindow->consumeMotionDown();
10461 mFocusedWindow->consumeMotionDown();
10462 // Focused window may or may not receive ACTION_MOVE
10463 // But it should definitely receive ACTION_CANCEL due to the ANR
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -080010464 const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010465 ASSERT_TRUE(moveOrCancelSequenceNum);
10466 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
10467 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -070010468 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010469 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
10470 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
10471 mFocusedWindow->consumeMotionCancel();
10472 } else {
10473 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
10474 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010475 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -080010476 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
10477 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010478
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010479 mUnfocusedWindow->assertNoEvents();
10480 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050010481 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -070010482}
10483
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010484/**
10485 * If we have no focused window, and a key comes in, we start the ANR timer.
10486 * The focused application should add a focused window before the timer runs out to prevent ANR.
10487 *
10488 * If the user touches another application during this time, the key should be dropped.
10489 * Next, if a new focused window comes in, without toggling the focused application,
10490 * then no ANR should occur.
10491 *
10492 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
10493 * but in some cases the policy may not update the focused application.
10494 */
10495TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
10496 std::shared_ptr<FakeApplicationHandle> focusedApplication =
10497 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouc033dfb2023-10-03 10:45:16 -070010498 focusedApplication->setDispatchingTimeout(300ms);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010499 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, focusedApplication);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010500 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
10501 mFocusedWindow->setFocusable(false);
10502
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010503 mDispatcher->onWindowInfosChanged(
10504 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010505 mFocusedWindow->consumeFocusEvent(false);
10506
10507 // Send a key. The ANR timer should start because there is no focused window.
10508 // 'focusedApplication' will get blamed if this timer completes.
10509 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010510 InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010511 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
10512 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
Linnan Li13bf76a2024-05-05 19:18:02 +080010513 /*injectionTimeout=*/100ms,
Harry Cutts33476232023-01-30 19:57:29 +000010514 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010515 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010516
10517 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
10518 // then the injected touches won't cause the focused event to get dropped.
10519 // The dispatcher only checks for whether the queue should be pruned upon queueing.
10520 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
10521 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
10522 // For this test, it means that the key would get delivered to the window once it becomes
10523 // focused.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010524 std::this_thread::sleep_for(100ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010525
10526 // Touch unfocused window. This should force the pending key to get dropped.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010527 mDispatcher->notifyMotion(
10528 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
10529 ui::LogicalDisplayId::DEFAULT, {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010530
10531 // We do not consume the motion right away, because that would require dispatcher to first
10532 // process (== drop) the key event, and by that time, ANR will be raised.
10533 // Set the focused window first.
10534 mFocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010535 mDispatcher->onWindowInfosChanged(
10536 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -050010537 setFocusedWindow(mFocusedWindow);
10538 mFocusedWindow->consumeFocusEvent(true);
10539 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
10540 // to another application. This could be a bug / behaviour in the policy.
10541
10542 mUnfocusedWindow->consumeMotionDown();
10543
10544 ASSERT_TRUE(mDispatcher->waitForIdle());
10545 // Should not ANR because we actually have a focused window. It was just added too slowly.
10546 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
10547}
10548
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010549/**
10550 * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having
10551 * an inconsistent event stream inside the dispatcher. In this test, we make sure that the
10552 * dispatcher doesn't prune pointer events incorrectly.
10553 *
10554 * This test reproduces a crash in InputDispatcher.
10555 * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur.
10556 *
10557 * Keep the currently focused application (mApplication), and have no focused window.
10558 * We set up two additional windows:
10559 * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the
10560 * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application
10561 * window. This window is not focusable, but is touchable.
10562 *
10563 * We first touch the navigation bar, which causes it to inject a key. Since there's no focused
10564 * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now
10565 * blocked. The dispatcher is waiting for 'mApplication' to add a focused window.
10566 *
10567 * Now, we touch "Another window". This window is owned by a different application than
10568 * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused
10569 * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start
10570 * dropping the events from its queue. Ensure that no crash occurs.
10571 *
10572 * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale.
10573 * This does not affect the test running time.
10574 */
10575TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) {
10576 std::shared_ptr<FakeApplicationHandle> systemUiApplication =
10577 std::make_shared<FakeApplicationHandle>();
10578 systemUiApplication->setDispatchingTimeout(3000ms);
10579 mFakePolicy->setStaleEventTimeout(3000ms);
10580 sp<FakeWindowHandle> navigationBar =
10581 sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010582 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010583 navigationBar->setFocusable(false);
10584 navigationBar->setWatchOutsideTouch(true);
10585 navigationBar->setFrame(Rect(0, 0, 100, 100));
10586
10587 mApplication->setDispatchingTimeout(3000ms);
10588 // 'mApplication' is already focused, but we call it again here to make it explicit.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010589 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication);
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010590
10591 std::shared_ptr<FakeApplicationHandle> anotherApplication =
10592 std::make_shared<FakeApplicationHandle>();
10593 sp<FakeWindowHandle> appWindow =
10594 sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010595 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010596 appWindow->setFocusable(false);
10597 appWindow->setFrame(Rect(100, 100, 200, 200));
10598
10599 mDispatcher->onWindowInfosChanged(
10600 {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0});
10601 // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus
10602 mFocusedWindow->consumeFocusEvent(false);
10603
10604 // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher
10605 // in response.
10606 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10607 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
10608 .build());
10609 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
10610
10611 // Key will not be sent anywhere because we have no focused window. It will remain pending.
10612 // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is.
10613 InputEventInjectionResult result =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010614 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
10615 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
Linnan Li13bf76a2024-05-05 19:18:02 +080010616 /*injectionTimeout=*/100ms,
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010617 /*allowKeyRepeat=*/false);
10618 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
10619
10620 // Finish the gesture - lift up finger and inject ACTION_UP key event
10621 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
10622 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
10623 .build());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010624 result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0,
10625 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE,
Linnan Li13bf76a2024-05-05 19:18:02 +080010626 /*injectionTimeout=*/100ms,
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -080010627 /*allowKeyRepeat=*/false);
10628 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
10629 // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be
10630 // getting any events yet.
10631 navigationBar->assertNoEvents();
10632
10633 // Now touch "Another window". This touch is going to a different application than the one we
10634 // are waiting for (which is 'mApplication').
10635 // This should cause the dispatcher to drop the pending focus-dispatched events (like the key
10636 // trying to be injected) and to continue processing the rest of the events in the original
10637 // order.
10638 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10639 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
10640 .build());
10641 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP));
10642 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE));
10643 appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
10644
10645 appWindow->assertNoEvents();
10646 navigationBar->assertNoEvents();
10647}
10648
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010649// These tests ensure we cannot send touch events to a window that's positioned behind a window
10650// that has feature NO_INPUT_CHANNEL.
10651// Layout:
10652// Top (closest to user)
10653// mNoInputWindow (above all windows)
10654// mBottomWindow
10655// Bottom (furthest from user)
10656class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
10657 virtual void SetUp() override {
10658 InputDispatcherTest::SetUp();
10659
10660 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010661 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
10662 "Window without input channel",
10663 ui::LogicalDisplayId::DEFAULT,
10664 /*createInputChannel=*/false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010665 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010666 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
10667 // It's perfectly valid for this window to not have an associated input channel
10668
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010669 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010670 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010671 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
10672
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010673 mDispatcher->onWindowInfosChanged(
10674 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010675 }
10676
10677protected:
10678 std::shared_ptr<FakeApplicationHandle> mApplication;
10679 sp<FakeWindowHandle> mNoInputWindow;
10680 sp<FakeWindowHandle> mBottomWindow;
10681};
10682
10683TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
10684 PointF touchedPoint = {10, 10};
10685
Prabir Pradhan678438e2023-04-13 19:32:51 +000010686 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010687 AINPUT_SOURCE_TOUCHSCREEN,
10688 ui::LogicalDisplayId::DEFAULT, {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010689
10690 mNoInputWindow->assertNoEvents();
10691 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
10692 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
10693 // and therefore should prevent mBottomWindow from receiving touches
10694 mBottomWindow->assertNoEvents();
10695}
10696
10697/**
10698 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
10699 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
10700 */
10701TEST_F(InputDispatcherMultiWindowOcclusionTests,
10702 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010703 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
10704 "Window with input channel and NO_INPUT_CHANNEL",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010705 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010706
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010707 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010708 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010709 mDispatcher->onWindowInfosChanged(
10710 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010711
10712 PointF touchedPoint = {10, 10};
10713
Prabir Pradhan678438e2023-04-13 19:32:51 +000010714 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010715 AINPUT_SOURCE_TOUCHSCREEN,
10716 ui::LogicalDisplayId::DEFAULT, {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -050010717
10718 mNoInputWindow->assertNoEvents();
10719 mBottomWindow->assertNoEvents();
10720}
10721
Vishnu Nair958da932020-08-21 17:12:37 -070010722class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
10723protected:
10724 std::shared_ptr<FakeApplicationHandle> mApp;
10725 sp<FakeWindowHandle> mWindow;
10726 sp<FakeWindowHandle> mMirror;
10727
10728 virtual void SetUp() override {
10729 InputDispatcherTest::SetUp();
10730 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010731 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow",
10732 ui::LogicalDisplayId::DEFAULT);
10733 mMirror = mWindow->clone(ui::LogicalDisplayId::DEFAULT);
10734 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp);
Vishnu Nair958da932020-08-21 17:12:37 -070010735 mWindow->setFocusable(true);
10736 mMirror->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010737 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010738 }
10739};
10740
10741TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
10742 // Request focus on a mirrored window
10743 setFocusedWindow(mMirror);
10744
10745 // window gets focused
10746 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010747 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010748 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010749 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010750}
10751
10752// A focused & mirrored window remains focused only if the window and its mirror are both
10753// focusable.
10754TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
10755 setFocusedWindow(mMirror);
10756
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +000010757 // window gets focused because it is above the mirror
Vishnu Nair958da932020-08-21 17:12:37 -070010758 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010759 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010760 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010761 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010762 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010763 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010764 mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010765
10766 mMirror->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010767 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010768
10769 // window loses focus since one of the windows associated with the token in not focusable
10770 mWindow->consumeFocusEvent(false);
10771
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010772 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010773 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -070010774 mWindow->assertNoEvents();
10775}
10776
10777// A focused & mirrored window remains focused until the window and its mirror both become
10778// invisible.
10779TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
10780 setFocusedWindow(mMirror);
10781
10782 // window gets focused
10783 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010784 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010785 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010786 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010787 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010788 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010789 mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010790
10791 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010792 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010793
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010794 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010795 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010796 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010797 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010798 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010799 mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010800
10801 mWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010802 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010803
10804 // window loses focus only after all windows associated with the token become invisible.
10805 mWindow->consumeFocusEvent(false);
10806
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010807 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010808 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -070010809 mWindow->assertNoEvents();
10810}
10811
10812// A focused & mirrored window remains focused until both windows are removed.
10813TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
10814 setFocusedWindow(mMirror);
10815
10816 // window gets focused
10817 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010818 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010819 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010820 mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010821 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010822 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010823 mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010824
10825 // single window is removed but the window token remains focused
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010826 mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010827
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010828 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010829 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010830 mMirror->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010831 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010832 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010833 mMirror->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Vishnu Nair958da932020-08-21 17:12:37 -070010834
10835 // Both windows are removed
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010836 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010837 mWindow->consumeFocusEvent(false);
10838
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010839 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010840 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -070010841 mWindow->assertNoEvents();
10842}
10843
10844// Focus request can be pending until one window becomes visible.
10845TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
10846 // Request focus on an invisible mirror.
10847 mWindow->setVisible(false);
10848 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010849 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010850 setFocusedWindow(mMirror);
10851
10852 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080010853 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010854 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010855 ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -070010856
10857 mMirror->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010858 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -070010859
10860 // window gets focused
10861 mWindow->consumeFocusEvent(true);
10862 // window gets the pending key event
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010863 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -070010864}
Prabir Pradhan99987712020-11-10 18:43:05 -080010865
10866class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
10867protected:
10868 std::shared_ptr<FakeApplicationHandle> mApp;
10869 sp<FakeWindowHandle> mWindow;
10870 sp<FakeWindowHandle> mSecondWindow;
10871
10872 void SetUp() override {
10873 InputDispatcherTest::SetUp();
10874 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010875 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow",
10876 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -080010877 mWindow->setFocusable(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010878 mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2",
10879 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -080010880 mSecondWindow->setFocusable(true);
10881
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070010882 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010883 mDispatcher->onWindowInfosChanged(
10884 {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan99987712020-11-10 18:43:05 -080010885
10886 setFocusedWindow(mWindow);
10887 mWindow->consumeFocusEvent(true);
10888 }
10889
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010890 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000010891 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -080010892 }
10893
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010894 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
10895 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -080010896 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Hiroki Sato25040232024-02-22 17:21:22 +090010897 auto request = mFakePolicy->assertSetPointerCaptureCalled(window, enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010898 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -080010899 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010900 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -080010901 }
10902};
10903
10904TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
10905 // Ensure that capture cannot be obtained for unfocused windows.
10906 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
10907 mFakePolicy->assertSetPointerCaptureNotCalled();
10908 mSecondWindow->assertNoEvents();
10909
10910 // Ensure that capture can be enabled from the focus window.
10911 requestAndVerifyPointerCapture(mWindow, true);
10912
10913 // Ensure that capture cannot be disabled from a window that does not have capture.
10914 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
10915 mFakePolicy->assertSetPointerCaptureNotCalled();
10916
10917 // Ensure that capture can be disabled from the window with capture.
10918 requestAndVerifyPointerCapture(mWindow, false);
10919}
10920
10921TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010922 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -080010923
10924 setFocusedWindow(mSecondWindow);
10925
10926 // Ensure that the capture disabled event was sent first.
10927 mWindow->consumeCaptureEvent(false);
10928 mWindow->consumeFocusEvent(false);
10929 mSecondWindow->consumeFocusEvent(true);
Hiroki Sato25040232024-02-22 17:21:22 +090010930 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan99987712020-11-10 18:43:05 -080010931
10932 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010933 notifyPointerCaptureChanged({});
10934 notifyPointerCaptureChanged(request);
10935 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -080010936 mWindow->assertNoEvents();
10937 mSecondWindow->assertNoEvents();
10938 mFakePolicy->assertSetPointerCaptureNotCalled();
10939}
10940
10941TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010942 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -080010943
10944 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010945 notifyPointerCaptureChanged({});
10946 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -080010947
10948 // Ensure that Pointer Capture is disabled.
Hiroki Sato25040232024-02-22 17:21:22 +090010949 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan99987712020-11-10 18:43:05 -080010950 mWindow->consumeCaptureEvent(false);
10951 mWindow->assertNoEvents();
10952}
10953
Prabir Pradhan167e6d92021-02-04 16:18:17 -080010954TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
10955 requestAndVerifyPointerCapture(mWindow, true);
10956
10957 // The first window loses focus.
10958 setFocusedWindow(mSecondWindow);
Hiroki Sato25040232024-02-22 17:21:22 +090010959 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -080010960 mWindow->consumeCaptureEvent(false);
10961
10962 // Request Pointer Capture from the second window before the notification from InputReader
10963 // arrives.
10964 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Hiroki Sato25040232024-02-22 17:21:22 +090010965 auto request = mFakePolicy->assertSetPointerCaptureCalled(mSecondWindow, true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -080010966
10967 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010968 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -080010969
10970 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010971 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -080010972
10973 mSecondWindow->consumeFocusEvent(true);
10974 mSecondWindow->consumeCaptureEvent(true);
10975}
10976
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010977TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
10978 // App repeatedly enables and disables capture.
10979 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
Hiroki Sato25040232024-02-22 17:21:22 +090010980 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010981 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
Hiroki Sato25040232024-02-22 17:21:22 +090010982 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010983 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
Hiroki Sato25040232024-02-22 17:21:22 +090010984 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000010985
10986 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
10987 // first request is now stale, this should do nothing.
10988 notifyPointerCaptureChanged(firstRequest);
10989 mWindow->assertNoEvents();
10990
10991 // InputReader notifies that the second request was enabled.
10992 notifyPointerCaptureChanged(secondRequest);
10993 mWindow->consumeCaptureEvent(true);
10994}
10995
Prabir Pradhan7092e262022-05-03 16:51:09 +000010996TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
10997 requestAndVerifyPointerCapture(mWindow, true);
10998
10999 // App toggles pointer capture off and on.
11000 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
Hiroki Sato25040232024-02-22 17:21:22 +090011001 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
Prabir Pradhan7092e262022-05-03 16:51:09 +000011002
11003 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
Hiroki Sato25040232024-02-22 17:21:22 +090011004 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true);
Prabir Pradhan7092e262022-05-03 16:51:09 +000011005
11006 // InputReader notifies that the latest "enable" request was processed, while skipping over the
11007 // preceding "disable" request.
11008 notifyPointerCaptureChanged(enableRequest);
11009
11010 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
11011 // any notifications.
11012 mWindow->assertNoEvents();
11013}
11014
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011015/**
11016 * One window. Hover mouse in the window, and then start capture. Make sure that the relative
11017 * mouse movements don't affect the previous mouse hovering state.
11018 * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no
11019 * HOVER_MOVE events).
11020 */
11021TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) {
11022 // Mouse hover on the window
11023 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
11024 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
11025 .build());
11026 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
11027 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
11028 .build());
11029
11030 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER)));
11031 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE)));
11032
11033 // Start pointer capture
11034 requestAndVerifyPointerCapture(mWindow, true);
11035
11036 // Send some relative mouse movements and receive them in the window.
11037 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE)
11038 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11))
11039 .build());
11040 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11),
11041 WithSource(AINPUT_SOURCE_MOUSE_RELATIVE)));
11042
11043 // Stop pointer capture
11044 requestAndVerifyPointerCapture(mWindow, false);
11045
11046 // Continue hovering on the window
11047 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
11048 .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115))
11049 .build());
11050 mWindow->consumeMotionEvent(
11051 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
11052
11053 mWindow->assertNoEvents();
11054}
11055
Prabir Pradhan9d3d5612024-06-06 20:34:26 +000011056TEST_F(InputDispatcherPointerCaptureTests, MultiDisplayPointerCapture) {
11057 // The default display is the focused display to begin with.
11058 requestAndVerifyPointerCapture(mWindow, true);
11059
11060 // Move the second window to a second display, make it the focused window on that display.
11061 mSecondWindow->editInfo()->displayId = SECOND_DISPLAY_ID;
11062 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
11063 setFocusedWindow(mSecondWindow);
11064 mSecondWindow->consumeFocusEvent(true);
11065
11066 mWindow->assertNoEvents();
11067
11068 // The second window cannot gain capture because it is not on the focused display.
11069 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
11070 mFakePolicy->assertSetPointerCaptureNotCalled();
11071 mSecondWindow->assertNoEvents();
11072
11073 // Make the second display the focused display.
11074 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
11075
11076 // This causes the first window to lose pointer capture, and it's unable to request capture.
11077 mWindow->consumeCaptureEvent(false);
11078 mFakePolicy->assertSetPointerCaptureCalled(mWindow, false);
11079
11080 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
11081 mFakePolicy->assertSetPointerCaptureNotCalled();
11082
11083 // The second window is now able to gain pointer capture successfully.
11084 requestAndVerifyPointerCapture(mSecondWindow, true);
11085}
11086
Hiroki Sato25040232024-02-22 17:21:22 +090011087using InputDispatcherPointerCaptureDeathTest = InputDispatcherPointerCaptureTests;
11088
11089TEST_F(InputDispatcherPointerCaptureDeathTest,
11090 NotifyPointerCaptureChangedWithWrongTokenAbortsDispatcher) {
11091 testing::GTEST_FLAG(death_test_style) = "threadsafe";
11092 ScopedSilentDeath _silentDeath;
11093
11094 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
11095 auto request = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true);
11096
11097 // Dispatch a pointer changed event with a wrong token.
11098 request.window = mSecondWindow->getToken();
11099 ASSERT_DEATH(
11100 {
11101 notifyPointerCaptureChanged(request);
11102 mSecondWindow->consumeCaptureEvent(true);
11103 },
11104 "Unexpected requested window for Pointer Capture.");
11105}
11106
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011107class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
11108protected:
11109 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +000011110
11111 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
11112 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
11113
11114 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
11115 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
11116
11117 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
11118 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
11119 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
11120 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
11121 MAXIMUM_OBSCURING_OPACITY);
11122
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011123 static constexpr gui::Uid TOUCHED_APP_UID{10001};
11124 static constexpr gui::Uid APP_B_UID{10002};
11125 static constexpr gui::Uid APP_C_UID{10003};
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011126
11127 sp<FakeWindowHandle> mTouchWindow;
11128
11129 virtual void SetUp() override {
11130 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011131 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011132 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
11133 }
11134
11135 virtual void TearDown() override {
11136 InputDispatcherTest::TearDown();
11137 mTouchWindow.clear();
11138 }
11139
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011140 sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode,
chaviw3277faf2021-05-19 16:45:23 -050011141 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011142 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011143 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011144 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011145 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011146 return window;
11147 }
11148
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011149 sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011150 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
11151 sp<FakeWindowHandle> window =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011152 sp<FakeWindowHandle>::make(app, mDispatcher, name, ui::LogicalDisplayId::DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011153 // Generate an arbitrary PID based on the UID
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011154 window->setOwnerInfo(gui::Pid{static_cast<pid_t>(1777 + (uid.val() % 10000))}, uid);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011155 return window;
11156 }
11157
11158 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000011159 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011160 AINPUT_SOURCE_TOUCHSCREEN,
11161 ui::LogicalDisplayId::DEFAULT, points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011162 }
11163};
11164
11165TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011166 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011167 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011168 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011169
11170 touch();
11171
11172 mTouchWindow->assertNoEvents();
11173}
11174
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011175TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +000011176 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
11177 const sp<FakeWindowHandle>& w =
11178 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011179 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000011180
11181 touch();
11182
11183 mTouchWindow->assertNoEvents();
11184}
11185
11186TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011187 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
11188 const sp<FakeWindowHandle>& w =
11189 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011190 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011191
11192 touch();
11193
11194 w->assertNoEvents();
11195}
11196
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011197TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011198 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011199 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011200
11201 touch();
11202
11203 mTouchWindow->consumeAnyMotionDown();
11204}
11205
11206TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011207 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011208 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011209 w->setFrame(Rect(0, 0, 50, 50));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011210 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011211
11212 touch({PointF{100, 100}});
11213
11214 mTouchWindow->consumeAnyMotionDown();
11215}
11216
11217TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011218 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011219 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011220 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011221
11222 touch();
11223
11224 mTouchWindow->consumeAnyMotionDown();
11225}
11226
11227TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
11228 const sp<FakeWindowHandle>& w =
11229 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011230 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011231
11232 touch();
11233
11234 mTouchWindow->consumeAnyMotionDown();
11235}
11236
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011237TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
11238 const sp<FakeWindowHandle>& w =
11239 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011240 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011241
11242 touch();
11243
11244 w->assertNoEvents();
11245}
11246
11247/**
11248 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
11249 * inside) while letting them pass-through. Note that even though touch passes through the occluding
11250 * window, the occluding window will still receive ACTION_OUTSIDE event.
11251 */
11252TEST_F(InputDispatcherUntrustedTouchesTest,
11253 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
11254 const sp<FakeWindowHandle>& w =
11255 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011256 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011257 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011258
11259 touch();
11260
11261 w->consumeMotionOutside();
11262}
11263
11264TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
11265 const sp<FakeWindowHandle>& w =
11266 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011267 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011268 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011269
11270 touch();
11271
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011272 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000011273}
11274
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011275TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011276 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011277 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11278 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011279 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011280
11281 touch();
11282
11283 mTouchWindow->consumeAnyMotionDown();
11284}
11285
11286TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
11287 const sp<FakeWindowHandle>& w =
11288 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11289 MAXIMUM_OBSCURING_OPACITY);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011290 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011291
11292 touch();
11293
11294 mTouchWindow->consumeAnyMotionDown();
11295}
11296
11297TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011298 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011299 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11300 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011301 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011302
11303 touch();
11304
11305 mTouchWindow->assertNoEvents();
11306}
11307
11308TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
11309 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
11310 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011311 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
11312 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011313 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011314 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
11315 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011316 mDispatcher->onWindowInfosChanged(
11317 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011318
11319 touch();
11320
11321 mTouchWindow->assertNoEvents();
11322}
11323
11324TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
11325 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
11326 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011327 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
11328 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011329 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011330 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
11331 OPACITY_FAR_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011332 mDispatcher->onWindowInfosChanged(
11333 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011334
11335 touch();
11336
11337 mTouchWindow->consumeAnyMotionDown();
11338}
11339
11340TEST_F(InputDispatcherUntrustedTouchesTest,
11341 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
11342 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011343 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11344 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011345 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011346 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
11347 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011348 mDispatcher->onWindowInfosChanged(
11349 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011350
11351 touch();
11352
11353 mTouchWindow->consumeAnyMotionDown();
11354}
11355
11356TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
11357 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011358 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11359 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000011360 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011361 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
11362 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011363 mDispatcher->onWindowInfosChanged(
11364 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000011365
11366 touch();
11367
11368 mTouchWindow->assertNoEvents();
11369}
11370
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011371TEST_F(InputDispatcherUntrustedTouchesTest,
11372 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
11373 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011374 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
11375 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011376 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011377 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11378 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011379 mDispatcher->onWindowInfosChanged(
11380 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011381
11382 touch();
11383
11384 mTouchWindow->assertNoEvents();
11385}
11386
11387TEST_F(InputDispatcherUntrustedTouchesTest,
11388 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
11389 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011390 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
11391 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011392 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011393 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11394 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011395 mDispatcher->onWindowInfosChanged(
11396 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011397
11398 touch();
11399
11400 mTouchWindow->consumeAnyMotionDown();
11401}
11402
11403TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
11404 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011405 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
11406 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011407 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011408
11409 touch();
11410
11411 mTouchWindow->consumeAnyMotionDown();
11412}
11413
11414TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
11415 const sp<FakeWindowHandle>& w =
11416 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011417 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000011418
11419 touch();
11420
11421 mTouchWindow->consumeAnyMotionDown();
11422}
11423
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000011424TEST_F(InputDispatcherUntrustedTouchesTest,
11425 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
11426 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
11427 const sp<FakeWindowHandle>& w =
11428 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011429 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000011430
11431 touch();
11432
11433 mTouchWindow->assertNoEvents();
11434}
11435
11436TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
11437 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
11438 const sp<FakeWindowHandle>& w =
11439 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011440 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000011441
11442 touch();
11443
11444 mTouchWindow->consumeAnyMotionDown();
11445}
11446
11447TEST_F(InputDispatcherUntrustedTouchesTest,
11448 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
11449 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
11450 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000011451 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11452 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011453 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000011454
11455 touch();
11456
11457 mTouchWindow->consumeAnyMotionDown();
11458}
11459
11460TEST_F(InputDispatcherUntrustedTouchesTest,
11461 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
11462 const sp<FakeWindowHandle>& w1 =
11463 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
11464 OPACITY_BELOW_THRESHOLD);
11465 const sp<FakeWindowHandle>& w2 =
11466 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
11467 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011468 mDispatcher->onWindowInfosChanged(
11469 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000011470
11471 touch();
11472
11473 mTouchWindow->assertNoEvents();
11474}
11475
11476/**
11477 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
11478 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
11479 * (which alone would result in allowing touches) does not affect the blocking behavior.
11480 */
11481TEST_F(InputDispatcherUntrustedTouchesTest,
11482 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
11483 const sp<FakeWindowHandle>& wB =
11484 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
11485 OPACITY_BELOW_THRESHOLD);
11486 const sp<FakeWindowHandle>& wC =
11487 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
11488 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011489 mDispatcher->onWindowInfosChanged(
11490 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000011491
11492 touch();
11493
11494 mTouchWindow->assertNoEvents();
11495}
11496
11497/**
11498 * This test is testing that a window from a different UID but with same application token doesn't
11499 * block the touch. Apps can share the application token for close UI collaboration for example.
11500 */
11501TEST_F(InputDispatcherUntrustedTouchesTest,
11502 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
11503 const sp<FakeWindowHandle>& w =
11504 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
11505 w->setApplicationToken(mTouchWindow->getApplicationToken());
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011506 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000011507
11508 touch();
11509
11510 mTouchWindow->consumeAnyMotionDown();
11511}
11512
arthurhungb89ccb02020-12-30 16:19:01 +080011513class InputDispatcherDragTests : public InputDispatcherTest {
11514protected:
11515 std::shared_ptr<FakeApplicationHandle> mApp;
11516 sp<FakeWindowHandle> mWindow;
11517 sp<FakeWindowHandle> mSecondWindow;
11518 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011519 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011520 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
11521 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +080011522
11523 void SetUp() override {
11524 InputDispatcherTest::SetUp();
11525 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011526 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow",
11527 ui::LogicalDisplayId::DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +080011528 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +080011529
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011530 mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2",
11531 ui::LogicalDisplayId::DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +080011532 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +080011533
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011534 mSpyWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow",
11535 ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011536 mSpyWindow->setSpy(true);
11537 mSpyWindow->setTrustedOverlay(true);
11538 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
11539
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011540 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011541 mDispatcher->onWindowInfosChanged(
11542 {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()},
11543 {},
11544 0,
11545 0});
arthurhungb89ccb02020-12-30 16:19:01 +080011546 }
11547
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011548 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
11549 switch (fromSource) {
11550 case AINPUT_SOURCE_TOUCHSCREEN:
11551 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011552 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011553 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011554 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11555 break;
11556 case AINPUT_SOURCE_STYLUS:
11557 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011558 injectMotionEvent(*mDispatcher,
11559 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
11560 AINPUT_SOURCE_STYLUS)
11561 .buttonState(
11562 AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
11563 .pointer(PointerBuilder(0, ToolType::STYLUS)
11564 .x(50)
11565 .y(50))
11566 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011567 break;
11568 case AINPUT_SOURCE_MOUSE:
11569 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011570 injectMotionEvent(*mDispatcher,
11571 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
11572 AINPUT_SOURCE_MOUSE)
11573 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
11574 .pointer(PointerBuilder(MOUSE_POINTER_ID,
11575 ToolType::MOUSE)
11576 .x(50)
11577 .y(50))
11578 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011579 break;
11580 default:
11581 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
11582 }
arthurhungb89ccb02020-12-30 16:19:01 +080011583
11584 // Window should receive motion event.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011585 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011586 // Spy window should also receive motion event
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011587 mSpyWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000011588 }
11589
11590 // Start performing drag, we will create a drag window and transfer touch to it.
11591 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
11592 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011593 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +000011594 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011595 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +000011596 }
arthurhungb89ccb02020-12-30 16:19:01 +080011597
11598 // The drag window covers the entire display
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011599 mDragWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow",
11600 ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011601 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011602 mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(),
11603 *mWindow->getInfo(), *mSecondWindow->getInfo()},
11604 {},
11605 0,
11606 0});
arthurhungb89ccb02020-12-30 16:19:01 +080011607
11608 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +000011609 bool transferred =
Prabir Pradhan367f3432024-02-13 23:05:58 +000011610 mDispatcher->transferTouchGesture(mWindow->getToken(), mDragWindow->getToken(),
11611 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +000011612 if (transferred) {
11613 mWindow->consumeMotionCancel();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011614 mDragWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
11615 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung54745652022-04-20 07:17:41 +000011616 }
11617 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +080011618 }
11619};
11620
11621TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011622 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +080011623
11624 // Move on window.
11625 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011626 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011627 ui::LogicalDisplayId::DEFAULT, {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080011628 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011629 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11630 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080011631 mWindow->consumeDragEvent(false, 50, 50);
11632 mSecondWindow->assertNoEvents();
11633
11634 // Move to another window.
11635 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011636 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011637 ui::LogicalDisplayId::DEFAULT, {150, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080011638 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011639 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11640 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080011641 mWindow->consumeDragEvent(true, 150, 50);
11642 mSecondWindow->consumeDragEvent(false, 50, 50);
11643
11644 // Move back to original window.
11645 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011646 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011647 ui::LogicalDisplayId::DEFAULT, {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080011648 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011649 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11650 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080011651 mWindow->consumeDragEvent(false, 50, 50);
11652 mSecondWindow->consumeDragEvent(true, -50, 50);
11653
11654 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011655 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011656 {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080011657 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011658 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080011659 mWindow->assertNoEvents();
11660 mSecondWindow->assertNoEvents();
11661}
11662
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011663TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011664 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011665
11666 // No cancel event after drag start
11667 mSpyWindow->assertNoEvents();
11668
11669 const MotionEvent secondFingerDownEvent =
11670 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11671 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011672 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11673 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011674 .build();
11675 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011676 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000011677 InputEventInjectionSync::WAIT_FOR_RESULT))
11678 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11679
11680 // Receives cancel for first pointer after next pointer down
11681 mSpyWindow->consumeMotionCancel();
11682 mSpyWindow->consumeMotionDown();
11683
11684 mSpyWindow->assertNoEvents();
11685}
11686
arthurhungf452d0b2021-01-06 00:19:52 +080011687TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011688 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +080011689
11690 // Move on window.
11691 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011692 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011693 ui::LogicalDisplayId::DEFAULT, {50, 50}))
arthurhungf452d0b2021-01-06 00:19:52 +080011694 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011695 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11696 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungf452d0b2021-01-06 00:19:52 +080011697 mWindow->consumeDragEvent(false, 50, 50);
11698 mSecondWindow->assertNoEvents();
11699
11700 // Move to another window.
11701 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011702 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011703 ui::LogicalDisplayId::DEFAULT, {150, 50}))
arthurhungf452d0b2021-01-06 00:19:52 +080011704 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011705 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11706 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungf452d0b2021-01-06 00:19:52 +080011707 mWindow->consumeDragEvent(true, 150, 50);
11708 mSecondWindow->consumeDragEvent(false, 50, 50);
11709
11710 // drop to another window.
11711 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011712 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
arthurhungf452d0b2021-01-06 00:19:52 +080011713 {150, 50}))
11714 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011715 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070011716 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhungf452d0b2021-01-06 00:19:52 +080011717 mWindow->assertNoEvents();
11718 mSecondWindow->assertNoEvents();
11719}
11720
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011721TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) {
11722 startDrag();
11723
11724 // No cancel event after drag start
11725 mSpyWindow->assertNoEvents();
11726
11727 const MotionEvent secondFingerDownEvent =
11728 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11729 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
11730 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11731 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
11732 .build();
11733 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
11734 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
11735 InputEventInjectionSync::WAIT_FOR_RESULT))
11736 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11737
11738 // Receives cancel for first pointer after next pointer down
11739 mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -080011740 mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1})));
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011741 mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
11742
11743 mSpyWindow->assertNoEvents();
11744
11745 // Spy window calls pilfer pointers
11746 EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken()));
11747 mDragWindow->assertNoEvents();
11748
11749 const MotionEvent firstFingerMoveEvent =
Siarhei Vishniakou96a15572023-12-18 10:47:52 -080011750 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011751 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
11752 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60))
11753 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
11754 .build();
11755 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou96a15572023-12-18 10:47:52 -080011756 injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011757 InputEventInjectionSync::WAIT_FOR_RESULT))
11758 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11759
11760 // Drag window should still receive the new event
Prabir Pradhan65455c72024-02-13 21:46:41 +000011761 mDragWindow->consumeMotionEvent(
11762 AllOf(WithMotionAction(ACTION_MOVE), WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)));
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000011763 mDragWindow->assertNoEvents();
11764}
11765
arthurhung6d4bed92021-03-17 11:59:33 +080011766TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011767 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +080011768
11769 // Move on window and keep button pressed.
11770 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011771 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080011772 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
11773 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011774 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080011775 .build()))
11776 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011777 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11778 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhung6d4bed92021-03-17 11:59:33 +080011779 mWindow->consumeDragEvent(false, 50, 50);
11780 mSecondWindow->assertNoEvents();
11781
11782 // Move to another window and release button, expect to drop item.
11783 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011784 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080011785 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
11786 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011787 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080011788 .build()))
11789 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011790 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11791 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhung6d4bed92021-03-17 11:59:33 +080011792 mWindow->assertNoEvents();
11793 mSecondWindow->assertNoEvents();
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070011794 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhung6d4bed92021-03-17 11:59:33 +080011795
11796 // nothing to the window.
11797 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011798 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080011799 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
11800 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011801 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080011802 .build()))
11803 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011804 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhung6d4bed92021-03-17 11:59:33 +080011805 mWindow->assertNoEvents();
11806 mSecondWindow->assertNoEvents();
11807}
11808
Arthur Hung54745652022-04-20 07:17:41 +000011809TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011810 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +080011811
11812 // Set second window invisible.
11813 mSecondWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011814 mDispatcher->onWindowInfosChanged(
11815 {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Arthur Hung6d0571e2021-04-09 20:18:16 +080011816
11817 // Move on window.
11818 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011819 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011820 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hung6d0571e2021-04-09 20:18:16 +080011821 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011822 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11823 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung6d0571e2021-04-09 20:18:16 +080011824 mWindow->consumeDragEvent(false, 50, 50);
11825 mSecondWindow->assertNoEvents();
11826
11827 // Move to another window.
11828 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011829 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011830 ui::LogicalDisplayId::DEFAULT, {150, 50}))
Arthur Hung6d0571e2021-04-09 20:18:16 +080011831 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011832 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11833 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung6d0571e2021-04-09 20:18:16 +080011834 mWindow->consumeDragEvent(true, 150, 50);
11835 mSecondWindow->assertNoEvents();
11836
11837 // drop to another window.
11838 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011839 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Arthur Hung6d0571e2021-04-09 20:18:16 +080011840 {150, 50}))
11841 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011842 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070011843 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
Arthur Hung6d0571e2021-04-09 20:18:16 +080011844 mWindow->assertNoEvents();
11845 mSecondWindow->assertNoEvents();
11846}
11847
Arthur Hung54745652022-04-20 07:17:41 +000011848TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011849 // Ensure window could track pointerIds if it didn't support split touch.
11850 mWindow->setPreventSplitting(true);
11851
Arthur Hung54745652022-04-20 07:17:41 +000011852 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011853 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
11854 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hung54745652022-04-20 07:17:41 +000011855 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011856 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000011857
11858 const MotionEvent secondFingerDownEvent =
11859 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011860 .displayId(ui::LogicalDisplayId::DEFAULT)
Arthur Hung54745652022-04-20 07:17:41 +000011861 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011862 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11863 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000011864 .build();
11865 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011866 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000011867 InputEventInjectionSync::WAIT_FOR_RESULT))
11868 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000011869 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +000011870
11871 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011872 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +000011873}
11874
11875TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
11876 // First down on second window.
11877 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011878 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
11879 ui::LogicalDisplayId::DEFAULT, {150, 50}))
Arthur Hung54745652022-04-20 07:17:41 +000011880 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11881
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011882 mSecondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000011883
11884 // Second down on first window.
11885 const MotionEvent secondFingerDownEvent =
11886 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011887 .displayId(ui::LogicalDisplayId::DEFAULT)
Arthur Hung54745652022-04-20 07:17:41 +000011888 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011889 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
11890 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000011891 .build();
11892 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011893 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000011894 InputEventInjectionSync::WAIT_FOR_RESULT))
11895 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011896 mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
11897 mSecondWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000011898
11899 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011900 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +000011901
11902 // Move on window.
11903 const MotionEvent secondFingerMoveEvent =
11904 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
11905 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011906 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
11907 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000011908 .build();
11909 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011910 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000011911 InputEventInjectionSync::WAIT_FOR_RESULT));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011912 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11913 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung54745652022-04-20 07:17:41 +000011914 mWindow->consumeDragEvent(false, 50, 50);
11915 mSecondWindow->consumeMotionMove();
11916
11917 // Release the drag pointer should perform drop.
11918 const MotionEvent secondFingerUpEvent =
11919 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
11920 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011921 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
11922 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000011923 .build();
11924 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011925 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000011926 InputEventInjectionSync::WAIT_FOR_RESULT));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011927 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070011928 mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken());
Arthur Hung54745652022-04-20 07:17:41 +000011929 mWindow->assertNoEvents();
11930 mSecondWindow->consumeMotionMove();
11931}
11932
Arthur Hung3915c1f2022-05-31 07:17:17 +000011933TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011934 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +000011935
11936 // Update window of second display.
11937 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011938 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011939 mDispatcher->onWindowInfosChanged(
11940 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
11941 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
11942 {},
11943 0,
11944 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +000011945
11946 // Let second display has a touch state.
11947 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011948 injectMotionEvent(*mDispatcher,
Arthur Hung3915c1f2022-05-31 07:17:17 +000011949 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
11950 AINPUT_SOURCE_TOUCHSCREEN)
11951 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011952 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +000011953 .build()));
Prabir Pradhan7662a8d2023-12-15 01:58:14 +000011954 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +000011955 // Update window again.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011956 mDispatcher->onWindowInfosChanged(
11957 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
11958 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
11959 {},
11960 0,
11961 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +000011962
11963 // Move on window.
11964 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011965 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011966 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Arthur Hung3915c1f2022-05-31 07:17:17 +000011967 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011968 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11969 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung3915c1f2022-05-31 07:17:17 +000011970 mWindow->consumeDragEvent(false, 50, 50);
11971 mSecondWindow->assertNoEvents();
11972
11973 // Move to another window.
11974 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011975 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011976 ui::LogicalDisplayId::DEFAULT, {150, 50}))
Arthur Hung3915c1f2022-05-31 07:17:17 +000011977 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011978 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
11979 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung3915c1f2022-05-31 07:17:17 +000011980 mWindow->consumeDragEvent(true, 150, 50);
11981 mSecondWindow->consumeDragEvent(false, 50, 50);
11982
11983 // drop to another window.
11984 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011985 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Arthur Hung3915c1f2022-05-31 07:17:17 +000011986 {150, 50}))
11987 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070011988 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070011989 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hung3915c1f2022-05-31 07:17:17 +000011990 mWindow->assertNoEvents();
11991 mSecondWindow->assertNoEvents();
11992}
11993
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011994TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
11995 startDrag(true, AINPUT_SOURCE_MOUSE);
11996 // Move on window.
11997 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011998 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000011999 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
12000 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012001 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012002 .x(50)
12003 .y(50))
12004 .build()))
12005 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012006 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
12007 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012008 mWindow->consumeDragEvent(false, 50, 50);
12009 mSecondWindow->assertNoEvents();
12010
12011 // Move to another window.
12012 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012013 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012014 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
12015 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012016 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012017 .x(150)
12018 .y(50))
12019 .build()))
12020 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012021 mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT,
12022 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012023 mWindow->consumeDragEvent(true, 150, 50);
12024 mSecondWindow->consumeDragEvent(false, 50, 50);
12025
12026 // drop to another window.
12027 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012028 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012029 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
12030 .buttonState(0)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012031 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012032 .x(150)
12033 .y(50))
12034 .build()))
12035 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012036 mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070012037 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hungb75c2aa2022-07-15 09:35:36 +000012038 mWindow->assertNoEvents();
12039 mSecondWindow->assertNoEvents();
12040}
12041
Linnan Li5af92f92023-07-14 14:36:22 +080012042/**
12043 * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag
12044 * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash.
12045 */
12046TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) {
12047 // Down on second window
12048 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012049 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12050 ui::LogicalDisplayId::DEFAULT, {150, 50}))
Linnan Li5af92f92023-07-14 14:36:22 +080012051 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12052
12053 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown());
12054 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown());
12055
12056 // Down on first window
12057 const MotionEvent secondFingerDownEvent =
12058 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012059 .displayId(ui::LogicalDisplayId::DEFAULT)
Linnan Li5af92f92023-07-14 14:36:22 +080012060 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
12061 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
12062 .build();
12063 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
12064 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
12065 InputEventInjectionSync::WAIT_FOR_RESULT))
12066 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12067 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
12068 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove());
12069 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1));
12070
12071 // Start drag on first window
12072 ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN));
12073
12074 // Trigger cancel
12075 mDispatcher->cancelCurrentTouch();
12076 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012077 ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan65455c72024-02-13 21:46:41 +000012078 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE));
Linnan Li5af92f92023-07-14 14:36:22 +080012079 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel());
12080
12081 ASSERT_TRUE(mDispatcher->waitForIdle());
12082 // The D&D finished with nullptr
12083 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
12084
12085 // Remove drag window
12086 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
12087
12088 // Inject a simple gesture, ensure dispatcher not crashed
12089 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012090 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12091 ui::LogicalDisplayId::DEFAULT, PointF{50, 50}))
Linnan Li5af92f92023-07-14 14:36:22 +080012092 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12093 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
12094
12095 const MotionEvent moveEvent =
12096 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012097 .displayId(ui::LogicalDisplayId::DEFAULT)
Linnan Li5af92f92023-07-14 14:36:22 +080012098 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12099 .build();
12100 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
12101 injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT,
12102 InputEventInjectionSync::WAIT_FOR_RESULT))
12103 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12104 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove());
12105
12106 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012107 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Linnan Li5af92f92023-07-14 14:36:22 +080012108 {50, 50}))
12109 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12110 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp());
12111}
12112
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000012113TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) {
12114 // Start hovering over the window.
12115 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
12116 injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012117 ui::LogicalDisplayId::DEFAULT, {50, 50}));
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000012118
12119 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)));
12120 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)));
12121
12122 ASSERT_FALSE(startDrag(/*sendDown=*/false))
12123 << "Drag and drop should not work with a hovering pointer";
12124}
12125
Vishnu Nair062a8672021-09-03 16:07:44 -070012126class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
12127
12128TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
12129 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012130 sp<FakeWindowHandle> window =
12131 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
12132 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012133 window->setDropInput(true);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012134 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair062a8672021-09-03 16:07:44 -070012135 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012136 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012137 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000012138 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070012139
12140 // With the flag set, window should not get any input
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012141 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012142 window->assertNoEvents();
12143
Prabir Pradhan678438e2023-04-13 19:32:51 +000012144 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012145 AINPUT_SOURCE_TOUCHSCREEN,
12146 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070012147 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012148 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -080012149 mDispatcher->waitForIdle();
Vishnu Nair062a8672021-09-03 16:07:44 -070012150 window->assertNoEvents();
12151
12152 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012153 window->setDropInput(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012154 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012155
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012156 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT));
12157 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012158
Prabir Pradhan678438e2023-04-13 19:32:51 +000012159 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012160 AINPUT_SOURCE_TOUCHSCREEN,
12161 ui::LogicalDisplayId::DEFAULT));
12162 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012163 window->assertNoEvents();
12164}
12165
12166TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
12167 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
12168 std::make_shared<FakeApplicationHandle>();
12169 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070012170 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012171 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012172 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012173 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012174 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070012175 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012176 sp<FakeWindowHandle> window =
12177 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
12178 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012179 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012180 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012181 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair062a8672021-09-03 16:07:44 -070012182 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012183 mDispatcher->onWindowInfosChanged(
12184 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012185 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000012186 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070012187
12188 // With the flag set, window should not get any input
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012189 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012190 window->assertNoEvents();
12191
Prabir Pradhan678438e2023-04-13 19:32:51 +000012192 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012193 AINPUT_SOURCE_TOUCHSCREEN,
12194 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070012195 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012196 ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012197 window->assertNoEvents();
12198
12199 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012200 window->setDropInputIfObscured(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012201 mDispatcher->onWindowInfosChanged(
12202 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012203
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012204 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT));
12205 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012206
Prabir Pradhan678438e2023-04-13 19:32:51 +000012207 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012208 AINPUT_SOURCE_TOUCHSCREEN,
12209 ui::LogicalDisplayId::DEFAULT));
12210 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT,
12211 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
Vishnu Nair062a8672021-09-03 16:07:44 -070012212 window->assertNoEvents();
12213}
12214
12215TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
12216 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
12217 std::make_shared<FakeApplicationHandle>();
12218 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070012219 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012220 ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012221 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012222 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012223 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070012224 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012225 sp<FakeWindowHandle> window =
12226 sp<FakeWindowHandle>::make(application, mDispatcher, "Test window",
12227 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012228 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012229 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012230 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Vishnu Nair062a8672021-09-03 16:07:44 -070012231 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012232 mDispatcher->onWindowInfosChanged(
12233 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012234 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000012235 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070012236
12237 // With the flag set, window should not get any input
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012238 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012239 window->assertNoEvents();
12240
Prabir Pradhan678438e2023-04-13 19:32:51 +000012241 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012242 AINPUT_SOURCE_TOUCHSCREEN,
12243 ui::LogicalDisplayId::DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070012244 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012245 ui::LogicalDisplayId::DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070012246 window->assertNoEvents();
12247
12248 // When the window is no longer obscured because it went on top, it should get input
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012249 mDispatcher->onWindowInfosChanged(
12250 {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070012251
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012252 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT));
12253 window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012254
Prabir Pradhan678438e2023-04-13 19:32:51 +000012255 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012256 AINPUT_SOURCE_TOUCHSCREEN,
12257 ui::LogicalDisplayId::DEFAULT));
12258 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070012259 window->assertNoEvents();
12260}
12261
Antonio Kantekf16f2832021-09-28 04:39:20 +000012262class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
12263protected:
12264 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +000012265 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +000012266 sp<FakeWindowHandle> mWindow;
12267 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +000012268 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +000012269
12270 void SetUp() override {
12271 InputDispatcherTest::SetUp();
12272
12273 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +000012274 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012275 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow",
12276 ui::LogicalDisplayId::DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012277 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000012278 setFocusedWindow(mWindow);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012279 mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2",
12280 ui::LogicalDisplayId::DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012281 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +000012282 mThirdWindow =
12283 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
12284 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
12285 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012286
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012287 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012288 mDispatcher->onWindowInfosChanged(
12289 {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()},
12290 {},
12291 0,
12292 0});
Antonio Kantek15beb512022-06-13 22:35:41 +000012293 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012294 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000012295
Antonio Kantek15beb512022-06-13 22:35:41 +000012296 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +000012297 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012298 WINDOW_UID, /*hasPermission=*/true,
12299 ui::LogicalDisplayId::DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -070012300 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
12301 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000012302 mThirdWindow->assertNoEvents();
12303 }
12304
12305 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
12306 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +000012307 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +000012308 SECOND_DISPLAY_ID)) {
12309 mWindow->assertNoEvents();
12310 mSecondWindow->assertNoEvents();
12311 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -070012312 }
Antonio Kantekf16f2832021-09-28 04:39:20 +000012313 }
12314
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012315 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Antonio Kantek15beb512022-06-13 22:35:41 +000012316 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -070012317 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012318 ui::LogicalDisplayId::DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000012319 mWindow->consumeTouchModeEvent(inTouchMode);
12320 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000012321 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +000012322 }
12323};
12324
Antonio Kantek26defcf2022-02-08 01:12:27 +000012325TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080012326 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +000012327 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
12328 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000012329 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +000012330}
12331
Antonio Kantek26defcf2022-02-08 01:12:27 +000012332TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
12333 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012334 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000012335 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012336 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek26defcf2022-02-08 01:12:27 +000012337 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000012338 ownerUid, /*hasPermission=*/false,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012339 ui::LogicalDisplayId::DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +000012340 mWindow->assertNoEvents();
12341 mSecondWindow->assertNoEvents();
12342}
12343
12344TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
12345 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012346 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000012347 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012348 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek15beb512022-06-13 22:35:41 +000012349 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000012350 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000012351}
12352
Antonio Kantekf16f2832021-09-28 04:39:20 +000012353TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080012354 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +000012355 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
12356 windowInfo.ownerPid, windowInfo.ownerUid,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012357 /*hasPermission=*/true,
12358 ui::LogicalDisplayId::DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000012359 mWindow->assertNoEvents();
12360 mSecondWindow->assertNoEvents();
12361}
12362
Antonio Kantek15beb512022-06-13 22:35:41 +000012363TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
12364 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
12365 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
12366 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000012367 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +000012368 mWindow->assertNoEvents();
12369 mSecondWindow->assertNoEvents();
12370 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
12371}
12372
Antonio Kantek48710e42022-03-24 14:19:30 -070012373TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
12374 // Interact with the window first.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012375 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012376 injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT))
Antonio Kantek48710e42022-03-24 14:19:30 -070012377 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012378 mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
Antonio Kantek48710e42022-03-24 14:19:30 -070012379
12380 // Then remove focus.
12381 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012382 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Antonio Kantek48710e42022-03-24 14:19:30 -070012383
12384 // Assert that caller can switch touch mode by owning one of the last interacted window.
12385 const WindowInfo& windowInfo = *mWindow->getInfo();
12386 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
12387 windowInfo.ownerPid, windowInfo.ownerUid,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012388 /*hasPermission=*/false,
12389 ui::LogicalDisplayId::DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -070012390}
12391
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012392class InputDispatcherSpyWindowTest : public InputDispatcherTest {
12393public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012394 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012395 std::shared_ptr<FakeApplicationHandle> application =
12396 std::make_shared<FakeApplicationHandle>();
12397 std::string name = "Fake Spy ";
12398 name += std::to_string(mSpyCount++);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012399 sp<FakeWindowHandle> spy =
12400 sp<FakeWindowHandle>::make(application, mDispatcher, name.c_str(),
12401 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012402 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -080012403 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012404 return spy;
12405 }
12406
12407 sp<FakeWindowHandle> createForeground() {
12408 std::shared_ptr<FakeApplicationHandle> application =
12409 std::make_shared<FakeApplicationHandle>();
12410 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070012411 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012412 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012413 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012414 return window;
12415 }
12416
12417private:
12418 int mSpyCount{0};
12419};
12420
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080012421using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012422/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -080012423 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
12424 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080012425TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
Siarhei Vishniakou21f77bd2023-07-18 17:51:35 -070012426 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080012427 ScopedSilentDeath _silentDeath;
12428
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012429 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -080012430 spy->setTrustedOverlay(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012431 ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}),
Prabir Pradhan5c85e052021-12-22 02:27:12 -080012432 ".* not a trusted overlay");
12433}
12434
12435/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012436 * Input injection into a display with a spy window but no foreground windows should succeed.
12437 */
12438TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012439 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012440 mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012441
12442 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012443 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12444 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012445 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012446 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012447}
12448
12449/**
12450 * Verify the order in which different input windows receive events. The touched foreground window
12451 * (if there is one) should always receive the event first. When there are multiple spy windows, the
12452 * spy windows will receive the event according to their Z-order, where the top-most spy window will
12453 * receive events before ones belows it.
12454 *
12455 * Here, we set up a scenario with four windows in the following Z order from the top:
12456 * spy1, spy2, window, spy3.
12457 * We then inject an event and verify that the foreground "window" receives it first, followed by
12458 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
12459 * window.
12460 */
12461TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
12462 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012463 auto spy1 = createSpy();
12464 auto spy2 = createSpy();
12465 auto spy3 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012466 mDispatcher->onWindowInfosChanged(
12467 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012468 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
12469 const size_t numChannels = channels.size();
12470
Michael Wright8e9a8562022-02-09 13:44:29 +000012471 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012472 if (!epollFd.ok()) {
12473 FAIL() << "Failed to create epoll fd";
12474 }
12475
12476 for (size_t i = 0; i < numChannels; i++) {
12477 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
12478 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
12479 FAIL() << "Failed to add fd to epoll";
12480 }
12481 }
12482
12483 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012484 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12485 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012486 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12487
12488 std::vector<size_t> eventOrder;
12489 std::vector<struct epoll_event> events(numChannels);
12490 for (;;) {
12491 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
12492 (100ms).count());
12493 if (nFds < 0) {
12494 FAIL() << "Failed to call epoll_wait";
12495 }
12496 if (nFds == 0) {
12497 break; // epoll_wait timed out
12498 }
12499 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -070012500 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -070012501 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012502 channels[i]->consumeMotionDown();
12503 }
12504 }
12505
12506 // Verify the order in which the events were received.
12507 EXPECT_EQ(3u, eventOrder.size());
12508 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
12509 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
12510 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
12511}
12512
12513/**
12514 * A spy window using the NOT_TOUCHABLE flag does not receive events.
12515 */
12516TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
12517 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012518 auto spy = createSpy();
12519 spy->setTouchable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012520 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012521
12522 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012523 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12524 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012525 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012526 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012527 spy->assertNoEvents();
12528}
12529
12530/**
12531 * A spy window will only receive gestures that originate within its touchable region. Gestures that
12532 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
12533 * to the window.
12534 */
12535TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
12536 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012537 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012538 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012539 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012540
12541 // Inject an event outside the spy window's touchable region.
12542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012543 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12544 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012545 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12546 window->consumeMotionDown();
12547 spy->assertNoEvents();
12548 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012549 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12550 ui::LogicalDisplayId::DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012551 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12552 window->consumeMotionUp();
12553 spy->assertNoEvents();
12554
12555 // Inject an event inside the spy window's touchable region.
12556 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012557 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12558 ui::LogicalDisplayId::DEFAULT, {5, 10}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012559 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12560 window->consumeMotionDown();
12561 spy->consumeMotionDown();
12562}
12563
12564/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012565 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080012566 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012567 */
12568TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
12569 auto window = createForeground();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012570 window->setOwnerInfo(gui::Pid{12}, gui::Uid{34});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012571 auto spy = createSpy();
12572 spy->setWatchOutsideTouch(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012573 spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012574 spy->setFrame(Rect{0, 0, 20, 20});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012575 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012576
12577 // Inject an event outside the spy window's frame and touchable region.
12578 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012579 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12580 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012581 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12582 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080012583 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012584}
12585
12586/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012587 * Even when a spy window spans over multiple foreground windows, the spy should receive all
12588 * pointers that are down within its bounds.
12589 */
12590TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
12591 auto windowLeft = createForeground();
12592 windowLeft->setFrame({0, 0, 100, 200});
12593 auto windowRight = createForeground();
12594 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012595 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012596 spy->setFrame({0, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012597 mDispatcher->onWindowInfosChanged(
12598 {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012599
12600 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012601 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12602 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012603 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12604 windowLeft->consumeMotionDown();
12605 spy->consumeMotionDown();
12606
12607 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080012608 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012609 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012610 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12611 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012612 .build();
12613 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012614 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012615 InputEventInjectionSync::WAIT_FOR_RESULT))
12616 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12617 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +000012618 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012619}
12620
12621/**
12622 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
12623 * the spy should receive the second pointer with ACTION_DOWN.
12624 */
12625TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
12626 auto window = createForeground();
12627 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012628 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012629 spyRight->setFrame({100, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012630 mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012631
12632 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012633 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12634 ui::LogicalDisplayId::DEFAULT, {50, 50}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012635 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12636 window->consumeMotionDown();
12637 spyRight->assertNoEvents();
12638
12639 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080012640 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012641 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012642 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12643 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012644 .build();
12645 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012646 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012647 InputEventInjectionSync::WAIT_FOR_RESULT))
12648 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000012649 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080012650 spyRight->consumeMotionDown();
12651}
12652
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012653/**
12654 * The spy window should not be able to affect whether or not touches are split. Only the foreground
12655 * windows should be allowed to control split touch.
12656 */
12657TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080012658 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012659 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012660 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080012661 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012662
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012663 auto window = createForeground();
12664 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012665
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012666 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012667
12668 // First finger down, no window touched.
12669 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012670 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12671 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012672 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012673 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012674 window->assertNoEvents();
12675
12676 // Second finger down on window, the window should receive touch down.
12677 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080012678 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012679 .displayId(ui::LogicalDisplayId::DEFAULT)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012680 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012681 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
12682 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012683 .build();
12684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012685 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012686 InputEventInjectionSync::WAIT_FOR_RESULT))
12687 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12688
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012689 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +000012690 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012691}
12692
12693/**
12694 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
12695 * do not receive key events.
12696 */
12697TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080012698 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012699 spy->setFocusable(false);
12700
12701 auto window = createForeground();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012702 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012703 setFocusedWindow(window);
12704 window->consumeFocusEvent(true);
12705
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012706 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012707 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012708 window->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012709
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012710 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012711 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012712 window->consumeKeyUp(ui::LogicalDisplayId::INVALID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080012713
12714 spy->assertNoEvents();
12715}
12716
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012717using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
12718
12719/**
12720 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
12721 * are currently sent to any other windows - including other spy windows - will also be cancelled.
12722 */
12723TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
12724 auto window = createForeground();
12725 auto spy1 = createSpy();
12726 auto spy2 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012727 mDispatcher->onWindowInfosChanged(
12728 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012729
12730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012731 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12732 ui::LogicalDisplayId::DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012733 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12734 window->consumeMotionDown();
12735 spy1->consumeMotionDown();
12736 spy2->consumeMotionDown();
12737
12738 // Pilfer pointers from the second spy window.
12739 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
12740 spy2->assertNoEvents();
12741 spy1->consumeMotionCancel();
12742 window->consumeMotionCancel();
12743
12744 // The rest of the gesture should only be sent to the second spy window.
12745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012746 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012747 ui::LogicalDisplayId::DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012748 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12749 spy2->consumeMotionMove();
12750 spy1->assertNoEvents();
12751 window->assertNoEvents();
12752}
12753
12754/**
12755 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
12756 * in the middle of the gesture.
12757 */
12758TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
12759 auto window = createForeground();
12760 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012761 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012762
12763 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012764 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12765 ui::LogicalDisplayId::DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012766 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012767 window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
12768 spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012769
12770 window->releaseChannel();
12771
12772 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
12773
12774 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012775 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12776 ui::LogicalDisplayId::DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012777 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012778 spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012779}
12780
12781/**
12782 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
12783 * the spy, but not to any other windows.
12784 */
12785TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
12786 auto spy = createSpy();
12787 auto window = createForeground();
12788
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012789 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012790
12791 // First finger down on the window and the spy.
12792 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012793 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12794 ui::LogicalDisplayId::DEFAULT, {100, 200}))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012795 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12796 spy->consumeMotionDown();
12797 window->consumeMotionDown();
12798
12799 // Spy window pilfers the pointers.
12800 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
12801 window->consumeMotionCancel();
12802
12803 // Second finger down on the window and spy, but the window should not receive the pointer down.
12804 const MotionEvent secondFingerDownEvent =
12805 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012806 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012807 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012808 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
12809 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012810 .build();
12811 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012812 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012813 InputEventInjectionSync::WAIT_FOR_RESULT))
12814 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12815
Harry Cutts33476232023-01-30 19:57:29 +000012816 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012817
12818 // Third finger goes down outside all windows, so injection should fail.
12819 const MotionEvent thirdFingerDownEvent =
12820 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012821 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012822 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012823 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
12824 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
12825 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012826 .build();
12827 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012828 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012829 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080012830 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012831
12832 spy->assertNoEvents();
12833 window->assertNoEvents();
12834}
12835
12836/**
12837 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
12838 */
12839TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
12840 auto spy = createSpy();
12841 spy->setFrame(Rect(0, 0, 100, 100));
12842 auto window = createForeground();
12843 window->setFrame(Rect(0, 0, 200, 200));
12844
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012845 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012846
12847 // First finger down on the window only
12848 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012849 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12850 ui::LogicalDisplayId::DEFAULT, {150, 150}))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012851 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12852 window->consumeMotionDown();
12853
12854 // Second finger down on the spy and window
12855 const MotionEvent secondFingerDownEvent =
12856 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012857 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012858 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012859 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
12860 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012861 .build();
12862 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012863 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012864 InputEventInjectionSync::WAIT_FOR_RESULT))
12865 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12866 spy->consumeMotionDown();
12867 window->consumeMotionPointerDown(1);
12868
12869 // Third finger down on the spy and window
12870 const MotionEvent thirdFingerDownEvent =
12871 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012872 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012873 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012874 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
12875 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
12876 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012877 .build();
12878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012879 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012880 InputEventInjectionSync::WAIT_FOR_RESULT))
12881 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12882 spy->consumeMotionPointerDown(1);
12883 window->consumeMotionPointerDown(2);
12884
12885 // Spy window pilfers the pointers.
12886 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Prabir Pradhan33cfc6d2024-06-11 20:17:44 +000012887 window->consumeMotionPointerUp(/*pointerIdx=*/2,
12888 AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT),
12889 WithFlags(AMOTION_EVENT_FLAG_CANCELED),
12890 WithPointerCount(3)));
12891 window->consumeMotionPointerUp(/*pointerIdx=*/1,
12892 AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT),
12893 WithFlags(AMOTION_EVENT_FLAG_CANCELED),
12894 WithPointerCount(2)));
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012895
12896 spy->assertNoEvents();
12897 window->assertNoEvents();
12898}
12899
12900/**
12901 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
12902 * other windows should be canceled. If this results in the cancellation of all pointers for some
12903 * window, then that window should receive ACTION_CANCEL.
12904 */
12905TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
12906 auto spy = createSpy();
12907 spy->setFrame(Rect(0, 0, 100, 100));
12908 auto window = createForeground();
12909 window->setFrame(Rect(0, 0, 200, 200));
12910
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012911 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012912
12913 // First finger down on both spy and window
12914 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012915 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12916 ui::LogicalDisplayId::DEFAULT, {10, 10}))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012917 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12918 window->consumeMotionDown();
12919 spy->consumeMotionDown();
12920
12921 // Second finger down on the spy and window
12922 const MotionEvent secondFingerDownEvent =
12923 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012924 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012925 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012926 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
12927 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012928 .build();
12929 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012930 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012931 InputEventInjectionSync::WAIT_FOR_RESULT))
12932 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12933 spy->consumeMotionPointerDown(1);
12934 window->consumeMotionPointerDown(1);
12935
12936 // Spy window pilfers the pointers.
12937 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
12938 window->consumeMotionCancel();
12939
12940 spy->assertNoEvents();
12941 window->assertNoEvents();
12942}
12943
12944/**
12945 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
12946 * be sent to other windows
12947 */
12948TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
12949 auto spy = createSpy();
12950 spy->setFrame(Rect(0, 0, 100, 100));
12951 auto window = createForeground();
12952 window->setFrame(Rect(0, 0, 200, 200));
12953
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012954 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012955
12956 // First finger down on both window and spy
12957 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012958 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
12959 ui::LogicalDisplayId::DEFAULT, {10, 10}))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012960 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12961 window->consumeMotionDown();
12962 spy->consumeMotionDown();
12963
12964 // Spy window pilfers the pointers.
12965 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
12966 window->consumeMotionCancel();
12967
12968 // Second finger down on the window only
12969 const MotionEvent secondFingerDownEvent =
12970 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070012971 .displayId(ui::LogicalDisplayId::DEFAULT)
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012972 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070012973 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
12974 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012975 .build();
12976 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012977 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000012978 InputEventInjectionSync::WAIT_FOR_RESULT))
12979 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
12980 window->consumeMotionDown();
12981 window->assertNoEvents();
12982
12983 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
12984 spy->consumeMotionMove();
12985 spy->assertNoEvents();
12986}
12987
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070012988/**
12989 * A window on the left and a window on the right. Also, a spy window that's above all of the
12990 * windows, and spanning both left and right windows.
12991 * Send simultaneous motion streams from two different devices, one to the left window, and another
12992 * to the right window.
12993 * Pilfer from spy window.
12994 * Check that the pilfering only affects the pointers that are actually being received by the spy.
12995 */
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070012996TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer_legacy) {
12997 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070012998 sp<FakeWindowHandle> spy = createSpy();
12999 spy->setFrame(Rect(0, 0, 200, 200));
13000 sp<FakeWindowHandle> leftWindow = createForeground();
13001 leftWindow->setFrame(Rect(0, 0, 100, 100));
13002
13003 sp<FakeWindowHandle> rightWindow = createForeground();
13004 rightWindow->setFrame(Rect(100, 0, 200, 100));
13005
13006 constexpr int32_t stylusDeviceId = 1;
13007 constexpr int32_t touchDeviceId = 2;
13008
13009 mDispatcher->onWindowInfosChanged(
13010 {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
13011
13012 // Stylus down on left window and spy
13013 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
13014 .deviceId(stylusDeviceId)
13015 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
13016 .build());
13017 leftWindow->consumeMotionEvent(
13018 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
13019 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
13020
13021 // Finger down on right window and spy - but spy already has stylus
13022 mDispatcher->notifyMotion(
13023 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
13024 .deviceId(touchDeviceId)
13025 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
13026 .build());
13027 rightWindow->consumeMotionEvent(
13028 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070013029 spy->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070013030
13031 // Act: pilfer from spy. Spy is currently receiving touch events.
13032 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070013033 leftWindow->consumeMotionEvent(
13034 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070013035 rightWindow->consumeMotionEvent(
13036 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
13037
13038 // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus
13039 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
13040 .deviceId(stylusDeviceId)
13041 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52))
13042 .build());
13043 mDispatcher->notifyMotion(
13044 MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
13045 .deviceId(touchDeviceId)
13046 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52))
13047 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070013048 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070013049
13050 spy->assertNoEvents();
13051 leftWindow->assertNoEvents();
13052 rightWindow->assertNoEvents();
13053}
13054
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013055/**
13056 * A window on the left and a window on the right. Also, a spy window that's above all of the
13057 * windows, and spanning both left and right windows.
13058 * Send simultaneous motion streams from two different devices, one to the left window, and another
13059 * to the right window.
13060 * Pilfer from spy window.
13061 * Check that the pilfering affects all of the pointers that are actually being received by the spy.
13062 * The spy should receive both the touch and the stylus events after pilfer.
13063 */
13064TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) {
13065 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
13066 sp<FakeWindowHandle> spy = createSpy();
13067 spy->setFrame(Rect(0, 0, 200, 200));
13068 sp<FakeWindowHandle> leftWindow = createForeground();
13069 leftWindow->setFrame(Rect(0, 0, 100, 100));
13070
13071 sp<FakeWindowHandle> rightWindow = createForeground();
13072 rightWindow->setFrame(Rect(100, 0, 200, 100));
13073
13074 constexpr int32_t stylusDeviceId = 1;
13075 constexpr int32_t touchDeviceId = 2;
13076
13077 mDispatcher->onWindowInfosChanged(
13078 {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
13079
13080 // Stylus down on left window and spy
13081 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
13082 .deviceId(stylusDeviceId)
13083 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
13084 .build());
13085 leftWindow->consumeMotionEvent(
13086 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
13087 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
13088
13089 // Finger down on right window and spy
13090 mDispatcher->notifyMotion(
13091 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
13092 .deviceId(touchDeviceId)
13093 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
13094 .build());
13095 rightWindow->consumeMotionEvent(
13096 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
13097 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
13098
13099 // Act: pilfer from spy. Spy is currently receiving touch events.
13100 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
13101 leftWindow->consumeMotionEvent(
13102 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId)));
13103 rightWindow->consumeMotionEvent(
13104 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
13105
13106 // Continue movements from both stylus and touch. Touch and stylus will be delivered to spy
Siarhei Vishniakou92bca1c2024-04-01 14:06:59 -070013107 // Instead of sending the two MOVE events for each input device together, and then receiving
13108 // them both, process them one at at time. InputConsumer is always in the batching mode, which
13109 // means that the two MOVE events will be initially put into a batch. Once the events are
13110 // batched, the 'consume' call may result in any of the MOVE events to be sent first (depending
13111 // on the implementation of InputConsumer), which would mean that the order of the received
13112 // events could be different depending on whether there are 1 or 2 events pending in the
13113 // InputChannel at the time the test calls 'consume'. To make assertions simpler here, and to
13114 // avoid this confusing behaviour, send and receive each MOVE event separately.
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013115 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
13116 .deviceId(stylusDeviceId)
13117 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52))
13118 .build());
Siarhei Vishniakou92bca1c2024-04-01 14:06:59 -070013119 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013120 mDispatcher->notifyMotion(
13121 MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
13122 .deviceId(touchDeviceId)
13123 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52))
13124 .build());
Siarhei Vishniakou92bca1c2024-04-01 14:06:59 -070013125 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013126
13127 spy->assertNoEvents();
13128 leftWindow->assertNoEvents();
13129 rightWindow->assertNoEvents();
13130}
13131
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000013132TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) {
13133 auto window = createForeground();
13134 auto spy = createSpy();
13135 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
13136
13137 mDispatcher->notifyMotion(
13138 MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
13139 .deviceId(1)
13140 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200))
13141 .build());
13142 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13143 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13144
13145 // Pilfer pointers from the spy window should fail.
13146 EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken()));
13147 spy->assertNoEvents();
13148 window->assertNoEvents();
13149}
13150
Prabir Pradhand65552b2021-10-07 11:23:50 -070013151class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
13152public:
13153 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
13154 std::shared_ptr<FakeApplicationHandle> overlayApplication =
13155 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013156 sp<FakeWindowHandle> overlay = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
13157 "Stylus interceptor window",
13158 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013159 overlay->setFocusable(false);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013160 overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080013161 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080013162 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013163 overlay->setTrustedOverlay(true);
13164
13165 std::shared_ptr<FakeApplicationHandle> application =
13166 std::make_shared<FakeApplicationHandle>();
13167 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070013168 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013169 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013170 window->setFocusable(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013171 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Prabir Pradhand65552b2021-10-07 11:23:50 -070013172
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013173 mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013174 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070013175 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000013176 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013177 return {std::move(overlay), std::move(window)};
13178 }
13179
13180 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000013181 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -070013182 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013183 ui::LogicalDisplayId::DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -070013184 }
13185
13186 void sendStylusEvent(int32_t action) {
13187 NotifyMotionArgs motionArgs =
13188 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013189 ui::LogicalDisplayId::DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070013190 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +000013191 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -070013192 }
13193};
13194
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080013195using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
13196
13197TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
Siarhei Vishniakouad3b6822023-06-22 14:17:35 -070013198 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080013199 ScopedSilentDeath _silentDeath;
13200
Prabir Pradhand65552b2021-10-07 11:23:50 -070013201 auto [overlay, window] = setupStylusOverlayScenario();
13202 overlay->setTrustedOverlay(false);
13203 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013204 ASSERT_DEATH(mDispatcher->onWindowInfosChanged(
13205 {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}),
Prabir Pradhand65552b2021-10-07 11:23:50 -070013206 ".* not a trusted overlay");
13207}
13208
13209TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
13210 auto [overlay, window] = setupStylusOverlayScenario();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013211 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070013212
13213 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
13214 overlay->consumeMotionDown();
13215 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
13216 overlay->consumeMotionUp();
13217
13218 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
13219 window->consumeMotionDown();
13220 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
13221 window->consumeMotionUp();
13222
13223 overlay->assertNoEvents();
13224 window->assertNoEvents();
13225}
13226
13227TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
13228 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -080013229 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013230 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070013231
13232 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
13233 overlay->consumeMotionDown();
13234 window->consumeMotionDown();
13235 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
13236 overlay->consumeMotionUp();
13237 window->consumeMotionUp();
13238
13239 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
13240 window->consumeMotionDown();
13241 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
13242 window->consumeMotionUp();
13243
13244 overlay->assertNoEvents();
13245 window->assertNoEvents();
13246}
13247
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000013248/**
13249 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
13250 * The scenario is as follows:
13251 * - The stylus interceptor overlay is configured as a spy window.
13252 * - The stylus interceptor spy receives the start of a new stylus gesture.
13253 * - It pilfers pointers and then configures itself to no longer be a spy.
13254 * - The stylus interceptor continues to receive the rest of the gesture.
13255 */
13256TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
13257 auto [overlay, window] = setupStylusOverlayScenario();
13258 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013259 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000013260
13261 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
13262 overlay->consumeMotionDown();
13263 window->consumeMotionDown();
13264
13265 // The interceptor pilfers the pointers.
13266 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
13267 window->consumeMotionCancel();
13268
13269 // The interceptor configures itself so that it is no longer a spy.
13270 overlay->setSpy(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013271 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000013272
13273 // It continues to receive the rest of the stylus gesture.
13274 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
13275 overlay->consumeMotionMove();
13276 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
13277 overlay->consumeMotionUp();
13278
13279 window->assertNoEvents();
13280}
13281
Prabir Pradhan5735a322022-04-11 17:23:34 +000013282struct User {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013283 gui::Pid mPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000013284 gui::Uid mUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +000013285 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
13286 std::unique_ptr<InputDispatcher>& mDispatcher;
13287
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013288 User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid)
Prabir Pradhan5735a322022-04-11 17:23:34 +000013289 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
13290
13291 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070013292 return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013293 ui::LogicalDisplayId::DEFAULT, {100, 200},
Prabir Pradhan5735a322022-04-11 17:23:34 +000013294 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
13295 AMOTION_EVENT_INVALID_CURSOR_POSITION},
13296 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
13297 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
13298 }
13299
13300 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Linnan Li13bf76a2024-05-05 19:18:02 +080013301 return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013302 ui::LogicalDisplayId::INVALID,
Prabir Pradhan5735a322022-04-11 17:23:34 +000013303 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +000013304 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +000013305 mPolicyFlags);
13306 }
13307
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013308 sp<FakeWindowHandle> createWindow(const char* name) const {
Prabir Pradhan5735a322022-04-11 17:23:34 +000013309 std::shared_ptr<FakeApplicationHandle> overlayApplication =
13310 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013311 sp<FakeWindowHandle> window =
13312 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, name,
13313 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +000013314 window->setOwnerInfo(mPid, mUid);
13315 return window;
13316 }
13317};
13318
13319using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
13320
13321TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013322 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013323 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013324 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013325
13326 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13327 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13328 window->consumeMotionDown();
13329
13330 setFocusedWindow(window);
13331 window->consumeFocusEvent(true);
13332
13333 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13334 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013335 window->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Prabir Pradhan5735a322022-04-11 17:23:34 +000013336}
13337
13338TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013339 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013340 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013341 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013342
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013343 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013344 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
13345 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13346
13347 setFocusedWindow(window);
13348 window->consumeFocusEvent(true);
13349
13350 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
13351 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
13352 window->assertNoEvents();
13353}
13354
13355TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013356 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013357 auto window = owner.createWindow("Owned window");
13358 auto spy = owner.createWindow("Owned spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013359 spy->setSpy(true);
13360 spy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013361 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013362
13363 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13364 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13365 spy->consumeMotionDown();
13366 window->consumeMotionDown();
13367}
13368
13369TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013370 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013371 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013372
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013373 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013374 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013375 randosSpy->setSpy(true);
13376 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013377 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013378
13379 // The event is targeted at owner's window, so injection should succeed, but the spy should
13380 // not receive the event.
13381 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13382 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13383 randosSpy->assertNoEvents();
13384 window->consumeMotionDown();
13385}
13386
13387TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013388 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013389 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013390
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013391 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013392 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013393 randosSpy->setSpy(true);
13394 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013395 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013396
13397 // A user that has injection permission can inject into any window.
13398 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070013399 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013400 ui::LogicalDisplayId::DEFAULT));
Prabir Pradhan5735a322022-04-11 17:23:34 +000013401 randosSpy->consumeMotionDown();
13402 window->consumeMotionDown();
13403
13404 setFocusedWindow(randosSpy);
13405 randosSpy->consumeFocusEvent(true);
13406
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070013407 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013408 randosSpy->consumeKeyDown(ui::LogicalDisplayId::INVALID);
Prabir Pradhan5735a322022-04-11 17:23:34 +000013409 window->assertNoEvents();
13410}
13411
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070013412TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013413 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013414 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013415
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000013416 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070013417 auto randosWindow = rando.createWindow("Rando's window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000013418 randosWindow->setFrame(Rect{-10, -10, -5, -5});
13419 randosWindow->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070013420 mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000013421
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070013422 // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids.
Prabir Pradhan5735a322022-04-11 17:23:34 +000013423 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
13424 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
13425 window->consumeMotionDown();
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070013426 randosWindow->assertNoEvents();
Prabir Pradhan5735a322022-04-11 17:23:34 +000013427}
13428
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013429using InputDispatcherPointerInWindowTest = InputDispatcherTest;
13430
13431TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) {
13432 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
13433
13434 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013435 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013436 left->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013437 sp<FakeWindowHandle> right =
13438 sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window",
13439 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013440 right->setFrame(Rect(100, 0, 200, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013441 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window",
13442 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013443 spy->setFrame(Rect(0, 0, 200, 100));
13444 spy->setTrustedOverlay(true);
13445 spy->setSpy(true);
13446
13447 mDispatcher->onWindowInfosChanged(
13448 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
13449
13450 // Hover into the left window.
13451 mDispatcher->notifyMotion(
13452 MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
13453 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50))
13454 .build());
13455
13456 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13457 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13458
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013459 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13460 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013461 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013462 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->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_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13466 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013467 /*pointerId=*/0));
13468
13469 // Hover move to the right window.
13470 mDispatcher->notifyMotion(
13471 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
13472 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50))
13473 .build());
13474
13475 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13476 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13477 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
13478
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013479 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13480 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013481 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013482 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->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(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13486 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013487 /*pointerId=*/0));
13488
13489 // Stop hovering.
13490 mDispatcher->notifyMotion(
13491 MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
13492 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50))
13493 .build());
13494
13495 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13496 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13497
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013498 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13499 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013500 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013501 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->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(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13505 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013506 /*pointerId=*/0));
13507}
13508
13509TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) {
13510 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
13511
13512 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013513 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013514 left->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013515 sp<FakeWindowHandle> right =
13516 sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window",
13517 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013518 right->setFrame(Rect(100, 0, 200, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013519 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window",
13520 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013521 spy->setFrame(Rect(0, 0, 200, 100));
13522 spy->setTrustedOverlay(true);
13523 spy->setSpy(true);
13524
13525 mDispatcher->onWindowInfosChanged(
13526 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
13527
13528 // First pointer down on left window.
13529 mDispatcher->notifyMotion(
13530 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
13531 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
13532 .build());
13533
13534 left->consumeMotionDown();
13535 spy->consumeMotionDown();
13536
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013537 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13538 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013539 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013540 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->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_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13544 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013545 /*pointerId=*/0));
13546
13547 // Second pointer down on right window.
13548 mDispatcher->notifyMotion(
13549 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
13550 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
13551 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
13552 .build());
13553
13554 left->consumeMotionMove();
13555 right->consumeMotionDown();
13556 spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
13557
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013558 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13559 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013560 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013561 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->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_FALSE(mDispatcher->isPointerInWindow(right->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(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13568 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013569 /*pointerId=*/1));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013570 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->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(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13574 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013575 /*pointerId=*/1));
13576
13577 // Second pointer up.
13578 mDispatcher->notifyMotion(
13579 MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
13580 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
13581 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
13582 .build());
13583
13584 left->consumeMotionMove();
13585 right->consumeMotionUp();
13586 spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
13587
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013588 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13589 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013590 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013591 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->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_FALSE(mDispatcher->isPointerInWindow(right->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(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13598 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013599 /*pointerId=*/1));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013600 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->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(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13604 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013605 /*pointerId=*/1));
13606
13607 // First pointer up.
13608 mDispatcher->notifyMotion(
13609 MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
13610 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
13611 .build());
13612
13613 left->consumeMotionUp();
13614 spy->consumeMotionUp();
13615
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013616 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13617 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013618 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013619 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT,
13620 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013621 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013622 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13623 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013624 /*pointerId=*/0));
13625}
13626
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013627TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse_legacy) {
13628 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013629 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
13630
13631 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013632 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013633 left->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013634 sp<FakeWindowHandle> right =
13635 sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window",
13636 ui::LogicalDisplayId::DEFAULT);
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013637 right->setFrame(Rect(100, 0, 200, 100));
13638
13639 mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0});
13640
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013641 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13642 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013643 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013644 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13645 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013646 /*pointerId=*/0));
13647
13648 // Hover move into the window.
13649 mDispatcher->notifyMotion(
13650 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13651 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50))
13652 .rawXCursorPosition(50)
13653 .rawYCursorPosition(50)
13654 .deviceId(DEVICE_ID)
13655 .build());
13656
13657 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13658
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013659 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13660 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013661 /*pointerId=*/0));
13662
13663 // Move the mouse with another device. This cancels the hovering pointer from the first device.
13664 mDispatcher->notifyMotion(
13665 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13666 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50))
13667 .rawXCursorPosition(51)
13668 .rawYCursorPosition(50)
13669 .deviceId(SECOND_DEVICE_ID)
13670 .build());
13671
13672 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13673 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13674
13675 // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets
13676 // a HOVER_EXIT from the first device.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013677 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13678 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013679 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013680 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013681 SECOND_DEVICE_ID,
13682 /*pointerId=*/0));
13683
13684 // Move the mouse outside the window. Document the current behavior, where the window does not
13685 // receive HOVER_EXIT even though the mouse left the window.
13686 mDispatcher->notifyMotion(
13687 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13688 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50))
13689 .rawXCursorPosition(150)
13690 .rawYCursorPosition(50)
13691 .deviceId(SECOND_DEVICE_ID)
13692 .build());
13693
13694 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
13695 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013696 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13697 DEVICE_ID,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013698 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013699 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
Prabir Pradhan64f21d22023-11-28 21:19:42 +000013700 SECOND_DEVICE_ID,
13701 /*pointerId=*/0));
13702}
13703
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013704/**
13705 * TODO(b/313689709) - correctly support multiple mouse devices, because they should be controlling
13706 * the same cursor, and therefore have a shared motion event stream.
13707 */
13708TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) {
13709 SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true);
13710 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
13711
13712 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013713 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013714 left->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013715 sp<FakeWindowHandle> right =
13716 sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window",
13717 ui::LogicalDisplayId::DEFAULT);
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013718 right->setFrame(Rect(100, 0, 200, 100));
13719
13720 mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0});
13721
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013722 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13723 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013724 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013725 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT,
13726 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013727 /*pointerId=*/0));
13728
13729 // Hover move into the window.
13730 mDispatcher->notifyMotion(
13731 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13732 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50))
13733 .rawXCursorPosition(50)
13734 .rawYCursorPosition(50)
13735 .deviceId(DEVICE_ID)
13736 .build());
13737
13738 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13739
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013740 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13741 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013742 /*pointerId=*/0));
13743
13744 // Move the mouse with another device
13745 mDispatcher->notifyMotion(
13746 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13747 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50))
13748 .rawXCursorPosition(51)
13749 .rawYCursorPosition(50)
13750 .deviceId(SECOND_DEVICE_ID)
13751 .build());
13752 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
13753
13754 // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets
13755 // a HOVER_EXIT from the first device.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013756 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13757 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013758 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013759 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013760 SECOND_DEVICE_ID,
13761 /*pointerId=*/0));
13762
13763 // Move the mouse outside the window. Document the current behavior, where the window does not
13764 // receive HOVER_EXIT even though the mouse left the window.
13765 mDispatcher->notifyMotion(
13766 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
13767 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50))
13768 .rawXCursorPosition(150)
13769 .rawYCursorPosition(50)
13770 .deviceId(SECOND_DEVICE_ID)
13771 .build());
13772
13773 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013774 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
13775 DEVICE_ID,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013776 /*pointerId=*/0));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070013777 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -070013778 SECOND_DEVICE_ID,
13779 /*pointerId=*/0));
13780}
13781
Garfield Tane84e6f92019-08-29 17:28:41 -070013782} // namespace android::inputdispatcher