blob: 8d5d1e449f7f05891b1f2dc04d9da70ba7699d51 [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>
Siarhei Vishniakou0438ca82024-03-12 14:27:25 -070044#include <input/InputConsumer.h>
Chris Ye0783e992020-06-02 21:34:49 -070045#include <input/InputTransport.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070046
Marin Shalamanova7fe3042021-01-29 21:02:08 +010047#include <ui/DisplayMode.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070048#include <ui/Rect.h>
49#include <ui/Region.h>
50
Prabir Pradhand0aba782021-12-14 00:44:21 -080051#include <private/android_filesystem_config.h>
52
Chris Ye0783e992020-06-02 21:34:49 -070053using android::os::IInputFlinger;
Robert Carr1c4c5592018-09-24 13:18:43 -070054
chaviw39d01472021-04-08 14:26:24 -050055using android::hardware::graphics::common::V1_1::BufferUsage;
56
chaviw98318de2021-05-19 16:45:23 -050057using android::gui::FocusRequest;
58using android::gui::InputApplicationInfo;
59using android::gui::TouchOcclusionMode;
60using android::gui::WindowInfo;
61
Vishnu Nair958da932020-08-21 17:12:37 -070062namespace android::test {
Robert Carr1c4c5592018-09-24 13:18:43 -070063
Vishnu Nairde19f852018-12-18 16:11:53 -080064using Transaction = SurfaceComposerClient::Transaction;
65
Robert Carr1c4c5592018-09-24 13:18:43 -070066sp<IInputFlinger> getInputFlinger() {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -080067 sp<IBinder> input(defaultServiceManager()->waitForService(String16("inputflinger")));
Robert Carr1c4c5592018-09-24 13:18:43 -070068 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 {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800107 android::os::InputChannelCore tempChannel;
108 android::binder::Status result =
109 mInputFlinger->createInputChannel("testchannels", &tempChannel);
110 if (!result.isOk()) {
111 ADD_FAILURE() << "binder call to createInputChannel failed";
112 }
113 mClientChannel = InputChannel::create(std::move(tempChannel));
Linus Tufvessona1858822022-03-04 09:32:07 +0000114 mInputInfo.token = mClientChannel->getConnectionToken();
115 mInputConsumer = new InputConsumer(mClientChannel);
116 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700117
Linus Tufvessona1858822022-03-04 09:32:07 +0000118 mInputInfo.name = "Test info";
119 mInputInfo.dispatchingTimeout = 5s;
120 mInputInfo.globalScaleFactor = 1.0;
121 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
Robert Carr1c4c5592018-09-24 13:18:43 -0700122
Linus Tufvessona1858822022-03-04 09:32:07 +0000123 InputApplicationInfo aInfo;
124 aInfo.token = new BBinder();
125 aInfo.name = "Test app info";
126 aInfo.dispatchingTimeoutMillis =
127 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
128 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700129 }
130
Vishnu Nairde19f852018-12-18 16:11:53 -0800131 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
132 int width, int height) {
133 sp<SurfaceControl> surfaceControl =
134 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800135 PIXEL_FORMAT_RGBA_8888,
136 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -0800137 return std::make_unique<InputSurface>(surfaceControl, width, height);
138 }
139
140 static std::unique_ptr<InputSurface> makeBufferInputSurface(
141 const sp<SurfaceComposerClient> &scc, int width, int height) {
142 sp<SurfaceControl> surfaceControl =
143 scc->createSurface(String8("Test Buffer Surface"), width, height,
144 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
145 return std::make_unique<InputSurface>(surfaceControl, width, height);
146 }
147
148 static std::unique_ptr<InputSurface> makeContainerInputSurface(
149 const sp<SurfaceComposerClient> &scc, int width, int height) {
150 sp<SurfaceControl> surfaceControl =
151 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
152 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
153 ISurfaceComposerClient::eFXSurfaceContainer);
154 return std::make_unique<InputSurface>(surfaceControl, width, height);
155 }
156
Linus Tufvessona1858822022-03-04 09:32:07 +0000157 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
158 const sp<SurfaceComposerClient> &scc, int width, int height) {
159 sp<SurfaceControl> surfaceControl =
160 scc->createSurface(String8("Test Container Surface"), 100 /* height */,
161 100 /* width */, PIXEL_FORMAT_RGBA_8888,
162 ISurfaceComposerClient::eFXSurfaceContainer);
163 return std::make_unique<InputSurface>(surfaceControl, width, height,
164 true /* noInputChannel */);
165 }
166
arthurhungb4a0f852020-06-16 11:02:50 +0800167 static std::unique_ptr<InputSurface> makeCursorInputSurface(
168 const sp<SurfaceComposerClient> &scc, int width, int height) {
169 sp<SurfaceControl> surfaceControl =
170 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
171 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
172 ISurfaceComposerClient::eCursorWindow);
173 return std::make_unique<InputSurface>(surfaceControl, width, height);
174 }
175
Egor Pasko5a67a562024-01-16 16:46:45 +0100176 InputEvent* consumeEvent(std::chrono::milliseconds timeout = 3000ms) {
177 mClientChannel->waitForMessage(timeout);
Robert Carr1c4c5592018-09-24 13:18:43 -0700178
179 InputEvent *ev;
180 uint32_t seqId;
181 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
182 if (consumed != OK) {
183 return nullptr;
184 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100185 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
186 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700187 return ev;
188 }
189
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100190 void assertFocusChange(bool hasFocus) {
191 InputEvent *ev = consumeEvent();
192 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700193 ASSERT_EQ(InputEventType::FOCUS, ev->getType());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100194 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
195 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
196 }
197
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700198 void expectTap(float x, float y) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700199 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100200 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700201 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700202 MotionEvent* mev = static_cast<MotionEvent*>(ev);
203 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
204 EXPECT_EQ(x, mev->getX(0));
205 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800206 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700207
208 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100209 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700210 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700211 mev = static_cast<MotionEvent*>(ev);
212 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800213 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700214 }
215
chaviw39cfa2e2020-11-04 14:19:02 -0800216 void expectTapWithFlag(int x, int y, int32_t flags) {
217 InputEvent *ev = consumeEvent();
218 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700219 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800220 MotionEvent *mev = static_cast<MotionEvent *>(ev);
221 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
222 EXPECT_EQ(x, mev->getX(0));
223 EXPECT_EQ(y, mev->getY(0));
224 EXPECT_EQ(flags, mev->getFlags() & flags);
225
226 ev = consumeEvent();
227 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700228 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800229 mev = static_cast<MotionEvent *>(ev);
230 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
231 EXPECT_EQ(flags, mev->getFlags() & flags);
232 }
233
Prabir Pradhand0aba782021-12-14 00:44:21 -0800234 void expectTapInDisplayCoordinates(int displayX, int displayY) {
235 InputEvent *ev = consumeEvent();
236 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700237 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800238 MotionEvent *mev = static_cast<MotionEvent *>(ev);
239 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
240 const PointerCoords &coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
241 EXPECT_EQ(displayX, coords.getX());
242 EXPECT_EQ(displayY, coords.getY());
243 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
244
245 ev = consumeEvent();
246 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700247 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800248 mev = static_cast<MotionEvent *>(ev);
249 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
250 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
251 }
252
Vishnu Naira066d902021-09-13 18:40:17 -0700253 void expectKey(uint32_t keycode) {
254 InputEvent *ev = consumeEvent();
255 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700256 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700257 KeyEvent *keyEvent = static_cast<KeyEvent *>(ev);
258 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
259 EXPECT_EQ(keycode, keyEvent->getKeyCode());
260 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
261
262 ev = consumeEvent();
263 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700264 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700265 keyEvent = static_cast<KeyEvent *>(ev);
266 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
267 EXPECT_EQ(keycode, keyEvent->getKeyCode());
268 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
269 }
270
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700271 void assertNoEvent() {
272 InputEvent* event = consumeEvent(/*timeout=*/100ms);
273 ASSERT_EQ(event, nullptr) << "Expected no event, but got " << *event;
274 }
275
chaviw39d01472021-04-08 14:26:24 -0500276 virtual ~InputSurface() {
Linus Tufvessona1858822022-03-04 09:32:07 +0000277 if (mClientChannel) {
278 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
279 }
chaviw39d01472021-04-08 14:26:24 -0500280 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700281
chaviw39d01472021-04-08 14:26:24 -0500282 virtual void doTransaction(
283 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
284 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700285 SurfaceComposerClient::Transaction t;
286 transactionBody(t, mSurfaceControl);
287 t.apply(true);
288 }
289
chaviw39d01472021-04-08 14:26:24 -0500290 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700291 SurfaceComposerClient::Transaction t;
292 t.show(mSurfaceControl);
293 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
294 t.setLayer(mSurfaceControl, LAYER_BASE);
295 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800296 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700297 t.setAlpha(mSurfaceControl, 1);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500298 auto reportedListener = sp<SynchronousWindowInfosReportedListener>::make();
299 t.addWindowInfosReportedListener(reportedListener);
300 t.apply();
301 reportedListener->wait();
Robert Carr1c4c5592018-09-24 13:18:43 -0700302 }
303
Vishnu Nair16a938f2021-09-24 07:14:54 -0700304 void requestFocus(int displayId = ADISPLAY_ID_DEFAULT) {
Vishnu Nair958da932020-08-21 17:12:37 -0700305 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800306 FocusRequest request;
307 request.token = mInputInfo.token;
308 request.windowName = mInputInfo.name;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800309 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700310 request.displayId = displayId;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800311 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700312 t.apply(true);
313 }
314
Robert Carr1c4c5592018-09-24 13:18:43 -0700315public:
316 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500317 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700318 sp<IInputFlinger> mInputFlinger;
319
chaviw98318de2021-05-19 16:45:23 -0500320 WindowInfo mInputInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700321
322 PreallocatedInputEventFactory mInputEventFactory;
323 InputConsumer* mInputConsumer;
324};
325
chaviw39d01472021-04-08 14:26:24 -0500326class BlastInputSurface : public InputSurface {
327public:
328 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
329 int height)
330 : InputSurface(sc, width, height) {
331 mParentSurfaceControl = parentSc;
332 }
333
334 ~BlastInputSurface() = default;
335
336 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
337 const sp<SurfaceComposerClient> &scc, int width, int height) {
338 sp<SurfaceControl> parentSc =
339 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
340 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
341 ISurfaceComposerClient::eFXSurfaceContainer);
342
343 sp<SurfaceControl> surfaceControl =
344 scc->createSurface(String8("Test Buffer Surface"), width, height,
345 PIXEL_FORMAT_RGBA_8888,
346 ISurfaceComposerClient::eFXSurfaceBufferState,
347 parentSc->getHandle());
348 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
349 }
350
351 void doTransaction(
352 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
353 transactionBody) override {
354 SurfaceComposerClient::Transaction t;
355 transactionBody(t, mParentSurfaceControl);
356 t.apply(true);
357 }
358
359 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
360 SurfaceComposerClient::Transaction t;
361 t.show(mParentSurfaceControl);
362 t.setLayer(mParentSurfaceControl, LAYER_BASE);
363 t.setPosition(mParentSurfaceControl, x, y);
364 t.setCrop(mParentSurfaceControl, crop);
365
366 t.show(mSurfaceControl);
367 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
368 t.setCrop(mSurfaceControl, crop);
369 t.setAlpha(mSurfaceControl, 1);
370 t.apply(true);
371 }
372
373private:
374 sp<SurfaceControl> mParentSurfaceControl;
375};
376
Robert Carr1c4c5592018-09-24 13:18:43 -0700377class InputSurfacesTest : public ::testing::Test {
378public:
379 InputSurfacesTest() {
380 ProcessState::self()->startThreadPool();
381 }
382
383 void SetUp() {
384 mComposerClient = new SurfaceComposerClient;
385 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Huihong Luo31b5ac22022-08-15 20:38:10 -0700386 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
387 ASSERT_FALSE(ids.empty());
388 // display 0 is picked for now, can extend to support all displays if needed
389 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100390 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800391
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100392 ui::DisplayMode mode;
393 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800394
395 // After a new buffer is queued, SurfaceFlinger is notified and will
396 // latch the new buffer on next vsync. Let's heuristically wait for 3
397 // vsyncs.
Alec Mouri55e31032023-10-02 20:34:18 +0000398 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.peakRefreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700399 }
400
401 void TearDown() {
402 mComposerClient->dispose();
403 }
404
405 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800406 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
407 }
408
chaviw39d01472021-04-08 14:26:24 -0500409 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
410 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
411 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
412 sp<GraphicBuffer> buffer =
413 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
414 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800415 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700416 }
417
418 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800419 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700420};
421
Vishnu Nair16a938f2021-09-24 07:14:54 -0700422void injectTapOnDisplay(int x, int y, int displayId) {
423 char *buf1, *buf2, *bufDisplayId;
Robert Carr1c4c5592018-09-24 13:18:43 -0700424 asprintf(&buf1, "%d", x);
425 asprintf(&buf2, "%d", y);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700426 asprintf(&bufDisplayId, "%d", displayId);
Robert Carr1c4c5592018-09-24 13:18:43 -0700427 if (fork() == 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700428 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
429 }
430}
431
432void injectTap(int x, int y) {
433 injectTapOnDisplay(x, y, ADISPLAY_ID_DEFAULT);
434}
435
436void injectKeyOnDisplay(uint32_t keycode, int displayId) {
437 char *buf1, *bufDisplayId;
438 asprintf(&buf1, "%d", keycode);
439 asprintf(&bufDisplayId, "%d", displayId);
440 if (fork() == 0) {
441 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
Robert Carr1c4c5592018-09-24 13:18:43 -0700442 }
443}
444
Vishnu Naira066d902021-09-13 18:40:17 -0700445void injectKey(uint32_t keycode) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700446 injectKeyOnDisplay(keycode, ADISPLAY_ID_NONE);
Vishnu Naira066d902021-09-13 18:40:17 -0700447}
448
Robert Carr1c4c5592018-09-24 13:18:43 -0700449TEST_F(InputSurfacesTest, can_receive_input) {
450 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
451 surface->showAt(100, 100);
452
453 injectTap(101, 101);
454
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100455 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700456}
457
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100458/**
459 * Set up two surfaces side-by-side. Tap each surface.
460 * Next, swap the positions of the two surfaces. Inject tap into the two
461 * original locations. Ensure that the tap is received by the surfaces in the
462 * reverse order.
463 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700464TEST_F(InputSurfacesTest, input_respects_positioning) {
465 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
466 surface->showAt(100, 100);
467
468 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
469 surface2->showAt(200, 200);
470
471 injectTap(201, 201);
472 surface2->expectTap(1, 1);
473
474 injectTap(101, 101);
475 surface->expectTap(1, 1);
476
477 surface2->doTransaction([](auto &t, auto &sc) {
478 t.setPosition(sc, 100, 100);
479 });
480 surface->doTransaction([](auto &t, auto &sc) {
481 t.setPosition(sc, 200, 200);
482 });
483
484 injectTap(101, 101);
485 surface2->expectTap(1, 1);
486
487 injectTap(201, 201);
488 surface->expectTap(1, 1);
489}
490
491TEST_F(InputSurfacesTest, input_respects_layering) {
492 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
493 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
494
495 surface->showAt(10, 10);
496 surface2->showAt(10, 10);
497
498 surface->doTransaction([](auto &t, auto &sc) {
499 t.setLayer(sc, LAYER_BASE + 1);
500 });
501
502 injectTap(11, 11);
503 surface->expectTap(1, 1);
504
505 surface2->doTransaction([](auto &t, auto &sc) {
506 t.setLayer(sc, LAYER_BASE + 1);
507 });
508
509 injectTap(11, 11);
510 surface2->expectTap(1, 1);
511
512 surface2->doTransaction([](auto &t, auto &sc) {
513 t.hide(sc);
514 });
515
516 injectTap(11, 11);
517 surface->expectTap(1, 1);
518}
519
Vishnu Nairde19f852018-12-18 16:11:53 -0800520// Surface Insets are set to offset the client content and draw a border around the client surface
521// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
522// of the client content.
523TEST_F(InputSurfacesTest, input_respects_surface_insets) {
524 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
525 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
526 bgSurface->showAt(100, 100);
527
528 fgSurface->mInputInfo.surfaceInset = 5;
529 fgSurface->showAt(100, 100);
530
531 injectTap(106, 106);
532 fgSurface->expectTap(1, 1);
533
534 injectTap(101, 101);
535 bgSurface->expectTap(1, 1);
536}
537
Vishnu Nairfed7c122023-03-18 01:54:43 +0000538TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) {
539 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
540 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
541 bgSurface->showAt(100, 100);
542
543 fgSurface->mInputInfo.surfaceInset = 5;
544 fgSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
545 fgSurface->showAt(100, 100);
546
547 injectTap(106, 106);
548 fgSurface->expectTap(1, 1);
549
550 injectTap(101, 101);
551 bgSurface->expectTap(1, 1);
552}
553
Vishnu Nairde19f852018-12-18 16:11:53 -0800554// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
555TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
556 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
557 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
558 parentSurface->showAt(100, 100);
559
560 childSurface->mInputInfo.surfaceInset = 10;
561 childSurface->showAt(100, 100);
562
563 childSurface->doTransaction([&](auto &t, auto &sc) {
564 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000565 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800566 });
567
568 injectTap(106, 106);
569 childSurface->expectTap(1, 1);
570
571 injectTap(101, 101);
572 parentSurface->expectTap(1, 1);
573}
574
Arthur Hung118b1142019-05-08 21:25:59 +0800575// Ensure a surface whose insets are scaled, handles the touch offset correctly.
576TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
577 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
578 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
579 bgSurface->showAt(100, 100);
580
581 fgSurface->mInputInfo.surfaceInset = 5;
582 fgSurface->showAt(100, 100);
583
584 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
585
586 // expect = touch / scale - inset
587 injectTap(112, 124);
588 fgSurface->expectTap(1, 1);
589
590 injectTap(101, 101);
591 bgSurface->expectTap(1, 1);
592}
593
Ady Abraham282f1d72019-07-24 18:05:56 -0700594TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700595 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
Ady Abraham282f1d72019-07-24 18:05:56 -0700596 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700597 bgSurface->showAt(100, 100);
598
Ady Abraham282f1d72019-07-24 18:05:56 -0700599 // In case we pass the very big inset without any checking.
600 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
601 fgSurface->showAt(100, 100);
602
603 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
604
605 // expect no crash for overflow, and inset size to be clamped to surface size
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700606 injectTap(112, 124);
607 bgSurface->expectTap(12, 24);
Ady Abraham282f1d72019-07-24 18:05:56 -0700608}
609
Prabir Pradhan33da9462022-06-14 14:55:57 +0000610TEST_F(InputSurfacesTest, touchable_region) {
611 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
612
613 surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31});
614
615 surface->showAt(11, 22);
616
617 // A tap within the surface but outside the touchable region should not be sent to the surface.
618 injectTap(20, 30);
Egor Pasko5a67a562024-01-16 16:46:45 +0100619 EXPECT_EQ(surface->consumeEvent(/*timeout=*/200ms), nullptr);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000620
621 injectTap(31, 52);
622 surface->expectTap(20, 30);
623}
624
625TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
626 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
627 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
628 bgSurface->showAt(100, 100);
629
630 // Set the touchable region to the values at the limit of its corresponding type.
631 // Since the surface is offset from the origin, the touchable region will be transformed into
632 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
633 // against such a situation.
634 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
635
636 fgSurface->showAt(100, 100);
637
638 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
639 // surface receives touch.
640 injectTap(112, 124);
641 bgSurface->expectTap(12, 24);
642}
643
644TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
645 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
646 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
647 bgSurface->showAt(0, 0);
648
649 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
650 fgSurface->showAt(0, 0);
651
652 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
653
654 // Expect no crash for overflow.
655 injectTap(12, 24);
Prabir Pradhana4c59bd2023-04-10 20:54:04 +0000656 bgSurface->expectTap(12, 24);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000657}
658
Vishnu Nairde19f852018-12-18 16:11:53 -0800659// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
660TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
661 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
662 surface->doTransaction([](auto &t, auto &sc) {
663 Region transparentRegion(Rect(0, 0, 10, 10));
664 t.setTransparentRegionHint(sc, transparentRegion);
665 });
666 surface->showAt(100, 100);
667 injectTap(101, 101);
668 surface->expectTap(1, 1);
669}
670
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700671// TODO(b/139494112) update tests once we define expected behavior
672// Ensure we still send input to the surface regardless of surface visibility changes due to the
673// first buffer being submitted or alpha changes.
674// Original bug ref: b/120839715
675TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800676 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500677 std::unique_ptr<BlastInputSurface> bufferSurface =
678 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800679
680 bgSurface->showAt(10, 10);
681 bufferSurface->showAt(10, 10);
682
683 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700684 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800685
chaviw39d01472021-04-08 14:26:24 -0500686 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800687 injectTap(11, 11);
688 bufferSurface->expectTap(1, 1);
689}
690
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000691TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800692 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500693 std::unique_ptr<BlastInputSurface> bufferSurface =
694 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
695 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800696
697 bgSurface->showAt(10, 10);
698 bufferSurface->showAt(10, 10);
699
700 injectTap(11, 11);
701 bufferSurface->expectTap(1, 1);
702
703 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
704
705 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000706 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800707}
708
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700709TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800710 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
711 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
712
713 bgSurface->showAt(10, 10);
714 fgSurface->showAt(10, 10);
715
716 injectTap(11, 11);
717 fgSurface->expectTap(1, 1);
718
719 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
720
721 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700722 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800723}
724
725TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
726 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
727 std::unique_ptr<InputSurface> containerSurface =
728 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
729
730 bgSurface->showAt(10, 10);
731 containerSurface->showAt(10, 10);
732
733 injectTap(11, 11);
734 containerSurface->expectTap(1, 1);
735
736 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
737
738 injectTap(11, 11);
739 bgSurface->expectTap(1, 1);
740}
chaviwfbe5d9c2018-12-26 12:23:37 -0800741
Arthur Hungd20b2702019-01-14 18:16:16 +0800742TEST_F(InputSurfacesTest, input_respects_outscreen) {
743 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
744 surface->showAt(-1, -1);
745
746 injectTap(0, 0);
747 surface->expectTap(1, 1);
748}
arthurhungb4a0f852020-06-16 11:02:50 +0800749
750TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
751 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
752 std::unique_ptr<InputSurface> cursorSurface =
753 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
754
755 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800756 cursorSurface->showAt(10, 10);
757
758 injectTap(11, 11);
759 surface->expectTap(1, 1);
760}
Vishnu Nair958da932020-08-21 17:12:37 -0700761
762TEST_F(InputSurfacesTest, can_be_focused) {
763 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
764 surface->showAt(100, 100);
765 surface->requestFocus();
766
767 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700768
769 injectKey(AKEYCODE_V);
770 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700771}
chaviw44a6d2b2020-09-08 17:14:16 -0700772
773TEST_F(InputSurfacesTest, rotate_surface) {
774 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
775 surface->showAt(10, 10);
776 surface->doTransaction([](auto &t, auto &sc) {
777 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
778 });
779 injectTap(8, 11);
780 surface->expectTap(1, 2);
781
782 surface->doTransaction([](auto &t, auto &sc) {
783 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
784 });
785 injectTap(9, 8);
786 surface->expectTap(1, 2);
787
788 surface->doTransaction([](auto &t, auto &sc) {
789 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
790 });
791 injectTap(12, 9);
792 surface->expectTap(1, 2);
793}
794
795TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
796 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
797 surface->showAt(10, 10);
798 surface->doTransaction([](auto &t, auto &sc) {
799 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
800 });
801 injectTap(2, 12);
802 surface->expectTap(1, 2);
803
804 surface->doTransaction([](auto &t, auto &sc) {
805 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
806 });
807 injectTap(8, 2);
808 surface->expectTap(1, 2);
809
810 surface->doTransaction([](auto &t, auto &sc) {
811 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
812 });
813 injectTap(18, 8);
814 surface->expectTap(1, 2);
815}
816
817TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
818 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
819 surface->mInputInfo.surfaceInset = 5;
820 surface->showAt(100, 100);
821
822 surface->doTransaction([](auto &t, auto &sc) {
823 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
824 });
825 injectTap(40, 120);
826 surface->expectTap(5, 10);
827
828 surface->doTransaction([](auto &t, auto &sc) {
829 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
830 });
831 injectTap(80, 40);
832 surface->expectTap(5, 10);
833
834 surface->doTransaction([](auto &t, auto &sc) {
835 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
836 });
837 injectTap(160, 80);
838 surface->expectTap(5, 10);
839}
840
chaviw39cfa2e2020-11-04 14:19:02 -0800841TEST_F(InputSurfacesTest, touch_flag_obscured) {
842 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
843 surface->showAt(100, 100);
844
845 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
846 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
847 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800848 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000849 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
Bernardo Rufino602ef712020-12-21 11:02:18 +0000850 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
851 // the default obscured/untrusted touch filter introduced in S.
852 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800853 nonTouchableSurface->showAt(100, 100);
854
855 injectTap(190, 199);
856 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
857}
858
859TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
860 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
861 surface->showAt(100, 100);
862
863 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
864 // that will be tapped. Window behind gets touch, but with flag
865 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
866 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
867 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800868 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
869 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000870 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
871 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800872 nonTouchableSurface->showAt(0, 0);
873 parentSurface->showAt(100, 100);
874
875 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800876 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800877 t.reparent(sc, parentSurface->mSurfaceControl);
878 });
879
880 injectTap(190, 199);
881 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
882}
883
884TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
885 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
886 surface->showAt(100, 100);
887
888 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
889 // the touchable window. Window behind gets touch with no obscured flags.
890 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
891 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800892 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
893 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000894 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
895 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800896 nonTouchableSurface->showAt(0, 0);
897 parentSurface->showAt(50, 50);
898
899 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800900 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800901 t.reparent(sc, parentSurface->mSurfaceControl);
902 });
903
904 injectTap(101, 110);
905 surface->expectTap(1, 10);
906}
907
chaviw7e72caf2020-12-02 16:50:43 -0800908TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
909 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
910
911 std::unique_ptr<InputSurface> bufferSurface =
912 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800913 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000914 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800915
916 surface->showAt(10, 10);
917 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
918
919 injectTap(11, 11);
920 surface->expectTap(1, 1);
921}
922
923TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
924 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
925
chaviw39d01472021-04-08 14:26:24 -0500926 std::unique_ptr<BlastInputSurface> bufferSurface =
927 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800928 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000929 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800930
931 surface->showAt(10, 10);
932 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
933
934 injectTap(11, 11);
935 surface->expectTap(1, 1);
936}
937
Vishnu Naira066d902021-09-13 18:40:17 -0700938TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
939 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
940 surface->doTransaction(
941 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
942 surface->showAt(100, 100);
943
944 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700945 surface->expectTap(1, 1);
Vishnu Naira066d902021-09-13 18:40:17 -0700946
947 surface->requestFocus();
948 surface->assertFocusChange(true);
949 injectKey(AKEYCODE_V);
950 surface->expectKey(AKEYCODE_V);
951}
952
953TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
954 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
955 surface->doTransaction([&](auto &t, auto &sc) {
956 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
957 t.setMatrix(sc, 2.0, 0, 0, 2.0);
958 });
959 surface->showAt(100, 100);
960
961 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700962 surface->expectTap(.5, .5);
Vishnu Naira066d902021-09-13 18:40:17 -0700963
964 surface->requestFocus();
965 surface->assertFocusChange(true);
966 injectKey(AKEYCODE_V);
967 surface->expectKey(AKEYCODE_V);
968}
969
970TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
971 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000972 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700973 surface->doTransaction(
974 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
975 surface->showAt(100, 100);
976 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800977 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000978 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700979 obscuringSurface->showAt(100, 100);
980 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700981 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700982
983 surface->requestFocus();
984 surface->assertFocusChange(true);
985 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700986 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700987}
988
989TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
990 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000991 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700992 surface->doTransaction(
993 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
994 surface->showAt(100, 100);
995 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800996 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000997 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700998 obscuringSurface->showAt(190, 190);
999
1000 injectTap(101, 101);
1001
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001002 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001003
1004 surface->requestFocus();
1005 surface->assertFocusChange(true);
1006 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001007 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001008}
1009
1010TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
1011 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1012 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1013
1014 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1015 surface->showAt(100, 100);
1016 surface->doTransaction([&](auto &t, auto &sc) {
1017 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1018 t.reparent(sc, parentSurface->mSurfaceControl);
1019 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
1020 });
1021
1022 injectTap(101, 101);
1023
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001024 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001025
1026 surface->requestFocus();
1027 surface->assertFocusChange(true);
1028 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001029 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001030}
1031
1032TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
1033 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1034 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1035
1036 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1037 surface->doTransaction([&](auto &t, auto &sc) {
1038 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1039 t.reparent(sc, parentSurface->mSurfaceControl);
1040 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1041 });
1042 surface->showAt(100, 100);
1043
1044 injectTap(111, 111);
1045
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001046 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001047
1048 surface->requestFocus();
1049 surface->assertFocusChange(true);
1050 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001051 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001052}
1053
Arthur Hung49d525a2021-11-19 15:11:51 +00001054TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1055 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1056
1057 std::unique_ptr<BlastInputSurface> bufferSurface =
1058 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1059
1060 surface->showAt(100, 100);
1061 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
1062 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1063
1064 injectTap(101, 101);
1065 surface->expectTap(1, 1);
1066}
1067
Vishnu Naira066d902021-09-13 18:40:17 -07001068TEST_F(InputSurfacesTest, drop_input_policy) {
1069 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1070 surface->doTransaction(
1071 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
1072 surface->showAt(100, 100);
1073
1074 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001075 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001076
1077 surface->requestFocus();
1078 surface->assertFocusChange(true);
1079 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001080 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001081}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001082
Prabir Pradhan8c285982022-01-28 09:19:39 -08001083TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1084 std::unique_ptr<InputSurface> bufferSurface =
1085 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1086
1087 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1088
1089 bufferSurface->requestFocus();
1090 bufferSurface->assertFocusChange(true);
1091}
1092
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001093/**
1094 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1095 * its own crop.
1096 */
1097TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1098 std::unique_ptr<InputSurface> parentContainer =
1099 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1100 std::unique_ptr<InputSurface> containerSurface =
1101 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1102 containerSurface->doTransaction(
1103 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1104 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1105 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1106 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1107 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1108
1109 // Receives events inside its own crop
1110 injectTap(21, 21);
1111 containerSurface->expectTap(1, 1); // Event is in layer space
1112
1113 // Does not receive events outside its crop
1114 injectTap(26, 26);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001115 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001116}
1117
1118/**
1119 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1120 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1121 */
1122TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
1123 std::unique_ptr<InputSurface> parentContainer =
1124 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1125 std::unique_ptr<InputSurface> containerSurface =
1126 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1127 containerSurface->doTransaction(
1128 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1129 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1130 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1131 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1132 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1133
1134 // Receives events inside parent bounds
1135 injectTap(21, 21);
1136 containerSurface->expectTap(1, 1); // Event is in layer space
1137
1138 // Does not receive events outside parent bounds
1139 injectTap(31, 31);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001140 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001141}
1142
1143/**
1144 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1145 * layer's bounds. The input events should be in the layer's coordinate space.
1146 */
1147TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1148 std::unique_ptr<InputSurface> cropLayer =
1149 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1150 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1151
1152 std::unique_ptr<InputSurface> containerSurface =
1153 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1154 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1155 containerSurface->mInputInfo.touchableRegionCropHandle =
1156 cropLayer->mSurfaceControl->getHandle();
1157 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1158
1159 // Receives events inside crop layer bounds
1160 injectTap(51, 51);
1161 containerSurface->expectTap(41, 41); // Event is in layer space
1162
1163 // Does not receive events outside crop layer bounds
1164 injectTap(21, 21);
1165 injectTap(71, 71);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001166 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001167}
1168
Linus Tufvessona1858822022-03-04 09:32:07 +00001169TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1170 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1171
1172 parent->showAt(100, 100);
1173 injectTap(101, 101);
1174 parent->expectTap(1, 1);
1175
1176 std::unique_ptr<InputSurface> childContainerSurface =
1177 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1178 childContainerSurface->showAt(0, 0);
1179 childContainerSurface->doTransaction(
1180 [&](auto &t, auto &sc) { t.reparent(sc, parent->mSurfaceControl); });
1181 injectTap(101, 101);
1182
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001183 parent->assertNoEvent();
Linus Tufvessona1858822022-03-04 09:32:07 +00001184}
1185
Vishnu Nair16a938f2021-09-24 07:14:54 -07001186class MultiDisplayTests : public InputSurfacesTest {
1187public:
1188 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Prabir Pradhand0aba782021-12-14 00:44:21 -08001189 void TearDown() override {
1190 for (auto &token : mVirtualDisplays) {
1191 SurfaceComposerClient::destroyDisplay(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001192 }
1193 InputSurfacesTest::TearDown();
1194 }
1195
Prabir Pradhand0aba782021-12-14 00:44:21 -08001196 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1197 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001198 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001199 sp<IGraphicBufferProducer> producer;
1200 BufferQueue::createBufferQueue(&producer, &consumer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001201 consumer->setConsumerName(String8("Virtual disp consumer"));
1202 consumer->setDefaultBufferSize(width, height);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001203 mProducers.push_back(producer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001204
Prabir Pradhand0aba782021-12-14 00:44:21 -08001205 std::string name = "VirtualDisplay";
1206 name += std::to_string(mVirtualDisplays.size());
1207 sp<IBinder> token = SurfaceComposerClient::createDisplay(String8(name.c_str()), isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001208 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001209 t.setDisplaySurface(token, producer);
1210 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1211 t.setDisplayLayerStack(token, layerStack);
1212 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1213 {offsetX, offsetY, offsetX + width, offsetY + height});
Vishnu Nair16a938f2021-09-24 07:14:54 -07001214 t.apply(true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001215
1216 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001217 }
1218
Prabir Pradhand0aba782021-12-14 00:44:21 -08001219 std::vector<sp<IBinder>> mVirtualDisplays;
1220 std::vector<sp<IGraphicBufferProducer>> mProducers;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001221};
1222
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001223TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001224 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1225 // Do not create a display associated with the LayerStack.
1226 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1227 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1228 surface->showAt(100, 100);
1229
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001230 // Touches should be dropped if the layer is on an invalid display.
Prabir Pradhand0aba782021-12-14 00:44:21 -08001231 injectTapOnDisplay(101, 101, layerStack.id);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001232 surface->assertNoEvent();
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001233
1234 // However, we still let the window be focused and receive keys.
1235 surface->requestFocus(layerStack.id);
1236 surface->assertFocusChange(true);
1237
1238 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1239 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001240}
1241
1242TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1243 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1244 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1245 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1246 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1247 surface->showAt(100, 100);
1248
1249 injectTapOnDisplay(101, 101, layerStack.id);
1250 surface->expectTap(1, 1);
1251
1252 surface->requestFocus(layerStack.id);
1253 surface->assertFocusChange(true);
1254 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1255 surface->expectKey(AKEYCODE_V);
1256}
1257
Vishnu Nair16a938f2021-09-24 07:14:54 -07001258TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1259 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1260 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1261 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1262 surface->doTransaction([&](auto &t, auto &sc) {
1263 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1264 t.setLayerStack(sc, layerStack);
1265 });
1266 surface->showAt(100, 100);
1267
Prabir Pradhand0aba782021-12-14 00:44:21 -08001268 injectTapOnDisplay(101, 101, layerStack.id);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001269
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001270 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001271
1272 surface->requestFocus(layerStack.id);
1273 surface->assertFocusChange(true);
1274 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001275 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001276}
1277
1278TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1279 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001280
1281 // Create the secure display as system, because only certain users can create secure displays.
1282 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001283 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001284 // Change the uid back to root.
1285 seteuid(AID_ROOT);
1286
Vishnu Nair16a938f2021-09-24 07:14:54 -07001287 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1288 surface->doTransaction([&](auto &t, auto &sc) {
1289 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1290 t.setLayerStack(sc, layerStack);
1291 });
1292 surface->showAt(100, 100);
1293
1294 injectTapOnDisplay(101, 101, layerStack.id);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001295 surface->expectTap(1, 1);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001296
1297 surface->requestFocus(layerStack.id);
1298 surface->assertFocusChange(true);
1299 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1300
1301 surface->expectKey(AKEYCODE_V);
1302}
1303
Vishnu Nair958da932020-08-21 17:12:37 -07001304} // namespace android::test