blob: d4b8dbeeb94b688647498b593c550d20401fd774 [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() {
66 sp<IBinder> input(defaultServiceManager()->getService(
67 String16("inputflinger")));
68 if (input == nullptr) {
69 ALOGE("Failed to link to input service");
70 } else { ALOGE("Linked to input"); }
71 return interface_cast<IInputFlinger>(input);
72}
73
74// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
75static const int LAYER_BASE = INT32_MAX - 10;
Chris Ye6c4243b2020-07-22 12:07:12 -070076static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070077
Patrick Williams0a4981a2023-07-28 15:08:52 -050078class SynchronousWindowInfosReportedListener : public gui::BnWindowInfosReportedListener {
79public:
80 binder::Status onWindowInfosReported() override {
81 std::lock_guard<std::mutex> lock{mMutex};
82 mWindowInfosReported = true;
83 mConditionVariable.notify_one();
84 return binder::Status::ok();
85 }
86
87 void wait() {
88 std::unique_lock<std::mutex> lock{mMutex};
89 mConditionVariable.wait(lock, [&] { return mWindowInfosReported; });
90 }
91
92private:
93 std::mutex mMutex;
94 std::condition_variable mConditionVariable;
95 bool mWindowInfosReported{false};
96};
97
Robert Carr1c4c5592018-09-24 13:18:43 -070098class InputSurface {
99public:
Linus Tufvessona1858822022-03-04 09:32:07 +0000100 InputSurface(const sp<SurfaceControl> &sc, int width, int height, bool noInputChannel = false) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800101 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -0700102
103 mInputFlinger = getInputFlinger();
Linus Tufvessona1858822022-03-04 09:32:07 +0000104 if (noInputChannel) {
105 mInputInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, true);
106 } else {
107 mClientChannel = std::make_shared<InputChannel>();
108 mInputFlinger->createInputChannel("testchannels", mClientChannel.get());
109 mInputInfo.token = mClientChannel->getConnectionToken();
110 mInputConsumer = new InputConsumer(mClientChannel);
111 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700112
Linus Tufvessona1858822022-03-04 09:32:07 +0000113 mInputInfo.name = "Test info";
114 mInputInfo.dispatchingTimeout = 5s;
115 mInputInfo.globalScaleFactor = 1.0;
116 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
Robert Carr1c4c5592018-09-24 13:18:43 -0700117
Linus Tufvessona1858822022-03-04 09:32:07 +0000118 InputApplicationInfo aInfo;
119 aInfo.token = new BBinder();
120 aInfo.name = "Test app info";
121 aInfo.dispatchingTimeoutMillis =
122 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
123 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700124 }
125
Vishnu Nairde19f852018-12-18 16:11:53 -0800126 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
127 int width, int height) {
128 sp<SurfaceControl> surfaceControl =
129 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800130 PIXEL_FORMAT_RGBA_8888,
131 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -0800132 return std::make_unique<InputSurface>(surfaceControl, width, height);
133 }
134
135 static std::unique_ptr<InputSurface> makeBufferInputSurface(
136 const sp<SurfaceComposerClient> &scc, int width, int height) {
137 sp<SurfaceControl> surfaceControl =
138 scc->createSurface(String8("Test Buffer Surface"), width, height,
139 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
140 return std::make_unique<InputSurface>(surfaceControl, width, height);
141 }
142
143 static std::unique_ptr<InputSurface> makeContainerInputSurface(
144 const sp<SurfaceComposerClient> &scc, int width, int height) {
145 sp<SurfaceControl> surfaceControl =
146 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
147 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
148 ISurfaceComposerClient::eFXSurfaceContainer);
149 return std::make_unique<InputSurface>(surfaceControl, width, height);
150 }
151
Linus Tufvessona1858822022-03-04 09:32:07 +0000152 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
153 const sp<SurfaceComposerClient> &scc, int width, int height) {
154 sp<SurfaceControl> surfaceControl =
155 scc->createSurface(String8("Test Container Surface"), 100 /* height */,
156 100 /* width */, PIXEL_FORMAT_RGBA_8888,
157 ISurfaceComposerClient::eFXSurfaceContainer);
158 return std::make_unique<InputSurface>(surfaceControl, width, height,
159 true /* noInputChannel */);
160 }
161
arthurhungb4a0f852020-06-16 11:02:50 +0800162 static std::unique_ptr<InputSurface> makeCursorInputSurface(
163 const sp<SurfaceComposerClient> &scc, int width, int height) {
164 sp<SurfaceControl> surfaceControl =
165 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
166 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
167 ISurfaceComposerClient::eCursorWindow);
168 return std::make_unique<InputSurface>(surfaceControl, width, height);
169 }
170
Vishnu Naira066d902021-09-13 18:40:17 -0700171 InputEvent *consumeEvent(int timeoutMs = 3000) {
172 waitForEventAvailable(timeoutMs);
Robert Carr1c4c5592018-09-24 13:18:43 -0700173
174 InputEvent *ev;
175 uint32_t seqId;
176 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
177 if (consumed != OK) {
178 return nullptr;
179 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100180 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
181 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700182 return ev;
183 }
184
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100185 void assertFocusChange(bool hasFocus) {
186 InputEvent *ev = consumeEvent();
187 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700188 ASSERT_EQ(InputEventType::FOCUS, ev->getType());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100189 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
190 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
191 }
192
Robert Carr1c4c5592018-09-24 13:18:43 -0700193 void expectTap(int x, int y) {
194 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100195 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700196 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700197 MotionEvent* mev = static_cast<MotionEvent*>(ev);
198 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
199 EXPECT_EQ(x, mev->getX(0));
200 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800201 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700202
203 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100204 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700205 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700206 mev = static_cast<MotionEvent*>(ev);
207 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800208 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700209 }
210
chaviw39cfa2e2020-11-04 14:19:02 -0800211 void expectTapWithFlag(int x, int y, int32_t flags) {
212 InputEvent *ev = consumeEvent();
213 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700214 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800215 MotionEvent *mev = static_cast<MotionEvent *>(ev);
216 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
217 EXPECT_EQ(x, mev->getX(0));
218 EXPECT_EQ(y, mev->getY(0));
219 EXPECT_EQ(flags, mev->getFlags() & flags);
220
221 ev = consumeEvent();
222 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700223 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800224 mev = static_cast<MotionEvent *>(ev);
225 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
226 EXPECT_EQ(flags, mev->getFlags() & flags);
227 }
228
Prabir Pradhand0aba782021-12-14 00:44:21 -0800229 void expectTapInDisplayCoordinates(int displayX, int displayY) {
230 InputEvent *ev = consumeEvent();
231 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700232 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800233 MotionEvent *mev = static_cast<MotionEvent *>(ev);
234 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
235 const PointerCoords &coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
236 EXPECT_EQ(displayX, coords.getX());
237 EXPECT_EQ(displayY, coords.getY());
238 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
239
240 ev = consumeEvent();
241 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700242 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800243 mev = static_cast<MotionEvent *>(ev);
244 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
245 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
246 }
247
Vishnu Naira066d902021-09-13 18:40:17 -0700248 void expectKey(uint32_t keycode) {
249 InputEvent *ev = consumeEvent();
250 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700251 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700252 KeyEvent *keyEvent = static_cast<KeyEvent *>(ev);
253 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
254 EXPECT_EQ(keycode, keyEvent->getKeyCode());
255 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
256
257 ev = consumeEvent();
258 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700259 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700260 keyEvent = static_cast<KeyEvent *>(ev);
261 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
262 EXPECT_EQ(keycode, keyEvent->getKeyCode());
263 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
264 }
265
chaviw39d01472021-04-08 14:26:24 -0500266 virtual ~InputSurface() {
Linus Tufvessona1858822022-03-04 09:32:07 +0000267 if (mClientChannel) {
268 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
269 }
chaviw39d01472021-04-08 14:26:24 -0500270 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700271
chaviw39d01472021-04-08 14:26:24 -0500272 virtual void doTransaction(
273 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
274 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700275 SurfaceComposerClient::Transaction t;
276 transactionBody(t, mSurfaceControl);
277 t.apply(true);
278 }
279
chaviw39d01472021-04-08 14:26:24 -0500280 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700281 SurfaceComposerClient::Transaction t;
282 t.show(mSurfaceControl);
283 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
284 t.setLayer(mSurfaceControl, LAYER_BASE);
285 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800286 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700287 t.setAlpha(mSurfaceControl, 1);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500288 auto reportedListener = sp<SynchronousWindowInfosReportedListener>::make();
289 t.addWindowInfosReportedListener(reportedListener);
290 t.apply();
291 reportedListener->wait();
Robert Carr1c4c5592018-09-24 13:18:43 -0700292 }
293
Vishnu Nair16a938f2021-09-24 07:14:54 -0700294 void requestFocus(int displayId = ADISPLAY_ID_DEFAULT) {
Vishnu Nair958da932020-08-21 17:12:37 -0700295 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800296 FocusRequest request;
297 request.token = mInputInfo.token;
298 request.windowName = mInputInfo.name;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800299 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700300 request.displayId = displayId;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800301 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700302 t.apply(true);
303 }
304
Robert Carr1c4c5592018-09-24 13:18:43 -0700305private:
Vishnu Naira066d902021-09-13 18:40:17 -0700306 void waitForEventAvailable(int timeoutMs) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700307 struct pollfd fd;
308
309 fd.fd = mClientChannel->getFd();
310 fd.events = POLLIN;
Vishnu Naira066d902021-09-13 18:40:17 -0700311 poll(&fd, 1, timeoutMs);
Robert Carr1c4c5592018-09-24 13:18:43 -0700312 }
313
Robert Carr1c4c5592018-09-24 13:18:43 -0700314public:
315 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500316 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700317 sp<IInputFlinger> mInputFlinger;
318
chaviw98318de2021-05-19 16:45:23 -0500319 WindowInfo mInputInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700320
321 PreallocatedInputEventFactory mInputEventFactory;
322 InputConsumer* mInputConsumer;
323};
324
chaviw39d01472021-04-08 14:26:24 -0500325class BlastInputSurface : public InputSurface {
326public:
327 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
328 int height)
329 : InputSurface(sc, width, height) {
330 mParentSurfaceControl = parentSc;
331 }
332
333 ~BlastInputSurface() = default;
334
335 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
336 const sp<SurfaceComposerClient> &scc, int width, int height) {
337 sp<SurfaceControl> parentSc =
338 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
339 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
340 ISurfaceComposerClient::eFXSurfaceContainer);
341
342 sp<SurfaceControl> surfaceControl =
343 scc->createSurface(String8("Test Buffer Surface"), width, height,
344 PIXEL_FORMAT_RGBA_8888,
345 ISurfaceComposerClient::eFXSurfaceBufferState,
346 parentSc->getHandle());
347 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
348 }
349
350 void doTransaction(
351 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
352 transactionBody) override {
353 SurfaceComposerClient::Transaction t;
354 transactionBody(t, mParentSurfaceControl);
355 t.apply(true);
356 }
357
358 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
359 SurfaceComposerClient::Transaction t;
360 t.show(mParentSurfaceControl);
361 t.setLayer(mParentSurfaceControl, LAYER_BASE);
362 t.setPosition(mParentSurfaceControl, x, y);
363 t.setCrop(mParentSurfaceControl, crop);
364
365 t.show(mSurfaceControl);
366 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
367 t.setCrop(mSurfaceControl, crop);
368 t.setAlpha(mSurfaceControl, 1);
369 t.apply(true);
370 }
371
372private:
373 sp<SurfaceControl> mParentSurfaceControl;
374};
375
Robert Carr1c4c5592018-09-24 13:18:43 -0700376class InputSurfacesTest : public ::testing::Test {
377public:
378 InputSurfacesTest() {
379 ProcessState::self()->startThreadPool();
380 }
381
382 void SetUp() {
383 mComposerClient = new SurfaceComposerClient;
384 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Huihong Luo31b5ac22022-08-15 20:38:10 -0700385 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
386 ASSERT_FALSE(ids.empty());
387 // display 0 is picked for now, can extend to support all displays if needed
388 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100389 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800390
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100391 ui::DisplayMode mode;
392 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800393
394 // After a new buffer is queued, SurfaceFlinger is notified and will
395 // latch the new buffer on next vsync. Let's heuristically wait for 3
396 // vsyncs.
Alec Mouri55e31032023-10-02 20:34:18 +0000397 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.peakRefreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700398 }
399
400 void TearDown() {
401 mComposerClient->dispose();
402 }
403
404 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800405 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
406 }
407
chaviw39d01472021-04-08 14:26:24 -0500408 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
409 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
410 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
411 sp<GraphicBuffer> buffer =
412 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
413 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800414 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700415 }
416
417 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800418 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700419};
420
Vishnu Nair16a938f2021-09-24 07:14:54 -0700421void injectTapOnDisplay(int x, int y, int displayId) {
422 char *buf1, *buf2, *bufDisplayId;
Robert Carr1c4c5592018-09-24 13:18:43 -0700423 asprintf(&buf1, "%d", x);
424 asprintf(&buf2, "%d", y);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700425 asprintf(&bufDisplayId, "%d", displayId);
Robert Carr1c4c5592018-09-24 13:18:43 -0700426 if (fork() == 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700427 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
428 }
429}
430
431void injectTap(int x, int y) {
432 injectTapOnDisplay(x, y, ADISPLAY_ID_DEFAULT);
433}
434
435void injectKeyOnDisplay(uint32_t keycode, int displayId) {
436 char *buf1, *bufDisplayId;
437 asprintf(&buf1, "%d", keycode);
438 asprintf(&bufDisplayId, "%d", displayId);
439 if (fork() == 0) {
440 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
Robert Carr1c4c5592018-09-24 13:18:43 -0700441 }
442}
443
Vishnu Naira066d902021-09-13 18:40:17 -0700444void injectKey(uint32_t keycode) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700445 injectKeyOnDisplay(keycode, ADISPLAY_ID_NONE);
Vishnu Naira066d902021-09-13 18:40:17 -0700446}
447
Robert Carr1c4c5592018-09-24 13:18:43 -0700448TEST_F(InputSurfacesTest, can_receive_input) {
449 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
450 surface->showAt(100, 100);
451
452 injectTap(101, 101);
453
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100454 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700455}
456
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100457/**
458 * Set up two surfaces side-by-side. Tap each surface.
459 * Next, swap the positions of the two surfaces. Inject tap into the two
460 * original locations. Ensure that the tap is received by the surfaces in the
461 * reverse order.
462 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700463TEST_F(InputSurfacesTest, input_respects_positioning) {
464 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
465 surface->showAt(100, 100);
466
467 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
468 surface2->showAt(200, 200);
469
470 injectTap(201, 201);
471 surface2->expectTap(1, 1);
472
473 injectTap(101, 101);
474 surface->expectTap(1, 1);
475
476 surface2->doTransaction([](auto &t, auto &sc) {
477 t.setPosition(sc, 100, 100);
478 });
479 surface->doTransaction([](auto &t, auto &sc) {
480 t.setPosition(sc, 200, 200);
481 });
482
483 injectTap(101, 101);
484 surface2->expectTap(1, 1);
485
486 injectTap(201, 201);
487 surface->expectTap(1, 1);
488}
489
490TEST_F(InputSurfacesTest, input_respects_layering) {
491 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
492 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
493
494 surface->showAt(10, 10);
495 surface2->showAt(10, 10);
496
497 surface->doTransaction([](auto &t, auto &sc) {
498 t.setLayer(sc, LAYER_BASE + 1);
499 });
500
501 injectTap(11, 11);
502 surface->expectTap(1, 1);
503
504 surface2->doTransaction([](auto &t, auto &sc) {
505 t.setLayer(sc, LAYER_BASE + 1);
506 });
507
508 injectTap(11, 11);
509 surface2->expectTap(1, 1);
510
511 surface2->doTransaction([](auto &t, auto &sc) {
512 t.hide(sc);
513 });
514
515 injectTap(11, 11);
516 surface->expectTap(1, 1);
517}
518
Vishnu Nairde19f852018-12-18 16:11:53 -0800519// Surface Insets are set to offset the client content and draw a border around the client surface
520// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
521// of the client content.
522TEST_F(InputSurfacesTest, input_respects_surface_insets) {
523 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
524 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
525 bgSurface->showAt(100, 100);
526
527 fgSurface->mInputInfo.surfaceInset = 5;
528 fgSurface->showAt(100, 100);
529
530 injectTap(106, 106);
531 fgSurface->expectTap(1, 1);
532
533 injectTap(101, 101);
534 bgSurface->expectTap(1, 1);
535}
536
Vishnu Nairfed7c122023-03-18 01:54:43 +0000537TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) {
538 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
539 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
540 bgSurface->showAt(100, 100);
541
542 fgSurface->mInputInfo.surfaceInset = 5;
543 fgSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
544 fgSurface->showAt(100, 100);
545
546 injectTap(106, 106);
547 fgSurface->expectTap(1, 1);
548
549 injectTap(101, 101);
550 bgSurface->expectTap(1, 1);
551}
552
Vishnu Nairde19f852018-12-18 16:11:53 -0800553// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
554TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
555 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
556 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
557 parentSurface->showAt(100, 100);
558
559 childSurface->mInputInfo.surfaceInset = 10;
560 childSurface->showAt(100, 100);
561
562 childSurface->doTransaction([&](auto &t, auto &sc) {
563 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000564 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800565 });
566
567 injectTap(106, 106);
568 childSurface->expectTap(1, 1);
569
570 injectTap(101, 101);
571 parentSurface->expectTap(1, 1);
572}
573
Arthur Hung118b1142019-05-08 21:25:59 +0800574// Ensure a surface whose insets are scaled, handles the touch offset correctly.
575TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
576 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
577 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
578 bgSurface->showAt(100, 100);
579
580 fgSurface->mInputInfo.surfaceInset = 5;
581 fgSurface->showAt(100, 100);
582
583 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
584
585 // expect = touch / scale - inset
586 injectTap(112, 124);
587 fgSurface->expectTap(1, 1);
588
589 injectTap(101, 101);
590 bgSurface->expectTap(1, 1);
591}
592
Ady Abraham282f1d72019-07-24 18:05:56 -0700593TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700594 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
Ady Abraham282f1d72019-07-24 18:05:56 -0700595 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700596 bgSurface->showAt(100, 100);
597
Ady Abraham282f1d72019-07-24 18:05:56 -0700598 // In case we pass the very big inset without any checking.
599 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
600 fgSurface->showAt(100, 100);
601
602 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
603
604 // expect no crash for overflow, and inset size to be clamped to surface size
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700605 injectTap(112, 124);
606 bgSurface->expectTap(12, 24);
Ady Abraham282f1d72019-07-24 18:05:56 -0700607}
608
Prabir Pradhan33da9462022-06-14 14:55:57 +0000609TEST_F(InputSurfacesTest, touchable_region) {
610 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
611
612 surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31});
613
614 surface->showAt(11, 22);
615
616 // A tap within the surface but outside the touchable region should not be sent to the surface.
617 injectTap(20, 30);
618 EXPECT_EQ(surface->consumeEvent(200 /*timeoutMs*/), nullptr);
619
620 injectTap(31, 52);
621 surface->expectTap(20, 30);
622}
623
624TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
625 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
626 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
627 bgSurface->showAt(100, 100);
628
629 // Set the touchable region to the values at the limit of its corresponding type.
630 // Since the surface is offset from the origin, the touchable region will be transformed into
631 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
632 // against such a situation.
633 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
634
635 fgSurface->showAt(100, 100);
636
637 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
638 // surface receives touch.
639 injectTap(112, 124);
640 bgSurface->expectTap(12, 24);
641}
642
643TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
644 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
645 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
646 bgSurface->showAt(0, 0);
647
648 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
649 fgSurface->showAt(0, 0);
650
651 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
652
653 // Expect no crash for overflow.
654 injectTap(12, 24);
Prabir Pradhana4c59bd2023-04-10 20:54:04 +0000655 bgSurface->expectTap(12, 24);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000656}
657
Vishnu Nairde19f852018-12-18 16:11:53 -0800658// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
659TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
660 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
661 surface->doTransaction([](auto &t, auto &sc) {
662 Region transparentRegion(Rect(0, 0, 10, 10));
663 t.setTransparentRegionHint(sc, transparentRegion);
664 });
665 surface->showAt(100, 100);
666 injectTap(101, 101);
667 surface->expectTap(1, 1);
668}
669
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700670// TODO(b/139494112) update tests once we define expected behavior
671// Ensure we still send input to the surface regardless of surface visibility changes due to the
672// first buffer being submitted or alpha changes.
673// Original bug ref: b/120839715
674TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800675 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500676 std::unique_ptr<BlastInputSurface> bufferSurface =
677 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800678
679 bgSurface->showAt(10, 10);
680 bufferSurface->showAt(10, 10);
681
682 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700683 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800684
chaviw39d01472021-04-08 14:26:24 -0500685 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800686 injectTap(11, 11);
687 bufferSurface->expectTap(1, 1);
688}
689
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000690TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800691 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500692 std::unique_ptr<BlastInputSurface> bufferSurface =
693 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
694 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800695
696 bgSurface->showAt(10, 10);
697 bufferSurface->showAt(10, 10);
698
699 injectTap(11, 11);
700 bufferSurface->expectTap(1, 1);
701
702 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
703
704 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000705 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800706}
707
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700708TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800709 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
710 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
711
712 bgSurface->showAt(10, 10);
713 fgSurface->showAt(10, 10);
714
715 injectTap(11, 11);
716 fgSurface->expectTap(1, 1);
717
718 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
719
720 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700721 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800722}
723
724TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
725 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
726 std::unique_ptr<InputSurface> containerSurface =
727 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
728
729 bgSurface->showAt(10, 10);
730 containerSurface->showAt(10, 10);
731
732 injectTap(11, 11);
733 containerSurface->expectTap(1, 1);
734
735 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
736
737 injectTap(11, 11);
738 bgSurface->expectTap(1, 1);
739}
chaviwfbe5d9c2018-12-26 12:23:37 -0800740
Arthur Hungd20b2702019-01-14 18:16:16 +0800741TEST_F(InputSurfacesTest, input_respects_outscreen) {
742 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
743 surface->showAt(-1, -1);
744
745 injectTap(0, 0);
746 surface->expectTap(1, 1);
747}
arthurhungb4a0f852020-06-16 11:02:50 +0800748
749TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
750 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
751 std::unique_ptr<InputSurface> cursorSurface =
752 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
753
754 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800755 cursorSurface->showAt(10, 10);
756
757 injectTap(11, 11);
758 surface->expectTap(1, 1);
759}
Vishnu Nair958da932020-08-21 17:12:37 -0700760
761TEST_F(InputSurfacesTest, can_be_focused) {
762 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
763 surface->showAt(100, 100);
764 surface->requestFocus();
765
766 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700767
768 injectKey(AKEYCODE_V);
769 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700770}
chaviw44a6d2b2020-09-08 17:14:16 -0700771
772TEST_F(InputSurfacesTest, rotate_surface) {
773 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
774 surface->showAt(10, 10);
775 surface->doTransaction([](auto &t, auto &sc) {
776 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
777 });
778 injectTap(8, 11);
779 surface->expectTap(1, 2);
780
781 surface->doTransaction([](auto &t, auto &sc) {
782 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
783 });
784 injectTap(9, 8);
785 surface->expectTap(1, 2);
786
787 surface->doTransaction([](auto &t, auto &sc) {
788 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
789 });
790 injectTap(12, 9);
791 surface->expectTap(1, 2);
792}
793
794TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
795 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
796 surface->showAt(10, 10);
797 surface->doTransaction([](auto &t, auto &sc) {
798 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
799 });
800 injectTap(2, 12);
801 surface->expectTap(1, 2);
802
803 surface->doTransaction([](auto &t, auto &sc) {
804 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
805 });
806 injectTap(8, 2);
807 surface->expectTap(1, 2);
808
809 surface->doTransaction([](auto &t, auto &sc) {
810 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
811 });
812 injectTap(18, 8);
813 surface->expectTap(1, 2);
814}
815
816TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
817 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
818 surface->mInputInfo.surfaceInset = 5;
819 surface->showAt(100, 100);
820
821 surface->doTransaction([](auto &t, auto &sc) {
822 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
823 });
824 injectTap(40, 120);
825 surface->expectTap(5, 10);
826
827 surface->doTransaction([](auto &t, auto &sc) {
828 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
829 });
830 injectTap(80, 40);
831 surface->expectTap(5, 10);
832
833 surface->doTransaction([](auto &t, auto &sc) {
834 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
835 });
836 injectTap(160, 80);
837 surface->expectTap(5, 10);
838}
839
chaviw39cfa2e2020-11-04 14:19:02 -0800840TEST_F(InputSurfacesTest, touch_flag_obscured) {
841 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
842 surface->showAt(100, 100);
843
844 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
845 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
846 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800847 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000848 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
Bernardo Rufino602ef712020-12-21 11:02:18 +0000849 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
850 // the default obscured/untrusted touch filter introduced in S.
851 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800852 nonTouchableSurface->showAt(100, 100);
853
854 injectTap(190, 199);
855 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
856}
857
858TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
859 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
860 surface->showAt(100, 100);
861
862 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
863 // that will be tapped. Window behind gets touch, but with flag
864 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
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);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000869 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
870 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800871 nonTouchableSurface->showAt(0, 0);
872 parentSurface->showAt(100, 100);
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(190, 199);
880 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
881}
882
883TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
884 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
885 surface->showAt(100, 100);
886
887 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
888 // the touchable window. Window behind gets touch with no obscured flags.
889 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
890 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800891 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
892 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000893 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
894 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800895 nonTouchableSurface->showAt(0, 0);
896 parentSurface->showAt(50, 50);
897
898 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800899 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800900 t.reparent(sc, parentSurface->mSurfaceControl);
901 });
902
903 injectTap(101, 110);
904 surface->expectTap(1, 10);
905}
906
chaviw7e72caf2020-12-02 16:50:43 -0800907TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
908 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
909
910 std::unique_ptr<InputSurface> bufferSurface =
911 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800912 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000913 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800914
915 surface->showAt(10, 10);
916 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
917
918 injectTap(11, 11);
919 surface->expectTap(1, 1);
920}
921
922TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
923 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
924
chaviw39d01472021-04-08 14:26:24 -0500925 std::unique_ptr<BlastInputSurface> bufferSurface =
926 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800927 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000928 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800929
930 surface->showAt(10, 10);
931 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
932
933 injectTap(11, 11);
934 surface->expectTap(1, 1);
935}
936
Vishnu Naira066d902021-09-13 18:40:17 -0700937TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
938 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
939 surface->doTransaction(
940 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
941 surface->showAt(100, 100);
942
943 injectTap(101, 101);
944
945 EXPECT_NE(surface->consumeEvent(), nullptr);
946 EXPECT_NE(surface->consumeEvent(), nullptr);
947
948 surface->requestFocus();
949 surface->assertFocusChange(true);
950 injectKey(AKEYCODE_V);
951 surface->expectKey(AKEYCODE_V);
952}
953
954TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
955 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
956 surface->doTransaction([&](auto &t, auto &sc) {
957 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
958 t.setMatrix(sc, 2.0, 0, 0, 2.0);
959 });
960 surface->showAt(100, 100);
961
962 injectTap(101, 101);
963
964 EXPECT_NE(surface->consumeEvent(), nullptr);
965 EXPECT_NE(surface->consumeEvent(), nullptr);
966
967 surface->requestFocus();
968 surface->assertFocusChange(true);
969 injectKey(AKEYCODE_V);
970 surface->expectKey(AKEYCODE_V);
971}
972
973TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
974 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000975 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700976 surface->doTransaction(
977 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
978 surface->showAt(100, 100);
979 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800980 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000981 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700982 obscuringSurface->showAt(100, 100);
983 injectTap(101, 101);
984 EXPECT_EQ(surface->consumeEvent(100), nullptr);
985
986 surface->requestFocus();
987 surface->assertFocusChange(true);
988 injectKey(AKEYCODE_V);
989 EXPECT_EQ(surface->consumeEvent(100), nullptr);
990}
991
992TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
993 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000994 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700995 surface->doTransaction(
996 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
997 surface->showAt(100, 100);
998 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800999 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001000 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -07001001 obscuringSurface->showAt(190, 190);
1002
1003 injectTap(101, 101);
1004
1005 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1006
1007 surface->requestFocus();
1008 surface->assertFocusChange(true);
1009 injectKey(AKEYCODE_V);
1010 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1011}
1012
1013TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
1014 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1015 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1016
1017 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1018 surface->showAt(100, 100);
1019 surface->doTransaction([&](auto &t, auto &sc) {
1020 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1021 t.reparent(sc, parentSurface->mSurfaceControl);
1022 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
1023 });
1024
1025 injectTap(101, 101);
1026
1027 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1028
1029 surface->requestFocus();
1030 surface->assertFocusChange(true);
1031 injectKey(AKEYCODE_V);
1032 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1033}
1034
1035TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
1036 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1037 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1038
1039 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1040 surface->doTransaction([&](auto &t, auto &sc) {
1041 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1042 t.reparent(sc, parentSurface->mSurfaceControl);
1043 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1044 });
1045 surface->showAt(100, 100);
1046
1047 injectTap(111, 111);
1048
1049 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1050
1051 surface->requestFocus();
1052 surface->assertFocusChange(true);
1053 injectKey(AKEYCODE_V);
1054 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1055}
1056
Arthur Hung49d525a2021-11-19 15:11:51 +00001057TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1058 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1059
1060 std::unique_ptr<BlastInputSurface> bufferSurface =
1061 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1062
1063 surface->showAt(100, 100);
1064 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
1065 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1066
1067 injectTap(101, 101);
1068 surface->expectTap(1, 1);
1069}
1070
Vishnu Naira066d902021-09-13 18:40:17 -07001071TEST_F(InputSurfacesTest, drop_input_policy) {
1072 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1073 surface->doTransaction(
1074 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
1075 surface->showAt(100, 100);
1076
1077 injectTap(101, 101);
1078
1079 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1080
1081 surface->requestFocus();
1082 surface->assertFocusChange(true);
1083 injectKey(AKEYCODE_V);
1084 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1085}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001086
Prabir Pradhan8c285982022-01-28 09:19:39 -08001087TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1088 std::unique_ptr<InputSurface> bufferSurface =
1089 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1090
1091 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1092
1093 bufferSurface->requestFocus();
1094 bufferSurface->assertFocusChange(true);
1095}
1096
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001097/**
1098 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1099 * its own crop.
1100 */
1101TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1102 std::unique_ptr<InputSurface> parentContainer =
1103 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1104 std::unique_ptr<InputSurface> containerSurface =
1105 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1106 containerSurface->doTransaction(
1107 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1108 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1109 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1110 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1111 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1112
1113 // Receives events inside its own crop
1114 injectTap(21, 21);
1115 containerSurface->expectTap(1, 1); // Event is in layer space
1116
1117 // Does not receive events outside its crop
1118 injectTap(26, 26);
1119 EXPECT_EQ(containerSurface->consumeEvent(100), nullptr);
1120}
1121
1122/**
1123 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1124 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1125 */
1126TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
1127 std::unique_ptr<InputSurface> parentContainer =
1128 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1129 std::unique_ptr<InputSurface> containerSurface =
1130 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1131 containerSurface->doTransaction(
1132 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1133 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1134 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1135 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1136 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1137
1138 // Receives events inside parent bounds
1139 injectTap(21, 21);
1140 containerSurface->expectTap(1, 1); // Event is in layer space
1141
1142 // Does not receive events outside parent bounds
1143 injectTap(31, 31);
1144 EXPECT_EQ(containerSurface->consumeEvent(100), nullptr);
1145}
1146
1147/**
1148 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1149 * layer's bounds. The input events should be in the layer's coordinate space.
1150 */
1151TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1152 std::unique_ptr<InputSurface> cropLayer =
1153 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1154 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1155
1156 std::unique_ptr<InputSurface> containerSurface =
1157 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1158 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1159 containerSurface->mInputInfo.touchableRegionCropHandle =
1160 cropLayer->mSurfaceControl->getHandle();
1161 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1162
1163 // Receives events inside crop layer bounds
1164 injectTap(51, 51);
1165 containerSurface->expectTap(41, 41); // Event is in layer space
1166
1167 // Does not receive events outside crop layer bounds
1168 injectTap(21, 21);
1169 injectTap(71, 71);
1170 EXPECT_EQ(containerSurface->consumeEvent(100), nullptr);
1171}
1172
Linus Tufvessona1858822022-03-04 09:32:07 +00001173TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1174 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1175
1176 parent->showAt(100, 100);
1177 injectTap(101, 101);
1178 parent->expectTap(1, 1);
1179
1180 std::unique_ptr<InputSurface> childContainerSurface =
1181 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1182 childContainerSurface->showAt(0, 0);
1183 childContainerSurface->doTransaction(
1184 [&](auto &t, auto &sc) { t.reparent(sc, parent->mSurfaceControl); });
1185 injectTap(101, 101);
1186
1187 EXPECT_EQ(parent->consumeEvent(100), nullptr);
1188}
1189
Vishnu Nair16a938f2021-09-24 07:14:54 -07001190class MultiDisplayTests : public InputSurfacesTest {
1191public:
1192 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Prabir Pradhand0aba782021-12-14 00:44:21 -08001193 void TearDown() override {
1194 for (auto &token : mVirtualDisplays) {
1195 SurfaceComposerClient::destroyDisplay(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001196 }
1197 InputSurfacesTest::TearDown();
1198 }
1199
Prabir Pradhand0aba782021-12-14 00:44:21 -08001200 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1201 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001202 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001203 sp<IGraphicBufferProducer> producer;
1204 BufferQueue::createBufferQueue(&producer, &consumer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001205 consumer->setConsumerName(String8("Virtual disp consumer"));
1206 consumer->setDefaultBufferSize(width, height);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001207 mProducers.push_back(producer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001208
Prabir Pradhand0aba782021-12-14 00:44:21 -08001209 std::string name = "VirtualDisplay";
1210 name += std::to_string(mVirtualDisplays.size());
1211 sp<IBinder> token = SurfaceComposerClient::createDisplay(String8(name.c_str()), isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001212 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001213 t.setDisplaySurface(token, producer);
1214 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1215 t.setDisplayLayerStack(token, layerStack);
1216 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1217 {offsetX, offsetY, offsetX + width, offsetY + height});
Vishnu Nair16a938f2021-09-24 07:14:54 -07001218 t.apply(true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001219
1220 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001221 }
1222
Prabir Pradhand0aba782021-12-14 00:44:21 -08001223 std::vector<sp<IBinder>> mVirtualDisplays;
1224 std::vector<sp<IGraphicBufferProducer>> mProducers;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001225};
1226
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001227TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001228 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1229 // Do not create a display associated with the LayerStack.
1230 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1231 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1232 surface->showAt(100, 100);
1233
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001234 // Touches should be dropped if the layer is on an invalid display.
Prabir Pradhand0aba782021-12-14 00:44:21 -08001235 injectTapOnDisplay(101, 101, layerStack.id);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001236 EXPECT_EQ(surface->consumeEvent(100), nullptr);
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001237
1238 // However, we still let the window be focused and receive keys.
1239 surface->requestFocus(layerStack.id);
1240 surface->assertFocusChange(true);
1241
1242 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1243 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001244}
1245
1246TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1247 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1248 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1249 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1250 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1251 surface->showAt(100, 100);
1252
1253 injectTapOnDisplay(101, 101, layerStack.id);
1254 surface->expectTap(1, 1);
1255
1256 surface->requestFocus(layerStack.id);
1257 surface->assertFocusChange(true);
1258 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1259 surface->expectKey(AKEYCODE_V);
1260}
1261
Vishnu Nair16a938f2021-09-24 07:14:54 -07001262TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1263 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1264 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1265 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1266 surface->doTransaction([&](auto &t, auto &sc) {
1267 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1268 t.setLayerStack(sc, layerStack);
1269 });
1270 surface->showAt(100, 100);
1271
Prabir Pradhand0aba782021-12-14 00:44:21 -08001272 injectTapOnDisplay(101, 101, layerStack.id);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001273
1274 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1275
1276 surface->requestFocus(layerStack.id);
1277 surface->assertFocusChange(true);
1278 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1279 EXPECT_EQ(surface->consumeEvent(100), nullptr);
1280}
1281
1282TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1283 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001284
1285 // Create the secure display as system, because only certain users can create secure displays.
1286 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001287 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001288 // Change the uid back to root.
1289 seteuid(AID_ROOT);
1290
Vishnu Nair16a938f2021-09-24 07:14:54 -07001291 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1292 surface->doTransaction([&](auto &t, auto &sc) {
1293 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1294 t.setLayerStack(sc, layerStack);
1295 });
1296 surface->showAt(100, 100);
1297
1298 injectTapOnDisplay(101, 101, layerStack.id);
1299 EXPECT_NE(surface->consumeEvent(), nullptr);
1300 EXPECT_NE(surface->consumeEvent(), nullptr);
1301
1302 surface->requestFocus(layerStack.id);
1303 surface->assertFocusChange(true);
1304 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1305
1306 surface->expectKey(AKEYCODE_V);
1307}
1308
Vishnu Nair958da932020-08-21 17:12:37 -07001309} // namespace android::test