blob: b0db3968728c6bba9dba90fbd5591df1e1ca9d70 [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;
Linnan Li13bf76a2024-05-05 19:18:02 +080062using android::ui::ADISPLAY_ID_DEFAULT;
63using android::ui::ADISPLAY_ID_NONE;
chaviw98318de2021-05-19 16:45:23 -050064
Linnan Li13bf76a2024-05-05 19:18:02 +080065namespace android {
66namespace {
67ui::LogicalDisplayId toDisplayId(ui::LayerStack layerStack) {
68 return ui::LogicalDisplayId{static_cast<int32_t>(layerStack.id)};
69}
70} // namespace
71namespace test {
Robert Carr1c4c5592018-09-24 13:18:43 -070072
Vishnu Nairde19f852018-12-18 16:11:53 -080073using Transaction = SurfaceComposerClient::Transaction;
74
Robert Carr1c4c5592018-09-24 13:18:43 -070075sp<IInputFlinger> getInputFlinger() {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -080076 sp<IBinder> input(defaultServiceManager()->waitForService(String16("inputflinger")));
Robert Carr1c4c5592018-09-24 13:18:43 -070077 if (input == nullptr) {
78 ALOGE("Failed to link to input service");
Linnan Li13bf76a2024-05-05 19:18:02 +080079 } else {
80 ALOGE("Linked to input");
81 }
Robert Carr1c4c5592018-09-24 13:18:43 -070082 return interface_cast<IInputFlinger>(input);
83}
84
85// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
86static const int LAYER_BASE = INT32_MAX - 10;
Chris Ye6c4243b2020-07-22 12:07:12 -070087static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070088
Patrick Williams0a4981a2023-07-28 15:08:52 -050089class SynchronousWindowInfosReportedListener : public gui::BnWindowInfosReportedListener {
90public:
91 binder::Status onWindowInfosReported() override {
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070092 std::scoped_lock lock{mLock};
Patrick Williams0a4981a2023-07-28 15:08:52 -050093 mWindowInfosReported = true;
94 mConditionVariable.notify_one();
95 return binder::Status::ok();
96 }
97
98 void wait() {
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070099 std::unique_lock lock{mLock};
100 android::base::ScopedLockAssertion assumeLocked(mLock);
101 mConditionVariable.wait(lock, [&]() REQUIRES(mLock) { return mWindowInfosReported; });
Patrick Williams0a4981a2023-07-28 15:08:52 -0500102 }
103
104private:
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -0700105 std::mutex mLock;
Patrick Williams0a4981a2023-07-28 15:08:52 -0500106 std::condition_variable mConditionVariable;
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -0700107 bool mWindowInfosReported GUARDED_BY(mLock){false};
Patrick Williams0a4981a2023-07-28 15:08:52 -0500108};
109
Robert Carr1c4c5592018-09-24 13:18:43 -0700110class InputSurface {
111public:
Linnan Li13bf76a2024-05-05 19:18:02 +0800112 InputSurface(const sp<SurfaceControl>& sc, int width, int height, bool noInputChannel = false) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800113 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -0700114
115 mInputFlinger = getInputFlinger();
Linus Tufvessona1858822022-03-04 09:32:07 +0000116 if (noInputChannel) {
117 mInputInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, true);
118 } else {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800119 android::os::InputChannelCore tempChannel;
120 android::binder::Status result =
121 mInputFlinger->createInputChannel("testchannels", &tempChannel);
122 if (!result.isOk()) {
123 ADD_FAILURE() << "binder call to createInputChannel failed";
124 }
125 mClientChannel = InputChannel::create(std::move(tempChannel));
Linus Tufvessona1858822022-03-04 09:32:07 +0000126 mInputInfo.token = mClientChannel->getConnectionToken();
127 mInputConsumer = new InputConsumer(mClientChannel);
128 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700129
Linus Tufvessona1858822022-03-04 09:32:07 +0000130 mInputInfo.name = "Test info";
131 mInputInfo.dispatchingTimeout = 5s;
132 mInputInfo.globalScaleFactor = 1.0;
133 mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height));
Robert Carr1c4c5592018-09-24 13:18:43 -0700134
Linus Tufvessona1858822022-03-04 09:32:07 +0000135 InputApplicationInfo aInfo;
136 aInfo.token = new BBinder();
137 aInfo.name = "Test app info";
138 aInfo.dispatchingTimeoutMillis =
139 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
140 mInputInfo.applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700141 }
142
Linnan Li13bf76a2024-05-05 19:18:02 +0800143 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient>& scc,
Vishnu Nairde19f852018-12-18 16:11:53 -0800144 int width, int height) {
145 sp<SurfaceControl> surfaceControl =
146 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800147 PIXEL_FORMAT_RGBA_8888,
148 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -0800149 return std::make_unique<InputSurface>(surfaceControl, width, height);
150 }
151
152 static std::unique_ptr<InputSurface> makeBufferInputSurface(
Linnan Li13bf76a2024-05-05 19:18:02 +0800153 const sp<SurfaceComposerClient>& scc, int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800154 sp<SurfaceControl> surfaceControl =
155 scc->createSurface(String8("Test Buffer Surface"), width, height,
156 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
157 return std::make_unique<InputSurface>(surfaceControl, width, height);
158 }
159
160 static std::unique_ptr<InputSurface> makeContainerInputSurface(
Linnan Li13bf76a2024-05-05 19:18:02 +0800161 const sp<SurfaceComposerClient>& scc, int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800162 sp<SurfaceControl> surfaceControl =
163 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
164 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
165 ISurfaceComposerClient::eFXSurfaceContainer);
166 return std::make_unique<InputSurface>(surfaceControl, width, height);
167 }
168
Linus Tufvessona1858822022-03-04 09:32:07 +0000169 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
Linnan Li13bf76a2024-05-05 19:18:02 +0800170 const sp<SurfaceComposerClient>& scc, int width, int height) {
Linus Tufvessona1858822022-03-04 09:32:07 +0000171 sp<SurfaceControl> surfaceControl =
172 scc->createSurface(String8("Test Container Surface"), 100 /* height */,
173 100 /* width */, PIXEL_FORMAT_RGBA_8888,
174 ISurfaceComposerClient::eFXSurfaceContainer);
175 return std::make_unique<InputSurface>(surfaceControl, width, height,
176 true /* noInputChannel */);
177 }
178
arthurhungb4a0f852020-06-16 11:02:50 +0800179 static std::unique_ptr<InputSurface> makeCursorInputSurface(
Linnan Li13bf76a2024-05-05 19:18:02 +0800180 const sp<SurfaceComposerClient>& scc, int width, int height) {
arthurhungb4a0f852020-06-16 11:02:50 +0800181 sp<SurfaceControl> surfaceControl =
182 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
183 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
184 ISurfaceComposerClient::eCursorWindow);
185 return std::make_unique<InputSurface>(surfaceControl, width, height);
186 }
187
Egor Pasko5a67a562024-01-16 16:46:45 +0100188 InputEvent* consumeEvent(std::chrono::milliseconds timeout = 3000ms) {
189 mClientChannel->waitForMessage(timeout);
Robert Carr1c4c5592018-09-24 13:18:43 -0700190
Linnan Li13bf76a2024-05-05 19:18:02 +0800191 InputEvent* ev;
Robert Carr1c4c5592018-09-24 13:18:43 -0700192 uint32_t seqId;
193 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
194 if (consumed != OK) {
195 return nullptr;
196 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100197 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
198 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700199 return ev;
200 }
201
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100202 void assertFocusChange(bool hasFocus) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800203 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100204 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700205 ASSERT_EQ(InputEventType::FOCUS, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800206 FocusEvent* focusEvent = static_cast<FocusEvent*>(ev);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100207 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
208 }
209
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700210 void expectTap(float x, float y) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700211 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100212 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700213 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700214 MotionEvent* mev = static_cast<MotionEvent*>(ev);
215 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
216 EXPECT_EQ(x, mev->getX(0));
217 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800218 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700219
220 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100221 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700222 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700223 mev = static_cast<MotionEvent*>(ev);
224 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800225 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700226 }
227
chaviw39cfa2e2020-11-04 14:19:02 -0800228 void expectTapWithFlag(int x, int y, int32_t flags) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800229 InputEvent* ev = consumeEvent();
chaviw39cfa2e2020-11-04 14:19:02 -0800230 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700231 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800232 MotionEvent* mev = static_cast<MotionEvent*>(ev);
chaviw39cfa2e2020-11-04 14:19:02 -0800233 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
234 EXPECT_EQ(x, mev->getX(0));
235 EXPECT_EQ(y, mev->getY(0));
236 EXPECT_EQ(flags, mev->getFlags() & flags);
237
238 ev = consumeEvent();
239 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700240 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800241 mev = static_cast<MotionEvent*>(ev);
chaviw39cfa2e2020-11-04 14:19:02 -0800242 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
243 EXPECT_EQ(flags, mev->getFlags() & flags);
244 }
245
Prabir Pradhand0aba782021-12-14 00:44:21 -0800246 void expectTapInDisplayCoordinates(int displayX, int displayY) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800247 InputEvent* ev = consumeEvent();
Prabir Pradhand0aba782021-12-14 00:44:21 -0800248 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700249 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800250 MotionEvent* mev = static_cast<MotionEvent*>(ev);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800251 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
Linnan Li13bf76a2024-05-05 19:18:02 +0800252 const PointerCoords& coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800253 EXPECT_EQ(displayX, coords.getX());
254 EXPECT_EQ(displayY, coords.getY());
255 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
256
257 ev = consumeEvent();
258 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700259 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800260 mev = static_cast<MotionEvent*>(ev);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800261 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
262 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
263 }
264
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -0700265 void expectKey(int32_t keycode) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800266 InputEvent* ev = consumeEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700267 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700268 ASSERT_EQ(InputEventType::KEY, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800269 KeyEvent* keyEvent = static_cast<KeyEvent*>(ev);
Vishnu Naira066d902021-09-13 18:40:17 -0700270 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
271 EXPECT_EQ(keycode, keyEvent->getKeyCode());
272 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
273
274 ev = consumeEvent();
275 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700276 ASSERT_EQ(InputEventType::KEY, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800277 keyEvent = static_cast<KeyEvent*>(ev);
Vishnu Naira066d902021-09-13 18:40:17 -0700278 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
279 EXPECT_EQ(keycode, keyEvent->getKeyCode());
280 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
281 }
282
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700283 void assertNoEvent() {
284 InputEvent* event = consumeEvent(/*timeout=*/100ms);
285 ASSERT_EQ(event, nullptr) << "Expected no event, but got " << *event;
286 }
287
chaviw39d01472021-04-08 14:26:24 -0500288 virtual ~InputSurface() {
Linus Tufvessona1858822022-03-04 09:32:07 +0000289 if (mClientChannel) {
290 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
291 }
chaviw39d01472021-04-08 14:26:24 -0500292 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700293
chaviw39d01472021-04-08 14:26:24 -0500294 virtual void doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800295 std::function<void(SurfaceComposerClient::Transaction&, const sp<SurfaceControl>&)>
chaviw39d01472021-04-08 14:26:24 -0500296 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700297 SurfaceComposerClient::Transaction t;
298 transactionBody(t, mSurfaceControl);
299 t.apply(true);
300 }
301
chaviw39d01472021-04-08 14:26:24 -0500302 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700303 SurfaceComposerClient::Transaction t;
304 t.show(mSurfaceControl);
305 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
306 t.setLayer(mSurfaceControl, LAYER_BASE);
307 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800308 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700309 t.setAlpha(mSurfaceControl, 1);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500310 auto reportedListener = sp<SynchronousWindowInfosReportedListener>::make();
311 t.addWindowInfosReportedListener(reportedListener);
312 t.apply();
313 reportedListener->wait();
Robert Carr1c4c5592018-09-24 13:18:43 -0700314 }
315
Linnan Li13bf76a2024-05-05 19:18:02 +0800316 void requestFocus(ui::LogicalDisplayId displayId = ADISPLAY_ID_DEFAULT) {
Vishnu Nair958da932020-08-21 17:12:37 -0700317 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800318 FocusRequest request;
319 request.token = mInputInfo.token;
320 request.windowName = mInputInfo.name;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800321 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Linnan Li13bf76a2024-05-05 19:18:02 +0800322 request.displayId = displayId.val();
Vishnu Nair9ad01462021-01-15 12:55:14 -0800323 t.setFocusedWindow(request);
Vishnu Nair958da932020-08-21 17:12:37 -0700324 t.apply(true);
325 }
326
Robert Carr1c4c5592018-09-24 13:18:43 -0700327public:
328 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500329 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700330 sp<IInputFlinger> mInputFlinger;
331
chaviw98318de2021-05-19 16:45:23 -0500332 WindowInfo mInputInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700333
334 PreallocatedInputEventFactory mInputEventFactory;
335 InputConsumer* mInputConsumer;
336};
337
chaviw39d01472021-04-08 14:26:24 -0500338class BlastInputSurface : public InputSurface {
339public:
Linnan Li13bf76a2024-05-05 19:18:02 +0800340 BlastInputSurface(const sp<SurfaceControl>& sc, const sp<SurfaceControl>& parentSc, int width,
chaviw39d01472021-04-08 14:26:24 -0500341 int height)
342 : InputSurface(sc, width, height) {
343 mParentSurfaceControl = parentSc;
344 }
345
346 ~BlastInputSurface() = default;
347
348 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
Linnan Li13bf76a2024-05-05 19:18:02 +0800349 const sp<SurfaceComposerClient>& scc, int width, int height) {
chaviw39d01472021-04-08 14:26:24 -0500350 sp<SurfaceControl> parentSc =
351 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
352 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
353 ISurfaceComposerClient::eFXSurfaceContainer);
354
355 sp<SurfaceControl> surfaceControl =
356 scc->createSurface(String8("Test Buffer Surface"), width, height,
357 PIXEL_FORMAT_RGBA_8888,
358 ISurfaceComposerClient::eFXSurfaceBufferState,
359 parentSc->getHandle());
360 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
361 }
362
363 void doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800364 std::function<void(SurfaceComposerClient::Transaction&, const sp<SurfaceControl>&)>
chaviw39d01472021-04-08 14:26:24 -0500365 transactionBody) override {
366 SurfaceComposerClient::Transaction t;
367 transactionBody(t, mParentSurfaceControl);
368 t.apply(true);
369 }
370
371 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
372 SurfaceComposerClient::Transaction t;
373 t.show(mParentSurfaceControl);
374 t.setLayer(mParentSurfaceControl, LAYER_BASE);
375 t.setPosition(mParentSurfaceControl, x, y);
376 t.setCrop(mParentSurfaceControl, crop);
377
378 t.show(mSurfaceControl);
379 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
380 t.setCrop(mSurfaceControl, crop);
381 t.setAlpha(mSurfaceControl, 1);
382 t.apply(true);
383 }
384
385private:
386 sp<SurfaceControl> mParentSurfaceControl;
387};
388
Robert Carr1c4c5592018-09-24 13:18:43 -0700389class InputSurfacesTest : public ::testing::Test {
390public:
Linnan Li13bf76a2024-05-05 19:18:02 +0800391 InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Robert Carr1c4c5592018-09-24 13:18:43 -0700392
393 void SetUp() {
394 mComposerClient = new SurfaceComposerClient;
395 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Huihong Luo31b5ac22022-08-15 20:38:10 -0700396 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
397 ASSERT_FALSE(ids.empty());
398 // display 0 is picked for now, can extend to support all displays if needed
399 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100400 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800401
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100402 ui::DisplayMode mode;
403 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800404
405 // After a new buffer is queued, SurfaceFlinger is notified and will
406 // latch the new buffer on next vsync. Let's heuristically wait for 3
407 // vsyncs.
Alec Mouri55e31032023-10-02 20:34:18 +0000408 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.peakRefreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700409 }
410
Linnan Li13bf76a2024-05-05 19:18:02 +0800411 void TearDown() { mComposerClient->dispose(); }
Robert Carr1c4c5592018-09-24 13:18:43 -0700412
413 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800414 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
415 }
416
Linnan Li13bf76a2024-05-05 19:18:02 +0800417 void postBuffer(const sp<SurfaceControl>& layer, int32_t w, int32_t h) {
chaviw39d01472021-04-08 14:26:24 -0500418 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
419 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
420 sp<GraphicBuffer> buffer =
421 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
422 Transaction().setBuffer(layer, buffer).apply(true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800423 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700424 }
425
426 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800427 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700428};
429
Linnan Li13bf76a2024-05-05 19:18:02 +0800430void injectTapOnDisplay(int x, int y, ui::LogicalDisplayId displayId) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700431 char *buf1, *buf2, *bufDisplayId;
Robert Carr1c4c5592018-09-24 13:18:43 -0700432 asprintf(&buf1, "%d", x);
433 asprintf(&buf2, "%d", y);
Linnan Li13bf76a2024-05-05 19:18:02 +0800434 asprintf(&bufDisplayId, "%d", displayId.val());
Robert Carr1c4c5592018-09-24 13:18:43 -0700435 if (fork() == 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700436 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
437 }
438}
439
440void injectTap(int x, int y) {
441 injectTapOnDisplay(x, y, ADISPLAY_ID_DEFAULT);
442}
443
Linnan Li13bf76a2024-05-05 19:18:02 +0800444void injectKeyOnDisplay(uint32_t keycode, ui::LogicalDisplayId displayId) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700445 char *buf1, *bufDisplayId;
446 asprintf(&buf1, "%d", keycode);
Linnan Li13bf76a2024-05-05 19:18:02 +0800447 asprintf(&bufDisplayId, "%d", displayId.val());
Vishnu Nair16a938f2021-09-24 07:14:54 -0700448 if (fork() == 0) {
449 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
Robert Carr1c4c5592018-09-24 13:18:43 -0700450 }
451}
452
Vishnu Naira066d902021-09-13 18:40:17 -0700453void injectKey(uint32_t keycode) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700454 injectKeyOnDisplay(keycode, ADISPLAY_ID_NONE);
Vishnu Naira066d902021-09-13 18:40:17 -0700455}
456
Robert Carr1c4c5592018-09-24 13:18:43 -0700457TEST_F(InputSurfacesTest, can_receive_input) {
458 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
459 surface->showAt(100, 100);
460
461 injectTap(101, 101);
462
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100463 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700464}
465
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100466/**
467 * Set up two surfaces side-by-side. Tap each surface.
468 * Next, swap the positions of the two surfaces. Inject tap into the two
469 * original locations. Ensure that the tap is received by the surfaces in the
470 * reverse order.
471 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700472TEST_F(InputSurfacesTest, input_respects_positioning) {
473 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
474 surface->showAt(100, 100);
475
476 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
477 surface2->showAt(200, 200);
478
479 injectTap(201, 201);
480 surface2->expectTap(1, 1);
481
482 injectTap(101, 101);
483 surface->expectTap(1, 1);
484
Linnan Li13bf76a2024-05-05 19:18:02 +0800485 surface2->doTransaction([](auto& t, auto& sc) { t.setPosition(sc, 100, 100); });
486 surface->doTransaction([](auto& t, auto& sc) { t.setPosition(sc, 200, 200); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700487
488 injectTap(101, 101);
489 surface2->expectTap(1, 1);
490
491 injectTap(201, 201);
492 surface->expectTap(1, 1);
493}
494
495TEST_F(InputSurfacesTest, input_respects_layering) {
496 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
497 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
498
499 surface->showAt(10, 10);
500 surface2->showAt(10, 10);
501
Linnan Li13bf76a2024-05-05 19:18:02 +0800502 surface->doTransaction([](auto& t, auto& sc) { t.setLayer(sc, LAYER_BASE + 1); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700503
504 injectTap(11, 11);
505 surface->expectTap(1, 1);
506
Linnan Li13bf76a2024-05-05 19:18:02 +0800507 surface2->doTransaction([](auto& t, auto& sc) { t.setLayer(sc, LAYER_BASE + 1); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700508
509 injectTap(11, 11);
510 surface2->expectTap(1, 1);
511
Linnan Li13bf76a2024-05-05 19:18:02 +0800512 surface2->doTransaction([](auto& t, auto& sc) { t.hide(sc); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700513
514 injectTap(11, 11);
515 surface->expectTap(1, 1);
516}
517
Vishnu Nairde19f852018-12-18 16:11:53 -0800518// Surface Insets are set to offset the client content and draw a border around the client surface
519// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
520// of the client content.
521TEST_F(InputSurfacesTest, input_respects_surface_insets) {
522 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
523 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
524 bgSurface->showAt(100, 100);
525
526 fgSurface->mInputInfo.surfaceInset = 5;
527 fgSurface->showAt(100, 100);
528
529 injectTap(106, 106);
530 fgSurface->expectTap(1, 1);
531
532 injectTap(101, 101);
533 bgSurface->expectTap(1, 1);
534}
535
Vishnu Nairfed7c122023-03-18 01:54:43 +0000536TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) {
537 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
538 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
539 bgSurface->showAt(100, 100);
540
541 fgSurface->mInputInfo.surfaceInset = 5;
542 fgSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
543 fgSurface->showAt(100, 100);
544
545 injectTap(106, 106);
546 fgSurface->expectTap(1, 1);
547
548 injectTap(101, 101);
549 bgSurface->expectTap(1, 1);
550}
551
Vishnu Nairde19f852018-12-18 16:11:53 -0800552// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
553TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
554 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
555 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
556 parentSurface->showAt(100, 100);
557
558 childSurface->mInputInfo.surfaceInset = 10;
559 childSurface->showAt(100, 100);
560
Linnan Li13bf76a2024-05-05 19:18:02 +0800561 childSurface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800562 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000563 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800564 });
565
566 injectTap(106, 106);
567 childSurface->expectTap(1, 1);
568
569 injectTap(101, 101);
570 parentSurface->expectTap(1, 1);
571}
572
Arthur Hung118b1142019-05-08 21:25:59 +0800573// Ensure a surface whose insets are scaled, handles the touch offset correctly.
574TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
575 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
576 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
577 bgSurface->showAt(100, 100);
578
579 fgSurface->mInputInfo.surfaceInset = 5;
580 fgSurface->showAt(100, 100);
581
Linnan Li13bf76a2024-05-05 19:18:02 +0800582 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
Arthur Hung118b1142019-05-08 21:25:59 +0800583
584 // expect = touch / scale - inset
585 injectTap(112, 124);
586 fgSurface->expectTap(1, 1);
587
588 injectTap(101, 101);
589 bgSurface->expectTap(1, 1);
590}
591
Ady Abraham282f1d72019-07-24 18:05:56 -0700592TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700593 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
Ady Abraham282f1d72019-07-24 18:05:56 -0700594 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700595 bgSurface->showAt(100, 100);
596
Ady Abraham282f1d72019-07-24 18:05:56 -0700597 // In case we pass the very big inset without any checking.
598 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
599 fgSurface->showAt(100, 100);
600
Linnan Li13bf76a2024-05-05 19:18:02 +0800601 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
Ady Abraham282f1d72019-07-24 18:05:56 -0700602
603 // expect no crash for overflow, and inset size to be clamped to surface size
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700604 injectTap(112, 124);
605 bgSurface->expectTap(12, 24);
Ady Abraham282f1d72019-07-24 18:05:56 -0700606}
607
Prabir Pradhan33da9462022-06-14 14:55:57 +0000608TEST_F(InputSurfacesTest, touchable_region) {
609 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
610
611 surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31});
612
613 surface->showAt(11, 22);
614
615 // A tap within the surface but outside the touchable region should not be sent to the surface.
616 injectTap(20, 30);
Egor Pasko5a67a562024-01-16 16:46:45 +0100617 EXPECT_EQ(surface->consumeEvent(/*timeout=*/200ms), nullptr);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000618
619 injectTap(31, 52);
620 surface->expectTap(20, 30);
621}
622
623TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
624 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
625 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
626 bgSurface->showAt(100, 100);
627
628 // Set the touchable region to the values at the limit of its corresponding type.
629 // Since the surface is offset from the origin, the touchable region will be transformed into
630 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
631 // against such a situation.
632 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
633
634 fgSurface->showAt(100, 100);
635
636 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
637 // surface receives touch.
638 injectTap(112, 124);
639 bgSurface->expectTap(12, 24);
640}
641
642TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
643 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
644 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
645 bgSurface->showAt(0, 0);
646
647 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
648 fgSurface->showAt(0, 0);
649
Linnan Li13bf76a2024-05-05 19:18:02 +0800650 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
Prabir Pradhan33da9462022-06-14 14:55:57 +0000651
652 // Expect no crash for overflow.
653 injectTap(12, 24);
Prabir Pradhana4c59bd2023-04-10 20:54:04 +0000654 bgSurface->expectTap(12, 24);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000655}
656
Vishnu Nairde19f852018-12-18 16:11:53 -0800657// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
658TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
659 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +0800660 surface->doTransaction([](auto& t, auto& sc) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800661 Region transparentRegion(Rect(0, 0, 10, 10));
662 t.setTransparentRegionHint(sc, transparentRegion);
663 });
664 surface->showAt(100, 100);
665 injectTap(101, 101);
666 surface->expectTap(1, 1);
667}
668
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700669// TODO(b/139494112) update tests once we define expected behavior
670// Ensure we still send input to the surface regardless of surface visibility changes due to the
671// first buffer being submitted or alpha changes.
672// Original bug ref: b/120839715
673TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800674 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500675 std::unique_ptr<BlastInputSurface> bufferSurface =
676 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800677
678 bgSurface->showAt(10, 10);
679 bufferSurface->showAt(10, 10);
680
681 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700682 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800683
chaviw39d01472021-04-08 14:26:24 -0500684 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800685 injectTap(11, 11);
686 bufferSurface->expectTap(1, 1);
687}
688
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000689TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800690 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500691 std::unique_ptr<BlastInputSurface> bufferSurface =
692 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
693 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800694
695 bgSurface->showAt(10, 10);
696 bufferSurface->showAt(10, 10);
697
698 injectTap(11, 11);
699 bufferSurface->expectTap(1, 1);
700
Linnan Li13bf76a2024-05-05 19:18:02 +0800701 bufferSurface->doTransaction([](auto& t, auto& sc) { t.setAlpha(sc, 0.0); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800702
703 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000704 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800705}
706
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700707TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800708 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
709 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
710
711 bgSurface->showAt(10, 10);
712 fgSurface->showAt(10, 10);
713
714 injectTap(11, 11);
715 fgSurface->expectTap(1, 1);
716
Linnan Li13bf76a2024-05-05 19:18:02 +0800717 fgSurface->doTransaction([](auto& t, auto& sc) { t.setAlpha(sc, 0.0); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800718
719 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700720 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800721}
722
723TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
724 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
725 std::unique_ptr<InputSurface> containerSurface =
726 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
727
728 bgSurface->showAt(10, 10);
729 containerSurface->showAt(10, 10);
730
731 injectTap(11, 11);
732 containerSurface->expectTap(1, 1);
733
Linnan Li13bf76a2024-05-05 19:18:02 +0800734 containerSurface->doTransaction([](auto& t, auto& sc) { t.hide(sc); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800735
736 injectTap(11, 11);
737 bgSurface->expectTap(1, 1);
738}
chaviwfbe5d9c2018-12-26 12:23:37 -0800739
Arthur Hungd20b2702019-01-14 18:16:16 +0800740TEST_F(InputSurfacesTest, input_respects_outscreen) {
741 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
742 surface->showAt(-1, -1);
743
744 injectTap(0, 0);
745 surface->expectTap(1, 1);
746}
arthurhungb4a0f852020-06-16 11:02:50 +0800747
748TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
749 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
750 std::unique_ptr<InputSurface> cursorSurface =
751 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
752
753 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800754 cursorSurface->showAt(10, 10);
755
756 injectTap(11, 11);
757 surface->expectTap(1, 1);
758}
Vishnu Nair958da932020-08-21 17:12:37 -0700759
760TEST_F(InputSurfacesTest, can_be_focused) {
761 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
762 surface->showAt(100, 100);
763 surface->requestFocus();
764
765 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700766
767 injectKey(AKEYCODE_V);
768 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700769}
chaviw44a6d2b2020-09-08 17:14:16 -0700770
771TEST_F(InputSurfacesTest, rotate_surface) {
772 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
773 surface->showAt(10, 10);
Linnan Li13bf76a2024-05-05 19:18:02 +0800774 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700775 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
776 });
777 injectTap(8, 11);
778 surface->expectTap(1, 2);
779
Linnan Li13bf76a2024-05-05 19:18:02 +0800780 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700781 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
782 });
783 injectTap(9, 8);
784 surface->expectTap(1, 2);
785
Linnan Li13bf76a2024-05-05 19:18:02 +0800786 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700787 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
788 });
789 injectTap(12, 9);
790 surface->expectTap(1, 2);
791}
792
793TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
794 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
795 surface->showAt(10, 10);
Linnan Li13bf76a2024-05-05 19:18:02 +0800796 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700797 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
798 });
799 injectTap(2, 12);
800 surface->expectTap(1, 2);
801
Linnan Li13bf76a2024-05-05 19:18:02 +0800802 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700803 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
804 });
805 injectTap(8, 2);
806 surface->expectTap(1, 2);
807
Linnan Li13bf76a2024-05-05 19:18:02 +0800808 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700809 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
810 });
811 injectTap(18, 8);
812 surface->expectTap(1, 2);
813}
814
815TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
816 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
817 surface->mInputInfo.surfaceInset = 5;
818 surface->showAt(100, 100);
819
Linnan Li13bf76a2024-05-05 19:18:02 +0800820 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700821 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
822 });
823 injectTap(40, 120);
824 surface->expectTap(5, 10);
825
Linnan Li13bf76a2024-05-05 19:18:02 +0800826 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700827 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
828 });
829 injectTap(80, 40);
830 surface->expectTap(5, 10);
831
Linnan Li13bf76a2024-05-05 19:18:02 +0800832 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700833 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
834 });
835 injectTap(160, 80);
836 surface->expectTap(5, 10);
837}
838
chaviw39cfa2e2020-11-04 14:19:02 -0800839TEST_F(InputSurfacesTest, touch_flag_obscured) {
840 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
841 surface->showAt(100, 100);
842
843 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
844 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
845 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800846 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000847 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
Bernardo Rufino602ef712020-12-21 11:02:18 +0000848 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
849 // the default obscured/untrusted touch filter introduced in S.
850 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800851 nonTouchableSurface->showAt(100, 100);
852
853 injectTap(190, 199);
854 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
855}
856
857TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
858 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
859 surface->showAt(100, 100);
860
861 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
862 // that will be tapped. Window behind gets touch, but with flag
863 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
864 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
865 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800866 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
867 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000868 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
869 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800870 nonTouchableSurface->showAt(0, 0);
871 parentSurface->showAt(100, 100);
872
Linnan Li13bf76a2024-05-05 19:18:02 +0800873 nonTouchableSurface->doTransaction([&](auto& t, auto& sc) {
chaviw25714502021-02-11 10:01:08 -0800874 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800875 t.reparent(sc, parentSurface->mSurfaceControl);
876 });
877
878 injectTap(190, 199);
879 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
880}
881
882TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
883 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
884 surface->showAt(100, 100);
885
886 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
887 // the touchable window. Window behind gets touch with no obscured flags.
888 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
889 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800890 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
891 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000892 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
893 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800894 nonTouchableSurface->showAt(0, 0);
895 parentSurface->showAt(50, 50);
896
Linnan Li13bf76a2024-05-05 19:18:02 +0800897 nonTouchableSurface->doTransaction([&](auto& t, auto& sc) {
chaviw25714502021-02-11 10:01:08 -0800898 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800899 t.reparent(sc, parentSurface->mSurfaceControl);
900 });
901
902 injectTap(101, 110);
903 surface->expectTap(1, 10);
904}
905
chaviw7e72caf2020-12-02 16:50:43 -0800906TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
907 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
908
909 std::unique_ptr<InputSurface> bufferSurface =
910 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800911 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000912 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800913
914 surface->showAt(10, 10);
915 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
916
917 injectTap(11, 11);
918 surface->expectTap(1, 1);
919}
920
921TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
922 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
923
chaviw39d01472021-04-08 14:26:24 -0500924 std::unique_ptr<BlastInputSurface> bufferSurface =
925 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800926 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000927 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800928
929 surface->showAt(10, 10);
930 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
931
932 injectTap(11, 11);
933 surface->expectTap(1, 1);
934}
935
Vishnu Naira066d902021-09-13 18:40:17 -0700936TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
937 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
938 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800939 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -0700940 surface->showAt(100, 100);
941
942 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700943 surface->expectTap(1, 1);
Vishnu Naira066d902021-09-13 18:40:17 -0700944
945 surface->requestFocus();
946 surface->assertFocusChange(true);
947 injectKey(AKEYCODE_V);
948 surface->expectKey(AKEYCODE_V);
949}
950
951TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
952 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +0800953 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -0700954 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
955 t.setMatrix(sc, 2.0, 0, 0, 2.0);
956 });
957 surface->showAt(100, 100);
958
959 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700960 surface->expectTap(.5, .5);
Vishnu Naira066d902021-09-13 18:40:17 -0700961
962 surface->requestFocus();
963 surface->assertFocusChange(true);
964 injectKey(AKEYCODE_V);
965 surface->expectKey(AKEYCODE_V);
966}
967
968TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
969 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000970 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700971 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800972 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -0700973 surface->showAt(100, 100);
974 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800975 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000976 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700977 obscuringSurface->showAt(100, 100);
978 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700979 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700980
981 surface->requestFocus();
982 surface->assertFocusChange(true);
983 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700984 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700985}
986
987TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
988 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000989 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700990 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800991 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -0700992 surface->showAt(100, 100);
993 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800994 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000995 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700996 obscuringSurface->showAt(190, 190);
997
998 injectTap(101, 101);
999
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001000 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001001
1002 surface->requestFocus();
1003 surface->assertFocusChange(true);
1004 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001005 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001006}
1007
1008TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
1009 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1010 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1011
1012 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1013 surface->showAt(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001014 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -07001015 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1016 t.reparent(sc, parentSurface->mSurfaceControl);
1017 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
1018 });
1019
1020 injectTap(101, 101);
1021
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001022 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001023
1024 surface->requestFocus();
1025 surface->assertFocusChange(true);
1026 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001027 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001028}
1029
1030TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
1031 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1032 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1033
1034 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001035 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -07001036 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1037 t.reparent(sc, parentSurface->mSurfaceControl);
1038 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1039 });
1040 surface->showAt(100, 100);
1041
1042 injectTap(111, 111);
1043
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001044 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001045
1046 surface->requestFocus();
1047 surface->assertFocusChange(true);
1048 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001049 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001050}
1051
Arthur Hung49d525a2021-11-19 15:11:51 +00001052TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1053 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1054
1055 std::unique_ptr<BlastInputSurface> bufferSurface =
1056 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1057
1058 surface->showAt(100, 100);
1059 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
1060 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1061
1062 injectTap(101, 101);
1063 surface->expectTap(1, 1);
1064}
1065
Vishnu Naira066d902021-09-13 18:40:17 -07001066TEST_F(InputSurfacesTest, drop_input_policy) {
1067 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1068 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001069 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
Vishnu Naira066d902021-09-13 18:40:17 -07001070 surface->showAt(100, 100);
1071
1072 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001073 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001074
1075 surface->requestFocus();
1076 surface->assertFocusChange(true);
1077 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001078 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001079}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001080
Prabir Pradhan8c285982022-01-28 09:19:39 -08001081TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1082 std::unique_ptr<InputSurface> bufferSurface =
1083 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1084
1085 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1086
1087 bufferSurface->requestFocus();
1088 bufferSurface->assertFocusChange(true);
1089}
1090
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001091/**
1092 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1093 * its own crop.
1094 */
1095TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1096 std::unique_ptr<InputSurface> parentContainer =
1097 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1098 std::unique_ptr<InputSurface> containerSurface =
1099 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1100 containerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001101 [&](auto& t, auto& sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001102 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1103 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1104 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1105 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1106
1107 // Receives events inside its own crop
1108 injectTap(21, 21);
1109 containerSurface->expectTap(1, 1); // Event is in layer space
1110
1111 // Does not receive events outside its crop
1112 injectTap(26, 26);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001113 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001114}
1115
1116/**
1117 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1118 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1119 */
1120TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
1121 std::unique_ptr<InputSurface> parentContainer =
1122 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1123 std::unique_ptr<InputSurface> containerSurface =
1124 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1125 containerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001126 [&](auto& t, auto& sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001127 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1128 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1129 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1130 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1131
1132 // Receives events inside parent bounds
1133 injectTap(21, 21);
1134 containerSurface->expectTap(1, 1); // Event is in layer space
1135
1136 // Does not receive events outside parent bounds
1137 injectTap(31, 31);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001138 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001139}
1140
1141/**
1142 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1143 * layer's bounds. The input events should be in the layer's coordinate space.
1144 */
1145TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1146 std::unique_ptr<InputSurface> cropLayer =
1147 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1148 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1149
1150 std::unique_ptr<InputSurface> containerSurface =
1151 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1152 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1153 containerSurface->mInputInfo.touchableRegionCropHandle =
1154 cropLayer->mSurfaceControl->getHandle();
1155 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1156
1157 // Receives events inside crop layer bounds
1158 injectTap(51, 51);
1159 containerSurface->expectTap(41, 41); // Event is in layer space
1160
1161 // Does not receive events outside crop layer bounds
1162 injectTap(21, 21);
1163 injectTap(71, 71);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001164 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001165}
1166
Linus Tufvessona1858822022-03-04 09:32:07 +00001167TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1168 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1169
1170 parent->showAt(100, 100);
1171 injectTap(101, 101);
1172 parent->expectTap(1, 1);
1173
1174 std::unique_ptr<InputSurface> childContainerSurface =
1175 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1176 childContainerSurface->showAt(0, 0);
1177 childContainerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001178 [&](auto& t, auto& sc) { t.reparent(sc, parent->mSurfaceControl); });
Linus Tufvessona1858822022-03-04 09:32:07 +00001179 injectTap(101, 101);
1180
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001181 parent->assertNoEvent();
Linus Tufvessona1858822022-03-04 09:32:07 +00001182}
1183
Vishnu Nair16a938f2021-09-24 07:14:54 -07001184class MultiDisplayTests : public InputSurfacesTest {
1185public:
1186 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Linnan Li13bf76a2024-05-05 19:18:02 +08001187
Prabir Pradhand0aba782021-12-14 00:44:21 -08001188 void TearDown() override {
Alan Dingd53801c2024-05-08 16:45:29 -07001189 std::for_each(mVirtualDisplays.begin(), mVirtualDisplays.end(),
1190 SurfaceComposerClient::destroyVirtualDisplay);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001191 InputSurfacesTest::TearDown();
1192 }
1193
Prabir Pradhand0aba782021-12-14 00:44:21 -08001194 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1195 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001196 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001197 sp<IGraphicBufferProducer> producer;
1198 BufferQueue::createBufferQueue(&producer, &consumer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001199 consumer->setConsumerName(String8("Virtual disp consumer"));
1200 consumer->setDefaultBufferSize(width, height);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001201 mProducers.push_back(producer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001202
Prabir Pradhand0aba782021-12-14 00:44:21 -08001203 std::string name = "VirtualDisplay";
1204 name += std::to_string(mVirtualDisplays.size());
Alan Dingd53801c2024-05-08 16:45:29 -07001205 sp<IBinder> token = SurfaceComposerClient::createVirtualDisplay(name, isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001206 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001207 t.setDisplaySurface(token, producer);
1208 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1209 t.setDisplayLayerStack(token, layerStack);
1210 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1211 {offsetX, offsetY, offsetX + width, offsetY + height});
Vishnu Nair16a938f2021-09-24 07:14:54 -07001212 t.apply(true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001213
1214 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001215 }
1216
Prabir Pradhand0aba782021-12-14 00:44:21 -08001217 std::vector<sp<IBinder>> mVirtualDisplays;
1218 std::vector<sp<IGraphicBufferProducer>> mProducers;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001219};
1220
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001221TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001222 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1223 // Do not create a display associated with the LayerStack.
1224 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001225 surface->doTransaction([&](auto& t, auto& sc) { t.setLayerStack(sc, layerStack); });
Prabir Pradhand0aba782021-12-14 00:44:21 -08001226 surface->showAt(100, 100);
1227
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001228 // Touches should be dropped if the layer is on an invalid display.
Linnan Li13bf76a2024-05-05 19:18:02 +08001229 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001230 surface->assertNoEvent();
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001231
1232 // However, we still let the window be focused and receive keys.
Linnan Li13bf76a2024-05-05 19:18:02 +08001233 surface->requestFocus(toDisplayId(layerStack));
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001234 surface->assertFocusChange(true);
1235
Linnan Li13bf76a2024-05-05 19:18:02 +08001236 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001237 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001238}
1239
1240TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1241 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1242 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1243 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001244 surface->doTransaction([&](auto& t, auto& sc) { t.setLayerStack(sc, layerStack); });
Prabir Pradhand0aba782021-12-14 00:44:21 -08001245 surface->showAt(100, 100);
1246
Linnan Li13bf76a2024-05-05 19:18:02 +08001247 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001248 surface->expectTap(1, 1);
1249
Linnan Li13bf76a2024-05-05 19:18:02 +08001250 surface->requestFocus(toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001251 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001252 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001253 surface->expectKey(AKEYCODE_V);
1254}
1255
Vishnu Nair16a938f2021-09-24 07:14:54 -07001256TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1257 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1258 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1259 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001260 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001261 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1262 t.setLayerStack(sc, layerStack);
1263 });
1264 surface->showAt(100, 100);
1265
Linnan Li13bf76a2024-05-05 19:18:02 +08001266 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001267
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001268 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001269
Linnan Li13bf76a2024-05-05 19:18:02 +08001270 surface->requestFocus(toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001271 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001272 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001273 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001274}
1275
1276TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1277 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001278
1279 // Create the secure display as system, because only certain users can create secure displays.
1280 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001281 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001282 // Change the uid back to root.
1283 seteuid(AID_ROOT);
1284
Vishnu Nair16a938f2021-09-24 07:14:54 -07001285 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001286 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001287 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1288 t.setLayerStack(sc, layerStack);
1289 });
1290 surface->showAt(100, 100);
1291
Linnan Li13bf76a2024-05-05 19:18:02 +08001292 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001293 surface->expectTap(1, 1);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001294
Linnan Li13bf76a2024-05-05 19:18:02 +08001295 surface->requestFocus(toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001296 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001297 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001298
1299 surface->expectKey(AKEYCODE_V);
1300}
1301
Linnan Li13bf76a2024-05-05 19:18:02 +08001302} // namespace test
1303} // namespace android