blob: 49c44a78d1999e3dc79af8d60c822670ce6ef5f4 [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
chaviw39d01472021-04-08 14:26:24 -050050using android::hardware::graphics::common::V1_1::BufferUsage;
51
Vishnu Nair958da932020-08-21 17:12:37 -070052namespace android::test {
Robert Carr1c4c5592018-09-24 13:18:43 -070053
Vishnu Nairde19f852018-12-18 16:11:53 -080054using Transaction = SurfaceComposerClient::Transaction;
55
Robert Carr1c4c5592018-09-24 13:18:43 -070056sp<IInputFlinger> getInputFlinger() {
57 sp<IBinder> input(defaultServiceManager()->getService(
58 String16("inputflinger")));
59 if (input == nullptr) {
60 ALOGE("Failed to link to input service");
61 } else { ALOGE("Linked to input"); }
62 return interface_cast<IInputFlinger>(input);
63}
64
65// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
66static const int LAYER_BASE = INT32_MAX - 10;
Chris Ye6c4243b2020-07-22 12:07:12 -070067static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070068
69class InputSurface {
70public:
Vishnu Nairde19f852018-12-18 16:11:53 -080071 InputSurface(const sp<SurfaceControl> &sc, int width, int height) {
72 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -070073
74 mInputFlinger = getInputFlinger();
Garfield Tan15601662020-09-22 15:32:38 -070075 mClientChannel = std::make_shared<InputChannel>();
76 mInputFlinger->createInputChannel("testchannels", mClientChannel.get());
Robert Carr1c4c5592018-09-24 13:18:43 -070077
78 populateInputInfo(width, height);
79
80 mInputConsumer = new InputConsumer(mClientChannel);
81 }
82
Vishnu Nairde19f852018-12-18 16:11:53 -080083 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
84 int width, int height) {
85 sp<SurfaceControl> surfaceControl =
86 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -080087 PIXEL_FORMAT_RGBA_8888,
88 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -080089 return std::make_unique<InputSurface>(surfaceControl, width, height);
90 }
91
92 static std::unique_ptr<InputSurface> makeBufferInputSurface(
93 const sp<SurfaceComposerClient> &scc, int width, int height) {
94 sp<SurfaceControl> surfaceControl =
95 scc->createSurface(String8("Test Buffer Surface"), width, height,
96 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
97 return std::make_unique<InputSurface>(surfaceControl, width, height);
98 }
99
100 static std::unique_ptr<InputSurface> makeContainerInputSurface(
101 const sp<SurfaceComposerClient> &scc, int width, int height) {
102 sp<SurfaceControl> surfaceControl =
103 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
104 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
105 ISurfaceComposerClient::eFXSurfaceContainer);
106 return std::make_unique<InputSurface>(surfaceControl, width, height);
107 }
108
arthurhungb4a0f852020-06-16 11:02:50 +0800109 static std::unique_ptr<InputSurface> makeCursorInputSurface(
110 const sp<SurfaceComposerClient> &scc, int width, int height) {
111 sp<SurfaceControl> surfaceControl =
112 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
113 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
114 ISurfaceComposerClient::eCursorWindow);
115 return std::make_unique<InputSurface>(surfaceControl, width, height);
116 }
117
Robert Carr1c4c5592018-09-24 13:18:43 -0700118 InputEvent* consumeEvent() {
119 waitForEventAvailable();
120
121 InputEvent *ev;
122 uint32_t seqId;
123 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
124 if (consumed != OK) {
125 return nullptr;
126 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100127 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
128 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700129 return ev;
130 }
131
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100132 void assertFocusChange(bool hasFocus) {
133 InputEvent *ev = consumeEvent();
134 ASSERT_NE(ev, nullptr);
135 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, ev->getType());
136 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
137 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
138 }
139
Robert Carr1c4c5592018-09-24 13:18:43 -0700140 void expectTap(int x, int y) {
141 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100142 ASSERT_NE(ev, nullptr);
143 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700144 MotionEvent* mev = static_cast<MotionEvent*>(ev);
145 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
146 EXPECT_EQ(x, mev->getX(0));
147 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800148 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700149
150 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100151 ASSERT_NE(ev, nullptr);
152 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700153 mev = static_cast<MotionEvent*>(ev);
154 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
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
chaviw39cfa2e2020-11-04 14:19:02 -0800158 void expectTapWithFlag(int x, int y, int32_t flags) {
159 InputEvent *ev = consumeEvent();
160 ASSERT_NE(ev, nullptr);
161 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
162 MotionEvent *mev = static_cast<MotionEvent *>(ev);
163 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
164 EXPECT_EQ(x, mev->getX(0));
165 EXPECT_EQ(y, mev->getY(0));
166 EXPECT_EQ(flags, mev->getFlags() & flags);
167
168 ev = consumeEvent();
169 ASSERT_NE(ev, nullptr);
170 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
171 mev = static_cast<MotionEvent *>(ev);
172 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
173 EXPECT_EQ(flags, mev->getFlags() & flags);
174 }
175
chaviw39d01472021-04-08 14:26:24 -0500176 virtual ~InputSurface() {
177 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
178 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700179
chaviw39d01472021-04-08 14:26:24 -0500180 virtual void doTransaction(
181 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
182 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700183 SurfaceComposerClient::Transaction t;
184 transactionBody(t, mSurfaceControl);
185 t.apply(true);
186 }
187
chaviw39d01472021-04-08 14:26:24 -0500188 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700189 SurfaceComposerClient::Transaction t;
190 t.show(mSurfaceControl);
191 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
192 t.setLayer(mSurfaceControl, LAYER_BASE);
193 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800194 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700195 t.setAlpha(mSurfaceControl, 1);
196 t.apply(true);
197 }
198
Vishnu Nair958da932020-08-21 17:12:37 -0700199 void requestFocus() {
200 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800201 FocusRequest request;
202 request.token = mInputInfo.token;
203 request.windowName = mInputInfo.name;
204 request.focusedToken = nullptr;
205 request.focusedWindowName = "";
206 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
207 request.displayId = 0;
208 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700209 t.apply(true);
210 }
211
Robert Carr1c4c5592018-09-24 13:18:43 -0700212private:
213 void waitForEventAvailable() {
214 struct pollfd fd;
215
216 fd.fd = mClientChannel->getFd();
217 fd.events = POLLIN;
218 poll(&fd, 1, 3000);
219 }
220
221 void populateInputInfo(int width, int height) {
Garfield Tan15601662020-09-22 15:32:38 -0700222 mInputInfo.token = mClientChannel->getConnectionToken();
Robert Carr1c4c5592018-09-24 13:18:43 -0700223 mInputInfo.name = "Test info";
Michael Wright44753b12020-07-08 13:48:11 +0100224 mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
225 mInputInfo.type = InputWindowInfo::Type::BASE_APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500226 mInputInfo.dispatchingTimeout = 5s;
Robert Carre07e1032018-11-26 12:55:53 -0800227 mInputInfo.globalScaleFactor = 1.0;
Vishnu Nair47074b82020-08-14 11:54:47 -0700228 mInputInfo.focusable = true;
Robert Carr1c4c5592018-09-24 13:18:43 -0700229 mInputInfo.hasWallpaper = false;
230 mInputInfo.paused = false;
231
232 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
233
234 // TODO: Fill in from SF?
235 mInputInfo.ownerPid = 11111;
236 mInputInfo.ownerUid = 11111;
Robert Carr1c4c5592018-09-24 13:18:43 -0700237 mInputInfo.displayId = 0;
Robert Carr740167f2018-10-11 19:03:41 -0700238
239 InputApplicationInfo aInfo;
240 aInfo.token = new BBinder();
241 aInfo.name = "Test app info";
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500242 aInfo.dispatchingTimeoutMillis =
243 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Robert Carr740167f2018-10-11 19:03:41 -0700244
245 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700246 }
247public:
248 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500249 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700250 sp<IInputFlinger> mInputFlinger;
251
252 InputWindowInfo mInputInfo;
253
254 PreallocatedInputEventFactory mInputEventFactory;
255 InputConsumer* mInputConsumer;
256};
257
chaviw39d01472021-04-08 14:26:24 -0500258class BlastInputSurface : public InputSurface {
259public:
260 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
261 int height)
262 : InputSurface(sc, width, height) {
263 mParentSurfaceControl = parentSc;
264 }
265
266 ~BlastInputSurface() = default;
267
268 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
269 const sp<SurfaceComposerClient> &scc, int width, int height) {
270 sp<SurfaceControl> parentSc =
271 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
272 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
273 ISurfaceComposerClient::eFXSurfaceContainer);
274
275 sp<SurfaceControl> surfaceControl =
276 scc->createSurface(String8("Test Buffer Surface"), width, height,
277 PIXEL_FORMAT_RGBA_8888,
278 ISurfaceComposerClient::eFXSurfaceBufferState,
279 parentSc->getHandle());
280 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
281 }
282
283 void doTransaction(
284 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
285 transactionBody) override {
286 SurfaceComposerClient::Transaction t;
287 transactionBody(t, mParentSurfaceControl);
288 t.apply(true);
289 }
290
291 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
292 SurfaceComposerClient::Transaction t;
293 t.show(mParentSurfaceControl);
294 t.setLayer(mParentSurfaceControl, LAYER_BASE);
295 t.setPosition(mParentSurfaceControl, x, y);
296 t.setCrop(mParentSurfaceControl, crop);
297
298 t.show(mSurfaceControl);
299 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
300 t.setCrop(mSurfaceControl, crop);
301 t.setAlpha(mSurfaceControl, 1);
302 t.apply(true);
303 }
304
305private:
306 sp<SurfaceControl> mParentSurfaceControl;
307};
308
Robert Carr1c4c5592018-09-24 13:18:43 -0700309class InputSurfacesTest : public ::testing::Test {
310public:
311 InputSurfacesTest() {
312 ProcessState::self()->startThreadPool();
313 }
314
315 void SetUp() {
316 mComposerClient = new SurfaceComposerClient;
317 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Vishnu Nairde19f852018-12-18 16:11:53 -0800318
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800319 const auto display = mComposerClient->getInternalDisplayToken();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100320 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800321
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100322 ui::DisplayMode mode;
323 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800324
325 // After a new buffer is queued, SurfaceFlinger is notified and will
326 // latch the new buffer on next vsync. Let's heuristically wait for 3
327 // vsyncs.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100328 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.refreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700329 }
330
331 void TearDown() {
332 mComposerClient->dispose();
333 }
334
335 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800336 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
337 }
338
chaviw39d01472021-04-08 14:26:24 -0500339 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
340 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
341 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
342 sp<GraphicBuffer> buffer =
343 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
344 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800345 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700346 }
347
348 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800349 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700350};
351
352void injectTap(int x, int y) {
353 char *buf1, *buf2;
354 asprintf(&buf1, "%d", x);
355 asprintf(&buf2, "%d", y);
356 if (fork() == 0) {
357 execlp("input", "input", "tap", buf1, buf2, NULL);
358 }
359}
360
361TEST_F(InputSurfacesTest, can_receive_input) {
362 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
363 surface->showAt(100, 100);
364
365 injectTap(101, 101);
366
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100367 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700368}
369
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100370/**
371 * Set up two surfaces side-by-side. Tap each surface.
372 * Next, swap the positions of the two surfaces. Inject tap into the two
373 * original locations. Ensure that the tap is received by the surfaces in the
374 * reverse order.
375 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700376TEST_F(InputSurfacesTest, input_respects_positioning) {
377 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
378 surface->showAt(100, 100);
379
380 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
381 surface2->showAt(200, 200);
382
383 injectTap(201, 201);
384 surface2->expectTap(1, 1);
385
386 injectTap(101, 101);
387 surface->expectTap(1, 1);
388
389 surface2->doTransaction([](auto &t, auto &sc) {
390 t.setPosition(sc, 100, 100);
391 });
392 surface->doTransaction([](auto &t, auto &sc) {
393 t.setPosition(sc, 200, 200);
394 });
395
396 injectTap(101, 101);
397 surface2->expectTap(1, 1);
398
399 injectTap(201, 201);
400 surface->expectTap(1, 1);
401}
402
403TEST_F(InputSurfacesTest, input_respects_layering) {
404 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
405 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
406
407 surface->showAt(10, 10);
408 surface2->showAt(10, 10);
409
410 surface->doTransaction([](auto &t, auto &sc) {
411 t.setLayer(sc, LAYER_BASE + 1);
412 });
413
414 injectTap(11, 11);
415 surface->expectTap(1, 1);
416
417 surface2->doTransaction([](auto &t, auto &sc) {
418 t.setLayer(sc, LAYER_BASE + 1);
419 });
420
421 injectTap(11, 11);
422 surface2->expectTap(1, 1);
423
424 surface2->doTransaction([](auto &t, auto &sc) {
425 t.hide(sc);
426 });
427
428 injectTap(11, 11);
429 surface->expectTap(1, 1);
430}
431
Vishnu Nairde19f852018-12-18 16:11:53 -0800432// Surface Insets are set to offset the client content and draw a border around the client surface
433// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
434// of the client content.
435TEST_F(InputSurfacesTest, input_respects_surface_insets) {
436 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
437 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
438 bgSurface->showAt(100, 100);
439
440 fgSurface->mInputInfo.surfaceInset = 5;
441 fgSurface->showAt(100, 100);
442
443 injectTap(106, 106);
444 fgSurface->expectTap(1, 1);
445
446 injectTap(101, 101);
447 bgSurface->expectTap(1, 1);
448}
449
450// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
451TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
452 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
453 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
454 parentSurface->showAt(100, 100);
455
456 childSurface->mInputInfo.surfaceInset = 10;
457 childSurface->showAt(100, 100);
458
459 childSurface->doTransaction([&](auto &t, auto &sc) {
460 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000461 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800462 });
463
464 injectTap(106, 106);
465 childSurface->expectTap(1, 1);
466
467 injectTap(101, 101);
468 parentSurface->expectTap(1, 1);
469}
470
Arthur Hung118b1142019-05-08 21:25:59 +0800471// Ensure a surface whose insets are scaled, handles the touch offset correctly.
472TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
473 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
474 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
475 bgSurface->showAt(100, 100);
476
477 fgSurface->mInputInfo.surfaceInset = 5;
478 fgSurface->showAt(100, 100);
479
480 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
481
482 // expect = touch / scale - inset
483 injectTap(112, 124);
484 fgSurface->expectTap(1, 1);
485
486 injectTap(101, 101);
487 bgSurface->expectTap(1, 1);
488}
489
Ady Abraham282f1d72019-07-24 18:05:56 -0700490TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
491 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
492 // In case we pass the very big inset without any checking.
493 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
494 fgSurface->showAt(100, 100);
495
496 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
497
498 // expect no crash for overflow, and inset size to be clamped to surface size
499 injectTap(202, 202);
500 fgSurface->expectTap(1, 1);
501}
502
Vishnu Nairde19f852018-12-18 16:11:53 -0800503// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
504TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
505 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
506 surface->doTransaction([](auto &t, auto &sc) {
507 Region transparentRegion(Rect(0, 0, 10, 10));
508 t.setTransparentRegionHint(sc, transparentRegion);
509 });
510 surface->showAt(100, 100);
511 injectTap(101, 101);
512 surface->expectTap(1, 1);
513}
514
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700515// TODO(b/139494112) update tests once we define expected behavior
516// Ensure we still send input to the surface regardless of surface visibility changes due to the
517// first buffer being submitted or alpha changes.
518// Original bug ref: b/120839715
519TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800520 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500521 std::unique_ptr<BlastInputSurface> bufferSurface =
522 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800523
524 bgSurface->showAt(10, 10);
525 bufferSurface->showAt(10, 10);
526
527 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700528 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800529
chaviw39d01472021-04-08 14:26:24 -0500530 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800531 injectTap(11, 11);
532 bufferSurface->expectTap(1, 1);
533}
534
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700535TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800536 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500537 std::unique_ptr<BlastInputSurface> bufferSurface =
538 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
539 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800540
541 bgSurface->showAt(10, 10);
542 bufferSurface->showAt(10, 10);
543
544 injectTap(11, 11);
545 bufferSurface->expectTap(1, 1);
546
547 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
548
549 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700550 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800551}
552
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700553TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800554 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
555 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
556
557 bgSurface->showAt(10, 10);
558 fgSurface->showAt(10, 10);
559
560 injectTap(11, 11);
561 fgSurface->expectTap(1, 1);
562
563 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
564
565 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700566 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800567}
568
569TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
570 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
571 std::unique_ptr<InputSurface> containerSurface =
572 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
573
574 bgSurface->showAt(10, 10);
575 containerSurface->showAt(10, 10);
576
577 injectTap(11, 11);
578 containerSurface->expectTap(1, 1);
579
580 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
581
582 injectTap(11, 11);
583 bgSurface->expectTap(1, 1);
584}
chaviwfbe5d9c2018-12-26 12:23:37 -0800585
Arthur Hungd20b2702019-01-14 18:16:16 +0800586TEST_F(InputSurfacesTest, input_respects_outscreen) {
587 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
588 surface->showAt(-1, -1);
589
590 injectTap(0, 0);
591 surface->expectTap(1, 1);
592}
arthurhungb4a0f852020-06-16 11:02:50 +0800593
594TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
595 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
596 std::unique_ptr<InputSurface> cursorSurface =
597 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
598
599 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800600 cursorSurface->showAt(10, 10);
601
602 injectTap(11, 11);
603 surface->expectTap(1, 1);
604}
Vishnu Nair958da932020-08-21 17:12:37 -0700605
606TEST_F(InputSurfacesTest, can_be_focused) {
607 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
608 surface->showAt(100, 100);
609 surface->requestFocus();
610
611 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700612}
chaviw44a6d2b2020-09-08 17:14:16 -0700613
614TEST_F(InputSurfacesTest, rotate_surface) {
615 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
616 surface->showAt(10, 10);
617 surface->doTransaction([](auto &t, auto &sc) {
618 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
619 });
620 injectTap(8, 11);
621 surface->expectTap(1, 2);
622
623 surface->doTransaction([](auto &t, auto &sc) {
624 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
625 });
626 injectTap(9, 8);
627 surface->expectTap(1, 2);
628
629 surface->doTransaction([](auto &t, auto &sc) {
630 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
631 });
632 injectTap(12, 9);
633 surface->expectTap(1, 2);
634}
635
636TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
637 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
638 surface->showAt(10, 10);
639 surface->doTransaction([](auto &t, auto &sc) {
640 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
641 });
642 injectTap(2, 12);
643 surface->expectTap(1, 2);
644
645 surface->doTransaction([](auto &t, auto &sc) {
646 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
647 });
648 injectTap(8, 2);
649 surface->expectTap(1, 2);
650
651 surface->doTransaction([](auto &t, auto &sc) {
652 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
653 });
654 injectTap(18, 8);
655 surface->expectTap(1, 2);
656}
657
658TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
659 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
660 surface->mInputInfo.surfaceInset = 5;
661 surface->showAt(100, 100);
662
663 surface->doTransaction([](auto &t, auto &sc) {
664 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
665 });
666 injectTap(40, 120);
667 surface->expectTap(5, 10);
668
669 surface->doTransaction([](auto &t, auto &sc) {
670 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
671 });
672 injectTap(80, 40);
673 surface->expectTap(5, 10);
674
675 surface->doTransaction([](auto &t, auto &sc) {
676 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
677 });
678 injectTap(160, 80);
679 surface->expectTap(5, 10);
680}
681
chaviw39cfa2e2020-11-04 14:19:02 -0800682TEST_F(InputSurfacesTest, touch_flag_obscured) {
683 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
684 surface->showAt(100, 100);
685
686 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
687 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
688 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
689 nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
690 nonTouchableSurface->mInputInfo.ownerUid = 22222;
Bernardo Rufino602ef712020-12-21 11:02:18 +0000691 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
692 // the default obscured/untrusted touch filter introduced in S.
693 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800694 nonTouchableSurface->showAt(100, 100);
695
696 injectTap(190, 199);
697 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
698}
699
700TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
701 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
702 surface->showAt(100, 100);
703
704 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
705 // that will be tapped. Window behind gets touch, but with flag
706 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
707 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
708 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
709 nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
710 parentSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
711 nonTouchableSurface->mInputInfo.ownerUid = 22222;
712 parentSurface->mInputInfo.ownerUid = 22222;
713 nonTouchableSurface->showAt(0, 0);
714 parentSurface->showAt(100, 100);
715
716 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800717 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800718 t.reparent(sc, parentSurface->mSurfaceControl);
719 });
720
721 injectTap(190, 199);
722 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
723}
724
725TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
726 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
727 surface->showAt(100, 100);
728
729 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
730 // the touchable window. Window behind gets touch with no obscured flags.
731 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
732 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
733 nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
734 parentSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
735 nonTouchableSurface->mInputInfo.ownerUid = 22222;
736 parentSurface->mInputInfo.ownerUid = 22222;
737 nonTouchableSurface->showAt(0, 0);
738 parentSurface->showAt(50, 50);
739
740 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800741 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800742 t.reparent(sc, parentSurface->mSurfaceControl);
743 });
744
745 injectTap(101, 110);
746 surface->expectTap(1, 10);
747}
748
chaviw7e72caf2020-12-02 16:50:43 -0800749TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
750 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
751
752 std::unique_ptr<InputSurface> bufferSurface =
753 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
754 bufferSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
755 bufferSurface->mInputInfo.ownerUid = 22222;
756
757 surface->showAt(10, 10);
758 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
759
760 injectTap(11, 11);
761 surface->expectTap(1, 1);
762}
763
764TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
765 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
766
chaviw39d01472021-04-08 14:26:24 -0500767 std::unique_ptr<BlastInputSurface> bufferSurface =
768 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
chaviw7e72caf2020-12-02 16:50:43 -0800769 bufferSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
770 bufferSurface->mInputInfo.ownerUid = 22222;
771
772 surface->showAt(10, 10);
773 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
774
775 injectTap(11, 11);
776 surface->expectTap(1, 1);
777}
778
Vishnu Nair958da932020-08-21 17:12:37 -0700779} // namespace android::test