blob: f441eaa95a762cec0e80ddee996d53ab26b61af4 [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
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070027#include <android-base/thread_annotations.h>
Patrick Williams0a4981a2023-07-28 15:08:52 -050028#include <android/gui/BnWindowInfosReportedListener.h>
Vishnu Naira066d902021-09-13 18:40:17 -070029#include <android/keycodes.h>
Vishnu Nairde19f852018-12-18 16:11:53 -080030#include <android/native_window.h>
31
Robert Carr1c4c5592018-09-24 13:18:43 -070032#include <binder/Binder.h>
33#include <binder/IServiceManager.h>
34#include <binder/Parcel.h>
35#include <binder/ProcessState.h>
36
Vishnu Nairde19f852018-12-18 16:11:53 -080037#include <gui/ISurfaceComposer.h>
38#include <gui/Surface.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070039#include <gui/SurfaceComposerClient.h>
40#include <gui/SurfaceControl.h>
41
Chris Ye0783e992020-06-02 21:34:49 -070042#include <android/os/IInputFlinger.h>
chaviw98318de2021-05-19 16:45:23 -050043#include <gui/WindowInfo.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070044#include <input/Input.h>
Siarhei Vishniakou0438ca82024-03-12 14:27:25 -070045#include <input/InputConsumer.h>
Chris Ye0783e992020-06-02 21:34:49 -070046#include <input/InputTransport.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070047
Marin Shalamanova7fe3042021-01-29 21:02:08 +010048#include <ui/DisplayMode.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070049#include <ui/Rect.h>
50#include <ui/Region.h>
51
Prabir Pradhand0aba782021-12-14 00:44:21 -080052#include <private/android_filesystem_config.h>
53
Chris Ye0783e992020-06-02 21:34:49 -070054using android::os::IInputFlinger;
Robert Carr1c4c5592018-09-24 13:18:43 -070055
chaviw39d01472021-04-08 14:26:24 -050056using android::hardware::graphics::common::V1_1::BufferUsage;
57
chaviw98318de2021-05-19 16:45:23 -050058using android::gui::FocusRequest;
59using android::gui::InputApplicationInfo;
60using android::gui::TouchOcclusionMode;
61using android::gui::WindowInfo;
62
Vishnu Nair958da932020-08-21 17:12:37 -070063namespace android::test {
Robert Carr1c4c5592018-09-24 13:18:43 -070064
Vishnu Nairde19f852018-12-18 16:11:53 -080065using Transaction = SurfaceComposerClient::Transaction;
66
Robert Carr1c4c5592018-09-24 13:18:43 -070067sp<IInputFlinger> getInputFlinger() {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -080068 sp<IBinder> input(defaultServiceManager()->waitForService(String16("inputflinger")));
Robert Carr1c4c5592018-09-24 13:18:43 -070069 if (input == nullptr) {
70 ALOGE("Failed to link to input service");
71 } else { ALOGE("Linked to input"); }
72 return interface_cast<IInputFlinger>(input);
73}
74
75// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
76static const int LAYER_BASE = INT32_MAX - 10;
Chris Ye6c4243b2020-07-22 12:07:12 -070077static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070078
Patrick Williams0a4981a2023-07-28 15:08:52 -050079class SynchronousWindowInfosReportedListener : public gui::BnWindowInfosReportedListener {
80public:
81 binder::Status onWindowInfosReported() override {
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070082 std::scoped_lock lock{mLock};
Patrick Williams0a4981a2023-07-28 15:08:52 -050083 mWindowInfosReported = true;
84 mConditionVariable.notify_one();
85 return binder::Status::ok();
86 }
87
88 void wait() {
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070089 std::unique_lock lock{mLock};
90 android::base::ScopedLockAssertion assumeLocked(mLock);
91 mConditionVariable.wait(lock, [&]() REQUIRES(mLock) { return mWindowInfosReported; });
Patrick Williams0a4981a2023-07-28 15:08:52 -050092 }
93
94private:
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070095 std::mutex mLock;
Patrick Williams0a4981a2023-07-28 15:08:52 -050096 std::condition_variable mConditionVariable;
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070097 bool mWindowInfosReported GUARDED_BY(mLock){false};
Patrick Williams0a4981a2023-07-28 15:08:52 -050098};
99
Robert Carr1c4c5592018-09-24 13:18:43 -0700100class InputSurface {
101public:
Linus Tufvessona1858822022-03-04 09:32:07 +0000102 InputSurface(const sp<SurfaceControl> &sc, int width, int height, bool noInputChannel = false) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800103 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -0700104
105 mInputFlinger = getInputFlinger();
Linus Tufvessona1858822022-03-04 09:32:07 +0000106 if (noInputChannel) {
107 mInputInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, true);
108 } else {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800109 android::os::InputChannelCore tempChannel;
110 android::binder::Status result =
111 mInputFlinger->createInputChannel("testchannels", &tempChannel);
112 if (!result.isOk()) {
113 ADD_FAILURE() << "binder call to createInputChannel failed";
114 }
115 mClientChannel = InputChannel::create(std::move(tempChannel));
Linus Tufvessona1858822022-03-04 09:32:07 +0000116 mInputInfo.token = mClientChannel->getConnectionToken();
117 mInputConsumer = new InputConsumer(mClientChannel);
118 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700119
Linus Tufvessona1858822022-03-04 09:32:07 +0000120 mInputInfo.name = "Test info";
121 mInputInfo.dispatchingTimeout = 5s;
122 mInputInfo.globalScaleFactor = 1.0;
123 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
Robert Carr1c4c5592018-09-24 13:18:43 -0700124
Linus Tufvessona1858822022-03-04 09:32:07 +0000125 InputApplicationInfo aInfo;
126 aInfo.token = new BBinder();
127 aInfo.name = "Test app info";
128 aInfo.dispatchingTimeoutMillis =
129 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
130 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700131 }
132
Vishnu Nairde19f852018-12-18 16:11:53 -0800133 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc,
134 int width, int height) {
135 sp<SurfaceControl> surfaceControl =
136 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800137 PIXEL_FORMAT_RGBA_8888,
138 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -0800139 return std::make_unique<InputSurface>(surfaceControl, width, height);
140 }
141
142 static std::unique_ptr<InputSurface> makeBufferInputSurface(
143 const sp<SurfaceComposerClient> &scc, int width, int height) {
144 sp<SurfaceControl> surfaceControl =
145 scc->createSurface(String8("Test Buffer Surface"), width, height,
146 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
147 return std::make_unique<InputSurface>(surfaceControl, width, height);
148 }
149
150 static std::unique_ptr<InputSurface> makeContainerInputSurface(
151 const sp<SurfaceComposerClient> &scc, int width, int height) {
152 sp<SurfaceControl> surfaceControl =
153 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
154 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
155 ISurfaceComposerClient::eFXSurfaceContainer);
156 return std::make_unique<InputSurface>(surfaceControl, width, height);
157 }
158
Linus Tufvessona1858822022-03-04 09:32:07 +0000159 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
160 const sp<SurfaceComposerClient> &scc, int width, int height) {
161 sp<SurfaceControl> surfaceControl =
162 scc->createSurface(String8("Test Container Surface"), 100 /* height */,
163 100 /* width */, PIXEL_FORMAT_RGBA_8888,
164 ISurfaceComposerClient::eFXSurfaceContainer);
165 return std::make_unique<InputSurface>(surfaceControl, width, height,
166 true /* noInputChannel */);
167 }
168
arthurhungb4a0f852020-06-16 11:02:50 +0800169 static std::unique_ptr<InputSurface> makeCursorInputSurface(
170 const sp<SurfaceComposerClient> &scc, int width, int height) {
171 sp<SurfaceControl> surfaceControl =
172 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
173 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
174 ISurfaceComposerClient::eCursorWindow);
175 return std::make_unique<InputSurface>(surfaceControl, width, height);
176 }
177
Egor Pasko5a67a562024-01-16 16:46:45 +0100178 InputEvent* consumeEvent(std::chrono::milliseconds timeout = 3000ms) {
179 mClientChannel->waitForMessage(timeout);
Robert Carr1c4c5592018-09-24 13:18:43 -0700180
181 InputEvent *ev;
182 uint32_t seqId;
183 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
184 if (consumed != OK) {
185 return nullptr;
186 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100187 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
188 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700189 return ev;
190 }
191
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100192 void assertFocusChange(bool hasFocus) {
193 InputEvent *ev = consumeEvent();
194 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700195 ASSERT_EQ(InputEventType::FOCUS, ev->getType());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100196 FocusEvent *focusEvent = static_cast<FocusEvent *>(ev);
197 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
198 }
199
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700200 void expectTap(float x, float y) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700201 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100202 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700203 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700204 MotionEvent* mev = static_cast<MotionEvent*>(ev);
205 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
206 EXPECT_EQ(x, mev->getX(0));
207 EXPECT_EQ(y, mev->getY(0));
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 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100211 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700212 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700213 mev = static_cast<MotionEvent*>(ev);
214 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800215 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700216 }
217
chaviw39cfa2e2020-11-04 14:19:02 -0800218 void expectTapWithFlag(int x, int y, int32_t flags) {
219 InputEvent *ev = consumeEvent();
220 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700221 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800222 MotionEvent *mev = static_cast<MotionEvent *>(ev);
223 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
224 EXPECT_EQ(x, mev->getX(0));
225 EXPECT_EQ(y, mev->getY(0));
226 EXPECT_EQ(flags, mev->getFlags() & flags);
227
228 ev = consumeEvent();
229 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700230 ASSERT_EQ(InputEventType::MOTION, ev->getType());
chaviw39cfa2e2020-11-04 14:19:02 -0800231 mev = static_cast<MotionEvent *>(ev);
232 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
233 EXPECT_EQ(flags, mev->getFlags() & flags);
234 }
235
Prabir Pradhand0aba782021-12-14 00:44:21 -0800236 void expectTapInDisplayCoordinates(int displayX, int displayY) {
237 InputEvent *ev = consumeEvent();
238 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700239 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800240 MotionEvent *mev = static_cast<MotionEvent *>(ev);
241 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
242 const PointerCoords &coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
243 EXPECT_EQ(displayX, coords.getX());
244 EXPECT_EQ(displayY, coords.getY());
245 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
246
247 ev = consumeEvent();
248 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700249 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Prabir Pradhand0aba782021-12-14 00:44:21 -0800250 mev = static_cast<MotionEvent *>(ev);
251 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
252 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
253 }
254
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -0700255 void expectKey(int32_t keycode) {
Vishnu Naira066d902021-09-13 18:40:17 -0700256 InputEvent *ev = consumeEvent();
257 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700258 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700259 KeyEvent *keyEvent = static_cast<KeyEvent *>(ev);
260 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
261 EXPECT_EQ(keycode, keyEvent->getKeyCode());
262 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
263
264 ev = consumeEvent();
265 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700266 ASSERT_EQ(InputEventType::KEY, ev->getType());
Vishnu Naira066d902021-09-13 18:40:17 -0700267 keyEvent = static_cast<KeyEvent *>(ev);
268 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
269 EXPECT_EQ(keycode, keyEvent->getKeyCode());
270 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
271 }
272
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700273 void assertNoEvent() {
274 InputEvent* event = consumeEvent(/*timeout=*/100ms);
275 ASSERT_EQ(event, nullptr) << "Expected no event, but got " << *event;
276 }
277
chaviw39d01472021-04-08 14:26:24 -0500278 virtual ~InputSurface() {
Linus Tufvessona1858822022-03-04 09:32:07 +0000279 if (mClientChannel) {
280 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
281 }
chaviw39d01472021-04-08 14:26:24 -0500282 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700283
chaviw39d01472021-04-08 14:26:24 -0500284 virtual void doTransaction(
285 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
286 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700287 SurfaceComposerClient::Transaction t;
288 transactionBody(t, mSurfaceControl);
289 t.apply(true);
290 }
291
chaviw39d01472021-04-08 14:26:24 -0500292 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700293 SurfaceComposerClient::Transaction t;
294 t.show(mSurfaceControl);
295 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
296 t.setLayer(mSurfaceControl, LAYER_BASE);
297 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800298 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700299 t.setAlpha(mSurfaceControl, 1);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500300 auto reportedListener = sp<SynchronousWindowInfosReportedListener>::make();
301 t.addWindowInfosReportedListener(reportedListener);
302 t.apply();
303 reportedListener->wait();
Robert Carr1c4c5592018-09-24 13:18:43 -0700304 }
305
Vishnu Nair16a938f2021-09-24 07:14:54 -0700306 void requestFocus(int displayId = ADISPLAY_ID_DEFAULT) {
Vishnu Nair958da932020-08-21 17:12:37 -0700307 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800308 FocusRequest request;
309 request.token = mInputInfo.token;
310 request.windowName = mInputInfo.name;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800311 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700312 request.displayId = displayId;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800313 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700314 t.apply(true);
315 }
316
Robert Carr1c4c5592018-09-24 13:18:43 -0700317public:
318 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500319 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700320 sp<IInputFlinger> mInputFlinger;
321
chaviw98318de2021-05-19 16:45:23 -0500322 WindowInfo mInputInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700323
324 PreallocatedInputEventFactory mInputEventFactory;
325 InputConsumer* mInputConsumer;
326};
327
chaviw39d01472021-04-08 14:26:24 -0500328class BlastInputSurface : public InputSurface {
329public:
330 BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width,
331 int height)
332 : InputSurface(sc, width, height) {
333 mParentSurfaceControl = parentSc;
334 }
335
336 ~BlastInputSurface() = default;
337
338 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
339 const sp<SurfaceComposerClient> &scc, int width, int height) {
340 sp<SurfaceControl> parentSc =
341 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
342 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
343 ISurfaceComposerClient::eFXSurfaceContainer);
344
345 sp<SurfaceControl> surfaceControl =
346 scc->createSurface(String8("Test Buffer Surface"), width, height,
347 PIXEL_FORMAT_RGBA_8888,
348 ISurfaceComposerClient::eFXSurfaceBufferState,
349 parentSc->getHandle());
350 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
351 }
352
353 void doTransaction(
354 std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)>
355 transactionBody) override {
356 SurfaceComposerClient::Transaction t;
357 transactionBody(t, mParentSurfaceControl);
358 t.apply(true);
359 }
360
361 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
362 SurfaceComposerClient::Transaction t;
363 t.show(mParentSurfaceControl);
364 t.setLayer(mParentSurfaceControl, LAYER_BASE);
365 t.setPosition(mParentSurfaceControl, x, y);
366 t.setCrop(mParentSurfaceControl, crop);
367
368 t.show(mSurfaceControl);
369 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
370 t.setCrop(mSurfaceControl, crop);
371 t.setAlpha(mSurfaceControl, 1);
372 t.apply(true);
373 }
374
375private:
376 sp<SurfaceControl> mParentSurfaceControl;
377};
378
Robert Carr1c4c5592018-09-24 13:18:43 -0700379class InputSurfacesTest : public ::testing::Test {
380public:
381 InputSurfacesTest() {
382 ProcessState::self()->startThreadPool();
383 }
384
385 void SetUp() {
386 mComposerClient = new SurfaceComposerClient;
387 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Huihong Luo31b5ac22022-08-15 20:38:10 -0700388 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
389 ASSERT_FALSE(ids.empty());
390 // display 0 is picked for now, can extend to support all displays if needed
391 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100392 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800393
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100394 ui::DisplayMode mode;
395 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800396
397 // After a new buffer is queued, SurfaceFlinger is notified and will
398 // latch the new buffer on next vsync. Let's heuristically wait for 3
399 // vsyncs.
Alec Mouri55e31032023-10-02 20:34:18 +0000400 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.peakRefreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700401 }
402
403 void TearDown() {
404 mComposerClient->dispose();
405 }
406
407 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800408 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
409 }
410
chaviw39d01472021-04-08 14:26:24 -0500411 void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) {
412 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
413 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
414 sp<GraphicBuffer> buffer =
415 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
416 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800417 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700418 }
419
420 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800421 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700422};
423
Vishnu Nair16a938f2021-09-24 07:14:54 -0700424void injectTapOnDisplay(int x, int y, int displayId) {
425 char *buf1, *buf2, *bufDisplayId;
Robert Carr1c4c5592018-09-24 13:18:43 -0700426 asprintf(&buf1, "%d", x);
427 asprintf(&buf2, "%d", y);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700428 asprintf(&bufDisplayId, "%d", displayId);
Robert Carr1c4c5592018-09-24 13:18:43 -0700429 if (fork() == 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700430 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
431 }
432}
433
434void injectTap(int x, int y) {
435 injectTapOnDisplay(x, y, ADISPLAY_ID_DEFAULT);
436}
437
438void injectKeyOnDisplay(uint32_t keycode, int displayId) {
439 char *buf1, *bufDisplayId;
440 asprintf(&buf1, "%d", keycode);
441 asprintf(&bufDisplayId, "%d", displayId);
442 if (fork() == 0) {
443 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
Robert Carr1c4c5592018-09-24 13:18:43 -0700444 }
445}
446
Vishnu Naira066d902021-09-13 18:40:17 -0700447void injectKey(uint32_t keycode) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700448 injectKeyOnDisplay(keycode, ADISPLAY_ID_NONE);
Vishnu Naira066d902021-09-13 18:40:17 -0700449}
450
Robert Carr1c4c5592018-09-24 13:18:43 -0700451TEST_F(InputSurfacesTest, can_receive_input) {
452 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
453 surface->showAt(100, 100);
454
455 injectTap(101, 101);
456
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100457 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700458}
459
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100460/**
461 * Set up two surfaces side-by-side. Tap each surface.
462 * Next, swap the positions of the two surfaces. Inject tap into the two
463 * original locations. Ensure that the tap is received by the surfaces in the
464 * reverse order.
465 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700466TEST_F(InputSurfacesTest, input_respects_positioning) {
467 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
468 surface->showAt(100, 100);
469
470 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
471 surface2->showAt(200, 200);
472
473 injectTap(201, 201);
474 surface2->expectTap(1, 1);
475
476 injectTap(101, 101);
477 surface->expectTap(1, 1);
478
479 surface2->doTransaction([](auto &t, auto &sc) {
480 t.setPosition(sc, 100, 100);
481 });
482 surface->doTransaction([](auto &t, auto &sc) {
483 t.setPosition(sc, 200, 200);
484 });
485
486 injectTap(101, 101);
487 surface2->expectTap(1, 1);
488
489 injectTap(201, 201);
490 surface->expectTap(1, 1);
491}
492
493TEST_F(InputSurfacesTest, input_respects_layering) {
494 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
495 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
496
497 surface->showAt(10, 10);
498 surface2->showAt(10, 10);
499
500 surface->doTransaction([](auto &t, auto &sc) {
501 t.setLayer(sc, LAYER_BASE + 1);
502 });
503
504 injectTap(11, 11);
505 surface->expectTap(1, 1);
506
507 surface2->doTransaction([](auto &t, auto &sc) {
508 t.setLayer(sc, LAYER_BASE + 1);
509 });
510
511 injectTap(11, 11);
512 surface2->expectTap(1, 1);
513
514 surface2->doTransaction([](auto &t, auto &sc) {
515 t.hide(sc);
516 });
517
518 injectTap(11, 11);
519 surface->expectTap(1, 1);
520}
521
Vishnu Nairde19f852018-12-18 16:11:53 -0800522// Surface Insets are set to offset the client content and draw a border around the client surface
523// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
524// of the client content.
525TEST_F(InputSurfacesTest, input_respects_surface_insets) {
526 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
527 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
528 bgSurface->showAt(100, 100);
529
530 fgSurface->mInputInfo.surfaceInset = 5;
531 fgSurface->showAt(100, 100);
532
533 injectTap(106, 106);
534 fgSurface->expectTap(1, 1);
535
536 injectTap(101, 101);
537 bgSurface->expectTap(1, 1);
538}
539
Vishnu Nairfed7c122023-03-18 01:54:43 +0000540TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) {
541 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
542 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
543 bgSurface->showAt(100, 100);
544
545 fgSurface->mInputInfo.surfaceInset = 5;
546 fgSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
547 fgSurface->showAt(100, 100);
548
549 injectTap(106, 106);
550 fgSurface->expectTap(1, 1);
551
552 injectTap(101, 101);
553 bgSurface->expectTap(1, 1);
554}
555
Vishnu Nairde19f852018-12-18 16:11:53 -0800556// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
557TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
558 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
559 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
560 parentSurface->showAt(100, 100);
561
562 childSurface->mInputInfo.surfaceInset = 10;
563 childSurface->showAt(100, 100);
564
565 childSurface->doTransaction([&](auto &t, auto &sc) {
566 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000567 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800568 });
569
570 injectTap(106, 106);
571 childSurface->expectTap(1, 1);
572
573 injectTap(101, 101);
574 parentSurface->expectTap(1, 1);
575}
576
Arthur Hung118b1142019-05-08 21:25:59 +0800577// Ensure a surface whose insets are scaled, handles the touch offset correctly.
578TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
579 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
580 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
581 bgSurface->showAt(100, 100);
582
583 fgSurface->mInputInfo.surfaceInset = 5;
584 fgSurface->showAt(100, 100);
585
586 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
587
588 // expect = touch / scale - inset
589 injectTap(112, 124);
590 fgSurface->expectTap(1, 1);
591
592 injectTap(101, 101);
593 bgSurface->expectTap(1, 1);
594}
595
Ady Abraham282f1d72019-07-24 18:05:56 -0700596TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700597 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
Ady Abraham282f1d72019-07-24 18:05:56 -0700598 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700599 bgSurface->showAt(100, 100);
600
Ady Abraham282f1d72019-07-24 18:05:56 -0700601 // In case we pass the very big inset without any checking.
602 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
603 fgSurface->showAt(100, 100);
604
605 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
606
607 // expect no crash for overflow, and inset size to be clamped to surface size
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700608 injectTap(112, 124);
609 bgSurface->expectTap(12, 24);
Ady Abraham282f1d72019-07-24 18:05:56 -0700610}
611
Prabir Pradhan33da9462022-06-14 14:55:57 +0000612TEST_F(InputSurfacesTest, touchable_region) {
613 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
614
615 surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31});
616
617 surface->showAt(11, 22);
618
619 // A tap within the surface but outside the touchable region should not be sent to the surface.
620 injectTap(20, 30);
Egor Pasko5a67a562024-01-16 16:46:45 +0100621 EXPECT_EQ(surface->consumeEvent(/*timeout=*/200ms), nullptr);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000622
623 injectTap(31, 52);
624 surface->expectTap(20, 30);
625}
626
627TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
628 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
629 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
630 bgSurface->showAt(100, 100);
631
632 // Set the touchable region to the values at the limit of its corresponding type.
633 // Since the surface is offset from the origin, the touchable region will be transformed into
634 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
635 // against such a situation.
636 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
637
638 fgSurface->showAt(100, 100);
639
640 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
641 // surface receives touch.
642 injectTap(112, 124);
643 bgSurface->expectTap(12, 24);
644}
645
646TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
647 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
648 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
649 bgSurface->showAt(0, 0);
650
651 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
652 fgSurface->showAt(0, 0);
653
654 fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
655
656 // Expect no crash for overflow.
657 injectTap(12, 24);
Prabir Pradhana4c59bd2023-04-10 20:54:04 +0000658 bgSurface->expectTap(12, 24);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000659}
660
Vishnu Nairde19f852018-12-18 16:11:53 -0800661// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
662TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
663 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
664 surface->doTransaction([](auto &t, auto &sc) {
665 Region transparentRegion(Rect(0, 0, 10, 10));
666 t.setTransparentRegionHint(sc, transparentRegion);
667 });
668 surface->showAt(100, 100);
669 injectTap(101, 101);
670 surface->expectTap(1, 1);
671}
672
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700673// TODO(b/139494112) update tests once we define expected behavior
674// Ensure we still send input to the surface regardless of surface visibility changes due to the
675// first buffer being submitted or alpha changes.
676// Original bug ref: b/120839715
677TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800678 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500679 std::unique_ptr<BlastInputSurface> bufferSurface =
680 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800681
682 bgSurface->showAt(10, 10);
683 bufferSurface->showAt(10, 10);
684
685 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700686 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800687
chaviw39d01472021-04-08 14:26:24 -0500688 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800689 injectTap(11, 11);
690 bufferSurface->expectTap(1, 1);
691}
692
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000693TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800694 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500695 std::unique_ptr<BlastInputSurface> bufferSurface =
696 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
697 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800698
699 bgSurface->showAt(10, 10);
700 bufferSurface->showAt(10, 10);
701
702 injectTap(11, 11);
703 bufferSurface->expectTap(1, 1);
704
705 bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
706
707 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000708 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800709}
710
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700711TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800712 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
713 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
714
715 bgSurface->showAt(10, 10);
716 fgSurface->showAt(10, 10);
717
718 injectTap(11, 11);
719 fgSurface->expectTap(1, 1);
720
721 fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); });
722
723 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700724 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800725}
726
727TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
728 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
729 std::unique_ptr<InputSurface> containerSurface =
730 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
731
732 bgSurface->showAt(10, 10);
733 containerSurface->showAt(10, 10);
734
735 injectTap(11, 11);
736 containerSurface->expectTap(1, 1);
737
738 containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); });
739
740 injectTap(11, 11);
741 bgSurface->expectTap(1, 1);
742}
chaviwfbe5d9c2018-12-26 12:23:37 -0800743
Arthur Hungd20b2702019-01-14 18:16:16 +0800744TEST_F(InputSurfacesTest, input_respects_outscreen) {
745 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
746 surface->showAt(-1, -1);
747
748 injectTap(0, 0);
749 surface->expectTap(1, 1);
750}
arthurhungb4a0f852020-06-16 11:02:50 +0800751
752TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
753 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
754 std::unique_ptr<InputSurface> cursorSurface =
755 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
756
757 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800758 cursorSurface->showAt(10, 10);
759
760 injectTap(11, 11);
761 surface->expectTap(1, 1);
762}
Vishnu Nair958da932020-08-21 17:12:37 -0700763
764TEST_F(InputSurfacesTest, can_be_focused) {
765 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
766 surface->showAt(100, 100);
767 surface->requestFocus();
768
769 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700770
771 injectKey(AKEYCODE_V);
772 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700773}
chaviw44a6d2b2020-09-08 17:14:16 -0700774
775TEST_F(InputSurfacesTest, rotate_surface) {
776 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
777 surface->showAt(10, 10);
778 surface->doTransaction([](auto &t, auto &sc) {
779 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
780 });
781 injectTap(8, 11);
782 surface->expectTap(1, 2);
783
784 surface->doTransaction([](auto &t, auto &sc) {
785 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
786 });
787 injectTap(9, 8);
788 surface->expectTap(1, 2);
789
790 surface->doTransaction([](auto &t, auto &sc) {
791 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
792 });
793 injectTap(12, 9);
794 surface->expectTap(1, 2);
795}
796
797TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
798 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
799 surface->showAt(10, 10);
800 surface->doTransaction([](auto &t, auto &sc) {
801 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
802 });
803 injectTap(2, 12);
804 surface->expectTap(1, 2);
805
806 surface->doTransaction([](auto &t, auto &sc) {
807 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
808 });
809 injectTap(8, 2);
810 surface->expectTap(1, 2);
811
812 surface->doTransaction([](auto &t, auto &sc) {
813 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
814 });
815 injectTap(18, 8);
816 surface->expectTap(1, 2);
817}
818
819TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
820 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
821 surface->mInputInfo.surfaceInset = 5;
822 surface->showAt(100, 100);
823
824 surface->doTransaction([](auto &t, auto &sc) {
825 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
826 });
827 injectTap(40, 120);
828 surface->expectTap(5, 10);
829
830 surface->doTransaction([](auto &t, auto &sc) {
831 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
832 });
833 injectTap(80, 40);
834 surface->expectTap(5, 10);
835
836 surface->doTransaction([](auto &t, auto &sc) {
837 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
838 });
839 injectTap(160, 80);
840 surface->expectTap(5, 10);
841}
842
chaviw39cfa2e2020-11-04 14:19:02 -0800843TEST_F(InputSurfacesTest, touch_flag_obscured) {
844 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
845 surface->showAt(100, 100);
846
847 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
848 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
849 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800850 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000851 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
Bernardo Rufino602ef712020-12-21 11:02:18 +0000852 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
853 // the default obscured/untrusted touch filter introduced in S.
854 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800855 nonTouchableSurface->showAt(100, 100);
856
857 injectTap(190, 199);
858 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
859}
860
861TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
862 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
863 surface->showAt(100, 100);
864
865 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
866 // that will be tapped. Window behind gets touch, but with flag
867 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
868 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
869 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800870 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
871 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000872 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
873 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800874 nonTouchableSurface->showAt(0, 0);
875 parentSurface->showAt(100, 100);
876
877 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800878 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800879 t.reparent(sc, parentSurface->mSurfaceControl);
880 });
881
882 injectTap(190, 199);
883 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
884}
885
886TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
887 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
888 surface->showAt(100, 100);
889
890 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
891 // the touchable window. Window behind gets touch with no obscured flags.
892 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
893 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800894 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
895 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000896 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
897 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800898 nonTouchableSurface->showAt(0, 0);
899 parentSurface->showAt(50, 50);
900
901 nonTouchableSurface->doTransaction([&](auto &t, auto &sc) {
chaviw25714502021-02-11 10:01:08 -0800902 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800903 t.reparent(sc, parentSurface->mSurfaceControl);
904 });
905
906 injectTap(101, 110);
907 surface->expectTap(1, 10);
908}
909
chaviw7e72caf2020-12-02 16:50:43 -0800910TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
911 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
912
913 std::unique_ptr<InputSurface> bufferSurface =
914 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800915 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000916 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800917
918 surface->showAt(10, 10);
919 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
920
921 injectTap(11, 11);
922 surface->expectTap(1, 1);
923}
924
925TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
926 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
927
chaviw39d01472021-04-08 14:26:24 -0500928 std::unique_ptr<BlastInputSurface> bufferSurface =
929 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800930 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000931 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800932
933 surface->showAt(10, 10);
934 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
935
936 injectTap(11, 11);
937 surface->expectTap(1, 1);
938}
939
Vishnu Naira066d902021-09-13 18:40:17 -0700940TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
941 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
942 surface->doTransaction(
943 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
944 surface->showAt(100, 100);
945
946 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700947 surface->expectTap(1, 1);
Vishnu Naira066d902021-09-13 18:40:17 -0700948
949 surface->requestFocus();
950 surface->assertFocusChange(true);
951 injectKey(AKEYCODE_V);
952 surface->expectKey(AKEYCODE_V);
953}
954
955TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
956 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
957 surface->doTransaction([&](auto &t, auto &sc) {
958 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
959 t.setMatrix(sc, 2.0, 0, 0, 2.0);
960 });
961 surface->showAt(100, 100);
962
963 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700964 surface->expectTap(.5, .5);
Vishnu Naira066d902021-09-13 18:40:17 -0700965
966 surface->requestFocus();
967 surface->assertFocusChange(true);
968 injectKey(AKEYCODE_V);
969 surface->expectKey(AKEYCODE_V);
970}
971
972TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
973 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000974 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700975 surface->doTransaction(
976 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
977 surface->showAt(100, 100);
978 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800979 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000980 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700981 obscuringSurface->showAt(100, 100);
982 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700983 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700984
985 surface->requestFocus();
986 surface->assertFocusChange(true);
987 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700988 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700989}
990
991TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
992 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000993 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700994 surface->doTransaction(
995 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
996 surface->showAt(100, 100);
997 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800998 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000999 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -07001000 obscuringSurface->showAt(190, 190);
1001
1002 injectTap(101, 101);
1003
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001004 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001005
1006 surface->requestFocus();
1007 surface->assertFocusChange(true);
1008 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001009 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001010}
1011
1012TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
1013 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1014 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1015
1016 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1017 surface->showAt(100, 100);
1018 surface->doTransaction([&](auto &t, auto &sc) {
1019 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1020 t.reparent(sc, parentSurface->mSurfaceControl);
1021 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
1022 });
1023
1024 injectTap(101, 101);
1025
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001026 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001027
1028 surface->requestFocus();
1029 surface->assertFocusChange(true);
1030 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001031 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001032}
1033
1034TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
1035 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1036 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1037
1038 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1039 surface->doTransaction([&](auto &t, auto &sc) {
1040 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1041 t.reparent(sc, parentSurface->mSurfaceControl);
1042 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1043 });
1044 surface->showAt(100, 100);
1045
1046 injectTap(111, 111);
1047
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001048 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001049
1050 surface->requestFocus();
1051 surface->assertFocusChange(true);
1052 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001053 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001054}
1055
Arthur Hung49d525a2021-11-19 15:11:51 +00001056TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1057 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1058
1059 std::unique_ptr<BlastInputSurface> bufferSurface =
1060 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1061
1062 surface->showAt(100, 100);
1063 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
1064 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1065
1066 injectTap(101, 101);
1067 surface->expectTap(1, 1);
1068}
1069
Vishnu Naira066d902021-09-13 18:40:17 -07001070TEST_F(InputSurfacesTest, drop_input_policy) {
1071 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1072 surface->doTransaction(
1073 [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
1074 surface->showAt(100, 100);
1075
1076 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001077 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001078
1079 surface->requestFocus();
1080 surface->assertFocusChange(true);
1081 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001082 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001083}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001084
Prabir Pradhan8c285982022-01-28 09:19:39 -08001085TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1086 std::unique_ptr<InputSurface> bufferSurface =
1087 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1088
1089 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1090
1091 bufferSurface->requestFocus();
1092 bufferSurface->assertFocusChange(true);
1093}
1094
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001095/**
1096 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1097 * its own crop.
1098 */
1099TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1100 std::unique_ptr<InputSurface> parentContainer =
1101 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1102 std::unique_ptr<InputSurface> containerSurface =
1103 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1104 containerSurface->doTransaction(
1105 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1106 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1107 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1108 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1109 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1110
1111 // Receives events inside its own crop
1112 injectTap(21, 21);
1113 containerSurface->expectTap(1, 1); // Event is in layer space
1114
1115 // Does not receive events outside its crop
1116 injectTap(26, 26);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001117 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001118}
1119
1120/**
1121 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1122 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1123 */
1124TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
1125 std::unique_ptr<InputSurface> parentContainer =
1126 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1127 std::unique_ptr<InputSurface> containerSurface =
1128 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1129 containerSurface->doTransaction(
1130 [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
1131 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1132 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1133 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1134 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1135
1136 // Receives events inside parent bounds
1137 injectTap(21, 21);
1138 containerSurface->expectTap(1, 1); // Event is in layer space
1139
1140 // Does not receive events outside parent bounds
1141 injectTap(31, 31);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001142 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001143}
1144
1145/**
1146 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1147 * layer's bounds. The input events should be in the layer's coordinate space.
1148 */
1149TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1150 std::unique_ptr<InputSurface> cropLayer =
1151 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1152 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1153
1154 std::unique_ptr<InputSurface> containerSurface =
1155 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1156 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1157 containerSurface->mInputInfo.touchableRegionCropHandle =
1158 cropLayer->mSurfaceControl->getHandle();
1159 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1160
1161 // Receives events inside crop layer bounds
1162 injectTap(51, 51);
1163 containerSurface->expectTap(41, 41); // Event is in layer space
1164
1165 // Does not receive events outside crop layer bounds
1166 injectTap(21, 21);
1167 injectTap(71, 71);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001168 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001169}
1170
Linus Tufvessona1858822022-03-04 09:32:07 +00001171TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1172 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1173
1174 parent->showAt(100, 100);
1175 injectTap(101, 101);
1176 parent->expectTap(1, 1);
1177
1178 std::unique_ptr<InputSurface> childContainerSurface =
1179 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1180 childContainerSurface->showAt(0, 0);
1181 childContainerSurface->doTransaction(
1182 [&](auto &t, auto &sc) { t.reparent(sc, parent->mSurfaceControl); });
1183 injectTap(101, 101);
1184
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001185 parent->assertNoEvent();
Linus Tufvessona1858822022-03-04 09:32:07 +00001186}
1187
Vishnu Nair16a938f2021-09-24 07:14:54 -07001188class MultiDisplayTests : public InputSurfacesTest {
1189public:
1190 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Prabir Pradhand0aba782021-12-14 00:44:21 -08001191 void TearDown() override {
1192 for (auto &token : mVirtualDisplays) {
1193 SurfaceComposerClient::destroyDisplay(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001194 }
1195 InputSurfacesTest::TearDown();
1196 }
1197
Prabir Pradhand0aba782021-12-14 00:44:21 -08001198 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1199 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001200 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001201 sp<IGraphicBufferProducer> producer;
1202 BufferQueue::createBufferQueue(&producer, &consumer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001203 consumer->setConsumerName(String8("Virtual disp consumer"));
1204 consumer->setDefaultBufferSize(width, height);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001205 mProducers.push_back(producer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001206
Prabir Pradhand0aba782021-12-14 00:44:21 -08001207 std::string name = "VirtualDisplay";
1208 name += std::to_string(mVirtualDisplays.size());
1209 sp<IBinder> token = SurfaceComposerClient::createDisplay(String8(name.c_str()), isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001210 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001211 t.setDisplaySurface(token, producer);
1212 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1213 t.setDisplayLayerStack(token, layerStack);
1214 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1215 {offsetX, offsetY, offsetX + width, offsetY + height});
Vishnu Nair16a938f2021-09-24 07:14:54 -07001216 t.apply(true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001217
1218 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001219 }
1220
Prabir Pradhand0aba782021-12-14 00:44:21 -08001221 std::vector<sp<IBinder>> mVirtualDisplays;
1222 std::vector<sp<IGraphicBufferProducer>> mProducers;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001223};
1224
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001225TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001226 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1227 // Do not create a display associated with the LayerStack.
1228 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1229 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1230 surface->showAt(100, 100);
1231
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001232 // Touches should be dropped if the layer is on an invalid display.
Prabir Pradhand0aba782021-12-14 00:44:21 -08001233 injectTapOnDisplay(101, 101, layerStack.id);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001234 surface->assertNoEvent();
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001235
1236 // However, we still let the window be focused and receive keys.
1237 surface->requestFocus(layerStack.id);
1238 surface->assertFocusChange(true);
1239
1240 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1241 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001242}
1243
1244TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1245 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1246 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1247 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1248 surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); });
1249 surface->showAt(100, 100);
1250
1251 injectTapOnDisplay(101, 101, layerStack.id);
1252 surface->expectTap(1, 1);
1253
1254 surface->requestFocus(layerStack.id);
1255 surface->assertFocusChange(true);
1256 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1257 surface->expectKey(AKEYCODE_V);
1258}
1259
Vishnu Nair16a938f2021-09-24 07:14:54 -07001260TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1261 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1262 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1263 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1264 surface->doTransaction([&](auto &t, auto &sc) {
1265 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1266 t.setLayerStack(sc, layerStack);
1267 });
1268 surface->showAt(100, 100);
1269
Prabir Pradhand0aba782021-12-14 00:44:21 -08001270 injectTapOnDisplay(101, 101, layerStack.id);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001271
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001272 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001273
1274 surface->requestFocus(layerStack.id);
1275 surface->assertFocusChange(true);
1276 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001277 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001278}
1279
1280TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1281 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001282
1283 // Create the secure display as system, because only certain users can create secure displays.
1284 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001285 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001286 // Change the uid back to root.
1287 seteuid(AID_ROOT);
1288
Vishnu Nair16a938f2021-09-24 07:14:54 -07001289 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1290 surface->doTransaction([&](auto &t, auto &sc) {
1291 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1292 t.setLayerStack(sc, layerStack);
1293 });
1294 surface->showAt(100, 100);
1295
1296 injectTapOnDisplay(101, 101, layerStack.id);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001297 surface->expectTap(1, 1);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001298
1299 surface->requestFocus(layerStack.id);
1300 surface->assertFocusChange(true);
1301 injectKeyOnDisplay(AKEYCODE_V, layerStack.id);
1302
1303 surface->expectKey(AKEYCODE_V);
1304}
1305
Vishnu Nair958da932020-08-21 17:12:37 -07001306} // namespace android::test