blob: a68ec29d522193ad5ec1d646b000c8c6b3ae291c [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;
Chris Ye6c4243b2020-07-22 12:07:12 -070066static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070067
68class InputSurface {
69public:
Vishnu Nairde19f852018-12-18 16:11:53 -080070 InputSurface(const sp<SurfaceControl> &sc, int width, int height) {
71 mSurfaceControl = sc;
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050072 std::unique_ptr<InputChannel> clientChannel;
73 InputChannel::openInputChannelPair("testchannels", mServerChannel, clientChannel);
74 mClientChannel = std::move(clientChannel);
Robert Carr1c4c5592018-09-24 13:18:43 -070075
76 mInputFlinger = getInputFlinger();
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -050077 mInputFlinger->registerInputChannel(*mServerChannel);
Robert Carr1c4c5592018-09-24 13:18:43 -070078
79 populateInputInfo(width, height);
80
81 mInputConsumer = new InputConsumer(mClientChannel);
82 }
83
Vishnu Nairde19f852018-12-18 16:11:53 -080084 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
85 int width, int height) {
86 sp<SurfaceControl> surfaceControl =
87 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -080088 PIXEL_FORMAT_RGBA_8888,
89 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -080090 return std::make_unique<InputSurface>(surfaceControl, width, height);
91 }
92
93 static std::unique_ptr<InputSurface> makeBufferInputSurface(
94 const sp<SurfaceComposerClient> &scc, int width, int height) {
95 sp<SurfaceControl> surfaceControl =
96 scc->createSurface(String8("Test Buffer Surface"), width, height,
97 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
98 return std::make_unique<InputSurface>(surfaceControl, width, height);
99 }
100
101 static std::unique_ptr<InputSurface> makeContainerInputSurface(
102 const sp<SurfaceComposerClient> &scc, int width, int height) {
103 sp<SurfaceControl> surfaceControl =
104 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
105 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
106 ISurfaceComposerClient::eFXSurfaceContainer);
107 return std::make_unique<InputSurface>(surfaceControl, width, height);
108 }
109
arthurhungb4a0f852020-06-16 11:02:50 +0800110 static std::unique_ptr<InputSurface> makeCursorInputSurface(
111 const sp<SurfaceComposerClient> &scc, int width, int height) {
112 sp<SurfaceControl> surfaceControl =
113 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
114 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
115 ISurfaceComposerClient::eCursorWindow);
116 return std::make_unique<InputSurface>(surfaceControl, width, height);
117 }
118
Robert Carr1c4c5592018-09-24 13:18:43 -0700119 InputEvent* consumeEvent() {
120 waitForEventAvailable();
121
122 InputEvent *ev;
123 uint32_t seqId;
124 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
125 if (consumed != OK) {
126 return nullptr;
127 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100128 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
129 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700130 return ev;
131 }
132
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100133 void assertFocusChange(bool hasFocus) {
134 InputEvent *ev = consumeEvent();
135 ASSERT_NE(ev, nullptr);
136 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, ev->getType());
137 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
138 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
139 }
140
Robert Carr1c4c5592018-09-24 13:18:43 -0700141 void expectTap(int x, int y) {
142 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100143 ASSERT_NE(ev, nullptr);
144 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700145 MotionEvent* mev = static_cast<MotionEvent*>(ev);
146 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
147 EXPECT_EQ(x, mev->getX(0));
148 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800149 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700150
151 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100152 ASSERT_NE(ev, nullptr);
153 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700154 mev = static_cast<MotionEvent*>(ev);
155 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800156 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700157 }
158
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500159 ~InputSurface() { mInputFlinger->unregisterInputChannel(*mServerChannel); }
Robert Carr1c4c5592018-09-24 13:18:43 -0700160
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) {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700189 mInputInfo.token = mServerChannel->getConnectionToken();
Robert Carr1c4c5592018-09-24 13:18:43 -0700190 mInputInfo.name = "Test info";
Michael Wright44753b12020-07-08 13:48:11 +0100191 mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
192 mInputInfo.type = InputWindowInfo::Type::BASE_APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500193 mInputInfo.dispatchingTimeout = 5s;
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;
Robert Carr1c4c5592018-09-24 13:18:43 -0700205 mInputInfo.displayId = 0;
Robert Carr740167f2018-10-11 19:03:41 -0700206
207 InputApplicationInfo aInfo;
208 aInfo.token = new BBinder();
209 aInfo.name = "Test app info";
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500210 aInfo.dispatchingTimeoutMillis =
211 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Robert Carr740167f2018-10-11 19:03:41 -0700212
213 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700214 }
215public:
216 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500217 std::unique_ptr<InputChannel> mServerChannel;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500218 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700219 sp<IInputFlinger> mInputFlinger;
220
221 InputWindowInfo mInputInfo;
222
223 PreallocatedInputEventFactory mInputEventFactory;
224 InputConsumer* mInputConsumer;
225};
226
227class InputSurfacesTest : public ::testing::Test {
228public:
229 InputSurfacesTest() {
230 ProcessState::self()->startThreadPool();
231 }
232
233 void SetUp() {
234 mComposerClient = new SurfaceComposerClient;
235 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Vishnu Nairde19f852018-12-18 16:11:53 -0800236
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800237 const auto display = mComposerClient->getInternalDisplayToken();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100238 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800239
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800240 DisplayConfig config;
241 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayConfig(display, &config));
Vishnu Nairde19f852018-12-18 16:11:53 -0800242
243 // After a new buffer is queued, SurfaceFlinger is notified and will
244 // latch the new buffer on next vsync. Let's heuristically wait for 3
245 // vsyncs.
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800246 mBufferPostDelay = static_cast<int32_t>(1e6 / config.refreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700247 }
248
249 void TearDown() {
250 mComposerClient->dispose();
251 }
252
253 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800254 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
255 }
256
257 void postBuffer(const sp<SurfaceControl> &layer) {
258 // wait for previous transactions (such as setSize) to complete
259 Transaction().apply(true);
260 ANativeWindow_Buffer buffer = {};
261 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
262 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
263 // Request an empty transaction to get applied synchronously to ensure the buffer is
264 // latched.
265 Transaction().apply(true);
266 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700267 }
268
269 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800270 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700271};
272
273void injectTap(int x, int y) {
274 char *buf1, *buf2;
275 asprintf(&buf1, "%d", x);
276 asprintf(&buf2, "%d", y);
277 if (fork() == 0) {
278 execlp("input", "input", "tap", buf1, buf2, NULL);
279 }
280}
281
282TEST_F(InputSurfacesTest, can_receive_input) {
283 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
284 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100285 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700286
287 injectTap(101, 101);
288
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100289 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700290}
291
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100292/**
293 * Set up two surfaces side-by-side. Tap each surface.
294 * Next, swap the positions of the two surfaces. Inject tap into the two
295 * original locations. Ensure that the tap is received by the surfaces in the
296 * reverse order.
297 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700298TEST_F(InputSurfacesTest, input_respects_positioning) {
299 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
300 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100301 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700302
303 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
304 surface2->showAt(200, 200);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100305 surface->assertFocusChange(false);
306 surface2->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700307
308 injectTap(201, 201);
309 surface2->expectTap(1, 1);
310
311 injectTap(101, 101);
312 surface->expectTap(1, 1);
313
314 surface2->doTransaction([](auto &t, auto &sc) {
315 t.setPosition(sc, 100, 100);
316 });
317 surface->doTransaction([](auto &t, auto &sc) {
318 t.setPosition(sc, 200, 200);
319 });
320
321 injectTap(101, 101);
322 surface2->expectTap(1, 1);
323
324 injectTap(201, 201);
325 surface->expectTap(1, 1);
326}
327
328TEST_F(InputSurfacesTest, input_respects_layering) {
329 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
330 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
331
332 surface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100333 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700334 surface2->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100335 surface->assertFocusChange(false);
336 surface2->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700337
338 surface->doTransaction([](auto &t, auto &sc) {
339 t.setLayer(sc, LAYER_BASE + 1);
340 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100341 surface2->assertFocusChange(false);
342 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700343
344 injectTap(11, 11);
345 surface->expectTap(1, 1);
346
347 surface2->doTransaction([](auto &t, auto &sc) {
348 t.setLayer(sc, LAYER_BASE + 1);
349 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100350 surface2->assertFocusChange(true);
351 surface->assertFocusChange(false);
Robert Carr1c4c5592018-09-24 13:18:43 -0700352
353 injectTap(11, 11);
354 surface2->expectTap(1, 1);
355
356 surface2->doTransaction([](auto &t, auto &sc) {
357 t.hide(sc);
358 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100359 surface2->assertFocusChange(false);
360 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700361
362 injectTap(11, 11);
363 surface->expectTap(1, 1);
364}
365
Vishnu Nairde19f852018-12-18 16:11:53 -0800366// Surface Insets are set to offset the client content and draw a border around the client surface
367// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
368// of the client content.
369TEST_F(InputSurfacesTest, input_respects_surface_insets) {
370 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
371 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
372 bgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100373 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800374
375 fgSurface->mInputInfo.surfaceInset = 5;
376 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100377 fgSurface->assertFocusChange(true);
378 bgSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800379
380 injectTap(106, 106);
381 fgSurface->expectTap(1, 1);
382
383 injectTap(101, 101);
384 bgSurface->expectTap(1, 1);
385}
386
387// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
388TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
389 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
390 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
391 parentSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100392 parentSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800393
394 childSurface->mInputInfo.surfaceInset = 10;
395 childSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100396 childSurface->assertFocusChange(true);
397 parentSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800398
399 childSurface->doTransaction([&](auto &t, auto &sc) {
400 t.setPosition(sc, -5, -5);
401 t.reparent(sc, parentSurface->mSurfaceControl->getHandle());
402 });
403
404 injectTap(106, 106);
405 childSurface->expectTap(1, 1);
406
407 injectTap(101, 101);
408 parentSurface->expectTap(1, 1);
409}
410
Arthur Hung118b1142019-05-08 21:25:59 +0800411// Ensure a surface whose insets are scaled, handles the touch offset correctly.
412TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
413 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
414 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
415 bgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100416 bgSurface->assertFocusChange(true);
Arthur Hung118b1142019-05-08 21:25:59 +0800417
418 fgSurface->mInputInfo.surfaceInset = 5;
419 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100420 bgSurface->assertFocusChange(false);
421 fgSurface->assertFocusChange(true);
Arthur Hung118b1142019-05-08 21:25:59 +0800422
423 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
424
425 // expect = touch / scale - inset
426 injectTap(112, 124);
427 fgSurface->expectTap(1, 1);
428
429 injectTap(101, 101);
430 bgSurface->expectTap(1, 1);
431}
432
Ady Abraham282f1d72019-07-24 18:05:56 -0700433TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
434 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
435 // In case we pass the very big inset without any checking.
436 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
437 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100438 fgSurface->assertFocusChange(true);
Ady Abraham282f1d72019-07-24 18:05:56 -0700439
440 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
441
442 // expect no crash for overflow, and inset size to be clamped to surface size
443 injectTap(202, 202);
444 fgSurface->expectTap(1, 1);
445}
446
Vishnu Nairde19f852018-12-18 16:11:53 -0800447// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
448TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
449 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
450 surface->doTransaction([](auto &t, auto &sc) {
451 Region transparentRegion(Rect(0, 0, 10, 10));
452 t.setTransparentRegionHint(sc, transparentRegion);
453 });
454 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100455 surface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800456 injectTap(101, 101);
457 surface->expectTap(1, 1);
458}
459
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700460// TODO(b/139494112) update tests once we define expected behavior
461// Ensure we still send input to the surface regardless of surface visibility changes due to the
462// first buffer being submitted or alpha changes.
463// Original bug ref: b/120839715
464TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800465 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
466 std::unique_ptr<InputSurface> bufferSurface =
467 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
468
469 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100470 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800471 bufferSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100472 bgSurface->assertFocusChange(false);
473 bufferSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800474
475 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700476 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800477
478 postBuffer(bufferSurface->mSurfaceControl);
479 injectTap(11, 11);
480 bufferSurface->expectTap(1, 1);
481}
482
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700483TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800484 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
485 std::unique_ptr<InputSurface> bufferSurface =
486 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
487 postBuffer(bufferSurface->mSurfaceControl);
488
489 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100490 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800491 bufferSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100492 bufferSurface->assertFocusChange(true);
493 bgSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800494
495 injectTap(11, 11);
496 bufferSurface->expectTap(1, 1);
497
498 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
499
500 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700501 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800502}
503
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700504TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800505 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
506 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
507
508 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100509 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800510 fgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100511 bgSurface->assertFocusChange(false);
512 fgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800513
514 injectTap(11, 11);
515 fgSurface->expectTap(1, 1);
516
517 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
518
519 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700520 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800521}
522
523TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
524 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
525 std::unique_ptr<InputSurface> containerSurface =
526 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
527
528 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100529 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800530 containerSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100531 bgSurface->assertFocusChange(false);
532 containerSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800533
534 injectTap(11, 11);
535 containerSurface->expectTap(1, 1);
536
537 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100538 containerSurface->assertFocusChange(false);
539 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800540
541 injectTap(11, 11);
542 bgSurface->expectTap(1, 1);
543}
chaviwfbe5d9c2018-12-26 12:23:37 -0800544
Arthur Hungd20b2702019-01-14 18:16:16 +0800545TEST_F(InputSurfacesTest, input_respects_outscreen) {
546 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
547 surface->showAt(-1, -1);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100548 surface->assertFocusChange(true);
Arthur Hungd20b2702019-01-14 18:16:16 +0800549
550 injectTap(0, 0);
551 surface->expectTap(1, 1);
552}
arthurhungb4a0f852020-06-16 11:02:50 +0800553
554TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
555 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
556 std::unique_ptr<InputSurface> cursorSurface =
557 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
558
559 surface->showAt(10, 10);
560 surface->assertFocusChange(true);
561 cursorSurface->showAt(10, 10);
562
563 injectTap(11, 11);
564 surface->expectTap(1, 1);
565}
Robert Carr1c4c5592018-09-24 13:18:43 -0700566}
567}