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