blob: cdd8ea65c70a84fdfd1339a96c41abc889e10786 [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 Nairfb060592021-09-13 18:40:17 -070027#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>
chaviw3277faf2021-05-19 16:45:23 -050041#include <gui/WindowInfo.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070042#include <input/Input.h>
Chris Ye0783e992020-06-02 21:34:49 -070043#include <input/InputTransport.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
chaviw3277faf2021-05-19 16:45:23 -050053using android::gui::FocusRequest;
54using android::gui::InputApplicationInfo;
55using android::gui::TouchOcclusionMode;
56using android::gui::WindowInfo;
57
Vishnu Nair958da932020-08-21 17:12:37 -070058namespace android::test {
Robert Carr1c4c5592018-09-24 13:18:43 -070059
Vishnu Nairde19f852018-12-18 16:11:53 -080060using Transaction = SurfaceComposerClient::Transaction;
61
Robert Carr1c4c5592018-09-24 13:18:43 -070062sp<IInputFlinger> getInputFlinger() {
63 sp<IBinder> input(defaultServiceManager()->getService(
64 String16("inputflinger")));
65 if (input == nullptr) {
66 ALOGE("Failed to link to input service");
67 } else { ALOGE("Linked to input"); }
68 return interface_cast<IInputFlinger>(input);
69}
70
71// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
72static const int LAYER_BASE = INT32_MAX - 10;
Chris Ye6c4243b2020-07-22 12:07:12 -070073static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070074
75class InputSurface {
76public:
Linus Tufvesson8758cd12022-03-04 09:32:07 +000077 InputSurface(const sp<SurfaceControl> &sc, int width, int height, bool noInputChannel = false) {
Vishnu Nairde19f852018-12-18 16:11:53 -080078 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -070079
80 mInputFlinger = getInputFlinger();
Linus Tufvesson8758cd12022-03-04 09:32:07 +000081 if (noInputChannel) {
82 mInputInfo.inputFeatures = WindowInfo::Feature::NO_INPUT_CHANNEL;
83 } else {
84 mClientChannel = std::make_shared<InputChannel>();
85 mInputFlinger->createInputChannel("testchannels", mClientChannel.get());
86 mInputInfo.token = mClientChannel->getConnectionToken();
87 mInputConsumer = new InputConsumer(mClientChannel);
88 }
Robert Carr1c4c5592018-09-24 13:18:43 -070089
Linus Tufvesson8758cd12022-03-04 09:32:07 +000090 mInputInfo.name = "Test info";
91 mInputInfo.dispatchingTimeout = 5s;
92 mInputInfo.globalScaleFactor = 1.0;
93 mInputInfo.flags = WindowInfo::Flag::NOT_TOUCH_MODAL;
94 mInputInfo.type = WindowInfo::Type::BASE_APPLICATION;
95 mInputInfo.focusable = true;
96 mInputInfo.hasWallpaper = false;
97 mInputInfo.paused = false;
98 // TODO: Fill in from SF?
99 mInputInfo.ownerPid = 11111;
100 mInputInfo.ownerUid = 11111;
101 mInputInfo.displayId = 0;
102 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
Robert Carr1c4c5592018-09-24 13:18:43 -0700103
Linus Tufvesson8758cd12022-03-04 09:32:07 +0000104 InputApplicationInfo aInfo;
105 aInfo.token = new BBinder();
106 aInfo.name = "Test app info";
107 aInfo.dispatchingTimeoutMillis =
108 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
109 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700110 }
111
Vishnu Nairde19f852018-12-18 16:11:53 -0800112 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
113 int width, int height) {
114 sp<SurfaceControl> surfaceControl =
115 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800116 PIXEL_FORMAT_RGBA_8888,
117 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -0800118 return std::make_unique<InputSurface>(surfaceControl, width, height);
119 }
120
121 static std::unique_ptr<InputSurface> makeBufferInputSurface(
122 const sp<SurfaceComposerClient> &scc, int width, int height) {
123 sp<SurfaceControl> surfaceControl =
124 scc->createSurface(String8("Test Buffer Surface"), width, height,
125 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
126 return std::make_unique<InputSurface>(surfaceControl, width, height);
127 }
128
129 static std::unique_ptr<InputSurface> makeContainerInputSurface(
130 const sp<SurfaceComposerClient> &scc, int width, int height) {
131 sp<SurfaceControl> surfaceControl =
132 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
133 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
134 ISurfaceComposerClient::eFXSurfaceContainer);
135 return std::make_unique<InputSurface>(surfaceControl, width, height);
136 }
137
Linus Tufvesson8758cd12022-03-04 09:32:07 +0000138 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
139 const sp<SurfaceComposerClient> &scc, int width, int height) {
140 sp<SurfaceControl> surfaceControl =
141 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
142 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
143 ISurfaceComposerClient::eFXSurfaceContainer);
144 return std::make_unique<InputSurface>(surfaceControl, width, height,
145 true /* noInputChannel */);
146 }
147
arthurhungb4a0f852020-06-16 11:02:50 +0800148 static std::unique_ptr<InputSurface> makeCursorInputSurface(
149 const sp<SurfaceComposerClient> &scc, int width, int height) {
150 sp<SurfaceControl> surfaceControl =
151 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
152 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
153 ISurfaceComposerClient::eCursorWindow);
154 return std::make_unique<InputSurface>(surfaceControl, width, height);
155 }
156
Vishnu Nairfb060592021-09-13 18:40:17 -0700157 InputEvent *consumeEvent(int timeoutMs = 3000) {
158 waitForEventAvailable(timeoutMs);
Robert Carr1c4c5592018-09-24 13:18:43 -0700159
160 InputEvent *ev;
161 uint32_t seqId;
162 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
163 if (consumed != OK) {
164 return nullptr;
165 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100166 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
167 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700168 return ev;
169 }
170
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100171 void assertFocusChange(bool hasFocus) {
172 InputEvent *ev = consumeEvent();
173 ASSERT_NE(ev, nullptr);
174 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, ev->getType());
175 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
176 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
177 }
178
Robert Carr1c4c5592018-09-24 13:18:43 -0700179 void expectTap(int x, int y) {
180 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100181 ASSERT_NE(ev, nullptr);
182 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700183 MotionEvent* mev = static_cast<MotionEvent*>(ev);
184 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
185 EXPECT_EQ(x, mev->getX(0));
186 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800187 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700188
189 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100190 ASSERT_NE(ev, nullptr);
191 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700192 mev = static_cast<MotionEvent*>(ev);
193 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800194 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700195 }
196
chaviw39cfa2e2020-11-04 14:19:02 -0800197 void expectTapWithFlag(int x, int y, int32_t flags) {
198 InputEvent *ev = consumeEvent();
199 ASSERT_NE(ev, nullptr);
200 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
201 MotionEvent *mev = static_cast<MotionEvent *>(ev);
202 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
203 EXPECT_EQ(x, mev->getX(0));
204 EXPECT_EQ(y, mev->getY(0));
205 EXPECT_EQ(flags, mev->getFlags() & flags);
206
207 ev = consumeEvent();
208 ASSERT_NE(ev, nullptr);
209 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
210 mev = static_cast<MotionEvent *>(ev);
211 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
212 EXPECT_EQ(flags, mev->getFlags() & flags);
213 }
214
Vishnu Nairfb060592021-09-13 18:40:17 -0700215 void expectKey(uint32_t keycode) {
216 InputEvent *ev = consumeEvent();
217 ASSERT_NE(ev, nullptr);
218 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, ev->getType());
219 KeyEvent *keyEvent = static_cast<KeyEvent *>(ev);
220 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
221 EXPECT_EQ(keycode, keyEvent->getKeyCode());
222 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
223
224 ev = consumeEvent();
225 ASSERT_NE(ev, nullptr);
226 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, ev->getType());
227 keyEvent = static_cast<KeyEvent *>(ev);
228 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
229 EXPECT_EQ(keycode, keyEvent->getKeyCode());
230 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
231 }
232
chaviw39d01472021-04-08 14:26:24 -0500233 virtual ~InputSurface() {
Linus Tufvesson8758cd12022-03-04 09:32:07 +0000234 if (mClientChannel) {
235 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
236 }
chaviw39d01472021-04-08 14:26:24 -0500237 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700238
chaviw39d01472021-04-08 14:26:24 -0500239 virtual void doTransaction(
240 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
241 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700242 SurfaceComposerClient::Transaction t;
243 transactionBody(t, mSurfaceControl);
244 t.apply(true);
245 }
246
chaviw39d01472021-04-08 14:26:24 -0500247 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700248 SurfaceComposerClient::Transaction t;
249 t.show(mSurfaceControl);
250 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
251 t.setLayer(mSurfaceControl, LAYER_BASE);
252 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800253 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700254 t.setAlpha(mSurfaceControl, 1);
255 t.apply(true);
256 }
257
Vishnu Nair958da932020-08-21 17:12:37 -0700258 void requestFocus() {
259 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800260 FocusRequest request;
261 request.token = mInputInfo.token;
262 request.windowName = mInputInfo.name;
263 request.focusedToken = nullptr;
264 request.focusedWindowName = "";
265 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
266 request.displayId = 0;
267 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700268 t.apply(true);
269 }
270
Robert Carr1c4c5592018-09-24 13:18:43 -0700271private:
Vishnu Nairfb060592021-09-13 18:40:17 -0700272 void waitForEventAvailable(int timeoutMs) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700273 struct pollfd fd;
274
275 fd.fd = mClientChannel->getFd();
276 fd.events = POLLIN;
Vishnu Nairfb060592021-09-13 18:40:17 -0700277 poll(&fd, 1, timeoutMs);
Robert Carr1c4c5592018-09-24 13:18:43 -0700278 }
279
Robert Carr1c4c5592018-09-24 13:18:43 -0700280public:
281 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500282 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700283 sp<IInputFlinger> mInputFlinger;
284
chaviw3277faf2021-05-19 16:45:23 -0500285 WindowInfo mInputInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700286
287 PreallocatedInputEventFactory mInputEventFactory;
288 InputConsumer* mInputConsumer;
289};
290
chaviw39d01472021-04-08 14:26:24 -0500291class BlastInputSurface : public InputSurface {
292public:
293 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
294 int height)
295 : InputSurface(sc, width, height) {
296 mParentSurfaceControl = parentSc;
297 }
298
299 ~BlastInputSurface() = default;
300
301 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
302 const sp<SurfaceComposerClient> &scc, int width, int height) {
303 sp<SurfaceControl> parentSc =
304 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
305 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
306 ISurfaceComposerClient::eFXSurfaceContainer);
307
308 sp<SurfaceControl> surfaceControl =
309 scc->createSurface(String8("Test Buffer Surface"), width, height,
310 PIXEL_FORMAT_RGBA_8888,
311 ISurfaceComposerClient::eFXSurfaceBufferState,
312 parentSc->getHandle());
313 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
314 }
315
316 void doTransaction(
317 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
318 transactionBody) override {
319 SurfaceComposerClient::Transaction t;
320 transactionBody(t, mParentSurfaceControl);
321 t.apply(true);
322 }
323
324 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
325 SurfaceComposerClient::Transaction t;
326 t.show(mParentSurfaceControl);
327 t.setLayer(mParentSurfaceControl, LAYER_BASE);
328 t.setPosition(mParentSurfaceControl, x, y);
329 t.setCrop(mParentSurfaceControl, crop);
330
331 t.show(mSurfaceControl);
332 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
333 t.setCrop(mSurfaceControl, crop);
334 t.setAlpha(mSurfaceControl, 1);
335 t.apply(true);
336 }
337
338private:
339 sp<SurfaceControl> mParentSurfaceControl;
340};
341
Robert Carr1c4c5592018-09-24 13:18:43 -0700342class InputSurfacesTest : public ::testing::Test {
343public:
344 InputSurfacesTest() {
345 ProcessState::self()->startThreadPool();
346 }
347
348 void SetUp() {
349 mComposerClient = new SurfaceComposerClient;
350 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Vishnu Nairde19f852018-12-18 16:11:53 -0800351
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800352 const auto display = mComposerClient->getInternalDisplayToken();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100353 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800354
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100355 ui::DisplayMode mode;
356 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800357
358 // After a new buffer is queued, SurfaceFlinger is notified and will
359 // latch the new buffer on next vsync. Let's heuristically wait for 3
360 // vsyncs.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100361 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.refreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700362 }
363
364 void TearDown() {
365 mComposerClient->dispose();
366 }
367
368 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800369 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
370 }
371
chaviw39d01472021-04-08 14:26:24 -0500372 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
373 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
374 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
375 sp<GraphicBuffer> buffer =
376 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
377 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800378 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700379 }
380
381 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800382 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700383};
384
385void injectTap(int x, int y) {
386 char *buf1, *buf2;
387 asprintf(&buf1, "%d", x);
388 asprintf(&buf2, "%d", y);
389 if (fork() == 0) {
390 execlp("input", "input", "tap", buf1, buf2, NULL);
391 }
392}
393
Vishnu Nairfb060592021-09-13 18:40:17 -0700394void injectKey(uint32_t keycode) {
395 char *buf1;
396 asprintf(&buf1, "%d", keycode);
397 if (fork() == 0) {
398 execlp("input", "input", "keyevent", buf1, NULL);
399 }
400}
401
Robert Carr1c4c5592018-09-24 13:18:43 -0700402TEST_F(InputSurfacesTest, can_receive_input) {
403 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
404 surface->showAt(100, 100);
405
406 injectTap(101, 101);
407
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100408 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700409}
410
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100411/**
412 * Set up two surfaces side-by-side. Tap each surface.
413 * Next, swap the positions of the two surfaces. Inject tap into the two
414 * original locations. Ensure that the tap is received by the surfaces in the
415 * reverse order.
416 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700417TEST_F(InputSurfacesTest, input_respects_positioning) {
418 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
419 surface->showAt(100, 100);
420
421 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
422 surface2->showAt(200, 200);
423
424 injectTap(201, 201);
425 surface2->expectTap(1, 1);
426
427 injectTap(101, 101);
428 surface->expectTap(1, 1);
429
430 surface2->doTransaction([](auto &t, auto &sc) {
431 t.setPosition(sc, 100, 100);
432 });
433 surface->doTransaction([](auto &t, auto &sc) {
434 t.setPosition(sc, 200, 200);
435 });
436
437 injectTap(101, 101);
438 surface2->expectTap(1, 1);
439
440 injectTap(201, 201);
441 surface->expectTap(1, 1);
442}
443
444TEST_F(InputSurfacesTest, input_respects_layering) {
445 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
446 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
447
448 surface->showAt(10, 10);
449 surface2->showAt(10, 10);
450
451 surface->doTransaction([](auto &t, auto &sc) {
452 t.setLayer(sc, LAYER_BASE + 1);
453 });
454
455 injectTap(11, 11);
456 surface->expectTap(1, 1);
457
458 surface2->doTransaction([](auto &t, auto &sc) {
459 t.setLayer(sc, LAYER_BASE + 1);
460 });
461
462 injectTap(11, 11);
463 surface2->expectTap(1, 1);
464
465 surface2->doTransaction([](auto &t, auto &sc) {
466 t.hide(sc);
467 });
468
469 injectTap(11, 11);
470 surface->expectTap(1, 1);
471}
472
Vishnu Nairde19f852018-12-18 16:11:53 -0800473// Surface Insets are set to offset the client content and draw a border around the client surface
474// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
475// of the client content.
476TEST_F(InputSurfacesTest, input_respects_surface_insets) {
477 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
478 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
479 bgSurface->showAt(100, 100);
480
481 fgSurface->mInputInfo.surfaceInset = 5;
482 fgSurface->showAt(100, 100);
483
484 injectTap(106, 106);
485 fgSurface->expectTap(1, 1);
486
487 injectTap(101, 101);
488 bgSurface->expectTap(1, 1);
489}
490
491// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
492TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
493 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
494 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
495 parentSurface->showAt(100, 100);
496
497 childSurface->mInputInfo.surfaceInset = 10;
498 childSurface->showAt(100, 100);
499
500 childSurface->doTransaction([&](auto &t, auto &sc) {
501 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000502 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800503 });
504
505 injectTap(106, 106);
506 childSurface->expectTap(1, 1);
507
508 injectTap(101, 101);
509 parentSurface->expectTap(1, 1);
510}
511
Arthur Hung118b1142019-05-08 21:25:59 +0800512// Ensure a surface whose insets are scaled, handles the touch offset correctly.
513TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
514 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
515 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
516 bgSurface->showAt(100, 100);
517
518 fgSurface->mInputInfo.surfaceInset = 5;
519 fgSurface->showAt(100, 100);
520
521 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
522
523 // expect = touch / scale - inset
524 injectTap(112, 124);
525 fgSurface->expectTap(1, 1);
526
527 injectTap(101, 101);
528 bgSurface->expectTap(1, 1);
529}
530
Ady Abraham282f1d72019-07-24 18:05:56 -0700531TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
532 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
533 // In case we pass the very big inset without any checking.
534 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
535 fgSurface->showAt(100, 100);
536
537 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
538
539 // expect no crash for overflow, and inset size to be clamped to surface size
540 injectTap(202, 202);
541 fgSurface->expectTap(1, 1);
542}
543
Vishnu Nairde19f852018-12-18 16:11:53 -0800544// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
545TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
546 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
547 surface->doTransaction([](auto &t, auto &sc) {
548 Region transparentRegion(Rect(0, 0, 10, 10));
549 t.setTransparentRegionHint(sc, transparentRegion);
550 });
551 surface->showAt(100, 100);
552 injectTap(101, 101);
553 surface->expectTap(1, 1);
554}
555
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700556// TODO(b/139494112) update tests once we define expected behavior
557// Ensure we still send input to the surface regardless of surface visibility changes due to the
558// first buffer being submitted or alpha changes.
559// Original bug ref: b/120839715
560TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800561 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500562 std::unique_ptr<BlastInputSurface> bufferSurface =
563 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800564
565 bgSurface->showAt(10, 10);
566 bufferSurface->showAt(10, 10);
567
568 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700569 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800570
chaviw39d01472021-04-08 14:26:24 -0500571 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800572 injectTap(11, 11);
573 bufferSurface->expectTap(1, 1);
574}
575
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700576TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800577 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500578 std::unique_ptr<BlastInputSurface> bufferSurface =
579 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
580 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800581
582 bgSurface->showAt(10, 10);
583 bufferSurface->showAt(10, 10);
584
585 injectTap(11, 11);
586 bufferSurface->expectTap(1, 1);
587
588 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
589
590 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700591 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800592}
593
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700594TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800595 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
596 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
597
598 bgSurface->showAt(10, 10);
599 fgSurface->showAt(10, 10);
600
601 injectTap(11, 11);
602 fgSurface->expectTap(1, 1);
603
604 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
605
606 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700607 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800608}
609
610TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
611 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
612 std::unique_ptr<InputSurface> containerSurface =
613 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
614
615 bgSurface->showAt(10, 10);
616 containerSurface->showAt(10, 10);
617
618 injectTap(11, 11);
619 containerSurface->expectTap(1, 1);
620
621 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
622
623 injectTap(11, 11);
624 bgSurface->expectTap(1, 1);
625}
chaviwfbe5d9c2018-12-26 12:23:37 -0800626
Arthur Hungd20b2702019-01-14 18:16:16 +0800627TEST_F(InputSurfacesTest, input_respects_outscreen) {
628 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
629 surface->showAt(-1, -1);
630
631 injectTap(0, 0);
632 surface->expectTap(1, 1);
633}
arthurhungb4a0f852020-06-16 11:02:50 +0800634
635TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
636 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
637 std::unique_ptr<InputSurface> cursorSurface =
638 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
639
640 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800641 cursorSurface->showAt(10, 10);
642
643 injectTap(11, 11);
644 surface->expectTap(1, 1);
645}
Vishnu Nair958da932020-08-21 17:12:37 -0700646
647TEST_F(InputSurfacesTest, can_be_focused) {
648 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
649 surface->showAt(100, 100);
650 surface->requestFocus();
651
652 surface->assertFocusChange(true);
Vishnu Nairfb060592021-09-13 18:40:17 -0700653
654 injectKey(AKEYCODE_V);
655 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700656}
chaviw44a6d2b2020-09-08 17:14:16 -0700657
658TEST_F(InputSurfacesTest, rotate_surface) {
659 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
660 surface->showAt(10, 10);
661 surface->doTransaction([](auto &t, auto &sc) {
662 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
663 });
664 injectTap(8, 11);
665 surface->expectTap(1, 2);
666
667 surface->doTransaction([](auto &t, auto &sc) {
668 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
669 });
670 injectTap(9, 8);
671 surface->expectTap(1, 2);
672
673 surface->doTransaction([](auto &t, auto &sc) {
674 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
675 });
676 injectTap(12, 9);
677 surface->expectTap(1, 2);
678}
679
680TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
681 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
682 surface->showAt(10, 10);
683 surface->doTransaction([](auto &t, auto &sc) {
684 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
685 });
686 injectTap(2, 12);
687 surface->expectTap(1, 2);
688
689 surface->doTransaction([](auto &t, auto &sc) {
690 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
691 });
692 injectTap(8, 2);
693 surface->expectTap(1, 2);
694
695 surface->doTransaction([](auto &t, auto &sc) {
696 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
697 });
698 injectTap(18, 8);
699 surface->expectTap(1, 2);
700}
701
702TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
703 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
704 surface->mInputInfo.surfaceInset = 5;
705 surface->showAt(100, 100);
706
707 surface->doTransaction([](auto &t, auto &sc) {
708 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
709 });
710 injectTap(40, 120);
711 surface->expectTap(5, 10);
712
713 surface->doTransaction([](auto &t, auto &sc) {
714 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
715 });
716 injectTap(80, 40);
717 surface->expectTap(5, 10);
718
719 surface->doTransaction([](auto &t, auto &sc) {
720 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
721 });
722 injectTap(160, 80);
723 surface->expectTap(5, 10);
724}
725
chaviw39cfa2e2020-11-04 14:19:02 -0800726TEST_F(InputSurfacesTest, touch_flag_obscured) {
727 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
728 surface->showAt(100, 100);
729
730 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
731 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
732 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
chaviw3277faf2021-05-19 16:45:23 -0500733 nonTouchableSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw39cfa2e2020-11-04 14:19:02 -0800734 nonTouchableSurface->mInputInfo.ownerUid = 22222;
Bernardo Rufino602ef712020-12-21 11:02:18 +0000735 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
736 // the default obscured/untrusted touch filter introduced in S.
737 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800738 nonTouchableSurface->showAt(100, 100);
739
740 injectTap(190, 199);
741 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
742}
743
744TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
745 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
746 surface->showAt(100, 100);
747
748 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
749 // that will be tapped. Window behind gets touch, but with flag
750 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
751 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
752 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
chaviw3277faf2021-05-19 16:45:23 -0500753 nonTouchableSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
754 parentSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw39cfa2e2020-11-04 14:19:02 -0800755 nonTouchableSurface->mInputInfo.ownerUid = 22222;
756 parentSurface->mInputInfo.ownerUid = 22222;
757 nonTouchableSurface->showAt(0, 0);
758 parentSurface->showAt(100, 100);
759
760 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800761 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800762 t.reparent(sc, parentSurface->mSurfaceControl);
763 });
764
765 injectTap(190, 199);
766 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
767}
768
769TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
770 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
771 surface->showAt(100, 100);
772
773 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
774 // the touchable window. Window behind gets touch with no obscured flags.
775 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
776 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
chaviw3277faf2021-05-19 16:45:23 -0500777 nonTouchableSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
778 parentSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw39cfa2e2020-11-04 14:19:02 -0800779 nonTouchableSurface->mInputInfo.ownerUid = 22222;
780 parentSurface->mInputInfo.ownerUid = 22222;
781 nonTouchableSurface->showAt(0, 0);
782 parentSurface->showAt(50, 50);
783
784 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800785 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800786 t.reparent(sc, parentSurface->mSurfaceControl);
787 });
788
789 injectTap(101, 110);
790 surface->expectTap(1, 10);
791}
792
chaviw7e72caf2020-12-02 16:50:43 -0800793TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
794 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
795
796 std::unique_ptr<InputSurface> bufferSurface =
797 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
chaviw3277faf2021-05-19 16:45:23 -0500798 bufferSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw7e72caf2020-12-02 16:50:43 -0800799 bufferSurface->mInputInfo.ownerUid = 22222;
800
801 surface->showAt(10, 10);
802 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
803
804 injectTap(11, 11);
805 surface->expectTap(1, 1);
806}
807
808TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
809 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
810
chaviw39d01472021-04-08 14:26:24 -0500811 std::unique_ptr<BlastInputSurface> bufferSurface =
812 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
chaviw3277faf2021-05-19 16:45:23 -0500813 bufferSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw7e72caf2020-12-02 16:50:43 -0800814 bufferSurface->mInputInfo.ownerUid = 22222;
815
816 surface->showAt(10, 10);
817 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
818
819 injectTap(11, 11);
820 surface->expectTap(1, 1);
821}
822
Vishnu Nairfb060592021-09-13 18:40:17 -0700823TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
824 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
825 surface->doTransaction(
826 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
827 surface->showAt(100, 100);
828
829 injectTap(101, 101);
830
831 EXPECT_NE(surface->consumeEvent(), nullptr);
832 EXPECT_NE(surface->consumeEvent(), nullptr);
833
834 surface->requestFocus();
835 surface->assertFocusChange(true);
836 injectKey(AKEYCODE_V);
837 surface->expectKey(AKEYCODE_V);
838}
839
840TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
841 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
842 surface->doTransaction([&](auto &t, auto &sc) {
843 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
844 t.setMatrix(sc, 2.0, 0, 0, 2.0);
845 });
846 surface->showAt(100, 100);
847
848 injectTap(101, 101);
849
850 EXPECT_NE(surface->consumeEvent(), nullptr);
851 EXPECT_NE(surface->consumeEvent(), nullptr);
852
853 surface->requestFocus();
854 surface->assertFocusChange(true);
855 injectKey(AKEYCODE_V);
856 surface->expectKey(AKEYCODE_V);
857}
858
859TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
860 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
861 surface->mInputInfo.ownerUid = 11111;
862 surface->doTransaction(
863 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
864 surface->showAt(100, 100);
865 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
866 obscuringSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
867 obscuringSurface->mInputInfo.ownerUid = 22222;
868 obscuringSurface->showAt(100, 100);
869 injectTap(101, 101);
870 EXPECT_EQ(surface->consumeEvent(100), nullptr);
871
872 surface->requestFocus();
873 surface->assertFocusChange(true);
874 injectKey(AKEYCODE_V);
875 EXPECT_EQ(surface->consumeEvent(100), nullptr);
876}
877
878TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
879 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
880 surface->mInputInfo.ownerUid = 11111;
881 surface->doTransaction(
882 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
883 surface->showAt(100, 100);
884 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
885 obscuringSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
886 obscuringSurface->mInputInfo.ownerUid = 22222;
887 obscuringSurface->showAt(190, 190);
888
889 injectTap(101, 101);
890
891 EXPECT_EQ(surface->consumeEvent(100), nullptr);
892
893 surface->requestFocus();
894 surface->assertFocusChange(true);
895 injectKey(AKEYCODE_V);
896 EXPECT_EQ(surface->consumeEvent(100), nullptr);
897}
898
899TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
900 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
901 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
902
903 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
904 surface->showAt(100, 100);
905 surface->doTransaction([&](auto &t, auto &sc) {
906 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
907 t.reparent(sc, parentSurface->mSurfaceControl);
908 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
909 });
910
911 injectTap(101, 101);
912
913 EXPECT_EQ(surface->consumeEvent(100), nullptr);
914
915 surface->requestFocus();
916 surface->assertFocusChange(true);
917 injectKey(AKEYCODE_V);
918 EXPECT_EQ(surface->consumeEvent(100), nullptr);
919}
920
921TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
922 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
923 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
924
925 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
926 surface->doTransaction([&](auto &t, auto &sc) {
927 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
928 t.reparent(sc, parentSurface->mSurfaceControl);
929 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
930 });
931 surface->showAt(100, 100);
932
933 injectTap(111, 111);
934
935 EXPECT_EQ(surface->consumeEvent(100), nullptr);
936
937 surface->requestFocus();
938 surface->assertFocusChange(true);
939 injectKey(AKEYCODE_V);
940 EXPECT_EQ(surface->consumeEvent(100), nullptr);
941}
942
Arthur Hung49d525a2021-11-19 15:11:51 +0000943TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
944 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
945
946 std::unique_ptr<BlastInputSurface> bufferSurface =
947 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
948
949 surface->showAt(100, 100);
950 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
951 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
952
953 injectTap(101, 101);
954 surface->expectTap(1, 1);
955}
956
Vishnu Nairfb060592021-09-13 18:40:17 -0700957TEST_F(InputSurfacesTest, drop_input_policy) {
958 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
959 surface->doTransaction(
960 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
961 surface->showAt(100, 100);
962
963 injectTap(101, 101);
964
965 EXPECT_EQ(surface->consumeEvent(100), nullptr);
966
967 surface->requestFocus();
968 surface->assertFocusChange(true);
969 injectKey(AKEYCODE_V);
970 EXPECT_EQ(surface->consumeEvent(100), nullptr);
971}
Linus Tufvesson8758cd12022-03-04 09:32:07 +0000972
973TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
974 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
975
976 parent->showAt(100, 100);
977 injectTap(101, 101);
978 parent->expectTap(1, 1);
979
980 std::unique_ptr<InputSurface> childContainerSurface =
981 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
982 childContainerSurface->showAt(0, 0);
983 childContainerSurface->doTransaction(
984 [&](auto &t, auto &sc) { t.reparent(sc, parent->mSurfaceControl); });
985 injectTap(101, 101);
986
987 EXPECT_EQ(parent->consumeEvent(100), nullptr);
988}
989
Vishnu Nair958da932020-08-21 17:12:37 -0700990} // namespace android::test