blob: 53e4af4454c2c8f2d151208edf599109e8cf1c62 [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 Nair9b0d13d2022-01-27 22:07:50 +000027#include <android/keycodes.h>
Vishnu Nairde19f852018-12-18 16:11:53 -080028#include <android/native_window.h>
29
Robert Carr1c4c5592018-09-24 13:18:43 -070030#include <binder/Binder.h>
31#include <binder/IServiceManager.h>
32#include <binder/Parcel.h>
33#include <binder/ProcessState.h>
34
Vishnu Nairde19f852018-12-18 16:11:53 -080035#include <gui/ISurfaceComposer.h>
36#include <gui/Surface.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070037#include <gui/SurfaceComposerClient.h>
38#include <gui/SurfaceControl.h>
39
Chris Ye0783e992020-06-02 21:34:49 -070040#include <android/os/IInputFlinger.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>
43#include <input/InputWindow.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070044
Marin Shalamanova7fe3042021-01-29 21:02:08 +010045#include <ui/DisplayMode.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070046#include <ui/Rect.h>
47#include <ui/Region.h>
48
Chris Ye0783e992020-06-02 21:34:49 -070049using android::os::IInputFlinger;
Robert Carr1c4c5592018-09-24 13:18:43 -070050
chaviw39d01472021-04-08 14:26:24 -050051using android::hardware::graphics::common::V1_1::BufferUsage;
52
Vishnu Nair958da932020-08-21 17:12:37 -070053namespace android::test {
Robert Carr1c4c5592018-09-24 13:18:43 -070054
Vishnu Nairde19f852018-12-18 16:11:53 -080055using Transaction = SurfaceComposerClient::Transaction;
56
Robert Carr1c4c5592018-09-24 13:18:43 -070057sp<IInputFlinger> getInputFlinger() {
58 sp<IBinder> input(defaultServiceManager()->getService(
59 String16("inputflinger")));
60 if (input == nullptr) {
61 ALOGE("Failed to link to input service");
62 } else { ALOGE("Linked to input"); }
63 return interface_cast<IInputFlinger>(input);
64}
65
66// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
67static const int LAYER_BASE = INT32_MAX - 10;
Chris Ye6c4243b2020-07-22 12:07:12 -070068static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070069
70class InputSurface {
71public:
Linus Tufvesson4c0e4312022-03-04 09:32:07 +000072 InputSurface(const sp<SurfaceControl> &sc, int width, int height, bool noInputChannel = false) {
Vishnu Nairde19f852018-12-18 16:11:53 -080073 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -070074
75 mInputFlinger = getInputFlinger();
Linus Tufvesson4c0e4312022-03-04 09:32:07 +000076 if (noInputChannel) {
77 mInputInfo.inputFeatures = InputWindowInfo::Feature::NO_INPUT_CHANNEL;
78 } else {
79 mClientChannel = std::make_shared<InputChannel>();
80 mInputFlinger->createInputChannel("testchannels", mClientChannel.get());
81 mInputInfo.token = mClientChannel->getConnectionToken();
82 mInputConsumer = new InputConsumer(mClientChannel);
83 }
Robert Carr1c4c5592018-09-24 13:18:43 -070084
Linus Tufvesson4c0e4312022-03-04 09:32:07 +000085 mInputInfo.name = "Test info";
86 mInputInfo.dispatchingTimeout = 5s;
87 mInputInfo.globalScaleFactor = 1.0;
88 mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
89 mInputInfo.type = InputWindowInfo::Type::BASE_APPLICATION;
90 mInputInfo.focusable = true;
91 mInputInfo.hasWallpaper = false;
92 mInputInfo.paused = false;
93 // TODO: Fill in from SF?
94 mInputInfo.ownerPid = 11111;
95 mInputInfo.ownerUid = 11111;
96 mInputInfo.displayId = 0;
97 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
Robert Carr1c4c5592018-09-24 13:18:43 -070098
Linus Tufvesson4c0e4312022-03-04 09:32:07 +000099 InputApplicationInfo aInfo;
100 aInfo.token = new BBinder();
101 aInfo.name = "Test app info";
102 aInfo.dispatchingTimeoutMillis =
103 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
104 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700105 }
106
Vishnu Nairde19f852018-12-18 16:11:53 -0800107 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
108 int width, int height) {
109 sp<SurfaceControl> surfaceControl =
110 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800111 PIXEL_FORMAT_RGBA_8888,
112 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -0800113 return std::make_unique<InputSurface>(surfaceControl, width, height);
114 }
115
116 static std::unique_ptr<InputSurface> makeBufferInputSurface(
117 const sp<SurfaceComposerClient> &scc, int width, int height) {
118 sp<SurfaceControl> surfaceControl =
119 scc->createSurface(String8("Test Buffer Surface"), width, height,
120 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
121 return std::make_unique<InputSurface>(surfaceControl, width, height);
122 }
123
124 static std::unique_ptr<InputSurface> makeContainerInputSurface(
125 const sp<SurfaceComposerClient> &scc, int width, int height) {
126 sp<SurfaceControl> surfaceControl =
127 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
128 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
129 ISurfaceComposerClient::eFXSurfaceContainer);
130 return std::make_unique<InputSurface>(surfaceControl, width, height);
131 }
132
Linus Tufvesson4c0e4312022-03-04 09:32:07 +0000133 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
134 const sp<SurfaceComposerClient> &scc, int width, int height) {
135 sp<SurfaceControl> surfaceControl =
136 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
137 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
138 ISurfaceComposerClient::eFXSurfaceContainer);
139 return std::make_unique<InputSurface>(surfaceControl, width, height,
140 true /* noInputChannel */);
141 }
142
arthurhungb4a0f852020-06-16 11:02:50 +0800143 static std::unique_ptr<InputSurface> makeCursorInputSurface(
144 const sp<SurfaceComposerClient> &scc, int width, int height) {
145 sp<SurfaceControl> surfaceControl =
146 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
147 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
148 ISurfaceComposerClient::eCursorWindow);
149 return std::make_unique<InputSurface>(surfaceControl, width, height);
150 }
151
Vishnu Nair9b0d13d2022-01-27 22:07:50 +0000152 InputEvent *consumeEvent(int timeoutMs = 3000) {
153 waitForEventAvailable(timeoutMs);
Robert Carr1c4c5592018-09-24 13:18:43 -0700154
155 InputEvent *ev;
156 uint32_t seqId;
157 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
158 if (consumed != OK) {
159 return nullptr;
160 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100161 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
162 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700163 return ev;
164 }
165
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100166 void assertFocusChange(bool hasFocus) {
167 InputEvent *ev = consumeEvent();
168 ASSERT_NE(ev, nullptr);
169 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, ev->getType());
170 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
171 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
172 }
173
Robert Carr1c4c5592018-09-24 13:18:43 -0700174 void expectTap(int x, int y) {
175 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100176 ASSERT_NE(ev, nullptr);
177 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700178 MotionEvent* mev = static_cast<MotionEvent*>(ev);
179 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
180 EXPECT_EQ(x, mev->getX(0));
181 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800182 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700183
184 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100185 ASSERT_NE(ev, nullptr);
186 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700187 mev = static_cast<MotionEvent*>(ev);
188 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800189 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700190 }
191
Vishnu Nair9b0d13d2022-01-27 22:07:50 +0000192 void expectKey(uint32_t keycode) {
193 InputEvent *ev = consumeEvent();
194 ASSERT_NE(ev, nullptr);
195 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, ev->getType());
196 KeyEvent *keyEvent = static_cast<KeyEvent *>(ev);
197 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
198 EXPECT_EQ(keycode, keyEvent->getKeyCode());
199 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
200
201 ev = consumeEvent();
202 ASSERT_NE(ev, nullptr);
203 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, ev->getType());
204 keyEvent = static_cast<KeyEvent *>(ev);
205 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
206 EXPECT_EQ(keycode, keyEvent->getKeyCode());
207 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
208 }
209
chaviw39cfa2e2020-11-04 14:19:02 -0800210 void expectTapWithFlag(int x, int y, int32_t flags) {
211 InputEvent *ev = consumeEvent();
212 ASSERT_NE(ev, nullptr);
213 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
214 MotionEvent *mev = static_cast<MotionEvent *>(ev);
215 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
216 EXPECT_EQ(x, mev->getX(0));
217 EXPECT_EQ(y, mev->getY(0));
218 EXPECT_EQ(flags, mev->getFlags() & flags);
219
220 ev = consumeEvent();
221 ASSERT_NE(ev, nullptr);
222 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
223 mev = static_cast<MotionEvent *>(ev);
224 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
225 EXPECT_EQ(flags, mev->getFlags() & flags);
226 }
227
chaviw39d01472021-04-08 14:26:24 -0500228 virtual ~InputSurface() {
Linus Tufvesson4c0e4312022-03-04 09:32:07 +0000229 if (mClientChannel) {
230 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
231 }
chaviw39d01472021-04-08 14:26:24 -0500232 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700233
chaviw39d01472021-04-08 14:26:24 -0500234 virtual void doTransaction(
235 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
236 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700237 SurfaceComposerClient::Transaction t;
238 transactionBody(t, mSurfaceControl);
239 t.apply(true);
240 }
241
chaviw39d01472021-04-08 14:26:24 -0500242 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700243 SurfaceComposerClient::Transaction t;
244 t.show(mSurfaceControl);
245 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
246 t.setLayer(mSurfaceControl, LAYER_BASE);
247 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800248 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700249 t.setAlpha(mSurfaceControl, 1);
250 t.apply(true);
251 }
252
Vishnu Nair958da932020-08-21 17:12:37 -0700253 void requestFocus() {
254 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800255 FocusRequest request;
256 request.token = mInputInfo.token;
257 request.windowName = mInputInfo.name;
258 request.focusedToken = nullptr;
259 request.focusedWindowName = "";
260 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
261 request.displayId = 0;
262 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700263 t.apply(true);
264 }
265
Robert Carr1c4c5592018-09-24 13:18:43 -0700266private:
Vishnu Nair9b0d13d2022-01-27 22:07:50 +0000267 void waitForEventAvailable(int timeoutMs) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700268 struct pollfd fd;
269
270 fd.fd = mClientChannel->getFd();
271 fd.events = POLLIN;
Vishnu Nair9b0d13d2022-01-27 22:07:50 +0000272 poll(&fd, 1, timeoutMs);
Robert Carr1c4c5592018-09-24 13:18:43 -0700273 }
274
Robert Carr1c4c5592018-09-24 13:18:43 -0700275public:
276 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500277 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700278 sp<IInputFlinger> mInputFlinger;
279
280 InputWindowInfo mInputInfo;
281
282 PreallocatedInputEventFactory mInputEventFactory;
283 InputConsumer* mInputConsumer;
284};
285
chaviw39d01472021-04-08 14:26:24 -0500286class BlastInputSurface : public InputSurface {
287public:
288 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
289 int height)
290 : InputSurface(sc, width, height) {
291 mParentSurfaceControl = parentSc;
292 }
293
294 ~BlastInputSurface() = default;
295
296 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
297 const sp<SurfaceComposerClient> &scc, int width, int height) {
298 sp<SurfaceControl> parentSc =
299 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
300 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
301 ISurfaceComposerClient::eFXSurfaceContainer);
302
303 sp<SurfaceControl> surfaceControl =
304 scc->createSurface(String8("Test Buffer Surface"), width, height,
305 PIXEL_FORMAT_RGBA_8888,
306 ISurfaceComposerClient::eFXSurfaceBufferState,
307 parentSc->getHandle());
308 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
309 }
310
311 void doTransaction(
312 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
313 transactionBody) override {
314 SurfaceComposerClient::Transaction t;
315 transactionBody(t, mParentSurfaceControl);
316 t.apply(true);
317 }
318
319 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
320 SurfaceComposerClient::Transaction t;
321 t.show(mParentSurfaceControl);
322 t.setLayer(mParentSurfaceControl, LAYER_BASE);
323 t.setPosition(mParentSurfaceControl, x, y);
324 t.setCrop(mParentSurfaceControl, crop);
325
326 t.show(mSurfaceControl);
327 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
328 t.setCrop(mSurfaceControl, crop);
329 t.setAlpha(mSurfaceControl, 1);
330 t.apply(true);
331 }
332
333private:
334 sp<SurfaceControl> mParentSurfaceControl;
335};
336
Robert Carr1c4c5592018-09-24 13:18:43 -0700337class InputSurfacesTest : public ::testing::Test {
338public:
339 InputSurfacesTest() {
340 ProcessState::self()->startThreadPool();
341 }
342
343 void SetUp() {
344 mComposerClient = new SurfaceComposerClient;
345 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Vishnu Nairde19f852018-12-18 16:11:53 -0800346
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800347 const auto display = mComposerClient->getInternalDisplayToken();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100348 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800349
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100350 ui::DisplayMode mode;
351 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800352
353 // After a new buffer is queued, SurfaceFlinger is notified and will
354 // latch the new buffer on next vsync. Let's heuristically wait for 3
355 // vsyncs.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100356 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.refreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700357 }
358
359 void TearDown() {
360 mComposerClient->dispose();
361 }
362
363 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800364 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
365 }
366
chaviw39d01472021-04-08 14:26:24 -0500367 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
368 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
369 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
370 sp<GraphicBuffer> buffer =
371 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
372 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800373 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700374 }
375
376 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800377 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700378};
379
380void injectTap(int x, int y) {
381 char *buf1, *buf2;
382 asprintf(&buf1, "%d", x);
383 asprintf(&buf2, "%d", y);
384 if (fork() == 0) {
385 execlp("input", "input", "tap", buf1, buf2, NULL);
386 }
387}
388
Vishnu Nair9b0d13d2022-01-27 22:07:50 +0000389void injectKey(uint32_t keycode) {
390 char *buf1;
391 asprintf(&buf1, "%d", keycode);
392 if (fork() == 0) {
393 execlp("input", "input", "keyevent", buf1, NULL);
394 }
395}
396
Robert Carr1c4c5592018-09-24 13:18:43 -0700397TEST_F(InputSurfacesTest, can_receive_input) {
398 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
399 surface->showAt(100, 100);
400
401 injectTap(101, 101);
402
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100403 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700404}
405
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100406/**
407 * Set up two surfaces side-by-side. Tap each surface.
408 * Next, swap the positions of the two surfaces. Inject tap into the two
409 * original locations. Ensure that the tap is received by the surfaces in the
410 * reverse order.
411 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700412TEST_F(InputSurfacesTest, input_respects_positioning) {
413 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
414 surface->showAt(100, 100);
415
416 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
417 surface2->showAt(200, 200);
418
419 injectTap(201, 201);
420 surface2->expectTap(1, 1);
421
422 injectTap(101, 101);
423 surface->expectTap(1, 1);
424
425 surface2->doTransaction([](auto &t, auto &sc) {
426 t.setPosition(sc, 100, 100);
427 });
428 surface->doTransaction([](auto &t, auto &sc) {
429 t.setPosition(sc, 200, 200);
430 });
431
432 injectTap(101, 101);
433 surface2->expectTap(1, 1);
434
435 injectTap(201, 201);
436 surface->expectTap(1, 1);
437}
438
439TEST_F(InputSurfacesTest, input_respects_layering) {
440 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
441 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
442
443 surface->showAt(10, 10);
444 surface2->showAt(10, 10);
445
446 surface->doTransaction([](auto &t, auto &sc) {
447 t.setLayer(sc, LAYER_BASE + 1);
448 });
449
450 injectTap(11, 11);
451 surface->expectTap(1, 1);
452
453 surface2->doTransaction([](auto &t, auto &sc) {
454 t.setLayer(sc, LAYER_BASE + 1);
455 });
456
457 injectTap(11, 11);
458 surface2->expectTap(1, 1);
459
460 surface2->doTransaction([](auto &t, auto &sc) {
461 t.hide(sc);
462 });
463
464 injectTap(11, 11);
465 surface->expectTap(1, 1);
466}
467
Vishnu Nairde19f852018-12-18 16:11:53 -0800468// Surface Insets are set to offset the client content and draw a border around the client surface
469// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
470// of the client content.
471TEST_F(InputSurfacesTest, input_respects_surface_insets) {
472 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
473 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
474 bgSurface->showAt(100, 100);
475
476 fgSurface->mInputInfo.surfaceInset = 5;
477 fgSurface->showAt(100, 100);
478
479 injectTap(106, 106);
480 fgSurface->expectTap(1, 1);
481
482 injectTap(101, 101);
483 bgSurface->expectTap(1, 1);
484}
485
486// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
487TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
488 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
489 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
490 parentSurface->showAt(100, 100);
491
492 childSurface->mInputInfo.surfaceInset = 10;
493 childSurface->showAt(100, 100);
494
495 childSurface->doTransaction([&](auto &t, auto &sc) {
496 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000497 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800498 });
499
500 injectTap(106, 106);
501 childSurface->expectTap(1, 1);
502
503 injectTap(101, 101);
504 parentSurface->expectTap(1, 1);
505}
506
Arthur Hung118b1142019-05-08 21:25:59 +0800507// Ensure a surface whose insets are scaled, handles the touch offset correctly.
508TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
509 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
510 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
511 bgSurface->showAt(100, 100);
512
513 fgSurface->mInputInfo.surfaceInset = 5;
514 fgSurface->showAt(100, 100);
515
516 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
517
518 // expect = touch / scale - inset
519 injectTap(112, 124);
520 fgSurface->expectTap(1, 1);
521
522 injectTap(101, 101);
523 bgSurface->expectTap(1, 1);
524}
525
Ady Abraham282f1d72019-07-24 18:05:56 -0700526TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
527 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
528 // In case we pass the very big inset without any checking.
529 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
530 fgSurface->showAt(100, 100);
531
532 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
533
534 // expect no crash for overflow, and inset size to be clamped to surface size
535 injectTap(202, 202);
536 fgSurface->expectTap(1, 1);
537}
538
Vishnu Nairde19f852018-12-18 16:11:53 -0800539// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
540TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
541 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
542 surface->doTransaction([](auto &t, auto &sc) {
543 Region transparentRegion(Rect(0, 0, 10, 10));
544 t.setTransparentRegionHint(sc, transparentRegion);
545 });
546 surface->showAt(100, 100);
547 injectTap(101, 101);
548 surface->expectTap(1, 1);
549}
550
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700551// TODO(b/139494112) update tests once we define expected behavior
552// Ensure we still send input to the surface regardless of surface visibility changes due to the
553// first buffer being submitted or alpha changes.
554// Original bug ref: b/120839715
555TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800556 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500557 std::unique_ptr<BlastInputSurface> bufferSurface =
558 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800559
560 bgSurface->showAt(10, 10);
561 bufferSurface->showAt(10, 10);
562
563 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700564 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800565
chaviw39d01472021-04-08 14:26:24 -0500566 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800567 injectTap(11, 11);
568 bufferSurface->expectTap(1, 1);
569}
570
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700571TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800572 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500573 std::unique_ptr<BlastInputSurface> bufferSurface =
574 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
575 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800576
577 bgSurface->showAt(10, 10);
578 bufferSurface->showAt(10, 10);
579
580 injectTap(11, 11);
581 bufferSurface->expectTap(1, 1);
582
583 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
584
585 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700586 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800587}
588
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700589TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800590 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
591 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
592
593 bgSurface->showAt(10, 10);
594 fgSurface->showAt(10, 10);
595
596 injectTap(11, 11);
597 fgSurface->expectTap(1, 1);
598
599 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
600
601 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700602 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800603}
604
605TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
606 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
607 std::unique_ptr<InputSurface> containerSurface =
608 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
609
610 bgSurface->showAt(10, 10);
611 containerSurface->showAt(10, 10);
612
613 injectTap(11, 11);
614 containerSurface->expectTap(1, 1);
615
616 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
617
618 injectTap(11, 11);
619 bgSurface->expectTap(1, 1);
620}
chaviwfbe5d9c2018-12-26 12:23:37 -0800621
Arthur Hungd20b2702019-01-14 18:16:16 +0800622TEST_F(InputSurfacesTest, input_respects_outscreen) {
623 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
624 surface->showAt(-1, -1);
625
626 injectTap(0, 0);
627 surface->expectTap(1, 1);
628}
arthurhungb4a0f852020-06-16 11:02:50 +0800629
630TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
631 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
632 std::unique_ptr<InputSurface> cursorSurface =
633 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
634
635 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800636 cursorSurface->showAt(10, 10);
637
638 injectTap(11, 11);
639 surface->expectTap(1, 1);
640}
Vishnu Nair958da932020-08-21 17:12:37 -0700641
Vishnu Nair9b0d13d2022-01-27 22:07:50 +0000642TEST_F(InputSurfacesTest, drop_input_policy) {
643 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
644 surface->doTransaction(
645 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
646 surface->showAt(100, 100);
647 surface->requestFocus();
648 surface->assertFocusChange(true);
649
650 injectTap(101, 101);
651 EXPECT_EQ(surface->consumeEvent(100), nullptr);
652
653 injectKey(AKEYCODE_V);
654 EXPECT_EQ(surface->consumeEvent(100), nullptr);
655}
656
Vishnu Nair958da932020-08-21 17:12:37 -0700657TEST_F(InputSurfacesTest, can_be_focused) {
658 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
659 surface->showAt(100, 100);
660 surface->requestFocus();
661
662 surface->assertFocusChange(true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700663}
chaviw44a6d2b2020-09-08 17:14:16 -0700664
665TEST_F(InputSurfacesTest, rotate_surface) {
666 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
667 surface->showAt(10, 10);
668 surface->doTransaction([](auto &t, auto &sc) {
669 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
670 });
671 injectTap(8, 11);
672 surface->expectTap(1, 2);
673
674 surface->doTransaction([](auto &t, auto &sc) {
675 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
676 });
677 injectTap(9, 8);
678 surface->expectTap(1, 2);
679
680 surface->doTransaction([](auto &t, auto &sc) {
681 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
682 });
683 injectTap(12, 9);
684 surface->expectTap(1, 2);
685}
686
687TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
688 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
689 surface->showAt(10, 10);
690 surface->doTransaction([](auto &t, auto &sc) {
691 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
692 });
693 injectTap(2, 12);
694 surface->expectTap(1, 2);
695
696 surface->doTransaction([](auto &t, auto &sc) {
697 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
698 });
699 injectTap(8, 2);
700 surface->expectTap(1, 2);
701
702 surface->doTransaction([](auto &t, auto &sc) {
703 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
704 });
705 injectTap(18, 8);
706 surface->expectTap(1, 2);
707}
708
709TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
710 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
711 surface->mInputInfo.surfaceInset = 5;
712 surface->showAt(100, 100);
713
714 surface->doTransaction([](auto &t, auto &sc) {
715 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
716 });
717 injectTap(40, 120);
718 surface->expectTap(5, 10);
719
720 surface->doTransaction([](auto &t, auto &sc) {
721 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
722 });
723 injectTap(80, 40);
724 surface->expectTap(5, 10);
725
726 surface->doTransaction([](auto &t, auto &sc) {
727 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
728 });
729 injectTap(160, 80);
730 surface->expectTap(5, 10);
731}
732
chaviw39cfa2e2020-11-04 14:19:02 -0800733TEST_F(InputSurfacesTest, touch_flag_obscured) {
734 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
735 surface->showAt(100, 100);
736
737 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
738 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
739 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
740 nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
741 nonTouchableSurface->mInputInfo.ownerUid = 22222;
Bernardo Rufino602ef712020-12-21 11:02:18 +0000742 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
743 // the default obscured/untrusted touch filter introduced in S.
744 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800745 nonTouchableSurface->showAt(100, 100);
746
747 injectTap(190, 199);
748 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
749}
750
751TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
752 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
753 surface->showAt(100, 100);
754
755 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
756 // that will be tapped. Window behind gets touch, but with flag
757 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
758 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
759 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
760 nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
761 parentSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
762 nonTouchableSurface->mInputInfo.ownerUid = 22222;
763 parentSurface->mInputInfo.ownerUid = 22222;
764 nonTouchableSurface->showAt(0, 0);
765 parentSurface->showAt(100, 100);
766
767 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800768 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800769 t.reparent(sc, parentSurface->mSurfaceControl);
770 });
771
772 injectTap(190, 199);
773 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
774}
775
776TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
777 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
778 surface->showAt(100, 100);
779
780 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
781 // the touchable window. Window behind gets touch with no obscured flags.
782 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
783 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
784 nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
785 parentSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
786 nonTouchableSurface->mInputInfo.ownerUid = 22222;
787 parentSurface->mInputInfo.ownerUid = 22222;
788 nonTouchableSurface->showAt(0, 0);
789 parentSurface->showAt(50, 50);
790
791 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800792 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800793 t.reparent(sc, parentSurface->mSurfaceControl);
794 });
795
796 injectTap(101, 110);
797 surface->expectTap(1, 10);
798}
799
chaviw7e72caf2020-12-02 16:50:43 -0800800TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
801 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
802
803 std::unique_ptr<InputSurface> bufferSurface =
804 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
805 bufferSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
806 bufferSurface->mInputInfo.ownerUid = 22222;
807
808 surface->showAt(10, 10);
809 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
810
811 injectTap(11, 11);
812 surface->expectTap(1, 1);
813}
814
815TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
816 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
817
chaviw39d01472021-04-08 14:26:24 -0500818 std::unique_ptr<BlastInputSurface> bufferSurface =
819 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
chaviw7e72caf2020-12-02 16:50:43 -0800820 bufferSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE;
821 bufferSurface->mInputInfo.ownerUid = 22222;
822
823 surface->showAt(10, 10);
824 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
825
826 injectTap(11, 11);
827 surface->expectTap(1, 1);
828}
829
Linus Tufvesson4c0e4312022-03-04 09:32:07 +0000830TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
831 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
832
833 parent->showAt(100, 100);
834 injectTap(101, 101);
835 parent->expectTap(1, 1);
836
837 std::unique_ptr<InputSurface> childContainerSurface =
838 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
839 childContainerSurface->showAt(0, 0);
840 childContainerSurface->doTransaction(
841 [&](auto &t, auto &sc) { t.reparent(sc, parent->mSurfaceControl); });
842 injectTap(101, 101);
843
844 EXPECT_EQ(parent->consumeEvent(100), nullptr);
845}
846
Vishnu Nair958da932020-08-21 17:12:37 -0700847} // namespace android::test