blob: 7ec2c1a913421cdde1a07bd60cf41a2ebab93c00 [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();
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -050075 mInputFlinger->registerInputChannel(*mServerChannel);
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
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500157 ~InputSurface() { mInputFlinger->unregisterInputChannel(*mServerChannel); }
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;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500214 std::shared_ptr<InputChannel> mServerChannel;
215 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700216 sp<IInputFlinger> mInputFlinger;
217
218 InputWindowInfo mInputInfo;
219
220 PreallocatedInputEventFactory mInputEventFactory;
221 InputConsumer* mInputConsumer;
222};
223
224class InputSurfacesTest : public ::testing::Test {
225public:
226 InputSurfacesTest() {
227 ProcessState::self()->startThreadPool();
228 }
229
230 void SetUp() {
231 mComposerClient = new SurfaceComposerClient;
232 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Vishnu Nairde19f852018-12-18 16:11:53 -0800233
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800234 const auto display = mComposerClient->getInternalDisplayToken();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100235 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800236
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800237 DisplayConfig config;
238 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayConfig(display, &config));
Vishnu Nairde19f852018-12-18 16:11:53 -0800239
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.
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800243 mBufferPostDelay = static_cast<int32_t>(1e6 / config.refreshRate) * 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
279TEST_F(InputSurfacesTest, can_receive_input) {
280 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
281 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100282 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700283
284 injectTap(101, 101);
285
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100286 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700287}
288
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100289/**
290 * Set up two surfaces side-by-side. Tap each surface.
291 * Next, swap the positions of the two surfaces. Inject tap into the two
292 * original locations. Ensure that the tap is received by the surfaces in the
293 * reverse order.
294 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700295TEST_F(InputSurfacesTest, input_respects_positioning) {
296 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
297 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100298 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700299
300 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
301 surface2->showAt(200, 200);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100302 surface->assertFocusChange(false);
303 surface2->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700304
305 injectTap(201, 201);
306 surface2->expectTap(1, 1);
307
308 injectTap(101, 101);
309 surface->expectTap(1, 1);
310
311 surface2->doTransaction([](auto &t, auto &sc) {
312 t.setPosition(sc, 100, 100);
313 });
314 surface->doTransaction([](auto &t, auto &sc) {
315 t.setPosition(sc, 200, 200);
316 });
317
318 injectTap(101, 101);
319 surface2->expectTap(1, 1);
320
321 injectTap(201, 201);
322 surface->expectTap(1, 1);
323}
324
325TEST_F(InputSurfacesTest, input_respects_layering) {
326 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
327 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
328
329 surface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100330 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700331 surface2->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100332 surface->assertFocusChange(false);
333 surface2->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700334
335 surface->doTransaction([](auto &t, auto &sc) {
336 t.setLayer(sc, LAYER_BASE + 1);
337 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100338 surface2->assertFocusChange(false);
339 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700340
341 injectTap(11, 11);
342 surface->expectTap(1, 1);
343
344 surface2->doTransaction([](auto &t, auto &sc) {
345 t.setLayer(sc, LAYER_BASE + 1);
346 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100347 surface2->assertFocusChange(true);
348 surface->assertFocusChange(false);
Robert Carr1c4c5592018-09-24 13:18:43 -0700349
350 injectTap(11, 11);
351 surface2->expectTap(1, 1);
352
353 surface2->doTransaction([](auto &t, auto &sc) {
354 t.hide(sc);
355 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100356 surface2->assertFocusChange(false);
357 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700358
359 injectTap(11, 11);
360 surface->expectTap(1, 1);
361}
362
Vishnu Nairde19f852018-12-18 16:11:53 -0800363// Surface Insets are set to offset the client content and draw a border around the client surface
364// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
365// of the client content.
366TEST_F(InputSurfacesTest, input_respects_surface_insets) {
367 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
368 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
369 bgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100370 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800371
372 fgSurface->mInputInfo.surfaceInset = 5;
373 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100374 fgSurface->assertFocusChange(true);
375 bgSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800376
377 injectTap(106, 106);
378 fgSurface->expectTap(1, 1);
379
380 injectTap(101, 101);
381 bgSurface->expectTap(1, 1);
382}
383
384// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
385TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
386 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
387 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
388 parentSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100389 parentSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800390
391 childSurface->mInputInfo.surfaceInset = 10;
392 childSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100393 childSurface->assertFocusChange(true);
394 parentSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800395
396 childSurface->doTransaction([&](auto &t, auto &sc) {
397 t.setPosition(sc, -5, -5);
398 t.reparent(sc, parentSurface->mSurfaceControl->getHandle());
399 });
400
401 injectTap(106, 106);
402 childSurface->expectTap(1, 1);
403
404 injectTap(101, 101);
405 parentSurface->expectTap(1, 1);
406}
407
Arthur Hung118b1142019-05-08 21:25:59 +0800408// Ensure a surface whose insets are scaled, handles the touch offset correctly.
409TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
410 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
411 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
412 bgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100413 bgSurface->assertFocusChange(true);
Arthur Hung118b1142019-05-08 21:25:59 +0800414
415 fgSurface->mInputInfo.surfaceInset = 5;
416 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100417 bgSurface->assertFocusChange(false);
418 fgSurface->assertFocusChange(true);
Arthur Hung118b1142019-05-08 21:25:59 +0800419
420 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
421
422 // expect = touch / scale - inset
423 injectTap(112, 124);
424 fgSurface->expectTap(1, 1);
425
426 injectTap(101, 101);
427 bgSurface->expectTap(1, 1);
428}
429
Ady Abraham282f1d72019-07-24 18:05:56 -0700430TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
431 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
432 // In case we pass the very big inset without any checking.
433 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
434 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100435 fgSurface->assertFocusChange(true);
Ady Abraham282f1d72019-07-24 18:05:56 -0700436
437 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
438
439 // expect no crash for overflow, and inset size to be clamped to surface size
440 injectTap(202, 202);
441 fgSurface->expectTap(1, 1);
442}
443
Vishnu Nairde19f852018-12-18 16:11:53 -0800444// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
445TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
446 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
447 surface->doTransaction([](auto &t, auto &sc) {
448 Region transparentRegion(Rect(0, 0, 10, 10));
449 t.setTransparentRegionHint(sc, transparentRegion);
450 });
451 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100452 surface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800453 injectTap(101, 101);
454 surface->expectTap(1, 1);
455}
456
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700457// TODO(b/139494112) update tests once we define expected behavior
458// Ensure we still send input to the surface regardless of surface visibility changes due to the
459// first buffer being submitted or alpha changes.
460// Original bug ref: b/120839715
461TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800462 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
463 std::unique_ptr<InputSurface> bufferSurface =
464 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
465
466 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100467 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800468 bufferSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100469 bgSurface->assertFocusChange(false);
470 bufferSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800471
472 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700473 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800474
475 postBuffer(bufferSurface->mSurfaceControl);
476 injectTap(11, 11);
477 bufferSurface->expectTap(1, 1);
478}
479
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700480TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800481 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
482 std::unique_ptr<InputSurface> bufferSurface =
483 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
484 postBuffer(bufferSurface->mSurfaceControl);
485
486 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100487 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800488 bufferSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100489 bufferSurface->assertFocusChange(true);
490 bgSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800491
492 injectTap(11, 11);
493 bufferSurface->expectTap(1, 1);
494
495 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
496
497 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700498 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800499}
500
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700501TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800502 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
503 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
504
505 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100506 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800507 fgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100508 bgSurface->assertFocusChange(false);
509 fgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800510
511 injectTap(11, 11);
512 fgSurface->expectTap(1, 1);
513
514 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
515
516 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700517 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800518}
519
520TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
521 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
522 std::unique_ptr<InputSurface> containerSurface =
523 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
524
525 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100526 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800527 containerSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100528 bgSurface->assertFocusChange(false);
529 containerSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800530
531 injectTap(11, 11);
532 containerSurface->expectTap(1, 1);
533
534 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100535 containerSurface->assertFocusChange(false);
536 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800537
538 injectTap(11, 11);
539 bgSurface->expectTap(1, 1);
540}
chaviwfbe5d9c2018-12-26 12:23:37 -0800541
Arthur Hungd20b2702019-01-14 18:16:16 +0800542TEST_F(InputSurfacesTest, input_respects_outscreen) {
543 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
544 surface->showAt(-1, -1);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100545 surface->assertFocusChange(true);
Arthur Hungd20b2702019-01-14 18:16:16 +0800546
547 injectTap(0, 0);
548 surface->expectTap(1, 1);
549}
arthurhungb4a0f852020-06-16 11:02:50 +0800550
551TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
552 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
553 std::unique_ptr<InputSurface> cursorSurface =
554 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
555
556 surface->showAt(10, 10);
557 surface->assertFocusChange(true);
558 cursorSurface->showAt(10, 10);
559
560 injectTap(11, 11);
561 surface->expectTap(1, 1);
562}
Robert Carr1c4c5592018-09-24 13:18:43 -0700563}
564}