blob: 5b429d00c1ef72fa589aa49d230841edca4b0eae [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";
Chris Ye6c4243b2020-07-22 12:07:12 -0700210 aInfo.dispatchingTimeoutNanos = DISPATCHING_TIMEOUT.count();
Robert Carr740167f2018-10-11 19:03:41 -0700211
212 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700213 }
214public:
215 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500216 std::unique_ptr<InputChannel> mServerChannel;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500217 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700218 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
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800236 const auto display = mComposerClient->getInternalDisplayToken();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100237 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800238
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800239 DisplayConfig config;
240 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayConfig(display, &config));
Vishnu Nairde19f852018-12-18 16:11:53 -0800241
242 // After a new buffer is queued, SurfaceFlinger is notified and will
243 // latch the new buffer on next vsync. Let's heuristically wait for 3
244 // vsyncs.
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800245 mBufferPostDelay = static_cast<int32_t>(1e6 / config.refreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700246 }
247
248 void TearDown() {
249 mComposerClient->dispose();
250 }
251
252 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800253 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
254 }
255
256 void postBuffer(const sp<SurfaceControl> &layer) {
257 // wait for previous transactions (such as setSize) to complete
258 Transaction().apply(true);
259 ANativeWindow_Buffer buffer = {};
260 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
261 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
262 // Request an empty transaction to get applied synchronously to ensure the buffer is
263 // latched.
264 Transaction().apply(true);
265 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700266 }
267
268 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800269 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700270};
271
272void injectTap(int x, int y) {
273 char *buf1, *buf2;
274 asprintf(&buf1, "%d", x);
275 asprintf(&buf2, "%d", y);
276 if (fork() == 0) {
277 execlp("input", "input", "tap", buf1, buf2, NULL);
278 }
279}
280
281TEST_F(InputSurfacesTest, can_receive_input) {
282 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
283 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100284 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700285
286 injectTap(101, 101);
287
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100288 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700289}
290
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100291/**
292 * Set up two surfaces side-by-side. Tap each surface.
293 * Next, swap the positions of the two surfaces. Inject tap into the two
294 * original locations. Ensure that the tap is received by the surfaces in the
295 * reverse order.
296 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700297TEST_F(InputSurfacesTest, input_respects_positioning) {
298 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
299 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100300 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700301
302 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
303 surface2->showAt(200, 200);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100304 surface->assertFocusChange(false);
305 surface2->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700306
307 injectTap(201, 201);
308 surface2->expectTap(1, 1);
309
310 injectTap(101, 101);
311 surface->expectTap(1, 1);
312
313 surface2->doTransaction([](auto &t, auto &sc) {
314 t.setPosition(sc, 100, 100);
315 });
316 surface->doTransaction([](auto &t, auto &sc) {
317 t.setPosition(sc, 200, 200);
318 });
319
320 injectTap(101, 101);
321 surface2->expectTap(1, 1);
322
323 injectTap(201, 201);
324 surface->expectTap(1, 1);
325}
326
327TEST_F(InputSurfacesTest, input_respects_layering) {
328 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
329 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
330
331 surface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100332 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700333 surface2->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100334 surface->assertFocusChange(false);
335 surface2->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700336
337 surface->doTransaction([](auto &t, auto &sc) {
338 t.setLayer(sc, LAYER_BASE + 1);
339 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100340 surface2->assertFocusChange(false);
341 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700342
343 injectTap(11, 11);
344 surface->expectTap(1, 1);
345
346 surface2->doTransaction([](auto &t, auto &sc) {
347 t.setLayer(sc, LAYER_BASE + 1);
348 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100349 surface2->assertFocusChange(true);
350 surface->assertFocusChange(false);
Robert Carr1c4c5592018-09-24 13:18:43 -0700351
352 injectTap(11, 11);
353 surface2->expectTap(1, 1);
354
355 surface2->doTransaction([](auto &t, auto &sc) {
356 t.hide(sc);
357 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100358 surface2->assertFocusChange(false);
359 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700360
361 injectTap(11, 11);
362 surface->expectTap(1, 1);
363}
364
Vishnu Nairde19f852018-12-18 16:11:53 -0800365// Surface Insets are set to offset the client content and draw a border around the client surface
366// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
367// of the client content.
368TEST_F(InputSurfacesTest, input_respects_surface_insets) {
369 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
370 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
371 bgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100372 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800373
374 fgSurface->mInputInfo.surfaceInset = 5;
375 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100376 fgSurface->assertFocusChange(true);
377 bgSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800378
379 injectTap(106, 106);
380 fgSurface->expectTap(1, 1);
381
382 injectTap(101, 101);
383 bgSurface->expectTap(1, 1);
384}
385
386// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
387TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
388 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
389 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
390 parentSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100391 parentSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800392
393 childSurface->mInputInfo.surfaceInset = 10;
394 childSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100395 childSurface->assertFocusChange(true);
396 parentSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800397
398 childSurface->doTransaction([&](auto &t, auto &sc) {
399 t.setPosition(sc, -5, -5);
400 t.reparent(sc, parentSurface->mSurfaceControl->getHandle());
401 });
402
403 injectTap(106, 106);
404 childSurface->expectTap(1, 1);
405
406 injectTap(101, 101);
407 parentSurface->expectTap(1, 1);
408}
409
Arthur Hung118b1142019-05-08 21:25:59 +0800410// Ensure a surface whose insets are scaled, handles the touch offset correctly.
411TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
412 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
413 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
414 bgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100415 bgSurface->assertFocusChange(true);
Arthur Hung118b1142019-05-08 21:25:59 +0800416
417 fgSurface->mInputInfo.surfaceInset = 5;
418 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100419 bgSurface->assertFocusChange(false);
420 fgSurface->assertFocusChange(true);
Arthur Hung118b1142019-05-08 21:25:59 +0800421
422 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
423
424 // expect = touch / scale - inset
425 injectTap(112, 124);
426 fgSurface->expectTap(1, 1);
427
428 injectTap(101, 101);
429 bgSurface->expectTap(1, 1);
430}
431
Ady Abraham282f1d72019-07-24 18:05:56 -0700432TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
433 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
434 // In case we pass the very big inset without any checking.
435 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
436 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100437 fgSurface->assertFocusChange(true);
Ady Abraham282f1d72019-07-24 18:05:56 -0700438
439 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
440
441 // expect no crash for overflow, and inset size to be clamped to surface size
442 injectTap(202, 202);
443 fgSurface->expectTap(1, 1);
444}
445
Vishnu Nairde19f852018-12-18 16:11:53 -0800446// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
447TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
448 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
449 surface->doTransaction([](auto &t, auto &sc) {
450 Region transparentRegion(Rect(0, 0, 10, 10));
451 t.setTransparentRegionHint(sc, transparentRegion);
452 });
453 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100454 surface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800455 injectTap(101, 101);
456 surface->expectTap(1, 1);
457}
458
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700459// TODO(b/139494112) update tests once we define expected behavior
460// Ensure we still send input to the surface regardless of surface visibility changes due to the
461// first buffer being submitted or alpha changes.
462// Original bug ref: b/120839715
463TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800464 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
465 std::unique_ptr<InputSurface> bufferSurface =
466 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
467
468 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100469 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800470 bufferSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100471 bgSurface->assertFocusChange(false);
472 bufferSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800473
474 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700475 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800476
477 postBuffer(bufferSurface->mSurfaceControl);
478 injectTap(11, 11);
479 bufferSurface->expectTap(1, 1);
480}
481
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700482TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800483 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
484 std::unique_ptr<InputSurface> bufferSurface =
485 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
486 postBuffer(bufferSurface->mSurfaceControl);
487
488 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100489 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800490 bufferSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100491 bufferSurface->assertFocusChange(true);
492 bgSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800493
494 injectTap(11, 11);
495 bufferSurface->expectTap(1, 1);
496
497 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
498
499 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700500 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800501}
502
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700503TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800504 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
505 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
506
507 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100508 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800509 fgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100510 bgSurface->assertFocusChange(false);
511 fgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800512
513 injectTap(11, 11);
514 fgSurface->expectTap(1, 1);
515
516 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
517
518 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700519 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800520}
521
522TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
523 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
524 std::unique_ptr<InputSurface> containerSurface =
525 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
526
527 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100528 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800529 containerSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100530 bgSurface->assertFocusChange(false);
531 containerSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800532
533 injectTap(11, 11);
534 containerSurface->expectTap(1, 1);
535
536 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100537 containerSurface->assertFocusChange(false);
538 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800539
540 injectTap(11, 11);
541 bgSurface->expectTap(1, 1);
542}
chaviwfbe5d9c2018-12-26 12:23:37 -0800543
Arthur Hungd20b2702019-01-14 18:16:16 +0800544TEST_F(InputSurfacesTest, input_respects_outscreen) {
545 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
546 surface->showAt(-1, -1);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100547 surface->assertFocusChange(true);
Arthur Hungd20b2702019-01-14 18:16:16 +0800548
549 injectTap(0, 0);
550 surface->expectTap(1, 1);
551}
arthurhungb4a0f852020-06-16 11:02:50 +0800552
553TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
554 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
555 std::unique_ptr<InputSurface> cursorSurface =
556 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
557
558 surface->showAt(10, 10);
559 surface->assertFocusChange(true);
560 cursorSurface->showAt(10, 10);
561
562 injectTap(11, 11);
563 surface->expectTap(1, 1);
564}
Robert Carr1c4c5592018-09-24 13:18:43 -0700565}
566}