blob: d65a40baf6fc890bcf6e5942986fc4e7c7d1a62a [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
Marin Shalamanova7fe3042021-01-29 21:02:08 +010044#include <ui/DisplayMode.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
Vishnu Nair958da932020-08-21 17:12:37 -070050namespace android::test {
Robert Carr1c4c5592018-09-24 13:18:43 -070051
Vishnu Nairde19f852018-12-18 16:11:53 -080052using Transaction = SurfaceComposerClient::Transaction;
53
Robert Carr1c4c5592018-09-24 13:18:43 -070054sp<IInputFlinger> getInputFlinger() {
55 sp<IBinder> input(defaultServiceManager()->getService(
56 String16("inputflinger")));
57 if (input == nullptr) {
58 ALOGE("Failed to link to input service");
59 } else { ALOGE("Linked to input"); }
60 return interface_cast<IInputFlinger>(input);
61}
62
63// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
64static const int LAYER_BASE = INT32_MAX - 10;
Chris Ye6c4243b2020-07-22 12:07:12 -070065static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070066
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 mInputFlinger = getInputFlinger();
Garfield Tan15601662020-09-22 15:32:38 -070073 mClientChannel = std::make_shared<InputChannel>();
74 mInputFlinger->createInputChannel("testchannels", mClientChannel.get());
Robert Carr1c4c5592018-09-24 13:18:43 -070075
76 populateInputInfo(width, height);
77
78 mInputConsumer = new InputConsumer(mClientChannel);
79 }
80
Vishnu Nairde19f852018-12-18 16:11:53 -080081 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
82 int width, int height) {
83 sp<SurfaceControl> surfaceControl =
84 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -080085 PIXEL_FORMAT_RGBA_8888,
86 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -080087 return std::make_unique<InputSurface>(surfaceControl, width, height);
88 }
89
90 static std::unique_ptr<InputSurface> makeBufferInputSurface(
91 const sp<SurfaceComposerClient> &scc, int width, int height) {
92 sp<SurfaceControl> surfaceControl =
93 scc->createSurface(String8("Test Buffer Surface"), width, height,
94 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
95 return std::make_unique<InputSurface>(surfaceControl, width, height);
96 }
97
chaviw7e72caf2020-12-02 16:50:43 -080098 static std::unique_ptr<InputSurface> makeBlastInputSurface(const sp<SurfaceComposerClient> &scc,
99 int width, int height) {
100 sp<SurfaceControl> surfaceControl =
101 scc->createSurface(String8("Test Buffer Surface"), width, height,
102 PIXEL_FORMAT_RGBA_8888,
103 ISurfaceComposerClient::eFXSurfaceBufferState);
104 return std::make_unique<InputSurface>(surfaceControl, width, height);
105 }
106
Vishnu Nairde19f852018-12-18 16:11:53 -0800107 static std::unique_ptr<InputSurface> makeContainerInputSurface(
108 const sp<SurfaceComposerClient> &scc, int width, int height) {
109 sp<SurfaceControl> surfaceControl =
110 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
111 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
112 ISurfaceComposerClient::eFXSurfaceContainer);
113 return std::make_unique<InputSurface>(surfaceControl, width, height);
114 }
115
arthurhungb4a0f852020-06-16 11:02:50 +0800116 static std::unique_ptr<InputSurface> makeCursorInputSurface(
117 const sp<SurfaceComposerClient> &scc, int width, int height) {
118 sp<SurfaceControl> surfaceControl =
119 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
120 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
121 ISurfaceComposerClient::eCursorWindow);
122 return std::make_unique<InputSurface>(surfaceControl, width, height);
123 }
124
Robert Carr1c4c5592018-09-24 13:18:43 -0700125 InputEvent* consumeEvent() {
126 waitForEventAvailable();
127
128 InputEvent *ev;
129 uint32_t seqId;
130 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
131 if (consumed != OK) {
132 return nullptr;
133 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100134 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
135 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700136 return ev;
137 }
138
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100139 void assertFocusChange(bool hasFocus) {
140 InputEvent *ev = consumeEvent();
141 ASSERT_NE(ev, nullptr);
142 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, ev->getType());
143 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
144 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
145 }
146
Robert Carr1c4c5592018-09-24 13:18:43 -0700147 void expectTap(int x, int y) {
148 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100149 ASSERT_NE(ev, nullptr);
150 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700151 MotionEvent* mev = static_cast<MotionEvent*>(ev);
152 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
153 EXPECT_EQ(x, mev->getX(0));
154 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800155 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700156
157 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100158 ASSERT_NE(ev, nullptr);
159 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700160 mev = static_cast<MotionEvent*>(ev);
161 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800162 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700163 }
164
chaviw39cfa2e2020-11-04 14:19:02 -0800165 void expectTapWithFlag(int x, int y, int32_t flags) {
166 InputEvent *ev = consumeEvent();
167 ASSERT_NE(ev, nullptr);
168 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
169 MotionEvent *mev = static_cast<MotionEvent *>(ev);
170 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
171 EXPECT_EQ(x, mev->getX(0));
172 EXPECT_EQ(y, mev->getY(0));
173 EXPECT_EQ(flags, mev->getFlags() & flags);
174
175 ev = consumeEvent();
176 ASSERT_NE(ev, nullptr);
177 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
178 mev = static_cast<MotionEvent *>(ev);
179 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
180 EXPECT_EQ(flags, mev->getFlags() & flags);
181 }
182
Garfield Tan15601662020-09-22 15:32:38 -0700183 ~InputSurface() { mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken()); }
Robert Carr1c4c5592018-09-24 13:18:43 -0700184
185 void doTransaction(std::function<void(SurfaceComposerClient::Transaction&,
186 const sp<SurfaceControl>&)> transactionBody) {
187 SurfaceComposerClient::Transaction t;
188 transactionBody(t, mSurfaceControl);
189 t.apply(true);
190 }
191
chaviw7e72caf2020-12-02 16:50:43 -0800192 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700193 SurfaceComposerClient::Transaction t;
194 t.show(mSurfaceControl);
195 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
196 t.setLayer(mSurfaceControl, LAYER_BASE);
197 t.setPosition(mSurfaceControl, x, y);
chaviw7e72caf2020-12-02 16:50:43 -0800198 t.setCrop_legacy(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700199 t.setAlpha(mSurfaceControl, 1);
200 t.apply(true);
201 }
202
Vishnu Nair958da932020-08-21 17:12:37 -0700203 void requestFocus() {
204 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800205 FocusRequest request;
206 request.token = mInputInfo.token;
207 request.windowName = mInputInfo.name;
208 request.focusedToken = nullptr;
209 request.focusedWindowName = "";
210 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
211 request.displayId = 0;
212 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700213 t.apply(true);
214 }
215
Robert Carr1c4c5592018-09-24 13:18:43 -0700216private:
217 void waitForEventAvailable() {
218 struct pollfd fd;
219
220 fd.fd = mClientChannel->getFd();
221 fd.events = POLLIN;
222 poll(&fd, 1, 3000);
223 }
224
225 void populateInputInfo(int width, int height) {
Garfield Tan15601662020-09-22 15:32:38 -0700226 mInputInfo.token = mClientChannel->getConnectionToken();
Robert Carr1c4c5592018-09-24 13:18:43 -0700227 mInputInfo.name = "Test info";
Michael Wright44753b12020-07-08 13:48:11 +0100228 mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
229 mInputInfo.type = InputWindowInfo::Type::BASE_APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500230 mInputInfo.dispatchingTimeout = 5s;
Robert Carre07e1032018-11-26 12:55:53 -0800231 mInputInfo.globalScaleFactor = 1.0;
Vishnu Nair47074b82020-08-14 11:54:47 -0700232 mInputInfo.focusable = true;
Robert Carr1c4c5592018-09-24 13:18:43 -0700233 mInputInfo.hasWallpaper = false;
234 mInputInfo.paused = false;
235
236 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
237
238 // TODO: Fill in from SF?
239 mInputInfo.ownerPid = 11111;
240 mInputInfo.ownerUid = 11111;
Robert Carr1c4c5592018-09-24 13:18:43 -0700241 mInputInfo.displayId = 0;
Robert Carr740167f2018-10-11 19:03:41 -0700242
243 InputApplicationInfo aInfo;
244 aInfo.token = new BBinder();
245 aInfo.name = "Test app info";
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500246 aInfo.dispatchingTimeoutMillis =
247 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Robert Carr740167f2018-10-11 19:03:41 -0700248
249 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700250 }
251public:
252 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500253 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700254 sp<IInputFlinger> mInputFlinger;
255
256 InputWindowInfo mInputInfo;
257
258 PreallocatedInputEventFactory mInputEventFactory;
259 InputConsumer* mInputConsumer;
260};
261
262class InputSurfacesTest : public ::testing::Test {
263public:
264 InputSurfacesTest() {
265 ProcessState::self()->startThreadPool();
266 }
267
268 void SetUp() {
269 mComposerClient = new SurfaceComposerClient;
270 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Vishnu Nairde19f852018-12-18 16:11:53 -0800271
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800272 const auto display = mComposerClient->getInternalDisplayToken();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100273 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800274
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100275 ui::DisplayMode mode;
276 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800277
278 // After a new buffer is queued, SurfaceFlinger is notified and will
279 // latch the new buffer on next vsync. Let's heuristically wait for 3
280 // vsyncs.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100281 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.refreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700282 }
283
284 void TearDown() {
285 mComposerClient->dispose();
286 }
287
288 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800289 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
290 }
291
292 void postBuffer(const sp<SurfaceControl> &layer) {
293 // wait for previous transactions (such as setSize) to complete
294 Transaction().apply(true);
295 ANativeWindow_Buffer buffer = {};
296 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
297 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
298 // Request an empty transaction to get applied synchronously to ensure the buffer is
299 // latched.
300 Transaction().apply(true);
301 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700302 }
303
304 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800305 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700306};
307
308void injectTap(int x, int y) {
309 char *buf1, *buf2;
310 asprintf(&buf1, "%d", x);
311 asprintf(&buf2, "%d", y);
312 if (fork() == 0) {
313 execlp("input", "input", "tap", buf1, buf2, NULL);
314 }
315}
316
317TEST_F(InputSurfacesTest, can_receive_input) {
318 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
319 surface->showAt(100, 100);
320
321 injectTap(101, 101);
322
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100323 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700324}
325
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100326/**
327 * Set up two surfaces side-by-side. Tap each surface.
328 * Next, swap the positions of the two surfaces. Inject tap into the two
329 * original locations. Ensure that the tap is received by the surfaces in the
330 * reverse order.
331 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700332TEST_F(InputSurfacesTest, input_respects_positioning) {
333 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
334 surface->showAt(100, 100);
335
336 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
337 surface2->showAt(200, 200);
338
339 injectTap(201, 201);
340 surface2->expectTap(1, 1);
341
342 injectTap(101, 101);
343 surface->expectTap(1, 1);
344
345 surface2->doTransaction([](auto &t, auto &sc) {
346 t.setPosition(sc, 100, 100);
347 });
348 surface->doTransaction([](auto &t, auto &sc) {
349 t.setPosition(sc, 200, 200);
350 });
351
352 injectTap(101, 101);
353 surface2->expectTap(1, 1);
354
355 injectTap(201, 201);
356 surface->expectTap(1, 1);
357}
358
359TEST_F(InputSurfacesTest, input_respects_layering) {
360 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
361 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
362
363 surface->showAt(10, 10);
364 surface2->showAt(10, 10);
365
366 surface->doTransaction([](auto &t, auto &sc) {
367 t.setLayer(sc, LAYER_BASE + 1);
368 });
369
370 injectTap(11, 11);
371 surface->expectTap(1, 1);
372
373 surface2->doTransaction([](auto &t, auto &sc) {
374 t.setLayer(sc, LAYER_BASE + 1);
375 });
376
377 injectTap(11, 11);
378 surface2->expectTap(1, 1);
379
380 surface2->doTransaction([](auto &t, auto &sc) {
381 t.hide(sc);
382 });
383
384 injectTap(11, 11);
385 surface->expectTap(1, 1);
386}
387
Vishnu Nairde19f852018-12-18 16:11:53 -0800388// Surface Insets are set to offset the client content and draw a border around the client surface
389// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
390// of the client content.
391TEST_F(InputSurfacesTest, input_respects_surface_insets) {
392 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
393 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
394 bgSurface->showAt(100, 100);
395
396 fgSurface->mInputInfo.surfaceInset = 5;
397 fgSurface->showAt(100, 100);
398
399 injectTap(106, 106);
400 fgSurface->expectTap(1, 1);
401
402 injectTap(101, 101);
403 bgSurface->expectTap(1, 1);
404}
405
406// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
407TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
408 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
409 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
410 parentSurface->showAt(100, 100);
411
412 childSurface->mInputInfo.surfaceInset = 10;
413 childSurface->showAt(100, 100);
414
415 childSurface->doTransaction([&](auto &t, auto &sc) {
416 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000417 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800418 });
419
420 injectTap(106, 106);
421 childSurface->expectTap(1, 1);
422
423 injectTap(101, 101);
424 parentSurface->expectTap(1, 1);
425}
426
Arthur Hung118b1142019-05-08 21:25:59 +0800427// Ensure a surface whose insets are scaled, handles the touch offset correctly.
428TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
429 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
430 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
431 bgSurface->showAt(100, 100);
432
433 fgSurface->mInputInfo.surfaceInset = 5;
434 fgSurface->showAt(100, 100);
435
436 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
437
438 // expect = touch / scale - inset
439 injectTap(112, 124);
440 fgSurface->expectTap(1, 1);
441
442 injectTap(101, 101);
443 bgSurface->expectTap(1, 1);
444}
445
Ady Abraham282f1d72019-07-24 18:05:56 -0700446TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
447 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
448 // In case we pass the very big inset without any checking.
449 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
450 fgSurface->showAt(100, 100);
451
452 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
453
454 // expect no crash for overflow, and inset size to be clamped to surface size
455 injectTap(202, 202);
456 fgSurface->expectTap(1, 1);
457}
458
Vishnu Nairde19f852018-12-18 16:11:53 -0800459// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
460TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
461 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
462 surface->doTransaction([](auto &t, auto &sc) {
463 Region transparentRegion(Rect(0, 0, 10, 10));
464 t.setTransparentRegionHint(sc, transparentRegion);
465 });
466 surface->showAt(100, 100);
467 injectTap(101, 101);
468 surface->expectTap(1, 1);
469}
470
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700471// TODO(b/139494112) update tests once we define expected behavior
472// Ensure we still send input to the surface regardless of surface visibility changes due to the
473// first buffer being submitted or alpha changes.
474// Original bug ref: b/120839715
475TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800476 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
477 std::unique_ptr<InputSurface> bufferSurface =
478 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
479
480 bgSurface->showAt(10, 10);
481 bufferSurface->showAt(10, 10);
482
483 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700484 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800485
486 postBuffer(bufferSurface->mSurfaceControl);
487 injectTap(11, 11);
488 bufferSurface->expectTap(1, 1);
489}
490
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700491TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800492 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
493 std::unique_ptr<InputSurface> bufferSurface =
494 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
495 postBuffer(bufferSurface->mSurfaceControl);
496
497 bgSurface->showAt(10, 10);
498 bufferSurface->showAt(10, 10);
499
500 injectTap(11, 11);
501 bufferSurface->expectTap(1, 1);
502
503 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
504
505 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700506 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800507}
508
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700509TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800510 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
511 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
512
513 bgSurface->showAt(10, 10);
514 fgSurface->showAt(10, 10);
515
516 injectTap(11, 11);
517 fgSurface->expectTap(1, 1);
518
519 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
520
521 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700522 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800523}
524
525TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
526 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
527 std::unique_ptr<InputSurface> containerSurface =
528 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
529
530 bgSurface->showAt(10, 10);
531 containerSurface->showAt(10, 10);
532
533 injectTap(11, 11);
534 containerSurface->expectTap(1, 1);
535
536 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
537
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);
545
546 injectTap(0, 0);
547 surface->expectTap(1, 1);
548}
arthurhungb4a0f852020-06-16 11:02:50 +0800549
550TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
551 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
552 std::unique_ptr<InputSurface> cursorSurface =
553 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
554
555 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800556 cursorSurface->showAt(10, 10);
557
558 injectTap(11, 11);
559 surface->expectTap(1, 1);
560}
Vishnu Nair958da932020-08-21 17:12:37 -0700561
562TEST_F(InputSurfacesTest, can_be_focused) {
563 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
564 surface->showAt(100, 100);
565 surface->requestFocus();
566
567 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700568}
chaviw44a6d2b2020-09-08 17:14:16 -0700569
570TEST_F(InputSurfacesTest, rotate_surface) {
571 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
572 surface->showAt(10, 10);
573 surface->doTransaction([](auto &t, auto &sc) {
574 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
575 });
576 injectTap(8, 11);
577 surface->expectTap(1, 2);
578
579 surface->doTransaction([](auto &t, auto &sc) {
580 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
581 });
582 injectTap(9, 8);
583 surface->expectTap(1, 2);
584
585 surface->doTransaction([](auto &t, auto &sc) {
586 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
587 });
588 injectTap(12, 9);
589 surface->expectTap(1, 2);
590}
591
592TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
593 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
594 surface->showAt(10, 10);
595 surface->doTransaction([](auto &t, auto &sc) {
596 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
597 });
598 injectTap(2, 12);
599 surface->expectTap(1, 2);
600
601 surface->doTransaction([](auto &t, auto &sc) {
602 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
603 });
604 injectTap(8, 2);
605 surface->expectTap(1, 2);
606
607 surface->doTransaction([](auto &t, auto &sc) {
608 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
609 });
610 injectTap(18, 8);
611 surface->expectTap(1, 2);
612}
613
614TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
615 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
616 surface->mInputInfo.surfaceInset = 5;
617 surface->showAt(100, 100);
618
619 surface->doTransaction([](auto &t, auto &sc) {
620 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
621 });
622 injectTap(40, 120);
623 surface->expectTap(5, 10);
624
625 surface->doTransaction([](auto &t, auto &sc) {
626 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
627 });
628 injectTap(80, 40);
629 surface->expectTap(5, 10);
630
631 surface->doTransaction([](auto &t, auto &sc) {
632 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
633 });
634 injectTap(160, 80);
635 surface->expectTap(5, 10);
636}
637
chaviw39cfa2e2020-11-04 14:19:02 -0800638TEST_F(InputSurfacesTest, touch_flag_obscured) {
639 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
640 surface->showAt(100, 100);
641
642 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
643 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
644 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
645 nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
646 nonTouchableSurface->mInputInfo.ownerUid = 22222;
Bernardo Rufino602ef712020-12-21 11:02:18 +0000647 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
648 // the default obscured/untrusted touch filter introduced in S.
649 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800650 nonTouchableSurface->showAt(100, 100);
651
652 injectTap(190, 199);
653 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
654}
655
656TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
657 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
658 surface->showAt(100, 100);
659
660 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
661 // that will be tapped. Window behind gets touch, but with flag
662 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
663 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
664 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
665 nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
666 parentSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
667 nonTouchableSurface->mInputInfo.ownerUid = 22222;
668 parentSurface->mInputInfo.ownerUid = 22222;
669 nonTouchableSurface->showAt(0, 0);
670 parentSurface->showAt(100, 100);
671
672 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
673 t.setCrop_legacy(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
674 t.reparent(sc, parentSurface->mSurfaceControl);
675 });
676
677 injectTap(190, 199);
678 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
679}
680
681TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
682 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
683 surface->showAt(100, 100);
684
685 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
686 // the touchable window. Window behind gets touch with no obscured flags.
687 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
688 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
689 nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
690 parentSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
691 nonTouchableSurface->mInputInfo.ownerUid = 22222;
692 parentSurface->mInputInfo.ownerUid = 22222;
693 nonTouchableSurface->showAt(0, 0);
694 parentSurface->showAt(50, 50);
695
696 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
697 t.setCrop_legacy(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
698 t.reparent(sc, parentSurface->mSurfaceControl);
699 });
700
701 injectTap(101, 110);
702 surface->expectTap(1, 10);
703}
704
chaviw7e72caf2020-12-02 16:50:43 -0800705TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
706 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
707
708 std::unique_ptr<InputSurface> bufferSurface =
709 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
710 bufferSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
711 bufferSurface->mInputInfo.ownerUid = 22222;
712
713 surface->showAt(10, 10);
714 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
715
716 injectTap(11, 11);
717 surface->expectTap(1, 1);
718}
719
720TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
721 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
722
723 std::unique_ptr<InputSurface> bufferSurface =
724 InputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
725 bufferSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
726 bufferSurface->mInputInfo.ownerUid = 22222;
727
728 surface->showAt(10, 10);
729 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
730
731 injectTap(11, 11);
732 surface->expectTap(1, 1);
733}
734
Vishnu Nair958da932020-08-21 17:12:37 -0700735} // namespace android::test