blob: 03b9cd75db67cd01ab2ab3fadb8a51ddea9c2f64 [file] [log] [blame]
Robert Carr1c4c5592018-09-24 13:18:43 -07001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <sys/time.h>
21#include <sys/types.h>
22#include <stdio.h>
23#include <poll.h>
24
25#include <memory>
26
Vishnu Nairde19f852018-12-18 16:11:53 -080027#include <android/native_window.h>
28
Robert Carr1c4c5592018-09-24 13:18:43 -070029#include <binder/Binder.h>
30#include <binder/IServiceManager.h>
31#include <binder/Parcel.h>
32#include <binder/ProcessState.h>
33
Vishnu Nairde19f852018-12-18 16:11:53 -080034#include <gui/ISurfaceComposer.h>
35#include <gui/Surface.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070036#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
Vishnu Nairde19f852018-12-18 16:11:53 -080044#include <ui/DisplayInfo.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070045#include <ui/Rect.h>
46#include <ui/Region.h>
47
48
49namespace android {
50namespace test {
51
Vishnu Nairde19f852018-12-18 16:11:53 -080052using Transaction = SurfaceComposerClient::Transaction;
53
Robert Carr1c4c5592018-09-24 13:18:43 -070054sp<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.
64static const int LAYER_BASE = INT32_MAX - 10;
65
66class InputSurface {
67public:
Vishnu Nairde19f852018-12-18 16:11:53 -080068 InputSurface(const sp<SurfaceControl> &sc, int width, int height) {
69 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -070070
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
Vishnu Nairde19f852018-12-18 16:11:53 -080082 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
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
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
Robert Carr1c4c5592018-09-24 13:18:43 -0700107 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
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
136 ~InputSurface() {
137 mInputFlinger->unregisterInputChannel(mServerChannel);
138 }
139
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
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
158private:
159 void waitForEventAvailable() {
160 struct pollfd fd;
161
162 fd.fd = mClientChannel->getFd();
163 fd.events = POLLIN;
164 poll(&fd, 1, 3000);
165 }
166
167 void populateInputInfo(int width, int height) {
Robert Carr5c8a0262018-10-03 16:30:44 -0700168 mInputInfo.token = mServerChannel->getToken();
Robert Carr1c4c5592018-09-24 13:18:43 -0700169 mInputInfo.name = "Test info";
170 mInputInfo.layoutParamsFlags = InputWindowInfo::FLAG_NOT_TOUCH_MODAL;
171 mInputInfo.layoutParamsType = InputWindowInfo::TYPE_BASE_APPLICATION;
172 mInputInfo.dispatchingTimeout = 100000;
Robert Carre07e1032018-11-26 12:55:53 -0800173 mInputInfo.globalScaleFactor = 1.0;
Robert Carr1c4c5592018-09-24 13:18:43 -0700174 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;
Robert Carr740167f2018-10-11 19:03:41 -0700186
187 InputApplicationInfo aInfo;
188 aInfo.token = new BBinder();
189 aInfo.name = "Test app info";
190 aInfo.dispatchingTimeout = 100000;
191
192 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700193 }
194public:
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
205class InputSurfacesTest : public ::testing::Test {
206public:
207 InputSurfacesTest() {
208 ProcessState::self()->startThreadPool();
209 }
210
211 void SetUp() {
212 mComposerClient = new SurfaceComposerClient;
213 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Vishnu Nairde19f852018-12-18 16:11:53 -0800214
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800215 const auto display = mComposerClient->getInternalDisplayToken();
216 ASSERT_FALSE(display == nullptr);
217
Vishnu Nairde19f852018-12-18 16:11:53 -0800218 DisplayInfo info;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800219 ASSERT_EQ(NO_ERROR, mComposerClient->getDisplayInfo(display, &info));
Vishnu Nairde19f852018-12-18 16:11:53 -0800220
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;
Robert Carr1c4c5592018-09-24 13:18:43 -0700225 }
226
227 void TearDown() {
228 mComposerClient->dispose();
229 }
230
231 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800232 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
233 }
234
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);
Robert Carr1c4c5592018-09-24 13:18:43 -0700245 }
246
247 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800248 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700249};
250
251void 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
260TEST_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
269TEST_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
296TEST_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
Vishnu Nairde19f852018-12-18 16:11:53 -0800325// 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.
328TEST_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
344TEST_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
Arthur Hung118b1142019-05-08 21:25:59 +0800364// Ensure a surface whose insets are scaled, handles the touch offset correctly.
365TEST_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
Ady Abraham282f1d72019-07-24 18:05:56 -0700383TEST_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
Vishnu Nairde19f852018-12-18 16:11:53 -0800396// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
397TEST_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
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700408// 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
412TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800413 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);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700421 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800422
423 postBuffer(bufferSurface->mSurfaceControl);
424 injectTap(11, 11);
425 bufferSurface->expectTap(1, 1);
426}
427
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700428TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800429 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);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700443 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800444}
445
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700446TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800447 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);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700459 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800460}
461
462TEST_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}
chaviwfbe5d9c2018-12-26 12:23:37 -0800478
Arthur Hungd20b2702019-01-14 18:16:16 +0800479TEST_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}
Robert Carr1c4c5592018-09-24 13:18:43 -0700486}
487}