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