blob: 7fc69ff93be92114c1b5b7042153e929f618adfa [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
chaviwfbe5d9c2018-12-26 12:23:37 -0800136 void expectMotionEvent(int motionEventType, int x, int y) {
137 InputEvent *ev = consumeEvent();
138 ASSERT_NE(ev, nullptr);
139 ASSERT_EQ(ev->getType(), AINPUT_EVENT_TYPE_MOTION);
140 MotionEvent *mev = static_cast<MotionEvent *>(ev);
141 EXPECT_EQ(motionEventType, mev->getAction());
142 EXPECT_EQ(x, mev->getX(0));
143 EXPECT_EQ(y, mev->getY(0));
144 }
145
146 void expectNoMotionEvent(int motionEventType) {
147 InputEvent *ev = consumeEvent();
148 if (ev == nullptr || ev->getType() != AINPUT_EVENT_TYPE_MOTION) {
149 // Didn't find an event or a motion event so assume action didn't occur.
150 return;
151 }
152
153 MotionEvent *mev = static_cast<MotionEvent *>(ev);
154 EXPECT_NE(motionEventType, mev->getAction());
155 }
156
Robert Carr1c4c5592018-09-24 13:18:43 -0700157 ~InputSurface() {
158 mInputFlinger->unregisterInputChannel(mServerChannel);
159 }
160
161 void doTransaction(std::function<void(SurfaceComposerClient::Transaction&,
162 const sp<SurfaceControl>&)> transactionBody) {
163 SurfaceComposerClient::Transaction t;
164 transactionBody(t, mSurfaceControl);
165 t.apply(true);
166 }
167
168 void showAt(int x, int y) {
169 SurfaceComposerClient::Transaction t;
170 t.show(mSurfaceControl);
171 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
172 t.setLayer(mSurfaceControl, LAYER_BASE);
173 t.setPosition(mSurfaceControl, x, y);
174 t.setCrop_legacy(mSurfaceControl, Rect(0, 0, 100, 100));
175 t.setAlpha(mSurfaceControl, 1);
176 t.apply(true);
177 }
178
179private:
180 void waitForEventAvailable() {
181 struct pollfd fd;
182
183 fd.fd = mClientChannel->getFd();
184 fd.events = POLLIN;
185 poll(&fd, 1, 3000);
186 }
187
188 void populateInputInfo(int width, int height) {
Robert Carr5c8a0262018-10-03 16:30:44 -0700189 mInputInfo.token = mServerChannel->getToken();
Robert Carr1c4c5592018-09-24 13:18:43 -0700190 mInputInfo.name = "Test info";
191 mInputInfo.layoutParamsFlags = InputWindowInfo::FLAG_NOT_TOUCH_MODAL;
192 mInputInfo.layoutParamsType = InputWindowInfo::TYPE_BASE_APPLICATION;
193 mInputInfo.dispatchingTimeout = 100000;
Robert Carre07e1032018-11-26 12:55:53 -0800194 mInputInfo.globalScaleFactor = 1.0;
Robert Carr1c4c5592018-09-24 13:18:43 -0700195 mInputInfo.canReceiveKeys = true;
196 mInputInfo.hasFocus = true;
197 mInputInfo.hasWallpaper = false;
198 mInputInfo.paused = false;
199
200 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
201
202 // TODO: Fill in from SF?
203 mInputInfo.ownerPid = 11111;
204 mInputInfo.ownerUid = 11111;
205 mInputInfo.inputFeatures = 0;
206 mInputInfo.displayId = 0;
Robert Carr740167f2018-10-11 19:03:41 -0700207
208 InputApplicationInfo aInfo;
209 aInfo.token = new BBinder();
210 aInfo.name = "Test app info";
211 aInfo.dispatchingTimeout = 100000;
212
213 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700214 }
215public:
216 sp<SurfaceControl> mSurfaceControl;
217 sp<InputChannel> mServerChannel, mClientChannel;
218 sp<IInputFlinger> mInputFlinger;
219
220 InputWindowInfo mInputInfo;
221
222 PreallocatedInputEventFactory mInputEventFactory;
223 InputConsumer* mInputConsumer;
224};
225
226class InputSurfacesTest : public ::testing::Test {
227public:
228 InputSurfacesTest() {
229 ProcessState::self()->startThreadPool();
230 }
231
232 void SetUp() {
233 mComposerClient = new SurfaceComposerClient;
234 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Vishnu Nairde19f852018-12-18 16:11:53 -0800235
236 DisplayInfo info;
237 auto display = mComposerClient->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
238 SurfaceComposerClient::getDisplayInfo(display, &info);
239
240 // After a new buffer is queued, SurfaceFlinger is notified and will
241 // latch the new buffer on next vsync. Let's heuristically wait for 3
242 // vsyncs.
243 mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700244 }
245
246 void TearDown() {
247 mComposerClient->dispose();
248 }
249
250 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800251 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
252 }
253
254 void postBuffer(const sp<SurfaceControl> &layer) {
255 // wait for previous transactions (such as setSize) to complete
256 Transaction().apply(true);
257 ANativeWindow_Buffer buffer = {};
258 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
259 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
260 // Request an empty transaction to get applied synchronously to ensure the buffer is
261 // latched.
262 Transaction().apply(true);
263 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700264 }
265
266 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800267 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700268};
269
270void injectTap(int x, int y) {
271 char *buf1, *buf2;
272 asprintf(&buf1, "%d", x);
273 asprintf(&buf2, "%d", y);
274 if (fork() == 0) {
275 execlp("input", "input", "tap", buf1, buf2, NULL);
276 }
277}
278
chaviwfbe5d9c2018-12-26 12:23:37 -0800279void injectMotionEvent(std::string event, int x, int y) {
280 char *buf1, *buf2;
281 asprintf(&buf1, "%d", x);
282 asprintf(&buf2, "%d", y);
283 if (fork() == 0) {
284 execlp("input", "input", "motionevent", event.c_str(), buf1, buf2, NULL);
285 }
286}
287
Robert Carr1c4c5592018-09-24 13:18:43 -0700288TEST_F(InputSurfacesTest, can_receive_input) {
289 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
290 surface->showAt(100, 100);
291
292 injectTap(101, 101);
293
294 EXPECT_TRUE(surface->consumeEvent() != nullptr);
295}
296
297TEST_F(InputSurfacesTest, input_respects_positioning) {
298 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
299 surface->showAt(100, 100);
300
301 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
302 surface2->showAt(200, 200);
303
304 injectTap(201, 201);
305 surface2->expectTap(1, 1);
306
307 injectTap(101, 101);
308 surface->expectTap(1, 1);
309
310 surface2->doTransaction([](auto &t, auto &sc) {
311 t.setPosition(sc, 100, 100);
312 });
313 surface->doTransaction([](auto &t, auto &sc) {
314 t.setPosition(sc, 200, 200);
315 });
316
317 injectTap(101, 101);
318 surface2->expectTap(1, 1);
319
320 injectTap(201, 201);
321 surface->expectTap(1, 1);
322}
323
324TEST_F(InputSurfacesTest, input_respects_layering) {
325 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
326 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
327
328 surface->showAt(10, 10);
329 surface2->showAt(10, 10);
330
331 surface->doTransaction([](auto &t, auto &sc) {
332 t.setLayer(sc, LAYER_BASE + 1);
333 });
334
335 injectTap(11, 11);
336 surface->expectTap(1, 1);
337
338 surface2->doTransaction([](auto &t, auto &sc) {
339 t.setLayer(sc, LAYER_BASE + 1);
340 });
341
342 injectTap(11, 11);
343 surface2->expectTap(1, 1);
344
345 surface2->doTransaction([](auto &t, auto &sc) {
346 t.hide(sc);
347 });
348
349 injectTap(11, 11);
350 surface->expectTap(1, 1);
351}
352
Vishnu Nairde19f852018-12-18 16:11:53 -0800353// Surface Insets are set to offset the client content and draw a border around the client surface
354// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
355// of the client content.
356TEST_F(InputSurfacesTest, input_respects_surface_insets) {
357 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
358 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
359 bgSurface->showAt(100, 100);
360
361 fgSurface->mInputInfo.surfaceInset = 5;
362 fgSurface->showAt(100, 100);
363
364 injectTap(106, 106);
365 fgSurface->expectTap(1, 1);
366
367 injectTap(101, 101);
368 bgSurface->expectTap(1, 1);
369}
370
371// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
372TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
373 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
374 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
375 parentSurface->showAt(100, 100);
376
377 childSurface->mInputInfo.surfaceInset = 10;
378 childSurface->showAt(100, 100);
379
380 childSurface->doTransaction([&](auto &t, auto &sc) {
381 t.setPosition(sc, -5, -5);
382 t.reparent(sc, parentSurface->mSurfaceControl->getHandle());
383 });
384
385 injectTap(106, 106);
386 childSurface->expectTap(1, 1);
387
388 injectTap(101, 101);
389 parentSurface->expectTap(1, 1);
390}
391
392// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
393TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
394 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
395 surface->doTransaction([](auto &t, auto &sc) {
396 Region transparentRegion(Rect(0, 0, 10, 10));
397 t.setTransparentRegionHint(sc, transparentRegion);
398 });
399 surface->showAt(100, 100);
400 injectTap(101, 101);
401 surface->expectTap(1, 1);
402}
403
404// Ensure we send the input to the right surface when the surface visibility changes due to the
405// first buffer being submitted. ref: b/120839715
406TEST_F(InputSurfacesTest, input_respects_buffer_layer_buffer) {
407 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
408 std::unique_ptr<InputSurface> bufferSurface =
409 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
410
411 bgSurface->showAt(10, 10);
412 bufferSurface->showAt(10, 10);
413
414 injectTap(11, 11);
415 bgSurface->expectTap(1, 1);
416
417 postBuffer(bufferSurface->mSurfaceControl);
418 injectTap(11, 11);
419 bufferSurface->expectTap(1, 1);
420}
421
422TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
423 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
424 std::unique_ptr<InputSurface> bufferSurface =
425 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
426 postBuffer(bufferSurface->mSurfaceControl);
427
428 bgSurface->showAt(10, 10);
429 bufferSurface->showAt(10, 10);
430
431 injectTap(11, 11);
432 bufferSurface->expectTap(1, 1);
433
434 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
435
436 injectTap(11, 11);
437 bgSurface->expectTap(1, 1);
438}
439
440TEST_F(InputSurfacesTest, input_respects_color_layer_alpha) {
441 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
442 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
443
444 bgSurface->showAt(10, 10);
445 fgSurface->showAt(10, 10);
446
447 injectTap(11, 11);
448 fgSurface->expectTap(1, 1);
449
450 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
451
452 injectTap(11, 11);
453 bgSurface->expectTap(1, 1);
454}
455
456TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
457 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
458 std::unique_ptr<InputSurface> containerSurface =
459 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
460
461 bgSurface->showAt(10, 10);
462 containerSurface->showAt(10, 10);
463
464 injectTap(11, 11);
465 containerSurface->expectTap(1, 1);
466
467 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
468
469 injectTap(11, 11);
470 bgSurface->expectTap(1, 1);
471}
chaviwfbe5d9c2018-12-26 12:23:37 -0800472
473TEST_F(InputSurfacesTest, transfer_touch_focus) {
474 std::unique_ptr<InputSurface> fromSurface = makeSurface(100, 100);
475
476 fromSurface->showAt(10, 10);
477 injectMotionEvent("DOWN", 11, 11);
478 fromSurface->expectMotionEvent(AMOTION_EVENT_ACTION_DOWN, 1, 1);
479
480 std::unique_ptr<InputSurface> toSurface = makeSurface(100, 100);
481 toSurface->showAt(10, 10);
482
483 sp<IBinder> fromToken = fromSurface->mServerChannel->getToken();
484 sp<IBinder> toToken = toSurface->mServerChannel->getToken();
485 SurfaceComposerClient::Transaction t;
486 t.transferTouchFocus(fromToken, toToken).apply(true);
487
488 injectMotionEvent("UP", 11, 11);
489 toSurface->expectMotionEvent(AMOTION_EVENT_ACTION_UP, 1, 1);
490 fromSurface->expectNoMotionEvent(AMOTION_EVENT_ACTION_UP);
491}
Robert Carr1c4c5592018-09-24 13:18:43 -0700492}
493}