1 /*
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
27 #include <android-base/thread_annotations.h>
28 #include <android/gui/BnWindowInfosReportedListener.h>
29 #include <android/keycodes.h>
30 #include <android/native_window.h>
31
32 #include <binder/Binder.h>
33 #include <binder/IServiceManager.h>
34 #include <binder/Parcel.h>
35 #include <binder/ProcessState.h>
36
37 #include <gui/ISurfaceComposer.h>
38 #include <gui/Surface.h>
39 #include <gui/SurfaceComposerClient.h>
40 #include <gui/SurfaceControl.h>
41
42 #include <android/os/IInputFlinger.h>
43 #include <gui/WindowInfo.h>
44 #include <input/Input.h>
45 #include <input/InputConsumer.h>
46 #include <input/InputTransport.h>
47
48 #include <ui/DisplayMode.h>
49 #include <ui/Rect.h>
50 #include <ui/Region.h>
51
52 #include <private/android_filesystem_config.h>
53
54 using android::os::IInputFlinger;
55
56 using android::hardware::graphics::common::V1_1::BufferUsage;
57
58 using android::gui::FocusRequest;
59 using android::gui::InputApplicationInfo;
60 using android::gui::TouchOcclusionMode;
61 using android::gui::WindowInfo;
62
63 namespace android {
64 namespace {
toDisplayId(ui::LayerStack layerStack)65 ui::LogicalDisplayId toDisplayId(ui::LayerStack layerStack) {
66 return ui::LogicalDisplayId{static_cast<int32_t>(layerStack.id)};
67 }
68 } // namespace
69 namespace test {
70
71 using Transaction = SurfaceComposerClient::Transaction;
72
getInputFlinger()73 sp<IInputFlinger> getInputFlinger() {
74 sp<IBinder> input(defaultServiceManager()->waitForService(String16("inputflinger")));
75 if (input == nullptr) {
76 ALOGE("Failed to link to input service");
77 } else {
78 ALOGE("Linked to input");
79 }
80 return interface_cast<IInputFlinger>(input);
81 }
82
83 // We use the top 10 layers as a way to haphazardly place ourselves above anything else.
84 static const int LAYER_BASE = INT32_MAX - 10;
85 static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
86
87 class SynchronousWindowInfosReportedListener : public gui::BnWindowInfosReportedListener {
88 public:
onWindowInfosReported()89 binder::Status onWindowInfosReported() override {
90 std::scoped_lock lock{mLock};
91 mWindowInfosReported = true;
92 mConditionVariable.notify_one();
93 return binder::Status::ok();
94 }
95
wait()96 void wait() {
97 std::unique_lock lock{mLock};
98 android::base::ScopedLockAssertion assumeLocked(mLock);
99 mConditionVariable.wait(lock, [&]() REQUIRES(mLock) { return mWindowInfosReported; });
100 }
101
102 private:
103 std::mutex mLock;
104 std::condition_variable mConditionVariable;
GUARDED_BY(mLock)105 bool mWindowInfosReported GUARDED_BY(mLock){false};
106 };
107
108 class InputSurface {
109 public:
InputSurface(const sp<SurfaceControl> & sc,int width,int height,bool noInputChannel=false)110 InputSurface(const sp<SurfaceControl>& sc, int width, int height, bool noInputChannel = false) {
111 mSurfaceControl = sc;
112
113 mInputFlinger = getInputFlinger();
114 if (noInputChannel) {
115 mInputInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, true);
116 } else {
117 android::os::InputChannelCore tempChannel;
118 android::binder::Status result =
119 mInputFlinger->createInputChannel("testchannels", &tempChannel);
120 if (!result.isOk()) {
121 ADD_FAILURE() << "binder call to createInputChannel failed";
122 }
123 mClientChannel = InputChannel::create(std::move(tempChannel));
124 mInputInfo.token = mClientChannel->getConnectionToken();
125 mInputConsumer = new InputConsumer(mClientChannel);
126 }
127
128 mInputInfo.name = "Test info";
129 mInputInfo.dispatchingTimeout = 5s;
130 mInputInfo.globalScaleFactor = 1.0;
131 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
132
133 InputApplicationInfo aInfo;
134 aInfo.token = new BBinder();
135 aInfo.name = "Test app info";
136 aInfo.dispatchingTimeoutMillis =
137 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
138 mInputInfo.applicationInfo = aInfo;
139 }
140
makeColorInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)141 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient>& scc,
142 int width, int height) {
143 sp<SurfaceControl> surfaceControl =
144 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
145 PIXEL_FORMAT_RGBA_8888,
146 ISurfaceComposerClient::eFXSurfaceEffect);
147 return std::make_unique<InputSurface>(surfaceControl, width, height);
148 }
149
makeBufferInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)150 static std::unique_ptr<InputSurface> makeBufferInputSurface(
151 const sp<SurfaceComposerClient>& scc, int width, int height) {
152 sp<SurfaceControl> surfaceControl =
153 scc->createSurface(String8("Test Buffer Surface"), width, height,
154 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
155 return std::make_unique<InputSurface>(surfaceControl, width, height);
156 }
157
makeContainerInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)158 static std::unique_ptr<InputSurface> makeContainerInputSurface(
159 const sp<SurfaceComposerClient>& scc, int width, int height) {
160 sp<SurfaceControl> surfaceControl =
161 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
162 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
163 ISurfaceComposerClient::eFXSurfaceContainer);
164 return std::make_unique<InputSurface>(surfaceControl, width, height);
165 }
166
makeContainerInputSurfaceNoInputChannel(const sp<SurfaceComposerClient> & scc,int width,int height)167 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
168 const sp<SurfaceComposerClient>& scc, int width, int height) {
169 sp<SurfaceControl> surfaceControl =
170 scc->createSurface(String8("Test Container Surface"), 100 /* height */,
171 100 /* width */, PIXEL_FORMAT_RGBA_8888,
172 ISurfaceComposerClient::eFXSurfaceContainer);
173 return std::make_unique<InputSurface>(surfaceControl, width, height,
174 true /* noInputChannel */);
175 }
176
makeCursorInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)177 static std::unique_ptr<InputSurface> makeCursorInputSurface(
178 const sp<SurfaceComposerClient>& scc, int width, int height) {
179 sp<SurfaceControl> surfaceControl =
180 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
181 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
182 ISurfaceComposerClient::eCursorWindow);
183 return std::make_unique<InputSurface>(surfaceControl, width, height);
184 }
185
consumeEvent(std::chrono::milliseconds timeout=3000ms)186 InputEvent* consumeEvent(std::chrono::milliseconds timeout = 3000ms) {
187 mClientChannel->waitForMessage(timeout);
188
189 InputEvent* ev;
190 uint32_t seqId;
191 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
192 if (consumed != OK) {
193 return nullptr;
194 }
195 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
196 EXPECT_EQ(OK, status) << "Could not send finished signal";
197 return ev;
198 }
199
assertFocusChange(bool hasFocus)200 void assertFocusChange(bool hasFocus) {
201 InputEvent* ev = consumeEvent();
202 ASSERT_NE(ev, nullptr);
203 ASSERT_EQ(InputEventType::FOCUS, ev->getType());
204 FocusEvent* focusEvent = static_cast<FocusEvent*>(ev);
205 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
206 }
207
expectTap(float x,float y)208 void expectTap(float x, float y) {
209 InputEvent* ev = consumeEvent();
210 ASSERT_NE(ev, nullptr);
211 ASSERT_EQ(InputEventType::MOTION, ev->getType());
212 MotionEvent* mev = static_cast<MotionEvent*>(ev);
213 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
214 EXPECT_EQ(x, mev->getX(0));
215 EXPECT_EQ(y, mev->getY(0));
216 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
217
218 ev = consumeEvent();
219 ASSERT_NE(ev, nullptr);
220 ASSERT_EQ(InputEventType::MOTION, ev->getType());
221 mev = static_cast<MotionEvent*>(ev);
222 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
223 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
224 }
225
expectTapWithFlag(int x,int y,int32_t flags)226 void expectTapWithFlag(int x, int y, int32_t flags) {
227 InputEvent* ev = consumeEvent();
228 ASSERT_NE(ev, nullptr);
229 ASSERT_EQ(InputEventType::MOTION, ev->getType());
230 MotionEvent* mev = static_cast<MotionEvent*>(ev);
231 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
232 EXPECT_EQ(x, mev->getX(0));
233 EXPECT_EQ(y, mev->getY(0));
234 EXPECT_EQ(flags, mev->getFlags() & flags);
235
236 ev = consumeEvent();
237 ASSERT_NE(ev, nullptr);
238 ASSERT_EQ(InputEventType::MOTION, ev->getType());
239 mev = static_cast<MotionEvent*>(ev);
240 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
241 EXPECT_EQ(flags, mev->getFlags() & flags);
242 }
243
expectTapInDisplayCoordinates(int displayX,int displayY)244 void expectTapInDisplayCoordinates(int displayX, int displayY) {
245 InputEvent* ev = consumeEvent();
246 ASSERT_NE(ev, nullptr);
247 ASSERT_EQ(InputEventType::MOTION, ev->getType());
248 MotionEvent* mev = static_cast<MotionEvent*>(ev);
249 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
250 const PointerCoords& coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
251 EXPECT_EQ(displayX, coords.getX());
252 EXPECT_EQ(displayY, coords.getY());
253 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
254
255 ev = consumeEvent();
256 ASSERT_NE(ev, nullptr);
257 ASSERT_EQ(InputEventType::MOTION, ev->getType());
258 mev = static_cast<MotionEvent*>(ev);
259 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
260 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
261 }
262
expectKey(int32_t keycode)263 void expectKey(int32_t keycode) {
264 InputEvent* ev = consumeEvent();
265 ASSERT_NE(ev, nullptr);
266 ASSERT_EQ(InputEventType::KEY, ev->getType());
267 KeyEvent* keyEvent = static_cast<KeyEvent*>(ev);
268 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
269 EXPECT_EQ(keycode, keyEvent->getKeyCode());
270 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
271
272 ev = consumeEvent();
273 ASSERT_NE(ev, nullptr);
274 ASSERT_EQ(InputEventType::KEY, ev->getType());
275 keyEvent = static_cast<KeyEvent*>(ev);
276 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
277 EXPECT_EQ(keycode, keyEvent->getKeyCode());
278 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
279 }
280
assertNoEvent()281 void assertNoEvent() {
282 InputEvent* event = consumeEvent(/*timeout=*/100ms);
283 ASSERT_EQ(event, nullptr) << "Expected no event, but got " << *event;
284 }
285
~InputSurface()286 virtual ~InputSurface() {
287 if (mClientChannel) {
288 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
289 }
290 }
291
doTransaction(std::function<void (SurfaceComposerClient::Transaction &,const sp<SurfaceControl> &)> transactionBody)292 virtual void doTransaction(
293 std::function<void(SurfaceComposerClient::Transaction&, const sp<SurfaceControl>&)>
294 transactionBody) {
295 SurfaceComposerClient::Transaction t;
296 transactionBody(t, mSurfaceControl);
297 t.apply(true);
298 }
299
showAt(int x,int y,Rect crop=Rect (0,0,100,100))300 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
301 SurfaceComposerClient::Transaction t;
302 t.show(mSurfaceControl);
303 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
304 t.setLayer(mSurfaceControl, LAYER_BASE);
305 t.setPosition(mSurfaceControl, x, y);
306 t.setCrop(mSurfaceControl, crop);
307 t.setAlpha(mSurfaceControl, 1);
308 auto reportedListener = sp<SynchronousWindowInfosReportedListener>::make();
309 t.addWindowInfosReportedListener(reportedListener);
310 t.apply();
311 reportedListener->wait();
312 }
313
requestFocus(ui::LogicalDisplayId displayId=ui::LogicalDisplayId::DEFAULT)314 void requestFocus(ui::LogicalDisplayId displayId = ui::LogicalDisplayId::DEFAULT) {
315 SurfaceComposerClient::Transaction t;
316 FocusRequest request;
317 request.token = mInputInfo.token;
318 request.windowName = mInputInfo.name;
319 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
320 request.displayId = displayId.val();
321 t.setFocusedWindow(request);
322 t.apply(true);
323 }
324
325 public:
326 sp<SurfaceControl> mSurfaceControl;
327 std::shared_ptr<InputChannel> mClientChannel;
328 sp<IInputFlinger> mInputFlinger;
329
330 WindowInfo mInputInfo;
331
332 PreallocatedInputEventFactory mInputEventFactory;
333 InputConsumer* mInputConsumer;
334 };
335
336 class BlastInputSurface : public InputSurface {
337 public:
BlastInputSurface(const sp<SurfaceControl> & sc,const sp<SurfaceControl> & parentSc,int width,int height)338 BlastInputSurface(const sp<SurfaceControl>& sc, const sp<SurfaceControl>& parentSc, int width,
339 int height)
340 : InputSurface(sc, width, height) {
341 mParentSurfaceControl = parentSc;
342 }
343
344 ~BlastInputSurface() = default;
345
makeBlastInputSurface(const sp<SurfaceComposerClient> & scc,int width,int height)346 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
347 const sp<SurfaceComposerClient>& scc, int width, int height) {
348 sp<SurfaceControl> parentSc =
349 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
350 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
351 ISurfaceComposerClient::eFXSurfaceContainer);
352
353 sp<SurfaceControl> surfaceControl =
354 scc->createSurface(String8("Test Buffer Surface"), width, height,
355 PIXEL_FORMAT_RGBA_8888,
356 ISurfaceComposerClient::eFXSurfaceBufferState,
357 parentSc->getHandle());
358 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
359 }
360
doTransaction(std::function<void (SurfaceComposerClient::Transaction &,const sp<SurfaceControl> &)> transactionBody)361 void doTransaction(
362 std::function<void(SurfaceComposerClient::Transaction&, const sp<SurfaceControl>&)>
363 transactionBody) override {
364 SurfaceComposerClient::Transaction t;
365 transactionBody(t, mParentSurfaceControl);
366 t.apply(true);
367 }
368
showAt(int x,int y,Rect crop=Rect (0,0,100,100))369 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
370 SurfaceComposerClient::Transaction t;
371 t.show(mParentSurfaceControl);
372 t.setLayer(mParentSurfaceControl, LAYER_BASE);
373 t.setPosition(mParentSurfaceControl, x, y);
374 t.setCrop(mParentSurfaceControl, crop);
375
376 t.show(mSurfaceControl);
377 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
378 t.setCrop(mSurfaceControl, crop);
379 t.setAlpha(mSurfaceControl, 1);
380 t.apply(true);
381 }
382
383 private:
384 sp<SurfaceControl> mParentSurfaceControl;
385 };
386
387 class InputSurfacesTest : public ::testing::Test {
388 public:
InputSurfacesTest()389 InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
390
SetUp()391 void SetUp() {
392 mComposerClient = new SurfaceComposerClient;
393 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
394 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
395 ASSERT_FALSE(ids.empty());
396 // display 0 is picked for now, can extend to support all displays if needed
397 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
398 ASSERT_NE(display, nullptr);
399
400 ui::DisplayMode mode;
401 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
402
403 // After a new buffer is queued, SurfaceFlinger is notified and will
404 // latch the new buffer on next vsync. Let's heuristically wait for 3
405 // vsyncs.
406 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.peakRefreshRate) * 3;
407 }
408
TearDown()409 void TearDown() { mComposerClient->dispose(); }
410
makeSurface(int width,int height)411 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
412 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
413 }
414
postBuffer(const sp<SurfaceControl> & layer,int32_t w,int32_t h)415 void postBuffer(const sp<SurfaceControl>& layer, int32_t w, int32_t h) {
416 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
417 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
418 sp<GraphicBuffer> buffer =
419 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
420 Transaction().setBuffer(layer, buffer).apply(true);
421 usleep(mBufferPostDelay);
422 }
423
424 sp<SurfaceComposerClient> mComposerClient;
425 int32_t mBufferPostDelay;
426 };
427
injectTapOnDisplay(int x,int y,ui::LogicalDisplayId displayId)428 void injectTapOnDisplay(int x, int y, ui::LogicalDisplayId displayId) {
429 char *buf1, *buf2, *bufDisplayId;
430 asprintf(&buf1, "%d", x);
431 asprintf(&buf2, "%d", y);
432 asprintf(&bufDisplayId, "%d", displayId.val());
433 if (fork() == 0) {
434 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
435 }
436 }
437
injectTap(int x,int y)438 void injectTap(int x, int y) {
439 injectTapOnDisplay(x, y, ui::LogicalDisplayId::DEFAULT);
440 }
441
injectKeyOnDisplay(uint32_t keycode,ui::LogicalDisplayId displayId)442 void injectKeyOnDisplay(uint32_t keycode, ui::LogicalDisplayId displayId) {
443 char *buf1, *bufDisplayId;
444 asprintf(&buf1, "%d", keycode);
445 asprintf(&bufDisplayId, "%d", displayId.val());
446 if (fork() == 0) {
447 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
448 }
449 }
450
injectKey(uint32_t keycode)451 void injectKey(uint32_t keycode) {
452 injectKeyOnDisplay(keycode, ui::LogicalDisplayId::INVALID);
453 }
454
TEST_F(InputSurfacesTest,can_receive_input)455 TEST_F(InputSurfacesTest, can_receive_input) {
456 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
457 surface->showAt(100, 100);
458
459 injectTap(101, 101);
460
461 EXPECT_NE(surface->consumeEvent(), nullptr);
462 }
463
464 /**
465 * Set up two surfaces side-by-side. Tap each surface.
466 * Next, swap the positions of the two surfaces. Inject tap into the two
467 * original locations. Ensure that the tap is received by the surfaces in the
468 * reverse order.
469 */
TEST_F(InputSurfacesTest,input_respects_positioning)470 TEST_F(InputSurfacesTest, input_respects_positioning) {
471 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
472 surface->showAt(100, 100);
473
474 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
475 surface2->showAt(200, 200);
476
477 injectTap(201, 201);
478 surface2->expectTap(1, 1);
479
480 injectTap(101, 101);
481 surface->expectTap(1, 1);
482
483 surface2->doTransaction([](auto& t, auto& sc) { t.setPosition(sc, 100, 100); });
484 surface->doTransaction([](auto& t, auto& sc) { t.setPosition(sc, 200, 200); });
485
486 injectTap(101, 101);
487 surface2->expectTap(1, 1);
488
489 injectTap(201, 201);
490 surface->expectTap(1, 1);
491 }
492
TEST_F(InputSurfacesTest,input_respects_layering)493 TEST_F(InputSurfacesTest, input_respects_layering) {
494 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
495 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
496
497 surface->showAt(10, 10);
498 surface2->showAt(10, 10);
499
500 surface->doTransaction([](auto& t, auto& sc) { t.setLayer(sc, LAYER_BASE + 1); });
501
502 injectTap(11, 11);
503 surface->expectTap(1, 1);
504
505 surface2->doTransaction([](auto& t, auto& sc) { t.setLayer(sc, LAYER_BASE + 1); });
506
507 injectTap(11, 11);
508 surface2->expectTap(1, 1);
509
510 surface2->doTransaction([](auto& t, auto& sc) { t.hide(sc); });
511
512 injectTap(11, 11);
513 surface->expectTap(1, 1);
514 }
515
516 // Surface Insets are set to offset the client content and draw a border around the client surface
517 // (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
518 // of the client content.
TEST_F(InputSurfacesTest,input_respects_surface_insets)519 TEST_F(InputSurfacesTest, input_respects_surface_insets) {
520 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
521 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
522 bgSurface->showAt(100, 100);
523
524 fgSurface->mInputInfo.surfaceInset = 5;
525 fgSurface->showAt(100, 100);
526
527 injectTap(106, 106);
528 fgSurface->expectTap(1, 1);
529
530 injectTap(101, 101);
531 bgSurface->expectTap(1, 1);
532 }
533
TEST_F(InputSurfacesTest,input_respects_surface_insets_with_replaceTouchableRegionWithCrop)534 TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) {
535 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
536 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
537 bgSurface->showAt(100, 100);
538
539 fgSurface->mInputInfo.surfaceInset = 5;
540 fgSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
541 fgSurface->showAt(100, 100);
542
543 injectTap(106, 106);
544 fgSurface->expectTap(1, 1);
545
546 injectTap(101, 101);
547 bgSurface->expectTap(1, 1);
548 }
549
550 // Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
TEST_F(InputSurfacesTest,input_respects_cropped_surface_insets)551 TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
552 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
553 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
554 parentSurface->showAt(100, 100);
555
556 childSurface->mInputInfo.surfaceInset = 10;
557 childSurface->showAt(100, 100);
558
559 childSurface->doTransaction([&](auto& t, auto& sc) {
560 t.setPosition(sc, -5, -5);
561 t.reparent(sc, parentSurface->mSurfaceControl);
562 });
563
564 injectTap(106, 106);
565 childSurface->expectTap(1, 1);
566
567 injectTap(101, 101);
568 parentSurface->expectTap(1, 1);
569 }
570
571 // Ensure a surface whose insets are scaled, handles the touch offset correctly.
TEST_F(InputSurfacesTest,input_respects_scaled_surface_insets)572 TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
573 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
574 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
575 bgSurface->showAt(100, 100);
576
577 fgSurface->mInputInfo.surfaceInset = 5;
578 fgSurface->showAt(100, 100);
579
580 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
581
582 // expect = touch / scale - inset
583 injectTap(112, 124);
584 fgSurface->expectTap(1, 1);
585
586 injectTap(101, 101);
587 bgSurface->expectTap(1, 1);
588 }
589
TEST_F(InputSurfacesTest,input_respects_scaled_surface_insets_overflow)590 TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
591 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
592 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
593 bgSurface->showAt(100, 100);
594
595 // In case we pass the very big inset without any checking.
596 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
597 fgSurface->showAt(100, 100);
598
599 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
600
601 // expect no crash for overflow, and inset size to be clamped to surface size
602 injectTap(112, 124);
603 bgSurface->expectTap(12, 24);
604 }
605
TEST_F(InputSurfacesTest,touchable_region)606 TEST_F(InputSurfacesTest, touchable_region) {
607 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
608
609 surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31});
610
611 surface->showAt(11, 22);
612
613 // A tap within the surface but outside the touchable region should not be sent to the surface.
614 injectTap(20, 30);
615 EXPECT_EQ(surface->consumeEvent(/*timeout=*/200ms), nullptr);
616
617 injectTap(31, 52);
618 surface->expectTap(20, 30);
619 }
620
TEST_F(InputSurfacesTest,input_respects_touchable_region_offset_overflow)621 TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
622 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
623 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
624 bgSurface->showAt(100, 100);
625
626 // Set the touchable region to the values at the limit of its corresponding type.
627 // Since the surface is offset from the origin, the touchable region will be transformed into
628 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
629 // against such a situation.
630 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
631
632 fgSurface->showAt(100, 100);
633
634 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
635 // surface receives touch.
636 injectTap(112, 124);
637 bgSurface->expectTap(12, 24);
638 }
639
TEST_F(InputSurfacesTest,input_respects_scaled_touchable_region_overflow)640 TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
641 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
642 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
643 bgSurface->showAt(0, 0);
644
645 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
646 fgSurface->showAt(0, 0);
647
648 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
649
650 // Expect no crash for overflow.
651 injectTap(12, 24);
652 bgSurface->expectTap(12, 24);
653 }
654
655 // Ensure we ignore transparent region when getting screen bounds when positioning input frame.
TEST_F(InputSurfacesTest,input_ignores_transparent_region)656 TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
657 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
658 surface->doTransaction([](auto& t, auto& sc) {
659 Region transparentRegion(Rect(0, 0, 10, 10));
660 t.setTransparentRegionHint(sc, transparentRegion);
661 });
662 surface->showAt(100, 100);
663 injectTap(101, 101);
664 surface->expectTap(1, 1);
665 }
666
667 // TODO(b/139494112) update tests once we define expected behavior
668 // Ensure we still send input to the surface regardless of surface visibility changes due to the
669 // first buffer being submitted or alpha changes.
670 // Original bug ref: b/120839715
TEST_F(InputSurfacesTest,input_ignores_buffer_layer_buffer)671 TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
672 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
673 std::unique_ptr<BlastInputSurface> bufferSurface =
674 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
675
676 bgSurface->showAt(10, 10);
677 bufferSurface->showAt(10, 10);
678
679 injectTap(11, 11);
680 bufferSurface->expectTap(1, 1);
681
682 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
683 injectTap(11, 11);
684 bufferSurface->expectTap(1, 1);
685 }
686
TEST_F(InputSurfacesTest,input_respects_buffer_layer_alpha)687 TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
688 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
689 std::unique_ptr<BlastInputSurface> bufferSurface =
690 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
691 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
692
693 bgSurface->showAt(10, 10);
694 bufferSurface->showAt(10, 10);
695
696 injectTap(11, 11);
697 bufferSurface->expectTap(1, 1);
698
699 bufferSurface->doTransaction([](auto& t, auto& sc) { t.setAlpha(sc, 0.0); });
700
701 injectTap(11, 11);
702 bgSurface->expectTap(1, 1);
703 }
704
TEST_F(InputSurfacesTest,input_ignores_color_layer_alpha)705 TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
706 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
707 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
708
709 bgSurface->showAt(10, 10);
710 fgSurface->showAt(10, 10);
711
712 injectTap(11, 11);
713 fgSurface->expectTap(1, 1);
714
715 fgSurface->doTransaction([](auto& t, auto& sc) { t.setAlpha(sc, 0.0); });
716
717 injectTap(11, 11);
718 fgSurface->expectTap(1, 1);
719 }
720
TEST_F(InputSurfacesTest,input_respects_container_layer_visiblity)721 TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
722 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
723 std::unique_ptr<InputSurface> containerSurface =
724 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
725
726 bgSurface->showAt(10, 10);
727 containerSurface->showAt(10, 10);
728
729 injectTap(11, 11);
730 containerSurface->expectTap(1, 1);
731
732 containerSurface->doTransaction([](auto& t, auto& sc) { t.hide(sc); });
733
734 injectTap(11, 11);
735 bgSurface->expectTap(1, 1);
736 }
737
TEST_F(InputSurfacesTest,input_respects_outscreen)738 TEST_F(InputSurfacesTest, input_respects_outscreen) {
739 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
740 surface->showAt(-1, -1);
741
742 injectTap(0, 0);
743 surface->expectTap(1, 1);
744 }
745
TEST_F(InputSurfacesTest,input_ignores_cursor_layer)746 TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
747 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
748 std::unique_ptr<InputSurface> cursorSurface =
749 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
750
751 surface->showAt(10, 10);
752 cursorSurface->showAt(10, 10);
753
754 injectTap(11, 11);
755 surface->expectTap(1, 1);
756 }
757
TEST_F(InputSurfacesTest,can_be_focused)758 TEST_F(InputSurfacesTest, can_be_focused) {
759 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
760 surface->showAt(100, 100);
761 surface->requestFocus();
762
763 surface->assertFocusChange(true);
764
765 injectKey(AKEYCODE_V);
766 surface->expectKey(AKEYCODE_V);
767 }
768
TEST_F(InputSurfacesTest,rotate_surface)769 TEST_F(InputSurfacesTest, rotate_surface) {
770 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
771 surface->showAt(10, 10);
772 surface->doTransaction([](auto& t, auto& sc) {
773 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
774 });
775 injectTap(8, 11);
776 surface->expectTap(1, 2);
777
778 surface->doTransaction([](auto& t, auto& sc) {
779 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
780 });
781 injectTap(9, 8);
782 surface->expectTap(1, 2);
783
784 surface->doTransaction([](auto& t, auto& sc) {
785 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
786 });
787 injectTap(12, 9);
788 surface->expectTap(1, 2);
789 }
790
TEST_F(InputSurfacesTest,rotate_surface_with_scale)791 TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
792 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
793 surface->showAt(10, 10);
794 surface->doTransaction([](auto& t, auto& sc) {
795 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
796 });
797 injectTap(2, 12);
798 surface->expectTap(1, 2);
799
800 surface->doTransaction([](auto& t, auto& sc) {
801 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
802 });
803 injectTap(8, 2);
804 surface->expectTap(1, 2);
805
806 surface->doTransaction([](auto& t, auto& sc) {
807 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
808 });
809 injectTap(18, 8);
810 surface->expectTap(1, 2);
811 }
812
TEST_F(InputSurfacesTest,rotate_surface_with_scale_and_insets)813 TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
814 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
815 surface->mInputInfo.surfaceInset = 5;
816 surface->showAt(100, 100);
817
818 surface->doTransaction([](auto& t, auto& sc) {
819 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
820 });
821 injectTap(40, 120);
822 surface->expectTap(5, 10);
823
824 surface->doTransaction([](auto& t, auto& sc) {
825 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
826 });
827 injectTap(80, 40);
828 surface->expectTap(5, 10);
829
830 surface->doTransaction([](auto& t, auto& sc) {
831 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
832 });
833 injectTap(160, 80);
834 surface->expectTap(5, 10);
835 }
836
TEST_F(InputSurfacesTest,touch_flag_obscured)837 TEST_F(InputSurfacesTest, touch_flag_obscured) {
838 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
839 surface->showAt(100, 100);
840
841 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
842 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
843 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
844 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
845 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
846 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
847 // the default obscured/untrusted touch filter introduced in S.
848 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
849 nonTouchableSurface->showAt(100, 100);
850
851 injectTap(190, 199);
852 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
853 }
854
TEST_F(InputSurfacesTest,touch_flag_partially_obscured_with_crop)855 TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
856 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
857 surface->showAt(100, 100);
858
859 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
860 // that will be tapped. Window behind gets touch, but with flag
861 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
862 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
863 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
864 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
865 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
866 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
867 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
868 nonTouchableSurface->showAt(0, 0);
869 parentSurface->showAt(100, 100);
870
871 nonTouchableSurface->doTransaction([&](auto& t, auto& sc) {
872 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
873 t.reparent(sc, parentSurface->mSurfaceControl);
874 });
875
876 injectTap(190, 199);
877 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
878 }
879
TEST_F(InputSurfacesTest,touch_not_obscured_with_crop)880 TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
881 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
882 surface->showAt(100, 100);
883
884 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
885 // the touchable window. Window behind gets touch with no obscured flags.
886 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
887 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
888 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
889 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
890 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
891 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
892 nonTouchableSurface->showAt(0, 0);
893 parentSurface->showAt(50, 50);
894
895 nonTouchableSurface->doTransaction([&](auto& t, auto& sc) {
896 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
897 t.reparent(sc, parentSurface->mSurfaceControl);
898 });
899
900 injectTap(101, 110);
901 surface->expectTap(1, 10);
902 }
903
TEST_F(InputSurfacesTest,touch_not_obscured_with_zero_sized_bql)904 TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
905 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
906
907 std::unique_ptr<InputSurface> bufferSurface =
908 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
909 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
910 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
911
912 surface->showAt(10, 10);
913 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
914
915 injectTap(11, 11);
916 surface->expectTap(1, 1);
917 }
918
TEST_F(InputSurfacesTest,touch_not_obscured_with_zero_sized_blast)919 TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
920 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
921
922 std::unique_ptr<BlastInputSurface> bufferSurface =
923 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
924 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
925 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
926
927 surface->showAt(10, 10);
928 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
929
930 injectTap(11, 11);
931 surface->expectTap(1, 1);
932 }
933
TEST_F(InputSurfacesTest,strict_unobscured_input_unobscured_window)934 TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
935 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
936 surface->doTransaction(
937 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
938 surface->showAt(100, 100);
939
940 injectTap(101, 101);
941 surface->expectTap(1, 1);
942
943 surface->requestFocus();
944 surface->assertFocusChange(true);
945 injectKey(AKEYCODE_V);
946 surface->expectKey(AKEYCODE_V);
947 }
948
TEST_F(InputSurfacesTest,strict_unobscured_input_scaled_without_crop_window)949 TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
950 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
951 surface->doTransaction([&](auto& t, auto& sc) {
952 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
953 t.setMatrix(sc, 2.0, 0, 0, 2.0);
954 });
955 surface->showAt(100, 100);
956
957 injectTap(101, 101);
958 surface->expectTap(.5, .5);
959
960 surface->requestFocus();
961 surface->assertFocusChange(true);
962 injectKey(AKEYCODE_V);
963 surface->expectKey(AKEYCODE_V);
964 }
965
TEST_F(InputSurfacesTest,strict_unobscured_input_obscured_window)966 TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
967 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
968 surface->mInputInfo.ownerUid = gui::Uid{11111};
969 surface->doTransaction(
970 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
971 surface->showAt(100, 100);
972 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
973 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
974 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
975 obscuringSurface->showAt(100, 100);
976 injectTap(101, 101);
977 surface->assertNoEvent();
978
979 surface->requestFocus();
980 surface->assertFocusChange(true);
981 injectKey(AKEYCODE_V);
982 surface->assertNoEvent();
983 }
984
TEST_F(InputSurfacesTest,strict_unobscured_input_partially_obscured_window)985 TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
986 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
987 surface->mInputInfo.ownerUid = gui::Uid{11111};
988 surface->doTransaction(
989 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
990 surface->showAt(100, 100);
991 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
992 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
993 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
994 obscuringSurface->showAt(190, 190);
995
996 injectTap(101, 101);
997
998 surface->assertNoEvent();
999
1000 surface->requestFocus();
1001 surface->assertFocusChange(true);
1002 injectKey(AKEYCODE_V);
1003 surface->assertNoEvent();
1004 }
1005
TEST_F(InputSurfacesTest,strict_unobscured_input_alpha_window)1006 TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
1007 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1008 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1009
1010 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1011 surface->showAt(100, 100);
1012 surface->doTransaction([&](auto& t, auto& sc) {
1013 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1014 t.reparent(sc, parentSurface->mSurfaceControl);
1015 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
1016 });
1017
1018 injectTap(101, 101);
1019
1020 surface->assertNoEvent();
1021
1022 surface->requestFocus();
1023 surface->assertFocusChange(true);
1024 injectKey(AKEYCODE_V);
1025 surface->assertNoEvent();
1026 }
1027
TEST_F(InputSurfacesTest,strict_unobscured_input_cropped_window)1028 TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
1029 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1030 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1031
1032 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1033 surface->doTransaction([&](auto& t, auto& sc) {
1034 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1035 t.reparent(sc, parentSurface->mSurfaceControl);
1036 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1037 });
1038 surface->showAt(100, 100);
1039
1040 injectTap(111, 111);
1041
1042 surface->assertNoEvent();
1043
1044 surface->requestFocus();
1045 surface->assertFocusChange(true);
1046 injectKey(AKEYCODE_V);
1047 surface->assertNoEvent();
1048 }
1049
TEST_F(InputSurfacesTest,ignore_touch_region_with_zero_sized_blast)1050 TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1051 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1052
1053 std::unique_ptr<BlastInputSurface> bufferSurface =
1054 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1055
1056 surface->showAt(100, 100);
1057 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
1058 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1059
1060 injectTap(101, 101);
1061 surface->expectTap(1, 1);
1062 }
1063
TEST_F(InputSurfacesTest,drop_input_policy)1064 TEST_F(InputSurfacesTest, drop_input_policy) {
1065 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1066 surface->doTransaction(
1067 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
1068 surface->showAt(100, 100);
1069
1070 injectTap(101, 101);
1071 surface->assertNoEvent();
1072
1073 surface->requestFocus();
1074 surface->assertFocusChange(true);
1075 injectKey(AKEYCODE_V);
1076 surface->assertNoEvent();
1077 }
1078
TEST_F(InputSurfacesTest,layer_with_valid_crop_can_be_focused)1079 TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1080 std::unique_ptr<InputSurface> bufferSurface =
1081 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1082
1083 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1084
1085 bufferSurface->requestFocus();
1086 bufferSurface->assertFocusChange(true);
1087 }
1088
1089 /**
1090 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1091 * its own crop.
1092 */
TEST_F(InputSurfacesTest,cropped_container_replaces_touchable_region_with_null_crop)1093 TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1094 std::unique_ptr<InputSurface> parentContainer =
1095 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1096 std::unique_ptr<InputSurface> containerSurface =
1097 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1098 containerSurface->doTransaction(
1099 [&](auto& t, auto& sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1100 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1101 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1102 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1103 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1104
1105 // Receives events inside its own crop
1106 injectTap(21, 21);
1107 containerSurface->expectTap(1, 1); // Event is in layer space
1108
1109 // Does not receive events outside its crop
1110 injectTap(26, 26);
1111 containerSurface->assertNoEvent();
1112 }
1113
1114 /**
1115 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1116 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1117 */
TEST_F(InputSurfacesTest,uncropped_container_replaces_touchable_region_with_null_crop)1118 TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
1119 std::unique_ptr<InputSurface> parentContainer =
1120 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1121 std::unique_ptr<InputSurface> containerSurface =
1122 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1123 containerSurface->doTransaction(
1124 [&](auto& t, auto& sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1125 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1126 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1127 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1128 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1129
1130 // Receives events inside parent bounds
1131 injectTap(21, 21);
1132 containerSurface->expectTap(1, 1); // Event is in layer space
1133
1134 // Does not receive events outside parent bounds
1135 injectTap(31, 31);
1136 containerSurface->assertNoEvent();
1137 }
1138
1139 /**
1140 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1141 * layer's bounds. The input events should be in the layer's coordinate space.
1142 */
TEST_F(InputSurfacesTest,replace_touchable_region_with_crop)1143 TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1144 std::unique_ptr<InputSurface> cropLayer =
1145 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1146 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1147
1148 std::unique_ptr<InputSurface> containerSurface =
1149 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1150 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1151 containerSurface->mInputInfo.touchableRegionCropHandle =
1152 cropLayer->mSurfaceControl->getHandle();
1153 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1154
1155 // Receives events inside crop layer bounds
1156 injectTap(51, 51);
1157 containerSurface->expectTap(41, 41); // Event is in layer space
1158
1159 // Does not receive events outside crop layer bounds
1160 injectTap(21, 21);
1161 injectTap(71, 71);
1162 containerSurface->assertNoEvent();
1163 }
1164
TEST_F(InputSurfacesTest,child_container_with_no_input_channel_blocks_parent)1165 TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1166 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1167
1168 parent->showAt(100, 100);
1169 injectTap(101, 101);
1170 parent->expectTap(1, 1);
1171
1172 std::unique_ptr<InputSurface> childContainerSurface =
1173 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1174 childContainerSurface->showAt(0, 0);
1175 childContainerSurface->doTransaction(
1176 [&](auto& t, auto& sc) { t.reparent(sc, parent->mSurfaceControl); });
1177 injectTap(101, 101);
1178
1179 parent->assertNoEvent();
1180 }
1181
1182 class MultiDisplayTests : public InputSurfacesTest {
1183 public:
MultiDisplayTests()1184 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
1185
TearDown()1186 void TearDown() override {
1187 std::for_each(mVirtualDisplays.begin(), mVirtualDisplays.end(),
1188 SurfaceComposerClient::destroyVirtualDisplay);
1189 InputSurfacesTest::TearDown();
1190 }
1191
createDisplay(int32_t width,int32_t height,bool isSecure,ui::LayerStack layerStack,bool receivesInput=true,int32_t offsetX=0,int32_t offsetY=0)1192 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1193 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
1194 sp<IGraphicBufferConsumer> consumer;
1195 sp<IGraphicBufferProducer> producer;
1196 BufferQueue::createBufferQueue(&producer, &consumer);
1197 consumer->setConsumerName(String8("Virtual disp consumer"));
1198 consumer->setDefaultBufferSize(width, height);
1199 mProducers.push_back(producer);
1200
1201 std::string name = "VirtualDisplay";
1202 name += std::to_string(mVirtualDisplays.size());
1203 sp<IBinder> token = SurfaceComposerClient::createVirtualDisplay(name, isSecure);
1204 SurfaceComposerClient::Transaction t;
1205 t.setDisplaySurface(token, producer);
1206 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1207 t.setDisplayLayerStack(token, layerStack);
1208 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1209 {offsetX, offsetY, offsetX + width, offsetY + height});
1210 t.apply(true);
1211
1212 mVirtualDisplays.push_back(token);
1213 }
1214
1215 std::vector<sp<IBinder>> mVirtualDisplays;
1216 std::vector<sp<IGraphicBufferProducer>> mProducers;
1217 };
1218
TEST_F(MultiDisplayTests,drop_touch_if_layer_on_invalid_display)1219 TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
1220 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1221 // Do not create a display associated with the LayerStack.
1222 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1223 surface->doTransaction([&](auto& t, auto& sc) { t.setLayerStack(sc, layerStack); });
1224 surface->showAt(100, 100);
1225
1226 // Touches should be dropped if the layer is on an invalid display.
1227 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
1228 surface->assertNoEvent();
1229
1230 // However, we still let the window be focused and receive keys.
1231 surface->requestFocus(toDisplayId(layerStack));
1232 surface->assertFocusChange(true);
1233
1234 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
1235 surface->expectKey(AKEYCODE_V);
1236 }
1237
TEST_F(MultiDisplayTests,virtual_display_receives_input)1238 TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1239 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1240 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1241 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1242 surface->doTransaction([&](auto& t, auto& sc) { t.setLayerStack(sc, layerStack); });
1243 surface->showAt(100, 100);
1244
1245 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
1246 surface->expectTap(1, 1);
1247
1248 surface->requestFocus(toDisplayId(layerStack));
1249 surface->assertFocusChange(true);
1250 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
1251 surface->expectKey(AKEYCODE_V);
1252 }
1253
TEST_F(MultiDisplayTests,drop_input_for_secure_layer_on_nonsecure_display)1254 TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1255 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1256 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1257 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1258 surface->doTransaction([&](auto& t, auto& sc) {
1259 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1260 t.setLayerStack(sc, layerStack);
1261 });
1262 surface->showAt(100, 100);
1263
1264 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
1265
1266 surface->assertNoEvent();
1267
1268 surface->requestFocus(toDisplayId(layerStack));
1269 surface->assertFocusChange(true);
1270 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
1271 surface->assertNoEvent();
1272 }
1273
TEST_F(MultiDisplayTests,dont_drop_input_for_secure_layer_on_secure_display)1274 TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1275 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1276
1277 // Create the secure display as system, because only certain users can create secure displays.
1278 seteuid(AID_SYSTEM);
1279 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
1280 // Change the uid back to root.
1281 seteuid(AID_ROOT);
1282
1283 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1284 surface->doTransaction([&](auto& t, auto& sc) {
1285 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1286 t.setLayerStack(sc, layerStack);
1287 });
1288 surface->showAt(100, 100);
1289
1290 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
1291 surface->expectTap(1, 1);
1292
1293 surface->requestFocus(toDisplayId(layerStack));
1294 surface->assertFocusChange(true);
1295 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
1296
1297 surface->expectKey(AKEYCODE_V);
1298 }
1299
1300 } // namespace test
1301 } // namespace android
1302