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 <input/InputWindow.h>
40 #include <input/IInputFlinger.h>
41 #include <input/InputTransport.h>
42 #include <input/Input.h>
43 
44 #include <ui/DisplayInfo.h>
45 #include <ui/Rect.h>
46 #include <ui/Region.h>
47 
48 
49 namespace android {
50 namespace test {
51 
52 using Transaction = SurfaceComposerClient::Transaction;
53 
getInputFlinger()54 sp<IInputFlinger> getInputFlinger() {
55    sp<IBinder> input(defaultServiceManager()->getService(
56             String16("inputflinger")));
57     if (input == nullptr) {
58         ALOGE("Failed to link to input service");
59     } else { ALOGE("Linked to input"); }
60     return interface_cast<IInputFlinger>(input);
61 }
62 
63 // We use the top 10 layers as a way to haphazardly place ourselves above anything else.
64 static const int LAYER_BASE = INT32_MAX - 10;
65 
66 class InputSurface {
67 public:
InputSurface(const sp<SurfaceControl> & sc,int width,int height)68     InputSurface(const sp<SurfaceControl> &sc, int width, int height) {
69         mSurfaceControl = sc;
70 
71         InputChannel::openInputChannelPair("testchannels", mServerChannel, mClientChannel);
72         mServerChannel->setToken(new BBinder());
73 
74         mInputFlinger = getInputFlinger();
75         mInputFlinger->registerInputChannel(mServerChannel);
76 
77         populateInputInfo(width, height);
78 
79         mInputConsumer = new InputConsumer(mClientChannel);
80     }
81 
makeColorInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)82     static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
83                                                                int width, int height) {
84         sp<SurfaceControl> surfaceControl =
85                 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
86                                    PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
87         return std::make_unique<InputSurface>(surfaceControl, width, height);
88     }
89 
makeBufferInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)90     static std::unique_ptr<InputSurface> makeBufferInputSurface(
91             const sp<SurfaceComposerClient> &scc, int width, int height) {
92         sp<SurfaceControl> surfaceControl =
93                 scc->createSurface(String8("Test Buffer Surface"), width, height,
94                                    PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
95         return std::make_unique<InputSurface>(surfaceControl, width, height);
96     }
97 
makeContainerInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)98     static std::unique_ptr<InputSurface> makeContainerInputSurface(
99             const sp<SurfaceComposerClient> &scc, int width, int height) {
100         sp<SurfaceControl> surfaceControl =
101                 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
102                                    0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
103                                    ISurfaceComposerClient::eFXSurfaceContainer);
104         return std::make_unique<InputSurface>(surfaceControl, width, height);
105     }
106 
consumeEvent()107     InputEvent* consumeEvent() {
108         waitForEventAvailable();
109 
110         InputEvent *ev;
111         uint32_t seqId;
112         status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
113         if (consumed != OK) {
114             return nullptr;
115         }
116         mInputConsumer->sendFinishedSignal(seqId, true);
117         return ev;
118     }
119 
expectTap(int x,int y)120     void expectTap(int x, int y) {
121         InputEvent* ev = consumeEvent();
122         EXPECT_TRUE(ev != nullptr);
123         EXPECT_TRUE(ev->getType() == AINPUT_EVENT_TYPE_MOTION);
124         MotionEvent* mev = static_cast<MotionEvent*>(ev);
125         EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
126         EXPECT_EQ(x, mev->getX(0));
127         EXPECT_EQ(y, mev->getY(0));
128 
129         ev = consumeEvent();
130         EXPECT_TRUE(ev != nullptr);
131         EXPECT_TRUE(ev->getType() == AINPUT_EVENT_TYPE_MOTION);
132         mev = static_cast<MotionEvent*>(ev);
133         EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
134     }
135 
~InputSurface()136     ~InputSurface() {
137         mInputFlinger->unregisterInputChannel(mServerChannel);
138     }
139 
doTransaction(std::function<void (SurfaceComposerClient::Transaction &,const sp<SurfaceControl> &)> transactionBody)140     void doTransaction(std::function<void(SurfaceComposerClient::Transaction&,
141                     const sp<SurfaceControl>&)> transactionBody) {
142         SurfaceComposerClient::Transaction t;
143         transactionBody(t, mSurfaceControl);
144         t.apply(true);
145     }
146 
showAt(int x,int y)147     void showAt(int x, int y) {
148         SurfaceComposerClient::Transaction t;
149         t.show(mSurfaceControl);
150         t.setInputWindowInfo(mSurfaceControl, mInputInfo);
151         t.setLayer(mSurfaceControl, LAYER_BASE);
152         t.setPosition(mSurfaceControl, x, y);
153         t.setCrop_legacy(mSurfaceControl, Rect(0, 0, 100, 100));
154         t.setAlpha(mSurfaceControl, 1);
155         t.apply(true);
156     }
157 
158 private:
waitForEventAvailable()159     void waitForEventAvailable() {
160         struct pollfd fd;
161 
162         fd.fd = mClientChannel->getFd();
163         fd.events = POLLIN;
164         poll(&fd, 1, 3000);
165     }
166 
populateInputInfo(int width,int height)167     void populateInputInfo(int width, int height) {
168         mInputInfo.token = mServerChannel->getToken();
169         mInputInfo.name = "Test info";
170         mInputInfo.layoutParamsFlags = InputWindowInfo::FLAG_NOT_TOUCH_MODAL;
171         mInputInfo.layoutParamsType = InputWindowInfo::TYPE_BASE_APPLICATION;
172         mInputInfo.dispatchingTimeout = 100000;
173         mInputInfo.globalScaleFactor = 1.0;
174         mInputInfo.canReceiveKeys = true;
175         mInputInfo.hasFocus = true;
176         mInputInfo.hasWallpaper = false;
177         mInputInfo.paused = false;
178 
179         mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
180 
181         // TODO: Fill in from SF?
182         mInputInfo.ownerPid = 11111;
183         mInputInfo.ownerUid = 11111;
184         mInputInfo.inputFeatures = 0;
185         mInputInfo.displayId = 0;
186 
187         InputApplicationInfo aInfo;
188         aInfo.token = new BBinder();
189         aInfo.name = "Test app info";
190         aInfo.dispatchingTimeout = 100000;
191 
192         mInputInfo.applicationInfo = aInfo;
193     }
194 public:
195     sp<SurfaceControl> mSurfaceControl;
196     sp<InputChannel> mServerChannel, mClientChannel;
197     sp<IInputFlinger> mInputFlinger;
198 
199     InputWindowInfo mInputInfo;
200 
201     PreallocatedInputEventFactory mInputEventFactory;
202     InputConsumer* mInputConsumer;
203 };
204 
205 class InputSurfacesTest : public ::testing::Test {
206 public:
InputSurfacesTest()207     InputSurfacesTest() {
208         ProcessState::self()->startThreadPool();
209     }
210 
SetUp()211     void SetUp() {
212         mComposerClient = new SurfaceComposerClient;
213         ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
214 
215         const auto display = mComposerClient->getInternalDisplayToken();
216         ASSERT_FALSE(display == nullptr);
217 
218         DisplayInfo info;
219         ASSERT_EQ(NO_ERROR, mComposerClient->getDisplayInfo(display, &info));
220 
221         // After a new buffer is queued, SurfaceFlinger is notified and will
222         // latch the new buffer on next vsync.  Let's heuristically wait for 3
223         // vsyncs.
224         mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
225     }
226 
TearDown()227     void TearDown() {
228         mComposerClient->dispose();
229     }
230 
makeSurface(int width,int height)231     std::unique_ptr<InputSurface> makeSurface(int width, int height) {
232         return InputSurface::makeColorInputSurface(mComposerClient, width, height);
233     }
234 
postBuffer(const sp<SurfaceControl> & layer)235     void postBuffer(const sp<SurfaceControl> &layer) {
236         // wait for previous transactions (such as setSize) to complete
237         Transaction().apply(true);
238         ANativeWindow_Buffer buffer = {};
239         EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
240         ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
241         // Request an empty transaction to get applied synchronously to ensure the buffer is
242         // latched.
243         Transaction().apply(true);
244         usleep(mBufferPostDelay);
245     }
246 
247     sp<SurfaceComposerClient> mComposerClient;
248     int32_t mBufferPostDelay;
249 };
250 
injectTap(int x,int y)251 void injectTap(int x, int y) {
252     char *buf1, *buf2;
253     asprintf(&buf1, "%d", x);
254     asprintf(&buf2, "%d", y);
255     if (fork() == 0) {
256         execlp("input", "input", "tap", buf1, buf2, NULL);
257     }
258 }
259 
TEST_F(InputSurfacesTest,can_receive_input)260 TEST_F(InputSurfacesTest, can_receive_input) {
261     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
262     surface->showAt(100, 100);
263 
264     injectTap(101, 101);
265 
266     EXPECT_TRUE(surface->consumeEvent() != nullptr);
267 }
268 
TEST_F(InputSurfacesTest,input_respects_positioning)269 TEST_F(InputSurfacesTest, input_respects_positioning) {
270     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
271     surface->showAt(100, 100);
272 
273     std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
274     surface2->showAt(200, 200);
275 
276     injectTap(201, 201);
277     surface2->expectTap(1, 1);
278 
279     injectTap(101, 101);
280     surface->expectTap(1, 1);
281 
282     surface2->doTransaction([](auto &t, auto &sc) {
283          t.setPosition(sc, 100, 100);
284     });
285     surface->doTransaction([](auto &t, auto &sc) {
286          t.setPosition(sc, 200, 200);
287     });
288 
289     injectTap(101, 101);
290     surface2->expectTap(1, 1);
291 
292     injectTap(201, 201);
293     surface->expectTap(1, 1);
294 }
295 
TEST_F(InputSurfacesTest,input_respects_layering)296 TEST_F(InputSurfacesTest, input_respects_layering) {
297     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
298     std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
299 
300     surface->showAt(10, 10);
301     surface2->showAt(10, 10);
302 
303     surface->doTransaction([](auto &t, auto &sc) {
304          t.setLayer(sc, LAYER_BASE + 1);
305     });
306 
307     injectTap(11, 11);
308     surface->expectTap(1, 1);
309 
310     surface2->doTransaction([](auto &t, auto &sc) {
311          t.setLayer(sc, LAYER_BASE + 1);
312     });
313 
314     injectTap(11, 11);
315     surface2->expectTap(1, 1);
316 
317     surface2->doTransaction([](auto &t, auto &sc) {
318          t.hide(sc);
319     });
320 
321     injectTap(11, 11);
322     surface->expectTap(1, 1);
323 }
324 
325 // Surface Insets are set to offset the client content and draw a border around the client surface
326 // (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
327 // of the client content.
TEST_F(InputSurfacesTest,input_respects_surface_insets)328 TEST_F(InputSurfacesTest, input_respects_surface_insets) {
329     std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
330     std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
331     bgSurface->showAt(100, 100);
332 
333     fgSurface->mInputInfo.surfaceInset = 5;
334     fgSurface->showAt(100, 100);
335 
336     injectTap(106, 106);
337     fgSurface->expectTap(1, 1);
338 
339     injectTap(101, 101);
340     bgSurface->expectTap(1, 1);
341 }
342 
343 // Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
TEST_F(InputSurfacesTest,input_respects_cropped_surface_insets)344 TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
345     std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
346     std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
347     parentSurface->showAt(100, 100);
348 
349     childSurface->mInputInfo.surfaceInset = 10;
350     childSurface->showAt(100, 100);
351 
352     childSurface->doTransaction([&](auto &t, auto &sc) {
353         t.setPosition(sc, -5, -5);
354         t.reparent(sc, parentSurface->mSurfaceControl->getHandle());
355     });
356 
357     injectTap(106, 106);
358     childSurface->expectTap(1, 1);
359 
360     injectTap(101, 101);
361     parentSurface->expectTap(1, 1);
362 }
363 
364 // Ensure a surface whose insets are scaled, handles the touch offset correctly.
TEST_F(InputSurfacesTest,input_respects_scaled_surface_insets)365 TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
366     std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
367     std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
368     bgSurface->showAt(100, 100);
369 
370     fgSurface->mInputInfo.surfaceInset = 5;
371     fgSurface->showAt(100, 100);
372 
373     fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
374 
375     // expect = touch / scale - inset
376     injectTap(112, 124);
377     fgSurface->expectTap(1, 1);
378 
379     injectTap(101, 101);
380     bgSurface->expectTap(1, 1);
381 }
382 
TEST_F(InputSurfacesTest,input_respects_scaled_surface_insets_overflow)383 TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
384     std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
385     // In case we pass the very big inset without any checking.
386     fgSurface->mInputInfo.surfaceInset = INT32_MAX;
387     fgSurface->showAt(100, 100);
388 
389     fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
390 
391     // expect no crash for overflow, and inset size to be clamped to surface size
392     injectTap(202, 202);
393     fgSurface->expectTap(1, 1);
394 }
395 
396 // Ensure we ignore transparent region when getting screen bounds when positioning input frame.
TEST_F(InputSurfacesTest,input_ignores_transparent_region)397 TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
398     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
399     surface->doTransaction([](auto &t, auto &sc) {
400         Region transparentRegion(Rect(0, 0, 10, 10));
401         t.setTransparentRegionHint(sc, transparentRegion);
402     });
403     surface->showAt(100, 100);
404     injectTap(101, 101);
405     surface->expectTap(1, 1);
406 }
407 
408 // TODO(b/139494112) update tests once we define expected behavior
409 // Ensure we still send input to the surface regardless of surface visibility changes due to the
410 // first buffer being submitted or alpha changes.
411 // Original bug ref: b/120839715
TEST_F(InputSurfacesTest,input_ignores_buffer_layer_buffer)412 TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
413     std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
414     std::unique_ptr<InputSurface> bufferSurface =
415             InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
416 
417     bgSurface->showAt(10, 10);
418     bufferSurface->showAt(10, 10);
419 
420     injectTap(11, 11);
421     bufferSurface->expectTap(1, 1);
422 
423     postBuffer(bufferSurface->mSurfaceControl);
424     injectTap(11, 11);
425     bufferSurface->expectTap(1, 1);
426 }
427 
TEST_F(InputSurfacesTest,input_ignores_buffer_layer_alpha)428 TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
429     std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
430     std::unique_ptr<InputSurface> bufferSurface =
431             InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
432     postBuffer(bufferSurface->mSurfaceControl);
433 
434     bgSurface->showAt(10, 10);
435     bufferSurface->showAt(10, 10);
436 
437     injectTap(11, 11);
438     bufferSurface->expectTap(1, 1);
439 
440     bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
441 
442     injectTap(11, 11);
443     bufferSurface->expectTap(1, 1);
444 }
445 
TEST_F(InputSurfacesTest,input_ignores_color_layer_alpha)446 TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
447     std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
448     std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
449 
450     bgSurface->showAt(10, 10);
451     fgSurface->showAt(10, 10);
452 
453     injectTap(11, 11);
454     fgSurface->expectTap(1, 1);
455 
456     fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
457 
458     injectTap(11, 11);
459     fgSurface->expectTap(1, 1);
460 }
461 
TEST_F(InputSurfacesTest,input_respects_container_layer_visiblity)462 TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
463     std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
464     std::unique_ptr<InputSurface> containerSurface =
465             InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
466 
467     bgSurface->showAt(10, 10);
468     containerSurface->showAt(10, 10);
469 
470     injectTap(11, 11);
471     containerSurface->expectTap(1, 1);
472 
473     containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
474 
475     injectTap(11, 11);
476     bgSurface->expectTap(1, 1);
477 }
478 
TEST_F(InputSurfacesTest,input_respects_outscreen)479 TEST_F(InputSurfacesTest, input_respects_outscreen) {
480     std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
481     surface->showAt(-1, -1);
482 
483     injectTap(0, 0);
484     surface->expectTap(1, 1);
485 }
486 }
487 }
488