blob: fc84c1b619ac825ff3453ee30728fa181893c9a9 [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>
chaviw98318de2021-05-19 16:45:23 -050040#include <gui/WindowInfo.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070041#include <input/Input.h>
Chris Ye0783e992020-06-02 21:34:49 -070042#include <input/InputTransport.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
chaviw39d01472021-04-08 14:26:24 -050050using android::hardware::graphics::common::V1_1::BufferUsage;
51
chaviw98318de2021-05-19 16:45:23 -050052using android::gui::FocusRequest;
53using android::gui::InputApplicationInfo;
54using android::gui::TouchOcclusionMode;
55using android::gui::WindowInfo;
56
Vishnu Nair958da932020-08-21 17:12:37 -070057namespace android::test {
Robert Carr1c4c5592018-09-24 13:18:43 -070058
Vishnu Nairde19f852018-12-18 16:11:53 -080059using Transaction = SurfaceComposerClient::Transaction;
60
Robert Carr1c4c5592018-09-24 13:18:43 -070061sp<IInputFlinger> getInputFlinger() {
62 sp<IBinder> input(defaultServiceManager()->getService(
63 String16("inputflinger")));
64 if (input == nullptr) {
65 ALOGE("Failed to link to input service");
66 } else { ALOGE("Linked to input"); }
67 return interface_cast<IInputFlinger>(input);
68}
69
70// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
71static const int LAYER_BASE = INT32_MAX - 10;
Chris Ye6c4243b2020-07-22 12:07:12 -070072static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070073
74class InputSurface {
75public:
Vishnu Nairde19f852018-12-18 16:11:53 -080076 InputSurface(const sp<SurfaceControl> &sc, int width, int height) {
77 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -070078
79 mInputFlinger = getInputFlinger();
Garfield Tan15601662020-09-22 15:32:38 -070080 mClientChannel = std::make_shared<InputChannel>();
81 mInputFlinger->createInputChannel("testchannels", mClientChannel.get());
Robert Carr1c4c5592018-09-24 13:18:43 -070082
83 populateInputInfo(width, height);
84
85 mInputConsumer = new InputConsumer(mClientChannel);
86 }
87
Vishnu Nairde19f852018-12-18 16:11:53 -080088 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
89 int width, int height) {
90 sp<SurfaceControl> surfaceControl =
91 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -080092 PIXEL_FORMAT_RGBA_8888,
93 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -080094 return std::make_unique<InputSurface>(surfaceControl, width, height);
95 }
96
97 static std::unique_ptr<InputSurface> makeBufferInputSurface(
98 const sp<SurfaceComposerClient> &scc, int width, int height) {
99 sp<SurfaceControl> surfaceControl =
100 scc->createSurface(String8("Test Buffer Surface"), width, height,
101 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
102 return std::make_unique<InputSurface>(surfaceControl, width, height);
103 }
104
105 static std::unique_ptr<InputSurface> makeContainerInputSurface(
106 const sp<SurfaceComposerClient> &scc, int width, int height) {
107 sp<SurfaceControl> surfaceControl =
108 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
109 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
110 ISurfaceComposerClient::eFXSurfaceContainer);
111 return std::make_unique<InputSurface>(surfaceControl, width, height);
112 }
113
arthurhungb4a0f852020-06-16 11:02:50 +0800114 static std::unique_ptr<InputSurface> makeCursorInputSurface(
115 const sp<SurfaceComposerClient> &scc, int width, int height) {
116 sp<SurfaceControl> surfaceControl =
117 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
118 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
119 ISurfaceComposerClient::eCursorWindow);
120 return std::make_unique<InputSurface>(surfaceControl, width, height);
121 }
122
Robert Carr1c4c5592018-09-24 13:18:43 -0700123 InputEvent* consumeEvent() {
124 waitForEventAvailable();
125
126 InputEvent *ev;
127 uint32_t seqId;
128 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
129 if (consumed != OK) {
130 return nullptr;
131 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100132 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
133 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700134 return ev;
135 }
136
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100137 void assertFocusChange(bool hasFocus) {
138 InputEvent *ev = consumeEvent();
139 ASSERT_NE(ev, nullptr);
140 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, ev->getType());
141 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
142 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
143 }
144
Robert Carr1c4c5592018-09-24 13:18:43 -0700145 void expectTap(int x, int y) {
146 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100147 ASSERT_NE(ev, nullptr);
148 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700149 MotionEvent* mev = static_cast<MotionEvent*>(ev);
150 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
151 EXPECT_EQ(x, mev->getX(0));
152 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800153 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700154
155 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100156 ASSERT_NE(ev, nullptr);
157 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700158 mev = static_cast<MotionEvent*>(ev);
159 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800160 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700161 }
162
chaviw39cfa2e2020-11-04 14:19:02 -0800163 void expectTapWithFlag(int x, int y, int32_t flags) {
164 InputEvent *ev = consumeEvent();
165 ASSERT_NE(ev, nullptr);
166 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
167 MotionEvent *mev = static_cast<MotionEvent *>(ev);
168 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
169 EXPECT_EQ(x, mev->getX(0));
170 EXPECT_EQ(y, mev->getY(0));
171 EXPECT_EQ(flags, mev->getFlags() & flags);
172
173 ev = consumeEvent();
174 ASSERT_NE(ev, nullptr);
175 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
176 mev = static_cast<MotionEvent *>(ev);
177 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
178 EXPECT_EQ(flags, mev->getFlags() & flags);
179 }
180
chaviw39d01472021-04-08 14:26:24 -0500181 virtual ~InputSurface() {
182 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
183 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700184
chaviw39d01472021-04-08 14:26:24 -0500185 virtual void doTransaction(
186 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
187 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700188 SurfaceComposerClient::Transaction t;
189 transactionBody(t, mSurfaceControl);
190 t.apply(true);
191 }
192
chaviw39d01472021-04-08 14:26:24 -0500193 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700194 SurfaceComposerClient::Transaction t;
195 t.show(mSurfaceControl);
196 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
197 t.setLayer(mSurfaceControl, LAYER_BASE);
198 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800199 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700200 t.setAlpha(mSurfaceControl, 1);
201 t.apply(true);
202 }
203
Vishnu Nair958da932020-08-21 17:12:37 -0700204 void requestFocus() {
205 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800206 FocusRequest request;
207 request.token = mInputInfo.token;
208 request.windowName = mInputInfo.name;
209 request.focusedToken = nullptr;
210 request.focusedWindowName = "";
211 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
212 request.displayId = 0;
213 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700214 t.apply(true);
215 }
216
Robert Carr1c4c5592018-09-24 13:18:43 -0700217private:
218 void waitForEventAvailable() {
219 struct pollfd fd;
220
221 fd.fd = mClientChannel->getFd();
222 fd.events = POLLIN;
223 poll(&fd, 1, 3000);
224 }
225
226 void populateInputInfo(int width, int height) {
Garfield Tan15601662020-09-22 15:32:38 -0700227 mInputInfo.token = mClientChannel->getConnectionToken();
Robert Carr1c4c5592018-09-24 13:18:43 -0700228 mInputInfo.name = "Test info";
chaviw98318de2021-05-19 16:45:23 -0500229 mInputInfo.flags = WindowInfo::Flag::NOT_TOUCH_MODAL;
230 mInputInfo.type = WindowInfo::Type::BASE_APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500231 mInputInfo.dispatchingTimeout = 5s;
Robert Carre07e1032018-11-26 12:55:53 -0800232 mInputInfo.globalScaleFactor = 1.0;
Vishnu Nair47074b82020-08-14 11:54:47 -0700233 mInputInfo.focusable = true;
Robert Carr1c4c5592018-09-24 13:18:43 -0700234 mInputInfo.hasWallpaper = false;
235 mInputInfo.paused = false;
236
237 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
238
239 // TODO: Fill in from SF?
240 mInputInfo.ownerPid = 11111;
241 mInputInfo.ownerUid = 11111;
Robert Carr1c4c5592018-09-24 13:18:43 -0700242 mInputInfo.displayId = 0;
Robert Carr740167f2018-10-11 19:03:41 -0700243
244 InputApplicationInfo aInfo;
245 aInfo.token = new BBinder();
246 aInfo.name = "Test app info";
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500247 aInfo.dispatchingTimeoutMillis =
248 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Robert Carr740167f2018-10-11 19:03:41 -0700249
250 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700251 }
252public:
253 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500254 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700255 sp<IInputFlinger> mInputFlinger;
256
chaviw98318de2021-05-19 16:45:23 -0500257 WindowInfo mInputInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700258
259 PreallocatedInputEventFactory mInputEventFactory;
260 InputConsumer* mInputConsumer;
261};
262
chaviw39d01472021-04-08 14:26:24 -0500263class BlastInputSurface : public InputSurface {
264public:
265 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
266 int height)
267 : InputSurface(sc, width, height) {
268 mParentSurfaceControl = parentSc;
269 }
270
271 ~BlastInputSurface() = default;
272
273 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
274 const sp<SurfaceComposerClient> &scc, int width, int height) {
275 sp<SurfaceControl> parentSc =
276 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
277 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
278 ISurfaceComposerClient::eFXSurfaceContainer);
279
280 sp<SurfaceControl> surfaceControl =
281 scc->createSurface(String8("Test Buffer Surface"), width, height,
282 PIXEL_FORMAT_RGBA_8888,
283 ISurfaceComposerClient::eFXSurfaceBufferState,
284 parentSc->getHandle());
285 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
286 }
287
288 void doTransaction(
289 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
290 transactionBody) override {
291 SurfaceComposerClient::Transaction t;
292 transactionBody(t, mParentSurfaceControl);
293 t.apply(true);
294 }
295
296 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
297 SurfaceComposerClient::Transaction t;
298 t.show(mParentSurfaceControl);
299 t.setLayer(mParentSurfaceControl, LAYER_BASE);
300 t.setPosition(mParentSurfaceControl, x, y);
301 t.setCrop(mParentSurfaceControl, crop);
302
303 t.show(mSurfaceControl);
304 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
305 t.setCrop(mSurfaceControl, crop);
306 t.setAlpha(mSurfaceControl, 1);
307 t.apply(true);
308 }
309
310private:
311 sp<SurfaceControl> mParentSurfaceControl;
312};
313
Robert Carr1c4c5592018-09-24 13:18:43 -0700314class InputSurfacesTest : public ::testing::Test {
315public:
316 InputSurfacesTest() {
317 ProcessState::self()->startThreadPool();
318 }
319
320 void SetUp() {
321 mComposerClient = new SurfaceComposerClient;
322 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Vishnu Nairde19f852018-12-18 16:11:53 -0800323
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800324 const auto display = mComposerClient->getInternalDisplayToken();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100325 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800326
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100327 ui::DisplayMode mode;
328 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800329
330 // After a new buffer is queued, SurfaceFlinger is notified and will
331 // latch the new buffer on next vsync. Let's heuristically wait for 3
332 // vsyncs.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100333 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.refreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700334 }
335
336 void TearDown() {
337 mComposerClient->dispose();
338 }
339
340 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800341 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
342 }
343
chaviw39d01472021-04-08 14:26:24 -0500344 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
345 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
346 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
347 sp<GraphicBuffer> buffer =
348 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
349 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800350 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700351 }
352
353 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800354 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700355};
356
357void injectTap(int x, int y) {
358 char *buf1, *buf2;
359 asprintf(&buf1, "%d", x);
360 asprintf(&buf2, "%d", y);
361 if (fork() == 0) {
362 execlp("input", "input", "tap", buf1, buf2, NULL);
363 }
364}
365
366TEST_F(InputSurfacesTest, can_receive_input) {
367 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
368 surface->showAt(100, 100);
369
370 injectTap(101, 101);
371
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100372 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700373}
374
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100375/**
376 * Set up two surfaces side-by-side. Tap each surface.
377 * Next, swap the positions of the two surfaces. Inject tap into the two
378 * original locations. Ensure that the tap is received by the surfaces in the
379 * reverse order.
380 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700381TEST_F(InputSurfacesTest, input_respects_positioning) {
382 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
383 surface->showAt(100, 100);
384
385 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
386 surface2->showAt(200, 200);
387
388 injectTap(201, 201);
389 surface2->expectTap(1, 1);
390
391 injectTap(101, 101);
392 surface->expectTap(1, 1);
393
394 surface2->doTransaction([](auto &t, auto &sc) {
395 t.setPosition(sc, 100, 100);
396 });
397 surface->doTransaction([](auto &t, auto &sc) {
398 t.setPosition(sc, 200, 200);
399 });
400
401 injectTap(101, 101);
402 surface2->expectTap(1, 1);
403
404 injectTap(201, 201);
405 surface->expectTap(1, 1);
406}
407
408TEST_F(InputSurfacesTest, input_respects_layering) {
409 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
410 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
411
412 surface->showAt(10, 10);
413 surface2->showAt(10, 10);
414
415 surface->doTransaction([](auto &t, auto &sc) {
416 t.setLayer(sc, LAYER_BASE + 1);
417 });
418
419 injectTap(11, 11);
420 surface->expectTap(1, 1);
421
422 surface2->doTransaction([](auto &t, auto &sc) {
423 t.setLayer(sc, LAYER_BASE + 1);
424 });
425
426 injectTap(11, 11);
427 surface2->expectTap(1, 1);
428
429 surface2->doTransaction([](auto &t, auto &sc) {
430 t.hide(sc);
431 });
432
433 injectTap(11, 11);
434 surface->expectTap(1, 1);
435}
436
Vishnu Nairde19f852018-12-18 16:11:53 -0800437// Surface Insets are set to offset the client content and draw a border around the client surface
438// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
439// of the client content.
440TEST_F(InputSurfacesTest, input_respects_surface_insets) {
441 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
442 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
443 bgSurface->showAt(100, 100);
444
445 fgSurface->mInputInfo.surfaceInset = 5;
446 fgSurface->showAt(100, 100);
447
448 injectTap(106, 106);
449 fgSurface->expectTap(1, 1);
450
451 injectTap(101, 101);
452 bgSurface->expectTap(1, 1);
453}
454
455// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
456TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
457 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
458 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
459 parentSurface->showAt(100, 100);
460
461 childSurface->mInputInfo.surfaceInset = 10;
462 childSurface->showAt(100, 100);
463
464 childSurface->doTransaction([&](auto &t, auto &sc) {
465 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000466 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800467 });
468
469 injectTap(106, 106);
470 childSurface->expectTap(1, 1);
471
472 injectTap(101, 101);
473 parentSurface->expectTap(1, 1);
474}
475
Arthur Hung118b1142019-05-08 21:25:59 +0800476// Ensure a surface whose insets are scaled, handles the touch offset correctly.
477TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
478 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
479 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
480 bgSurface->showAt(100, 100);
481
482 fgSurface->mInputInfo.surfaceInset = 5;
483 fgSurface->showAt(100, 100);
484
485 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
486
487 // expect = touch / scale - inset
488 injectTap(112, 124);
489 fgSurface->expectTap(1, 1);
490
491 injectTap(101, 101);
492 bgSurface->expectTap(1, 1);
493}
494
Ady Abraham282f1d72019-07-24 18:05:56 -0700495TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
496 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
497 // In case we pass the very big inset without any checking.
498 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
499 fgSurface->showAt(100, 100);
500
501 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
502
503 // expect no crash for overflow, and inset size to be clamped to surface size
504 injectTap(202, 202);
505 fgSurface->expectTap(1, 1);
506}
507
Vishnu Nairde19f852018-12-18 16:11:53 -0800508// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
509TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
510 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
511 surface->doTransaction([](auto &t, auto &sc) {
512 Region transparentRegion(Rect(0, 0, 10, 10));
513 t.setTransparentRegionHint(sc, transparentRegion);
514 });
515 surface->showAt(100, 100);
516 injectTap(101, 101);
517 surface->expectTap(1, 1);
518}
519
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700520// TODO(b/139494112) update tests once we define expected behavior
521// Ensure we still send input to the surface regardless of surface visibility changes due to the
522// first buffer being submitted or alpha changes.
523// Original bug ref: b/120839715
524TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800525 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500526 std::unique_ptr<BlastInputSurface> bufferSurface =
527 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800528
529 bgSurface->showAt(10, 10);
530 bufferSurface->showAt(10, 10);
531
532 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700533 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800534
chaviw39d01472021-04-08 14:26:24 -0500535 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800536 injectTap(11, 11);
537 bufferSurface->expectTap(1, 1);
538}
539
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700540TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800541 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500542 std::unique_ptr<BlastInputSurface> bufferSurface =
543 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
544 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800545
546 bgSurface->showAt(10, 10);
547 bufferSurface->showAt(10, 10);
548
549 injectTap(11, 11);
550 bufferSurface->expectTap(1, 1);
551
552 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
553
554 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700555 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800556}
557
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700558TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800559 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
560 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
561
562 bgSurface->showAt(10, 10);
563 fgSurface->showAt(10, 10);
564
565 injectTap(11, 11);
566 fgSurface->expectTap(1, 1);
567
568 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
569
570 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700571 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800572}
573
574TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
575 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
576 std::unique_ptr<InputSurface> containerSurface =
577 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
578
579 bgSurface->showAt(10, 10);
580 containerSurface->showAt(10, 10);
581
582 injectTap(11, 11);
583 containerSurface->expectTap(1, 1);
584
585 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
586
587 injectTap(11, 11);
588 bgSurface->expectTap(1, 1);
589}
chaviwfbe5d9c2018-12-26 12:23:37 -0800590
Arthur Hungd20b2702019-01-14 18:16:16 +0800591TEST_F(InputSurfacesTest, input_respects_outscreen) {
592 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
593 surface->showAt(-1, -1);
594
595 injectTap(0, 0);
596 surface->expectTap(1, 1);
597}
arthurhungb4a0f852020-06-16 11:02:50 +0800598
599TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
600 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
601 std::unique_ptr<InputSurface> cursorSurface =
602 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
603
604 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800605 cursorSurface->showAt(10, 10);
606
607 injectTap(11, 11);
608 surface->expectTap(1, 1);
609}
Vishnu Nair958da932020-08-21 17:12:37 -0700610
611TEST_F(InputSurfacesTest, can_be_focused) {
612 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
613 surface->showAt(100, 100);
614 surface->requestFocus();
615
616 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700617}
chaviw44a6d2b2020-09-08 17:14:16 -0700618
619TEST_F(InputSurfacesTest, rotate_surface) {
620 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
621 surface->showAt(10, 10);
622 surface->doTransaction([](auto &t, auto &sc) {
623 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
624 });
625 injectTap(8, 11);
626 surface->expectTap(1, 2);
627
628 surface->doTransaction([](auto &t, auto &sc) {
629 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
630 });
631 injectTap(9, 8);
632 surface->expectTap(1, 2);
633
634 surface->doTransaction([](auto &t, auto &sc) {
635 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
636 });
637 injectTap(12, 9);
638 surface->expectTap(1, 2);
639}
640
641TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
642 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
643 surface->showAt(10, 10);
644 surface->doTransaction([](auto &t, auto &sc) {
645 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
646 });
647 injectTap(2, 12);
648 surface->expectTap(1, 2);
649
650 surface->doTransaction([](auto &t, auto &sc) {
651 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
652 });
653 injectTap(8, 2);
654 surface->expectTap(1, 2);
655
656 surface->doTransaction([](auto &t, auto &sc) {
657 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
658 });
659 injectTap(18, 8);
660 surface->expectTap(1, 2);
661}
662
663TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
664 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
665 surface->mInputInfo.surfaceInset = 5;
666 surface->showAt(100, 100);
667
668 surface->doTransaction([](auto &t, auto &sc) {
669 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
670 });
671 injectTap(40, 120);
672 surface->expectTap(5, 10);
673
674 surface->doTransaction([](auto &t, auto &sc) {
675 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
676 });
677 injectTap(80, 40);
678 surface->expectTap(5, 10);
679
680 surface->doTransaction([](auto &t, auto &sc) {
681 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
682 });
683 injectTap(160, 80);
684 surface->expectTap(5, 10);
685}
686
chaviw39cfa2e2020-11-04 14:19:02 -0800687TEST_F(InputSurfacesTest, touch_flag_obscured) {
688 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
689 surface->showAt(100, 100);
690
691 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
692 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
693 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
chaviw98318de2021-05-19 16:45:23 -0500694 nonTouchableSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw39cfa2e2020-11-04 14:19:02 -0800695 nonTouchableSurface->mInputInfo.ownerUid = 22222;
Bernardo Rufino602ef712020-12-21 11:02:18 +0000696 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
697 // the default obscured/untrusted touch filter introduced in S.
698 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800699 nonTouchableSurface->showAt(100, 100);
700
701 injectTap(190, 199);
702 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
703}
704
705TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
706 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
707 surface->showAt(100, 100);
708
709 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
710 // that will be tapped. Window behind gets touch, but with flag
711 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
712 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
713 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
chaviw98318de2021-05-19 16:45:23 -0500714 nonTouchableSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
715 parentSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw39cfa2e2020-11-04 14:19:02 -0800716 nonTouchableSurface->mInputInfo.ownerUid = 22222;
717 parentSurface->mInputInfo.ownerUid = 22222;
718 nonTouchableSurface->showAt(0, 0);
719 parentSurface->showAt(100, 100);
720
721 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800722 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800723 t.reparent(sc, parentSurface->mSurfaceControl);
724 });
725
726 injectTap(190, 199);
727 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
728}
729
730TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
731 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
732 surface->showAt(100, 100);
733
734 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
735 // the touchable window. Window behind gets touch with no obscured flags.
736 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
737 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
chaviw98318de2021-05-19 16:45:23 -0500738 nonTouchableSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
739 parentSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw39cfa2e2020-11-04 14:19:02 -0800740 nonTouchableSurface->mInputInfo.ownerUid = 22222;
741 parentSurface->mInputInfo.ownerUid = 22222;
742 nonTouchableSurface->showAt(0, 0);
743 parentSurface->showAt(50, 50);
744
745 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800746 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800747 t.reparent(sc, parentSurface->mSurfaceControl);
748 });
749
750 injectTap(101, 110);
751 surface->expectTap(1, 10);
752}
753
chaviw7e72caf2020-12-02 16:50:43 -0800754TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
755 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
756
757 std::unique_ptr<InputSurface> bufferSurface =
758 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
chaviw98318de2021-05-19 16:45:23 -0500759 bufferSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw7e72caf2020-12-02 16:50:43 -0800760 bufferSurface->mInputInfo.ownerUid = 22222;
761
762 surface->showAt(10, 10);
763 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
764
765 injectTap(11, 11);
766 surface->expectTap(1, 1);
767}
768
769TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
770 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
771
chaviw39d01472021-04-08 14:26:24 -0500772 std::unique_ptr<BlastInputSurface> bufferSurface =
773 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
chaviw98318de2021-05-19 16:45:23 -0500774 bufferSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw7e72caf2020-12-02 16:50:43 -0800775 bufferSurface->mInputInfo.ownerUid = 22222;
776
777 surface->showAt(10, 10);
778 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
779
780 injectTap(11, 11);
781 surface->expectTap(1, 1);
782}
783
Vishnu Nair958da932020-08-21 17:12:37 -0700784} // namespace android::test