• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/native_window.h>
28 
29 #include <binder/Binder.h>
30 #include <binder/IServiceManager.h>
31 #include <binder/Parcel.h>
32 #include <binder/ProcessState.h>
33 
34 #include <gui/ISurfaceComposer.h>
35 #include <gui/Surface.h>
36 #include <gui/SurfaceComposerClient.h>
37 #include <gui/SurfaceControl.h>
38 
39 #include <android/os/IInputFlinger.h>
40 #include <input/Input.h>
41 #include <input/InputTransport.h>
42 #include <input/InputWindow.h>
43 
44 #include <ui/DisplayMode.h>
45 #include <ui/Rect.h>
46 #include <ui/Region.h>
47 
48 using android::os::IInputFlinger;
49 
50 using android::hardware::graphics::common::V1_1::BufferUsage;
51 
52 namespace android::test {
53 
54 using Transaction = SurfaceComposerClient::Transaction;
55 
getInputFlinger()56 sp<IInputFlinger> getInputFlinger() {
57    sp<IBinder> input(defaultServiceManager()->getService(
58             String16("inputflinger")));
59     if (input == nullptr) {
60         ALOGE("Failed to link to input service");
61     } else { ALOGE("Linked to input"); }
62     return interface_cast<IInputFlinger>(input);
63 }
64 
65 // We use the top 10 layers as a way to haphazardly place ourselves above anything else.
66 static const int LAYER_BASE = INT32_MAX - 10;
67 static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
68 
69 class InputSurface {
70 public:
InputSurface(const sp<SurfaceControl> & sc,int width,int height)71     InputSurface(const sp<SurfaceControl> &sc, int width, int height) {
72         mSurfaceControl = sc;
73 
74         mInputFlinger = getInputFlinger();
75         mClientChannel = std::make_shared<InputChannel>();
76         mInputFlinger->createInputChannel("testchannels", mClientChannel.get());
77 
78         populateInputInfo(width, height);
79 
80         mInputConsumer = new InputConsumer(mClientChannel);
81     }
82 
makeColorInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)83     static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
84                                                                int width, int height) {
85         sp<SurfaceControl> surfaceControl =
86                 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
87                                    PIXEL_FORMAT_RGBA_8888,
88                                    ISurfaceComposerClient::eFXSurfaceEffect);
89         return std::make_unique<InputSurface>(surfaceControl, width, height);
90     }
91 
makeBufferInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)92     static std::unique_ptr<InputSurface> makeBufferInputSurface(
93             const sp<SurfaceComposerClient> &scc, int width, int height) {
94         sp<SurfaceControl> surfaceControl =
95                 scc->createSurface(String8("Test Buffer Surface"), width, height,
96                                    PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
97         return std::make_unique<InputSurface>(surfaceControl, width, height);
98     }
99 
makeContainerInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)100     static std::unique_ptr<InputSurface> makeContainerInputSurface(
101             const sp<SurfaceComposerClient> &scc, int width, int height) {
102         sp<SurfaceControl> surfaceControl =
103                 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
104                                    0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
105                                    ISurfaceComposerClient::eFXSurfaceContainer);
106         return std::make_unique<InputSurface>(surfaceControl, width, height);
107     }
108 
makeCursorInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)109     static std::unique_ptr<InputSurface> makeCursorInputSurface(
110             const sp<SurfaceComposerClient> &scc, int width, int height) {
111         sp<SurfaceControl> surfaceControl =
112                 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
113                                    0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
114                                    ISurfaceComposerClient::eCursorWindow);
115         return std::make_unique<InputSurface>(surfaceControl, width, height);
116     }
117 
consumeEvent()118     InputEvent* consumeEvent() {
119         waitForEventAvailable();
120 
121         InputEvent *ev;
122         uint32_t seqId;
123         status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
124         if (consumed != OK) {
125             return nullptr;
126         }
127         status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
128         EXPECT_EQ(OK, status) << "Could not send finished signal";
129         return ev;
130     }
131 
assertFocusChange(bool hasFocus)132     void assertFocusChange(bool hasFocus) {
133         InputEvent *ev = consumeEvent();
134         ASSERT_NE(ev, nullptr);
135         ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, ev->getType());
136         FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
137         EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
138     }
139 
expectTap(int x,int y)140     void expectTap(int x, int y) {
141         InputEvent* ev = consumeEvent();
142         ASSERT_NE(ev, nullptr);
143         ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
144         MotionEvent* mev = static_cast<MotionEvent*>(ev);
145         EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
146         EXPECT_EQ(x, mev->getX(0));
147         EXPECT_EQ(y, mev->getY(0));
148         EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
149 
150         ev = consumeEvent();
151         ASSERT_NE(ev, nullptr);
152         ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
153         mev = static_cast<MotionEvent*>(ev);
154         EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
155         EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
156     }
157 
expectTapWithFlag(int x,int y,int32_t flags)158     void expectTapWithFlag(int x, int y, int32_t flags) {
159         InputEvent *ev = consumeEvent();
160         ASSERT_NE(ev, nullptr);
161         ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
162         MotionEvent *mev = static_cast<MotionEvent *>(ev);
163         EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
164         EXPECT_EQ(x, mev->getX(0));
165         EXPECT_EQ(y, mev->getY(0));
166         EXPECT_EQ(flags, mev->getFlags() & flags);
167 
168         ev = consumeEvent();
169         ASSERT_NE(ev, nullptr);
170         ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
171         mev = static_cast<MotionEvent *>(ev);
172         EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
173         EXPECT_EQ(flags, mev->getFlags() & flags);
174     }
175 
~InputSurface()176     virtual ~InputSurface() {
177         mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
178     }
179 
doTransaction(std::function<void (SurfaceComposerClient::Transaction &,const sp<SurfaceControl> &)> transactionBody)180     virtual void doTransaction(
181             std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
182                     transactionBody) {
183         SurfaceComposerClient::Transaction t;
184         transactionBody(t, mSurfaceControl);
185         t.apply(true);
186     }
187 
showAt(int x,int y,Rect crop=Rect (0,0,100,100))188     virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
189         SurfaceComposerClient::Transaction t;
190         t.show(mSurfaceControl);
191         t.setInputWindowInfo(mSurfaceControl, mInputInfo);
192         t.setLayer(mSurfaceControl, LAYER_BASE);
193         t.setPosition(mSurfaceControl, x, y);
194         t.setCrop(mSurfaceControl, crop);
195         t.setAlpha(mSurfaceControl, 1);
196         t.apply(true);
197     }
198 
requestFocus()199     void requestFocus() {
200         SurfaceComposerClient::Transaction t;
201         FocusRequest request;
202         request.token = mInputInfo.token;
203         request.windowName = mInputInfo.name;
204         request.focusedToken = nullptr;
205         request.focusedWindowName = "";
206         request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
207         request.displayId = 0;
208         t.setFocusedWindow(request);
209         t.apply(true);
210     }
211 
212 private:
waitForEventAvailable()213     void waitForEventAvailable() {
214         struct pollfd fd;
215 
216         fd.fd = mClientChannel->getFd();
217         fd.events = POLLIN;
218         poll(&fd, 1, 3000);
219     }
220 
populateInputInfo(int width,int height)221     void populateInputInfo(int width, int height) {
222         mInputInfo.token = mClientChannel->getConnectionToken();
223         mInputInfo.name = "Test info";
224         mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
225         mInputInfo.type = InputWindowInfo::Type::BASE_APPLICATION;
226         mInputInfo.dispatchingTimeout = 5s;
227         mInputInfo.globalScaleFactor = 1.0;
228         mInputInfo.focusable = true;
229         mInputInfo.hasWallpaper = false;
230         mInputInfo.paused = false;
231 
232         mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
233 
234         // TODO: Fill in from SF?
235         mInputInfo.ownerPid = 11111;
236         mInputInfo.ownerUid = 11111;
237         mInputInfo.displayId = 0;
238 
239         InputApplicationInfo aInfo;
240         aInfo.token = new BBinder();
241         aInfo.name = "Test app info";
242         aInfo.dispatchingTimeoutMillis =
243                 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
244 
245         mInputInfo.applicationInfo = aInfo;
246     }
247 public:
248     sp<SurfaceControl> mSurfaceControl;
249     std::shared_ptr<InputChannel> mClientChannel;
250     sp<IInputFlinger> mInputFlinger;
251 
252     InputWindowInfo mInputInfo;
253 
254     PreallocatedInputEventFactory mInputEventFactory;
255     InputConsumer* mInputConsumer;
256 };
257 
258 class BlastInputSurface : public InputSurface {
259 public:
BlastInputSurface(const sp<SurfaceControl> & sc,const sp<SurfaceControl> & parentSc,int width,int height)260     BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
261                       int height)
262           : InputSurface(sc, width, height) {
263         mParentSurfaceControl = parentSc;
264     }
265 
266     ~BlastInputSurface() = default;
267 
makeBlastInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)268     static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
269             const sp<SurfaceComposerClient> &scc, int width, int height) {
270         sp<SurfaceControl> parentSc =
271                 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
272                                    0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
273                                    ISurfaceComposerClient::eFXSurfaceContainer);
274 
275         sp<SurfaceControl> surfaceControl =
276                 scc->createSurface(String8("Test Buffer Surface"), width, height,
277                                    PIXEL_FORMAT_RGBA_8888,
278                                    ISurfaceComposerClient::eFXSurfaceBufferState,
279                                    parentSc->getHandle());
280         return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
281     }
282 
doTransaction(std::function<void (SurfaceComposerClient::Transaction &,const sp<SurfaceControl> &)> transactionBody)283     void doTransaction(
284             std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
285                     transactionBody) override {
286         SurfaceComposerClient::Transaction t;
287         transactionBody(t, mParentSurfaceControl);
288         t.apply(true);
289     }
290 
showAt(int x,int y,Rect crop=Rect (0,0,100,100))291     void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
292         SurfaceComposerClient::Transaction t;
293         t.show(mParentSurfaceControl);
294         t.setLayer(mParentSurfaceControl, LAYER_BASE);
295         t.setPosition(mParentSurfaceControl, x, y);
296         t.setCrop(mParentSurfaceControl, crop);
297 
298         t.show(mSurfaceControl);
299         t.setInputWindowInfo(mSurfaceControl, mInputInfo);
300         t.setCrop(mSurfaceControl, crop);
301         t.setAlpha(mSurfaceControl, 1);
302         t.apply(true);
303     }
304 
305 private:
306     sp<SurfaceControl> mParentSurfaceControl;
307 };
308 
309 class InputSurfacesTest : public ::testing::Test {
310 public:
InputSurfacesTest()311     InputSurfacesTest() {
312         ProcessState::self()->startThreadPool();
313     }
314 
SetUp()315     void SetUp() {
316         mComposerClient = new SurfaceComposerClient;
317         ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
318 
319         const auto display = mComposerClient->getInternalDisplayToken();
320         ASSERT_NE(display, nullptr);
321 
322         ui::DisplayMode mode;
323         ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
324 
325         // After a new buffer is queued, SurfaceFlinger is notified and will
326         // latch the new buffer on next vsync.  Let's heuristically wait for 3
327         // vsyncs.
328         mBufferPostDelay = static_cast<int32_t>(1e6 / mode.refreshRate) * 3;
329     }
330 
TearDown()331     void TearDown() {
332         mComposerClient->dispose();
333     }
334 
makeSurface(int width,int height)335     std::unique_ptr<InputSurface> makeSurface(int width, int height) {
336         return InputSurface::makeColorInputSurface(mComposerClient, width, height);
337     }
338 
postBuffer(const sp<SurfaceControl> & layer,int32_t w,int32_t h)339     void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
340         int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
341                 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
342         sp<GraphicBuffer> buffer =
343                 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
344         Transaction().setBuffer(layer, buffer).apply(true);
345         usleep(mBufferPostDelay);
346     }
347 
348     sp<SurfaceComposerClient> mComposerClient;
349     int32_t mBufferPostDelay;
350 };
351 
injectTap(int x,int y)352 void injectTap(int x, int y) {
353     char *buf1, *buf2;
354     asprintf(&buf1, "%d", x);
355     asprintf(&buf2, "%d", y);
356     if (fork() == 0) {
357         execlp("input", "input", "tap", buf1, buf2, NULL);
358     }
359 }
360 
TEST_F(InputSurfacesTest,can_receive_input)361 TEST_F(InputSurfacesTest, can_receive_input) {
362     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
363     surface->showAt(100, 100);
364 
365     injectTap(101, 101);
366 
367     EXPECT_NE(surface->consumeEvent(), nullptr);
368 }
369 
370 /**
371  * Set up two surfaces side-by-side. Tap each surface.
372  * Next, swap the positions of the two surfaces. Inject tap into the two
373  * original locations. Ensure that the tap is received by the surfaces in the
374  * reverse order.
375  */
TEST_F(InputSurfacesTest,input_respects_positioning)376 TEST_F(InputSurfacesTest, input_respects_positioning) {
377     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
378     surface->showAt(100, 100);
379 
380     std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
381     surface2->showAt(200, 200);
382 
383     injectTap(201, 201);
384     surface2->expectTap(1, 1);
385 
386     injectTap(101, 101);
387     surface->expectTap(1, 1);
388 
389     surface2->doTransaction([](auto &t, auto &sc) {
390          t.setPosition(sc, 100, 100);
391     });
392     surface->doTransaction([](auto &t, auto &sc) {
393          t.setPosition(sc, 200, 200);
394     });
395 
396     injectTap(101, 101);
397     surface2->expectTap(1, 1);
398 
399     injectTap(201, 201);
400     surface->expectTap(1, 1);
401 }
402 
TEST_F(InputSurfacesTest,input_respects_layering)403 TEST_F(InputSurfacesTest, input_respects_layering) {
404     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
405     std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
406 
407     surface->showAt(10, 10);
408     surface2->showAt(10, 10);
409 
410     surface->doTransaction([](auto &t, auto &sc) {
411          t.setLayer(sc, LAYER_BASE + 1);
412     });
413 
414     injectTap(11, 11);
415     surface->expectTap(1, 1);
416 
417     surface2->doTransaction([](auto &t, auto &sc) {
418          t.setLayer(sc, LAYER_BASE + 1);
419     });
420 
421     injectTap(11, 11);
422     surface2->expectTap(1, 1);
423 
424     surface2->doTransaction([](auto &t, auto &sc) {
425          t.hide(sc);
426     });
427 
428     injectTap(11, 11);
429     surface->expectTap(1, 1);
430 }
431 
432 // Surface Insets are set to offset the client content and draw a border around the client surface
433 // (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
434 // of the client content.
TEST_F(InputSurfacesTest,input_respects_surface_insets)435 TEST_F(InputSurfacesTest, input_respects_surface_insets) {
436     std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
437     std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
438     bgSurface->showAt(100, 100);
439 
440     fgSurface->mInputInfo.surfaceInset = 5;
441     fgSurface->showAt(100, 100);
442 
443     injectTap(106, 106);
444     fgSurface->expectTap(1, 1);
445 
446     injectTap(101, 101);
447     bgSurface->expectTap(1, 1);
448 }
449 
450 // Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
TEST_F(InputSurfacesTest,input_respects_cropped_surface_insets)451 TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
452     std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
453     std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
454     parentSurface->showAt(100, 100);
455 
456     childSurface->mInputInfo.surfaceInset = 10;
457     childSurface->showAt(100, 100);
458 
459     childSurface->doTransaction([&](auto &t, auto &sc) {
460         t.setPosition(sc, -5, -5);
461         t.reparent(sc, parentSurface->mSurfaceControl);
462     });
463 
464     injectTap(106, 106);
465     childSurface->expectTap(1, 1);
466 
467     injectTap(101, 101);
468     parentSurface->expectTap(1, 1);
469 }
470 
471 // Ensure a surface whose insets are scaled, handles the touch offset correctly.
TEST_F(InputSurfacesTest,input_respects_scaled_surface_insets)472 TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
473     std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
474     std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
475     bgSurface->showAt(100, 100);
476 
477     fgSurface->mInputInfo.surfaceInset = 5;
478     fgSurface->showAt(100, 100);
479 
480     fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
481 
482     // expect = touch / scale - inset
483     injectTap(112, 124);
484     fgSurface->expectTap(1, 1);
485 
486     injectTap(101, 101);
487     bgSurface->expectTap(1, 1);
488 }
489 
TEST_F(InputSurfacesTest,input_respects_scaled_surface_insets_overflow)490 TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
491     std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
492     // In case we pass the very big inset without any checking.
493     fgSurface->mInputInfo.surfaceInset = INT32_MAX;
494     fgSurface->showAt(100, 100);
495 
496     fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
497 
498     // expect no crash for overflow, and inset size to be clamped to surface size
499     injectTap(202, 202);
500     fgSurface->expectTap(1, 1);
501 }
502 
503 // Ensure we ignore transparent region when getting screen bounds when positioning input frame.
TEST_F(InputSurfacesTest,input_ignores_transparent_region)504 TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
505     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
506     surface->doTransaction([](auto &t, auto &sc) {
507         Region transparentRegion(Rect(0, 0, 10, 10));
508         t.setTransparentRegionHint(sc, transparentRegion);
509     });
510     surface->showAt(100, 100);
511     injectTap(101, 101);
512     surface->expectTap(1, 1);
513 }
514 
515 // TODO(b/139494112) update tests once we define expected behavior
516 // Ensure we still send input to the surface regardless of surface visibility changes due to the
517 // first buffer being submitted or alpha changes.
518 // Original bug ref: b/120839715
TEST_F(InputSurfacesTest,input_ignores_buffer_layer_buffer)519 TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
520     std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
521     std::unique_ptr<BlastInputSurface> bufferSurface =
522             BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
523 
524     bgSurface->showAt(10, 10);
525     bufferSurface->showAt(10, 10);
526 
527     injectTap(11, 11);
528     bufferSurface->expectTap(1, 1);
529 
530     postBuffer(bufferSurface->mSurfaceControl, 100, 100);
531     injectTap(11, 11);
532     bufferSurface->expectTap(1, 1);
533 }
534 
TEST_F(InputSurfacesTest,input_ignores_buffer_layer_alpha)535 TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
536     std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
537     std::unique_ptr<BlastInputSurface> bufferSurface =
538             BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
539     postBuffer(bufferSurface->mSurfaceControl, 100, 100);
540 
541     bgSurface->showAt(10, 10);
542     bufferSurface->showAt(10, 10);
543 
544     injectTap(11, 11);
545     bufferSurface->expectTap(1, 1);
546 
547     bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
548 
549     injectTap(11, 11);
550     bufferSurface->expectTap(1, 1);
551 }
552 
TEST_F(InputSurfacesTest,input_ignores_color_layer_alpha)553 TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
554     std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
555     std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
556 
557     bgSurface->showAt(10, 10);
558     fgSurface->showAt(10, 10);
559 
560     injectTap(11, 11);
561     fgSurface->expectTap(1, 1);
562 
563     fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
564 
565     injectTap(11, 11);
566     fgSurface->expectTap(1, 1);
567 }
568 
TEST_F(InputSurfacesTest,input_respects_container_layer_visiblity)569 TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
570     std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
571     std::unique_ptr<InputSurface> containerSurface =
572             InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
573 
574     bgSurface->showAt(10, 10);
575     containerSurface->showAt(10, 10);
576 
577     injectTap(11, 11);
578     containerSurface->expectTap(1, 1);
579 
580     containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
581 
582     injectTap(11, 11);
583     bgSurface->expectTap(1, 1);
584 }
585 
TEST_F(InputSurfacesTest,input_respects_outscreen)586 TEST_F(InputSurfacesTest, input_respects_outscreen) {
587     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
588     surface->showAt(-1, -1);
589 
590     injectTap(0, 0);
591     surface->expectTap(1, 1);
592 }
593 
TEST_F(InputSurfacesTest,input_ignores_cursor_layer)594 TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
595     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
596     std::unique_ptr<InputSurface> cursorSurface =
597             InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
598 
599     surface->showAt(10, 10);
600     cursorSurface->showAt(10, 10);
601 
602     injectTap(11, 11);
603     surface->expectTap(1, 1);
604 }
605 
TEST_F(InputSurfacesTest,can_be_focused)606 TEST_F(InputSurfacesTest, can_be_focused) {
607     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
608     surface->showAt(100, 100);
609     surface->requestFocus();
610 
611     surface->assertFocusChange(true);
612 }
613 
TEST_F(InputSurfacesTest,rotate_surface)614 TEST_F(InputSurfacesTest, rotate_surface) {
615     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
616     surface->showAt(10, 10);
617     surface->doTransaction([](auto &t, auto &sc) {
618         t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
619     });
620     injectTap(8, 11);
621     surface->expectTap(1, 2);
622 
623     surface->doTransaction([](auto &t, auto &sc) {
624         t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
625     });
626     injectTap(9, 8);
627     surface->expectTap(1, 2);
628 
629     surface->doTransaction([](auto &t, auto &sc) {
630         t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
631     });
632     injectTap(12, 9);
633     surface->expectTap(1, 2);
634 }
635 
TEST_F(InputSurfacesTest,rotate_surface_with_scale)636 TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
637     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
638     surface->showAt(10, 10);
639     surface->doTransaction([](auto &t, auto &sc) {
640         t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
641     });
642     injectTap(2, 12);
643     surface->expectTap(1, 2);
644 
645     surface->doTransaction([](auto &t, auto &sc) {
646         t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
647     });
648     injectTap(8, 2);
649     surface->expectTap(1, 2);
650 
651     surface->doTransaction([](auto &t, auto &sc) {
652         t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
653     });
654     injectTap(18, 8);
655     surface->expectTap(1, 2);
656 }
657 
TEST_F(InputSurfacesTest,rotate_surface_with_scale_and_insets)658 TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
659     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
660     surface->mInputInfo.surfaceInset = 5;
661     surface->showAt(100, 100);
662 
663     surface->doTransaction([](auto &t, auto &sc) {
664         t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
665     });
666     injectTap(40, 120);
667     surface->expectTap(5, 10);
668 
669     surface->doTransaction([](auto &t, auto &sc) {
670         t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
671     });
672     injectTap(80, 40);
673     surface->expectTap(5, 10);
674 
675     surface->doTransaction([](auto &t, auto &sc) {
676         t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
677     });
678     injectTap(160, 80);
679     surface->expectTap(5, 10);
680 }
681 
TEST_F(InputSurfacesTest,touch_flag_obscured)682 TEST_F(InputSurfacesTest, touch_flag_obscured) {
683     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
684     surface->showAt(100, 100);
685 
686     // Add non touchable window to fully cover touchable window. Window behind gets touch, but
687     // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
688     std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
689     nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
690     nonTouchableSurface->mInputInfo.ownerUid = 22222;
691     // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
692     // the default obscured/untrusted touch filter introduced in S.
693     nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
694     nonTouchableSurface->showAt(100, 100);
695 
696     injectTap(190, 199);
697     surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
698 }
699 
TEST_F(InputSurfacesTest,touch_flag_partially_obscured_with_crop)700 TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
701     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
702     surface->showAt(100, 100);
703 
704     // Add non touchable window to cover touchable window, but parent is cropped to not cover area
705     // that will be tapped. Window behind gets touch, but with flag
706     // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
707     std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
708     std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
709     nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
710     parentSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
711     nonTouchableSurface->mInputInfo.ownerUid = 22222;
712     parentSurface->mInputInfo.ownerUid = 22222;
713     nonTouchableSurface->showAt(0, 0);
714     parentSurface->showAt(100, 100);
715 
716     nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
717         t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
718         t.reparent(sc, parentSurface->mSurfaceControl);
719     });
720 
721     injectTap(190, 199);
722     surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
723 }
724 
TEST_F(InputSurfacesTest,touch_not_obscured_with_crop)725 TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
726     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
727     surface->showAt(100, 100);
728 
729     // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
730     // the touchable window. Window behind gets touch with no obscured flags.
731     std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
732     std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
733     nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
734     parentSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
735     nonTouchableSurface->mInputInfo.ownerUid = 22222;
736     parentSurface->mInputInfo.ownerUid = 22222;
737     nonTouchableSurface->showAt(0, 0);
738     parentSurface->showAt(50, 50);
739 
740     nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
741         t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
742         t.reparent(sc, parentSurface->mSurfaceControl);
743     });
744 
745     injectTap(101, 110);
746     surface->expectTap(1, 10);
747 }
748 
TEST_F(InputSurfacesTest,touch_not_obscured_with_zero_sized_bql)749 TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
750     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
751 
752     std::unique_ptr<InputSurface> bufferSurface =
753             InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
754     bufferSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
755     bufferSurface->mInputInfo.ownerUid = 22222;
756 
757     surface->showAt(10, 10);
758     bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
759 
760     injectTap(11, 11);
761     surface->expectTap(1, 1);
762 }
763 
TEST_F(InputSurfacesTest,touch_not_obscured_with_zero_sized_blast)764 TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
765     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
766 
767     std::unique_ptr<BlastInputSurface> bufferSurface =
768             BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
769     bufferSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
770     bufferSurface->mInputInfo.ownerUid = 22222;
771 
772     surface->showAt(10, 10);
773     bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
774 
775     injectTap(11, 11);
776     surface->expectTap(1, 1);
777 }
778 
779 } // namespace android::test
780