blob: 383d5916a4487b74f1d1563daedeebb3ee5005bc [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
Chris Ye0783e992020-06-02 21:34:49 -070039#include <android/os/IInputFlinger.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070040#include <input/Input.h>
Chris Ye0783e992020-06-02 21:34:49 -070041#include <input/InputTransport.h>
42#include <input/InputWindow.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070043
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080044#include <ui/DisplayConfig.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070045#include <ui/Rect.h>
46#include <ui/Region.h>
47
Chris Ye0783e992020-06-02 21:34:49 -070048using android::os::IInputFlinger;
Robert Carr1c4c5592018-09-24 13:18:43 -070049
50namespace android {
51namespace test {
52
Vishnu Nairde19f852018-12-18 16:11:53 -080053using Transaction = SurfaceComposerClient::Transaction;
54
Robert Carr1c4c5592018-09-24 13:18:43 -070055sp<IInputFlinger> getInputFlinger() {
56 sp<IBinder> input(defaultServiceManager()->getService(
57 String16("inputflinger")));
58 if (input == nullptr) {
59 ALOGE("Failed to link to input service");
60 } else { ALOGE("Linked to input"); }
61 return interface_cast<IInputFlinger>(input);
62}
63
64// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
65static const int LAYER_BASE = INT32_MAX - 10;
66
67class InputSurface {
68public:
Vishnu Nairde19f852018-12-18 16:11:53 -080069 InputSurface(const sp<SurfaceControl> &sc, int width, int height) {
70 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -070071
72 InputChannel::openInputChannelPair("testchannels", mServerChannel, mClientChannel);
Robert Carr1c4c5592018-09-24 13:18:43 -070073
74 mInputFlinger = getInputFlinger();
Chris Ye0783e992020-06-02 21:34:49 -070075 mInputFlinger->registerInputChannel(mServerChannel->getInfo());
Robert Carr1c4c5592018-09-24 13:18:43 -070076
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 */,
Vishnu Nairfa247b12020-02-11 08:58:26 -080086 PIXEL_FORMAT_RGBA_8888,
87 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -080088 return std::make_unique<InputSurface>(surfaceControl, width, height);
89 }
90
91 static std::unique_ptr<InputSurface> makeBufferInputSurface(
92 const sp<SurfaceComposerClient> &scc, int width, int height) {
93 sp<SurfaceControl> surfaceControl =
94 scc->createSurface(String8("Test Buffer Surface"), width, height,
95 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
96 return std::make_unique<InputSurface>(surfaceControl, width, height);
97 }
98
99 static std::unique_ptr<InputSurface> makeContainerInputSurface(
100 const sp<SurfaceComposerClient> &scc, int width, int height) {
101 sp<SurfaceControl> surfaceControl =
102 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
103 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
104 ISurfaceComposerClient::eFXSurfaceContainer);
105 return std::make_unique<InputSurface>(surfaceControl, width, height);
106 }
107
arthurhungb4a0f852020-06-16 11:02:50 +0800108 static std::unique_ptr<InputSurface> makeCursorInputSurface(
109 const sp<SurfaceComposerClient> &scc, int width, int height) {
110 sp<SurfaceControl> surfaceControl =
111 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
112 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
113 ISurfaceComposerClient::eCursorWindow);
114 return std::make_unique<InputSurface>(surfaceControl, width, height);
115 }
116
Robert Carr1c4c5592018-09-24 13:18:43 -0700117 InputEvent* consumeEvent() {
118 waitForEventAvailable();
119
120 InputEvent *ev;
121 uint32_t seqId;
122 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
123 if (consumed != OK) {
124 return nullptr;
125 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100126 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
127 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700128 return ev;
129 }
130
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100131 void assertFocusChange(bool hasFocus) {
132 InputEvent *ev = consumeEvent();
133 ASSERT_NE(ev, nullptr);
134 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, ev->getType());
135 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
136 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
137 }
138
Robert Carr1c4c5592018-09-24 13:18:43 -0700139 void expectTap(int x, int y) {
140 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100141 ASSERT_NE(ev, nullptr);
142 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700143 MotionEvent* mev = static_cast<MotionEvent*>(ev);
144 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
145 EXPECT_EQ(x, mev->getX(0));
146 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800147 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700148
149 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100150 ASSERT_NE(ev, nullptr);
151 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700152 mev = static_cast<MotionEvent*>(ev);
153 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800154 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700155 }
156
Chris Ye0783e992020-06-02 21:34:49 -0700157 ~InputSurface() { mInputFlinger->unregisterInputChannel(mServerChannel->getInfo()); }
Robert Carr1c4c5592018-09-24 13:18:43 -0700158
159 void doTransaction(std::function<void(SurfaceComposerClient::Transaction&,
160 const sp<SurfaceControl>&)> transactionBody) {
161 SurfaceComposerClient::Transaction t;
162 transactionBody(t, mSurfaceControl);
163 t.apply(true);
164 }
165
166 void showAt(int x, int y) {
167 SurfaceComposerClient::Transaction t;
168 t.show(mSurfaceControl);
169 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
170 t.setLayer(mSurfaceControl, LAYER_BASE);
171 t.setPosition(mSurfaceControl, x, y);
172 t.setCrop_legacy(mSurfaceControl, Rect(0, 0, 100, 100));
173 t.setAlpha(mSurfaceControl, 1);
174 t.apply(true);
175 }
176
177private:
178 void waitForEventAvailable() {
179 struct pollfd fd;
180
181 fd.fd = mClientChannel->getFd();
182 fd.events = POLLIN;
183 poll(&fd, 1, 3000);
184 }
185
186 void populateInputInfo(int width, int height) {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700187 mInputInfo.token = mServerChannel->getConnectionToken();
Robert Carr1c4c5592018-09-24 13:18:43 -0700188 mInputInfo.name = "Test info";
Michael Wright44753b12020-07-08 13:48:11 +0100189 mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
190 mInputInfo.type = InputWindowInfo::Type::BASE_APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500191 mInputInfo.dispatchingTimeout = 5s;
Robert Carre07e1032018-11-26 12:55:53 -0800192 mInputInfo.globalScaleFactor = 1.0;
Robert Carr1c4c5592018-09-24 13:18:43 -0700193 mInputInfo.canReceiveKeys = true;
194 mInputInfo.hasFocus = true;
195 mInputInfo.hasWallpaper = false;
196 mInputInfo.paused = false;
197
198 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
199
200 // TODO: Fill in from SF?
201 mInputInfo.ownerPid = 11111;
202 mInputInfo.ownerUid = 11111;
Robert Carr1c4c5592018-09-24 13:18:43 -0700203 mInputInfo.displayId = 0;
Robert Carr740167f2018-10-11 19:03:41 -0700204
205 InputApplicationInfo aInfo;
206 aInfo.token = new BBinder();
207 aInfo.name = "Test app info";
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500208 aInfo.dispatchingTimeout = 5s;
Robert Carr740167f2018-10-11 19:03:41 -0700209
210 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700211 }
212public:
213 sp<SurfaceControl> mSurfaceControl;
214 sp<InputChannel> mServerChannel, mClientChannel;
215 sp<IInputFlinger> mInputFlinger;
216
217 InputWindowInfo mInputInfo;
218
219 PreallocatedInputEventFactory mInputEventFactory;
220 InputConsumer* mInputConsumer;
221};
222
223class InputSurfacesTest : public ::testing::Test {
224public:
225 InputSurfacesTest() {
226 ProcessState::self()->startThreadPool();
227 }
228
229 void SetUp() {
230 mComposerClient = new SurfaceComposerClient;
231 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Vishnu Nairde19f852018-12-18 16:11:53 -0800232
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800233 const auto display = mComposerClient->getInternalDisplayToken();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100234 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800235
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800236 DisplayConfig config;
237 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayConfig(display, &config));
Vishnu Nairde19f852018-12-18 16:11:53 -0800238
239 // After a new buffer is queued, SurfaceFlinger is notified and will
240 // latch the new buffer on next vsync. Let's heuristically wait for 3
241 // vsyncs.
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800242 mBufferPostDelay = static_cast<int32_t>(1e6 / config.refreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700243 }
244
245 void TearDown() {
246 mComposerClient->dispose();
247 }
248
249 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800250 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
251 }
252
253 void postBuffer(const sp<SurfaceControl> &layer) {
254 // wait for previous transactions (such as setSize) to complete
255 Transaction().apply(true);
256 ANativeWindow_Buffer buffer = {};
257 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
258 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
259 // Request an empty transaction to get applied synchronously to ensure the buffer is
260 // latched.
261 Transaction().apply(true);
262 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700263 }
264
265 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800266 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700267};
268
269void injectTap(int x, int y) {
270 char *buf1, *buf2;
271 asprintf(&buf1, "%d", x);
272 asprintf(&buf2, "%d", y);
273 if (fork() == 0) {
274 execlp("input", "input", "tap", buf1, buf2, NULL);
275 }
276}
277
278TEST_F(InputSurfacesTest, can_receive_input) {
279 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
280 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100281 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700282
283 injectTap(101, 101);
284
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100285 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700286}
287
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100288/**
289 * Set up two surfaces side-by-side. Tap each surface.
290 * Next, swap the positions of the two surfaces. Inject tap into the two
291 * original locations. Ensure that the tap is received by the surfaces in the
292 * reverse order.
293 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700294TEST_F(InputSurfacesTest, input_respects_positioning) {
295 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
296 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100297 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700298
299 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
300 surface2->showAt(200, 200);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100301 surface->assertFocusChange(false);
302 surface2->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700303
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);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100329 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700330 surface2->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100331 surface->assertFocusChange(false);
332 surface2->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700333
334 surface->doTransaction([](auto &t, auto &sc) {
335 t.setLayer(sc, LAYER_BASE + 1);
336 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100337 surface2->assertFocusChange(false);
338 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700339
340 injectTap(11, 11);
341 surface->expectTap(1, 1);
342
343 surface2->doTransaction([](auto &t, auto &sc) {
344 t.setLayer(sc, LAYER_BASE + 1);
345 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100346 surface2->assertFocusChange(true);
347 surface->assertFocusChange(false);
Robert Carr1c4c5592018-09-24 13:18:43 -0700348
349 injectTap(11, 11);
350 surface2->expectTap(1, 1);
351
352 surface2->doTransaction([](auto &t, auto &sc) {
353 t.hide(sc);
354 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100355 surface2->assertFocusChange(false);
356 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700357
358 injectTap(11, 11);
359 surface->expectTap(1, 1);
360}
361
Vishnu Nairde19f852018-12-18 16:11:53 -0800362// Surface Insets are set to offset the client content and draw a border around the client surface
363// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
364// of the client content.
365TEST_F(InputSurfacesTest, input_respects_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);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100369 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800370
371 fgSurface->mInputInfo.surfaceInset = 5;
372 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100373 fgSurface->assertFocusChange(true);
374 bgSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800375
376 injectTap(106, 106);
377 fgSurface->expectTap(1, 1);
378
379 injectTap(101, 101);
380 bgSurface->expectTap(1, 1);
381}
382
383// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
384TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
385 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
386 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
387 parentSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100388 parentSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800389
390 childSurface->mInputInfo.surfaceInset = 10;
391 childSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100392 childSurface->assertFocusChange(true);
393 parentSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800394
395 childSurface->doTransaction([&](auto &t, auto &sc) {
396 t.setPosition(sc, -5, -5);
397 t.reparent(sc, parentSurface->mSurfaceControl->getHandle());
398 });
399
400 injectTap(106, 106);
401 childSurface->expectTap(1, 1);
402
403 injectTap(101, 101);
404 parentSurface->expectTap(1, 1);
405}
406
Arthur Hung118b1142019-05-08 21:25:59 +0800407// Ensure a surface whose insets are scaled, handles the touch offset correctly.
408TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
409 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
410 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
411 bgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100412 bgSurface->assertFocusChange(true);
Arthur Hung118b1142019-05-08 21:25:59 +0800413
414 fgSurface->mInputInfo.surfaceInset = 5;
415 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100416 bgSurface->assertFocusChange(false);
417 fgSurface->assertFocusChange(true);
Arthur Hung118b1142019-05-08 21:25:59 +0800418
419 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
420
421 // expect = touch / scale - inset
422 injectTap(112, 124);
423 fgSurface->expectTap(1, 1);
424
425 injectTap(101, 101);
426 bgSurface->expectTap(1, 1);
427}
428
Ady Abraham282f1d72019-07-24 18:05:56 -0700429TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
430 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
431 // In case we pass the very big inset without any checking.
432 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
433 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100434 fgSurface->assertFocusChange(true);
Ady Abraham282f1d72019-07-24 18:05:56 -0700435
436 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
437
438 // expect no crash for overflow, and inset size to be clamped to surface size
439 injectTap(202, 202);
440 fgSurface->expectTap(1, 1);
441}
442
Vishnu Nairde19f852018-12-18 16:11:53 -0800443// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
444TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
445 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
446 surface->doTransaction([](auto &t, auto &sc) {
447 Region transparentRegion(Rect(0, 0, 10, 10));
448 t.setTransparentRegionHint(sc, transparentRegion);
449 });
450 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100451 surface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800452 injectTap(101, 101);
453 surface->expectTap(1, 1);
454}
455
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700456// TODO(b/139494112) update tests once we define expected behavior
457// Ensure we still send input to the surface regardless of surface visibility changes due to the
458// first buffer being submitted or alpha changes.
459// Original bug ref: b/120839715
460TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800461 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
462 std::unique_ptr<InputSurface> bufferSurface =
463 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
464
465 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100466 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800467 bufferSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100468 bgSurface->assertFocusChange(false);
469 bufferSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800470
471 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700472 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800473
474 postBuffer(bufferSurface->mSurfaceControl);
475 injectTap(11, 11);
476 bufferSurface->expectTap(1, 1);
477}
478
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700479TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800480 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
481 std::unique_ptr<InputSurface> bufferSurface =
482 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
483 postBuffer(bufferSurface->mSurfaceControl);
484
485 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100486 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800487 bufferSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100488 bufferSurface->assertFocusChange(true);
489 bgSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800490
491 injectTap(11, 11);
492 bufferSurface->expectTap(1, 1);
493
494 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
495
496 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700497 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800498}
499
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700500TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800501 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
502 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
503
504 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100505 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800506 fgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100507 bgSurface->assertFocusChange(false);
508 fgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800509
510 injectTap(11, 11);
511 fgSurface->expectTap(1, 1);
512
513 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
514
515 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700516 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800517}
518
519TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
520 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
521 std::unique_ptr<InputSurface> containerSurface =
522 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
523
524 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100525 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800526 containerSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100527 bgSurface->assertFocusChange(false);
528 containerSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800529
530 injectTap(11, 11);
531 containerSurface->expectTap(1, 1);
532
533 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100534 containerSurface->assertFocusChange(false);
535 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800536
537 injectTap(11, 11);
538 bgSurface->expectTap(1, 1);
539}
chaviwfbe5d9c2018-12-26 12:23:37 -0800540
Arthur Hungd20b2702019-01-14 18:16:16 +0800541TEST_F(InputSurfacesTest, input_respects_outscreen) {
542 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
543 surface->showAt(-1, -1);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100544 surface->assertFocusChange(true);
Arthur Hungd20b2702019-01-14 18:16:16 +0800545
546 injectTap(0, 0);
547 surface->expectTap(1, 1);
548}
arthurhungb4a0f852020-06-16 11:02:50 +0800549
550TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
551 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
552 std::unique_ptr<InputSurface> cursorSurface =
553 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
554
555 surface->showAt(10, 10);
556 surface->assertFocusChange(true);
557 cursorSurface->showAt(10, 10);
558
559 injectTap(11, 11);
560 surface->expectTap(1, 1);
561}
Robert Carr1c4c5592018-09-24 13:18:43 -0700562}
563}