blob: c75c46c516df0f113e5a902f607113fc325ca2c4 [file] [log] [blame]
Robert Carr1c4c5592018-09-24 13:18:43 -07001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <sys/time.h>
21#include <sys/types.h>
22#include <stdio.h>
23#include <poll.h>
24
25#include <memory>
26
Vishnu Nairde19f852018-12-18 16:11:53 -080027#include <android/native_window.h>
28
Robert Carr1c4c5592018-09-24 13:18:43 -070029#include <binder/Binder.h>
30#include <binder/IServiceManager.h>
31#include <binder/Parcel.h>
32#include <binder/ProcessState.h>
33
Vishnu Nairde19f852018-12-18 16:11:53 -080034#include <gui/ISurfaceComposer.h>
35#include <gui/Surface.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070036#include <gui/SurfaceComposerClient.h>
37#include <gui/SurfaceControl.h>
38
Chris Ye0783e992020-06-02 21:34:49 -070039#include <android/os/IInputFlinger.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070040#include <input/Input.h>
Chris Ye0783e992020-06-02 21:34:49 -070041#include <input/InputTransport.h>
42#include <input/InputWindow.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070043
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080044#include <ui/DisplayConfig.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070045#include <ui/Rect.h>
46#include <ui/Region.h>
47
Chris Ye0783e992020-06-02 21:34:49 -070048using android::os::IInputFlinger;
Robert Carr1c4c5592018-09-24 13:18:43 -070049
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;
205 t.setFocusedWindow(mInputInfo.token, nullptr, systemTime(SYSTEM_TIME_MONOTONIC),
206 0 /* displayId */);
207 t.apply(true);
208 }
209
Robert Carr1c4c5592018-09-24 13:18:43 -0700210private:
211 void waitForEventAvailable() {
212 struct pollfd fd;
213
214 fd.fd = mClientChannel->getFd();
215 fd.events = POLLIN;
216 poll(&fd, 1, 3000);
217 }
218
219 void populateInputInfo(int width, int height) {
Garfield Tan15601662020-09-22 15:32:38 -0700220 mInputInfo.token = mClientChannel->getConnectionToken();
Robert Carr1c4c5592018-09-24 13:18:43 -0700221 mInputInfo.name = "Test info";
Michael Wright44753b12020-07-08 13:48:11 +0100222 mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
223 mInputInfo.type = InputWindowInfo::Type::BASE_APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500224 mInputInfo.dispatchingTimeout = 5s;
Robert Carre07e1032018-11-26 12:55:53 -0800225 mInputInfo.globalScaleFactor = 1.0;
Vishnu Nair47074b82020-08-14 11:54:47 -0700226 mInputInfo.focusable = true;
Robert Carr1c4c5592018-09-24 13:18:43 -0700227 mInputInfo.hasWallpaper = false;
228 mInputInfo.paused = false;
229
230 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
231
232 // TODO: Fill in from SF?
233 mInputInfo.ownerPid = 11111;
234 mInputInfo.ownerUid = 11111;
Robert Carr1c4c5592018-09-24 13:18:43 -0700235 mInputInfo.displayId = 0;
Robert Carr740167f2018-10-11 19:03:41 -0700236
237 InputApplicationInfo aInfo;
238 aInfo.token = new BBinder();
239 aInfo.name = "Test app info";
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500240 aInfo.dispatchingTimeoutMillis =
241 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Robert Carr740167f2018-10-11 19:03:41 -0700242
243 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700244 }
245public:
246 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500247 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700248 sp<IInputFlinger> mInputFlinger;
249
250 InputWindowInfo mInputInfo;
251
252 PreallocatedInputEventFactory mInputEventFactory;
253 InputConsumer* mInputConsumer;
254};
255
256class InputSurfacesTest : public ::testing::Test {
257public:
258 InputSurfacesTest() {
259 ProcessState::self()->startThreadPool();
260 }
261
262 void SetUp() {
263 mComposerClient = new SurfaceComposerClient;
264 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Vishnu Nairde19f852018-12-18 16:11:53 -0800265
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800266 const auto display = mComposerClient->getInternalDisplayToken();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100267 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800268
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800269 DisplayConfig config;
270 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayConfig(display, &config));
Vishnu Nairde19f852018-12-18 16:11:53 -0800271
272 // After a new buffer is queued, SurfaceFlinger is notified and will
273 // latch the new buffer on next vsync. Let's heuristically wait for 3
274 // vsyncs.
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800275 mBufferPostDelay = static_cast<int32_t>(1e6 / config.refreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700276 }
277
278 void TearDown() {
279 mComposerClient->dispose();
280 }
281
282 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800283 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
284 }
285
286 void postBuffer(const sp<SurfaceControl> &layer) {
287 // wait for previous transactions (such as setSize) to complete
288 Transaction().apply(true);
289 ANativeWindow_Buffer buffer = {};
290 EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
291 ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
292 // Request an empty transaction to get applied synchronously to ensure the buffer is
293 // latched.
294 Transaction().apply(true);
295 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700296 }
297
298 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800299 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700300};
301
302void injectTap(int x, int y) {
303 char *buf1, *buf2;
304 asprintf(&buf1, "%d", x);
305 asprintf(&buf2, "%d", y);
306 if (fork() == 0) {
307 execlp("input", "input", "tap", buf1, buf2, NULL);
308 }
309}
310
311TEST_F(InputSurfacesTest, can_receive_input) {
312 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
313 surface->showAt(100, 100);
314
315 injectTap(101, 101);
316
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100317 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700318}
319
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100320/**
321 * Set up two surfaces side-by-side. Tap each surface.
322 * Next, swap the positions of the two surfaces. Inject tap into the two
323 * original locations. Ensure that the tap is received by the surfaces in the
324 * reverse order.
325 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700326TEST_F(InputSurfacesTest, input_respects_positioning) {
327 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
328 surface->showAt(100, 100);
329
330 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
331 surface2->showAt(200, 200);
332
333 injectTap(201, 201);
334 surface2->expectTap(1, 1);
335
336 injectTap(101, 101);
337 surface->expectTap(1, 1);
338
339 surface2->doTransaction([](auto &t, auto &sc) {
340 t.setPosition(sc, 100, 100);
341 });
342 surface->doTransaction([](auto &t, auto &sc) {
343 t.setPosition(sc, 200, 200);
344 });
345
346 injectTap(101, 101);
347 surface2->expectTap(1, 1);
348
349 injectTap(201, 201);
350 surface->expectTap(1, 1);
351}
352
353TEST_F(InputSurfacesTest, input_respects_layering) {
354 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
355 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
356
357 surface->showAt(10, 10);
358 surface2->showAt(10, 10);
359
360 surface->doTransaction([](auto &t, auto &sc) {
361 t.setLayer(sc, LAYER_BASE + 1);
362 });
363
364 injectTap(11, 11);
365 surface->expectTap(1, 1);
366
367 surface2->doTransaction([](auto &t, auto &sc) {
368 t.setLayer(sc, LAYER_BASE + 1);
369 });
370
371 injectTap(11, 11);
372 surface2->expectTap(1, 1);
373
374 surface2->doTransaction([](auto &t, auto &sc) {
375 t.hide(sc);
376 });
377
378 injectTap(11, 11);
379 surface->expectTap(1, 1);
380}
381
Vishnu Nairde19f852018-12-18 16:11:53 -0800382// Surface Insets are set to offset the client content and draw a border around the client surface
383// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
384// of the client content.
385TEST_F(InputSurfacesTest, input_respects_surface_insets) {
386 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
387 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
388 bgSurface->showAt(100, 100);
389
390 fgSurface->mInputInfo.surfaceInset = 5;
391 fgSurface->showAt(100, 100);
392
393 injectTap(106, 106);
394 fgSurface->expectTap(1, 1);
395
396 injectTap(101, 101);
397 bgSurface->expectTap(1, 1);
398}
399
400// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
401TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
402 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
403 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
404 parentSurface->showAt(100, 100);
405
406 childSurface->mInputInfo.surfaceInset = 10;
407 childSurface->showAt(100, 100);
408
409 childSurface->doTransaction([&](auto &t, auto &sc) {
410 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000411 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800412 });
413
414 injectTap(106, 106);
415 childSurface->expectTap(1, 1);
416
417 injectTap(101, 101);
418 parentSurface->expectTap(1, 1);
419}
420
Arthur Hung118b1142019-05-08 21:25:59 +0800421// Ensure a surface whose insets are scaled, handles the touch offset correctly.
422TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
423 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
424 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
425 bgSurface->showAt(100, 100);
426
427 fgSurface->mInputInfo.surfaceInset = 5;
428 fgSurface->showAt(100, 100);
429
430 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
431
432 // expect = touch / scale - inset
433 injectTap(112, 124);
434 fgSurface->expectTap(1, 1);
435
436 injectTap(101, 101);
437 bgSurface->expectTap(1, 1);
438}
439
Ady Abraham282f1d72019-07-24 18:05:56 -0700440TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
441 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
442 // In case we pass the very big inset without any checking.
443 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
444 fgSurface->showAt(100, 100);
445
446 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
447
448 // expect no crash for overflow, and inset size to be clamped to surface size
449 injectTap(202, 202);
450 fgSurface->expectTap(1, 1);
451}
452
Vishnu Nairde19f852018-12-18 16:11:53 -0800453// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
454TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
455 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
456 surface->doTransaction([](auto &t, auto &sc) {
457 Region transparentRegion(Rect(0, 0, 10, 10));
458 t.setTransparentRegionHint(sc, transparentRegion);
459 });
460 surface->showAt(100, 100);
461 injectTap(101, 101);
462 surface->expectTap(1, 1);
463}
464
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700465// TODO(b/139494112) update tests once we define expected behavior
466// Ensure we still send input to the surface regardless of surface visibility changes due to the
467// first buffer being submitted or alpha changes.
468// Original bug ref: b/120839715
469TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800470 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
471 std::unique_ptr<InputSurface> bufferSurface =
472 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
473
474 bgSurface->showAt(10, 10);
475 bufferSurface->showAt(10, 10);
476
477 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700478 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800479
480 postBuffer(bufferSurface->mSurfaceControl);
481 injectTap(11, 11);
482 bufferSurface->expectTap(1, 1);
483}
484
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700485TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800486 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
487 std::unique_ptr<InputSurface> bufferSurface =
488 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
489 postBuffer(bufferSurface->mSurfaceControl);
490
491 bgSurface->showAt(10, 10);
492 bufferSurface->showAt(10, 10);
493
494 injectTap(11, 11);
495 bufferSurface->expectTap(1, 1);
496
497 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
498
499 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700500 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800501}
502
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700503TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800504 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
505 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
506
507 bgSurface->showAt(10, 10);
508 fgSurface->showAt(10, 10);
509
510 injectTap(11, 11);
511 fgSurface->expectTap(1, 1);
512
513 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
514
515 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700516 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800517}
518
519TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
520 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
521 std::unique_ptr<InputSurface> containerSurface =
522 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
523
524 bgSurface->showAt(10, 10);
525 containerSurface->showAt(10, 10);
526
527 injectTap(11, 11);
528 containerSurface->expectTap(1, 1);
529
530 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
531
532 injectTap(11, 11);
533 bgSurface->expectTap(1, 1);
534}
chaviwfbe5d9c2018-12-26 12:23:37 -0800535
Arthur Hungd20b2702019-01-14 18:16:16 +0800536TEST_F(InputSurfacesTest, input_respects_outscreen) {
537 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
538 surface->showAt(-1, -1);
539
540 injectTap(0, 0);
541 surface->expectTap(1, 1);
542}
arthurhungb4a0f852020-06-16 11:02:50 +0800543
544TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
545 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
546 std::unique_ptr<InputSurface> cursorSurface =
547 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
548
549 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800550 cursorSurface->showAt(10, 10);
551
552 injectTap(11, 11);
553 surface->expectTap(1, 1);
554}
Vishnu Nair958da932020-08-21 17:12:37 -0700555
556TEST_F(InputSurfacesTest, can_be_focused) {
557 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
558 surface->showAt(100, 100);
559 surface->requestFocus();
560
561 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700562}
chaviw44a6d2b2020-09-08 17:14:16 -0700563
564TEST_F(InputSurfacesTest, rotate_surface) {
565 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
566 surface->showAt(10, 10);
567 surface->doTransaction([](auto &t, auto &sc) {
568 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
569 });
570 injectTap(8, 11);
571 surface->expectTap(1, 2);
572
573 surface->doTransaction([](auto &t, auto &sc) {
574 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
575 });
576 injectTap(9, 8);
577 surface->expectTap(1, 2);
578
579 surface->doTransaction([](auto &t, auto &sc) {
580 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
581 });
582 injectTap(12, 9);
583 surface->expectTap(1, 2);
584}
585
586TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
587 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
588 surface->showAt(10, 10);
589 surface->doTransaction([](auto &t, auto &sc) {
590 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
591 });
592 injectTap(2, 12);
593 surface->expectTap(1, 2);
594
595 surface->doTransaction([](auto &t, auto &sc) {
596 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
597 });
598 injectTap(8, 2);
599 surface->expectTap(1, 2);
600
601 surface->doTransaction([](auto &t, auto &sc) {
602 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
603 });
604 injectTap(18, 8);
605 surface->expectTap(1, 2);
606}
607
608TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
609 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
610 surface->mInputInfo.surfaceInset = 5;
611 surface->showAt(100, 100);
612
613 surface->doTransaction([](auto &t, auto &sc) {
614 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
615 });
616 injectTap(40, 120);
617 surface->expectTap(5, 10);
618
619 surface->doTransaction([](auto &t, auto &sc) {
620 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
621 });
622 injectTap(80, 40);
623 surface->expectTap(5, 10);
624
625 surface->doTransaction([](auto &t, auto &sc) {
626 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
627 });
628 injectTap(160, 80);
629 surface->expectTap(5, 10);
630}
631
chaviw39cfa2e2020-11-04 14:19:02 -0800632TEST_F(InputSurfacesTest, touch_flag_obscured) {
633 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
634 surface->showAt(100, 100);
635
636 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
637 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
638 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
639 nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
640 nonTouchableSurface->mInputInfo.ownerUid = 22222;
641 nonTouchableSurface->showAt(100, 100);
642
643 injectTap(190, 199);
644 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
645}
646
647TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
648 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
649 surface->showAt(100, 100);
650
651 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
652 // that will be tapped. Window behind gets touch, but with flag
653 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
654 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
655 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
656 nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
657 parentSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
658 nonTouchableSurface->mInputInfo.ownerUid = 22222;
659 parentSurface->mInputInfo.ownerUid = 22222;
660 nonTouchableSurface->showAt(0, 0);
661 parentSurface->showAt(100, 100);
662
663 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
664 t.setCrop_legacy(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
665 t.reparent(sc, parentSurface->mSurfaceControl);
666 });
667
668 injectTap(190, 199);
669 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
670}
671
672TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
673 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
674 surface->showAt(100, 100);
675
676 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
677 // the touchable window. Window behind gets touch with no obscured flags.
678 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
679 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
680 nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
681 parentSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
682 nonTouchableSurface->mInputInfo.ownerUid = 22222;
683 parentSurface->mInputInfo.ownerUid = 22222;
684 nonTouchableSurface->showAt(0, 0);
685 parentSurface->showAt(50, 50);
686
687 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
688 t.setCrop_legacy(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
689 t.reparent(sc, parentSurface->mSurfaceControl);
690 });
691
692 injectTap(101, 110);
693 surface->expectTap(1, 10);
694}
695
chaviw7e72caf2020-12-02 16:50:43 -0800696TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
697 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
698
699 std::unique_ptr<InputSurface> bufferSurface =
700 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
701 bufferSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
702 bufferSurface->mInputInfo.ownerUid = 22222;
703
704 surface->showAt(10, 10);
705 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
706
707 injectTap(11, 11);
708 surface->expectTap(1, 1);
709}
710
711TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
712 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
713
714 std::unique_ptr<InputSurface> bufferSurface =
715 InputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
716 bufferSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
717 bufferSurface->mInputInfo.ownerUid = 22222;
718
719 surface->showAt(10, 10);
720 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
721
722 injectTap(11, 11);
723 surface->expectTap(1, 1);
724}
725
Vishnu Nair958da932020-08-21 17:12:37 -0700726} // namespace android::test