blob: a9d6e8d3bf3d86426930a15b9fafa67f0a492fd6 [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
Patrick Williams0a4981a2023-07-28 15:08:52 -050027#include <android/gui/BnWindowInfosReportedListener.h>
Vishnu Naira066d902021-09-13 18:40:17 -070028#include <android/keycodes.h>
Vishnu Nairde19f852018-12-18 16:11:53 -080029#include <android/native_window.h>
30
Robert Carr1c4c5592018-09-24 13:18:43 -070031#include <binder/Binder.h>
32#include <binder/IServiceManager.h>
33#include <binder/Parcel.h>
34#include <binder/ProcessState.h>
35
Vishnu Nairde19f852018-12-18 16:11:53 -080036#include <gui/ISurfaceComposer.h>
37#include <gui/Surface.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070038#include <gui/SurfaceComposerClient.h>
39#include <gui/SurfaceControl.h>
40
Chris Ye0783e992020-06-02 21:34:49 -070041#include <android/os/IInputFlinger.h>
chaviw98318de2021-05-19 16:45:23 -050042#include <gui/WindowInfo.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070043#include <input/Input.h>
Chris Ye0783e992020-06-02 21:34:49 -070044#include <input/InputTransport.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070045
Marin Shalamanova7fe3042021-01-29 21:02:08 +010046#include <ui/DisplayMode.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070047#include <ui/Rect.h>
48#include <ui/Region.h>
49
Prabir Pradhand0aba782021-12-14 00:44:21 -080050#include <private/android_filesystem_config.h>
51
Chris Ye0783e992020-06-02 21:34:49 -070052using android::os::IInputFlinger;
Robert Carr1c4c5592018-09-24 13:18:43 -070053
chaviw39d01472021-04-08 14:26:24 -050054using android::hardware::graphics::common::V1_1::BufferUsage;
55
chaviw98318de2021-05-19 16:45:23 -050056using android::gui::FocusRequest;
57using android::gui::InputApplicationInfo;
58using android::gui::TouchOcclusionMode;
59using android::gui::WindowInfo;
60
Vishnu Nair958da932020-08-21 17:12:37 -070061namespace android::test {
Robert Carr1c4c5592018-09-24 13:18:43 -070062
Vishnu Nairde19f852018-12-18 16:11:53 -080063using Transaction = SurfaceComposerClient::Transaction;
64
Robert Carr1c4c5592018-09-24 13:18:43 -070065sp<IInputFlinger> getInputFlinger() {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -080066 sp<IBinder> input(defaultServiceManager()->waitForService(String16("inputflinger")));
Robert Carr1c4c5592018-09-24 13:18:43 -070067 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
Patrick Williams0a4981a2023-07-28 15:08:52 -050077class SynchronousWindowInfosReportedListener : public gui::BnWindowInfosReportedListener {
78public:
79 binder::Status onWindowInfosReported() override {
80 std::lock_guard<std::mutex> lock{mMutex};
81 mWindowInfosReported = true;
82 mConditionVariable.notify_one();
83 return binder::Status::ok();
84 }
85
86 void wait() {
87 std::unique_lock<std::mutex> lock{mMutex};
88 mConditionVariable.wait(lock, [&] { return mWindowInfosReported; });
89 }
90
91private:
92 std::mutex mMutex;
93 std::condition_variable mConditionVariable;
94 bool mWindowInfosReported{false};
95};
96
Robert Carr1c4c5592018-09-24 13:18:43 -070097class InputSurface {
98public:
Linus Tufvessona1858822022-03-04 09:32:07 +000099 InputSurface(const sp<SurfaceControl> &sc, int width, int height, bool noInputChannel = false) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800100 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -0700101
102 mInputFlinger = getInputFlinger();
Linus Tufvessona1858822022-03-04 09:32:07 +0000103 if (noInputChannel) {
104 mInputInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, true);
105 } else {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800106 android::os::InputChannelCore tempChannel;
107 android::binder::Status result =
108 mInputFlinger->createInputChannel("testchannels", &tempChannel);
109 if (!result.isOk()) {
110 ADD_FAILURE() << "binder call to createInputChannel failed";
111 }
112 mClientChannel = InputChannel::create(std::move(tempChannel));
Linus Tufvessona1858822022-03-04 09:32:07 +0000113 mInputInfo.token = mClientChannel->getConnectionToken();
114 mInputConsumer = new InputConsumer(mClientChannel);
115 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700116
Linus Tufvessona1858822022-03-04 09:32:07 +0000117 mInputInfo.name = "Test info";
118 mInputInfo.dispatchingTimeout = 5s;
119 mInputInfo.globalScaleFactor = 1.0;
120 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
Robert Carr1c4c5592018-09-24 13:18:43 -0700121
Linus Tufvessona1858822022-03-04 09:32:07 +0000122 InputApplicationInfo aInfo;
123 aInfo.token = new BBinder();
124 aInfo.name = "Test app info";
125 aInfo.dispatchingTimeoutMillis =
126 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
127 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700128 }
129
Vishnu Nairde19f852018-12-18 16:11:53 -0800130 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
131 int width, int height) {
132 sp<SurfaceControl> surfaceControl =
133 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800134 PIXEL_FORMAT_RGBA_8888,
135 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -0800136 return std::make_unique<InputSurface>(surfaceControl, width, height);
137 }
138
139 static std::unique_ptr<InputSurface> makeBufferInputSurface(
140 const sp<SurfaceComposerClient> &scc, int width, int height) {
141 sp<SurfaceControl> surfaceControl =
142 scc->createSurface(String8("Test Buffer Surface"), width, height,
143 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
144 return std::make_unique<InputSurface>(surfaceControl, width, height);
145 }
146
147 static std::unique_ptr<InputSurface> makeContainerInputSurface(
148 const sp<SurfaceComposerClient> &scc, int width, int height) {
149 sp<SurfaceControl> surfaceControl =
150 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
151 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
152 ISurfaceComposerClient::eFXSurfaceContainer);
153 return std::make_unique<InputSurface>(surfaceControl, width, height);
154 }
155
Linus Tufvessona1858822022-03-04 09:32:07 +0000156 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
157 const sp<SurfaceComposerClient> &scc, int width, int height) {
158 sp<SurfaceControl> surfaceControl =
159 scc->createSurface(String8("Test Container Surface"), 100 /* height */,
160 100 /* width */, PIXEL_FORMAT_RGBA_8888,
161 ISurfaceComposerClient::eFXSurfaceContainer);
162 return std::make_unique<InputSurface>(surfaceControl, width, height,
163 true /* noInputChannel */);
164 }
165
arthurhungb4a0f852020-06-16 11:02:50 +0800166 static std::unique_ptr<InputSurface> makeCursorInputSurface(
167 const sp<SurfaceComposerClient> &scc, int width, int height) {
168 sp<SurfaceControl> surfaceControl =
169 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
170 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
171 ISurfaceComposerClient::eCursorWindow);
172 return std::make_unique<InputSurface>(surfaceControl, width, height);
173 }
174
Egor Pasko5a67a562024-01-16 16:46:45 +0100175 InputEvent* consumeEvent(std::chrono::milliseconds timeout = 3000ms) {
176 mClientChannel->waitForMessage(timeout);
Robert Carr1c4c5592018-09-24 13:18:43 -0700177
178 InputEvent *ev;
179 uint32_t seqId;
180 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
181 if (consumed != OK) {
182 return nullptr;
183 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100184 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
185 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700186 return ev;
187 }
188
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100189 void assertFocusChange(bool hasFocus) {
190 InputEvent *ev = consumeEvent();
191 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700192 ASSERT_EQ(InputEventType::FOCUS, ev->getType());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100193 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
194 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
195 }
196
Robert Carr1c4c5592018-09-24 13:18:43 -0700197 void expectTap(int x, int y) {
198 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100199 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700200 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700201 MotionEvent* mev = static_cast<MotionEvent*>(ev);
202 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
203 EXPECT_EQ(x, mev->getX(0));
204 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800205 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700206
207 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100208 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700209 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700210 mev = static_cast<MotionEvent*>(ev);
211 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800212 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700213 }
214
chaviw39cfa2e2020-11-04 14:19:02 -0800215 void expectTapWithFlag(int x, int y, int32_t flags) {
216 InputEvent *ev = consumeEvent();
217 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700218 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800219 MotionEvent *mev = static_cast<MotionEvent *>(ev);
220 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
221 EXPECT_EQ(x, mev->getX(0));
222 EXPECT_EQ(y, mev->getY(0));
223 EXPECT_EQ(flags, mev->getFlags() & flags);
224
225 ev = consumeEvent();
226 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700227 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800228 mev = static_cast<MotionEvent *>(ev);
229 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
230 EXPECT_EQ(flags, mev->getFlags() & flags);
231 }
232
Prabir Pradhand0aba782021-12-14 00:44:21 -0800233 void expectTapInDisplayCoordinates(int displayX, int displayY) {
234 InputEvent *ev = consumeEvent();
235 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700236 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800237 MotionEvent *mev = static_cast<MotionEvent *>(ev);
238 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
239 const PointerCoords &coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
240 EXPECT_EQ(displayX, coords.getX());
241 EXPECT_EQ(displayY, coords.getY());
242 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
243
244 ev = consumeEvent();
245 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700246 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800247 mev = static_cast<MotionEvent *>(ev);
248 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
249 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
250 }
251
Vishnu Naira066d902021-09-13 18:40:17 -0700252 void expectKey(uint32_t keycode) {
253 InputEvent *ev = consumeEvent();
254 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700255 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700256 KeyEvent *keyEvent = static_cast<KeyEvent *>(ev);
257 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
258 EXPECT_EQ(keycode, keyEvent->getKeyCode());
259 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
260
261 ev = consumeEvent();
262 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700263 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700264 keyEvent = static_cast<KeyEvent *>(ev);
265 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
266 EXPECT_EQ(keycode, keyEvent->getKeyCode());
267 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
268 }
269
chaviw39d01472021-04-08 14:26:24 -0500270 virtual ~InputSurface() {
Linus Tufvessona1858822022-03-04 09:32:07 +0000271 if (mClientChannel) {
272 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
273 }
chaviw39d01472021-04-08 14:26:24 -0500274 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700275
chaviw39d01472021-04-08 14:26:24 -0500276 virtual void doTransaction(
277 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
278 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700279 SurfaceComposerClient::Transaction t;
280 transactionBody(t, mSurfaceControl);
281 t.apply(true);
282 }
283
chaviw39d01472021-04-08 14:26:24 -0500284 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700285 SurfaceComposerClient::Transaction t;
286 t.show(mSurfaceControl);
287 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
288 t.setLayer(mSurfaceControl, LAYER_BASE);
289 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800290 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700291 t.setAlpha(mSurfaceControl, 1);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500292 auto reportedListener = sp<SynchronousWindowInfosReportedListener>::make();
293 t.addWindowInfosReportedListener(reportedListener);
294 t.apply();
295 reportedListener->wait();
Robert Carr1c4c5592018-09-24 13:18:43 -0700296 }
297
Vishnu Nair16a938f2021-09-24 07:14:54 -0700298 void requestFocus(int displayId = ADISPLAY_ID_DEFAULT) {
Vishnu Nair958da932020-08-21 17:12:37 -0700299 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800300 FocusRequest request;
301 request.token = mInputInfo.token;
302 request.windowName = mInputInfo.name;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800303 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700304 request.displayId = displayId;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800305 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700306 t.apply(true);
307 }
308
Robert Carr1c4c5592018-09-24 13:18:43 -0700309public:
310 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500311 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700312 sp<IInputFlinger> mInputFlinger;
313
chaviw98318de2021-05-19 16:45:23 -0500314 WindowInfo mInputInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700315
316 PreallocatedInputEventFactory mInputEventFactory;
317 InputConsumer* mInputConsumer;
318};
319
chaviw39d01472021-04-08 14:26:24 -0500320class BlastInputSurface : public InputSurface {
321public:
322 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
323 int height)
324 : InputSurface(sc, width, height) {
325 mParentSurfaceControl = parentSc;
326 }
327
328 ~BlastInputSurface() = default;
329
330 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
331 const sp<SurfaceComposerClient> &scc, int width, int height) {
332 sp<SurfaceControl> parentSc =
333 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
334 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
335 ISurfaceComposerClient::eFXSurfaceContainer);
336
337 sp<SurfaceControl> surfaceControl =
338 scc->createSurface(String8("Test Buffer Surface"), width, height,
339 PIXEL_FORMAT_RGBA_8888,
340 ISurfaceComposerClient::eFXSurfaceBufferState,
341 parentSc->getHandle());
342 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
343 }
344
345 void doTransaction(
346 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
347 transactionBody) override {
348 SurfaceComposerClient::Transaction t;
349 transactionBody(t, mParentSurfaceControl);
350 t.apply(true);
351 }
352
353 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
354 SurfaceComposerClient::Transaction t;
355 t.show(mParentSurfaceControl);
356 t.setLayer(mParentSurfaceControl, LAYER_BASE);
357 t.setPosition(mParentSurfaceControl, x, y);
358 t.setCrop(mParentSurfaceControl, crop);
359
360 t.show(mSurfaceControl);
361 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
362 t.setCrop(mSurfaceControl, crop);
363 t.setAlpha(mSurfaceControl, 1);
364 t.apply(true);
365 }
366
367private:
368 sp<SurfaceControl> mParentSurfaceControl;
369};
370
Robert Carr1c4c5592018-09-24 13:18:43 -0700371class InputSurfacesTest : public ::testing::Test {
372public:
373 InputSurfacesTest() {
374 ProcessState::self()->startThreadPool();
375 }
376
377 void SetUp() {
378 mComposerClient = new SurfaceComposerClient;
379 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Huihong Luo31b5ac22022-08-15 20:38:10 -0700380 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
381 ASSERT_FALSE(ids.empty());
382 // display 0 is picked for now, can extend to support all displays if needed
383 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100384 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800385
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100386 ui::DisplayMode mode;
387 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800388
389 // After a new buffer is queued, SurfaceFlinger is notified and will
390 // latch the new buffer on next vsync. Let's heuristically wait for 3
391 // vsyncs.
Alec Mouri55e31032023-10-02 20:34:18 +0000392 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.peakRefreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700393 }
394
395 void TearDown() {
396 mComposerClient->dispose();
397 }
398
399 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800400 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
401 }
402
chaviw39d01472021-04-08 14:26:24 -0500403 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
404 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
405 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
406 sp<GraphicBuffer> buffer =
407 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
408 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800409 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700410 }
411
412 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800413 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700414};
415
Vishnu Nair16a938f2021-09-24 07:14:54 -0700416void injectTapOnDisplay(int x, int y, int displayId) {
417 char *buf1, *buf2, *bufDisplayId;
Robert Carr1c4c5592018-09-24 13:18:43 -0700418 asprintf(&buf1, "%d", x);
419 asprintf(&buf2, "%d", y);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700420 asprintf(&bufDisplayId, "%d", displayId);
Robert Carr1c4c5592018-09-24 13:18:43 -0700421 if (fork() == 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700422 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
423 }
424}
425
426void injectTap(int x, int y) {
427 injectTapOnDisplay(x, y, ADISPLAY_ID_DEFAULT);
428}
429
430void injectKeyOnDisplay(uint32_t keycode, int displayId) {
431 char *buf1, *bufDisplayId;
432 asprintf(&buf1, "%d", keycode);
433 asprintf(&bufDisplayId, "%d", displayId);
434 if (fork() == 0) {
435 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
Robert Carr1c4c5592018-09-24 13:18:43 -0700436 }
437}
438
Vishnu Naira066d902021-09-13 18:40:17 -0700439void injectKey(uint32_t keycode) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700440 injectKeyOnDisplay(keycode, ADISPLAY_ID_NONE);
Vishnu Naira066d902021-09-13 18:40:17 -0700441}
442
Robert Carr1c4c5592018-09-24 13:18:43 -0700443TEST_F(InputSurfacesTest, can_receive_input) {
444 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
445 surface->showAt(100, 100);
446
447 injectTap(101, 101);
448
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100449 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700450}
451
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100452/**
453 * Set up two surfaces side-by-side. Tap each surface.
454 * Next, swap the positions of the two surfaces. Inject tap into the two
455 * original locations. Ensure that the tap is received by the surfaces in the
456 * reverse order.
457 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700458TEST_F(InputSurfacesTest, input_respects_positioning) {
459 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
460 surface->showAt(100, 100);
461
462 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
463 surface2->showAt(200, 200);
464
465 injectTap(201, 201);
466 surface2->expectTap(1, 1);
467
468 injectTap(101, 101);
469 surface->expectTap(1, 1);
470
471 surface2->doTransaction([](auto &t, auto &sc) {
472 t.setPosition(sc, 100, 100);
473 });
474 surface->doTransaction([](auto &t, auto &sc) {
475 t.setPosition(sc, 200, 200);
476 });
477
478 injectTap(101, 101);
479 surface2->expectTap(1, 1);
480
481 injectTap(201, 201);
482 surface->expectTap(1, 1);
483}
484
485TEST_F(InputSurfacesTest, input_respects_layering) {
486 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
487 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
488
489 surface->showAt(10, 10);
490 surface2->showAt(10, 10);
491
492 surface->doTransaction([](auto &t, auto &sc) {
493 t.setLayer(sc, LAYER_BASE + 1);
494 });
495
496 injectTap(11, 11);
497 surface->expectTap(1, 1);
498
499 surface2->doTransaction([](auto &t, auto &sc) {
500 t.setLayer(sc, LAYER_BASE + 1);
501 });
502
503 injectTap(11, 11);
504 surface2->expectTap(1, 1);
505
506 surface2->doTransaction([](auto &t, auto &sc) {
507 t.hide(sc);
508 });
509
510 injectTap(11, 11);
511 surface->expectTap(1, 1);
512}
513
Vishnu Nairde19f852018-12-18 16:11:53 -0800514// Surface Insets are set to offset the client content and draw a border around the client surface
515// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
516// of the client content.
517TEST_F(InputSurfacesTest, input_respects_surface_insets) {
518 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
519 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
520 bgSurface->showAt(100, 100);
521
522 fgSurface->mInputInfo.surfaceInset = 5;
523 fgSurface->showAt(100, 100);
524
525 injectTap(106, 106);
526 fgSurface->expectTap(1, 1);
527
528 injectTap(101, 101);
529 bgSurface->expectTap(1, 1);
530}
531
Vishnu Nairfed7c122023-03-18 01:54:43 +0000532TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) {
533 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
534 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
535 bgSurface->showAt(100, 100);
536
537 fgSurface->mInputInfo.surfaceInset = 5;
538 fgSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
539 fgSurface->showAt(100, 100);
540
541 injectTap(106, 106);
542 fgSurface->expectTap(1, 1);
543
544 injectTap(101, 101);
545 bgSurface->expectTap(1, 1);
546}
547
Vishnu Nairde19f852018-12-18 16:11:53 -0800548// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
549TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
550 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
551 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
552 parentSurface->showAt(100, 100);
553
554 childSurface->mInputInfo.surfaceInset = 10;
555 childSurface->showAt(100, 100);
556
557 childSurface->doTransaction([&](auto &t, auto &sc) {
558 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000559 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800560 });
561
562 injectTap(106, 106);
563 childSurface->expectTap(1, 1);
564
565 injectTap(101, 101);
566 parentSurface->expectTap(1, 1);
567}
568
Arthur Hung118b1142019-05-08 21:25:59 +0800569// Ensure a surface whose insets are scaled, handles the touch offset correctly.
570TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
571 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
572 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
573 bgSurface->showAt(100, 100);
574
575 fgSurface->mInputInfo.surfaceInset = 5;
576 fgSurface->showAt(100, 100);
577
578 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
579
580 // expect = touch / scale - inset
581 injectTap(112, 124);
582 fgSurface->expectTap(1, 1);
583
584 injectTap(101, 101);
585 bgSurface->expectTap(1, 1);
586}
587
Ady Abraham282f1d72019-07-24 18:05:56 -0700588TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700589 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
Ady Abraham282f1d72019-07-24 18:05:56 -0700590 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700591 bgSurface->showAt(100, 100);
592
Ady Abraham282f1d72019-07-24 18:05:56 -0700593 // In case we pass the very big inset without any checking.
594 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
595 fgSurface->showAt(100, 100);
596
597 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
598
599 // expect no crash for overflow, and inset size to be clamped to surface size
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700600 injectTap(112, 124);
601 bgSurface->expectTap(12, 24);
Ady Abraham282f1d72019-07-24 18:05:56 -0700602}
603
Prabir Pradhan33da9462022-06-14 14:55:57 +0000604TEST_F(InputSurfacesTest, touchable_region) {
605 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
606
607 surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31});
608
609 surface->showAt(11, 22);
610
611 // A tap within the surface but outside the touchable region should not be sent to the surface.
612 injectTap(20, 30);
Egor Pasko5a67a562024-01-16 16:46:45 +0100613 EXPECT_EQ(surface->consumeEvent(/*timeout=*/200ms), nullptr);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000614
615 injectTap(31, 52);
616 surface->expectTap(20, 30);
617}
618
619TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
620 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
621 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
622 bgSurface->showAt(100, 100);
623
624 // Set the touchable region to the values at the limit of its corresponding type.
625 // Since the surface is offset from the origin, the touchable region will be transformed into
626 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
627 // against such a situation.
628 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
629
630 fgSurface->showAt(100, 100);
631
632 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
633 // surface receives touch.
634 injectTap(112, 124);
635 bgSurface->expectTap(12, 24);
636}
637
638TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
639 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
640 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
641 bgSurface->showAt(0, 0);
642
643 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
644 fgSurface->showAt(0, 0);
645
646 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
647
648 // Expect no crash for overflow.
649 injectTap(12, 24);
Prabir Pradhana4c59bd2023-04-10 20:54:04 +0000650 bgSurface->expectTap(12, 24);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000651}
652
Vishnu Nairde19f852018-12-18 16:11:53 -0800653// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
654TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
655 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
656 surface->doTransaction([](auto &t, auto &sc) {
657 Region transparentRegion(Rect(0, 0, 10, 10));
658 t.setTransparentRegionHint(sc, transparentRegion);
659 });
660 surface->showAt(100, 100);
661 injectTap(101, 101);
662 surface->expectTap(1, 1);
663}
664
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700665// TODO(b/139494112) update tests once we define expected behavior
666// Ensure we still send input to the surface regardless of surface visibility changes due to the
667// first buffer being submitted or alpha changes.
668// Original bug ref: b/120839715
669TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800670 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500671 std::unique_ptr<BlastInputSurface> bufferSurface =
672 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800673
674 bgSurface->showAt(10, 10);
675 bufferSurface->showAt(10, 10);
676
677 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700678 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800679
chaviw39d01472021-04-08 14:26:24 -0500680 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800681 injectTap(11, 11);
682 bufferSurface->expectTap(1, 1);
683}
684
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000685TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800686 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500687 std::unique_ptr<BlastInputSurface> bufferSurface =
688 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
689 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800690
691 bgSurface->showAt(10, 10);
692 bufferSurface->showAt(10, 10);
693
694 injectTap(11, 11);
695 bufferSurface->expectTap(1, 1);
696
697 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
698
699 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000700 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800701}
702
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700703TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800704 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
705 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
706
707 bgSurface->showAt(10, 10);
708 fgSurface->showAt(10, 10);
709
710 injectTap(11, 11);
711 fgSurface->expectTap(1, 1);
712
713 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
714
715 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700716 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800717}
718
719TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
720 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
721 std::unique_ptr<InputSurface> containerSurface =
722 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
723
724 bgSurface->showAt(10, 10);
725 containerSurface->showAt(10, 10);
726
727 injectTap(11, 11);
728 containerSurface->expectTap(1, 1);
729
730 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
731
732 injectTap(11, 11);
733 bgSurface->expectTap(1, 1);
734}
chaviwfbe5d9c2018-12-26 12:23:37 -0800735
Arthur Hungd20b2702019-01-14 18:16:16 +0800736TEST_F(InputSurfacesTest, input_respects_outscreen) {
737 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
738 surface->showAt(-1, -1);
739
740 injectTap(0, 0);
741 surface->expectTap(1, 1);
742}
arthurhungb4a0f852020-06-16 11:02:50 +0800743
744TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
745 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
746 std::unique_ptr<InputSurface> cursorSurface =
747 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
748
749 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800750 cursorSurface->showAt(10, 10);
751
752 injectTap(11, 11);
753 surface->expectTap(1, 1);
754}
Vishnu Nair958da932020-08-21 17:12:37 -0700755
756TEST_F(InputSurfacesTest, can_be_focused) {
757 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
758 surface->showAt(100, 100);
759 surface->requestFocus();
760
761 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700762
763 injectKey(AKEYCODE_V);
764 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700765}
chaviw44a6d2b2020-09-08 17:14:16 -0700766
767TEST_F(InputSurfacesTest, rotate_surface) {
768 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
769 surface->showAt(10, 10);
770 surface->doTransaction([](auto &t, auto &sc) {
771 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
772 });
773 injectTap(8, 11);
774 surface->expectTap(1, 2);
775
776 surface->doTransaction([](auto &t, auto &sc) {
777 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
778 });
779 injectTap(9, 8);
780 surface->expectTap(1, 2);
781
782 surface->doTransaction([](auto &t, auto &sc) {
783 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
784 });
785 injectTap(12, 9);
786 surface->expectTap(1, 2);
787}
788
789TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
790 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
791 surface->showAt(10, 10);
792 surface->doTransaction([](auto &t, auto &sc) {
793 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
794 });
795 injectTap(2, 12);
796 surface->expectTap(1, 2);
797
798 surface->doTransaction([](auto &t, auto &sc) {
799 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
800 });
801 injectTap(8, 2);
802 surface->expectTap(1, 2);
803
804 surface->doTransaction([](auto &t, auto &sc) {
805 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
806 });
807 injectTap(18, 8);
808 surface->expectTap(1, 2);
809}
810
811TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
812 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
813 surface->mInputInfo.surfaceInset = 5;
814 surface->showAt(100, 100);
815
816 surface->doTransaction([](auto &t, auto &sc) {
817 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
818 });
819 injectTap(40, 120);
820 surface->expectTap(5, 10);
821
822 surface->doTransaction([](auto &t, auto &sc) {
823 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
824 });
825 injectTap(80, 40);
826 surface->expectTap(5, 10);
827
828 surface->doTransaction([](auto &t, auto &sc) {
829 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
830 });
831 injectTap(160, 80);
832 surface->expectTap(5, 10);
833}
834
chaviw39cfa2e2020-11-04 14:19:02 -0800835TEST_F(InputSurfacesTest, touch_flag_obscured) {
836 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
837 surface->showAt(100, 100);
838
839 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
840 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
841 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800842 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000843 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
Bernardo Rufino602ef712020-12-21 11:02:18 +0000844 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
845 // the default obscured/untrusted touch filter introduced in S.
846 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800847 nonTouchableSurface->showAt(100, 100);
848
849 injectTap(190, 199);
850 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
851}
852
853TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
854 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
855 surface->showAt(100, 100);
856
857 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
858 // that will be tapped. Window behind gets touch, but with flag
859 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
860 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
861 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800862 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
863 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000864 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
865 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800866 nonTouchableSurface->showAt(0, 0);
867 parentSurface->showAt(100, 100);
868
869 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800870 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800871 t.reparent(sc, parentSurface->mSurfaceControl);
872 });
873
874 injectTap(190, 199);
875 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
876}
877
878TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
879 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
880 surface->showAt(100, 100);
881
882 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
883 // the touchable window. Window behind gets touch with no obscured flags.
884 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
885 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800886 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
887 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000888 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
889 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800890 nonTouchableSurface->showAt(0, 0);
891 parentSurface->showAt(50, 50);
892
893 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800894 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800895 t.reparent(sc, parentSurface->mSurfaceControl);
896 });
897
898 injectTap(101, 110);
899 surface->expectTap(1, 10);
900}
901
chaviw7e72caf2020-12-02 16:50:43 -0800902TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
903 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
904
905 std::unique_ptr<InputSurface> bufferSurface =
906 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800907 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000908 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800909
910 surface->showAt(10, 10);
911 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
912
913 injectTap(11, 11);
914 surface->expectTap(1, 1);
915}
916
917TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
918 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
919
chaviw39d01472021-04-08 14:26:24 -0500920 std::unique_ptr<BlastInputSurface> bufferSurface =
921 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800922 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000923 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800924
925 surface->showAt(10, 10);
926 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
927
928 injectTap(11, 11);
929 surface->expectTap(1, 1);
930}
931
Vishnu Naira066d902021-09-13 18:40:17 -0700932TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
933 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
934 surface->doTransaction(
935 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
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_scaled_without_crop_window) {
950 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
951 surface->doTransaction([&](auto &t, auto &sc) {
952 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
953 t.setMatrix(sc, 2.0, 0, 0, 2.0);
954 });
955 surface->showAt(100, 100);
956
957 injectTap(101, 101);
958
959 EXPECT_NE(surface->consumeEvent(), nullptr);
960 EXPECT_NE(surface->consumeEvent(), nullptr);
961
962 surface->requestFocus();
963 surface->assertFocusChange(true);
964 injectKey(AKEYCODE_V);
965 surface->expectKey(AKEYCODE_V);
966}
967
968TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
969 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000970 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700971 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);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000976 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700977 obscuringSurface->showAt(100, 100);
978 injectTap(101, 101);
Egor Pasko5a67a562024-01-16 16:46:45 +0100979 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -0700980
981 surface->requestFocus();
982 surface->assertFocusChange(true);
983 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +0100984 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -0700985}
986
987TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
988 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000989 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700990 surface->doTransaction(
991 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
992 surface->showAt(100, 100);
993 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800994 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000995 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700996 obscuringSurface->showAt(190, 190);
997
998 injectTap(101, 101);
999
Egor Pasko5a67a562024-01-16 16:46:45 +01001000 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001001
1002 surface->requestFocus();
1003 surface->assertFocusChange(true);
1004 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001005 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001006}
1007
1008TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
1009 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1010 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1011
1012 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1013 surface->showAt(100, 100);
1014 surface->doTransaction([&](auto &t, auto &sc) {
1015 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1016 t.reparent(sc, parentSurface->mSurfaceControl);
1017 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
1018 });
1019
1020 injectTap(101, 101);
1021
Egor Pasko5a67a562024-01-16 16:46:45 +01001022 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001023
1024 surface->requestFocus();
1025 surface->assertFocusChange(true);
1026 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001027 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001028}
1029
1030TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
1031 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1032 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1033
1034 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1035 surface->doTransaction([&](auto &t, auto &sc) {
1036 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1037 t.reparent(sc, parentSurface->mSurfaceControl);
1038 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1039 });
1040 surface->showAt(100, 100);
1041
1042 injectTap(111, 111);
1043
Egor Pasko5a67a562024-01-16 16:46:45 +01001044 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001045
1046 surface->requestFocus();
1047 surface->assertFocusChange(true);
1048 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001049 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001050}
1051
Arthur Hung49d525a2021-11-19 15:11:51 +00001052TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1053 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1054
1055 std::unique_ptr<BlastInputSurface> bufferSurface =
1056 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1057
1058 surface->showAt(100, 100);
1059 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
1060 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1061
1062 injectTap(101, 101);
1063 surface->expectTap(1, 1);
1064}
1065
Vishnu Naira066d902021-09-13 18:40:17 -07001066TEST_F(InputSurfacesTest, drop_input_policy) {
1067 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1068 surface->doTransaction(
1069 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
1070 surface->showAt(100, 100);
1071
1072 injectTap(101, 101);
1073
Egor Pasko5a67a562024-01-16 16:46:45 +01001074 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001075
1076 surface->requestFocus();
1077 surface->assertFocusChange(true);
1078 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001079 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001080}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001081
Prabir Pradhan8c285982022-01-28 09:19:39 -08001082TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1083 std::unique_ptr<InputSurface> bufferSurface =
1084 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1085
1086 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1087
1088 bufferSurface->requestFocus();
1089 bufferSurface->assertFocusChange(true);
1090}
1091
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001092/**
1093 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1094 * its own crop.
1095 */
1096TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1097 std::unique_ptr<InputSurface> parentContainer =
1098 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1099 std::unique_ptr<InputSurface> containerSurface =
1100 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1101 containerSurface->doTransaction(
1102 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1103 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1104 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1105 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1106 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1107
1108 // Receives events inside its own crop
1109 injectTap(21, 21);
1110 containerSurface->expectTap(1, 1); // Event is in layer space
1111
1112 // Does not receive events outside its crop
1113 injectTap(26, 26);
Egor Pasko5a67a562024-01-16 16:46:45 +01001114 EXPECT_EQ(containerSurface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001115}
1116
1117/**
1118 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1119 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1120 */
1121TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
1122 std::unique_ptr<InputSurface> parentContainer =
1123 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1124 std::unique_ptr<InputSurface> containerSurface =
1125 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1126 containerSurface->doTransaction(
1127 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1128 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1129 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1130 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1131 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1132
1133 // Receives events inside parent bounds
1134 injectTap(21, 21);
1135 containerSurface->expectTap(1, 1); // Event is in layer space
1136
1137 // Does not receive events outside parent bounds
1138 injectTap(31, 31);
Egor Pasko5a67a562024-01-16 16:46:45 +01001139 EXPECT_EQ(containerSurface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001140}
1141
1142/**
1143 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1144 * layer's bounds. The input events should be in the layer's coordinate space.
1145 */
1146TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1147 std::unique_ptr<InputSurface> cropLayer =
1148 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1149 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1150
1151 std::unique_ptr<InputSurface> containerSurface =
1152 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1153 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1154 containerSurface->mInputInfo.touchableRegionCropHandle =
1155 cropLayer->mSurfaceControl->getHandle();
1156 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1157
1158 // Receives events inside crop layer bounds
1159 injectTap(51, 51);
1160 containerSurface->expectTap(41, 41); // Event is in layer space
1161
1162 // Does not receive events outside crop layer bounds
1163 injectTap(21, 21);
1164 injectTap(71, 71);
Egor Pasko5a67a562024-01-16 16:46:45 +01001165 EXPECT_EQ(containerSurface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001166}
1167
Linus Tufvessona1858822022-03-04 09:32:07 +00001168TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1169 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1170
1171 parent->showAt(100, 100);
1172 injectTap(101, 101);
1173 parent->expectTap(1, 1);
1174
1175 std::unique_ptr<InputSurface> childContainerSurface =
1176 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1177 childContainerSurface->showAt(0, 0);
1178 childContainerSurface->doTransaction(
1179 [&](auto &t, auto &sc) { t.reparent(sc, parent->mSurfaceControl); });
1180 injectTap(101, 101);
1181
Egor Pasko5a67a562024-01-16 16:46:45 +01001182 EXPECT_EQ(parent->consumeEvent(/*timeout=*/100ms), nullptr);
Linus Tufvessona1858822022-03-04 09:32:07 +00001183}
1184
Vishnu Nair16a938f2021-09-24 07:14:54 -07001185class MultiDisplayTests : public InputSurfacesTest {
1186public:
1187 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Prabir Pradhand0aba782021-12-14 00:44:21 -08001188 void TearDown() override {
1189 for (auto &token : mVirtualDisplays) {
1190 SurfaceComposerClient::destroyDisplay(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001191 }
1192 InputSurfacesTest::TearDown();
1193 }
1194
Prabir Pradhand0aba782021-12-14 00:44:21 -08001195 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1196 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001197 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001198 sp<IGraphicBufferProducer> producer;
1199 BufferQueue::createBufferQueue(&producer, &consumer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001200 consumer->setConsumerName(String8("Virtual disp consumer"));
1201 consumer->setDefaultBufferSize(width, height);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001202 mProducers.push_back(producer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001203
Prabir Pradhand0aba782021-12-14 00:44:21 -08001204 std::string name = "VirtualDisplay";
1205 name += std::to_string(mVirtualDisplays.size());
1206 sp<IBinder> token = SurfaceComposerClient::createDisplay(String8(name.c_str()), isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001207 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001208 t.setDisplaySurface(token, producer);
1209 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1210 t.setDisplayLayerStack(token, layerStack);
1211 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1212 {offsetX, offsetY, offsetX + width, offsetY + height});
Vishnu Nair16a938f2021-09-24 07:14:54 -07001213 t.apply(true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001214
1215 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001216 }
1217
Prabir Pradhand0aba782021-12-14 00:44:21 -08001218 std::vector<sp<IBinder>> mVirtualDisplays;
1219 std::vector<sp<IGraphicBufferProducer>> mProducers;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001220};
1221
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001222TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001223 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1224 // Do not create a display associated with the 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
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001229 // Touches should be dropped if the layer is on an invalid display.
Prabir Pradhand0aba782021-12-14 00:44:21 -08001230 injectTapOnDisplay(101, 101, layerStack.id);
Egor Pasko5a67a562024-01-16 16:46:45 +01001231 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001232
1233 // However, we still let the window be focused and receive keys.
1234 surface->requestFocus(layerStack.id);
1235 surface->assertFocusChange(true);
1236
1237 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1238 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001239}
1240
1241TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1242 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1243 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1244 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1245 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1246 surface->showAt(100, 100);
1247
1248 injectTapOnDisplay(101, 101, layerStack.id);
1249 surface->expectTap(1, 1);
1250
1251 surface->requestFocus(layerStack.id);
1252 surface->assertFocusChange(true);
1253 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1254 surface->expectKey(AKEYCODE_V);
1255}
1256
Vishnu Nair16a938f2021-09-24 07:14:54 -07001257TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1258 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1259 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1260 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1261 surface->doTransaction([&](auto &t, auto &sc) {
1262 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1263 t.setLayerStack(sc, layerStack);
1264 });
1265 surface->showAt(100, 100);
1266
Prabir Pradhand0aba782021-12-14 00:44:21 -08001267 injectTapOnDisplay(101, 101, layerStack.id);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001268
Egor Pasko5a67a562024-01-16 16:46:45 +01001269 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001270
1271 surface->requestFocus(layerStack.id);
1272 surface->assertFocusChange(true);
1273 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
Egor Pasko5a67a562024-01-16 16:46:45 +01001274 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001275}
1276
1277TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1278 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001279
1280 // Create the secure display as system, because only certain users can create secure displays.
1281 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001282 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001283 // Change the uid back to root.
1284 seteuid(AID_ROOT);
1285
Vishnu Nair16a938f2021-09-24 07:14:54 -07001286 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1287 surface->doTransaction([&](auto &t, auto &sc) {
1288 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1289 t.setLayerStack(sc, layerStack);
1290 });
1291 surface->showAt(100, 100);
1292
1293 injectTapOnDisplay(101, 101, layerStack.id);
1294 EXPECT_NE(surface->consumeEvent(), nullptr);
1295 EXPECT_NE(surface->consumeEvent(), nullptr);
1296
1297 surface->requestFocus(layerStack.id);
1298 surface->assertFocusChange(true);
1299 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1300
1301 surface->expectKey(AKEYCODE_V);
1302}
1303
Vishnu Nair958da932020-08-21 17:12:37 -07001304} // namespace android::test