blob: 60542bd47caa76d0e8449235cfad003a1b9d64b0 [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
215 DisplayInfo info;
216 auto display = mComposerClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
217 SurfaceComposerClient::getDisplayInfo(display, &info);
218
219 // After a new buffer is queued, SurfaceFlinger is notified and will
220 // latch the new buffer on next vsync. Let's heuristically wait for 3
221 // vsyncs.
222 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700223 }
224
225 void TearDown() {
226 mComposerClient->dispose();
227 }
228
229 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800230 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
231 }
232
233 void postBuffer(const sp<SurfaceControl> &layer) {
234 // wait for previous transactions (such as setSize) to complete
235 Transaction().apply(true);
236 ANativeWindow_Buffer buffer = {};
237 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
238 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
239 // Request an empty transaction to get applied synchronously to ensure the buffer is
240 // latched.
241 Transaction().apply(true);
242 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700243 }
244
245 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800246 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700247};
248
249void injectTap(int x, int y) {
250 char *buf1, *buf2;
251 asprintf(&buf1, "%d", x);
252 asprintf(&buf2, "%d", y);
253 if (fork() == 0) {
254 execlp("input", "input", "tap", buf1, buf2, NULL);
255 }
256}
257
258TEST_F(InputSurfacesTest, can_receive_input) {
259 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
260 surface->showAt(100, 100);
261
262 injectTap(101, 101);
263
264 EXPECT_TRUE(surface->consumeEvent() != nullptr);
265}
266
267TEST_F(InputSurfacesTest, input_respects_positioning) {
268 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
269 surface->showAt(100, 100);
270
271 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
272 surface2->showAt(200, 200);
273
274 injectTap(201, 201);
275 surface2->expectTap(1, 1);
276
277 injectTap(101, 101);
278 surface->expectTap(1, 1);
279
280 surface2->doTransaction([](auto &t, auto &sc) {
281 t.setPosition(sc, 100, 100);
282 });
283 surface->doTransaction([](auto &t, auto &sc) {
284 t.setPosition(sc, 200, 200);
285 });
286
287 injectTap(101, 101);
288 surface2->expectTap(1, 1);
289
290 injectTap(201, 201);
291 surface->expectTap(1, 1);
292}
293
294TEST_F(InputSurfacesTest, input_respects_layering) {
295 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
296 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
297
298 surface->showAt(10, 10);
299 surface2->showAt(10, 10);
300
301 surface->doTransaction([](auto &t, auto &sc) {
302 t.setLayer(sc, LAYER_BASE + 1);
303 });
304
305 injectTap(11, 11);
306 surface->expectTap(1, 1);
307
308 surface2->doTransaction([](auto &t, auto &sc) {
309 t.setLayer(sc, LAYER_BASE + 1);
310 });
311
312 injectTap(11, 11);
313 surface2->expectTap(1, 1);
314
315 surface2->doTransaction([](auto &t, auto &sc) {
316 t.hide(sc);
317 });
318
319 injectTap(11, 11);
320 surface->expectTap(1, 1);
321}
322
Vishnu Nairde19f852018-12-18 16:11:53 -0800323// Surface Insets are set to offset the client content and draw a border around the client surface
324// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
325// of the client content.
326TEST_F(InputSurfacesTest, input_respects_surface_insets) {
327 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
328 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
329 bgSurface->showAt(100, 100);
330
331 fgSurface->mInputInfo.surfaceInset = 5;
332 fgSurface->showAt(100, 100);
333
334 injectTap(106, 106);
335 fgSurface->expectTap(1, 1);
336
337 injectTap(101, 101);
338 bgSurface->expectTap(1, 1);
339}
340
341// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
342TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
343 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
344 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
345 parentSurface->showAt(100, 100);
346
347 childSurface->mInputInfo.surfaceInset = 10;
348 childSurface->showAt(100, 100);
349
350 childSurface->doTransaction([&](auto &t, auto &sc) {
351 t.setPosition(sc, -5, -5);
352 t.reparent(sc, parentSurface->mSurfaceControl->getHandle());
353 });
354
355 injectTap(106, 106);
356 childSurface->expectTap(1, 1);
357
358 injectTap(101, 101);
359 parentSurface->expectTap(1, 1);
360}
361
362// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
363TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
364 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
365 surface->doTransaction([](auto &t, auto &sc) {
366 Region transparentRegion(Rect(0, 0, 10, 10));
367 t.setTransparentRegionHint(sc, transparentRegion);
368 });
369 surface->showAt(100, 100);
370 injectTap(101, 101);
371 surface->expectTap(1, 1);
372}
373
374// Ensure we send the input to the right surface when the surface visibility changes due to the
375// first buffer being submitted. ref: b/120839715
376TEST_F(InputSurfacesTest, input_respects_buffer_layer_buffer) {
377 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
378 std::unique_ptr<InputSurface> bufferSurface =
379 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
380
381 bgSurface->showAt(10, 10);
382 bufferSurface->showAt(10, 10);
383
384 injectTap(11, 11);
385 bgSurface->expectTap(1, 1);
386
387 postBuffer(bufferSurface->mSurfaceControl);
388 injectTap(11, 11);
389 bufferSurface->expectTap(1, 1);
390}
391
392TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
393 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
394 std::unique_ptr<InputSurface> bufferSurface =
395 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
396 postBuffer(bufferSurface->mSurfaceControl);
397
398 bgSurface->showAt(10, 10);
399 bufferSurface->showAt(10, 10);
400
401 injectTap(11, 11);
402 bufferSurface->expectTap(1, 1);
403
404 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
405
406 injectTap(11, 11);
407 bgSurface->expectTap(1, 1);
408}
409
410TEST_F(InputSurfacesTest, input_respects_color_layer_alpha) {
411 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
412 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
413
414 bgSurface->showAt(10, 10);
415 fgSurface->showAt(10, 10);
416
417 injectTap(11, 11);
418 fgSurface->expectTap(1, 1);
419
420 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
421
422 injectTap(11, 11);
423 bgSurface->expectTap(1, 1);
424}
425
426TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
427 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
428 std::unique_ptr<InputSurface> containerSurface =
429 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
430
431 bgSurface->showAt(10, 10);
432 containerSurface->showAt(10, 10);
433
434 injectTap(11, 11);
435 containerSurface->expectTap(1, 1);
436
437 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
438
439 injectTap(11, 11);
440 bgSurface->expectTap(1, 1);
441}
Robert Carr1c4c5592018-09-24 13:18:43 -0700442}
443}