blob: b9ab80310194d5daed635f5b263e44c9f4abb6ab [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
Patrick Williams0a4981a2023-07-28 15:08:52 -050027#include <android/gui/BnWindowInfosReportedListener.h>
Vishnu Naira066d902021-09-13 18:40:17 -070028#include <android/keycodes.h>
Vishnu Nairde19f852018-12-18 16:11:53 -080029#include <android/native_window.h>
30
Robert Carr1c4c5592018-09-24 13:18:43 -070031#include <binder/Binder.h>
32#include <binder/IServiceManager.h>
33#include <binder/Parcel.h>
34#include <binder/ProcessState.h>
35
Vishnu Nairde19f852018-12-18 16:11:53 -080036#include <gui/ISurfaceComposer.h>
37#include <gui/Surface.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070038#include <gui/SurfaceComposerClient.h>
39#include <gui/SurfaceControl.h>
40
Chris Ye0783e992020-06-02 21:34:49 -070041#include <android/os/IInputFlinger.h>
chaviw98318de2021-05-19 16:45:23 -050042#include <gui/WindowInfo.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070043#include <input/Input.h>
Chris Ye0783e992020-06-02 21:34:49 -070044#include <input/InputTransport.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070045
Marin Shalamanova7fe3042021-01-29 21:02:08 +010046#include <ui/DisplayMode.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070047#include <ui/Rect.h>
48#include <ui/Region.h>
49
Prabir Pradhand0aba782021-12-14 00:44:21 -080050#include <private/android_filesystem_config.h>
51
Chris Ye0783e992020-06-02 21:34:49 -070052using android::os::IInputFlinger;
Robert Carr1c4c5592018-09-24 13:18:43 -070053
chaviw39d01472021-04-08 14:26:24 -050054using android::hardware::graphics::common::V1_1::BufferUsage;
55
chaviw98318de2021-05-19 16:45:23 -050056using android::gui::FocusRequest;
57using android::gui::InputApplicationInfo;
58using android::gui::TouchOcclusionMode;
59using android::gui::WindowInfo;
60
Vishnu Nair958da932020-08-21 17:12:37 -070061namespace android::test {
Robert Carr1c4c5592018-09-24 13:18:43 -070062
Vishnu Nairde19f852018-12-18 16:11:53 -080063using Transaction = SurfaceComposerClient::Transaction;
64
Robert Carr1c4c5592018-09-24 13:18:43 -070065sp<IInputFlinger> getInputFlinger() {
66 sp<IBinder> input(defaultServiceManager()->getService(
67 String16("inputflinger")));
68 if (input == nullptr) {
69 ALOGE("Failed to link to input service");
70 } else { ALOGE("Linked to input"); }
71 return interface_cast<IInputFlinger>(input);
72}
73
74// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
75static const int LAYER_BASE = INT32_MAX - 10;
Chris Ye6c4243b2020-07-22 12:07:12 -070076static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070077
Patrick Williams0a4981a2023-07-28 15:08:52 -050078class SynchronousWindowInfosReportedListener : public gui::BnWindowInfosReportedListener {
79public:
80 binder::Status onWindowInfosReported() override {
81 std::lock_guard<std::mutex> lock{mMutex};
82 mWindowInfosReported = true;
83 mConditionVariable.notify_one();
84 return binder::Status::ok();
85 }
86
87 void wait() {
88 std::unique_lock<std::mutex> lock{mMutex};
89 mConditionVariable.wait(lock, [&] { return mWindowInfosReported; });
90 }
91
92private:
93 std::mutex mMutex;
94 std::condition_variable mConditionVariable;
95 bool mWindowInfosReported{false};
96};
97
Robert Carr1c4c5592018-09-24 13:18:43 -070098class InputSurface {
99public:
Linus Tufvessona1858822022-03-04 09:32:07 +0000100 InputSurface(const sp<SurfaceControl> &sc, int width, int height, bool noInputChannel = false) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800101 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -0700102
103 mInputFlinger = getInputFlinger();
Linus Tufvessona1858822022-03-04 09:32:07 +0000104 if (noInputChannel) {
105 mInputInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, true);
106 } else {
107 mClientChannel = std::make_shared<InputChannel>();
108 mInputFlinger->createInputChannel("testchannels", mClientChannel.get());
109 mInputInfo.token = mClientChannel->getConnectionToken();
110 mInputConsumer = new InputConsumer(mClientChannel);
111 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700112
Linus Tufvessona1858822022-03-04 09:32:07 +0000113 mInputInfo.name = "Test info";
114 mInputInfo.dispatchingTimeout = 5s;
115 mInputInfo.globalScaleFactor = 1.0;
116 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
Robert Carr1c4c5592018-09-24 13:18:43 -0700117
Linus Tufvessona1858822022-03-04 09:32:07 +0000118 InputApplicationInfo aInfo;
119 aInfo.token = new BBinder();
120 aInfo.name = "Test app info";
121 aInfo.dispatchingTimeoutMillis =
122 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
123 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700124 }
125
Vishnu Nairde19f852018-12-18 16:11:53 -0800126 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
127 int width, int height) {
128 sp<SurfaceControl> surfaceControl =
129 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800130 PIXEL_FORMAT_RGBA_8888,
131 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -0800132 return std::make_unique<InputSurface>(surfaceControl, width, height);
133 }
134
135 static std::unique_ptr<InputSurface> makeBufferInputSurface(
136 const sp<SurfaceComposerClient> &scc, int width, int height) {
137 sp<SurfaceControl> surfaceControl =
138 scc->createSurface(String8("Test Buffer Surface"), width, height,
139 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
140 return std::make_unique<InputSurface>(surfaceControl, width, height);
141 }
142
143 static std::unique_ptr<InputSurface> makeContainerInputSurface(
144 const sp<SurfaceComposerClient> &scc, int width, int height) {
145 sp<SurfaceControl> surfaceControl =
146 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
147 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
148 ISurfaceComposerClient::eFXSurfaceContainer);
149 return std::make_unique<InputSurface>(surfaceControl, width, height);
150 }
151
Linus Tufvessona1858822022-03-04 09:32:07 +0000152 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
153 const sp<SurfaceComposerClient> &scc, int width, int height) {
154 sp<SurfaceControl> surfaceControl =
155 scc->createSurface(String8("Test Container Surface"), 100 /* height */,
156 100 /* width */, PIXEL_FORMAT_RGBA_8888,
157 ISurfaceComposerClient::eFXSurfaceContainer);
158 return std::make_unique<InputSurface>(surfaceControl, width, height,
159 true /* noInputChannel */);
160 }
161
arthurhungb4a0f852020-06-16 11:02:50 +0800162 static std::unique_ptr<InputSurface> makeCursorInputSurface(
163 const sp<SurfaceComposerClient> &scc, int width, int height) {
164 sp<SurfaceControl> surfaceControl =
165 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
166 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
167 ISurfaceComposerClient::eCursorWindow);
168 return std::make_unique<InputSurface>(surfaceControl, width, height);
169 }
170
Egor Pasko5a67a562024-01-16 16:46:45 +0100171 InputEvent* consumeEvent(std::chrono::milliseconds timeout = 3000ms) {
172 mClientChannel->waitForMessage(timeout);
Robert Carr1c4c5592018-09-24 13:18:43 -0700173
174 InputEvent *ev;
175 uint32_t seqId;
176 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
177 if (consumed != OK) {
178 return nullptr;
179 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100180 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
181 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700182 return ev;
183 }
184
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100185 void assertFocusChange(bool hasFocus) {
186 InputEvent *ev = consumeEvent();
187 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700188 ASSERT_EQ(InputEventType::FOCUS, ev->getType());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100189 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
190 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
191 }
192
Robert Carr1c4c5592018-09-24 13:18:43 -0700193 void expectTap(int x, int y) {
194 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100195 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700196 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700197 MotionEvent* mev = static_cast<MotionEvent*>(ev);
198 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
199 EXPECT_EQ(x, mev->getX(0));
200 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800201 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700202
203 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100204 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700205 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700206 mev = static_cast<MotionEvent*>(ev);
207 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
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
chaviw39cfa2e2020-11-04 14:19:02 -0800211 void expectTapWithFlag(int x, int y, int32_t flags) {
212 InputEvent *ev = consumeEvent();
213 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700214 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800215 MotionEvent *mev = static_cast<MotionEvent *>(ev);
216 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
217 EXPECT_EQ(x, mev->getX(0));
218 EXPECT_EQ(y, mev->getY(0));
219 EXPECT_EQ(flags, mev->getFlags() & flags);
220
221 ev = consumeEvent();
222 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700223 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800224 mev = static_cast<MotionEvent *>(ev);
225 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
226 EXPECT_EQ(flags, mev->getFlags() & flags);
227 }
228
Prabir Pradhand0aba782021-12-14 00:44:21 -0800229 void expectTapInDisplayCoordinates(int displayX, int displayY) {
230 InputEvent *ev = consumeEvent();
231 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700232 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800233 MotionEvent *mev = static_cast<MotionEvent *>(ev);
234 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
235 const PointerCoords &coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
236 EXPECT_EQ(displayX, coords.getX());
237 EXPECT_EQ(displayY, coords.getY());
238 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
239
240 ev = consumeEvent();
241 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700242 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800243 mev = static_cast<MotionEvent *>(ev);
244 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
245 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
246 }
247
Vishnu Naira066d902021-09-13 18:40:17 -0700248 void expectKey(uint32_t keycode) {
249 InputEvent *ev = consumeEvent();
250 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700251 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700252 KeyEvent *keyEvent = static_cast<KeyEvent *>(ev);
253 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
254 EXPECT_EQ(keycode, keyEvent->getKeyCode());
255 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
256
257 ev = consumeEvent();
258 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700259 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700260 keyEvent = static_cast<KeyEvent *>(ev);
261 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
262 EXPECT_EQ(keycode, keyEvent->getKeyCode());
263 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
264 }
265
chaviw39d01472021-04-08 14:26:24 -0500266 virtual ~InputSurface() {
Linus Tufvessona1858822022-03-04 09:32:07 +0000267 if (mClientChannel) {
268 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
269 }
chaviw39d01472021-04-08 14:26:24 -0500270 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700271
chaviw39d01472021-04-08 14:26:24 -0500272 virtual void doTransaction(
273 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
274 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700275 SurfaceComposerClient::Transaction t;
276 transactionBody(t, mSurfaceControl);
277 t.apply(true);
278 }
279
chaviw39d01472021-04-08 14:26:24 -0500280 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700281 SurfaceComposerClient::Transaction t;
282 t.show(mSurfaceControl);
283 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
284 t.setLayer(mSurfaceControl, LAYER_BASE);
285 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800286 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700287 t.setAlpha(mSurfaceControl, 1);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500288 auto reportedListener = sp<SynchronousWindowInfosReportedListener>::make();
289 t.addWindowInfosReportedListener(reportedListener);
290 t.apply();
291 reportedListener->wait();
Robert Carr1c4c5592018-09-24 13:18:43 -0700292 }
293
Vishnu Nair16a938f2021-09-24 07:14:54 -0700294 void requestFocus(int displayId = ADISPLAY_ID_DEFAULT) {
Vishnu Nair958da932020-08-21 17:12:37 -0700295 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800296 FocusRequest request;
297 request.token = mInputInfo.token;
298 request.windowName = mInputInfo.name;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800299 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700300 request.displayId = displayId;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800301 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700302 t.apply(true);
303 }
304
Robert Carr1c4c5592018-09-24 13:18:43 -0700305public:
306 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500307 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700308 sp<IInputFlinger> mInputFlinger;
309
chaviw98318de2021-05-19 16:45:23 -0500310 WindowInfo mInputInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700311
312 PreallocatedInputEventFactory mInputEventFactory;
313 InputConsumer* mInputConsumer;
314};
315
chaviw39d01472021-04-08 14:26:24 -0500316class BlastInputSurface : public InputSurface {
317public:
318 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
319 int height)
320 : InputSurface(sc, width, height) {
321 mParentSurfaceControl = parentSc;
322 }
323
324 ~BlastInputSurface() = default;
325
326 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
327 const sp<SurfaceComposerClient> &scc, int width, int height) {
328 sp<SurfaceControl> parentSc =
329 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
330 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
331 ISurfaceComposerClient::eFXSurfaceContainer);
332
333 sp<SurfaceControl> surfaceControl =
334 scc->createSurface(String8("Test Buffer Surface"), width, height,
335 PIXEL_FORMAT_RGBA_8888,
336 ISurfaceComposerClient::eFXSurfaceBufferState,
337 parentSc->getHandle());
338 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
339 }
340
341 void doTransaction(
342 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
343 transactionBody) override {
344 SurfaceComposerClient::Transaction t;
345 transactionBody(t, mParentSurfaceControl);
346 t.apply(true);
347 }
348
349 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
350 SurfaceComposerClient::Transaction t;
351 t.show(mParentSurfaceControl);
352 t.setLayer(mParentSurfaceControl, LAYER_BASE);
353 t.setPosition(mParentSurfaceControl, x, y);
354 t.setCrop(mParentSurfaceControl, crop);
355
356 t.show(mSurfaceControl);
357 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
358 t.setCrop(mSurfaceControl, crop);
359 t.setAlpha(mSurfaceControl, 1);
360 t.apply(true);
361 }
362
363private:
364 sp<SurfaceControl> mParentSurfaceControl;
365};
366
Robert Carr1c4c5592018-09-24 13:18:43 -0700367class InputSurfacesTest : public ::testing::Test {
368public:
369 InputSurfacesTest() {
370 ProcessState::self()->startThreadPool();
371 }
372
373 void SetUp() {
374 mComposerClient = new SurfaceComposerClient;
375 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Huihong Luo31b5ac22022-08-15 20:38:10 -0700376 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
377 ASSERT_FALSE(ids.empty());
378 // display 0 is picked for now, can extend to support all displays if needed
379 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100380 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800381
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100382 ui::DisplayMode mode;
383 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800384
385 // After a new buffer is queued, SurfaceFlinger is notified and will
386 // latch the new buffer on next vsync. Let's heuristically wait for 3
387 // vsyncs.
Alec Mouri55e31032023-10-02 20:34:18 +0000388 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.peakRefreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700389 }
390
391 void TearDown() {
392 mComposerClient->dispose();
393 }
394
395 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800396 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
397 }
398
chaviw39d01472021-04-08 14:26:24 -0500399 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
400 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
401 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
402 sp<GraphicBuffer> buffer =
403 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
404 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800405 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700406 }
407
408 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800409 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700410};
411
Vishnu Nair16a938f2021-09-24 07:14:54 -0700412void injectTapOnDisplay(int x, int y, int displayId) {
413 char *buf1, *buf2, *bufDisplayId;
Robert Carr1c4c5592018-09-24 13:18:43 -0700414 asprintf(&buf1, "%d", x);
415 asprintf(&buf2, "%d", y);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700416 asprintf(&bufDisplayId, "%d", displayId);
Robert Carr1c4c5592018-09-24 13:18:43 -0700417 if (fork() == 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700418 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
419 }
420}
421
422void injectTap(int x, int y) {
423 injectTapOnDisplay(x, y, ADISPLAY_ID_DEFAULT);
424}
425
426void injectKeyOnDisplay(uint32_t keycode, int displayId) {
427 char *buf1, *bufDisplayId;
428 asprintf(&buf1, "%d", keycode);
429 asprintf(&bufDisplayId, "%d", displayId);
430 if (fork() == 0) {
431 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
Robert Carr1c4c5592018-09-24 13:18:43 -0700432 }
433}
434
Vishnu Naira066d902021-09-13 18:40:17 -0700435void injectKey(uint32_t keycode) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700436 injectKeyOnDisplay(keycode, ADISPLAY_ID_NONE);
Vishnu Naira066d902021-09-13 18:40:17 -0700437}
438
Robert Carr1c4c5592018-09-24 13:18:43 -0700439TEST_F(InputSurfacesTest, can_receive_input) {
440 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
441 surface->showAt(100, 100);
442
443 injectTap(101, 101);
444
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100445 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700446}
447
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100448/**
449 * Set up two surfaces side-by-side. Tap each surface.
450 * Next, swap the positions of the two surfaces. Inject tap into the two
451 * original locations. Ensure that the tap is received by the surfaces in the
452 * reverse order.
453 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700454TEST_F(InputSurfacesTest, input_respects_positioning) {
455 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
456 surface->showAt(100, 100);
457
458 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
459 surface2->showAt(200, 200);
460
461 injectTap(201, 201);
462 surface2->expectTap(1, 1);
463
464 injectTap(101, 101);
465 surface->expectTap(1, 1);
466
467 surface2->doTransaction([](auto &t, auto &sc) {
468 t.setPosition(sc, 100, 100);
469 });
470 surface->doTransaction([](auto &t, auto &sc) {
471 t.setPosition(sc, 200, 200);
472 });
473
474 injectTap(101, 101);
475 surface2->expectTap(1, 1);
476
477 injectTap(201, 201);
478 surface->expectTap(1, 1);
479}
480
481TEST_F(InputSurfacesTest, input_respects_layering) {
482 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
483 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
484
485 surface->showAt(10, 10);
486 surface2->showAt(10, 10);
487
488 surface->doTransaction([](auto &t, auto &sc) {
489 t.setLayer(sc, LAYER_BASE + 1);
490 });
491
492 injectTap(11, 11);
493 surface->expectTap(1, 1);
494
495 surface2->doTransaction([](auto &t, auto &sc) {
496 t.setLayer(sc, LAYER_BASE + 1);
497 });
498
499 injectTap(11, 11);
500 surface2->expectTap(1, 1);
501
502 surface2->doTransaction([](auto &t, auto &sc) {
503 t.hide(sc);
504 });
505
506 injectTap(11, 11);
507 surface->expectTap(1, 1);
508}
509
Vishnu Nairde19f852018-12-18 16:11:53 -0800510// Surface Insets are set to offset the client content and draw a border around the client surface
511// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
512// of the client content.
513TEST_F(InputSurfacesTest, input_respects_surface_insets) {
514 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
515 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
516 bgSurface->showAt(100, 100);
517
518 fgSurface->mInputInfo.surfaceInset = 5;
519 fgSurface->showAt(100, 100);
520
521 injectTap(106, 106);
522 fgSurface->expectTap(1, 1);
523
524 injectTap(101, 101);
525 bgSurface->expectTap(1, 1);
526}
527
Vishnu Nairfed7c122023-03-18 01:54:43 +0000528TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) {
529 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
530 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
531 bgSurface->showAt(100, 100);
532
533 fgSurface->mInputInfo.surfaceInset = 5;
534 fgSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
535 fgSurface->showAt(100, 100);
536
537 injectTap(106, 106);
538 fgSurface->expectTap(1, 1);
539
540 injectTap(101, 101);
541 bgSurface->expectTap(1, 1);
542}
543
Vishnu Nairde19f852018-12-18 16:11:53 -0800544// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
545TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
546 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
547 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
548 parentSurface->showAt(100, 100);
549
550 childSurface->mInputInfo.surfaceInset = 10;
551 childSurface->showAt(100, 100);
552
553 childSurface->doTransaction([&](auto &t, auto &sc) {
554 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000555 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800556 });
557
558 injectTap(106, 106);
559 childSurface->expectTap(1, 1);
560
561 injectTap(101, 101);
562 parentSurface->expectTap(1, 1);
563}
564
Arthur Hung118b1142019-05-08 21:25:59 +0800565// Ensure a surface whose insets are scaled, handles the touch offset correctly.
566TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
567 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
568 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
569 bgSurface->showAt(100, 100);
570
571 fgSurface->mInputInfo.surfaceInset = 5;
572 fgSurface->showAt(100, 100);
573
574 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
575
576 // expect = touch / scale - inset
577 injectTap(112, 124);
578 fgSurface->expectTap(1, 1);
579
580 injectTap(101, 101);
581 bgSurface->expectTap(1, 1);
582}
583
Ady Abraham282f1d72019-07-24 18:05:56 -0700584TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700585 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
Ady Abraham282f1d72019-07-24 18:05:56 -0700586 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700587 bgSurface->showAt(100, 100);
588
Ady Abraham282f1d72019-07-24 18:05:56 -0700589 // In case we pass the very big inset without any checking.
590 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
591 fgSurface->showAt(100, 100);
592
593 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
594
595 // expect no crash for overflow, and inset size to be clamped to surface size
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700596 injectTap(112, 124);
597 bgSurface->expectTap(12, 24);
Ady Abraham282f1d72019-07-24 18:05:56 -0700598}
599
Prabir Pradhan33da9462022-06-14 14:55:57 +0000600TEST_F(InputSurfacesTest, touchable_region) {
601 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
602
603 surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31});
604
605 surface->showAt(11, 22);
606
607 // A tap within the surface but outside the touchable region should not be sent to the surface.
608 injectTap(20, 30);
Egor Pasko5a67a562024-01-16 16:46:45 +0100609 EXPECT_EQ(surface->consumeEvent(/*timeout=*/200ms), nullptr);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000610
611 injectTap(31, 52);
612 surface->expectTap(20, 30);
613}
614
615TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
616 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
617 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
618 bgSurface->showAt(100, 100);
619
620 // Set the touchable region to the values at the limit of its corresponding type.
621 // Since the surface is offset from the origin, the touchable region will be transformed into
622 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
623 // against such a situation.
624 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
625
626 fgSurface->showAt(100, 100);
627
628 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
629 // surface receives touch.
630 injectTap(112, 124);
631 bgSurface->expectTap(12, 24);
632}
633
634TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
635 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
636 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
637 bgSurface->showAt(0, 0);
638
639 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
640 fgSurface->showAt(0, 0);
641
642 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
643
644 // Expect no crash for overflow.
645 injectTap(12, 24);
Prabir Pradhana4c59bd2023-04-10 20:54:04 +0000646 bgSurface->expectTap(12, 24);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000647}
648
Vishnu Nairde19f852018-12-18 16:11:53 -0800649// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
650TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
651 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
652 surface->doTransaction([](auto &t, auto &sc) {
653 Region transparentRegion(Rect(0, 0, 10, 10));
654 t.setTransparentRegionHint(sc, transparentRegion);
655 });
656 surface->showAt(100, 100);
657 injectTap(101, 101);
658 surface->expectTap(1, 1);
659}
660
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700661// TODO(b/139494112) update tests once we define expected behavior
662// Ensure we still send input to the surface regardless of surface visibility changes due to the
663// first buffer being submitted or alpha changes.
664// Original bug ref: b/120839715
665TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800666 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500667 std::unique_ptr<BlastInputSurface> bufferSurface =
668 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800669
670 bgSurface->showAt(10, 10);
671 bufferSurface->showAt(10, 10);
672
673 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700674 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800675
chaviw39d01472021-04-08 14:26:24 -0500676 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800677 injectTap(11, 11);
678 bufferSurface->expectTap(1, 1);
679}
680
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000681TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800682 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500683 std::unique_ptr<BlastInputSurface> bufferSurface =
684 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
685 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800686
687 bgSurface->showAt(10, 10);
688 bufferSurface->showAt(10, 10);
689
690 injectTap(11, 11);
691 bufferSurface->expectTap(1, 1);
692
693 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
694
695 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000696 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800697}
698
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700699TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800700 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
701 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
702
703 bgSurface->showAt(10, 10);
704 fgSurface->showAt(10, 10);
705
706 injectTap(11, 11);
707 fgSurface->expectTap(1, 1);
708
709 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
710
711 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700712 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800713}
714
715TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
716 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
717 std::unique_ptr<InputSurface> containerSurface =
718 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
719
720 bgSurface->showAt(10, 10);
721 containerSurface->showAt(10, 10);
722
723 injectTap(11, 11);
724 containerSurface->expectTap(1, 1);
725
726 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
727
728 injectTap(11, 11);
729 bgSurface->expectTap(1, 1);
730}
chaviwfbe5d9c2018-12-26 12:23:37 -0800731
Arthur Hungd20b2702019-01-14 18:16:16 +0800732TEST_F(InputSurfacesTest, input_respects_outscreen) {
733 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
734 surface->showAt(-1, -1);
735
736 injectTap(0, 0);
737 surface->expectTap(1, 1);
738}
arthurhungb4a0f852020-06-16 11:02:50 +0800739
740TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
741 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
742 std::unique_ptr<InputSurface> cursorSurface =
743 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
744
745 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800746 cursorSurface->showAt(10, 10);
747
748 injectTap(11, 11);
749 surface->expectTap(1, 1);
750}
Vishnu Nair958da932020-08-21 17:12:37 -0700751
752TEST_F(InputSurfacesTest, can_be_focused) {
753 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
754 surface->showAt(100, 100);
755 surface->requestFocus();
756
757 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700758
759 injectKey(AKEYCODE_V);
760 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700761}
chaviw44a6d2b2020-09-08 17:14:16 -0700762
763TEST_F(InputSurfacesTest, rotate_surface) {
764 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
765 surface->showAt(10, 10);
766 surface->doTransaction([](auto &t, auto &sc) {
767 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
768 });
769 injectTap(8, 11);
770 surface->expectTap(1, 2);
771
772 surface->doTransaction([](auto &t, auto &sc) {
773 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
774 });
775 injectTap(9, 8);
776 surface->expectTap(1, 2);
777
778 surface->doTransaction([](auto &t, auto &sc) {
779 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
780 });
781 injectTap(12, 9);
782 surface->expectTap(1, 2);
783}
784
785TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
786 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
787 surface->showAt(10, 10);
788 surface->doTransaction([](auto &t, auto &sc) {
789 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
790 });
791 injectTap(2, 12);
792 surface->expectTap(1, 2);
793
794 surface->doTransaction([](auto &t, auto &sc) {
795 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
796 });
797 injectTap(8, 2);
798 surface->expectTap(1, 2);
799
800 surface->doTransaction([](auto &t, auto &sc) {
801 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
802 });
803 injectTap(18, 8);
804 surface->expectTap(1, 2);
805}
806
807TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
808 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
809 surface->mInputInfo.surfaceInset = 5;
810 surface->showAt(100, 100);
811
812 surface->doTransaction([](auto &t, auto &sc) {
813 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
814 });
815 injectTap(40, 120);
816 surface->expectTap(5, 10);
817
818 surface->doTransaction([](auto &t, auto &sc) {
819 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
820 });
821 injectTap(80, 40);
822 surface->expectTap(5, 10);
823
824 surface->doTransaction([](auto &t, auto &sc) {
825 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
826 });
827 injectTap(160, 80);
828 surface->expectTap(5, 10);
829}
830
chaviw39cfa2e2020-11-04 14:19:02 -0800831TEST_F(InputSurfacesTest, touch_flag_obscured) {
832 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
833 surface->showAt(100, 100);
834
835 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
836 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
837 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800838 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000839 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
Bernardo Rufino602ef712020-12-21 11:02:18 +0000840 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
841 // the default obscured/untrusted touch filter introduced in S.
842 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800843 nonTouchableSurface->showAt(100, 100);
844
845 injectTap(190, 199);
846 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
847}
848
849TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
850 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
851 surface->showAt(100, 100);
852
853 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
854 // that will be tapped. Window behind gets touch, but with flag
855 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
856 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
857 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800858 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
859 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000860 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
861 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800862 nonTouchableSurface->showAt(0, 0);
863 parentSurface->showAt(100, 100);
864
865 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800866 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800867 t.reparent(sc, parentSurface->mSurfaceControl);
868 });
869
870 injectTap(190, 199);
871 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
872}
873
874TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
875 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
876 surface->showAt(100, 100);
877
878 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
879 // the touchable window. Window behind gets touch with no obscured flags.
880 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
881 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800882 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
883 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000884 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
885 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800886 nonTouchableSurface->showAt(0, 0);
887 parentSurface->showAt(50, 50);
888
889 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800890 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800891 t.reparent(sc, parentSurface->mSurfaceControl);
892 });
893
894 injectTap(101, 110);
895 surface->expectTap(1, 10);
896}
897
chaviw7e72caf2020-12-02 16:50:43 -0800898TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
899 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
900
901 std::unique_ptr<InputSurface> bufferSurface =
902 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800903 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000904 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800905
906 surface->showAt(10, 10);
907 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
908
909 injectTap(11, 11);
910 surface->expectTap(1, 1);
911}
912
913TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
914 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
915
chaviw39d01472021-04-08 14:26:24 -0500916 std::unique_ptr<BlastInputSurface> bufferSurface =
917 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800918 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000919 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800920
921 surface->showAt(10, 10);
922 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
923
924 injectTap(11, 11);
925 surface->expectTap(1, 1);
926}
927
Vishnu Naira066d902021-09-13 18:40:17 -0700928TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
929 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
930 surface->doTransaction(
931 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
932 surface->showAt(100, 100);
933
934 injectTap(101, 101);
935
936 EXPECT_NE(surface->consumeEvent(), nullptr);
937 EXPECT_NE(surface->consumeEvent(), nullptr);
938
939 surface->requestFocus();
940 surface->assertFocusChange(true);
941 injectKey(AKEYCODE_V);
942 surface->expectKey(AKEYCODE_V);
943}
944
945TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
946 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
947 surface->doTransaction([&](auto &t, auto &sc) {
948 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
949 t.setMatrix(sc, 2.0, 0, 0, 2.0);
950 });
951 surface->showAt(100, 100);
952
953 injectTap(101, 101);
954
955 EXPECT_NE(surface->consumeEvent(), nullptr);
956 EXPECT_NE(surface->consumeEvent(), nullptr);
957
958 surface->requestFocus();
959 surface->assertFocusChange(true);
960 injectKey(AKEYCODE_V);
961 surface->expectKey(AKEYCODE_V);
962}
963
964TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
965 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000966 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700967 surface->doTransaction(
968 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
969 surface->showAt(100, 100);
970 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800971 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000972 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700973 obscuringSurface->showAt(100, 100);
974 injectTap(101, 101);
Egor Pasko5a67a562024-01-16 16:46:45 +0100975 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -0700976
977 surface->requestFocus();
978 surface->assertFocusChange(true);
979 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +0100980 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -0700981}
982
983TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
984 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000985 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700986 surface->doTransaction(
987 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
988 surface->showAt(100, 100);
989 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800990 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000991 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700992 obscuringSurface->showAt(190, 190);
993
994 injectTap(101, 101);
995
Egor Pasko5a67a562024-01-16 16:46:45 +0100996 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -0700997
998 surface->requestFocus();
999 surface->assertFocusChange(true);
1000 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001001 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001002}
1003
1004TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
1005 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1006 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1007
1008 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1009 surface->showAt(100, 100);
1010 surface->doTransaction([&](auto &t, auto &sc) {
1011 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1012 t.reparent(sc, parentSurface->mSurfaceControl);
1013 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
1014 });
1015
1016 injectTap(101, 101);
1017
Egor Pasko5a67a562024-01-16 16:46:45 +01001018 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001019
1020 surface->requestFocus();
1021 surface->assertFocusChange(true);
1022 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001023 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001024}
1025
1026TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
1027 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1028 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1029
1030 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1031 surface->doTransaction([&](auto &t, auto &sc) {
1032 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1033 t.reparent(sc, parentSurface->mSurfaceControl);
1034 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1035 });
1036 surface->showAt(100, 100);
1037
1038 injectTap(111, 111);
1039
Egor Pasko5a67a562024-01-16 16:46:45 +01001040 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001041
1042 surface->requestFocus();
1043 surface->assertFocusChange(true);
1044 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001045 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001046}
1047
Arthur Hung49d525a2021-11-19 15:11:51 +00001048TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1049 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1050
1051 std::unique_ptr<BlastInputSurface> bufferSurface =
1052 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1053
1054 surface->showAt(100, 100);
1055 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
1056 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1057
1058 injectTap(101, 101);
1059 surface->expectTap(1, 1);
1060}
1061
Vishnu Naira066d902021-09-13 18:40:17 -07001062TEST_F(InputSurfacesTest, drop_input_policy) {
1063 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1064 surface->doTransaction(
1065 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
1066 surface->showAt(100, 100);
1067
1068 injectTap(101, 101);
1069
Egor Pasko5a67a562024-01-16 16:46:45 +01001070 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001071
1072 surface->requestFocus();
1073 surface->assertFocusChange(true);
1074 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001075 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001076}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001077
Prabir Pradhan8c285982022-01-28 09:19:39 -08001078TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1079 std::unique_ptr<InputSurface> bufferSurface =
1080 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1081
1082 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1083
1084 bufferSurface->requestFocus();
1085 bufferSurface->assertFocusChange(true);
1086}
1087
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001088/**
1089 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1090 * its own crop.
1091 */
1092TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1093 std::unique_ptr<InputSurface> parentContainer =
1094 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1095 std::unique_ptr<InputSurface> containerSurface =
1096 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1097 containerSurface->doTransaction(
1098 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1099 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1100 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1101 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1102 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1103
1104 // Receives events inside its own crop
1105 injectTap(21, 21);
1106 containerSurface->expectTap(1, 1); // Event is in layer space
1107
1108 // Does not receive events outside its crop
1109 injectTap(26, 26);
Egor Pasko5a67a562024-01-16 16:46:45 +01001110 EXPECT_EQ(containerSurface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001111}
1112
1113/**
1114 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1115 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1116 */
1117TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
1118 std::unique_ptr<InputSurface> parentContainer =
1119 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1120 std::unique_ptr<InputSurface> containerSurface =
1121 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1122 containerSurface->doTransaction(
1123 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1124 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1125 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1126 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1127 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1128
1129 // Receives events inside parent bounds
1130 injectTap(21, 21);
1131 containerSurface->expectTap(1, 1); // Event is in layer space
1132
1133 // Does not receive events outside parent bounds
1134 injectTap(31, 31);
Egor Pasko5a67a562024-01-16 16:46:45 +01001135 EXPECT_EQ(containerSurface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001136}
1137
1138/**
1139 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1140 * layer's bounds. The input events should be in the layer's coordinate space.
1141 */
1142TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1143 std::unique_ptr<InputSurface> cropLayer =
1144 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1145 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1146
1147 std::unique_ptr<InputSurface> containerSurface =
1148 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1149 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1150 containerSurface->mInputInfo.touchableRegionCropHandle =
1151 cropLayer->mSurfaceControl->getHandle();
1152 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1153
1154 // Receives events inside crop layer bounds
1155 injectTap(51, 51);
1156 containerSurface->expectTap(41, 41); // Event is in layer space
1157
1158 // Does not receive events outside crop layer bounds
1159 injectTap(21, 21);
1160 injectTap(71, 71);
Egor Pasko5a67a562024-01-16 16:46:45 +01001161 EXPECT_EQ(containerSurface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001162}
1163
Linus Tufvessona1858822022-03-04 09:32:07 +00001164TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1165 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1166
1167 parent->showAt(100, 100);
1168 injectTap(101, 101);
1169 parent->expectTap(1, 1);
1170
1171 std::unique_ptr<InputSurface> childContainerSurface =
1172 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1173 childContainerSurface->showAt(0, 0);
1174 childContainerSurface->doTransaction(
1175 [&](auto &t, auto &sc) { t.reparent(sc, parent->mSurfaceControl); });
1176 injectTap(101, 101);
1177
Egor Pasko5a67a562024-01-16 16:46:45 +01001178 EXPECT_EQ(parent->consumeEvent(/*timeout=*/100ms), nullptr);
Linus Tufvessona1858822022-03-04 09:32:07 +00001179}
1180
Vishnu Nair16a938f2021-09-24 07:14:54 -07001181class MultiDisplayTests : public InputSurfacesTest {
1182public:
1183 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Prabir Pradhand0aba782021-12-14 00:44:21 -08001184 void TearDown() override {
1185 for (auto &token : mVirtualDisplays) {
1186 SurfaceComposerClient::destroyDisplay(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001187 }
1188 InputSurfacesTest::TearDown();
1189 }
1190
Prabir Pradhand0aba782021-12-14 00:44:21 -08001191 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1192 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001193 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001194 sp<IGraphicBufferProducer> producer;
1195 BufferQueue::createBufferQueue(&producer, &consumer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001196 consumer->setConsumerName(String8("Virtual disp consumer"));
1197 consumer->setDefaultBufferSize(width, height);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001198 mProducers.push_back(producer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001199
Prabir Pradhand0aba782021-12-14 00:44:21 -08001200 std::string name = "VirtualDisplay";
1201 name += std::to_string(mVirtualDisplays.size());
1202 sp<IBinder> token = SurfaceComposerClient::createDisplay(String8(name.c_str()), isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001203 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001204 t.setDisplaySurface(token, producer);
1205 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1206 t.setDisplayLayerStack(token, layerStack);
1207 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1208 {offsetX, offsetY, offsetX + width, offsetY + height});
Vishnu Nair16a938f2021-09-24 07:14:54 -07001209 t.apply(true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001210
1211 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001212 }
1213
Prabir Pradhand0aba782021-12-14 00:44:21 -08001214 std::vector<sp<IBinder>> mVirtualDisplays;
1215 std::vector<sp<IGraphicBufferProducer>> mProducers;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001216};
1217
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001218TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001219 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1220 // Do not create a display associated with the LayerStack.
1221 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1222 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1223 surface->showAt(100, 100);
1224
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001225 // Touches should be dropped if the layer is on an invalid display.
Prabir Pradhand0aba782021-12-14 00:44:21 -08001226 injectTapOnDisplay(101, 101, layerStack.id);
Egor Pasko5a67a562024-01-16 16:46:45 +01001227 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001228
1229 // However, we still let the window be focused and receive keys.
1230 surface->requestFocus(layerStack.id);
1231 surface->assertFocusChange(true);
1232
1233 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1234 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001235}
1236
1237TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1238 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1239 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1240 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1241 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1242 surface->showAt(100, 100);
1243
1244 injectTapOnDisplay(101, 101, layerStack.id);
1245 surface->expectTap(1, 1);
1246
1247 surface->requestFocus(layerStack.id);
1248 surface->assertFocusChange(true);
1249 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1250 surface->expectKey(AKEYCODE_V);
1251}
1252
Vishnu Nair16a938f2021-09-24 07:14:54 -07001253TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1254 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1255 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1256 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1257 surface->doTransaction([&](auto &t, auto &sc) {
1258 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1259 t.setLayerStack(sc, layerStack);
1260 });
1261 surface->showAt(100, 100);
1262
Prabir Pradhand0aba782021-12-14 00:44:21 -08001263 injectTapOnDisplay(101, 101, layerStack.id);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001264
Egor Pasko5a67a562024-01-16 16:46:45 +01001265 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001266
1267 surface->requestFocus(layerStack.id);
1268 surface->assertFocusChange(true);
1269 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
Egor Pasko5a67a562024-01-16 16:46:45 +01001270 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001271}
1272
1273TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1274 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001275
1276 // Create the secure display as system, because only certain users can create secure displays.
1277 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001278 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001279 // Change the uid back to root.
1280 seteuid(AID_ROOT);
1281
Vishnu Nair16a938f2021-09-24 07:14:54 -07001282 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1283 surface->doTransaction([&](auto &t, auto &sc) {
1284 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1285 t.setLayerStack(sc, layerStack);
1286 });
1287 surface->showAt(100, 100);
1288
1289 injectTapOnDisplay(101, 101, layerStack.id);
1290 EXPECT_NE(surface->consumeEvent(), nullptr);
1291 EXPECT_NE(surface->consumeEvent(), nullptr);
1292
1293 surface->requestFocus(layerStack.id);
1294 surface->assertFocusChange(true);
1295 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1296
1297 surface->expectKey(AKEYCODE_V);
1298}
1299
Vishnu Nair958da932020-08-21 17:12:37 -07001300} // namespace android::test