blob: 3344e0b6909ece9ac96db6a65a9e284985fbc91c [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
Prabir Pradhand0aba782021-12-14 00:44:21 -080049#include <private/android_filesystem_config.h>
50
Chris Ye0783e992020-06-02 21:34:49 -070051using android::os::IInputFlinger;
Robert Carr1c4c5592018-09-24 13:18:43 -070052
chaviw39d01472021-04-08 14:26:24 -050053using android::hardware::graphics::common::V1_1::BufferUsage;
54
chaviw98318de2021-05-19 16:45:23 -050055using android::gui::FocusRequest;
56using android::gui::InputApplicationInfo;
57using android::gui::TouchOcclusionMode;
58using android::gui::WindowInfo;
59
Vishnu Nair958da932020-08-21 17:12:37 -070060namespace android::test {
Robert Carr1c4c5592018-09-24 13:18:43 -070061
Vishnu Nairde19f852018-12-18 16:11:53 -080062using Transaction = SurfaceComposerClient::Transaction;
63
Robert Carr1c4c5592018-09-24 13:18:43 -070064sp<IInputFlinger> getInputFlinger() {
65 sp<IBinder> input(defaultServiceManager()->getService(
66 String16("inputflinger")));
67 if (input == nullptr) {
68 ALOGE("Failed to link to input service");
69 } else { ALOGE("Linked to input"); }
70 return interface_cast<IInputFlinger>(input);
71}
72
73// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
74static const int LAYER_BASE = INT32_MAX - 10;
Chris Ye6c4243b2020-07-22 12:07:12 -070075static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070076
77class InputSurface {
78public:
Linus Tufvessona1858822022-03-04 09:32:07 +000079 InputSurface(const sp<SurfaceControl> &sc, int width, int height, bool noInputChannel = false) {
Vishnu Nairde19f852018-12-18 16:11:53 -080080 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -070081
82 mInputFlinger = getInputFlinger();
Linus Tufvessona1858822022-03-04 09:32:07 +000083 if (noInputChannel) {
84 mInputInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, true);
85 } else {
86 mClientChannel = std::make_shared<InputChannel>();
87 mInputFlinger->createInputChannel("testchannels", mClientChannel.get());
88 mInputInfo.token = mClientChannel->getConnectionToken();
89 mInputConsumer = new InputConsumer(mClientChannel);
90 }
Robert Carr1c4c5592018-09-24 13:18:43 -070091
Linus Tufvessona1858822022-03-04 09:32:07 +000092 mInputInfo.name = "Test info";
93 mInputInfo.dispatchingTimeout = 5s;
94 mInputInfo.globalScaleFactor = 1.0;
95 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
Robert Carr1c4c5592018-09-24 13:18:43 -070096
Linus Tufvessona1858822022-03-04 09:32:07 +000097 InputApplicationInfo aInfo;
98 aInfo.token = new BBinder();
99 aInfo.name = "Test app info";
100 aInfo.dispatchingTimeoutMillis =
101 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
102 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700103 }
104
Vishnu Nairde19f852018-12-18 16:11:53 -0800105 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
106 int width, int height) {
107 sp<SurfaceControl> surfaceControl =
108 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800109 PIXEL_FORMAT_RGBA_8888,
110 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -0800111 return std::make_unique<InputSurface>(surfaceControl, width, height);
112 }
113
114 static std::unique_ptr<InputSurface> makeBufferInputSurface(
115 const sp<SurfaceComposerClient> &scc, int width, int height) {
116 sp<SurfaceControl> surfaceControl =
117 scc->createSurface(String8("Test Buffer Surface"), width, height,
118 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
119 return std::make_unique<InputSurface>(surfaceControl, width, height);
120 }
121
122 static std::unique_ptr<InputSurface> makeContainerInputSurface(
123 const sp<SurfaceComposerClient> &scc, int width, int height) {
124 sp<SurfaceControl> surfaceControl =
125 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
126 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
127 ISurfaceComposerClient::eFXSurfaceContainer);
128 return std::make_unique<InputSurface>(surfaceControl, width, height);
129 }
130
Linus Tufvessona1858822022-03-04 09:32:07 +0000131 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
132 const sp<SurfaceComposerClient> &scc, int width, int height) {
133 sp<SurfaceControl> surfaceControl =
134 scc->createSurface(String8("Test Container Surface"), 100 /* height */,
135 100 /* width */, PIXEL_FORMAT_RGBA_8888,
136 ISurfaceComposerClient::eFXSurfaceContainer);
137 return std::make_unique<InputSurface>(surfaceControl, width, height,
138 true /* noInputChannel */);
139 }
140
arthurhungb4a0f852020-06-16 11:02:50 +0800141 static std::unique_ptr<InputSurface> makeCursorInputSurface(
142 const sp<SurfaceComposerClient> &scc, int width, int height) {
143 sp<SurfaceControl> surfaceControl =
144 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
145 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
146 ISurfaceComposerClient::eCursorWindow);
147 return std::make_unique<InputSurface>(surfaceControl, width, height);
148 }
149
Vishnu Naira066d902021-09-13 18:40:17 -0700150 InputEvent *consumeEvent(int timeoutMs = 3000) {
151 waitForEventAvailable(timeoutMs);
Robert Carr1c4c5592018-09-24 13:18:43 -0700152
153 InputEvent *ev;
154 uint32_t seqId;
155 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
156 if (consumed != OK) {
157 return nullptr;
158 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100159 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
160 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700161 return ev;
162 }
163
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100164 void assertFocusChange(bool hasFocus) {
165 InputEvent *ev = consumeEvent();
166 ASSERT_NE(ev, nullptr);
167 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, ev->getType());
168 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
169 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
170 }
171
Robert Carr1c4c5592018-09-24 13:18:43 -0700172 void expectTap(int x, int y) {
173 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100174 ASSERT_NE(ev, nullptr);
175 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700176 MotionEvent* mev = static_cast<MotionEvent*>(ev);
177 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
178 EXPECT_EQ(x, mev->getX(0));
179 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800180 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700181
182 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100183 ASSERT_NE(ev, nullptr);
184 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700185 mev = static_cast<MotionEvent*>(ev);
186 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800187 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700188 }
189
chaviw39cfa2e2020-11-04 14:19:02 -0800190 void expectTapWithFlag(int x, int y, int32_t flags) {
191 InputEvent *ev = consumeEvent();
192 ASSERT_NE(ev, nullptr);
193 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
194 MotionEvent *mev = static_cast<MotionEvent *>(ev);
195 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
196 EXPECT_EQ(x, mev->getX(0));
197 EXPECT_EQ(y, mev->getY(0));
198 EXPECT_EQ(flags, mev->getFlags() & flags);
199
200 ev = consumeEvent();
201 ASSERT_NE(ev, nullptr);
202 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
203 mev = static_cast<MotionEvent *>(ev);
204 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
205 EXPECT_EQ(flags, mev->getFlags() & flags);
206 }
207
Prabir Pradhand0aba782021-12-14 00:44:21 -0800208 void expectTapInDisplayCoordinates(int displayX, int displayY) {
209 InputEvent *ev = consumeEvent();
210 ASSERT_NE(ev, nullptr);
211 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
212 MotionEvent *mev = static_cast<MotionEvent *>(ev);
213 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
214 const PointerCoords &coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
215 EXPECT_EQ(displayX, coords.getX());
216 EXPECT_EQ(displayY, coords.getY());
217 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
218
219 ev = consumeEvent();
220 ASSERT_NE(ev, nullptr);
221 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType());
222 mev = static_cast<MotionEvent *>(ev);
223 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
224 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
225 }
226
Vishnu Naira066d902021-09-13 18:40:17 -0700227 void expectKey(uint32_t keycode) {
228 InputEvent *ev = consumeEvent();
229 ASSERT_NE(ev, nullptr);
230 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, ev->getType());
231 KeyEvent *keyEvent = static_cast<KeyEvent *>(ev);
232 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
233 EXPECT_EQ(keycode, keyEvent->getKeyCode());
234 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
235
236 ev = consumeEvent();
237 ASSERT_NE(ev, nullptr);
238 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, ev->getType());
239 keyEvent = static_cast<KeyEvent *>(ev);
240 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
241 EXPECT_EQ(keycode, keyEvent->getKeyCode());
242 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
243 }
244
chaviw39d01472021-04-08 14:26:24 -0500245 virtual ~InputSurface() {
Linus Tufvessona1858822022-03-04 09:32:07 +0000246 if (mClientChannel) {
247 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
248 }
chaviw39d01472021-04-08 14:26:24 -0500249 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700250
chaviw39d01472021-04-08 14:26:24 -0500251 virtual void doTransaction(
252 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
253 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700254 SurfaceComposerClient::Transaction t;
255 transactionBody(t, mSurfaceControl);
256 t.apply(true);
257 }
258
chaviw39d01472021-04-08 14:26:24 -0500259 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700260 SurfaceComposerClient::Transaction t;
261 t.show(mSurfaceControl);
262 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
263 t.setLayer(mSurfaceControl, LAYER_BASE);
264 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800265 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700266 t.setAlpha(mSurfaceControl, 1);
267 t.apply(true);
268 }
269
Vishnu Nair16a938f2021-09-24 07:14:54 -0700270 void requestFocus(int displayId = ADISPLAY_ID_DEFAULT) {
Vishnu Nair958da932020-08-21 17:12:37 -0700271 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800272 FocusRequest request;
273 request.token = mInputInfo.token;
274 request.windowName = mInputInfo.name;
275 request.focusedToken = nullptr;
276 request.focusedWindowName = "";
277 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700278 request.displayId = displayId;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800279 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700280 t.apply(true);
281 }
282
Robert Carr1c4c5592018-09-24 13:18:43 -0700283private:
Vishnu Naira066d902021-09-13 18:40:17 -0700284 void waitForEventAvailable(int timeoutMs) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700285 struct pollfd fd;
286
287 fd.fd = mClientChannel->getFd();
288 fd.events = POLLIN;
Vishnu Naira066d902021-09-13 18:40:17 -0700289 poll(&fd, 1, timeoutMs);
Robert Carr1c4c5592018-09-24 13:18:43 -0700290 }
291
Robert Carr1c4c5592018-09-24 13:18:43 -0700292public:
293 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500294 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700295 sp<IInputFlinger> mInputFlinger;
296
chaviw98318de2021-05-19 16:45:23 -0500297 WindowInfo mInputInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700298
299 PreallocatedInputEventFactory mInputEventFactory;
300 InputConsumer* mInputConsumer;
301};
302
chaviw39d01472021-04-08 14:26:24 -0500303class BlastInputSurface : public InputSurface {
304public:
305 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
306 int height)
307 : InputSurface(sc, width, height) {
308 mParentSurfaceControl = parentSc;
309 }
310
311 ~BlastInputSurface() = default;
312
313 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
314 const sp<SurfaceComposerClient> &scc, int width, int height) {
315 sp<SurfaceControl> parentSc =
316 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
317 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
318 ISurfaceComposerClient::eFXSurfaceContainer);
319
320 sp<SurfaceControl> surfaceControl =
321 scc->createSurface(String8("Test Buffer Surface"), width, height,
322 PIXEL_FORMAT_RGBA_8888,
323 ISurfaceComposerClient::eFXSurfaceBufferState,
324 parentSc->getHandle());
325 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
326 }
327
328 void doTransaction(
329 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
330 transactionBody) override {
331 SurfaceComposerClient::Transaction t;
332 transactionBody(t, mParentSurfaceControl);
333 t.apply(true);
334 }
335
336 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
337 SurfaceComposerClient::Transaction t;
338 t.show(mParentSurfaceControl);
339 t.setLayer(mParentSurfaceControl, LAYER_BASE);
340 t.setPosition(mParentSurfaceControl, x, y);
341 t.setCrop(mParentSurfaceControl, crop);
342
343 t.show(mSurfaceControl);
344 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
345 t.setCrop(mSurfaceControl, crop);
346 t.setAlpha(mSurfaceControl, 1);
347 t.apply(true);
348 }
349
350private:
351 sp<SurfaceControl> mParentSurfaceControl;
352};
353
Robert Carr1c4c5592018-09-24 13:18:43 -0700354class InputSurfacesTest : public ::testing::Test {
355public:
356 InputSurfacesTest() {
357 ProcessState::self()->startThreadPool();
358 }
359
360 void SetUp() {
361 mComposerClient = new SurfaceComposerClient;
362 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Huihong Luo31b5ac22022-08-15 20:38:10 -0700363 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
364 ASSERT_FALSE(ids.empty());
365 // display 0 is picked for now, can extend to support all displays if needed
366 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100367 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800368
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100369 ui::DisplayMode mode;
370 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800371
372 // After a new buffer is queued, SurfaceFlinger is notified and will
373 // latch the new buffer on next vsync. Let's heuristically wait for 3
374 // vsyncs.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100375 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.refreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700376 }
377
378 void TearDown() {
379 mComposerClient->dispose();
380 }
381
382 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800383 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
384 }
385
chaviw39d01472021-04-08 14:26:24 -0500386 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
387 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
388 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
389 sp<GraphicBuffer> buffer =
390 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
391 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800392 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700393 }
394
395 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800396 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700397};
398
Vishnu Nair16a938f2021-09-24 07:14:54 -0700399void injectTapOnDisplay(int x, int y, int displayId) {
400 char *buf1, *buf2, *bufDisplayId;
Robert Carr1c4c5592018-09-24 13:18:43 -0700401 asprintf(&buf1, "%d", x);
402 asprintf(&buf2, "%d", y);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700403 asprintf(&bufDisplayId, "%d", displayId);
Robert Carr1c4c5592018-09-24 13:18:43 -0700404 if (fork() == 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700405 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
406 }
407}
408
409void injectTap(int x, int y) {
410 injectTapOnDisplay(x, y, ADISPLAY_ID_DEFAULT);
411}
412
413void injectKeyOnDisplay(uint32_t keycode, int displayId) {
414 char *buf1, *bufDisplayId;
415 asprintf(&buf1, "%d", keycode);
416 asprintf(&bufDisplayId, "%d", displayId);
417 if (fork() == 0) {
418 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
Robert Carr1c4c5592018-09-24 13:18:43 -0700419 }
420}
421
Vishnu Naira066d902021-09-13 18:40:17 -0700422void injectKey(uint32_t keycode) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700423 injectKeyOnDisplay(keycode, ADISPLAY_ID_NONE);
Vishnu Naira066d902021-09-13 18:40:17 -0700424}
425
Robert Carr1c4c5592018-09-24 13:18:43 -0700426TEST_F(InputSurfacesTest, can_receive_input) {
427 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
428 surface->showAt(100, 100);
429
430 injectTap(101, 101);
431
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100432 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700433}
434
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100435/**
436 * Set up two surfaces side-by-side. Tap each surface.
437 * Next, swap the positions of the two surfaces. Inject tap into the two
438 * original locations. Ensure that the tap is received by the surfaces in the
439 * reverse order.
440 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700441TEST_F(InputSurfacesTest, input_respects_positioning) {
442 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
443 surface->showAt(100, 100);
444
445 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
446 surface2->showAt(200, 200);
447
448 injectTap(201, 201);
449 surface2->expectTap(1, 1);
450
451 injectTap(101, 101);
452 surface->expectTap(1, 1);
453
454 surface2->doTransaction([](auto &t, auto &sc) {
455 t.setPosition(sc, 100, 100);
456 });
457 surface->doTransaction([](auto &t, auto &sc) {
458 t.setPosition(sc, 200, 200);
459 });
460
461 injectTap(101, 101);
462 surface2->expectTap(1, 1);
463
464 injectTap(201, 201);
465 surface->expectTap(1, 1);
466}
467
468TEST_F(InputSurfacesTest, input_respects_layering) {
469 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
470 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
471
472 surface->showAt(10, 10);
473 surface2->showAt(10, 10);
474
475 surface->doTransaction([](auto &t, auto &sc) {
476 t.setLayer(sc, LAYER_BASE + 1);
477 });
478
479 injectTap(11, 11);
480 surface->expectTap(1, 1);
481
482 surface2->doTransaction([](auto &t, auto &sc) {
483 t.setLayer(sc, LAYER_BASE + 1);
484 });
485
486 injectTap(11, 11);
487 surface2->expectTap(1, 1);
488
489 surface2->doTransaction([](auto &t, auto &sc) {
490 t.hide(sc);
491 });
492
493 injectTap(11, 11);
494 surface->expectTap(1, 1);
495}
496
Vishnu Nairde19f852018-12-18 16:11:53 -0800497// Surface Insets are set to offset the client content and draw a border around the client surface
498// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
499// of the client content.
500TEST_F(InputSurfacesTest, input_respects_surface_insets) {
501 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
502 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
503 bgSurface->showAt(100, 100);
504
505 fgSurface->mInputInfo.surfaceInset = 5;
506 fgSurface->showAt(100, 100);
507
508 injectTap(106, 106);
509 fgSurface->expectTap(1, 1);
510
511 injectTap(101, 101);
512 bgSurface->expectTap(1, 1);
513}
514
515// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
516TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
517 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
518 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
519 parentSurface->showAt(100, 100);
520
521 childSurface->mInputInfo.surfaceInset = 10;
522 childSurface->showAt(100, 100);
523
524 childSurface->doTransaction([&](auto &t, auto &sc) {
525 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000526 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800527 });
528
529 injectTap(106, 106);
530 childSurface->expectTap(1, 1);
531
532 injectTap(101, 101);
533 parentSurface->expectTap(1, 1);
534}
535
Arthur Hung118b1142019-05-08 21:25:59 +0800536// Ensure a surface whose insets are scaled, handles the touch offset correctly.
537TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
538 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
539 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
540 bgSurface->showAt(100, 100);
541
542 fgSurface->mInputInfo.surfaceInset = 5;
543 fgSurface->showAt(100, 100);
544
545 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
546
547 // expect = touch / scale - inset
548 injectTap(112, 124);
549 fgSurface->expectTap(1, 1);
550
551 injectTap(101, 101);
552 bgSurface->expectTap(1, 1);
553}
554
Ady Abraham282f1d72019-07-24 18:05:56 -0700555TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700556 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
Ady Abraham282f1d72019-07-24 18:05:56 -0700557 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700558 bgSurface->showAt(100, 100);
559
Ady Abraham282f1d72019-07-24 18:05:56 -0700560 // In case we pass the very big inset without any checking.
561 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
562 fgSurface->showAt(100, 100);
563
564 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
565
566 // expect no crash for overflow, and inset size to be clamped to surface size
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700567 injectTap(112, 124);
568 bgSurface->expectTap(12, 24);
Ady Abraham282f1d72019-07-24 18:05:56 -0700569}
570
Prabir Pradhan33da9462022-06-14 14:55:57 +0000571TEST_F(InputSurfacesTest, touchable_region) {
572 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
573
574 surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31});
575
576 surface->showAt(11, 22);
577
578 // A tap within the surface but outside the touchable region should not be sent to the surface.
579 injectTap(20, 30);
580 EXPECT_EQ(surface->consumeEvent(200 /*timeoutMs*/), nullptr);
581
582 injectTap(31, 52);
583 surface->expectTap(20, 30);
584}
585
586TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
587 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
588 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
589 bgSurface->showAt(100, 100);
590
591 // Set the touchable region to the values at the limit of its corresponding type.
592 // Since the surface is offset from the origin, the touchable region will be transformed into
593 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
594 // against such a situation.
595 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
596
597 fgSurface->showAt(100, 100);
598
599 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
600 // surface receives touch.
601 injectTap(112, 124);
602 bgSurface->expectTap(12, 24);
603}
604
605TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
606 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
607 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
608 bgSurface->showAt(0, 0);
609
610 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
611 fgSurface->showAt(0, 0);
612
613 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
614
615 // Expect no crash for overflow.
616 injectTap(12, 24);
617 fgSurface->expectTap(6, 12);
618}
619
Vishnu Nairde19f852018-12-18 16:11:53 -0800620// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
621TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
622 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
623 surface->doTransaction([](auto &t, auto &sc) {
624 Region transparentRegion(Rect(0, 0, 10, 10));
625 t.setTransparentRegionHint(sc, transparentRegion);
626 });
627 surface->showAt(100, 100);
628 injectTap(101, 101);
629 surface->expectTap(1, 1);
630}
631
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700632// TODO(b/139494112) update tests once we define expected behavior
633// Ensure we still send input to the surface regardless of surface visibility changes due to the
634// first buffer being submitted or alpha changes.
635// Original bug ref: b/120839715
636TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800637 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500638 std::unique_ptr<BlastInputSurface> bufferSurface =
639 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800640
641 bgSurface->showAt(10, 10);
642 bufferSurface->showAt(10, 10);
643
644 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700645 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800646
chaviw39d01472021-04-08 14:26:24 -0500647 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800648 injectTap(11, 11);
649 bufferSurface->expectTap(1, 1);
650}
651
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000652TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800653 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500654 std::unique_ptr<BlastInputSurface> bufferSurface =
655 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
656 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800657
658 bgSurface->showAt(10, 10);
659 bufferSurface->showAt(10, 10);
660
661 injectTap(11, 11);
662 bufferSurface->expectTap(1, 1);
663
664 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
665
666 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000667 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800668}
669
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700670TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800671 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
672 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
673
674 bgSurface->showAt(10, 10);
675 fgSurface->showAt(10, 10);
676
677 injectTap(11, 11);
678 fgSurface->expectTap(1, 1);
679
680 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
681
682 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700683 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800684}
685
686TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
687 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
688 std::unique_ptr<InputSurface> containerSurface =
689 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
690
691 bgSurface->showAt(10, 10);
692 containerSurface->showAt(10, 10);
693
694 injectTap(11, 11);
695 containerSurface->expectTap(1, 1);
696
697 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
698
699 injectTap(11, 11);
700 bgSurface->expectTap(1, 1);
701}
chaviwfbe5d9c2018-12-26 12:23:37 -0800702
Arthur Hungd20b2702019-01-14 18:16:16 +0800703TEST_F(InputSurfacesTest, input_respects_outscreen) {
704 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
705 surface->showAt(-1, -1);
706
707 injectTap(0, 0);
708 surface->expectTap(1, 1);
709}
arthurhungb4a0f852020-06-16 11:02:50 +0800710
711TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
712 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
713 std::unique_ptr<InputSurface> cursorSurface =
714 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
715
716 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800717 cursorSurface->showAt(10, 10);
718
719 injectTap(11, 11);
720 surface->expectTap(1, 1);
721}
Vishnu Nair958da932020-08-21 17:12:37 -0700722
723TEST_F(InputSurfacesTest, can_be_focused) {
724 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
725 surface->showAt(100, 100);
726 surface->requestFocus();
727
728 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700729
730 injectKey(AKEYCODE_V);
731 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700732}
chaviw44a6d2b2020-09-08 17:14:16 -0700733
734TEST_F(InputSurfacesTest, rotate_surface) {
735 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
736 surface->showAt(10, 10);
737 surface->doTransaction([](auto &t, auto &sc) {
738 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
739 });
740 injectTap(8, 11);
741 surface->expectTap(1, 2);
742
743 surface->doTransaction([](auto &t, auto &sc) {
744 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
745 });
746 injectTap(9, 8);
747 surface->expectTap(1, 2);
748
749 surface->doTransaction([](auto &t, auto &sc) {
750 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
751 });
752 injectTap(12, 9);
753 surface->expectTap(1, 2);
754}
755
756TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
757 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
758 surface->showAt(10, 10);
759 surface->doTransaction([](auto &t, auto &sc) {
760 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
761 });
762 injectTap(2, 12);
763 surface->expectTap(1, 2);
764
765 surface->doTransaction([](auto &t, auto &sc) {
766 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
767 });
768 injectTap(8, 2);
769 surface->expectTap(1, 2);
770
771 surface->doTransaction([](auto &t, auto &sc) {
772 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
773 });
774 injectTap(18, 8);
775 surface->expectTap(1, 2);
776}
777
778TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
779 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
780 surface->mInputInfo.surfaceInset = 5;
781 surface->showAt(100, 100);
782
783 surface->doTransaction([](auto &t, auto &sc) {
784 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
785 });
786 injectTap(40, 120);
787 surface->expectTap(5, 10);
788
789 surface->doTransaction([](auto &t, auto &sc) {
790 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
791 });
792 injectTap(80, 40);
793 surface->expectTap(5, 10);
794
795 surface->doTransaction([](auto &t, auto &sc) {
796 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
797 });
798 injectTap(160, 80);
799 surface->expectTap(5, 10);
800}
801
chaviw39cfa2e2020-11-04 14:19:02 -0800802TEST_F(InputSurfacesTest, touch_flag_obscured) {
803 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
804 surface->showAt(100, 100);
805
806 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
807 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
808 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800809 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
chaviw39cfa2e2020-11-04 14:19:02 -0800810 nonTouchableSurface->mInputInfo.ownerUid = 22222;
Bernardo Rufino602ef712020-12-21 11:02:18 +0000811 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
812 // the default obscured/untrusted touch filter introduced in S.
813 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800814 nonTouchableSurface->showAt(100, 100);
815
816 injectTap(190, 199);
817 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
818}
819
820TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
821 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
822 surface->showAt(100, 100);
823
824 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
825 // that will be tapped. Window behind gets touch, but with flag
826 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
827 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
828 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800829 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
830 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
chaviw39cfa2e2020-11-04 14:19:02 -0800831 nonTouchableSurface->mInputInfo.ownerUid = 22222;
832 parentSurface->mInputInfo.ownerUid = 22222;
833 nonTouchableSurface->showAt(0, 0);
834 parentSurface->showAt(100, 100);
835
836 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800837 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800838 t.reparent(sc, parentSurface->mSurfaceControl);
839 });
840
841 injectTap(190, 199);
842 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
843}
844
845TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
846 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
847 surface->showAt(100, 100);
848
849 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
850 // the touchable window. Window behind gets touch with no obscured flags.
851 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
852 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800853 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
854 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
chaviw39cfa2e2020-11-04 14:19:02 -0800855 nonTouchableSurface->mInputInfo.ownerUid = 22222;
856 parentSurface->mInputInfo.ownerUid = 22222;
857 nonTouchableSurface->showAt(0, 0);
858 parentSurface->showAt(50, 50);
859
860 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800861 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800862 t.reparent(sc, parentSurface->mSurfaceControl);
863 });
864
865 injectTap(101, 110);
866 surface->expectTap(1, 10);
867}
868
chaviw7e72caf2020-12-02 16:50:43 -0800869TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
870 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
871
872 std::unique_ptr<InputSurface> bufferSurface =
873 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800874 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
chaviw7e72caf2020-12-02 16:50:43 -0800875 bufferSurface->mInputInfo.ownerUid = 22222;
876
877 surface->showAt(10, 10);
878 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
879
880 injectTap(11, 11);
881 surface->expectTap(1, 1);
882}
883
884TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
885 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
886
chaviw39d01472021-04-08 14:26:24 -0500887 std::unique_ptr<BlastInputSurface> bufferSurface =
888 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800889 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
chaviw7e72caf2020-12-02 16:50:43 -0800890 bufferSurface->mInputInfo.ownerUid = 22222;
891
892 surface->showAt(10, 10);
893 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
894
895 injectTap(11, 11);
896 surface->expectTap(1, 1);
897}
898
Vishnu Naira066d902021-09-13 18:40:17 -0700899TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
900 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
901 surface->doTransaction(
902 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
903 surface->showAt(100, 100);
904
905 injectTap(101, 101);
906
907 EXPECT_NE(surface->consumeEvent(), nullptr);
908 EXPECT_NE(surface->consumeEvent(), nullptr);
909
910 surface->requestFocus();
911 surface->assertFocusChange(true);
912 injectKey(AKEYCODE_V);
913 surface->expectKey(AKEYCODE_V);
914}
915
916TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
917 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
918 surface->doTransaction([&](auto &t, auto &sc) {
919 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
920 t.setMatrix(sc, 2.0, 0, 0, 2.0);
921 });
922 surface->showAt(100, 100);
923
924 injectTap(101, 101);
925
926 EXPECT_NE(surface->consumeEvent(), nullptr);
927 EXPECT_NE(surface->consumeEvent(), nullptr);
928
929 surface->requestFocus();
930 surface->assertFocusChange(true);
931 injectKey(AKEYCODE_V);
932 surface->expectKey(AKEYCODE_V);
933}
934
935TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
936 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
937 surface->mInputInfo.ownerUid = 11111;
938 surface->doTransaction(
939 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
940 surface->showAt(100, 100);
941 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800942 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Vishnu Naira066d902021-09-13 18:40:17 -0700943 obscuringSurface->mInputInfo.ownerUid = 22222;
944 obscuringSurface->showAt(100, 100);
945 injectTap(101, 101);
946 EXPECT_EQ(surface->consumeEvent(100), nullptr);
947
948 surface->requestFocus();
949 surface->assertFocusChange(true);
950 injectKey(AKEYCODE_V);
951 EXPECT_EQ(surface->consumeEvent(100), nullptr);
952}
953
954TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
955 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
956 surface->mInputInfo.ownerUid = 11111;
957 surface->doTransaction(
958 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
959 surface->showAt(100, 100);
960 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800961 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Vishnu Naira066d902021-09-13 18:40:17 -0700962 obscuringSurface->mInputInfo.ownerUid = 22222;
963 obscuringSurface->showAt(190, 190);
964
965 injectTap(101, 101);
966
967 EXPECT_EQ(surface->consumeEvent(100), nullptr);
968
969 surface->requestFocus();
970 surface->assertFocusChange(true);
971 injectKey(AKEYCODE_V);
972 EXPECT_EQ(surface->consumeEvent(100), nullptr);
973}
974
975TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
976 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
977 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
978
979 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
980 surface->showAt(100, 100);
981 surface->doTransaction([&](auto &t, auto &sc) {
982 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
983 t.reparent(sc, parentSurface->mSurfaceControl);
984 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
985 });
986
987 injectTap(101, 101);
988
989 EXPECT_EQ(surface->consumeEvent(100), nullptr);
990
991 surface->requestFocus();
992 surface->assertFocusChange(true);
993 injectKey(AKEYCODE_V);
994 EXPECT_EQ(surface->consumeEvent(100), nullptr);
995}
996
997TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
998 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
999 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1000
1001 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1002 surface->doTransaction([&](auto &t, auto &sc) {
1003 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1004 t.reparent(sc, parentSurface->mSurfaceControl);
1005 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1006 });
1007 surface->showAt(100, 100);
1008
1009 injectTap(111, 111);
1010
1011 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1012
1013 surface->requestFocus();
1014 surface->assertFocusChange(true);
1015 injectKey(AKEYCODE_V);
1016 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1017}
1018
Arthur Hung49d525a2021-11-19 15:11:51 +00001019TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1020 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1021
1022 std::unique_ptr<BlastInputSurface> bufferSurface =
1023 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1024
1025 surface->showAt(100, 100);
1026 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
1027 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1028
1029 injectTap(101, 101);
1030 surface->expectTap(1, 1);
1031}
1032
Vishnu Naira066d902021-09-13 18:40:17 -07001033TEST_F(InputSurfacesTest, drop_input_policy) {
1034 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1035 surface->doTransaction(
1036 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
1037 surface->showAt(100, 100);
1038
1039 injectTap(101, 101);
1040
1041 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1042
1043 surface->requestFocus();
1044 surface->assertFocusChange(true);
1045 injectKey(AKEYCODE_V);
1046 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1047}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001048
Prabir Pradhan8c285982022-01-28 09:19:39 -08001049TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1050 std::unique_ptr<InputSurface> bufferSurface =
1051 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1052
1053 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1054
1055 bufferSurface->requestFocus();
1056 bufferSurface->assertFocusChange(true);
1057}
1058
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001059/**
1060 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1061 * its own crop.
1062 */
1063TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1064 std::unique_ptr<InputSurface> parentContainer =
1065 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1066 std::unique_ptr<InputSurface> containerSurface =
1067 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1068 containerSurface->doTransaction(
1069 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1070 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1071 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1072 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1073 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1074
1075 // Receives events inside its own crop
1076 injectTap(21, 21);
1077 containerSurface->expectTap(1, 1); // Event is in layer space
1078
1079 // Does not receive events outside its crop
1080 injectTap(26, 26);
1081 EXPECT_EQ(containerSurface->consumeEvent(100), nullptr);
1082}
1083
1084/**
1085 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1086 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1087 */
1088TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
1089 std::unique_ptr<InputSurface> parentContainer =
1090 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1091 std::unique_ptr<InputSurface> containerSurface =
1092 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1093 containerSurface->doTransaction(
1094 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1095 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1096 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1097 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1098 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1099
1100 // Receives events inside parent bounds
1101 injectTap(21, 21);
1102 containerSurface->expectTap(1, 1); // Event is in layer space
1103
1104 // Does not receive events outside parent bounds
1105 injectTap(31, 31);
1106 EXPECT_EQ(containerSurface->consumeEvent(100), nullptr);
1107}
1108
1109/**
1110 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1111 * layer's bounds. The input events should be in the layer's coordinate space.
1112 */
1113TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1114 std::unique_ptr<InputSurface> cropLayer =
1115 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1116 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1117
1118 std::unique_ptr<InputSurface> containerSurface =
1119 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1120 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1121 containerSurface->mInputInfo.touchableRegionCropHandle =
1122 cropLayer->mSurfaceControl->getHandle();
1123 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1124
1125 // Receives events inside crop layer bounds
1126 injectTap(51, 51);
1127 containerSurface->expectTap(41, 41); // Event is in layer space
1128
1129 // Does not receive events outside crop layer bounds
1130 injectTap(21, 21);
1131 injectTap(71, 71);
1132 EXPECT_EQ(containerSurface->consumeEvent(100), nullptr);
1133}
1134
Linus Tufvessona1858822022-03-04 09:32:07 +00001135TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1136 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1137
1138 parent->showAt(100, 100);
1139 injectTap(101, 101);
1140 parent->expectTap(1, 1);
1141
1142 std::unique_ptr<InputSurface> childContainerSurface =
1143 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1144 childContainerSurface->showAt(0, 0);
1145 childContainerSurface->doTransaction(
1146 [&](auto &t, auto &sc) { t.reparent(sc, parent->mSurfaceControl); });
1147 injectTap(101, 101);
1148
1149 EXPECT_EQ(parent->consumeEvent(100), nullptr);
1150}
1151
Vishnu Nair16a938f2021-09-24 07:14:54 -07001152class MultiDisplayTests : public InputSurfacesTest {
1153public:
1154 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Prabir Pradhand0aba782021-12-14 00:44:21 -08001155 void TearDown() override {
1156 for (auto &token : mVirtualDisplays) {
1157 SurfaceComposerClient::destroyDisplay(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001158 }
1159 InputSurfacesTest::TearDown();
1160 }
1161
Prabir Pradhand0aba782021-12-14 00:44:21 -08001162 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1163 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001164 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001165 sp<IGraphicBufferProducer> producer;
1166 BufferQueue::createBufferQueue(&producer, &consumer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001167 consumer->setConsumerName(String8("Virtual disp consumer"));
1168 consumer->setDefaultBufferSize(width, height);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001169 mProducers.push_back(producer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001170
Prabir Pradhand0aba782021-12-14 00:44:21 -08001171 std::string name = "VirtualDisplay";
1172 name += std::to_string(mVirtualDisplays.size());
1173 sp<IBinder> token = SurfaceComposerClient::createDisplay(String8(name.c_str()), isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001174 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001175 t.setDisplaySurface(token, producer);
1176 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1177 t.setDisplayLayerStack(token, layerStack);
1178 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1179 {offsetX, offsetY, offsetX + width, offsetY + height});
Vishnu Nair16a938f2021-09-24 07:14:54 -07001180 t.apply(true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001181
1182 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001183 }
1184
Prabir Pradhand0aba782021-12-14 00:44:21 -08001185 std::vector<sp<IBinder>> mVirtualDisplays;
1186 std::vector<sp<IGraphicBufferProducer>> mProducers;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001187};
1188
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001189TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001190 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1191 // Do not create a display associated with the LayerStack.
1192 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1193 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1194 surface->showAt(100, 100);
1195
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001196 // Touches should be dropped if the layer is on an invalid display.
Prabir Pradhand0aba782021-12-14 00:44:21 -08001197 injectTapOnDisplay(101, 101, layerStack.id);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001198 EXPECT_EQ(surface->consumeEvent(100), nullptr);
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001199
1200 // However, we still let the window be focused and receive keys.
1201 surface->requestFocus(layerStack.id);
1202 surface->assertFocusChange(true);
1203
1204 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1205 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001206}
1207
1208TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1209 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1210 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1211 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1212 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1213 surface->showAt(100, 100);
1214
1215 injectTapOnDisplay(101, 101, layerStack.id);
1216 surface->expectTap(1, 1);
1217
1218 surface->requestFocus(layerStack.id);
1219 surface->assertFocusChange(true);
1220 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1221 surface->expectKey(AKEYCODE_V);
1222}
1223
1224/**
1225 * When multiple DisplayDevices are mapped to the same layerStack, use the configuration for the
1226 * display that can receive input.
1227 */
1228TEST_F(MultiDisplayTests, many_to_one_display_mapping) {
1229 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1230 createDisplay(1000, 1000, false /*isSecure*/, layerStack, false /*receivesInput*/,
1231 100 /*offsetX*/, 100 /*offsetY*/);
1232 createDisplay(1000, 1000, false /*isSecure*/, layerStack, true /*receivesInput*/,
1233 200 /*offsetX*/, 200 /*offsetY*/);
1234 createDisplay(1000, 1000, false /*isSecure*/, layerStack, false /*receivesInput*/,
1235 300 /*offsetX*/, 300 /*offsetY*/);
1236 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1237 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1238 surface->showAt(10, 10);
1239
1240 // Input injection happens in logical display coordinates.
1241 injectTapOnDisplay(11, 11, layerStack.id);
1242 // Expect that the display transform for the display that receives input was used.
1243 surface->expectTapInDisplayCoordinates(211, 211);
1244
1245 surface->requestFocus(layerStack.id);
1246 surface->assertFocusChange(true);
1247 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1248}
1249
Vishnu Nair16a938f2021-09-24 07:14:54 -07001250TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1251 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1252 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1253 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1254 surface->doTransaction([&](auto &t, auto &sc) {
1255 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1256 t.setLayerStack(sc, layerStack);
1257 });
1258 surface->showAt(100, 100);
1259
Prabir Pradhand0aba782021-12-14 00:44:21 -08001260 injectTapOnDisplay(101, 101, layerStack.id);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001261
1262 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1263
1264 surface->requestFocus(layerStack.id);
1265 surface->assertFocusChange(true);
1266 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1267 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1268}
1269
1270TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1271 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001272
1273 // Create the secure display as system, because only certain users can create secure displays.
1274 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001275 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001276 // Change the uid back to root.
1277 seteuid(AID_ROOT);
1278
Vishnu Nair16a938f2021-09-24 07:14:54 -07001279 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1280 surface->doTransaction([&](auto &t, auto &sc) {
1281 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1282 t.setLayerStack(sc, layerStack);
1283 });
1284 surface->showAt(100, 100);
1285
1286 injectTapOnDisplay(101, 101, layerStack.id);
1287 EXPECT_NE(surface->consumeEvent(), nullptr);
1288 EXPECT_NE(surface->consumeEvent(), nullptr);
1289
1290 surface->requestFocus(layerStack.id);
1291 surface->assertFocusChange(true);
1292 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1293
1294 surface->expectKey(AKEYCODE_V);
1295}
1296
Vishnu Nair958da932020-08-21 17:12:37 -07001297} // namespace android::test