blob: 8e42c688af414ef7038c0a5ee1d269531c936e6a [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 Naircbe9c102022-02-08 02:53:27 +000027#include <android/keycodes.h>
Vishnu Nairde19f852018-12-18 16:11:53 -080028#include <android/native_window.h>
29
Robert Carr1c4c5592018-09-24 13:18:43 -070030#include <binder/Binder.h>
31#include <binder/IServiceManager.h>
32#include <binder/Parcel.h>
33#include <binder/ProcessState.h>
34
Vishnu Nairde19f852018-12-18 16:11:53 -080035#include <gui/ISurfaceComposer.h>
36#include <gui/Surface.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070037#include <gui/SurfaceComposerClient.h>
38#include <gui/SurfaceControl.h>
39
40#include <input/InputWindow.h>
41#include <input/IInputFlinger.h>
42#include <input/InputTransport.h>
43#include <input/Input.h>
44
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080045#include <ui/DisplayConfig.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070046#include <ui/Rect.h>
47#include <ui/Region.h>
48
49
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();
75 mInputFlinger->registerInputChannel(mServerChannel);
76
77 populateInputInfo(width, height);
78
79 mInputConsumer = new InputConsumer(mClientChannel);
80 }
81
Vishnu Nairde19f852018-12-18 16:11:53 -080082 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
83 int width, int height) {
84 sp<SurfaceControl> surfaceControl =
85 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
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
Vishnu Naircbe9c102022-02-08 02:53:27 +0000117 InputEvent *consumeEvent(int timeoutMs = 3000) {
118 waitForEventAvailable(timeoutMs);
Robert Carr1c4c5592018-09-24 13:18:43 -0700119
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
Vishnu Naircbe9c102022-02-08 02:53:27 +0000157 void expectKey(uint32_t keycode) {
158 InputEvent *ev = consumeEvent();
159 ASSERT_NE(ev, nullptr);
160 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, ev->getType());
161 KeyEvent *keyEvent = static_cast<KeyEvent *>(ev);
162 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
163 EXPECT_EQ(keycode, keyEvent->getKeyCode());
164 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
165
166 ev = consumeEvent();
167 ASSERT_NE(ev, nullptr);
168 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, ev->getType());
169 keyEvent = static_cast<KeyEvent *>(ev);
170 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
171 EXPECT_EQ(keycode, keyEvent->getKeyCode());
172 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
173 }
174
Robert Carr1c4c5592018-09-24 13:18:43 -0700175 ~InputSurface() {
176 mInputFlinger->unregisterInputChannel(mServerChannel);
177 }
178
179 void doTransaction(std::function<void(SurfaceComposerClient::Transaction&,
180 const sp<SurfaceControl>&)> transactionBody) {
181 SurfaceComposerClient::Transaction t;
182 transactionBody(t, mSurfaceControl);
183 t.apply(true);
184 }
185
186 void showAt(int x, int y) {
187 SurfaceComposerClient::Transaction t;
188 t.show(mSurfaceControl);
189 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
190 t.setLayer(mSurfaceControl, LAYER_BASE);
191 t.setPosition(mSurfaceControl, x, y);
192 t.setCrop_legacy(mSurfaceControl, Rect(0, 0, 100, 100));
193 t.setAlpha(mSurfaceControl, 1);
194 t.apply(true);
195 }
196
197private:
Vishnu Naircbe9c102022-02-08 02:53:27 +0000198 void waitForEventAvailable(int timeoutMs) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700199 struct pollfd fd;
200
201 fd.fd = mClientChannel->getFd();
202 fd.events = POLLIN;
Vishnu Naircbe9c102022-02-08 02:53:27 +0000203 poll(&fd, 1, timeoutMs);
Robert Carr1c4c5592018-09-24 13:18:43 -0700204 }
205
206 void populateInputInfo(int width, int height) {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700207 mInputInfo.token = mServerChannel->getConnectionToken();
Robert Carr1c4c5592018-09-24 13:18:43 -0700208 mInputInfo.name = "Test info";
209 mInputInfo.layoutParamsFlags = InputWindowInfo::FLAG_NOT_TOUCH_MODAL;
210 mInputInfo.layoutParamsType = InputWindowInfo::TYPE_BASE_APPLICATION;
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700211 mInputInfo.dispatchingTimeout = seconds_to_nanoseconds(5);
Robert Carre07e1032018-11-26 12:55:53 -0800212 mInputInfo.globalScaleFactor = 1.0;
Robert Carr1c4c5592018-09-24 13:18:43 -0700213 mInputInfo.canReceiveKeys = true;
214 mInputInfo.hasFocus = true;
215 mInputInfo.hasWallpaper = false;
216 mInputInfo.paused = false;
217
218 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
219
220 // TODO: Fill in from SF?
221 mInputInfo.ownerPid = 11111;
222 mInputInfo.ownerUid = 11111;
223 mInputInfo.inputFeatures = 0;
224 mInputInfo.displayId = 0;
Robert Carr740167f2018-10-11 19:03:41 -0700225
226 InputApplicationInfo aInfo;
227 aInfo.token = new BBinder();
228 aInfo.name = "Test app info";
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700229 aInfo.dispatchingTimeout = seconds_to_nanoseconds(5);
Robert Carr740167f2018-10-11 19:03:41 -0700230
231 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700232 }
233public:
234 sp<SurfaceControl> mSurfaceControl;
235 sp<InputChannel> mServerChannel, mClientChannel;
236 sp<IInputFlinger> mInputFlinger;
237
238 InputWindowInfo mInputInfo;
239
240 PreallocatedInputEventFactory mInputEventFactory;
241 InputConsumer* mInputConsumer;
242};
243
244class InputSurfacesTest : public ::testing::Test {
245public:
246 InputSurfacesTest() {
247 ProcessState::self()->startThreadPool();
248 }
249
250 void SetUp() {
251 mComposerClient = new SurfaceComposerClient;
252 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Vishnu Nairde19f852018-12-18 16:11:53 -0800253
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800254 const auto display = mComposerClient->getInternalDisplayToken();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100255 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800256
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800257 DisplayConfig config;
258 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayConfig(display, &config));
Vishnu Nairde19f852018-12-18 16:11:53 -0800259
260 // After a new buffer is queued, SurfaceFlinger is notified and will
261 // latch the new buffer on next vsync. Let's heuristically wait for 3
262 // vsyncs.
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800263 mBufferPostDelay = static_cast<int32_t>(1e6 / config.refreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700264 }
265
266 void TearDown() {
267 mComposerClient->dispose();
268 }
269
270 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800271 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
272 }
273
274 void postBuffer(const sp<SurfaceControl> &layer) {
275 // wait for previous transactions (such as setSize) to complete
276 Transaction().apply(true);
277 ANativeWindow_Buffer buffer = {};
278 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
279 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
280 // Request an empty transaction to get applied synchronously to ensure the buffer is
281 // latched.
282 Transaction().apply(true);
283 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700284 }
285
286 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800287 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700288};
289
290void injectTap(int x, int y) {
291 char *buf1, *buf2;
292 asprintf(&buf1, "%d", x);
293 asprintf(&buf2, "%d", y);
294 if (fork() == 0) {
295 execlp("input", "input", "tap", buf1, buf2, NULL);
296 }
297}
298
Vishnu Naircbe9c102022-02-08 02:53:27 +0000299void injectKey(uint32_t keycode) {
300 char *buf1;
301 asprintf(&buf1, "%d", keycode);
302 if (fork() == 0) {
303 execlp("input", "input", "keyevent", buf1, NULL);
304 }
305}
306
Robert Carr1c4c5592018-09-24 13:18:43 -0700307TEST_F(InputSurfacesTest, can_receive_input) {
308 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
309 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100310 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700311
312 injectTap(101, 101);
313
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100314 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700315}
316
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100317/**
318 * Set up two surfaces side-by-side. Tap each surface.
319 * Next, swap the positions of the two surfaces. Inject tap into the two
320 * original locations. Ensure that the tap is received by the surfaces in the
321 * reverse order.
322 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700323TEST_F(InputSurfacesTest, input_respects_positioning) {
324 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
325 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100326 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700327
328 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
329 surface2->showAt(200, 200);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100330 surface->assertFocusChange(false);
331 surface2->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700332
333 injectTap(201, 201);
334 surface2->expectTap(1, 1);
335
336 injectTap(101, 101);
337 surface->expectTap(1, 1);
338
339 surface2->doTransaction([](auto &t, auto &sc) {
340 t.setPosition(sc, 100, 100);
341 });
342 surface->doTransaction([](auto &t, auto &sc) {
343 t.setPosition(sc, 200, 200);
344 });
345
346 injectTap(101, 101);
347 surface2->expectTap(1, 1);
348
349 injectTap(201, 201);
350 surface->expectTap(1, 1);
351}
352
353TEST_F(InputSurfacesTest, input_respects_layering) {
354 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
355 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
356
357 surface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100358 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700359 surface2->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100360 surface->assertFocusChange(false);
361 surface2->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700362
363 surface->doTransaction([](auto &t, auto &sc) {
364 t.setLayer(sc, LAYER_BASE + 1);
365 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100366 surface2->assertFocusChange(false);
367 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700368
369 injectTap(11, 11);
370 surface->expectTap(1, 1);
371
372 surface2->doTransaction([](auto &t, auto &sc) {
373 t.setLayer(sc, LAYER_BASE + 1);
374 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100375 surface2->assertFocusChange(true);
376 surface->assertFocusChange(false);
Robert Carr1c4c5592018-09-24 13:18:43 -0700377
378 injectTap(11, 11);
379 surface2->expectTap(1, 1);
380
381 surface2->doTransaction([](auto &t, auto &sc) {
382 t.hide(sc);
383 });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100384 surface2->assertFocusChange(false);
385 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700386
387 injectTap(11, 11);
388 surface->expectTap(1, 1);
389}
390
Vishnu Nairde19f852018-12-18 16:11:53 -0800391// Surface Insets are set to offset the client content and draw a border around the client surface
392// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
393// of the client content.
394TEST_F(InputSurfacesTest, input_respects_surface_insets) {
395 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
396 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
397 bgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100398 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800399
400 fgSurface->mInputInfo.surfaceInset = 5;
401 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100402 fgSurface->assertFocusChange(true);
403 bgSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800404
405 injectTap(106, 106);
406 fgSurface->expectTap(1, 1);
407
408 injectTap(101, 101);
409 bgSurface->expectTap(1, 1);
410}
411
412// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
413TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
414 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
415 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
416 parentSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100417 parentSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800418
419 childSurface->mInputInfo.surfaceInset = 10;
420 childSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100421 childSurface->assertFocusChange(true);
422 parentSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800423
424 childSurface->doTransaction([&](auto &t, auto &sc) {
425 t.setPosition(sc, -5, -5);
426 t.reparent(sc, parentSurface->mSurfaceControl->getHandle());
427 });
428
429 injectTap(106, 106);
430 childSurface->expectTap(1, 1);
431
432 injectTap(101, 101);
433 parentSurface->expectTap(1, 1);
434}
435
Arthur Hung118b1142019-05-08 21:25:59 +0800436// Ensure a surface whose insets are scaled, handles the touch offset correctly.
437TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
438 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
439 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
440 bgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100441 bgSurface->assertFocusChange(true);
Arthur Hung118b1142019-05-08 21:25:59 +0800442
443 fgSurface->mInputInfo.surfaceInset = 5;
444 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100445 bgSurface->assertFocusChange(false);
446 fgSurface->assertFocusChange(true);
Arthur Hung118b1142019-05-08 21:25:59 +0800447
448 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
449
450 // expect = touch / scale - inset
451 injectTap(112, 124);
452 fgSurface->expectTap(1, 1);
453
454 injectTap(101, 101);
455 bgSurface->expectTap(1, 1);
456}
457
Ady Abraham282f1d72019-07-24 18:05:56 -0700458TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
459 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
460 // In case we pass the very big inset without any checking.
461 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
462 fgSurface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100463 fgSurface->assertFocusChange(true);
Ady Abraham282f1d72019-07-24 18:05:56 -0700464
465 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
466
467 // expect no crash for overflow, and inset size to be clamped to surface size
468 injectTap(202, 202);
469 fgSurface->expectTap(1, 1);
470}
471
Vishnu Nairde19f852018-12-18 16:11:53 -0800472// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
473TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
474 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
475 surface->doTransaction([](auto &t, auto &sc) {
476 Region transparentRegion(Rect(0, 0, 10, 10));
477 t.setTransparentRegionHint(sc, transparentRegion);
478 });
479 surface->showAt(100, 100);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100480 surface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800481 injectTap(101, 101);
482 surface->expectTap(1, 1);
483}
484
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700485// TODO(b/139494112) update tests once we define expected behavior
486// Ensure we still send input to the surface regardless of surface visibility changes due to the
487// first buffer being submitted or alpha changes.
488// Original bug ref: b/120839715
489TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800490 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
491 std::unique_ptr<InputSurface> bufferSurface =
492 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
493
494 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100495 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800496 bufferSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100497 bgSurface->assertFocusChange(false);
498 bufferSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800499
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 postBuffer(bufferSurface->mSurfaceControl);
504 injectTap(11, 11);
505 bufferSurface->expectTap(1, 1);
506}
507
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700508TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800509 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
510 std::unique_ptr<InputSurface> bufferSurface =
511 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
512 postBuffer(bufferSurface->mSurfaceControl);
513
514 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100515 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800516 bufferSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100517 bufferSurface->assertFocusChange(true);
518 bgSurface->assertFocusChange(false);
Vishnu Nairde19f852018-12-18 16:11:53 -0800519
520 injectTap(11, 11);
521 bufferSurface->expectTap(1, 1);
522
523 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
524
525 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700526 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800527}
528
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700529TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800530 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
531 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
532
533 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100534 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800535 fgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100536 bgSurface->assertFocusChange(false);
537 fgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800538
539 injectTap(11, 11);
540 fgSurface->expectTap(1, 1);
541
542 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
543
544 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700545 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800546}
547
548TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
549 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
550 std::unique_ptr<InputSurface> containerSurface =
551 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
552
553 bgSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100554 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800555 containerSurface->showAt(10, 10);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100556 bgSurface->assertFocusChange(false);
557 containerSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800558
559 injectTap(11, 11);
560 containerSurface->expectTap(1, 1);
561
562 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100563 containerSurface->assertFocusChange(false);
564 bgSurface->assertFocusChange(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800565
566 injectTap(11, 11);
567 bgSurface->expectTap(1, 1);
568}
chaviwfbe5d9c2018-12-26 12:23:37 -0800569
Arthur Hungd20b2702019-01-14 18:16:16 +0800570TEST_F(InputSurfacesTest, input_respects_outscreen) {
571 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
572 surface->showAt(-1, -1);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100573 surface->assertFocusChange(true);
Arthur Hungd20b2702019-01-14 18:16:16 +0800574
575 injectTap(0, 0);
576 surface->expectTap(1, 1);
577}
arthurhungb4a0f852020-06-16 11:02:50 +0800578
579TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
580 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
581 std::unique_ptr<InputSurface> cursorSurface =
582 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
583
584 surface->showAt(10, 10);
585 surface->assertFocusChange(true);
586 cursorSurface->showAt(10, 10);
587
588 injectTap(11, 11);
589 surface->expectTap(1, 1);
590}
Vishnu Naircbe9c102022-02-08 02:53:27 +0000591
592TEST_F(InputSurfacesTest, drop_input_policy) {
593 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
594 surface->doTransaction(
595 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
596 surface->showAt(100, 100);
597 surface->assertFocusChange(true);
598
599 injectTap(101, 101);
600 EXPECT_EQ(surface->consumeEvent(100), nullptr);
601
602 injectKey(AKEYCODE_V);
603 EXPECT_EQ(surface->consumeEvent(100), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700604}
605}
Vishnu Naircbe9c102022-02-08 02:53:27 +0000606} // namespace android