1 /*
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
27 #include <android/keycodes.h>
28 #include <android/native_window.h>
29
30 #include <binder/Binder.h>
31 #include <binder/IServiceManager.h>
32 #include <binder/Parcel.h>
33 #include <binder/ProcessState.h>
34
35 #include <gui/ISurfaceComposer.h>
36 #include <gui/Surface.h>
37 #include <gui/SurfaceComposerClient.h>
38 #include <gui/SurfaceControl.h>
39
40 #include <android/os/IInputFlinger.h>
41 #include <gui/WindowInfo.h>
42 #include <input/Input.h>
43 #include <input/InputTransport.h>
44
45 #include <ui/DisplayMode.h>
46 #include <ui/Rect.h>
47 #include <ui/Region.h>
48
49 #include <private/android_filesystem_config.h>
50
51 using android::os::IInputFlinger;
52
53 using android::hardware::graphics::common::V1_1::BufferUsage;
54
55 using android::gui::FocusRequest;
56 using android::gui::InputApplicationInfo;
57 using android::gui::TouchOcclusionMode;
58 using android::gui::WindowInfo;
59
60 namespace android::test {
61
62 using Transaction = SurfaceComposerClient::Transaction;
63
getInputFlinger()64 sp<IInputFlinger> getInputFlinger() {
65 sp<IBinder> input(defaultServiceManager()->getService(
66 String16("inputflinger")));
67 if (input == nullptr) {
68 ALOGE("Failed to link to input service");
69 } else { ALOGE("Linked to input"); }
70 return interface_cast<IInputFlinger>(input);
71 }
72
73 // We use the top 10 layers as a way to haphazardly place ourselves above anything else.
74 static const int LAYER_BASE = INT32_MAX - 10;
75 static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
76
77 class InputSurface {
78 public:
InputSurface(const sp<SurfaceControl> & sc,int width,int height,bool noInputChannel=false)79 InputSurface(const sp<SurfaceControl> &sc, int width, int height, bool noInputChannel = false) {
80 mSurfaceControl = sc;
81
82 mInputFlinger = getInputFlinger();
83 if (noInputChannel) {
84 mInputInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, true);
85 } else {
86 mClientChannel = std::make_shared<InputChannel>();
87 mInputFlinger->createInputChannel("testchannels", mClientChannel.get());
88 mInputInfo.token = mClientChannel->getConnectionToken();
89 mInputConsumer = new InputConsumer(mClientChannel);
90 }
91
92 mInputInfo.name = "Test info";
93 mInputInfo.dispatchingTimeout = 5s;
94 mInputInfo.globalScaleFactor = 1.0;
95 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
96
97 InputApplicationInfo aInfo;
98 aInfo.token = new BBinder();
99 aInfo.name = "Test app info";
100 aInfo.dispatchingTimeoutMillis =
101 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
102 mInputInfo.applicationInfo = aInfo;
103 }
104
makeColorInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)105 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
106 int width, int height) {
107 sp<SurfaceControl> surfaceControl =
108 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
109 PIXEL_FORMAT_RGBA_8888,
110 ISurfaceComposerClient::eFXSurfaceEffect);
111 return std::make_unique<InputSurface>(surfaceControl, width, height);
112 }
113
makeBufferInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)114 static std::unique_ptr<InputSurface> makeBufferInputSurface(
115 const sp<SurfaceComposerClient> &scc, int width, int height) {
116 sp<SurfaceControl> surfaceControl =
117 scc->createSurface(String8("Test Buffer Surface"), width, height,
118 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
119 return std::make_unique<InputSurface>(surfaceControl, width, height);
120 }
121
makeContainerInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)122 static std::unique_ptr<InputSurface> makeContainerInputSurface(
123 const sp<SurfaceComposerClient> &scc, int width, int height) {
124 sp<SurfaceControl> surfaceControl =
125 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
126 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
127 ISurfaceComposerClient::eFXSurfaceContainer);
128 return std::make_unique<InputSurface>(surfaceControl, width, height);
129 }
130
makeContainerInputSurfaceNoInputChannel(const sp<SurfaceComposerClient> & scc,int width,int height)131 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
132 const sp<SurfaceComposerClient> &scc, int width, int height) {
133 sp<SurfaceControl> surfaceControl =
134 scc->createSurface(String8("Test Container Surface"), 100 /* height */,
135 100 /* width */, PIXEL_FORMAT_RGBA_8888,
136 ISurfaceComposerClient::eFXSurfaceContainer);
137 return std::make_unique<InputSurface>(surfaceControl, width, height,
138 true /* noInputChannel */);
139 }
140
makeCursorInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)141 static std::unique_ptr<InputSurface> makeCursorInputSurface(
142 const sp<SurfaceComposerClient> &scc, int width, int height) {
143 sp<SurfaceControl> surfaceControl =
144 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
145 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
146 ISurfaceComposerClient::eCursorWindow);
147 return std::make_unique<InputSurface>(surfaceControl, width, height);
148 }
149
consumeEvent(int timeoutMs=3000)150 InputEvent *consumeEvent(int timeoutMs = 3000) {
151 waitForEventAvailable(timeoutMs);
152
153 InputEvent *ev;
154 uint32_t seqId;
155 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
156 if (consumed != OK) {
157 return nullptr;
158 }
159 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
160 EXPECT_EQ(OK, status) << "Could not send finished signal";
161 return ev;
162 }
163
assertFocusChange(bool hasFocus)164 void assertFocusChange(bool hasFocus) {
165 InputEvent *ev = consumeEvent();
166 ASSERT_NE(ev, nullptr);
167 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, ev->getType());
168 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
169 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
170 }
171
expectTap(int x,int y)172 void expectTap(int x, int y) {
173 InputEvent* ev = consumeEvent();
174 ASSERT_NE(ev, nullptr);
175 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
176 MotionEvent* mev = static_cast<MotionEvent*>(ev);
177 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
178 EXPECT_EQ(x, mev->getX(0));
179 EXPECT_EQ(y, mev->getY(0));
180 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
181
182 ev = consumeEvent();
183 ASSERT_NE(ev, nullptr);
184 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
185 mev = static_cast<MotionEvent*>(ev);
186 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
187 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
188 }
189
expectTapWithFlag(int x,int y,int32_t flags)190 void expectTapWithFlag(int x, int y, int32_t flags) {
191 InputEvent *ev = consumeEvent();
192 ASSERT_NE(ev, nullptr);
193 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
194 MotionEvent *mev = static_cast<MotionEvent *>(ev);
195 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
196 EXPECT_EQ(x, mev->getX(0));
197 EXPECT_EQ(y, mev->getY(0));
198 EXPECT_EQ(flags, mev->getFlags() & flags);
199
200 ev = consumeEvent();
201 ASSERT_NE(ev, nullptr);
202 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
203 mev = static_cast<MotionEvent *>(ev);
204 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
205 EXPECT_EQ(flags, mev->getFlags() & flags);
206 }
207
expectTapInDisplayCoordinates(int displayX,int displayY)208 void expectTapInDisplayCoordinates(int displayX, int displayY) {
209 InputEvent *ev = consumeEvent();
210 ASSERT_NE(ev, nullptr);
211 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
212 MotionEvent *mev = static_cast<MotionEvent *>(ev);
213 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
214 const PointerCoords &coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
215 EXPECT_EQ(displayX, coords.getX());
216 EXPECT_EQ(displayY, coords.getY());
217 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
218
219 ev = consumeEvent();
220 ASSERT_NE(ev, nullptr);
221 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
222 mev = static_cast<MotionEvent *>(ev);
223 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
224 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
225 }
226
expectKey(uint32_t keycode)227 void expectKey(uint32_t keycode) {
228 InputEvent *ev = consumeEvent();
229 ASSERT_NE(ev, nullptr);
230 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, ev->getType());
231 KeyEvent *keyEvent = static_cast<KeyEvent *>(ev);
232 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
233 EXPECT_EQ(keycode, keyEvent->getKeyCode());
234 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
235
236 ev = consumeEvent();
237 ASSERT_NE(ev, nullptr);
238 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, ev->getType());
239 keyEvent = static_cast<KeyEvent *>(ev);
240 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
241 EXPECT_EQ(keycode, keyEvent->getKeyCode());
242 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
243 }
244
~InputSurface()245 virtual ~InputSurface() {
246 if (mClientChannel) {
247 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
248 }
249 }
250
doTransaction(std::function<void (SurfaceComposerClient::Transaction &,const sp<SurfaceControl> &)> transactionBody)251 virtual void doTransaction(
252 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
253 transactionBody) {
254 SurfaceComposerClient::Transaction t;
255 transactionBody(t, mSurfaceControl);
256 t.apply(true);
257 }
258
showAt(int x,int y,Rect crop=Rect (0,0,100,100))259 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
260 SurfaceComposerClient::Transaction t;
261 t.show(mSurfaceControl);
262 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
263 t.setLayer(mSurfaceControl, LAYER_BASE);
264 t.setPosition(mSurfaceControl, x, y);
265 t.setCrop(mSurfaceControl, crop);
266 t.setAlpha(mSurfaceControl, 1);
267 t.apply(true);
268 }
269
requestFocus(int displayId=ADISPLAY_ID_DEFAULT)270 void requestFocus(int displayId = ADISPLAY_ID_DEFAULT) {
271 SurfaceComposerClient::Transaction t;
272 FocusRequest request;
273 request.token = mInputInfo.token;
274 request.windowName = mInputInfo.name;
275 request.focusedToken = nullptr;
276 request.focusedWindowName = "";
277 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
278 request.displayId = displayId;
279 t.setFocusedWindow(request);
280 t.apply(true);
281 }
282
283 private:
waitForEventAvailable(int timeoutMs)284 void waitForEventAvailable(int timeoutMs) {
285 struct pollfd fd;
286
287 fd.fd = mClientChannel->getFd();
288 fd.events = POLLIN;
289 poll(&fd, 1, timeoutMs);
290 }
291
292 public:
293 sp<SurfaceControl> mSurfaceControl;
294 std::shared_ptr<InputChannel> mClientChannel;
295 sp<IInputFlinger> mInputFlinger;
296
297 WindowInfo mInputInfo;
298
299 PreallocatedInputEventFactory mInputEventFactory;
300 InputConsumer* mInputConsumer;
301 };
302
303 class BlastInputSurface : public InputSurface {
304 public:
BlastInputSurface(const sp<SurfaceControl> & sc,const sp<SurfaceControl> & parentSc,int width,int height)305 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
306 int height)
307 : InputSurface(sc, width, height) {
308 mParentSurfaceControl = parentSc;
309 }
310
311 ~BlastInputSurface() = default;
312
makeBlastInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)313 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
314 const sp<SurfaceComposerClient> &scc, int width, int height) {
315 sp<SurfaceControl> parentSc =
316 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
317 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
318 ISurfaceComposerClient::eFXSurfaceContainer);
319
320 sp<SurfaceControl> surfaceControl =
321 scc->createSurface(String8("Test Buffer Surface"), width, height,
322 PIXEL_FORMAT_RGBA_8888,
323 ISurfaceComposerClient::eFXSurfaceBufferState,
324 parentSc->getHandle());
325 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
326 }
327
doTransaction(std::function<void (SurfaceComposerClient::Transaction &,const sp<SurfaceControl> &)> transactionBody)328 void doTransaction(
329 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
330 transactionBody) override {
331 SurfaceComposerClient::Transaction t;
332 transactionBody(t, mParentSurfaceControl);
333 t.apply(true);
334 }
335
showAt(int x,int y,Rect crop=Rect (0,0,100,100))336 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
337 SurfaceComposerClient::Transaction t;
338 t.show(mParentSurfaceControl);
339 t.setLayer(mParentSurfaceControl, LAYER_BASE);
340 t.setPosition(mParentSurfaceControl, x, y);
341 t.setCrop(mParentSurfaceControl, crop);
342
343 t.show(mSurfaceControl);
344 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
345 t.setCrop(mSurfaceControl, crop);
346 t.setAlpha(mSurfaceControl, 1);
347 t.apply(true);
348 }
349
350 private:
351 sp<SurfaceControl> mParentSurfaceControl;
352 };
353
354 class InputSurfacesTest : public ::testing::Test {
355 public:
InputSurfacesTest()356 InputSurfacesTest() {
357 ProcessState::self()->startThreadPool();
358 }
359
SetUp()360 void SetUp() {
361 mComposerClient = new SurfaceComposerClient;
362 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
363
364 const auto display = mComposerClient->getInternalDisplayToken();
365 ASSERT_NE(display, nullptr);
366
367 ui::DisplayMode mode;
368 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
369
370 // After a new buffer is queued, SurfaceFlinger is notified and will
371 // latch the new buffer on next vsync. Let's heuristically wait for 3
372 // vsyncs.
373 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.refreshRate) * 3;
374 }
375
TearDown()376 void TearDown() {
377 mComposerClient->dispose();
378 }
379
makeSurface(int width,int height)380 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
381 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
382 }
383
postBuffer(const sp<SurfaceControl> & layer,int32_t w,int32_t h)384 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
385 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
386 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
387 sp<GraphicBuffer> buffer =
388 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
389 Transaction().setBuffer(layer, buffer).apply(true);
390 usleep(mBufferPostDelay);
391 }
392
393 sp<SurfaceComposerClient> mComposerClient;
394 int32_t mBufferPostDelay;
395 };
396
injectTapOnDisplay(int x,int y,int displayId)397 void injectTapOnDisplay(int x, int y, int displayId) {
398 char *buf1, *buf2, *bufDisplayId;
399 asprintf(&buf1, "%d", x);
400 asprintf(&buf2, "%d", y);
401 asprintf(&bufDisplayId, "%d", displayId);
402 if (fork() == 0) {
403 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
404 }
405 }
406
injectTap(int x,int y)407 void injectTap(int x, int y) {
408 injectTapOnDisplay(x, y, ADISPLAY_ID_DEFAULT);
409 }
410
injectKeyOnDisplay(uint32_t keycode,int displayId)411 void injectKeyOnDisplay(uint32_t keycode, int displayId) {
412 char *buf1, *bufDisplayId;
413 asprintf(&buf1, "%d", keycode);
414 asprintf(&bufDisplayId, "%d", displayId);
415 if (fork() == 0) {
416 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
417 }
418 }
419
injectKey(uint32_t keycode)420 void injectKey(uint32_t keycode) {
421 injectKeyOnDisplay(keycode, ADISPLAY_ID_NONE);
422 }
423
TEST_F(InputSurfacesTest,can_receive_input)424 TEST_F(InputSurfacesTest, can_receive_input) {
425 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
426 surface->showAt(100, 100);
427
428 injectTap(101, 101);
429
430 EXPECT_NE(surface->consumeEvent(), nullptr);
431 }
432
433 /**
434 * Set up two surfaces side-by-side. Tap each surface.
435 * Next, swap the positions of the two surfaces. Inject tap into the two
436 * original locations. Ensure that the tap is received by the surfaces in the
437 * reverse order.
438 */
TEST_F(InputSurfacesTest,input_respects_positioning)439 TEST_F(InputSurfacesTest, input_respects_positioning) {
440 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
441 surface->showAt(100, 100);
442
443 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
444 surface2->showAt(200, 200);
445
446 injectTap(201, 201);
447 surface2->expectTap(1, 1);
448
449 injectTap(101, 101);
450 surface->expectTap(1, 1);
451
452 surface2->doTransaction([](auto &t, auto &sc) {
453 t.setPosition(sc, 100, 100);
454 });
455 surface->doTransaction([](auto &t, auto &sc) {
456 t.setPosition(sc, 200, 200);
457 });
458
459 injectTap(101, 101);
460 surface2->expectTap(1, 1);
461
462 injectTap(201, 201);
463 surface->expectTap(1, 1);
464 }
465
TEST_F(InputSurfacesTest,input_respects_layering)466 TEST_F(InputSurfacesTest, input_respects_layering) {
467 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
468 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
469
470 surface->showAt(10, 10);
471 surface2->showAt(10, 10);
472
473 surface->doTransaction([](auto &t, auto &sc) {
474 t.setLayer(sc, LAYER_BASE + 1);
475 });
476
477 injectTap(11, 11);
478 surface->expectTap(1, 1);
479
480 surface2->doTransaction([](auto &t, auto &sc) {
481 t.setLayer(sc, LAYER_BASE + 1);
482 });
483
484 injectTap(11, 11);
485 surface2->expectTap(1, 1);
486
487 surface2->doTransaction([](auto &t, auto &sc) {
488 t.hide(sc);
489 });
490
491 injectTap(11, 11);
492 surface->expectTap(1, 1);
493 }
494
495 // Surface Insets are set to offset the client content and draw a border around the client surface
496 // (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
497 // of the client content.
TEST_F(InputSurfacesTest,input_respects_surface_insets)498 TEST_F(InputSurfacesTest, input_respects_surface_insets) {
499 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
500 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
501 bgSurface->showAt(100, 100);
502
503 fgSurface->mInputInfo.surfaceInset = 5;
504 fgSurface->showAt(100, 100);
505
506 injectTap(106, 106);
507 fgSurface->expectTap(1, 1);
508
509 injectTap(101, 101);
510 bgSurface->expectTap(1, 1);
511 }
512
513 // Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
TEST_F(InputSurfacesTest,input_respects_cropped_surface_insets)514 TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
515 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
516 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
517 parentSurface->showAt(100, 100);
518
519 childSurface->mInputInfo.surfaceInset = 10;
520 childSurface->showAt(100, 100);
521
522 childSurface->doTransaction([&](auto &t, auto &sc) {
523 t.setPosition(sc, -5, -5);
524 t.reparent(sc, parentSurface->mSurfaceControl);
525 });
526
527 injectTap(106, 106);
528 childSurface->expectTap(1, 1);
529
530 injectTap(101, 101);
531 parentSurface->expectTap(1, 1);
532 }
533
534 // Ensure a surface whose insets are scaled, handles the touch offset correctly.
TEST_F(InputSurfacesTest,input_respects_scaled_surface_insets)535 TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
536 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
537 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
538 bgSurface->showAt(100, 100);
539
540 fgSurface->mInputInfo.surfaceInset = 5;
541 fgSurface->showAt(100, 100);
542
543 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
544
545 // expect = touch / scale - inset
546 injectTap(112, 124);
547 fgSurface->expectTap(1, 1);
548
549 injectTap(101, 101);
550 bgSurface->expectTap(1, 1);
551 }
552
TEST_F(InputSurfacesTest,input_respects_scaled_surface_insets_overflow)553 TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
554 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
555 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
556 bgSurface->showAt(100, 100);
557
558 // In case we pass the very big inset without any checking.
559 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
560 fgSurface->showAt(100, 100);
561
562 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
563
564 // expect no crash for overflow, and inset size to be clamped to surface size
565 injectTap(112, 124);
566 bgSurface->expectTap(12, 24);
567 }
568
TEST_F(InputSurfacesTest,touchable_region)569 TEST_F(InputSurfacesTest, touchable_region) {
570 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
571
572 surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31});
573
574 surface->showAt(11, 22);
575
576 // A tap within the surface but outside the touchable region should not be sent to the surface.
577 injectTap(20, 30);
578 EXPECT_EQ(surface->consumeEvent(200 /*timeoutMs*/), nullptr);
579
580 injectTap(31, 52);
581 surface->expectTap(20, 30);
582 }
583
TEST_F(InputSurfacesTest,input_respects_touchable_region_offset_overflow)584 TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
585 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
586 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
587 bgSurface->showAt(100, 100);
588
589 // Set the touchable region to the values at the limit of its corresponding type.
590 // Since the surface is offset from the origin, the touchable region will be transformed into
591 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
592 // against such a situation.
593 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
594
595 fgSurface->showAt(100, 100);
596
597 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
598 // surface receives touch.
599 injectTap(112, 124);
600 bgSurface->expectTap(12, 24);
601 }
602
TEST_F(InputSurfacesTest,input_respects_scaled_touchable_region_overflow)603 TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
604 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
605 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
606 bgSurface->showAt(0, 0);
607
608 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
609 fgSurface->showAt(0, 0);
610
611 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
612
613 // Expect no crash for overflow.
614 injectTap(12, 24);
615 fgSurface->expectTap(6, 12);
616 }
617
618 // Ensure we ignore transparent region when getting screen bounds when positioning input frame.
TEST_F(InputSurfacesTest,input_ignores_transparent_region)619 TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
620 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
621 surface->doTransaction([](auto &t, auto &sc) {
622 Region transparentRegion(Rect(0, 0, 10, 10));
623 t.setTransparentRegionHint(sc, transparentRegion);
624 });
625 surface->showAt(100, 100);
626 injectTap(101, 101);
627 surface->expectTap(1, 1);
628 }
629
630 // TODO(b/139494112) update tests once we define expected behavior
631 // Ensure we still send input to the surface regardless of surface visibility changes due to the
632 // first buffer being submitted or alpha changes.
633 // Original bug ref: b/120839715
TEST_F(InputSurfacesTest,input_ignores_buffer_layer_buffer)634 TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
635 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
636 std::unique_ptr<BlastInputSurface> bufferSurface =
637 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
638
639 bgSurface->showAt(10, 10);
640 bufferSurface->showAt(10, 10);
641
642 injectTap(11, 11);
643 bufferSurface->expectTap(1, 1);
644
645 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
646 injectTap(11, 11);
647 bufferSurface->expectTap(1, 1);
648 }
649
TEST_F(InputSurfacesTest,input_respects_buffer_layer_alpha)650 TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
651 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
652 std::unique_ptr<BlastInputSurface> bufferSurface =
653 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
654 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
655
656 bgSurface->showAt(10, 10);
657 bufferSurface->showAt(10, 10);
658
659 injectTap(11, 11);
660 bufferSurface->expectTap(1, 1);
661
662 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
663
664 injectTap(11, 11);
665 bgSurface->expectTap(1, 1);
666 }
667
TEST_F(InputSurfacesTest,input_ignores_color_layer_alpha)668 TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
669 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
670 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
671
672 bgSurface->showAt(10, 10);
673 fgSurface->showAt(10, 10);
674
675 injectTap(11, 11);
676 fgSurface->expectTap(1, 1);
677
678 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
679
680 injectTap(11, 11);
681 fgSurface->expectTap(1, 1);
682 }
683
TEST_F(InputSurfacesTest,input_respects_container_layer_visiblity)684 TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
685 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
686 std::unique_ptr<InputSurface> containerSurface =
687 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
688
689 bgSurface->showAt(10, 10);
690 containerSurface->showAt(10, 10);
691
692 injectTap(11, 11);
693 containerSurface->expectTap(1, 1);
694
695 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
696
697 injectTap(11, 11);
698 bgSurface->expectTap(1, 1);
699 }
700
TEST_F(InputSurfacesTest,input_respects_outscreen)701 TEST_F(InputSurfacesTest, input_respects_outscreen) {
702 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
703 surface->showAt(-1, -1);
704
705 injectTap(0, 0);
706 surface->expectTap(1, 1);
707 }
708
TEST_F(InputSurfacesTest,input_ignores_cursor_layer)709 TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
710 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
711 std::unique_ptr<InputSurface> cursorSurface =
712 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
713
714 surface->showAt(10, 10);
715 cursorSurface->showAt(10, 10);
716
717 injectTap(11, 11);
718 surface->expectTap(1, 1);
719 }
720
TEST_F(InputSurfacesTest,can_be_focused)721 TEST_F(InputSurfacesTest, can_be_focused) {
722 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
723 surface->showAt(100, 100);
724 surface->requestFocus();
725
726 surface->assertFocusChange(true);
727
728 injectKey(AKEYCODE_V);
729 surface->expectKey(AKEYCODE_V);
730 }
731
TEST_F(InputSurfacesTest,rotate_surface)732 TEST_F(InputSurfacesTest, rotate_surface) {
733 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
734 surface->showAt(10, 10);
735 surface->doTransaction([](auto &t, auto &sc) {
736 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
737 });
738 injectTap(8, 11);
739 surface->expectTap(1, 2);
740
741 surface->doTransaction([](auto &t, auto &sc) {
742 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
743 });
744 injectTap(9, 8);
745 surface->expectTap(1, 2);
746
747 surface->doTransaction([](auto &t, auto &sc) {
748 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
749 });
750 injectTap(12, 9);
751 surface->expectTap(1, 2);
752 }
753
TEST_F(InputSurfacesTest,rotate_surface_with_scale)754 TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
755 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
756 surface->showAt(10, 10);
757 surface->doTransaction([](auto &t, auto &sc) {
758 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
759 });
760 injectTap(2, 12);
761 surface->expectTap(1, 2);
762
763 surface->doTransaction([](auto &t, auto &sc) {
764 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
765 });
766 injectTap(8, 2);
767 surface->expectTap(1, 2);
768
769 surface->doTransaction([](auto &t, auto &sc) {
770 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
771 });
772 injectTap(18, 8);
773 surface->expectTap(1, 2);
774 }
775
TEST_F(InputSurfacesTest,rotate_surface_with_scale_and_insets)776 TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
777 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
778 surface->mInputInfo.surfaceInset = 5;
779 surface->showAt(100, 100);
780
781 surface->doTransaction([](auto &t, auto &sc) {
782 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
783 });
784 injectTap(40, 120);
785 surface->expectTap(5, 10);
786
787 surface->doTransaction([](auto &t, auto &sc) {
788 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
789 });
790 injectTap(80, 40);
791 surface->expectTap(5, 10);
792
793 surface->doTransaction([](auto &t, auto &sc) {
794 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
795 });
796 injectTap(160, 80);
797 surface->expectTap(5, 10);
798 }
799
TEST_F(InputSurfacesTest,touch_flag_obscured)800 TEST_F(InputSurfacesTest, touch_flag_obscured) {
801 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
802 surface->showAt(100, 100);
803
804 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
805 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
806 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
807 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
808 nonTouchableSurface->mInputInfo.ownerUid = 22222;
809 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
810 // the default obscured/untrusted touch filter introduced in S.
811 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
812 nonTouchableSurface->showAt(100, 100);
813
814 injectTap(190, 199);
815 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
816 }
817
TEST_F(InputSurfacesTest,touch_flag_partially_obscured_with_crop)818 TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
819 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
820 surface->showAt(100, 100);
821
822 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
823 // that will be tapped. Window behind gets touch, but with flag
824 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
825 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
826 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
827 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
828 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
829 nonTouchableSurface->mInputInfo.ownerUid = 22222;
830 parentSurface->mInputInfo.ownerUid = 22222;
831 nonTouchableSurface->showAt(0, 0);
832 parentSurface->showAt(100, 100);
833
834 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
835 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
836 t.reparent(sc, parentSurface->mSurfaceControl);
837 });
838
839 injectTap(190, 199);
840 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
841 }
842
TEST_F(InputSurfacesTest,touch_not_obscured_with_crop)843 TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
844 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
845 surface->showAt(100, 100);
846
847 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
848 // the touchable window. Window behind gets touch with no obscured flags.
849 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
850 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
851 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
852 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
853 nonTouchableSurface->mInputInfo.ownerUid = 22222;
854 parentSurface->mInputInfo.ownerUid = 22222;
855 nonTouchableSurface->showAt(0, 0);
856 parentSurface->showAt(50, 50);
857
858 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
859 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
860 t.reparent(sc, parentSurface->mSurfaceControl);
861 });
862
863 injectTap(101, 110);
864 surface->expectTap(1, 10);
865 }
866
TEST_F(InputSurfacesTest,touch_not_obscured_with_zero_sized_bql)867 TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
868 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
869
870 std::unique_ptr<InputSurface> bufferSurface =
871 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
872 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
873 bufferSurface->mInputInfo.ownerUid = 22222;
874
875 surface->showAt(10, 10);
876 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
877
878 injectTap(11, 11);
879 surface->expectTap(1, 1);
880 }
881
TEST_F(InputSurfacesTest,touch_not_obscured_with_zero_sized_blast)882 TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
883 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
884
885 std::unique_ptr<BlastInputSurface> bufferSurface =
886 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
887 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
888 bufferSurface->mInputInfo.ownerUid = 22222;
889
890 surface->showAt(10, 10);
891 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
892
893 injectTap(11, 11);
894 surface->expectTap(1, 1);
895 }
896
TEST_F(InputSurfacesTest,strict_unobscured_input_unobscured_window)897 TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
898 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
899 surface->doTransaction(
900 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
901 surface->showAt(100, 100);
902
903 injectTap(101, 101);
904
905 EXPECT_NE(surface->consumeEvent(), nullptr);
906 EXPECT_NE(surface->consumeEvent(), nullptr);
907
908 surface->requestFocus();
909 surface->assertFocusChange(true);
910 injectKey(AKEYCODE_V);
911 surface->expectKey(AKEYCODE_V);
912 }
913
TEST_F(InputSurfacesTest,strict_unobscured_input_scaled_without_crop_window)914 TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
915 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
916 surface->doTransaction([&](auto &t, auto &sc) {
917 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
918 t.setMatrix(sc, 2.0, 0, 0, 2.0);
919 });
920 surface->showAt(100, 100);
921
922 injectTap(101, 101);
923
924 EXPECT_NE(surface->consumeEvent(), nullptr);
925 EXPECT_NE(surface->consumeEvent(), nullptr);
926
927 surface->requestFocus();
928 surface->assertFocusChange(true);
929 injectKey(AKEYCODE_V);
930 surface->expectKey(AKEYCODE_V);
931 }
932
TEST_F(InputSurfacesTest,strict_unobscured_input_obscured_window)933 TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
934 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
935 surface->mInputInfo.ownerUid = 11111;
936 surface->doTransaction(
937 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
938 surface->showAt(100, 100);
939 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
940 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
941 obscuringSurface->mInputInfo.ownerUid = 22222;
942 obscuringSurface->showAt(100, 100);
943 injectTap(101, 101);
944 EXPECT_EQ(surface->consumeEvent(100), nullptr);
945
946 surface->requestFocus();
947 surface->assertFocusChange(true);
948 injectKey(AKEYCODE_V);
949 EXPECT_EQ(surface->consumeEvent(100), nullptr);
950 }
951
TEST_F(InputSurfacesTest,strict_unobscured_input_partially_obscured_window)952 TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
953 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
954 surface->mInputInfo.ownerUid = 11111;
955 surface->doTransaction(
956 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
957 surface->showAt(100, 100);
958 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
959 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
960 obscuringSurface->mInputInfo.ownerUid = 22222;
961 obscuringSurface->showAt(190, 190);
962
963 injectTap(101, 101);
964
965 EXPECT_EQ(surface->consumeEvent(100), nullptr);
966
967 surface->requestFocus();
968 surface->assertFocusChange(true);
969 injectKey(AKEYCODE_V);
970 EXPECT_EQ(surface->consumeEvent(100), nullptr);
971 }
972
TEST_F(InputSurfacesTest,strict_unobscured_input_alpha_window)973 TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
974 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
975 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
976
977 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
978 surface->showAt(100, 100);
979 surface->doTransaction([&](auto &t, auto &sc) {
980 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
981 t.reparent(sc, parentSurface->mSurfaceControl);
982 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
983 });
984
985 injectTap(101, 101);
986
987 EXPECT_EQ(surface->consumeEvent(100), nullptr);
988
989 surface->requestFocus();
990 surface->assertFocusChange(true);
991 injectKey(AKEYCODE_V);
992 EXPECT_EQ(surface->consumeEvent(100), nullptr);
993 }
994
TEST_F(InputSurfacesTest,strict_unobscured_input_cropped_window)995 TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
996 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
997 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
998
999 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1000 surface->doTransaction([&](auto &t, auto &sc) {
1001 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1002 t.reparent(sc, parentSurface->mSurfaceControl);
1003 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1004 });
1005 surface->showAt(100, 100);
1006
1007 injectTap(111, 111);
1008
1009 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1010
1011 surface->requestFocus();
1012 surface->assertFocusChange(true);
1013 injectKey(AKEYCODE_V);
1014 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1015 }
1016
TEST_F(InputSurfacesTest,ignore_touch_region_with_zero_sized_blast)1017 TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1018 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1019
1020 std::unique_ptr<BlastInputSurface> bufferSurface =
1021 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1022
1023 surface->showAt(100, 100);
1024 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
1025 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1026
1027 injectTap(101, 101);
1028 surface->expectTap(1, 1);
1029 }
1030
TEST_F(InputSurfacesTest,drop_input_policy)1031 TEST_F(InputSurfacesTest, drop_input_policy) {
1032 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1033 surface->doTransaction(
1034 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
1035 surface->showAt(100, 100);
1036
1037 injectTap(101, 101);
1038
1039 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1040
1041 surface->requestFocus();
1042 surface->assertFocusChange(true);
1043 injectKey(AKEYCODE_V);
1044 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1045 }
1046
TEST_F(InputSurfacesTest,layer_with_valid_crop_can_be_focused)1047 TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1048 std::unique_ptr<InputSurface> bufferSurface =
1049 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1050
1051 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1052
1053 bufferSurface->requestFocus();
1054 bufferSurface->assertFocusChange(true);
1055 }
1056
1057 /**
1058 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1059 * its own crop.
1060 */
TEST_F(InputSurfacesTest,cropped_container_replaces_touchable_region_with_null_crop)1061 TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1062 std::unique_ptr<InputSurface> parentContainer =
1063 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1064 std::unique_ptr<InputSurface> containerSurface =
1065 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1066 containerSurface->doTransaction(
1067 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1068 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1069 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1070 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1071 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1072
1073 // Receives events inside its own crop
1074 injectTap(21, 21);
1075 containerSurface->expectTap(1, 1); // Event is in layer space
1076
1077 // Does not receive events outside its crop
1078 injectTap(26, 26);
1079 EXPECT_EQ(containerSurface->consumeEvent(100), nullptr);
1080 }
1081
1082 /**
1083 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1084 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1085 */
TEST_F(InputSurfacesTest,uncropped_container_replaces_touchable_region_with_null_crop)1086 TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
1087 std::unique_ptr<InputSurface> parentContainer =
1088 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1089 std::unique_ptr<InputSurface> containerSurface =
1090 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1091 containerSurface->doTransaction(
1092 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1093 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1094 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1095 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1096 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1097
1098 // Receives events inside parent bounds
1099 injectTap(21, 21);
1100 containerSurface->expectTap(1, 1); // Event is in layer space
1101
1102 // Does not receive events outside parent bounds
1103 injectTap(31, 31);
1104 EXPECT_EQ(containerSurface->consumeEvent(100), nullptr);
1105 }
1106
1107 /**
1108 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1109 * layer's bounds. The input events should be in the layer's coordinate space.
1110 */
TEST_F(InputSurfacesTest,replace_touchable_region_with_crop)1111 TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1112 std::unique_ptr<InputSurface> cropLayer =
1113 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1114 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1115
1116 std::unique_ptr<InputSurface> containerSurface =
1117 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1118 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1119 containerSurface->mInputInfo.touchableRegionCropHandle =
1120 cropLayer->mSurfaceControl->getHandle();
1121 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1122
1123 // Receives events inside crop layer bounds
1124 injectTap(51, 51);
1125 containerSurface->expectTap(41, 41); // Event is in layer space
1126
1127 // Does not receive events outside crop layer bounds
1128 injectTap(21, 21);
1129 injectTap(71, 71);
1130 EXPECT_EQ(containerSurface->consumeEvent(100), nullptr);
1131 }
1132
TEST_F(InputSurfacesTest,child_container_with_no_input_channel_blocks_parent)1133 TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1134 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1135
1136 parent->showAt(100, 100);
1137 injectTap(101, 101);
1138 parent->expectTap(1, 1);
1139
1140 std::unique_ptr<InputSurface> childContainerSurface =
1141 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1142 childContainerSurface->showAt(0, 0);
1143 childContainerSurface->doTransaction(
1144 [&](auto &t, auto &sc) { t.reparent(sc, parent->mSurfaceControl); });
1145 injectTap(101, 101);
1146
1147 EXPECT_EQ(parent->consumeEvent(100), nullptr);
1148 }
1149
1150 class MultiDisplayTests : public InputSurfacesTest {
1151 public:
MultiDisplayTests()1152 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
TearDown()1153 void TearDown() override {
1154 for (auto &token : mVirtualDisplays) {
1155 SurfaceComposerClient::destroyDisplay(token);
1156 }
1157 InputSurfacesTest::TearDown();
1158 }
1159
createDisplay(int32_t width,int32_t height,bool isSecure,ui::LayerStack layerStack,bool receivesInput=true,int32_t offsetX=0,int32_t offsetY=0)1160 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1161 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
1162 sp<IGraphicBufferConsumer> consumer;
1163 sp<IGraphicBufferProducer> producer;
1164 BufferQueue::createBufferQueue(&producer, &consumer);
1165 consumer->setConsumerName(String8("Virtual disp consumer"));
1166 consumer->setDefaultBufferSize(width, height);
1167 mProducers.push_back(producer);
1168
1169 std::string name = "VirtualDisplay";
1170 name += std::to_string(mVirtualDisplays.size());
1171 sp<IBinder> token = SurfaceComposerClient::createDisplay(String8(name.c_str()), isSecure);
1172 SurfaceComposerClient::Transaction t;
1173 t.setDisplaySurface(token, producer);
1174 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1175 t.setDisplayLayerStack(token, layerStack);
1176 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1177 {offsetX, offsetY, offsetX + width, offsetY + height});
1178 t.apply(true);
1179
1180 mVirtualDisplays.push_back(token);
1181 }
1182
1183 std::vector<sp<IBinder>> mVirtualDisplays;
1184 std::vector<sp<IGraphicBufferProducer>> mProducers;
1185 };
1186
TEST_F(MultiDisplayTests,drop_touch_if_layer_on_invalid_display)1187 TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
1188 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1189 // Do not create a display associated with the LayerStack.
1190 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1191 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1192 surface->showAt(100, 100);
1193
1194 // Touches should be dropped if the layer is on an invalid display.
1195 injectTapOnDisplay(101, 101, layerStack.id);
1196 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1197
1198 // However, we still let the window be focused and receive keys.
1199 surface->requestFocus(layerStack.id);
1200 surface->assertFocusChange(true);
1201
1202 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1203 surface->expectKey(AKEYCODE_V);
1204 }
1205
TEST_F(MultiDisplayTests,virtual_display_receives_input)1206 TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1207 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1208 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1209 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1210 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1211 surface->showAt(100, 100);
1212
1213 injectTapOnDisplay(101, 101, layerStack.id);
1214 surface->expectTap(1, 1);
1215
1216 surface->requestFocus(layerStack.id);
1217 surface->assertFocusChange(true);
1218 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1219 surface->expectKey(AKEYCODE_V);
1220 }
1221
1222 /**
1223 * When multiple DisplayDevices are mapped to the same layerStack, use the configuration for the
1224 * display that can receive input.
1225 */
TEST_F(MultiDisplayTests,many_to_one_display_mapping)1226 TEST_F(MultiDisplayTests, many_to_one_display_mapping) {
1227 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1228 createDisplay(1000, 1000, false /*isSecure*/, layerStack, false /*receivesInput*/,
1229 100 /*offsetX*/, 100 /*offsetY*/);
1230 createDisplay(1000, 1000, false /*isSecure*/, layerStack, true /*receivesInput*/,
1231 200 /*offsetX*/, 200 /*offsetY*/);
1232 createDisplay(1000, 1000, false /*isSecure*/, layerStack, false /*receivesInput*/,
1233 300 /*offsetX*/, 300 /*offsetY*/);
1234 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1235 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1236 surface->showAt(10, 10);
1237
1238 // Input injection happens in logical display coordinates.
1239 injectTapOnDisplay(11, 11, layerStack.id);
1240 // Expect that the display transform for the display that receives input was used.
1241 surface->expectTapInDisplayCoordinates(211, 211);
1242
1243 surface->requestFocus(layerStack.id);
1244 surface->assertFocusChange(true);
1245 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1246 }
1247
TEST_F(MultiDisplayTests,drop_input_for_secure_layer_on_nonsecure_display)1248 TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1249 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1250 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1251 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1252 surface->doTransaction([&](auto &t, auto &sc) {
1253 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1254 t.setLayerStack(sc, layerStack);
1255 });
1256 surface->showAt(100, 100);
1257
1258 injectTapOnDisplay(101, 101, layerStack.id);
1259
1260 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1261
1262 surface->requestFocus(layerStack.id);
1263 surface->assertFocusChange(true);
1264 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1265 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1266 }
1267
TEST_F(MultiDisplayTests,dont_drop_input_for_secure_layer_on_secure_display)1268 TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1269 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1270
1271 // Create the secure display as system, because only certain users can create secure displays.
1272 seteuid(AID_SYSTEM);
1273 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
1274 // Change the uid back to root.
1275 seteuid(AID_ROOT);
1276
1277 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1278 surface->doTransaction([&](auto &t, auto &sc) {
1279 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1280 t.setLayerStack(sc, layerStack);
1281 });
1282 surface->showAt(100, 100);
1283
1284 injectTapOnDisplay(101, 101, layerStack.id);
1285 EXPECT_NE(surface->consumeEvent(), nullptr);
1286 EXPECT_NE(surface->consumeEvent(), nullptr);
1287
1288 surface->requestFocus(layerStack.id);
1289 surface->assertFocusChange(true);
1290 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1291
1292 surface->expectKey(AKEYCODE_V);
1293 }
1294
1295 } // namespace android::test
1296