blob: 9791212e078fa64d41de4b0976751eddc7202a81 [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>
Siarhei Vishniakou0438ca82024-03-12 14:27:25 -070044#include <input/InputConsumer.h>
Chris Ye0783e992020-06-02 21:34:49 -070045#include <input/InputTransport.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070046
Marin Shalamanova7fe3042021-01-29 21:02:08 +010047#include <ui/DisplayMode.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070048#include <ui/Rect.h>
49#include <ui/Region.h>
50
Prabir Pradhand0aba782021-12-14 00:44:21 -080051#include <private/android_filesystem_config.h>
52
Chris Ye0783e992020-06-02 21:34:49 -070053using android::os::IInputFlinger;
Robert Carr1c4c5592018-09-24 13:18:43 -070054
chaviw39d01472021-04-08 14:26:24 -050055using android::hardware::graphics::common::V1_1::BufferUsage;
56
chaviw98318de2021-05-19 16:45:23 -050057using android::gui::FocusRequest;
58using android::gui::InputApplicationInfo;
59using android::gui::TouchOcclusionMode;
60using android::gui::WindowInfo;
61
Vishnu Nair958da932020-08-21 17:12:37 -070062namespace android::test {
Robert Carr1c4c5592018-09-24 13:18:43 -070063
Vishnu Nairde19f852018-12-18 16:11:53 -080064using Transaction = SurfaceComposerClient::Transaction;
65
Robert Carr1c4c5592018-09-24 13:18:43 -070066sp<IInputFlinger> getInputFlinger() {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -080067 sp<IBinder> input(defaultServiceManager()->waitForService(String16("inputflinger")));
Robert Carr1c4c5592018-09-24 13:18:43 -070068 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 {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800107 android::os::InputChannelCore tempChannel;
108 android::binder::Status result =
109 mInputFlinger->createInputChannel("testchannels", &tempChannel);
110 if (!result.isOk()) {
111 ADD_FAILURE() << "binder call to createInputChannel failed";
112 }
113 mClientChannel = InputChannel::create(std::move(tempChannel));
Linus Tufvessona1858822022-03-04 09:32:07 +0000114 mInputInfo.token = mClientChannel->getConnectionToken();
115 mInputConsumer = new InputConsumer(mClientChannel);
116 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700117
Linus Tufvessona1858822022-03-04 09:32:07 +0000118 mInputInfo.name = "Test info";
119 mInputInfo.dispatchingTimeout = 5s;
120 mInputInfo.globalScaleFactor = 1.0;
121 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
Robert Carr1c4c5592018-09-24 13:18:43 -0700122
Linus Tufvessona1858822022-03-04 09:32:07 +0000123 InputApplicationInfo aInfo;
124 aInfo.token = new BBinder();
125 aInfo.name = "Test app info";
126 aInfo.dispatchingTimeoutMillis =
127 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
128 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700129 }
130
Vishnu Nairde19f852018-12-18 16:11:53 -0800131 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
132 int width, int height) {
133 sp<SurfaceControl> surfaceControl =
134 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800135 PIXEL_FORMAT_RGBA_8888,
136 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -0800137 return std::make_unique<InputSurface>(surfaceControl, width, height);
138 }
139
140 static std::unique_ptr<InputSurface> makeBufferInputSurface(
141 const sp<SurfaceComposerClient> &scc, int width, int height) {
142 sp<SurfaceControl> surfaceControl =
143 scc->createSurface(String8("Test Buffer Surface"), width, height,
144 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
145 return std::make_unique<InputSurface>(surfaceControl, width, height);
146 }
147
148 static std::unique_ptr<InputSurface> makeContainerInputSurface(
149 const sp<SurfaceComposerClient> &scc, int width, int height) {
150 sp<SurfaceControl> surfaceControl =
151 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
152 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
153 ISurfaceComposerClient::eFXSurfaceContainer);
154 return std::make_unique<InputSurface>(surfaceControl, width, height);
155 }
156
Linus Tufvessona1858822022-03-04 09:32:07 +0000157 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
158 const sp<SurfaceComposerClient> &scc, int width, int height) {
159 sp<SurfaceControl> surfaceControl =
160 scc->createSurface(String8("Test Container Surface"), 100 /* height */,
161 100 /* width */, PIXEL_FORMAT_RGBA_8888,
162 ISurfaceComposerClient::eFXSurfaceContainer);
163 return std::make_unique<InputSurface>(surfaceControl, width, height,
164 true /* noInputChannel */);
165 }
166
arthurhungb4a0f852020-06-16 11:02:50 +0800167 static std::unique_ptr<InputSurface> makeCursorInputSurface(
168 const sp<SurfaceComposerClient> &scc, int width, int height) {
169 sp<SurfaceControl> surfaceControl =
170 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
171 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
172 ISurfaceComposerClient::eCursorWindow);
173 return std::make_unique<InputSurface>(surfaceControl, width, height);
174 }
175
Egor Pasko5a67a562024-01-16 16:46:45 +0100176 InputEvent* consumeEvent(std::chrono::milliseconds timeout = 3000ms) {
177 mClientChannel->waitForMessage(timeout);
Robert Carr1c4c5592018-09-24 13:18:43 -0700178
179 InputEvent *ev;
180 uint32_t seqId;
181 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
182 if (consumed != OK) {
183 return nullptr;
184 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100185 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
186 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700187 return ev;
188 }
189
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100190 void assertFocusChange(bool hasFocus) {
191 InputEvent *ev = consumeEvent();
192 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700193 ASSERT_EQ(InputEventType::FOCUS, ev->getType());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100194 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
195 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
196 }
197
Robert Carr1c4c5592018-09-24 13:18:43 -0700198 void expectTap(int x, int y) {
199 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100200 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700201 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700202 MotionEvent* mev = static_cast<MotionEvent*>(ev);
203 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
204 EXPECT_EQ(x, mev->getX(0));
205 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800206 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700207
208 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100209 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700210 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700211 mev = static_cast<MotionEvent*>(ev);
212 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800213 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700214 }
215
chaviw39cfa2e2020-11-04 14:19:02 -0800216 void expectTapWithFlag(int x, int y, int32_t flags) {
217 InputEvent *ev = consumeEvent();
218 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700219 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800220 MotionEvent *mev = static_cast<MotionEvent *>(ev);
221 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
222 EXPECT_EQ(x, mev->getX(0));
223 EXPECT_EQ(y, mev->getY(0));
224 EXPECT_EQ(flags, mev->getFlags() & flags);
225
226 ev = consumeEvent();
227 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700228 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800229 mev = static_cast<MotionEvent *>(ev);
230 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
231 EXPECT_EQ(flags, mev->getFlags() & flags);
232 }
233
Prabir Pradhand0aba782021-12-14 00:44:21 -0800234 void expectTapInDisplayCoordinates(int displayX, int displayY) {
235 InputEvent *ev = consumeEvent();
236 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700237 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800238 MotionEvent *mev = static_cast<MotionEvent *>(ev);
239 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
240 const PointerCoords &coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
241 EXPECT_EQ(displayX, coords.getX());
242 EXPECT_EQ(displayY, coords.getY());
243 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
244
245 ev = consumeEvent();
246 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700247 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800248 mev = static_cast<MotionEvent *>(ev);
249 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
250 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
251 }
252
Vishnu Naira066d902021-09-13 18:40:17 -0700253 void expectKey(uint32_t keycode) {
254 InputEvent *ev = consumeEvent();
255 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700256 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700257 KeyEvent *keyEvent = static_cast<KeyEvent *>(ev);
258 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
259 EXPECT_EQ(keycode, keyEvent->getKeyCode());
260 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
261
262 ev = consumeEvent();
263 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700264 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700265 keyEvent = static_cast<KeyEvent *>(ev);
266 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
267 EXPECT_EQ(keycode, keyEvent->getKeyCode());
268 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
269 }
270
chaviw39d01472021-04-08 14:26:24 -0500271 virtual ~InputSurface() {
Linus Tufvessona1858822022-03-04 09:32:07 +0000272 if (mClientChannel) {
273 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
274 }
chaviw39d01472021-04-08 14:26:24 -0500275 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700276
chaviw39d01472021-04-08 14:26:24 -0500277 virtual void doTransaction(
278 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
279 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700280 SurfaceComposerClient::Transaction t;
281 transactionBody(t, mSurfaceControl);
282 t.apply(true);
283 }
284
chaviw39d01472021-04-08 14:26:24 -0500285 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700286 SurfaceComposerClient::Transaction t;
287 t.show(mSurfaceControl);
288 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
289 t.setLayer(mSurfaceControl, LAYER_BASE);
290 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800291 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700292 t.setAlpha(mSurfaceControl, 1);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500293 auto reportedListener = sp<SynchronousWindowInfosReportedListener>::make();
294 t.addWindowInfosReportedListener(reportedListener);
295 t.apply();
296 reportedListener->wait();
Robert Carr1c4c5592018-09-24 13:18:43 -0700297 }
298
Vishnu Nair16a938f2021-09-24 07:14:54 -0700299 void requestFocus(int displayId = ADISPLAY_ID_DEFAULT) {
Vishnu Nair958da932020-08-21 17:12:37 -0700300 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800301 FocusRequest request;
302 request.token = mInputInfo.token;
303 request.windowName = mInputInfo.name;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800304 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700305 request.displayId = displayId;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800306 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700307 t.apply(true);
308 }
309
Robert Carr1c4c5592018-09-24 13:18:43 -0700310public:
311 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500312 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700313 sp<IInputFlinger> mInputFlinger;
314
chaviw98318de2021-05-19 16:45:23 -0500315 WindowInfo mInputInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700316
317 PreallocatedInputEventFactory mInputEventFactory;
318 InputConsumer* mInputConsumer;
319};
320
chaviw39d01472021-04-08 14:26:24 -0500321class BlastInputSurface : public InputSurface {
322public:
323 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
324 int height)
325 : InputSurface(sc, width, height) {
326 mParentSurfaceControl = parentSc;
327 }
328
329 ~BlastInputSurface() = default;
330
331 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
332 const sp<SurfaceComposerClient> &scc, int width, int height) {
333 sp<SurfaceControl> parentSc =
334 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
335 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
336 ISurfaceComposerClient::eFXSurfaceContainer);
337
338 sp<SurfaceControl> surfaceControl =
339 scc->createSurface(String8("Test Buffer Surface"), width, height,
340 PIXEL_FORMAT_RGBA_8888,
341 ISurfaceComposerClient::eFXSurfaceBufferState,
342 parentSc->getHandle());
343 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
344 }
345
346 void doTransaction(
347 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
348 transactionBody) override {
349 SurfaceComposerClient::Transaction t;
350 transactionBody(t, mParentSurfaceControl);
351 t.apply(true);
352 }
353
354 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
355 SurfaceComposerClient::Transaction t;
356 t.show(mParentSurfaceControl);
357 t.setLayer(mParentSurfaceControl, LAYER_BASE);
358 t.setPosition(mParentSurfaceControl, x, y);
359 t.setCrop(mParentSurfaceControl, crop);
360
361 t.show(mSurfaceControl);
362 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
363 t.setCrop(mSurfaceControl, crop);
364 t.setAlpha(mSurfaceControl, 1);
365 t.apply(true);
366 }
367
368private:
369 sp<SurfaceControl> mParentSurfaceControl;
370};
371
Robert Carr1c4c5592018-09-24 13:18:43 -0700372class InputSurfacesTest : public ::testing::Test {
373public:
374 InputSurfacesTest() {
375 ProcessState::self()->startThreadPool();
376 }
377
378 void SetUp() {
379 mComposerClient = new SurfaceComposerClient;
380 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Huihong Luo31b5ac22022-08-15 20:38:10 -0700381 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
382 ASSERT_FALSE(ids.empty());
383 // display 0 is picked for now, can extend to support all displays if needed
384 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100385 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800386
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100387 ui::DisplayMode mode;
388 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800389
390 // After a new buffer is queued, SurfaceFlinger is notified and will
391 // latch the new buffer on next vsync. Let's heuristically wait for 3
392 // vsyncs.
Alec Mouri55e31032023-10-02 20:34:18 +0000393 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.peakRefreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700394 }
395
396 void TearDown() {
397 mComposerClient->dispose();
398 }
399
400 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800401 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
402 }
403
chaviw39d01472021-04-08 14:26:24 -0500404 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
405 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
406 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
407 sp<GraphicBuffer> buffer =
408 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
409 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800410 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700411 }
412
413 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800414 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700415};
416
Vishnu Nair16a938f2021-09-24 07:14:54 -0700417void injectTapOnDisplay(int x, int y, int displayId) {
418 char *buf1, *buf2, *bufDisplayId;
Robert Carr1c4c5592018-09-24 13:18:43 -0700419 asprintf(&buf1, "%d", x);
420 asprintf(&buf2, "%d", y);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700421 asprintf(&bufDisplayId, "%d", displayId);
Robert Carr1c4c5592018-09-24 13:18:43 -0700422 if (fork() == 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700423 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
424 }
425}
426
427void injectTap(int x, int y) {
428 injectTapOnDisplay(x, y, ADISPLAY_ID_DEFAULT);
429}
430
431void injectKeyOnDisplay(uint32_t keycode, int displayId) {
432 char *buf1, *bufDisplayId;
433 asprintf(&buf1, "%d", keycode);
434 asprintf(&bufDisplayId, "%d", displayId);
435 if (fork() == 0) {
436 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
Robert Carr1c4c5592018-09-24 13:18:43 -0700437 }
438}
439
Vishnu Naira066d902021-09-13 18:40:17 -0700440void injectKey(uint32_t keycode) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700441 injectKeyOnDisplay(keycode, ADISPLAY_ID_NONE);
Vishnu Naira066d902021-09-13 18:40:17 -0700442}
443
Robert Carr1c4c5592018-09-24 13:18:43 -0700444TEST_F(InputSurfacesTest, can_receive_input) {
445 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
446 surface->showAt(100, 100);
447
448 injectTap(101, 101);
449
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100450 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700451}
452
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100453/**
454 * Set up two surfaces side-by-side. Tap each surface.
455 * Next, swap the positions of the two surfaces. Inject tap into the two
456 * original locations. Ensure that the tap is received by the surfaces in the
457 * reverse order.
458 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700459TEST_F(InputSurfacesTest, input_respects_positioning) {
460 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
461 surface->showAt(100, 100);
462
463 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
464 surface2->showAt(200, 200);
465
466 injectTap(201, 201);
467 surface2->expectTap(1, 1);
468
469 injectTap(101, 101);
470 surface->expectTap(1, 1);
471
472 surface2->doTransaction([](auto &t, auto &sc) {
473 t.setPosition(sc, 100, 100);
474 });
475 surface->doTransaction([](auto &t, auto &sc) {
476 t.setPosition(sc, 200, 200);
477 });
478
479 injectTap(101, 101);
480 surface2->expectTap(1, 1);
481
482 injectTap(201, 201);
483 surface->expectTap(1, 1);
484}
485
486TEST_F(InputSurfacesTest, input_respects_layering) {
487 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
488 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
489
490 surface->showAt(10, 10);
491 surface2->showAt(10, 10);
492
493 surface->doTransaction([](auto &t, auto &sc) {
494 t.setLayer(sc, LAYER_BASE + 1);
495 });
496
497 injectTap(11, 11);
498 surface->expectTap(1, 1);
499
500 surface2->doTransaction([](auto &t, auto &sc) {
501 t.setLayer(sc, LAYER_BASE + 1);
502 });
503
504 injectTap(11, 11);
505 surface2->expectTap(1, 1);
506
507 surface2->doTransaction([](auto &t, auto &sc) {
508 t.hide(sc);
509 });
510
511 injectTap(11, 11);
512 surface->expectTap(1, 1);
513}
514
Vishnu Nairde19f852018-12-18 16:11:53 -0800515// Surface Insets are set to offset the client content and draw a border around the client surface
516// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
517// of the client content.
518TEST_F(InputSurfacesTest, input_respects_surface_insets) {
519 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
520 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
521 bgSurface->showAt(100, 100);
522
523 fgSurface->mInputInfo.surfaceInset = 5;
524 fgSurface->showAt(100, 100);
525
526 injectTap(106, 106);
527 fgSurface->expectTap(1, 1);
528
529 injectTap(101, 101);
530 bgSurface->expectTap(1, 1);
531}
532
Vishnu Nairfed7c122023-03-18 01:54:43 +0000533TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) {
534 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
535 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
536 bgSurface->showAt(100, 100);
537
538 fgSurface->mInputInfo.surfaceInset = 5;
539 fgSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
540 fgSurface->showAt(100, 100);
541
542 injectTap(106, 106);
543 fgSurface->expectTap(1, 1);
544
545 injectTap(101, 101);
546 bgSurface->expectTap(1, 1);
547}
548
Vishnu Nairde19f852018-12-18 16:11:53 -0800549// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
550TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
551 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
552 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
553 parentSurface->showAt(100, 100);
554
555 childSurface->mInputInfo.surfaceInset = 10;
556 childSurface->showAt(100, 100);
557
558 childSurface->doTransaction([&](auto &t, auto &sc) {
559 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000560 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800561 });
562
563 injectTap(106, 106);
564 childSurface->expectTap(1, 1);
565
566 injectTap(101, 101);
567 parentSurface->expectTap(1, 1);
568}
569
Arthur Hung118b1142019-05-08 21:25:59 +0800570// Ensure a surface whose insets are scaled, handles the touch offset correctly.
571TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
572 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
573 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
574 bgSurface->showAt(100, 100);
575
576 fgSurface->mInputInfo.surfaceInset = 5;
577 fgSurface->showAt(100, 100);
578
579 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
580
581 // expect = touch / scale - inset
582 injectTap(112, 124);
583 fgSurface->expectTap(1, 1);
584
585 injectTap(101, 101);
586 bgSurface->expectTap(1, 1);
587}
588
Ady Abraham282f1d72019-07-24 18:05:56 -0700589TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700590 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
Ady Abraham282f1d72019-07-24 18:05:56 -0700591 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700592 bgSurface->showAt(100, 100);
593
Ady Abraham282f1d72019-07-24 18:05:56 -0700594 // In case we pass the very big inset without any checking.
595 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
596 fgSurface->showAt(100, 100);
597
598 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
599
600 // expect no crash for overflow, and inset size to be clamped to surface size
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700601 injectTap(112, 124);
602 bgSurface->expectTap(12, 24);
Ady Abraham282f1d72019-07-24 18:05:56 -0700603}
604
Prabir Pradhan33da9462022-06-14 14:55:57 +0000605TEST_F(InputSurfacesTest, touchable_region) {
606 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
607
608 surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31});
609
610 surface->showAt(11, 22);
611
612 // A tap within the surface but outside the touchable region should not be sent to the surface.
613 injectTap(20, 30);
Egor Pasko5a67a562024-01-16 16:46:45 +0100614 EXPECT_EQ(surface->consumeEvent(/*timeout=*/200ms), nullptr);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000615
616 injectTap(31, 52);
617 surface->expectTap(20, 30);
618}
619
620TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
621 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
622 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
623 bgSurface->showAt(100, 100);
624
625 // Set the touchable region to the values at the limit of its corresponding type.
626 // Since the surface is offset from the origin, the touchable region will be transformed into
627 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
628 // against such a situation.
629 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
630
631 fgSurface->showAt(100, 100);
632
633 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
634 // surface receives touch.
635 injectTap(112, 124);
636 bgSurface->expectTap(12, 24);
637}
638
639TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
640 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
641 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
642 bgSurface->showAt(0, 0);
643
644 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
645 fgSurface->showAt(0, 0);
646
647 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
648
649 // Expect no crash for overflow.
650 injectTap(12, 24);
Prabir Pradhana4c59bd2023-04-10 20:54:04 +0000651 bgSurface->expectTap(12, 24);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000652}
653
Vishnu Nairde19f852018-12-18 16:11:53 -0800654// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
655TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
656 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
657 surface->doTransaction([](auto &t, auto &sc) {
658 Region transparentRegion(Rect(0, 0, 10, 10));
659 t.setTransparentRegionHint(sc, transparentRegion);
660 });
661 surface->showAt(100, 100);
662 injectTap(101, 101);
663 surface->expectTap(1, 1);
664}
665
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700666// TODO(b/139494112) update tests once we define expected behavior
667// Ensure we still send input to the surface regardless of surface visibility changes due to the
668// first buffer being submitted or alpha changes.
669// Original bug ref: b/120839715
670TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800671 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500672 std::unique_ptr<BlastInputSurface> bufferSurface =
673 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800674
675 bgSurface->showAt(10, 10);
676 bufferSurface->showAt(10, 10);
677
678 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700679 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800680
chaviw39d01472021-04-08 14:26:24 -0500681 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800682 injectTap(11, 11);
683 bufferSurface->expectTap(1, 1);
684}
685
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000686TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800687 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500688 std::unique_ptr<BlastInputSurface> bufferSurface =
689 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
690 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800691
692 bgSurface->showAt(10, 10);
693 bufferSurface->showAt(10, 10);
694
695 injectTap(11, 11);
696 bufferSurface->expectTap(1, 1);
697
698 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
699
700 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000701 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800702}
703
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700704TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800705 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
706 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
707
708 bgSurface->showAt(10, 10);
709 fgSurface->showAt(10, 10);
710
711 injectTap(11, 11);
712 fgSurface->expectTap(1, 1);
713
714 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
715
716 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700717 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800718}
719
720TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
721 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
722 std::unique_ptr<InputSurface> containerSurface =
723 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
724
725 bgSurface->showAt(10, 10);
726 containerSurface->showAt(10, 10);
727
728 injectTap(11, 11);
729 containerSurface->expectTap(1, 1);
730
731 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
732
733 injectTap(11, 11);
734 bgSurface->expectTap(1, 1);
735}
chaviwfbe5d9c2018-12-26 12:23:37 -0800736
Arthur Hungd20b2702019-01-14 18:16:16 +0800737TEST_F(InputSurfacesTest, input_respects_outscreen) {
738 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
739 surface->showAt(-1, -1);
740
741 injectTap(0, 0);
742 surface->expectTap(1, 1);
743}
arthurhungb4a0f852020-06-16 11:02:50 +0800744
745TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
746 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
747 std::unique_ptr<InputSurface> cursorSurface =
748 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
749
750 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800751 cursorSurface->showAt(10, 10);
752
753 injectTap(11, 11);
754 surface->expectTap(1, 1);
755}
Vishnu Nair958da932020-08-21 17:12:37 -0700756
757TEST_F(InputSurfacesTest, can_be_focused) {
758 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
759 surface->showAt(100, 100);
760 surface->requestFocus();
761
762 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700763
764 injectKey(AKEYCODE_V);
765 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700766}
chaviw44a6d2b2020-09-08 17:14:16 -0700767
768TEST_F(InputSurfacesTest, rotate_surface) {
769 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
770 surface->showAt(10, 10);
771 surface->doTransaction([](auto &t, auto &sc) {
772 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
773 });
774 injectTap(8, 11);
775 surface->expectTap(1, 2);
776
777 surface->doTransaction([](auto &t, auto &sc) {
778 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
779 });
780 injectTap(9, 8);
781 surface->expectTap(1, 2);
782
783 surface->doTransaction([](auto &t, auto &sc) {
784 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
785 });
786 injectTap(12, 9);
787 surface->expectTap(1, 2);
788}
789
790TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
791 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
792 surface->showAt(10, 10);
793 surface->doTransaction([](auto &t, auto &sc) {
794 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
795 });
796 injectTap(2, 12);
797 surface->expectTap(1, 2);
798
799 surface->doTransaction([](auto &t, auto &sc) {
800 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
801 });
802 injectTap(8, 2);
803 surface->expectTap(1, 2);
804
805 surface->doTransaction([](auto &t, auto &sc) {
806 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
807 });
808 injectTap(18, 8);
809 surface->expectTap(1, 2);
810}
811
812TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
813 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
814 surface->mInputInfo.surfaceInset = 5;
815 surface->showAt(100, 100);
816
817 surface->doTransaction([](auto &t, auto &sc) {
818 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
819 });
820 injectTap(40, 120);
821 surface->expectTap(5, 10);
822
823 surface->doTransaction([](auto &t, auto &sc) {
824 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
825 });
826 injectTap(80, 40);
827 surface->expectTap(5, 10);
828
829 surface->doTransaction([](auto &t, auto &sc) {
830 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
831 });
832 injectTap(160, 80);
833 surface->expectTap(5, 10);
834}
835
chaviw39cfa2e2020-11-04 14:19:02 -0800836TEST_F(InputSurfacesTest, touch_flag_obscured) {
837 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
838 surface->showAt(100, 100);
839
840 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
841 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
842 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800843 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000844 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
Bernardo Rufino602ef712020-12-21 11:02:18 +0000845 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
846 // the default obscured/untrusted touch filter introduced in S.
847 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800848 nonTouchableSurface->showAt(100, 100);
849
850 injectTap(190, 199);
851 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
852}
853
854TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
855 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
856 surface->showAt(100, 100);
857
858 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
859 // that will be tapped. Window behind gets touch, but with flag
860 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
861 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
862 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800863 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
864 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000865 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
866 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800867 nonTouchableSurface->showAt(0, 0);
868 parentSurface->showAt(100, 100);
869
870 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800871 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800872 t.reparent(sc, parentSurface->mSurfaceControl);
873 });
874
875 injectTap(190, 199);
876 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
877}
878
879TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
880 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
881 surface->showAt(100, 100);
882
883 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
884 // the touchable window. Window behind gets touch with no obscured flags.
885 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
886 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800887 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
888 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000889 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
890 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800891 nonTouchableSurface->showAt(0, 0);
892 parentSurface->showAt(50, 50);
893
894 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800895 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800896 t.reparent(sc, parentSurface->mSurfaceControl);
897 });
898
899 injectTap(101, 110);
900 surface->expectTap(1, 10);
901}
902
chaviw7e72caf2020-12-02 16:50:43 -0800903TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
904 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
905
906 std::unique_ptr<InputSurface> bufferSurface =
907 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800908 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000909 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800910
911 surface->showAt(10, 10);
912 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
913
914 injectTap(11, 11);
915 surface->expectTap(1, 1);
916}
917
918TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
919 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
920
chaviw39d01472021-04-08 14:26:24 -0500921 std::unique_ptr<BlastInputSurface> bufferSurface =
922 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800923 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000924 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800925
926 surface->showAt(10, 10);
927 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
928
929 injectTap(11, 11);
930 surface->expectTap(1, 1);
931}
932
Vishnu Naira066d902021-09-13 18:40:17 -0700933TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
934 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
935 surface->doTransaction(
936 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
937 surface->showAt(100, 100);
938
939 injectTap(101, 101);
940
941 EXPECT_NE(surface->consumeEvent(), nullptr);
942 EXPECT_NE(surface->consumeEvent(), nullptr);
943
944 surface->requestFocus();
945 surface->assertFocusChange(true);
946 injectKey(AKEYCODE_V);
947 surface->expectKey(AKEYCODE_V);
948}
949
950TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
951 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
952 surface->doTransaction([&](auto &t, auto &sc) {
953 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
954 t.setMatrix(sc, 2.0, 0, 0, 2.0);
955 });
956 surface->showAt(100, 100);
957
958 injectTap(101, 101);
959
960 EXPECT_NE(surface->consumeEvent(), nullptr);
961 EXPECT_NE(surface->consumeEvent(), nullptr);
962
963 surface->requestFocus();
964 surface->assertFocusChange(true);
965 injectKey(AKEYCODE_V);
966 surface->expectKey(AKEYCODE_V);
967}
968
969TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
970 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000971 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700972 surface->doTransaction(
973 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
974 surface->showAt(100, 100);
975 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800976 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000977 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700978 obscuringSurface->showAt(100, 100);
979 injectTap(101, 101);
Egor Pasko5a67a562024-01-16 16:46:45 +0100980 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -0700981
982 surface->requestFocus();
983 surface->assertFocusChange(true);
984 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +0100985 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -0700986}
987
988TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
989 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000990 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700991 surface->doTransaction(
992 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
993 surface->showAt(100, 100);
994 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800995 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000996 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700997 obscuringSurface->showAt(190, 190);
998
999 injectTap(101, 101);
1000
Egor Pasko5a67a562024-01-16 16:46:45 +01001001 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001002
1003 surface->requestFocus();
1004 surface->assertFocusChange(true);
1005 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001006 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001007}
1008
1009TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
1010 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1011 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1012
1013 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1014 surface->showAt(100, 100);
1015 surface->doTransaction([&](auto &t, auto &sc) {
1016 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1017 t.reparent(sc, parentSurface->mSurfaceControl);
1018 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
1019 });
1020
1021 injectTap(101, 101);
1022
Egor Pasko5a67a562024-01-16 16:46:45 +01001023 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001024
1025 surface->requestFocus();
1026 surface->assertFocusChange(true);
1027 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001028 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001029}
1030
1031TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
1032 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1033 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1034
1035 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1036 surface->doTransaction([&](auto &t, auto &sc) {
1037 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1038 t.reparent(sc, parentSurface->mSurfaceControl);
1039 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1040 });
1041 surface->showAt(100, 100);
1042
1043 injectTap(111, 111);
1044
Egor Pasko5a67a562024-01-16 16:46:45 +01001045 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001046
1047 surface->requestFocus();
1048 surface->assertFocusChange(true);
1049 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001050 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001051}
1052
Arthur Hung49d525a2021-11-19 15:11:51 +00001053TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1054 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1055
1056 std::unique_ptr<BlastInputSurface> bufferSurface =
1057 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1058
1059 surface->showAt(100, 100);
1060 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
1061 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1062
1063 injectTap(101, 101);
1064 surface->expectTap(1, 1);
1065}
1066
Vishnu Naira066d902021-09-13 18:40:17 -07001067TEST_F(InputSurfacesTest, drop_input_policy) {
1068 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1069 surface->doTransaction(
1070 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
1071 surface->showAt(100, 100);
1072
1073 injectTap(101, 101);
1074
Egor Pasko5a67a562024-01-16 16:46:45 +01001075 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001076
1077 surface->requestFocus();
1078 surface->assertFocusChange(true);
1079 injectKey(AKEYCODE_V);
Egor Pasko5a67a562024-01-16 16:46:45 +01001080 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Naira066d902021-09-13 18:40:17 -07001081}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001082
Prabir Pradhan8c285982022-01-28 09:19:39 -08001083TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1084 std::unique_ptr<InputSurface> bufferSurface =
1085 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1086
1087 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1088
1089 bufferSurface->requestFocus();
1090 bufferSurface->assertFocusChange(true);
1091}
1092
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001093/**
1094 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1095 * its own crop.
1096 */
1097TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1098 std::unique_ptr<InputSurface> parentContainer =
1099 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1100 std::unique_ptr<InputSurface> containerSurface =
1101 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1102 containerSurface->doTransaction(
1103 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1104 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1105 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1106 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1107 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1108
1109 // Receives events inside its own crop
1110 injectTap(21, 21);
1111 containerSurface->expectTap(1, 1); // Event is in layer space
1112
1113 // Does not receive events outside its crop
1114 injectTap(26, 26);
Egor Pasko5a67a562024-01-16 16:46:45 +01001115 EXPECT_EQ(containerSurface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001116}
1117
1118/**
1119 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1120 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1121 */
1122TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
1123 std::unique_ptr<InputSurface> parentContainer =
1124 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1125 std::unique_ptr<InputSurface> containerSurface =
1126 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1127 containerSurface->doTransaction(
1128 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1129 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1130 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1131 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1132 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1133
1134 // Receives events inside parent bounds
1135 injectTap(21, 21);
1136 containerSurface->expectTap(1, 1); // Event is in layer space
1137
1138 // Does not receive events outside parent bounds
1139 injectTap(31, 31);
Egor Pasko5a67a562024-01-16 16:46:45 +01001140 EXPECT_EQ(containerSurface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001141}
1142
1143/**
1144 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1145 * layer's bounds. The input events should be in the layer's coordinate space.
1146 */
1147TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1148 std::unique_ptr<InputSurface> cropLayer =
1149 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1150 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1151
1152 std::unique_ptr<InputSurface> containerSurface =
1153 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1154 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1155 containerSurface->mInputInfo.touchableRegionCropHandle =
1156 cropLayer->mSurfaceControl->getHandle();
1157 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1158
1159 // Receives events inside crop layer bounds
1160 injectTap(51, 51);
1161 containerSurface->expectTap(41, 41); // Event is in layer space
1162
1163 // Does not receive events outside crop layer bounds
1164 injectTap(21, 21);
1165 injectTap(71, 71);
Egor Pasko5a67a562024-01-16 16:46:45 +01001166 EXPECT_EQ(containerSurface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001167}
1168
Linus Tufvessona1858822022-03-04 09:32:07 +00001169TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1170 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1171
1172 parent->showAt(100, 100);
1173 injectTap(101, 101);
1174 parent->expectTap(1, 1);
1175
1176 std::unique_ptr<InputSurface> childContainerSurface =
1177 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1178 childContainerSurface->showAt(0, 0);
1179 childContainerSurface->doTransaction(
1180 [&](auto &t, auto &sc) { t.reparent(sc, parent->mSurfaceControl); });
1181 injectTap(101, 101);
1182
Egor Pasko5a67a562024-01-16 16:46:45 +01001183 EXPECT_EQ(parent->consumeEvent(/*timeout=*/100ms), nullptr);
Linus Tufvessona1858822022-03-04 09:32:07 +00001184}
1185
Vishnu Nair16a938f2021-09-24 07:14:54 -07001186class MultiDisplayTests : public InputSurfacesTest {
1187public:
1188 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Prabir Pradhand0aba782021-12-14 00:44:21 -08001189 void TearDown() override {
1190 for (auto &token : mVirtualDisplays) {
1191 SurfaceComposerClient::destroyDisplay(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001192 }
1193 InputSurfacesTest::TearDown();
1194 }
1195
Prabir Pradhand0aba782021-12-14 00:44:21 -08001196 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1197 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001198 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001199 sp<IGraphicBufferProducer> producer;
1200 BufferQueue::createBufferQueue(&producer, &consumer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001201 consumer->setConsumerName(String8("Virtual disp consumer"));
1202 consumer->setDefaultBufferSize(width, height);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001203 mProducers.push_back(producer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001204
Prabir Pradhand0aba782021-12-14 00:44:21 -08001205 std::string name = "VirtualDisplay";
1206 name += std::to_string(mVirtualDisplays.size());
1207 sp<IBinder> token = SurfaceComposerClient::createDisplay(String8(name.c_str()), isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001208 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001209 t.setDisplaySurface(token, producer);
1210 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1211 t.setDisplayLayerStack(token, layerStack);
1212 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1213 {offsetX, offsetY, offsetX + width, offsetY + height});
Vishnu Nair16a938f2021-09-24 07:14:54 -07001214 t.apply(true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001215
1216 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001217 }
1218
Prabir Pradhand0aba782021-12-14 00:44:21 -08001219 std::vector<sp<IBinder>> mVirtualDisplays;
1220 std::vector<sp<IGraphicBufferProducer>> mProducers;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001221};
1222
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001223TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001224 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1225 // Do not create a display associated with the LayerStack.
1226 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1227 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1228 surface->showAt(100, 100);
1229
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001230 // Touches should be dropped if the layer is on an invalid display.
Prabir Pradhand0aba782021-12-14 00:44:21 -08001231 injectTapOnDisplay(101, 101, layerStack.id);
Egor Pasko5a67a562024-01-16 16:46:45 +01001232 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001233
1234 // However, we still let the window be focused and receive keys.
1235 surface->requestFocus(layerStack.id);
1236 surface->assertFocusChange(true);
1237
1238 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1239 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001240}
1241
1242TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1243 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1244 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1245 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1246 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1247 surface->showAt(100, 100);
1248
1249 injectTapOnDisplay(101, 101, layerStack.id);
1250 surface->expectTap(1, 1);
1251
1252 surface->requestFocus(layerStack.id);
1253 surface->assertFocusChange(true);
1254 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1255 surface->expectKey(AKEYCODE_V);
1256}
1257
Vishnu Nair16a938f2021-09-24 07:14:54 -07001258TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1259 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1260 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1261 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1262 surface->doTransaction([&](auto &t, auto &sc) {
1263 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1264 t.setLayerStack(sc, layerStack);
1265 });
1266 surface->showAt(100, 100);
1267
Prabir Pradhand0aba782021-12-14 00:44:21 -08001268 injectTapOnDisplay(101, 101, layerStack.id);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001269
Egor Pasko5a67a562024-01-16 16:46:45 +01001270 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001271
1272 surface->requestFocus(layerStack.id);
1273 surface->assertFocusChange(true);
1274 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
Egor Pasko5a67a562024-01-16 16:46:45 +01001275 EXPECT_EQ(surface->consumeEvent(/*timeout=*/100ms), nullptr);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001276}
1277
1278TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1279 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001280
1281 // Create the secure display as system, because only certain users can create secure displays.
1282 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001283 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001284 // Change the uid back to root.
1285 seteuid(AID_ROOT);
1286
Vishnu Nair16a938f2021-09-24 07:14:54 -07001287 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1288 surface->doTransaction([&](auto &t, auto &sc) {
1289 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1290 t.setLayerStack(sc, layerStack);
1291 });
1292 surface->showAt(100, 100);
1293
1294 injectTapOnDisplay(101, 101, layerStack.id);
1295 EXPECT_NE(surface->consumeEvent(), nullptr);
1296 EXPECT_NE(surface->consumeEvent(), nullptr);
1297
1298 surface->requestFocus(layerStack.id);
1299 surface->assertFocusChange(true);
1300 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1301
1302 surface->expectKey(AKEYCODE_V);
1303}
1304
Vishnu Nair958da932020-08-21 17:12:37 -07001305} // namespace android::test