blob: 4ec7a06cb8270aeabe649e96851ce1df7e75cba6 [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);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700167 ASSERT_EQ(InputEventType::FOCUS, ev->getType());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100168 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);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700175 ASSERT_EQ(InputEventType::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);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700184 ASSERT_EQ(InputEventType::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);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700193 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800194 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);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700202 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800203 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);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700211 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800212 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);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700221 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800222 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);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700230 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700231 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);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700238 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700239 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;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800275 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700276 request.displayId = displayId;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800277 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700278 t.apply(true);
279 }
280
Robert Carr1c4c5592018-09-24 13:18:43 -0700281private:
Vishnu Naira066d902021-09-13 18:40:17 -0700282 void waitForEventAvailable(int timeoutMs) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700283 struct pollfd fd;
284
285 fd.fd = mClientChannel->getFd();
286 fd.events = POLLIN;
Vishnu Naira066d902021-09-13 18:40:17 -0700287 poll(&fd, 1, timeoutMs);
Robert Carr1c4c5592018-09-24 13:18:43 -0700288 }
289
Robert Carr1c4c5592018-09-24 13:18:43 -0700290public:
291 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500292 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700293 sp<IInputFlinger> mInputFlinger;
294
chaviw98318de2021-05-19 16:45:23 -0500295 WindowInfo mInputInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700296
297 PreallocatedInputEventFactory mInputEventFactory;
298 InputConsumer* mInputConsumer;
299};
300
chaviw39d01472021-04-08 14:26:24 -0500301class BlastInputSurface : public InputSurface {
302public:
303 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
304 int height)
305 : InputSurface(sc, width, height) {
306 mParentSurfaceControl = parentSc;
307 }
308
309 ~BlastInputSurface() = default;
310
311 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
312 const sp<SurfaceComposerClient> &scc, int width, int height) {
313 sp<SurfaceControl> parentSc =
314 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
315 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
316 ISurfaceComposerClient::eFXSurfaceContainer);
317
318 sp<SurfaceControl> surfaceControl =
319 scc->createSurface(String8("Test Buffer Surface"), width, height,
320 PIXEL_FORMAT_RGBA_8888,
321 ISurfaceComposerClient::eFXSurfaceBufferState,
322 parentSc->getHandle());
323 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
324 }
325
326 void doTransaction(
327 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
328 transactionBody) override {
329 SurfaceComposerClient::Transaction t;
330 transactionBody(t, mParentSurfaceControl);
331 t.apply(true);
332 }
333
334 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
335 SurfaceComposerClient::Transaction t;
336 t.show(mParentSurfaceControl);
337 t.setLayer(mParentSurfaceControl, LAYER_BASE);
338 t.setPosition(mParentSurfaceControl, x, y);
339 t.setCrop(mParentSurfaceControl, crop);
340
341 t.show(mSurfaceControl);
342 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
343 t.setCrop(mSurfaceControl, crop);
344 t.setAlpha(mSurfaceControl, 1);
345 t.apply(true);
346 }
347
348private:
349 sp<SurfaceControl> mParentSurfaceControl;
350};
351
Robert Carr1c4c5592018-09-24 13:18:43 -0700352class InputSurfacesTest : public ::testing::Test {
353public:
354 InputSurfacesTest() {
355 ProcessState::self()->startThreadPool();
356 }
357
358 void SetUp() {
359 mComposerClient = new SurfaceComposerClient;
360 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Huihong Luo31b5ac22022-08-15 20:38:10 -0700361 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
362 ASSERT_FALSE(ids.empty());
363 // display 0 is picked for now, can extend to support all displays if needed
364 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100365 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800366
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100367 ui::DisplayMode mode;
368 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800369
370 // After a new buffer is queued, SurfaceFlinger is notified and will
371 // latch the new buffer on next vsync. Let's heuristically wait for 3
372 // vsyncs.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100373 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.refreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700374 }
375
376 void TearDown() {
377 mComposerClient->dispose();
378 }
379
380 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800381 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
382 }
383
chaviw39d01472021-04-08 14:26:24 -0500384 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
385 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
386 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
387 sp<GraphicBuffer> buffer =
388 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
389 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800390 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700391 }
392
393 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800394 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700395};
396
Vishnu Nair16a938f2021-09-24 07:14:54 -0700397void injectTapOnDisplay(int x, int y, int displayId) {
398 char *buf1, *buf2, *bufDisplayId;
Robert Carr1c4c5592018-09-24 13:18:43 -0700399 asprintf(&buf1, "%d", x);
400 asprintf(&buf2, "%d", y);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700401 asprintf(&bufDisplayId, "%d", displayId);
Robert Carr1c4c5592018-09-24 13:18:43 -0700402 if (fork() == 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700403 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
404 }
405}
406
407void injectTap(int x, int y) {
408 injectTapOnDisplay(x, y, ADISPLAY_ID_DEFAULT);
409}
410
411void injectKeyOnDisplay(uint32_t keycode, int displayId) {
412 char *buf1, *bufDisplayId;
413 asprintf(&buf1, "%d", keycode);
414 asprintf(&bufDisplayId, "%d", displayId);
415 if (fork() == 0) {
416 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
Robert Carr1c4c5592018-09-24 13:18:43 -0700417 }
418}
419
Vishnu Naira066d902021-09-13 18:40:17 -0700420void injectKey(uint32_t keycode) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700421 injectKeyOnDisplay(keycode, ADISPLAY_ID_NONE);
Vishnu Naira066d902021-09-13 18:40:17 -0700422}
423
Robert Carr1c4c5592018-09-24 13:18:43 -0700424TEST_F(InputSurfacesTest, can_receive_input) {
425 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
426 surface->showAt(100, 100);
427
428 injectTap(101, 101);
429
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100430 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700431}
432
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100433/**
434 * Set up two surfaces side-by-side. Tap each surface.
435 * Next, swap the positions of the two surfaces. Inject tap into the two
436 * original locations. Ensure that the tap is received by the surfaces in the
437 * reverse order.
438 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700439TEST_F(InputSurfacesTest, input_respects_positioning) {
440 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
441 surface->showAt(100, 100);
442
443 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
444 surface2->showAt(200, 200);
445
446 injectTap(201, 201);
447 surface2->expectTap(1, 1);
448
449 injectTap(101, 101);
450 surface->expectTap(1, 1);
451
452 surface2->doTransaction([](auto &t, auto &sc) {
453 t.setPosition(sc, 100, 100);
454 });
455 surface->doTransaction([](auto &t, auto &sc) {
456 t.setPosition(sc, 200, 200);
457 });
458
459 injectTap(101, 101);
460 surface2->expectTap(1, 1);
461
462 injectTap(201, 201);
463 surface->expectTap(1, 1);
464}
465
466TEST_F(InputSurfacesTest, input_respects_layering) {
467 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
468 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
469
470 surface->showAt(10, 10);
471 surface2->showAt(10, 10);
472
473 surface->doTransaction([](auto &t, auto &sc) {
474 t.setLayer(sc, LAYER_BASE + 1);
475 });
476
477 injectTap(11, 11);
478 surface->expectTap(1, 1);
479
480 surface2->doTransaction([](auto &t, auto &sc) {
481 t.setLayer(sc, LAYER_BASE + 1);
482 });
483
484 injectTap(11, 11);
485 surface2->expectTap(1, 1);
486
487 surface2->doTransaction([](auto &t, auto &sc) {
488 t.hide(sc);
489 });
490
491 injectTap(11, 11);
492 surface->expectTap(1, 1);
493}
494
Vishnu Nairde19f852018-12-18 16:11:53 -0800495// Surface Insets are set to offset the client content and draw a border around the client surface
496// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
497// of the client content.
498TEST_F(InputSurfacesTest, input_respects_surface_insets) {
499 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
500 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
501 bgSurface->showAt(100, 100);
502
503 fgSurface->mInputInfo.surfaceInset = 5;
504 fgSurface->showAt(100, 100);
505
506 injectTap(106, 106);
507 fgSurface->expectTap(1, 1);
508
509 injectTap(101, 101);
510 bgSurface->expectTap(1, 1);
511}
512
Vishnu Nairfed7c122023-03-18 01:54:43 +0000513TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) {
514 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
515 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
516 bgSurface->showAt(100, 100);
517
518 fgSurface->mInputInfo.surfaceInset = 5;
519 fgSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
520 fgSurface->showAt(100, 100);
521
522 injectTap(106, 106);
523 fgSurface->expectTap(1, 1);
524
525 injectTap(101, 101);
526 bgSurface->expectTap(1, 1);
527}
528
Vishnu Nairde19f852018-12-18 16:11:53 -0800529// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
530TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
531 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
532 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
533 parentSurface->showAt(100, 100);
534
535 childSurface->mInputInfo.surfaceInset = 10;
536 childSurface->showAt(100, 100);
537
538 childSurface->doTransaction([&](auto &t, auto &sc) {
539 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000540 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800541 });
542
543 injectTap(106, 106);
544 childSurface->expectTap(1, 1);
545
546 injectTap(101, 101);
547 parentSurface->expectTap(1, 1);
548}
549
Arthur Hung118b1142019-05-08 21:25:59 +0800550// Ensure a surface whose insets are scaled, handles the touch offset correctly.
551TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
552 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
553 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
554 bgSurface->showAt(100, 100);
555
556 fgSurface->mInputInfo.surfaceInset = 5;
557 fgSurface->showAt(100, 100);
558
559 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
560
561 // expect = touch / scale - inset
562 injectTap(112, 124);
563 fgSurface->expectTap(1, 1);
564
565 injectTap(101, 101);
566 bgSurface->expectTap(1, 1);
567}
568
Ady Abraham282f1d72019-07-24 18:05:56 -0700569TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700570 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
Ady Abraham282f1d72019-07-24 18:05:56 -0700571 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700572 bgSurface->showAt(100, 100);
573
Ady Abraham282f1d72019-07-24 18:05:56 -0700574 // In case we pass the very big inset without any checking.
575 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
576 fgSurface->showAt(100, 100);
577
578 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
579
580 // expect no crash for overflow, and inset size to be clamped to surface size
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700581 injectTap(112, 124);
582 bgSurface->expectTap(12, 24);
Ady Abraham282f1d72019-07-24 18:05:56 -0700583}
584
Prabir Pradhan33da9462022-06-14 14:55:57 +0000585TEST_F(InputSurfacesTest, touchable_region) {
586 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
587
588 surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31});
589
590 surface->showAt(11, 22);
591
592 // A tap within the surface but outside the touchable region should not be sent to the surface.
593 injectTap(20, 30);
594 EXPECT_EQ(surface->consumeEvent(200 /*timeoutMs*/), nullptr);
595
596 injectTap(31, 52);
597 surface->expectTap(20, 30);
598}
599
600TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
601 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
602 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
603 bgSurface->showAt(100, 100);
604
605 // Set the touchable region to the values at the limit of its corresponding type.
606 // Since the surface is offset from the origin, the touchable region will be transformed into
607 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
608 // against such a situation.
609 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
610
611 fgSurface->showAt(100, 100);
612
613 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
614 // surface receives touch.
615 injectTap(112, 124);
616 bgSurface->expectTap(12, 24);
617}
618
619TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
620 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
621 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
622 bgSurface->showAt(0, 0);
623
624 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
625 fgSurface->showAt(0, 0);
626
627 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
628
629 // Expect no crash for overflow.
630 injectTap(12, 24);
Prabir Pradhana4c59bd2023-04-10 20:54:04 +0000631 bgSurface->expectTap(12, 24);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000632}
633
Vishnu Nairde19f852018-12-18 16:11:53 -0800634// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
635TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
636 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
637 surface->doTransaction([](auto &t, auto &sc) {
638 Region transparentRegion(Rect(0, 0, 10, 10));
639 t.setTransparentRegionHint(sc, transparentRegion);
640 });
641 surface->showAt(100, 100);
642 injectTap(101, 101);
643 surface->expectTap(1, 1);
644}
645
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700646// TODO(b/139494112) update tests once we define expected behavior
647// Ensure we still send input to the surface regardless of surface visibility changes due to the
648// first buffer being submitted or alpha changes.
649// Original bug ref: b/120839715
650TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800651 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500652 std::unique_ptr<BlastInputSurface> bufferSurface =
653 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800654
655 bgSurface->showAt(10, 10);
656 bufferSurface->showAt(10, 10);
657
658 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700659 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800660
chaviw39d01472021-04-08 14:26:24 -0500661 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800662 injectTap(11, 11);
663 bufferSurface->expectTap(1, 1);
664}
665
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000666TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800667 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500668 std::unique_ptr<BlastInputSurface> bufferSurface =
669 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
670 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800671
672 bgSurface->showAt(10, 10);
673 bufferSurface->showAt(10, 10);
674
675 injectTap(11, 11);
676 bufferSurface->expectTap(1, 1);
677
678 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
679
680 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000681 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800682}
683
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700684TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800685 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
686 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
687
688 bgSurface->showAt(10, 10);
689 fgSurface->showAt(10, 10);
690
691 injectTap(11, 11);
692 fgSurface->expectTap(1, 1);
693
694 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
695
696 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700697 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800698}
699
700TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
701 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
702 std::unique_ptr<InputSurface> containerSurface =
703 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
704
705 bgSurface->showAt(10, 10);
706 containerSurface->showAt(10, 10);
707
708 injectTap(11, 11);
709 containerSurface->expectTap(1, 1);
710
711 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
712
713 injectTap(11, 11);
714 bgSurface->expectTap(1, 1);
715}
chaviwfbe5d9c2018-12-26 12:23:37 -0800716
Arthur Hungd20b2702019-01-14 18:16:16 +0800717TEST_F(InputSurfacesTest, input_respects_outscreen) {
718 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
719 surface->showAt(-1, -1);
720
721 injectTap(0, 0);
722 surface->expectTap(1, 1);
723}
arthurhungb4a0f852020-06-16 11:02:50 +0800724
725TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
726 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
727 std::unique_ptr<InputSurface> cursorSurface =
728 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
729
730 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800731 cursorSurface->showAt(10, 10);
732
733 injectTap(11, 11);
734 surface->expectTap(1, 1);
735}
Vishnu Nair958da932020-08-21 17:12:37 -0700736
737TEST_F(InputSurfacesTest, can_be_focused) {
738 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
739 surface->showAt(100, 100);
740 surface->requestFocus();
741
742 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700743
744 injectKey(AKEYCODE_V);
745 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700746}
chaviw44a6d2b2020-09-08 17:14:16 -0700747
748TEST_F(InputSurfacesTest, rotate_surface) {
749 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
750 surface->showAt(10, 10);
751 surface->doTransaction([](auto &t, auto &sc) {
752 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
753 });
754 injectTap(8, 11);
755 surface->expectTap(1, 2);
756
757 surface->doTransaction([](auto &t, auto &sc) {
758 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
759 });
760 injectTap(9, 8);
761 surface->expectTap(1, 2);
762
763 surface->doTransaction([](auto &t, auto &sc) {
764 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
765 });
766 injectTap(12, 9);
767 surface->expectTap(1, 2);
768}
769
770TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
771 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
772 surface->showAt(10, 10);
773 surface->doTransaction([](auto &t, auto &sc) {
774 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
775 });
776 injectTap(2, 12);
777 surface->expectTap(1, 2);
778
779 surface->doTransaction([](auto &t, auto &sc) {
780 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
781 });
782 injectTap(8, 2);
783 surface->expectTap(1, 2);
784
785 surface->doTransaction([](auto &t, auto &sc) {
786 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
787 });
788 injectTap(18, 8);
789 surface->expectTap(1, 2);
790}
791
792TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
793 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
794 surface->mInputInfo.surfaceInset = 5;
795 surface->showAt(100, 100);
796
797 surface->doTransaction([](auto &t, auto &sc) {
798 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
799 });
800 injectTap(40, 120);
801 surface->expectTap(5, 10);
802
803 surface->doTransaction([](auto &t, auto &sc) {
804 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
805 });
806 injectTap(80, 40);
807 surface->expectTap(5, 10);
808
809 surface->doTransaction([](auto &t, auto &sc) {
810 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
811 });
812 injectTap(160, 80);
813 surface->expectTap(5, 10);
814}
815
chaviw39cfa2e2020-11-04 14:19:02 -0800816TEST_F(InputSurfacesTest, touch_flag_obscured) {
817 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
818 surface->showAt(100, 100);
819
820 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
821 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
822 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800823 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
chaviw39cfa2e2020-11-04 14:19:02 -0800824 nonTouchableSurface->mInputInfo.ownerUid = 22222;
Bernardo Rufino602ef712020-12-21 11:02:18 +0000825 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
826 // the default obscured/untrusted touch filter introduced in S.
827 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800828 nonTouchableSurface->showAt(100, 100);
829
830 injectTap(190, 199);
831 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
832}
833
834TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
835 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
836 surface->showAt(100, 100);
837
838 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
839 // that will be tapped. Window behind gets touch, but with flag
840 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
841 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
842 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800843 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
844 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
chaviw39cfa2e2020-11-04 14:19:02 -0800845 nonTouchableSurface->mInputInfo.ownerUid = 22222;
846 parentSurface->mInputInfo.ownerUid = 22222;
847 nonTouchableSurface->showAt(0, 0);
848 parentSurface->showAt(100, 100);
849
850 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800851 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800852 t.reparent(sc, parentSurface->mSurfaceControl);
853 });
854
855 injectTap(190, 199);
856 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
857}
858
859TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
860 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
861 surface->showAt(100, 100);
862
863 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
864 // the touchable window. Window behind gets touch with no obscured flags.
865 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
866 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800867 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
868 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
chaviw39cfa2e2020-11-04 14:19:02 -0800869 nonTouchableSurface->mInputInfo.ownerUid = 22222;
870 parentSurface->mInputInfo.ownerUid = 22222;
871 nonTouchableSurface->showAt(0, 0);
872 parentSurface->showAt(50, 50);
873
874 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800875 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800876 t.reparent(sc, parentSurface->mSurfaceControl);
877 });
878
879 injectTap(101, 110);
880 surface->expectTap(1, 10);
881}
882
chaviw7e72caf2020-12-02 16:50:43 -0800883TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
884 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
885
886 std::unique_ptr<InputSurface> bufferSurface =
887 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800888 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
chaviw7e72caf2020-12-02 16:50:43 -0800889 bufferSurface->mInputInfo.ownerUid = 22222;
890
891 surface->showAt(10, 10);
892 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
893
894 injectTap(11, 11);
895 surface->expectTap(1, 1);
896}
897
898TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
899 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
900
chaviw39d01472021-04-08 14:26:24 -0500901 std::unique_ptr<BlastInputSurface> bufferSurface =
902 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800903 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
chaviw7e72caf2020-12-02 16:50:43 -0800904 bufferSurface->mInputInfo.ownerUid = 22222;
905
906 surface->showAt(10, 10);
907 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
908
909 injectTap(11, 11);
910 surface->expectTap(1, 1);
911}
912
Vishnu Naira066d902021-09-13 18:40:17 -0700913TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
914 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
915 surface->doTransaction(
916 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
917 surface->showAt(100, 100);
918
919 injectTap(101, 101);
920
921 EXPECT_NE(surface->consumeEvent(), nullptr);
922 EXPECT_NE(surface->consumeEvent(), nullptr);
923
924 surface->requestFocus();
925 surface->assertFocusChange(true);
926 injectKey(AKEYCODE_V);
927 surface->expectKey(AKEYCODE_V);
928}
929
930TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
931 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
932 surface->doTransaction([&](auto &t, auto &sc) {
933 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
934 t.setMatrix(sc, 2.0, 0, 0, 2.0);
935 });
936 surface->showAt(100, 100);
937
938 injectTap(101, 101);
939
940 EXPECT_NE(surface->consumeEvent(), nullptr);
941 EXPECT_NE(surface->consumeEvent(), nullptr);
942
943 surface->requestFocus();
944 surface->assertFocusChange(true);
945 injectKey(AKEYCODE_V);
946 surface->expectKey(AKEYCODE_V);
947}
948
949TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
950 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
951 surface->mInputInfo.ownerUid = 11111;
952 surface->doTransaction(
953 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
954 surface->showAt(100, 100);
955 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800956 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Vishnu Naira066d902021-09-13 18:40:17 -0700957 obscuringSurface->mInputInfo.ownerUid = 22222;
958 obscuringSurface->showAt(100, 100);
959 injectTap(101, 101);
960 EXPECT_EQ(surface->consumeEvent(100), nullptr);
961
962 surface->requestFocus();
963 surface->assertFocusChange(true);
964 injectKey(AKEYCODE_V);
965 EXPECT_EQ(surface->consumeEvent(100), nullptr);
966}
967
968TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
969 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
970 surface->mInputInfo.ownerUid = 11111;
971 surface->doTransaction(
972 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
973 surface->showAt(100, 100);
974 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800975 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Vishnu Naira066d902021-09-13 18:40:17 -0700976 obscuringSurface->mInputInfo.ownerUid = 22222;
977 obscuringSurface->showAt(190, 190);
978
979 injectTap(101, 101);
980
981 EXPECT_EQ(surface->consumeEvent(100), nullptr);
982
983 surface->requestFocus();
984 surface->assertFocusChange(true);
985 injectKey(AKEYCODE_V);
986 EXPECT_EQ(surface->consumeEvent(100), nullptr);
987}
988
989TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
990 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
991 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
992
993 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
994 surface->showAt(100, 100);
995 surface->doTransaction([&](auto &t, auto &sc) {
996 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
997 t.reparent(sc, parentSurface->mSurfaceControl);
998 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
999 });
1000
1001 injectTap(101, 101);
1002
1003 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1004
1005 surface->requestFocus();
1006 surface->assertFocusChange(true);
1007 injectKey(AKEYCODE_V);
1008 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1009}
1010
1011TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
1012 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1013 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1014
1015 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1016 surface->doTransaction([&](auto &t, auto &sc) {
1017 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1018 t.reparent(sc, parentSurface->mSurfaceControl);
1019 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1020 });
1021 surface->showAt(100, 100);
1022
1023 injectTap(111, 111);
1024
1025 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1026
1027 surface->requestFocus();
1028 surface->assertFocusChange(true);
1029 injectKey(AKEYCODE_V);
1030 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1031}
1032
Arthur Hung49d525a2021-11-19 15:11:51 +00001033TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1034 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1035
1036 std::unique_ptr<BlastInputSurface> bufferSurface =
1037 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1038
1039 surface->showAt(100, 100);
1040 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
1041 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1042
1043 injectTap(101, 101);
1044 surface->expectTap(1, 1);
1045}
1046
Vishnu Naira066d902021-09-13 18:40:17 -07001047TEST_F(InputSurfacesTest, drop_input_policy) {
1048 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1049 surface->doTransaction(
1050 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
1051 surface->showAt(100, 100);
1052
1053 injectTap(101, 101);
1054
1055 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1056
1057 surface->requestFocus();
1058 surface->assertFocusChange(true);
1059 injectKey(AKEYCODE_V);
1060 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1061}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001062
Prabir Pradhan8c285982022-01-28 09:19:39 -08001063TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1064 std::unique_ptr<InputSurface> bufferSurface =
1065 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1066
1067 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1068
1069 bufferSurface->requestFocus();
1070 bufferSurface->assertFocusChange(true);
1071}
1072
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001073/**
1074 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1075 * its own crop.
1076 */
1077TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1078 std::unique_ptr<InputSurface> parentContainer =
1079 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1080 std::unique_ptr<InputSurface> containerSurface =
1081 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1082 containerSurface->doTransaction(
1083 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1084 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1085 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1086 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1087 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1088
1089 // Receives events inside its own crop
1090 injectTap(21, 21);
1091 containerSurface->expectTap(1, 1); // Event is in layer space
1092
1093 // Does not receive events outside its crop
1094 injectTap(26, 26);
1095 EXPECT_EQ(containerSurface->consumeEvent(100), nullptr);
1096}
1097
1098/**
1099 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1100 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1101 */
1102TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
1103 std::unique_ptr<InputSurface> parentContainer =
1104 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1105 std::unique_ptr<InputSurface> containerSurface =
1106 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1107 containerSurface->doTransaction(
1108 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1109 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1110 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1111 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1112 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1113
1114 // Receives events inside parent bounds
1115 injectTap(21, 21);
1116 containerSurface->expectTap(1, 1); // Event is in layer space
1117
1118 // Does not receive events outside parent bounds
1119 injectTap(31, 31);
1120 EXPECT_EQ(containerSurface->consumeEvent(100), nullptr);
1121}
1122
1123/**
1124 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1125 * layer's bounds. The input events should be in the layer's coordinate space.
1126 */
1127TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1128 std::unique_ptr<InputSurface> cropLayer =
1129 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1130 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1131
1132 std::unique_ptr<InputSurface> containerSurface =
1133 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1134 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1135 containerSurface->mInputInfo.touchableRegionCropHandle =
1136 cropLayer->mSurfaceControl->getHandle();
1137 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1138
1139 // Receives events inside crop layer bounds
1140 injectTap(51, 51);
1141 containerSurface->expectTap(41, 41); // Event is in layer space
1142
1143 // Does not receive events outside crop layer bounds
1144 injectTap(21, 21);
1145 injectTap(71, 71);
1146 EXPECT_EQ(containerSurface->consumeEvent(100), nullptr);
1147}
1148
Linus Tufvessona1858822022-03-04 09:32:07 +00001149TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1150 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1151
1152 parent->showAt(100, 100);
1153 injectTap(101, 101);
1154 parent->expectTap(1, 1);
1155
1156 std::unique_ptr<InputSurface> childContainerSurface =
1157 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1158 childContainerSurface->showAt(0, 0);
1159 childContainerSurface->doTransaction(
1160 [&](auto &t, auto &sc) { t.reparent(sc, parent->mSurfaceControl); });
1161 injectTap(101, 101);
1162
1163 EXPECT_EQ(parent->consumeEvent(100), nullptr);
1164}
1165
Vishnu Nair16a938f2021-09-24 07:14:54 -07001166class MultiDisplayTests : public InputSurfacesTest {
1167public:
1168 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Prabir Pradhand0aba782021-12-14 00:44:21 -08001169 void TearDown() override {
1170 for (auto &token : mVirtualDisplays) {
1171 SurfaceComposerClient::destroyDisplay(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001172 }
1173 InputSurfacesTest::TearDown();
1174 }
1175
Prabir Pradhand0aba782021-12-14 00:44:21 -08001176 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1177 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001178 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001179 sp<IGraphicBufferProducer> producer;
1180 BufferQueue::createBufferQueue(&producer, &consumer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001181 consumer->setConsumerName(String8("Virtual disp consumer"));
1182 consumer->setDefaultBufferSize(width, height);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001183 mProducers.push_back(producer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001184
Prabir Pradhand0aba782021-12-14 00:44:21 -08001185 std::string name = "VirtualDisplay";
1186 name += std::to_string(mVirtualDisplays.size());
1187 sp<IBinder> token = SurfaceComposerClient::createDisplay(String8(name.c_str()), isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001188 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001189 t.setDisplaySurface(token, producer);
1190 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1191 t.setDisplayLayerStack(token, layerStack);
1192 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1193 {offsetX, offsetY, offsetX + width, offsetY + height});
Vishnu Nair16a938f2021-09-24 07:14:54 -07001194 t.apply(true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001195
1196 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001197 }
1198
Prabir Pradhand0aba782021-12-14 00:44:21 -08001199 std::vector<sp<IBinder>> mVirtualDisplays;
1200 std::vector<sp<IGraphicBufferProducer>> mProducers;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001201};
1202
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001203TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001204 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1205 // Do not create a display associated with the LayerStack.
1206 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1207 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1208 surface->showAt(100, 100);
1209
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001210 // Touches should be dropped if the layer is on an invalid display.
Prabir Pradhand0aba782021-12-14 00:44:21 -08001211 injectTapOnDisplay(101, 101, layerStack.id);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001212 EXPECT_EQ(surface->consumeEvent(100), nullptr);
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001213
1214 // However, we still let the window be focused and receive keys.
1215 surface->requestFocus(layerStack.id);
1216 surface->assertFocusChange(true);
1217
1218 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1219 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001220}
1221
1222TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1223 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1224 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1225 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1226 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1227 surface->showAt(100, 100);
1228
1229 injectTapOnDisplay(101, 101, layerStack.id);
1230 surface->expectTap(1, 1);
1231
1232 surface->requestFocus(layerStack.id);
1233 surface->assertFocusChange(true);
1234 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1235 surface->expectKey(AKEYCODE_V);
1236}
1237
Vishnu Nair16a938f2021-09-24 07:14:54 -07001238TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1239 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1240 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1241 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1242 surface->doTransaction([&](auto &t, auto &sc) {
1243 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1244 t.setLayerStack(sc, layerStack);
1245 });
1246 surface->showAt(100, 100);
1247
Prabir Pradhand0aba782021-12-14 00:44:21 -08001248 injectTapOnDisplay(101, 101, layerStack.id);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001249
1250 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1251
1252 surface->requestFocus(layerStack.id);
1253 surface->assertFocusChange(true);
1254 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1255 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1256}
1257
1258TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1259 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001260
1261 // Create the secure display as system, because only certain users can create secure displays.
1262 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001263 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001264 // Change the uid back to root.
1265 seteuid(AID_ROOT);
1266
Vishnu Nair16a938f2021-09-24 07:14:54 -07001267 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1268 surface->doTransaction([&](auto &t, auto &sc) {
1269 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1270 t.setLayerStack(sc, layerStack);
1271 });
1272 surface->showAt(100, 100);
1273
1274 injectTapOnDisplay(101, 101, layerStack.id);
1275 EXPECT_NE(surface->consumeEvent(), nullptr);
1276 EXPECT_NE(surface->consumeEvent(), nullptr);
1277
1278 surface->requestFocus(layerStack.id);
1279 surface->assertFocusChange(true);
1280 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1281
1282 surface->expectKey(AKEYCODE_V);
1283}
1284
Vishnu Nair958da932020-08-21 17:12:37 -07001285} // namespace android::test