blob: 16208a7773b1fec093517baf2918cbf91a57c80b [file] [log] [blame]
Robert Carr1c4c5592018-09-24 13:18:43 -07001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <sys/time.h>
21#include <sys/types.h>
22#include <stdio.h>
23#include <poll.h>
24
25#include <memory>
26
Vishnu Naira066d902021-09-13 18:40:17 -070027#include <android/keycodes.h>
Vishnu Nairde19f852018-12-18 16:11:53 -080028#include <android/native_window.h>
29
Robert Carr1c4c5592018-09-24 13:18:43 -070030#include <binder/Binder.h>
31#include <binder/IServiceManager.h>
32#include <binder/Parcel.h>
33#include <binder/ProcessState.h>
34
Vishnu Nairde19f852018-12-18 16:11:53 -080035#include <gui/ISurfaceComposer.h>
36#include <gui/Surface.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070037#include <gui/SurfaceComposerClient.h>
38#include <gui/SurfaceControl.h>
39
Chris Ye0783e992020-06-02 21:34:49 -070040#include <android/os/IInputFlinger.h>
chaviw98318de2021-05-19 16:45:23 -050041#include <gui/WindowInfo.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070042#include <input/Input.h>
Chris Ye0783e992020-06-02 21:34:49 -070043#include <input/InputTransport.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070044
Marin Shalamanova7fe3042021-01-29 21:02:08 +010045#include <ui/DisplayMode.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070046#include <ui/Rect.h>
47#include <ui/Region.h>
48
Chris Ye0783e992020-06-02 21:34:49 -070049using android::os::IInputFlinger;
Robert Carr1c4c5592018-09-24 13:18:43 -070050
chaviw39d01472021-04-08 14:26:24 -050051using android::hardware::graphics::common::V1_1::BufferUsage;
52
chaviw98318de2021-05-19 16:45:23 -050053using android::gui::FocusRequest;
54using android::gui::InputApplicationInfo;
55using android::gui::TouchOcclusionMode;
56using android::gui::WindowInfo;
57
Vishnu Nair958da932020-08-21 17:12:37 -070058namespace android::test {
Robert Carr1c4c5592018-09-24 13:18:43 -070059
Vishnu Nairde19f852018-12-18 16:11:53 -080060using Transaction = SurfaceComposerClient::Transaction;
61
Robert Carr1c4c5592018-09-24 13:18:43 -070062sp<IInputFlinger> getInputFlinger() {
63 sp<IBinder> input(defaultServiceManager()->getService(
64 String16("inputflinger")));
65 if (input == nullptr) {
66 ALOGE("Failed to link to input service");
67 } else { ALOGE("Linked to input"); }
68 return interface_cast<IInputFlinger>(input);
69}
70
71// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
72static const int LAYER_BASE = INT32_MAX - 10;
Chris Ye6c4243b2020-07-22 12:07:12 -070073static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070074
75class InputSurface {
76public:
Vishnu Nairde19f852018-12-18 16:11:53 -080077 InputSurface(const sp<SurfaceControl> &sc, int width, int height) {
78 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -070079
80 mInputFlinger = getInputFlinger();
Garfield Tan15601662020-09-22 15:32:38 -070081 mClientChannel = std::make_shared<InputChannel>();
82 mInputFlinger->createInputChannel("testchannels", mClientChannel.get());
Robert Carr1c4c5592018-09-24 13:18:43 -070083
84 populateInputInfo(width, height);
85
86 mInputConsumer = new InputConsumer(mClientChannel);
87 }
88
Vishnu Nairde19f852018-12-18 16:11:53 -080089 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
90 int width, int height) {
91 sp<SurfaceControl> surfaceControl =
92 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -080093 PIXEL_FORMAT_RGBA_8888,
94 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -080095 return std::make_unique<InputSurface>(surfaceControl, width, height);
96 }
97
98 static std::unique_ptr<InputSurface> makeBufferInputSurface(
99 const sp<SurfaceComposerClient> &scc, int width, int height) {
100 sp<SurfaceControl> surfaceControl =
101 scc->createSurface(String8("Test Buffer Surface"), width, height,
102 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
103 return std::make_unique<InputSurface>(surfaceControl, width, height);
104 }
105
106 static std::unique_ptr<InputSurface> makeContainerInputSurface(
107 const sp<SurfaceComposerClient> &scc, int width, int height) {
108 sp<SurfaceControl> surfaceControl =
109 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
110 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
111 ISurfaceComposerClient::eFXSurfaceContainer);
112 return std::make_unique<InputSurface>(surfaceControl, width, height);
113 }
114
arthurhungb4a0f852020-06-16 11:02:50 +0800115 static std::unique_ptr<InputSurface> makeCursorInputSurface(
116 const sp<SurfaceComposerClient> &scc, int width, int height) {
117 sp<SurfaceControl> surfaceControl =
118 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
119 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
120 ISurfaceComposerClient::eCursorWindow);
121 return std::make_unique<InputSurface>(surfaceControl, width, height);
122 }
123
Vishnu Naira066d902021-09-13 18:40:17 -0700124 InputEvent *consumeEvent(int timeoutMs = 3000) {
125 waitForEventAvailable(timeoutMs);
Robert Carr1c4c5592018-09-24 13:18:43 -0700126
127 InputEvent *ev;
128 uint32_t seqId;
129 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
130 if (consumed != OK) {
131 return nullptr;
132 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100133 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
134 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700135 return ev;
136 }
137
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100138 void assertFocusChange(bool hasFocus) {
139 InputEvent *ev = consumeEvent();
140 ASSERT_NE(ev, nullptr);
141 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, ev->getType());
142 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
143 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
144 }
145
Robert Carr1c4c5592018-09-24 13:18:43 -0700146 void expectTap(int x, int y) {
147 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100148 ASSERT_NE(ev, nullptr);
149 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700150 MotionEvent* mev = static_cast<MotionEvent*>(ev);
151 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
152 EXPECT_EQ(x, mev->getX(0));
153 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800154 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700155
156 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100157 ASSERT_NE(ev, nullptr);
158 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700159 mev = static_cast<MotionEvent*>(ev);
160 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800161 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700162 }
163
chaviw39cfa2e2020-11-04 14:19:02 -0800164 void expectTapWithFlag(int x, int y, int32_t flags) {
165 InputEvent *ev = consumeEvent();
166 ASSERT_NE(ev, nullptr);
167 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
168 MotionEvent *mev = static_cast<MotionEvent *>(ev);
169 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
170 EXPECT_EQ(x, mev->getX(0));
171 EXPECT_EQ(y, mev->getY(0));
172 EXPECT_EQ(flags, mev->getFlags() & flags);
173
174 ev = consumeEvent();
175 ASSERT_NE(ev, nullptr);
176 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
177 mev = static_cast<MotionEvent *>(ev);
178 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
179 EXPECT_EQ(flags, mev->getFlags() & flags);
180 }
181
Vishnu Naira066d902021-09-13 18:40:17 -0700182 void expectKey(uint32_t keycode) {
183 InputEvent *ev = consumeEvent();
184 ASSERT_NE(ev, nullptr);
185 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, ev->getType());
186 KeyEvent *keyEvent = static_cast<KeyEvent *>(ev);
187 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
188 EXPECT_EQ(keycode, keyEvent->getKeyCode());
189 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
190
191 ev = consumeEvent();
192 ASSERT_NE(ev, nullptr);
193 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, ev->getType());
194 keyEvent = static_cast<KeyEvent *>(ev);
195 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
196 EXPECT_EQ(keycode, keyEvent->getKeyCode());
197 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
198 }
199
chaviw39d01472021-04-08 14:26:24 -0500200 virtual ~InputSurface() {
201 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
202 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700203
chaviw39d01472021-04-08 14:26:24 -0500204 virtual void doTransaction(
205 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
206 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700207 SurfaceComposerClient::Transaction t;
208 transactionBody(t, mSurfaceControl);
209 t.apply(true);
210 }
211
chaviw39d01472021-04-08 14:26:24 -0500212 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700213 SurfaceComposerClient::Transaction t;
214 t.show(mSurfaceControl);
215 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
216 t.setLayer(mSurfaceControl, LAYER_BASE);
217 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800218 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700219 t.setAlpha(mSurfaceControl, 1);
220 t.apply(true);
221 }
222
Vishnu Nair16a938f2021-09-24 07:14:54 -0700223 void requestFocus(int displayId = ADISPLAY_ID_DEFAULT) {
Vishnu Nair958da932020-08-21 17:12:37 -0700224 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800225 FocusRequest request;
226 request.token = mInputInfo.token;
227 request.windowName = mInputInfo.name;
228 request.focusedToken = nullptr;
229 request.focusedWindowName = "";
230 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700231 request.displayId = displayId;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800232 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700233 t.apply(true);
234 }
235
Robert Carr1c4c5592018-09-24 13:18:43 -0700236private:
Vishnu Naira066d902021-09-13 18:40:17 -0700237 void waitForEventAvailable(int timeoutMs) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700238 struct pollfd fd;
239
240 fd.fd = mClientChannel->getFd();
241 fd.events = POLLIN;
Vishnu Naira066d902021-09-13 18:40:17 -0700242 poll(&fd, 1, timeoutMs);
Robert Carr1c4c5592018-09-24 13:18:43 -0700243 }
244
245 void populateInputInfo(int width, int height) {
Garfield Tan15601662020-09-22 15:32:38 -0700246 mInputInfo.token = mClientChannel->getConnectionToken();
Robert Carr1c4c5592018-09-24 13:18:43 -0700247 mInputInfo.name = "Test info";
chaviw98318de2021-05-19 16:45:23 -0500248 mInputInfo.flags = WindowInfo::Flag::NOT_TOUCH_MODAL;
249 mInputInfo.type = WindowInfo::Type::BASE_APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500250 mInputInfo.dispatchingTimeout = 5s;
Robert Carre07e1032018-11-26 12:55:53 -0800251 mInputInfo.globalScaleFactor = 1.0;
Vishnu Nair47074b82020-08-14 11:54:47 -0700252 mInputInfo.focusable = true;
Robert Carr1c4c5592018-09-24 13:18:43 -0700253 mInputInfo.hasWallpaper = false;
254 mInputInfo.paused = false;
255
256 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
257
Robert Carr740167f2018-10-11 19:03:41 -0700258 InputApplicationInfo aInfo;
259 aInfo.token = new BBinder();
260 aInfo.name = "Test app info";
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500261 aInfo.dispatchingTimeoutMillis =
262 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Robert Carr740167f2018-10-11 19:03:41 -0700263
264 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700265 }
266public:
267 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500268 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700269 sp<IInputFlinger> mInputFlinger;
270
chaviw98318de2021-05-19 16:45:23 -0500271 WindowInfo mInputInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700272
273 PreallocatedInputEventFactory mInputEventFactory;
274 InputConsumer* mInputConsumer;
275};
276
chaviw39d01472021-04-08 14:26:24 -0500277class BlastInputSurface : public InputSurface {
278public:
279 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
280 int height)
281 : InputSurface(sc, width, height) {
282 mParentSurfaceControl = parentSc;
283 }
284
285 ~BlastInputSurface() = default;
286
287 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
288 const sp<SurfaceComposerClient> &scc, int width, int height) {
289 sp<SurfaceControl> parentSc =
290 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
291 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
292 ISurfaceComposerClient::eFXSurfaceContainer);
293
294 sp<SurfaceControl> surfaceControl =
295 scc->createSurface(String8("Test Buffer Surface"), width, height,
296 PIXEL_FORMAT_RGBA_8888,
297 ISurfaceComposerClient::eFXSurfaceBufferState,
298 parentSc->getHandle());
299 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
300 }
301
302 void doTransaction(
303 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
304 transactionBody) override {
305 SurfaceComposerClient::Transaction t;
306 transactionBody(t, mParentSurfaceControl);
307 t.apply(true);
308 }
309
310 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
311 SurfaceComposerClient::Transaction t;
312 t.show(mParentSurfaceControl);
313 t.setLayer(mParentSurfaceControl, LAYER_BASE);
314 t.setPosition(mParentSurfaceControl, x, y);
315 t.setCrop(mParentSurfaceControl, crop);
316
317 t.show(mSurfaceControl);
318 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
319 t.setCrop(mSurfaceControl, crop);
320 t.setAlpha(mSurfaceControl, 1);
321 t.apply(true);
322 }
323
324private:
325 sp<SurfaceControl> mParentSurfaceControl;
326};
327
Robert Carr1c4c5592018-09-24 13:18:43 -0700328class InputSurfacesTest : public ::testing::Test {
329public:
330 InputSurfacesTest() {
331 ProcessState::self()->startThreadPool();
332 }
333
334 void SetUp() {
335 mComposerClient = new SurfaceComposerClient;
336 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Vishnu Nairde19f852018-12-18 16:11:53 -0800337
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800338 const auto display = mComposerClient->getInternalDisplayToken();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100339 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800340
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100341 ui::DisplayMode mode;
342 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800343
344 // After a new buffer is queued, SurfaceFlinger is notified and will
345 // latch the new buffer on next vsync. Let's heuristically wait for 3
346 // vsyncs.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100347 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.refreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700348 }
349
350 void TearDown() {
351 mComposerClient->dispose();
352 }
353
354 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800355 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
356 }
357
chaviw39d01472021-04-08 14:26:24 -0500358 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
359 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
360 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
361 sp<GraphicBuffer> buffer =
362 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
363 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800364 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700365 }
366
367 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800368 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700369};
370
Vishnu Nair16a938f2021-09-24 07:14:54 -0700371void injectTapOnDisplay(int x, int y, int displayId) {
372 char *buf1, *buf2, *bufDisplayId;
Robert Carr1c4c5592018-09-24 13:18:43 -0700373 asprintf(&buf1, "%d", x);
374 asprintf(&buf2, "%d", y);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700375 asprintf(&bufDisplayId, "%d", displayId);
Robert Carr1c4c5592018-09-24 13:18:43 -0700376 if (fork() == 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700377 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
378 }
379}
380
381void injectTap(int x, int y) {
382 injectTapOnDisplay(x, y, ADISPLAY_ID_DEFAULT);
383}
384
385void injectKeyOnDisplay(uint32_t keycode, int displayId) {
386 char *buf1, *bufDisplayId;
387 asprintf(&buf1, "%d", keycode);
388 asprintf(&bufDisplayId, "%d", displayId);
389 if (fork() == 0) {
390 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
Robert Carr1c4c5592018-09-24 13:18:43 -0700391 }
392}
393
Vishnu Naira066d902021-09-13 18:40:17 -0700394void injectKey(uint32_t keycode) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700395 injectKeyOnDisplay(keycode, ADISPLAY_ID_NONE);
Vishnu Naira066d902021-09-13 18:40:17 -0700396}
397
Robert Carr1c4c5592018-09-24 13:18:43 -0700398TEST_F(InputSurfacesTest, can_receive_input) {
399 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
400 surface->showAt(100, 100);
401
402 injectTap(101, 101);
403
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100404 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700405}
406
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100407/**
408 * Set up two surfaces side-by-side. Tap each surface.
409 * Next, swap the positions of the two surfaces. Inject tap into the two
410 * original locations. Ensure that the tap is received by the surfaces in the
411 * reverse order.
412 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700413TEST_F(InputSurfacesTest, input_respects_positioning) {
414 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
415 surface->showAt(100, 100);
416
417 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
418 surface2->showAt(200, 200);
419
420 injectTap(201, 201);
421 surface2->expectTap(1, 1);
422
423 injectTap(101, 101);
424 surface->expectTap(1, 1);
425
426 surface2->doTransaction([](auto &t, auto &sc) {
427 t.setPosition(sc, 100, 100);
428 });
429 surface->doTransaction([](auto &t, auto &sc) {
430 t.setPosition(sc, 200, 200);
431 });
432
433 injectTap(101, 101);
434 surface2->expectTap(1, 1);
435
436 injectTap(201, 201);
437 surface->expectTap(1, 1);
438}
439
440TEST_F(InputSurfacesTest, input_respects_layering) {
441 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
442 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
443
444 surface->showAt(10, 10);
445 surface2->showAt(10, 10);
446
447 surface->doTransaction([](auto &t, auto &sc) {
448 t.setLayer(sc, LAYER_BASE + 1);
449 });
450
451 injectTap(11, 11);
452 surface->expectTap(1, 1);
453
454 surface2->doTransaction([](auto &t, auto &sc) {
455 t.setLayer(sc, LAYER_BASE + 1);
456 });
457
458 injectTap(11, 11);
459 surface2->expectTap(1, 1);
460
461 surface2->doTransaction([](auto &t, auto &sc) {
462 t.hide(sc);
463 });
464
465 injectTap(11, 11);
466 surface->expectTap(1, 1);
467}
468
Vishnu Nairde19f852018-12-18 16:11:53 -0800469// Surface Insets are set to offset the client content and draw a border around the client surface
470// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
471// of the client content.
472TEST_F(InputSurfacesTest, input_respects_surface_insets) {
473 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
474 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
475 bgSurface->showAt(100, 100);
476
477 fgSurface->mInputInfo.surfaceInset = 5;
478 fgSurface->showAt(100, 100);
479
480 injectTap(106, 106);
481 fgSurface->expectTap(1, 1);
482
483 injectTap(101, 101);
484 bgSurface->expectTap(1, 1);
485}
486
487// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
488TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
489 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
490 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
491 parentSurface->showAt(100, 100);
492
493 childSurface->mInputInfo.surfaceInset = 10;
494 childSurface->showAt(100, 100);
495
496 childSurface->doTransaction([&](auto &t, auto &sc) {
497 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000498 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800499 });
500
501 injectTap(106, 106);
502 childSurface->expectTap(1, 1);
503
504 injectTap(101, 101);
505 parentSurface->expectTap(1, 1);
506}
507
Arthur Hung118b1142019-05-08 21:25:59 +0800508// Ensure a surface whose insets are scaled, handles the touch offset correctly.
509TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
510 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
511 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
512 bgSurface->showAt(100, 100);
513
514 fgSurface->mInputInfo.surfaceInset = 5;
515 fgSurface->showAt(100, 100);
516
517 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
518
519 // expect = touch / scale - inset
520 injectTap(112, 124);
521 fgSurface->expectTap(1, 1);
522
523 injectTap(101, 101);
524 bgSurface->expectTap(1, 1);
525}
526
Ady Abraham282f1d72019-07-24 18:05:56 -0700527TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
528 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
529 // In case we pass the very big inset without any checking.
530 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
531 fgSurface->showAt(100, 100);
532
533 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
534
535 // expect no crash for overflow, and inset size to be clamped to surface size
536 injectTap(202, 202);
537 fgSurface->expectTap(1, 1);
538}
539
Vishnu Nairde19f852018-12-18 16:11:53 -0800540// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
541TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
542 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
543 surface->doTransaction([](auto &t, auto &sc) {
544 Region transparentRegion(Rect(0, 0, 10, 10));
545 t.setTransparentRegionHint(sc, transparentRegion);
546 });
547 surface->showAt(100, 100);
548 injectTap(101, 101);
549 surface->expectTap(1, 1);
550}
551
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700552// TODO(b/139494112) update tests once we define expected behavior
553// Ensure we still send input to the surface regardless of surface visibility changes due to the
554// first buffer being submitted or alpha changes.
555// Original bug ref: b/120839715
556TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800557 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500558 std::unique_ptr<BlastInputSurface> bufferSurface =
559 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800560
561 bgSurface->showAt(10, 10);
562 bufferSurface->showAt(10, 10);
563
564 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700565 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800566
chaviw39d01472021-04-08 14:26:24 -0500567 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800568 injectTap(11, 11);
569 bufferSurface->expectTap(1, 1);
570}
571
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700572TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800573 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500574 std::unique_ptr<BlastInputSurface> bufferSurface =
575 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
576 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800577
578 bgSurface->showAt(10, 10);
579 bufferSurface->showAt(10, 10);
580
581 injectTap(11, 11);
582 bufferSurface->expectTap(1, 1);
583
584 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
585
586 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700587 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800588}
589
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700590TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800591 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
592 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
593
594 bgSurface->showAt(10, 10);
595 fgSurface->showAt(10, 10);
596
597 injectTap(11, 11);
598 fgSurface->expectTap(1, 1);
599
600 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
601
602 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700603 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800604}
605
606TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
607 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
608 std::unique_ptr<InputSurface> containerSurface =
609 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
610
611 bgSurface->showAt(10, 10);
612 containerSurface->showAt(10, 10);
613
614 injectTap(11, 11);
615 containerSurface->expectTap(1, 1);
616
617 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
618
619 injectTap(11, 11);
620 bgSurface->expectTap(1, 1);
621}
chaviwfbe5d9c2018-12-26 12:23:37 -0800622
Arthur Hungd20b2702019-01-14 18:16:16 +0800623TEST_F(InputSurfacesTest, input_respects_outscreen) {
624 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
625 surface->showAt(-1, -1);
626
627 injectTap(0, 0);
628 surface->expectTap(1, 1);
629}
arthurhungb4a0f852020-06-16 11:02:50 +0800630
631TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
632 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
633 std::unique_ptr<InputSurface> cursorSurface =
634 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
635
636 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800637 cursorSurface->showAt(10, 10);
638
639 injectTap(11, 11);
640 surface->expectTap(1, 1);
641}
Vishnu Nair958da932020-08-21 17:12:37 -0700642
643TEST_F(InputSurfacesTest, can_be_focused) {
644 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
645 surface->showAt(100, 100);
646 surface->requestFocus();
647
648 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700649
650 injectKey(AKEYCODE_V);
651 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700652}
chaviw44a6d2b2020-09-08 17:14:16 -0700653
654TEST_F(InputSurfacesTest, rotate_surface) {
655 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
656 surface->showAt(10, 10);
657 surface->doTransaction([](auto &t, auto &sc) {
658 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
659 });
660 injectTap(8, 11);
661 surface->expectTap(1, 2);
662
663 surface->doTransaction([](auto &t, auto &sc) {
664 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
665 });
666 injectTap(9, 8);
667 surface->expectTap(1, 2);
668
669 surface->doTransaction([](auto &t, auto &sc) {
670 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
671 });
672 injectTap(12, 9);
673 surface->expectTap(1, 2);
674}
675
676TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
677 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
678 surface->showAt(10, 10);
679 surface->doTransaction([](auto &t, auto &sc) {
680 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
681 });
682 injectTap(2, 12);
683 surface->expectTap(1, 2);
684
685 surface->doTransaction([](auto &t, auto &sc) {
686 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
687 });
688 injectTap(8, 2);
689 surface->expectTap(1, 2);
690
691 surface->doTransaction([](auto &t, auto &sc) {
692 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
693 });
694 injectTap(18, 8);
695 surface->expectTap(1, 2);
696}
697
698TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
699 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
700 surface->mInputInfo.surfaceInset = 5;
701 surface->showAt(100, 100);
702
703 surface->doTransaction([](auto &t, auto &sc) {
704 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
705 });
706 injectTap(40, 120);
707 surface->expectTap(5, 10);
708
709 surface->doTransaction([](auto &t, auto &sc) {
710 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
711 });
712 injectTap(80, 40);
713 surface->expectTap(5, 10);
714
715 surface->doTransaction([](auto &t, auto &sc) {
716 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
717 });
718 injectTap(160, 80);
719 surface->expectTap(5, 10);
720}
721
chaviw39cfa2e2020-11-04 14:19:02 -0800722TEST_F(InputSurfacesTest, touch_flag_obscured) {
723 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
724 surface->showAt(100, 100);
725
726 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
727 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
728 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
chaviw98318de2021-05-19 16:45:23 -0500729 nonTouchableSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw39cfa2e2020-11-04 14:19:02 -0800730 nonTouchableSurface->mInputInfo.ownerUid = 22222;
Bernardo Rufino602ef712020-12-21 11:02:18 +0000731 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
732 // the default obscured/untrusted touch filter introduced in S.
733 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800734 nonTouchableSurface->showAt(100, 100);
735
736 injectTap(190, 199);
737 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
738}
739
740TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
741 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
742 surface->showAt(100, 100);
743
744 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
745 // that will be tapped. Window behind gets touch, but with flag
746 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
747 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
748 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
chaviw98318de2021-05-19 16:45:23 -0500749 nonTouchableSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
750 parentSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw39cfa2e2020-11-04 14:19:02 -0800751 nonTouchableSurface->mInputInfo.ownerUid = 22222;
752 parentSurface->mInputInfo.ownerUid = 22222;
753 nonTouchableSurface->showAt(0, 0);
754 parentSurface->showAt(100, 100);
755
756 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800757 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800758 t.reparent(sc, parentSurface->mSurfaceControl);
759 });
760
761 injectTap(190, 199);
762 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
763}
764
765TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
766 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
767 surface->showAt(100, 100);
768
769 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
770 // the touchable window. Window behind gets touch with no obscured flags.
771 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
772 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
chaviw98318de2021-05-19 16:45:23 -0500773 nonTouchableSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
774 parentSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw39cfa2e2020-11-04 14:19:02 -0800775 nonTouchableSurface->mInputInfo.ownerUid = 22222;
776 parentSurface->mInputInfo.ownerUid = 22222;
777 nonTouchableSurface->showAt(0, 0);
778 parentSurface->showAt(50, 50);
779
780 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800781 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800782 t.reparent(sc, parentSurface->mSurfaceControl);
783 });
784
785 injectTap(101, 110);
786 surface->expectTap(1, 10);
787}
788
chaviw7e72caf2020-12-02 16:50:43 -0800789TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
790 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
791
792 std::unique_ptr<InputSurface> bufferSurface =
793 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
chaviw98318de2021-05-19 16:45:23 -0500794 bufferSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw7e72caf2020-12-02 16:50:43 -0800795 bufferSurface->mInputInfo.ownerUid = 22222;
796
797 surface->showAt(10, 10);
798 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
799
800 injectTap(11, 11);
801 surface->expectTap(1, 1);
802}
803
804TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
805 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
806
chaviw39d01472021-04-08 14:26:24 -0500807 std::unique_ptr<BlastInputSurface> bufferSurface =
808 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
chaviw98318de2021-05-19 16:45:23 -0500809 bufferSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
chaviw7e72caf2020-12-02 16:50:43 -0800810 bufferSurface->mInputInfo.ownerUid = 22222;
811
812 surface->showAt(10, 10);
813 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
814
815 injectTap(11, 11);
816 surface->expectTap(1, 1);
817}
818
Vishnu Naira066d902021-09-13 18:40:17 -0700819TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
820 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
821 surface->doTransaction(
822 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
823 surface->showAt(100, 100);
824
825 injectTap(101, 101);
826
827 EXPECT_NE(surface->consumeEvent(), nullptr);
828 EXPECT_NE(surface->consumeEvent(), nullptr);
829
830 surface->requestFocus();
831 surface->assertFocusChange(true);
832 injectKey(AKEYCODE_V);
833 surface->expectKey(AKEYCODE_V);
834}
835
836TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
837 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
838 surface->doTransaction([&](auto &t, auto &sc) {
839 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
840 t.setMatrix(sc, 2.0, 0, 0, 2.0);
841 });
842 surface->showAt(100, 100);
843
844 injectTap(101, 101);
845
846 EXPECT_NE(surface->consumeEvent(), nullptr);
847 EXPECT_NE(surface->consumeEvent(), nullptr);
848
849 surface->requestFocus();
850 surface->assertFocusChange(true);
851 injectKey(AKEYCODE_V);
852 surface->expectKey(AKEYCODE_V);
853}
854
855TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
856 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
857 surface->mInputInfo.ownerUid = 11111;
858 surface->doTransaction(
859 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
860 surface->showAt(100, 100);
861 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
862 obscuringSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
863 obscuringSurface->mInputInfo.ownerUid = 22222;
864 obscuringSurface->showAt(100, 100);
865 injectTap(101, 101);
866 EXPECT_EQ(surface->consumeEvent(100), nullptr);
867
868 surface->requestFocus();
869 surface->assertFocusChange(true);
870 injectKey(AKEYCODE_V);
871 EXPECT_EQ(surface->consumeEvent(100), nullptr);
872}
873
874TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
875 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
876 surface->mInputInfo.ownerUid = 11111;
877 surface->doTransaction(
878 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
879 surface->showAt(100, 100);
880 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
881 obscuringSurface->mInputInfo.flags = WindowInfo::Flag::NOT_TOUCHABLE;
882 obscuringSurface->mInputInfo.ownerUid = 22222;
883 obscuringSurface->showAt(190, 190);
884
885 injectTap(101, 101);
886
887 EXPECT_EQ(surface->consumeEvent(100), nullptr);
888
889 surface->requestFocus();
890 surface->assertFocusChange(true);
891 injectKey(AKEYCODE_V);
892 EXPECT_EQ(surface->consumeEvent(100), nullptr);
893}
894
895TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
896 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
897 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
898
899 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
900 surface->showAt(100, 100);
901 surface->doTransaction([&](auto &t, auto &sc) {
902 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
903 t.reparent(sc, parentSurface->mSurfaceControl);
904 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
905 });
906
907 injectTap(101, 101);
908
909 EXPECT_EQ(surface->consumeEvent(100), nullptr);
910
911 surface->requestFocus();
912 surface->assertFocusChange(true);
913 injectKey(AKEYCODE_V);
914 EXPECT_EQ(surface->consumeEvent(100), nullptr);
915}
916
917TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
918 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
919 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
920
921 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
922 surface->doTransaction([&](auto &t, auto &sc) {
923 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
924 t.reparent(sc, parentSurface->mSurfaceControl);
925 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
926 });
927 surface->showAt(100, 100);
928
929 injectTap(111, 111);
930
931 EXPECT_EQ(surface->consumeEvent(100), nullptr);
932
933 surface->requestFocus();
934 surface->assertFocusChange(true);
935 injectKey(AKEYCODE_V);
936 EXPECT_EQ(surface->consumeEvent(100), nullptr);
937}
938
939TEST_F(InputSurfacesTest, drop_input_policy) {
940 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
941 surface->doTransaction(
942 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
943 surface->showAt(100, 100);
944
945 injectTap(101, 101);
946
947 EXPECT_EQ(surface->consumeEvent(100), nullptr);
948
949 surface->requestFocus();
950 surface->assertFocusChange(true);
951 injectKey(AKEYCODE_V);
952 EXPECT_EQ(surface->consumeEvent(100), nullptr);
953}
Vishnu Nair16a938f2021-09-24 07:14:54 -0700954
955class MultiDisplayTests : public InputSurfacesTest {
956public:
957 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
958 void TearDown() {
959 if (mVirtualDisplay) {
960 SurfaceComposerClient::destroyDisplay(mVirtualDisplay);
961 }
962 InputSurfacesTest::TearDown();
963 }
964
965 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack) {
966 sp<IGraphicBufferConsumer> consumer;
967 BufferQueue::createBufferQueue(&mProducer, &consumer);
968 consumer->setConsumerName(String8("Virtual disp consumer"));
969 consumer->setDefaultBufferSize(width, height);
970
971 mVirtualDisplay = SurfaceComposerClient::createDisplay(String8("VirtualDisplay"), isSecure);
972 SurfaceComposerClient::Transaction t;
973 t.setDisplaySurface(mVirtualDisplay, mProducer);
974 t.setDisplayFlags(mVirtualDisplay, 0x01 /* DisplayDevice::eReceivesInput */);
975 t.setDisplayLayerStack(mVirtualDisplay, layerStack);
976 t.apply(true);
977 }
978
979 sp<IBinder> mVirtualDisplay;
980 sp<IGraphicBufferProducer> mProducer;
981};
982
983TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
984 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
985 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
986 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
987 surface->doTransaction([&](auto &t, auto &sc) {
988 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
989 t.setLayerStack(sc, layerStack);
990 });
991 surface->showAt(100, 100);
992
993 injectTap(101, 101);
994
995 EXPECT_EQ(surface->consumeEvent(100), nullptr);
996
997 surface->requestFocus(layerStack.id);
998 surface->assertFocusChange(true);
999 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1000 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1001}
1002
1003TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1004 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1005 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
1006 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1007 surface->doTransaction([&](auto &t, auto &sc) {
1008 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1009 t.setLayerStack(sc, layerStack);
1010 });
1011 surface->showAt(100, 100);
1012
1013 injectTapOnDisplay(101, 101, layerStack.id);
1014 EXPECT_NE(surface->consumeEvent(), nullptr);
1015 EXPECT_NE(surface->consumeEvent(), nullptr);
1016
1017 surface->requestFocus(layerStack.id);
1018 surface->assertFocusChange(true);
1019 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1020
1021 surface->expectKey(AKEYCODE_V);
1022}
1023
Vishnu Nair958da932020-08-21 17:12:37 -07001024} // namespace android::test