blob: 7222c607e7750b1afea2819ecd9cbae01ac277da [file] [log] [blame]
Robert Carr1c4c5592018-09-24 13:18:43 -07001/*
2 * Copyright 2018 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
17#include <gtest/gtest.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <sys/time.h>
21#include <sys/types.h>
22#include <stdio.h>
23#include <poll.h>
24
25#include <memory>
26
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070027#include <android-base/thread_annotations.h>
Patrick Williams0a4981a2023-07-28 15:08:52 -050028#include <android/gui/BnWindowInfosReportedListener.h>
Vishnu Naira066d902021-09-13 18:40:17 -070029#include <android/keycodes.h>
Vishnu Nairde19f852018-12-18 16:11:53 -080030#include <android/native_window.h>
31
Robert Carr1c4c5592018-09-24 13:18:43 -070032#include <binder/Binder.h>
33#include <binder/IServiceManager.h>
34#include <binder/Parcel.h>
35#include <binder/ProcessState.h>
36
Vishnu Nairde19f852018-12-18 16:11:53 -080037#include <gui/ISurfaceComposer.h>
38#include <gui/Surface.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070039#include <gui/SurfaceComposerClient.h>
40#include <gui/SurfaceControl.h>
41
Chris Ye0783e992020-06-02 21:34:49 -070042#include <android/os/IInputFlinger.h>
chaviw98318de2021-05-19 16:45:23 -050043#include <gui/WindowInfo.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070044#include <input/Input.h>
Siarhei Vishniakou0438ca82024-03-12 14:27:25 -070045#include <input/InputConsumer.h>
Chris Ye0783e992020-06-02 21:34:49 -070046#include <input/InputTransport.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070047
Marin Shalamanova7fe3042021-01-29 21:02:08 +010048#include <ui/DisplayMode.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070049#include <ui/Rect.h>
50#include <ui/Region.h>
51
Prabir Pradhand0aba782021-12-14 00:44:21 -080052#include <private/android_filesystem_config.h>
53
Chris Ye0783e992020-06-02 21:34:49 -070054using android::os::IInputFlinger;
Robert Carr1c4c5592018-09-24 13:18:43 -070055
chaviw39d01472021-04-08 14:26:24 -050056using android::hardware::graphics::common::V1_1::BufferUsage;
57
chaviw98318de2021-05-19 16:45:23 -050058using android::gui::FocusRequest;
59using android::gui::InputApplicationInfo;
60using android::gui::TouchOcclusionMode;
61using android::gui::WindowInfo;
62
Vishnu Nair958da932020-08-21 17:12:37 -070063namespace android::test {
Robert Carr1c4c5592018-09-24 13:18:43 -070064
Vishnu Nairde19f852018-12-18 16:11:53 -080065using Transaction = SurfaceComposerClient::Transaction;
66
Robert Carr1c4c5592018-09-24 13:18:43 -070067sp<IInputFlinger> getInputFlinger() {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -080068 sp<IBinder> input(defaultServiceManager()->waitForService(String16("inputflinger")));
Robert Carr1c4c5592018-09-24 13:18:43 -070069 if (input == nullptr) {
70 ALOGE("Failed to link to input service");
71 } else { ALOGE("Linked to input"); }
72 return interface_cast<IInputFlinger>(input);
73}
74
75// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
76static const int LAYER_BASE = INT32_MAX - 10;
Chris Ye6c4243b2020-07-22 12:07:12 -070077static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070078
Patrick Williams0a4981a2023-07-28 15:08:52 -050079class SynchronousWindowInfosReportedListener : public gui::BnWindowInfosReportedListener {
80public:
81 binder::Status onWindowInfosReported() override {
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070082 std::scoped_lock lock{mLock};
Patrick Williams0a4981a2023-07-28 15:08:52 -050083 mWindowInfosReported = true;
84 mConditionVariable.notify_one();
85 return binder::Status::ok();
86 }
87
88 void wait() {
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070089 std::unique_lock lock{mLock};
90 android::base::ScopedLockAssertion assumeLocked(mLock);
91 mConditionVariable.wait(lock, [&]() REQUIRES(mLock) { return mWindowInfosReported; });
Patrick Williams0a4981a2023-07-28 15:08:52 -050092 }
93
94private:
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070095 std::mutex mLock;
Patrick Williams0a4981a2023-07-28 15:08:52 -050096 std::condition_variable mConditionVariable;
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070097 bool mWindowInfosReported GUARDED_BY(mLock){false};
Patrick Williams0a4981a2023-07-28 15:08:52 -050098};
99
Robert Carr1c4c5592018-09-24 13:18:43 -0700100class InputSurface {
101public:
Linus Tufvessona1858822022-03-04 09:32:07 +0000102 InputSurface(const sp<SurfaceControl> &sc, int width, int height, bool noInputChannel = false) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800103 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -0700104
105 mInputFlinger = getInputFlinger();
Linus Tufvessona1858822022-03-04 09:32:07 +0000106 if (noInputChannel) {
107 mInputInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, true);
108 } else {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800109 android::os::InputChannelCore tempChannel;
110 android::binder::Status result =
111 mInputFlinger->createInputChannel("testchannels", &tempChannel);
112 if (!result.isOk()) {
113 ADD_FAILURE() << "binder call to createInputChannel failed";
114 }
115 mClientChannel = InputChannel::create(std::move(tempChannel));
Linus Tufvessona1858822022-03-04 09:32:07 +0000116 mInputInfo.token = mClientChannel->getConnectionToken();
117 mInputConsumer = new InputConsumer(mClientChannel);
118 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700119
Linus Tufvessona1858822022-03-04 09:32:07 +0000120 mInputInfo.name = "Test info";
121 mInputInfo.dispatchingTimeout = 5s;
122 mInputInfo.globalScaleFactor = 1.0;
123 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
Robert Carr1c4c5592018-09-24 13:18:43 -0700124
Linus Tufvessona1858822022-03-04 09:32:07 +0000125 InputApplicationInfo aInfo;
126 aInfo.token = new BBinder();
127 aInfo.name = "Test app info";
128 aInfo.dispatchingTimeoutMillis =
129 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
130 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700131 }
132
Vishnu Nairde19f852018-12-18 16:11:53 -0800133 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
134 int width, int height) {
135 sp<SurfaceControl> surfaceControl =
136 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800137 PIXEL_FORMAT_RGBA_8888,
138 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -0800139 return std::make_unique<InputSurface>(surfaceControl, width, height);
140 }
141
142 static std::unique_ptr<InputSurface> makeBufferInputSurface(
143 const sp<SurfaceComposerClient> &scc, int width, int height) {
144 sp<SurfaceControl> surfaceControl =
145 scc->createSurface(String8("Test Buffer Surface"), width, height,
146 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
147 return std::make_unique<InputSurface>(surfaceControl, width, height);
148 }
149
150 static std::unique_ptr<InputSurface> makeContainerInputSurface(
151 const sp<SurfaceComposerClient> &scc, int width, int height) {
152 sp<SurfaceControl> surfaceControl =
153 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
154 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
155 ISurfaceComposerClient::eFXSurfaceContainer);
156 return std::make_unique<InputSurface>(surfaceControl, width, height);
157 }
158
Linus Tufvessona1858822022-03-04 09:32:07 +0000159 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
160 const sp<SurfaceComposerClient> &scc, int width, int height) {
161 sp<SurfaceControl> surfaceControl =
162 scc->createSurface(String8("Test Container Surface"), 100 /* height */,
163 100 /* width */, PIXEL_FORMAT_RGBA_8888,
164 ISurfaceComposerClient::eFXSurfaceContainer);
165 return std::make_unique<InputSurface>(surfaceControl, width, height,
166 true /* noInputChannel */);
167 }
168
arthurhungb4a0f852020-06-16 11:02:50 +0800169 static std::unique_ptr<InputSurface> makeCursorInputSurface(
170 const sp<SurfaceComposerClient> &scc, int width, int height) {
171 sp<SurfaceControl> surfaceControl =
172 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
173 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
174 ISurfaceComposerClient::eCursorWindow);
175 return std::make_unique<InputSurface>(surfaceControl, width, height);
176 }
177
Egor Pasko5a67a562024-01-16 16:46:45 +0100178 InputEvent* consumeEvent(std::chrono::milliseconds timeout = 3000ms) {
179 mClientChannel->waitForMessage(timeout);
Robert Carr1c4c5592018-09-24 13:18:43 -0700180
181 InputEvent *ev;
182 uint32_t seqId;
183 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
184 if (consumed != OK) {
185 return nullptr;
186 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100187 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
188 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700189 return ev;
190 }
191
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100192 void assertFocusChange(bool hasFocus) {
193 InputEvent *ev = consumeEvent();
194 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700195 ASSERT_EQ(InputEventType::FOCUS, ev->getType());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100196 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
197 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
198 }
199
Robert Carr1c4c5592018-09-24 13:18:43 -0700200 void expectTap(int x, int y) {
201 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100202 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700203 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700204 MotionEvent* mev = static_cast<MotionEvent*>(ev);
205 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
206 EXPECT_EQ(x, mev->getX(0));
207 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800208 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700209
210 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100211 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700212 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700213 mev = static_cast<MotionEvent*>(ev);
214 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800215 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700216 }
217
chaviw39cfa2e2020-11-04 14:19:02 -0800218 void expectTapWithFlag(int x, int y, int32_t flags) {
219 InputEvent *ev = consumeEvent();
220 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700221 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800222 MotionEvent *mev = static_cast<MotionEvent *>(ev);
223 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
224 EXPECT_EQ(x, mev->getX(0));
225 EXPECT_EQ(y, mev->getY(0));
226 EXPECT_EQ(flags, mev->getFlags() & flags);
227
228 ev = consumeEvent();
229 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700230 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800231 mev = static_cast<MotionEvent *>(ev);
232 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
233 EXPECT_EQ(flags, mev->getFlags() & flags);
234 }
235
Prabir Pradhand0aba782021-12-14 00:44:21 -0800236 void expectTapInDisplayCoordinates(int displayX, int displayY) {
237 InputEvent *ev = consumeEvent();
238 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700239 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800240 MotionEvent *mev = static_cast<MotionEvent *>(ev);
241 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
242 const PointerCoords &coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
243 EXPECT_EQ(displayX, coords.getX());
244 EXPECT_EQ(displayY, coords.getY());
245 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
246
247 ev = consumeEvent();
248 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700249 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800250 mev = static_cast<MotionEvent *>(ev);
251 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
252 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
253 }
254
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -0700255 void expectKey(int32_t keycode) {
Vishnu Naira066d902021-09-13 18:40:17 -0700256 InputEvent *ev = consumeEvent();
257 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700258 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700259 KeyEvent *keyEvent = static_cast<KeyEvent *>(ev);
260 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
261 EXPECT_EQ(keycode, keyEvent->getKeyCode());
262 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
263
264 ev = consumeEvent();
265 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700266 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700267 keyEvent = static_cast<KeyEvent *>(ev);
268 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
269 EXPECT_EQ(keycode, keyEvent->getKeyCode());
270 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
271 }
272
chaviw39d01472021-04-08 14:26:24 -0500273 virtual ~InputSurface() {
Linus Tufvessona1858822022-03-04 09:32:07 +0000274 if (mClientChannel) {
275 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
276 }
chaviw39d01472021-04-08 14:26:24 -0500277 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700278
chaviw39d01472021-04-08 14:26:24 -0500279 virtual void doTransaction(
280 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
281 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700282 SurfaceComposerClient::Transaction t;
283 transactionBody(t, mSurfaceControl);
284 t.apply(true);
285 }
286
chaviw39d01472021-04-08 14:26:24 -0500287 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700288 SurfaceComposerClient::Transaction t;
289 t.show(mSurfaceControl);
290 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
291 t.setLayer(mSurfaceControl, LAYER_BASE);
292 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800293 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700294 t.setAlpha(mSurfaceControl, 1);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500295 auto reportedListener = sp<SynchronousWindowInfosReportedListener>::make();
296 t.addWindowInfosReportedListener(reportedListener);
297 t.apply();
298 reportedListener->wait();
Robert Carr1c4c5592018-09-24 13:18:43 -0700299 }
300
Vishnu Nair16a938f2021-09-24 07:14:54 -0700301 void requestFocus(int displayId = ADISPLAY_ID_DEFAULT) {
Vishnu Nair958da932020-08-21 17:12:37 -0700302 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800303 FocusRequest request;
304 request.token = mInputInfo.token;
305 request.windowName = mInputInfo.name;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800306 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700307 request.displayId = displayId;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800308 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700309 t.apply(true);
310 }
311
Robert Carr1c4c5592018-09-24 13:18:43 -0700312public:
313 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500314 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700315 sp<IInputFlinger> mInputFlinger;
316
chaviw98318de2021-05-19 16:45:23 -0500317 WindowInfo mInputInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700318
319 PreallocatedInputEventFactory mInputEventFactory;
320 InputConsumer* mInputConsumer;
321};
322
chaviw39d01472021-04-08 14:26:24 -0500323class BlastInputSurface : public InputSurface {
324public:
325 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
326 int height)
327 : InputSurface(sc, width, height) {
328 mParentSurfaceControl = parentSc;
329 }
330
331 ~BlastInputSurface() = default;
332
333 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
334 const sp<SurfaceComposerClient> &scc, int width, int height) {
335 sp<SurfaceControl> parentSc =
336 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
337 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
338 ISurfaceComposerClient::eFXSurfaceContainer);
339
340 sp<SurfaceControl> surfaceControl =
341 scc->createSurface(String8("Test Buffer Surface"), width, height,
342 PIXEL_FORMAT_RGBA_8888,
343 ISurfaceComposerClient::eFXSurfaceBufferState,
344 parentSc->getHandle());
345 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
346 }
347
348 void doTransaction(
349 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
350 transactionBody) override {
351 SurfaceComposerClient::Transaction t;
352 transactionBody(t, mParentSurfaceControl);
353 t.apply(true);
354 }
355
356 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
357 SurfaceComposerClient::Transaction t;
358 t.show(mParentSurfaceControl);
359 t.setLayer(mParentSurfaceControl, LAYER_BASE);
360 t.setPosition(mParentSurfaceControl, x, y);
361 t.setCrop(mParentSurfaceControl, crop);
362
363 t.show(mSurfaceControl);
364 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
365 t.setCrop(mSurfaceControl, crop);
366 t.setAlpha(mSurfaceControl, 1);
367 t.apply(true);
368 }
369
370private:
371 sp<SurfaceControl> mParentSurfaceControl;
372};
373
Robert Carr1c4c5592018-09-24 13:18:43 -0700374class InputSurfacesTest : public ::testing::Test {
375public:
376 InputSurfacesTest() {
377 ProcessState::self()->startThreadPool();
378 }
379
380 void SetUp() {
381 mComposerClient = new SurfaceComposerClient;
382 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Huihong Luo31b5ac22022-08-15 20:38:10 -0700383 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
384 ASSERT_FALSE(ids.empty());
385 // display 0 is picked for now, can extend to support all displays if needed
386 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100387 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800388
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100389 ui::DisplayMode mode;
390 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800391
392 // After a new buffer is queued, SurfaceFlinger is notified and will
393 // latch the new buffer on next vsync. Let's heuristically wait for 3
394 // vsyncs.
Alec Mouri55e31032023-10-02 20:34:18 +0000395 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.peakRefreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700396 }
397
398 void TearDown() {
399 mComposerClient->dispose();
400 }
401
402 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800403 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
404 }
405
chaviw39d01472021-04-08 14:26:24 -0500406 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
407 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
408 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
409 sp<GraphicBuffer> buffer =
410 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
411 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800412 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700413 }
414
415 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800416 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700417};
418
Vishnu Nair16a938f2021-09-24 07:14:54 -0700419void injectTapOnDisplay(int x, int y, int displayId) {
420 char *buf1, *buf2, *bufDisplayId;
Robert Carr1c4c5592018-09-24 13:18:43 -0700421 asprintf(&buf1, "%d", x);
422 asprintf(&buf2, "%d", y);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700423 asprintf(&bufDisplayId, "%d", displayId);
Robert Carr1c4c5592018-09-24 13:18:43 -0700424 if (fork() == 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700425 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
426 }
427}
428
429void injectTap(int x, int y) {
430 injectTapOnDisplay(x, y, ADISPLAY_ID_DEFAULT);
431}
432
433void injectKeyOnDisplay(uint32_t keycode, int displayId) {
434 char *buf1, *bufDisplayId;
435 asprintf(&buf1, "%d", keycode);
436 asprintf(&bufDisplayId, "%d", displayId);
437 if (fork() == 0) {
438 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
Robert Carr1c4c5592018-09-24 13:18:43 -0700439 }
440}
441
Vishnu Naira066d902021-09-13 18:40:17 -0700442void injectKey(uint32_t keycode) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700443 injectKeyOnDisplay(keycode, ADISPLAY_ID_NONE);
Vishnu Naira066d902021-09-13 18:40:17 -0700444}
445
Robert Carr1c4c5592018-09-24 13:18:43 -0700446TEST_F(InputSurfacesTest, can_receive_input) {
447 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
448 surface->showAt(100, 100);
449
450 injectTap(101, 101);
451
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100452 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700453}
454
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100455/**
456 * Set up two surfaces side-by-side. Tap each surface.
457 * Next, swap the positions of the two surfaces. Inject tap into the two
458 * original locations. Ensure that the tap is received by the surfaces in the
459 * reverse order.
460 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700461TEST_F(InputSurfacesTest, input_respects_positioning) {
462 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
463 surface->showAt(100, 100);
464
465 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
466 surface2->showAt(200, 200);
467
468 injectTap(201, 201);
469 surface2->expectTap(1, 1);
470
471 injectTap(101, 101);
472 surface->expectTap(1, 1);
473
474 surface2->doTransaction([](auto &t, auto &sc) {
475 t.setPosition(sc, 100, 100);
476 });
477 surface->doTransaction([](auto &t, auto &sc) {
478 t.setPosition(sc, 200, 200);
479 });
480
481 injectTap(101, 101);
482 surface2->expectTap(1, 1);
483
484 injectTap(201, 201);
485 surface->expectTap(1, 1);
486}
487
488TEST_F(InputSurfacesTest, input_respects_layering) {
489 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
490 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
491
492 surface->showAt(10, 10);
493 surface2->showAt(10, 10);
494
495 surface->doTransaction([](auto &t, auto &sc) {
496 t.setLayer(sc, LAYER_BASE + 1);
497 });
498
499 injectTap(11, 11);
500 surface->expectTap(1, 1);
501
502 surface2->doTransaction([](auto &t, auto &sc) {
503 t.setLayer(sc, LAYER_BASE + 1);
504 });
505
506 injectTap(11, 11);
507 surface2->expectTap(1, 1);
508
509 surface2->doTransaction([](auto &t, auto &sc) {
510 t.hide(sc);
511 });
512
513 injectTap(11, 11);
514 surface->expectTap(1, 1);
515}
516
Vishnu Nairde19f852018-12-18 16:11:53 -0800517// Surface Insets are set to offset the client content and draw a border around the client surface
518// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
519// of the client content.
520TEST_F(InputSurfacesTest, input_respects_surface_insets) {
521 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
522 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
523 bgSurface->showAt(100, 100);
524
525 fgSurface->mInputInfo.surfaceInset = 5;
526 fgSurface->showAt(100, 100);
527
528 injectTap(106, 106);
529 fgSurface->expectTap(1, 1);
530
531 injectTap(101, 101);
532 bgSurface->expectTap(1, 1);
533}
534
Vishnu Nairfed7c122023-03-18 01:54:43 +0000535TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) {
536 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
537 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
538 bgSurface->showAt(100, 100);
539
540 fgSurface->mInputInfo.surfaceInset = 5;
541 fgSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
542 fgSurface->showAt(100, 100);
543
544 injectTap(106, 106);
545 fgSurface->expectTap(1, 1);
546
547 injectTap(101, 101);
548 bgSurface->expectTap(1, 1);
549}
550
Vishnu Nairde19f852018-12-18 16:11:53 -0800551// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
552TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
553 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
554 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
555 parentSurface->showAt(100, 100);
556
557 childSurface->mInputInfo.surfaceInset = 10;
558 childSurface->showAt(100, 100);
559
560 childSurface->doTransaction([&](auto &t, auto &sc) {
561 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000562 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800563 });
564
565 injectTap(106, 106);
566 childSurface->expectTap(1, 1);
567
568 injectTap(101, 101);
569 parentSurface->expectTap(1, 1);
570}
571
Arthur Hung118b1142019-05-08 21:25:59 +0800572// Ensure a surface whose insets are scaled, handles the touch offset correctly.
573TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
574 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
575 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
576 bgSurface->showAt(100, 100);
577
578 fgSurface->mInputInfo.surfaceInset = 5;
579 fgSurface->showAt(100, 100);
580
581 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
582
583 // expect = touch / scale - inset
584 injectTap(112, 124);
585 fgSurface->expectTap(1, 1);
586
587 injectTap(101, 101);
588 bgSurface->expectTap(1, 1);
589}
590
Ady Abraham282f1d72019-07-24 18:05:56 -0700591TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700592 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
Ady Abraham282f1d72019-07-24 18:05:56 -0700593 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700594 bgSurface->showAt(100, 100);
595
Ady Abraham282f1d72019-07-24 18:05:56 -0700596 // In case we pass the very big inset without any checking.
597 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
598 fgSurface->showAt(100, 100);
599
600 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
601
602 // expect no crash for overflow, and inset size to be clamped to surface size
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700603 injectTap(112, 124);
604 bgSurface->expectTap(12, 24);
Ady Abraham282f1d72019-07-24 18:05:56 -0700605}
606
Prabir Pradhan33da9462022-06-14 14:55:57 +0000607TEST_F(InputSurfacesTest, touchable_region) {
608 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
609
610 surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31});
611
612 surface->showAt(11, 22);
613
614 // A tap within the surface but outside the touchable region should not be sent to the surface.
615 injectTap(20, 30);
Egor Pasko5a67a562024-01-16 16:46:45 +0100616 EXPECT_EQ(surface->consumeEvent(/*timeout=*/200ms), nullptr);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000617
618 injectTap(31, 52);
619 surface->expectTap(20, 30);
620}
621
622TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
623 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
624 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
625 bgSurface->showAt(100, 100);
626
627 // Set the touchable region to the values at the limit of its corresponding type.
628 // Since the surface is offset from the origin, the touchable region will be transformed into
629 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
630 // against such a situation.
631 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
632
633 fgSurface->showAt(100, 100);
634
635 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
636 // surface receives touch.
637 injectTap(112, 124);
638 bgSurface->expectTap(12, 24);
639}
640
641TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
642 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
643 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
644 bgSurface->showAt(0, 0);
645
646 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
647 fgSurface->showAt(0, 0);
648
649 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
650
651 // Expect no crash for overflow.
652 injectTap(12, 24);
Prabir Pradhana4c59bd2023-04-10 20:54:04 +0000653 bgSurface->expectTap(12, 24);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000654}
655
Vishnu Nairde19f852018-12-18 16:11:53 -0800656// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
657TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
658 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
659 surface->doTransaction([](auto &t, auto &sc) {
660 Region transparentRegion(Rect(0, 0, 10, 10));
661 t.setTransparentRegionHint(sc, transparentRegion);
662 });
663 surface->showAt(100, 100);
664 injectTap(101, 101);
665 surface->expectTap(1, 1);
666}
667
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700668// TODO(b/139494112) update tests once we define expected behavior
669// Ensure we still send input to the surface regardless of surface visibility changes due to the
670// first buffer being submitted or alpha changes.
671// Original bug ref: b/120839715
672TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800673 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500674 std::unique_ptr<BlastInputSurface> bufferSurface =
675 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800676
677 bgSurface->showAt(10, 10);
678 bufferSurface->showAt(10, 10);
679
680 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700681 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800682
chaviw39d01472021-04-08 14:26:24 -0500683 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800684 injectTap(11, 11);
685 bufferSurface->expectTap(1, 1);
686}
687
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000688TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800689 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500690 std::unique_ptr<BlastInputSurface> bufferSurface =
691 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
692 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800693
694 bgSurface->showAt(10, 10);
695 bufferSurface->showAt(10, 10);
696
697 injectTap(11, 11);
698 bufferSurface->expectTap(1, 1);
699
700 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
701
702 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000703 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800704}
705
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700706TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800707 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
708 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
709
710 bgSurface->showAt(10, 10);
711 fgSurface->showAt(10, 10);
712
713 injectTap(11, 11);
714 fgSurface->expectTap(1, 1);
715
716 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
717
718 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700719 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800720}
721
722TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
723 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
724 std::unique_ptr<InputSurface> containerSurface =
725 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
726
727 bgSurface->showAt(10, 10);
728 containerSurface->showAt(10, 10);
729
730 injectTap(11, 11);
731 containerSurface->expectTap(1, 1);
732
733 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
734
735 injectTap(11, 11);
736 bgSurface->expectTap(1, 1);
737}
chaviwfbe5d9c2018-12-26 12:23:37 -0800738
Arthur Hungd20b2702019-01-14 18:16:16 +0800739TEST_F(InputSurfacesTest, input_respects_outscreen) {
740 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
741 surface->showAt(-1, -1);
742
743 injectTap(0, 0);
744 surface->expectTap(1, 1);
745}
arthurhungb4a0f852020-06-16 11:02:50 +0800746
747TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
748 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
749 std::unique_ptr<InputSurface> cursorSurface =
750 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
751
752 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800753 cursorSurface->showAt(10, 10);
754
755 injectTap(11, 11);
756 surface->expectTap(1, 1);
757}
Vishnu Nair958da932020-08-21 17:12:37 -0700758
759TEST_F(InputSurfacesTest, can_be_focused) {
760 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
761 surface->showAt(100, 100);
762 surface->requestFocus();
763
764 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700765
766 injectKey(AKEYCODE_V);
767 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700768}
chaviw44a6d2b2020-09-08 17:14:16 -0700769
770TEST_F(InputSurfacesTest, rotate_surface) {
771 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
772 surface->showAt(10, 10);
773 surface->doTransaction([](auto &t, auto &sc) {
774 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
775 });
776 injectTap(8, 11);
777 surface->expectTap(1, 2);
778
779 surface->doTransaction([](auto &t, auto &sc) {
780 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
781 });
782 injectTap(9, 8);
783 surface->expectTap(1, 2);
784
785 surface->doTransaction([](auto &t, auto &sc) {
786 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
787 });
788 injectTap(12, 9);
789 surface->expectTap(1, 2);
790}
791
792TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
793 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
794 surface->showAt(10, 10);
795 surface->doTransaction([](auto &t, auto &sc) {
796 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
797 });
798 injectTap(2, 12);
799 surface->expectTap(1, 2);
800
801 surface->doTransaction([](auto &t, auto &sc) {
802 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
803 });
804 injectTap(8, 2);
805 surface->expectTap(1, 2);
806
807 surface->doTransaction([](auto &t, auto &sc) {
808 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
809 });
810 injectTap(18, 8);
811 surface->expectTap(1, 2);
812}
813
814TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
815 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
816 surface->mInputInfo.surfaceInset = 5;
817 surface->showAt(100, 100);
818
819 surface->doTransaction([](auto &t, auto &sc) {
820 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
821 });
822 injectTap(40, 120);
823 surface->expectTap(5, 10);
824
825 surface->doTransaction([](auto &t, auto &sc) {
826 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
827 });
828 injectTap(80, 40);
829 surface->expectTap(5, 10);
830
831 surface->doTransaction([](auto &t, auto &sc) {
832 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
833 });
834 injectTap(160, 80);
835 surface->expectTap(5, 10);
836}
837
chaviw39cfa2e2020-11-04 14:19:02 -0800838TEST_F(InputSurfacesTest, touch_flag_obscured) {
839 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
840 surface->showAt(100, 100);
841
842 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
843 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
844 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800845 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000846 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
Bernardo Rufino602ef712020-12-21 11:02:18 +0000847 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
848 // the default obscured/untrusted touch filter introduced in S.
849 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800850 nonTouchableSurface->showAt(100, 100);
851
852 injectTap(190, 199);
853 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
854}
855
856TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
857 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
858 surface->showAt(100, 100);
859
860 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
861 // that will be tapped. Window behind gets touch, but with flag
862 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
863 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
864 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800865 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
866 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000867 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
868 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800869 nonTouchableSurface->showAt(0, 0);
870 parentSurface->showAt(100, 100);
871
872 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800873 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800874 t.reparent(sc, parentSurface->mSurfaceControl);
875 });
876
877 injectTap(190, 199);
878 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
879}
880
881TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
882 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
883 surface->showAt(100, 100);
884
885 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
886 // the touchable window. Window behind gets touch with no obscured flags.
887 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
888 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800889 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
890 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000891 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
892 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800893 nonTouchableSurface->showAt(0, 0);
894 parentSurface->showAt(50, 50);
895
896 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800897 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800898 t.reparent(sc, parentSurface->mSurfaceControl);
899 });
900
901 injectTap(101, 110);
902 surface->expectTap(1, 10);
903}
904
chaviw7e72caf2020-12-02 16:50:43 -0800905TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
906 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
907
908 std::unique_ptr<InputSurface> bufferSurface =
909 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800910 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000911 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800912
913 surface->showAt(10, 10);
914 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
915
916 injectTap(11, 11);
917 surface->expectTap(1, 1);
918}
919
920TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
921 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
922
chaviw39d01472021-04-08 14:26:24 -0500923 std::unique_ptr<BlastInputSurface> bufferSurface =
924 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800925 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000926 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800927
928 surface->showAt(10, 10);
929 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
930
931 injectTap(11, 11);
932 surface->expectTap(1, 1);
933}
934
Vishnu Naira066d902021-09-13 18:40:17 -0700935TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
936 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
937 surface->doTransaction(
938 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
939 surface->showAt(100, 100);
940
941 injectTap(101, 101);
942
943 EXPECT_NE(surface->consumeEvent(), nullptr);
944 EXPECT_NE(surface->consumeEvent(), nullptr);
945
946 surface->requestFocus();
947 surface->assertFocusChange(true);
948 injectKey(AKEYCODE_V);
949 surface->expectKey(AKEYCODE_V);
950}
951
952TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
953 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
954 surface->doTransaction([&](auto &t, auto &sc) {
955 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
956 t.setMatrix(sc, 2.0, 0, 0, 2.0);
957 });
958 surface->showAt(100, 100);
959
960 injectTap(101, 101);
961
962 EXPECT_NE(surface->consumeEvent(), nullptr);
963 EXPECT_NE(surface->consumeEvent(), nullptr);
964
965 surface->requestFocus();
966 surface->assertFocusChange(true);
967 injectKey(AKEYCODE_V);
968 surface->expectKey(AKEYCODE_V);
969}
970
971TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
972 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000973 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700974 surface->doTransaction(
975 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
976 surface->showAt(100, 100);
977 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800978 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000979 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700980 obscuringSurface->showAt(100, 100);
981 injectTap(101, 101);
Egor Pasko5a67a562024-01-16 16:46:45 +0100982 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -0700983
984 surface->requestFocus();
985 surface->assertFocusChange(true);
986 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +0100987 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -0700988}
989
990TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
991 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000992 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700993 surface->doTransaction(
994 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
995 surface->showAt(100, 100);
996 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800997 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000998 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700999 obscuringSurface->showAt(190, 190);
1000
1001 injectTap(101, 101);
1002
Egor Pasko5a67a562024-01-16 16:46:45 +01001003 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001004
1005 surface->requestFocus();
1006 surface->assertFocusChange(true);
1007 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001008 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001009}
1010
1011TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
1012 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1013 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1014
1015 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1016 surface->showAt(100, 100);
1017 surface->doTransaction([&](auto &t, auto &sc) {
1018 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1019 t.reparent(sc, parentSurface->mSurfaceControl);
1020 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
1021 });
1022
1023 injectTap(101, 101);
1024
Egor Pasko5a67a562024-01-16 16:46:45 +01001025 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001026
1027 surface->requestFocus();
1028 surface->assertFocusChange(true);
1029 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001030 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001031}
1032
1033TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
1034 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1035 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1036
1037 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1038 surface->doTransaction([&](auto &t, auto &sc) {
1039 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1040 t.reparent(sc, parentSurface->mSurfaceControl);
1041 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1042 });
1043 surface->showAt(100, 100);
1044
1045 injectTap(111, 111);
1046
Egor Pasko5a67a562024-01-16 16:46:45 +01001047 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001048
1049 surface->requestFocus();
1050 surface->assertFocusChange(true);
1051 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001052 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001053}
1054
Arthur Hung49d525a2021-11-19 15:11:51 +00001055TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1056 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1057
1058 std::unique_ptr<BlastInputSurface> bufferSurface =
1059 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1060
1061 surface->showAt(100, 100);
1062 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
1063 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1064
1065 injectTap(101, 101);
1066 surface->expectTap(1, 1);
1067}
1068
Vishnu Naira066d902021-09-13 18:40:17 -07001069TEST_F(InputSurfacesTest, drop_input_policy) {
1070 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1071 surface->doTransaction(
1072 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
1073 surface->showAt(100, 100);
1074
1075 injectTap(101, 101);
1076
Egor Pasko5a67a562024-01-16 16:46:45 +01001077 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001078
1079 surface->requestFocus();
1080 surface->assertFocusChange(true);
1081 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001082 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001083}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001084
Prabir Pradhan8c285982022-01-28 09:19:39 -08001085TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1086 std::unique_ptr<InputSurface> bufferSurface =
1087 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1088
1089 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1090
1091 bufferSurface->requestFocus();
1092 bufferSurface->assertFocusChange(true);
1093}
1094
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001095/**
1096 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1097 * its own crop.
1098 */
1099TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1100 std::unique_ptr<InputSurface> parentContainer =
1101 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1102 std::unique_ptr<InputSurface> containerSurface =
1103 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1104 containerSurface->doTransaction(
1105 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1106 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1107 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1108 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1109 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1110
1111 // Receives events inside its own crop
1112 injectTap(21, 21);
1113 containerSurface->expectTap(1, 1); // Event is in layer space
1114
1115 // Does not receive events outside its crop
1116 injectTap(26, 26);
Egor Pasko5a67a562024-01-16 16:46:45 +01001117 EXPECT_EQ(containerSurface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001118}
1119
1120/**
1121 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1122 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1123 */
1124TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
1125 std::unique_ptr<InputSurface> parentContainer =
1126 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1127 std::unique_ptr<InputSurface> containerSurface =
1128 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1129 containerSurface->doTransaction(
1130 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1131 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1132 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1133 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1134 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1135
1136 // Receives events inside parent bounds
1137 injectTap(21, 21);
1138 containerSurface->expectTap(1, 1); // Event is in layer space
1139
1140 // Does not receive events outside parent bounds
1141 injectTap(31, 31);
Egor Pasko5a67a562024-01-16 16:46:45 +01001142 EXPECT_EQ(containerSurface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001143}
1144
1145/**
1146 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1147 * layer's bounds. The input events should be in the layer's coordinate space.
1148 */
1149TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1150 std::unique_ptr<InputSurface> cropLayer =
1151 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1152 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1153
1154 std::unique_ptr<InputSurface> containerSurface =
1155 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1156 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1157 containerSurface->mInputInfo.touchableRegionCropHandle =
1158 cropLayer->mSurfaceControl->getHandle();
1159 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1160
1161 // Receives events inside crop layer bounds
1162 injectTap(51, 51);
1163 containerSurface->expectTap(41, 41); // Event is in layer space
1164
1165 // Does not receive events outside crop layer bounds
1166 injectTap(21, 21);
1167 injectTap(71, 71);
Egor Pasko5a67a562024-01-16 16:46:45 +01001168 EXPECT_EQ(containerSurface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001169}
1170
Linus Tufvessona1858822022-03-04 09:32:07 +00001171TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1172 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1173
1174 parent->showAt(100, 100);
1175 injectTap(101, 101);
1176 parent->expectTap(1, 1);
1177
1178 std::unique_ptr<InputSurface> childContainerSurface =
1179 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1180 childContainerSurface->showAt(0, 0);
1181 childContainerSurface->doTransaction(
1182 [&](auto &t, auto &sc) { t.reparent(sc, parent->mSurfaceControl); });
1183 injectTap(101, 101);
1184
Egor Pasko5a67a562024-01-16 16:46:45 +01001185 EXPECT_EQ(parent->consumeEvent(/*timeout=*/100ms), nullptr);
Linus Tufvessona1858822022-03-04 09:32:07 +00001186}
1187
Vishnu Nair16a938f2021-09-24 07:14:54 -07001188class MultiDisplayTests : public InputSurfacesTest {
1189public:
1190 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Prabir Pradhand0aba782021-12-14 00:44:21 -08001191 void TearDown() override {
1192 for (auto &token : mVirtualDisplays) {
1193 SurfaceComposerClient::destroyDisplay(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001194 }
1195 InputSurfacesTest::TearDown();
1196 }
1197
Prabir Pradhand0aba782021-12-14 00:44:21 -08001198 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1199 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001200 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001201 sp<IGraphicBufferProducer> producer;
1202 BufferQueue::createBufferQueue(&producer, &consumer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001203 consumer->setConsumerName(String8("Virtual disp consumer"));
1204 consumer->setDefaultBufferSize(width, height);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001205 mProducers.push_back(producer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001206
Prabir Pradhand0aba782021-12-14 00:44:21 -08001207 std::string name = "VirtualDisplay";
1208 name += std::to_string(mVirtualDisplays.size());
1209 sp<IBinder> token = SurfaceComposerClient::createDisplay(String8(name.c_str()), isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001210 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001211 t.setDisplaySurface(token, producer);
1212 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1213 t.setDisplayLayerStack(token, layerStack);
1214 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1215 {offsetX, offsetY, offsetX + width, offsetY + height});
Vishnu Nair16a938f2021-09-24 07:14:54 -07001216 t.apply(true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001217
1218 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001219 }
1220
Prabir Pradhand0aba782021-12-14 00:44:21 -08001221 std::vector<sp<IBinder>> mVirtualDisplays;
1222 std::vector<sp<IGraphicBufferProducer>> mProducers;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001223};
1224
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001225TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001226 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1227 // Do not create a display associated with the LayerStack.
1228 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1229 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1230 surface->showAt(100, 100);
1231
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001232 // Touches should be dropped if the layer is on an invalid display.
Prabir Pradhand0aba782021-12-14 00:44:21 -08001233 injectTapOnDisplay(101, 101, layerStack.id);
Egor Pasko5a67a562024-01-16 16:46:45 +01001234 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001235
1236 // However, we still let the window be focused and receive keys.
1237 surface->requestFocus(layerStack.id);
1238 surface->assertFocusChange(true);
1239
1240 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1241 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001242}
1243
1244TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1245 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1246 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1247 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1248 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1249 surface->showAt(100, 100);
1250
1251 injectTapOnDisplay(101, 101, layerStack.id);
1252 surface->expectTap(1, 1);
1253
1254 surface->requestFocus(layerStack.id);
1255 surface->assertFocusChange(true);
1256 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1257 surface->expectKey(AKEYCODE_V);
1258}
1259
Vishnu Nair16a938f2021-09-24 07:14:54 -07001260TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1261 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1262 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1263 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1264 surface->doTransaction([&](auto &t, auto &sc) {
1265 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1266 t.setLayerStack(sc, layerStack);
1267 });
1268 surface->showAt(100, 100);
1269
Prabir Pradhand0aba782021-12-14 00:44:21 -08001270 injectTapOnDisplay(101, 101, layerStack.id);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001271
Egor Pasko5a67a562024-01-16 16:46:45 +01001272 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001273
1274 surface->requestFocus(layerStack.id);
1275 surface->assertFocusChange(true);
1276 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
Egor Pasko5a67a562024-01-16 16:46:45 +01001277 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001278}
1279
1280TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1281 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001282
1283 // Create the secure display as system, because only certain users can create secure displays.
1284 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001285 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001286 // Change the uid back to root.
1287 seteuid(AID_ROOT);
1288
Vishnu Nair16a938f2021-09-24 07:14:54 -07001289 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1290 surface->doTransaction([&](auto &t, auto &sc) {
1291 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1292 t.setLayerStack(sc, layerStack);
1293 });
1294 surface->showAt(100, 100);
1295
1296 injectTapOnDisplay(101, 101, layerStack.id);
1297 EXPECT_NE(surface->consumeEvent(), nullptr);
1298 EXPECT_NE(surface->consumeEvent(), nullptr);
1299
1300 surface->requestFocus(layerStack.id);
1301 surface->assertFocusChange(true);
1302 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1303
1304 surface->expectKey(AKEYCODE_V);
1305}
1306
Vishnu Nair958da932020-08-21 17:12:37 -07001307} // namespace android::test