blob: 06f00a4d7467b23391c8bb9e0d9ad8dbf1fd1971 [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
Jim Shargof48bef82024-12-04 18:56:59 +000037#include <gui/IConsumerListener.h>
38#include <gui/IGraphicBufferConsumer.h>
Vishnu Nairde19f852018-12-18 16:11:53 -080039#include <gui/ISurfaceComposer.h>
40#include <gui/Surface.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070041#include <gui/SurfaceComposerClient.h>
42#include <gui/SurfaceControl.h>
43
Chris Ye0783e992020-06-02 21:34:49 -070044#include <android/os/IInputFlinger.h>
chaviw98318de2021-05-19 16:45:23 -050045#include <gui/WindowInfo.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070046#include <input/Input.h>
Siarhei Vishniakou0438ca82024-03-12 14:27:25 -070047#include <input/InputConsumer.h>
Chris Ye0783e992020-06-02 21:34:49 -070048#include <input/InputTransport.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070049
Marin Shalamanova7fe3042021-01-29 21:02:08 +010050#include <ui/DisplayMode.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070051#include <ui/Rect.h>
52#include <ui/Region.h>
53
Prabir Pradhand0aba782021-12-14 00:44:21 -080054#include <private/android_filesystem_config.h>
55
Chris Ye0783e992020-06-02 21:34:49 -070056using android::os::IInputFlinger;
Robert Carr1c4c5592018-09-24 13:18:43 -070057
chaviw39d01472021-04-08 14:26:24 -050058using android::hardware::graphics::common::V1_1::BufferUsage;
59
chaviw98318de2021-05-19 16:45:23 -050060using android::gui::FocusRequest;
61using android::gui::InputApplicationInfo;
62using android::gui::TouchOcclusionMode;
63using android::gui::WindowInfo;
64
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 {
Harry Cutts917d6da2024-12-02 18:33:09 +000080 ALOGI("Linked to input");
Linnan Li13bf76a2024-05-05 19:18:02 +080081 }
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) {
Patrick Williams87602162024-11-05 10:13:19 -0600117 mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, true);
Linus Tufvessona1858822022-03-04 09:32:07 +0000118 } else {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800119 android::os::InputChannelCore tempChannel;
120 android::binder::Status result =
Arpit Singha9ec7d42024-12-04 14:34:05 +0000121 mInputFlinger->createInputChannel(sc->getName() + " channel", &tempChannel);
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800122 if (!result.isOk()) {
123 ADD_FAILURE() << "binder call to createInputChannel failed";
124 }
125 mClientChannel = InputChannel::create(std::move(tempChannel));
Patrick Williams87602162024-11-05 10:13:19 -0600126 mInputInfo->editInfo()->token = mClientChannel->getConnectionToken();
Linus Tufvessona1858822022-03-04 09:32:07 +0000127 mInputConsumer = new InputConsumer(mClientChannel);
128 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700129
Patrick Williams87602162024-11-05 10:13:19 -0600130 mInputInfo->editInfo()->name = "Test info";
131 mInputInfo->editInfo()->dispatchingTimeout = 5s;
132 mInputInfo->editInfo()->globalScaleFactor = 1.0;
133 mInputInfo->editInfo()->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();
Patrick Williams87602162024-11-05 10:13:19 -0600140 mInputInfo->editInfo()->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,
Arpit Singha9ec7d42024-12-04 14:34:05 +0000144 int width, int height,
145 const std::string& name) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800146 sp<SurfaceControl> surfaceControl =
Arpit Singha9ec7d42024-12-04 14:34:05 +0000147 scc->createSurface(String8(name.c_str()), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800148 PIXEL_FORMAT_RGBA_8888,
149 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -0800150 return std::make_unique<InputSurface>(surfaceControl, width, height);
151 }
152
153 static std::unique_ptr<InputSurface> makeBufferInputSurface(
Arpit Singha9ec7d42024-12-04 14:34:05 +0000154 const sp<SurfaceComposerClient>& scc, int width, int height,
155 const std::string& name = "Test Buffer Surface") {
Vishnu Nairde19f852018-12-18 16:11:53 -0800156 sp<SurfaceControl> surfaceControl =
Arpit Singha9ec7d42024-12-04 14:34:05 +0000157 scc->createSurface(String8(name.c_str()), width, height, PIXEL_FORMAT_RGBA_8888,
158 0 /* flags */);
Vishnu Nairde19f852018-12-18 16:11:53 -0800159 return std::make_unique<InputSurface>(surfaceControl, width, height);
160 }
161
162 static std::unique_ptr<InputSurface> makeContainerInputSurface(
Arpit Singha9ec7d42024-12-04 14:34:05 +0000163 const sp<SurfaceComposerClient>& scc, int width, int height,
164 const std::string& name = "Test Container Surface") {
Vishnu Nairde19f852018-12-18 16:11:53 -0800165 sp<SurfaceControl> surfaceControl =
Arpit Singha9ec7d42024-12-04 14:34:05 +0000166 scc->createSurface(String8(name.c_str()), 0 /* bufHeight */, 0 /* bufWidth */,
167 PIXEL_FORMAT_RGBA_8888,
Vishnu Nairde19f852018-12-18 16:11:53 -0800168 ISurfaceComposerClient::eFXSurfaceContainer);
169 return std::make_unique<InputSurface>(surfaceControl, width, height);
170 }
171
Linus Tufvessona1858822022-03-04 09:32:07 +0000172 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
Arpit Singha9ec7d42024-12-04 14:34:05 +0000173 const sp<SurfaceComposerClient>& scc, int width, int height,
174 const std::string& name = "Test Container Surface") {
Linus Tufvessona1858822022-03-04 09:32:07 +0000175 sp<SurfaceControl> surfaceControl =
Arpit Singha9ec7d42024-12-04 14:34:05 +0000176 scc->createSurface(String8(name.c_str()), 100 /* height */, 100 /* width */,
177 PIXEL_FORMAT_RGBA_8888,
Linus Tufvessona1858822022-03-04 09:32:07 +0000178 ISurfaceComposerClient::eFXSurfaceContainer);
179 return std::make_unique<InputSurface>(surfaceControl, width, height,
180 true /* noInputChannel */);
181 }
182
arthurhungb4a0f852020-06-16 11:02:50 +0800183 static std::unique_ptr<InputSurface> makeCursorInputSurface(
Arpit Singha9ec7d42024-12-04 14:34:05 +0000184 const sp<SurfaceComposerClient>& scc, int width, int height,
185 const std::string& name = "Test Cursor Surface") {
arthurhungb4a0f852020-06-16 11:02:50 +0800186 sp<SurfaceControl> surfaceControl =
Arpit Singha9ec7d42024-12-04 14:34:05 +0000187 scc->createSurface(String8(name.c_str()), 0 /* bufHeight */, 0 /* bufWidth */,
188 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eCursorWindow);
arthurhungb4a0f852020-06-16 11:02:50 +0800189 return std::make_unique<InputSurface>(surfaceControl, width, height);
190 }
191
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100192 void assertFocusChange(bool hasFocus) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800193 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100194 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700195 ASSERT_EQ(InputEventType::FOCUS, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800196 FocusEvent* focusEvent = static_cast<FocusEvent*>(ev);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100197 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) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800219 InputEvent* ev = consumeEvent();
chaviw39cfa2e2020-11-04 14:19:02 -0800220 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700221 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800222 MotionEvent* mev = static_cast<MotionEvent*>(ev);
chaviw39cfa2e2020-11-04 14:19:02 -0800223 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());
Linnan Li13bf76a2024-05-05 19:18:02 +0800231 mev = static_cast<MotionEvent*>(ev);
chaviw39cfa2e2020-11-04 14:19:02 -0800232 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) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800237 InputEvent* ev = consumeEvent();
Prabir Pradhand0aba782021-12-14 00:44:21 -0800238 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700239 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800240 MotionEvent* mev = static_cast<MotionEvent*>(ev);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800241 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
Linnan Li13bf76a2024-05-05 19:18:02 +0800242 const PointerCoords& coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800243 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());
Linnan Li13bf76a2024-05-05 19:18:02 +0800250 mev = static_cast<MotionEvent*>(ev);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800251 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) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800256 InputEvent* ev = consumeEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700257 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700258 ASSERT_EQ(InputEventType::KEY, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800259 KeyEvent* keyEvent = static_cast<KeyEvent*>(ev);
Vishnu Naira066d902021-09-13 18:40:17 -0700260 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());
Linnan Li13bf76a2024-05-05 19:18:02 +0800267 keyEvent = static_cast<KeyEvent*>(ev);
Vishnu Naira066d902021-09-13 18:40:17 -0700268 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(
Linnan Li13bf76a2024-05-05 19:18:02 +0800285 std::function<void(SurfaceComposerClient::Transaction&, const sp<SurfaceControl>&)>
chaviw39d01472021-04-08 14:26:24 -0500286 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700287 SurfaceComposerClient::Transaction t;
288 transactionBody(t, mSurfaceControl);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000289 t.apply(/*synchronously=*/true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700290 }
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);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000302 t.apply(/*synchronously=*/true);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500303 reportedListener->wait();
Robert Carr1c4c5592018-09-24 13:18:43 -0700304 }
305
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700306 void requestFocus(ui::LogicalDisplayId displayId = ui::LogicalDisplayId::DEFAULT) {
Vishnu Nair958da932020-08-21 17:12:37 -0700307 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800308 FocusRequest request;
Patrick Williams87602162024-11-05 10:13:19 -0600309 request.token = mInputInfo->getInfo()->token;
310 request.windowName = mInputInfo->getInfo()->name;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800311 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Linnan Li13bf76a2024-05-05 19:18:02 +0800312 request.displayId = displayId.val();
Vishnu Nair9ad01462021-01-15 12:55:14 -0800313 t.setFocusedWindow(request);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000314 t.apply(/*synchronously=*/true);
Vishnu Nair958da932020-08-21 17:12:37 -0700315 }
316
Robert Carr1c4c5592018-09-24 13:18:43 -0700317public:
Siarhei Vishniakou1d038d42024-10-29 11:25:58 -0700318 // But should be private
Patrick Williams87602162024-11-05 10:13:19 -0600319 sp<gui::WindowInfoHandle> mInputInfo = sp<gui::WindowInfoHandle>::make();
Robert Carr1c4c5592018-09-24 13:18:43 -0700320 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakou1d038d42024-10-29 11:25:58 -0700321
322private:
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500323 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700324 sp<IInputFlinger> mInputFlinger;
325
Robert Carr1c4c5592018-09-24 13:18:43 -0700326 PreallocatedInputEventFactory mInputEventFactory;
327 InputConsumer* mInputConsumer;
Siarhei Vishniakou1d038d42024-10-29 11:25:58 -0700328
329 InputEvent* consumeEvent(std::chrono::milliseconds timeout = 3000ms) {
330 mClientChannel->waitForMessage(timeout);
331
332 InputEvent* ev;
333 uint32_t seqId;
334 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
335 if (consumed != OK) {
336 return nullptr;
337 }
338 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
339 EXPECT_EQ(OK, status) << "Could not send finished signal";
340 return ev;
341 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700342};
343
chaviw39d01472021-04-08 14:26:24 -0500344class BlastInputSurface : public InputSurface {
345public:
Linnan Li13bf76a2024-05-05 19:18:02 +0800346 BlastInputSurface(const sp<SurfaceControl>& sc, const sp<SurfaceControl>& parentSc, int width,
chaviw39d01472021-04-08 14:26:24 -0500347 int height)
348 : InputSurface(sc, width, height) {
349 mParentSurfaceControl = parentSc;
350 }
351
352 ~BlastInputSurface() = default;
353
354 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
Linnan Li13bf76a2024-05-05 19:18:02 +0800355 const sp<SurfaceComposerClient>& scc, int width, int height) {
chaviw39d01472021-04-08 14:26:24 -0500356 sp<SurfaceControl> parentSc =
357 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
358 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
359 ISurfaceComposerClient::eFXSurfaceContainer);
360
361 sp<SurfaceControl> surfaceControl =
362 scc->createSurface(String8("Test Buffer Surface"), width, height,
363 PIXEL_FORMAT_RGBA_8888,
364 ISurfaceComposerClient::eFXSurfaceBufferState,
365 parentSc->getHandle());
366 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
367 }
368
369 void doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800370 std::function<void(SurfaceComposerClient::Transaction&, const sp<SurfaceControl>&)>
chaviw39d01472021-04-08 14:26:24 -0500371 transactionBody) override {
372 SurfaceComposerClient::Transaction t;
373 transactionBody(t, mParentSurfaceControl);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000374 t.apply(/*synchronously=*/true);
chaviw39d01472021-04-08 14:26:24 -0500375 }
376
377 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
378 SurfaceComposerClient::Transaction t;
379 t.show(mParentSurfaceControl);
380 t.setLayer(mParentSurfaceControl, LAYER_BASE);
381 t.setPosition(mParentSurfaceControl, x, y);
382 t.setCrop(mParentSurfaceControl, crop);
383
384 t.show(mSurfaceControl);
385 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
386 t.setCrop(mSurfaceControl, crop);
387 t.setAlpha(mSurfaceControl, 1);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000388 t.apply(/*synchronously=*/true);
chaviw39d01472021-04-08 14:26:24 -0500389 }
390
391private:
392 sp<SurfaceControl> mParentSurfaceControl;
393};
394
Robert Carr1c4c5592018-09-24 13:18:43 -0700395class InputSurfacesTest : public ::testing::Test {
396public:
Linnan Li13bf76a2024-05-05 19:18:02 +0800397 InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Robert Carr1c4c5592018-09-24 13:18:43 -0700398
399 void SetUp() {
400 mComposerClient = new SurfaceComposerClient;
401 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Huihong Luo31b5ac22022-08-15 20:38:10 -0700402 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
403 ASSERT_FALSE(ids.empty());
404 // display 0 is picked for now, can extend to support all displays if needed
405 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100406 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800407
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100408 ui::DisplayMode mode;
409 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800410
411 // After a new buffer is queued, SurfaceFlinger is notified and will
412 // latch the new buffer on next vsync. Let's heuristically wait for 3
413 // vsyncs.
Alec Mouri55e31032023-10-02 20:34:18 +0000414 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.peakRefreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700415 }
416
Linnan Li13bf76a2024-05-05 19:18:02 +0800417 void TearDown() { mComposerClient->dispose(); }
Robert Carr1c4c5592018-09-24 13:18:43 -0700418
Arpit Singha9ec7d42024-12-04 14:34:05 +0000419 std::unique_ptr<InputSurface> makeSurface(int width, int height,
420 const std::string& name = "Test Surface") const {
421 return InputSurface::makeColorInputSurface(mComposerClient, width, height, name);
Vishnu Nairde19f852018-12-18 16:11:53 -0800422 }
423
Linnan Li13bf76a2024-05-05 19:18:02 +0800424 void postBuffer(const sp<SurfaceControl>& layer, int32_t w, int32_t h) {
chaviw39d01472021-04-08 14:26:24 -0500425 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
426 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
427 sp<GraphicBuffer> buffer =
428 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
Prabir Pradhana97fd942024-10-28 21:21:53 +0000429 Transaction().setBuffer(layer, buffer).apply(/*synchronously=*/true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800430 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700431 }
432
433 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800434 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700435};
436
Linnan Li13bf76a2024-05-05 19:18:02 +0800437void injectTapOnDisplay(int x, int y, ui::LogicalDisplayId displayId) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700438 char *buf1, *buf2, *bufDisplayId;
Robert Carr1c4c5592018-09-24 13:18:43 -0700439 asprintf(&buf1, "%d", x);
440 asprintf(&buf2, "%d", y);
Linnan Li13bf76a2024-05-05 19:18:02 +0800441 asprintf(&bufDisplayId, "%d", displayId.val());
Robert Carr1c4c5592018-09-24 13:18:43 -0700442 if (fork() == 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700443 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
444 }
445}
446
447void injectTap(int x, int y) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700448 injectTapOnDisplay(x, y, ui::LogicalDisplayId::DEFAULT);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700449}
450
Linnan Li13bf76a2024-05-05 19:18:02 +0800451void injectKeyOnDisplay(uint32_t keycode, ui::LogicalDisplayId displayId) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700452 char *buf1, *bufDisplayId;
453 asprintf(&buf1, "%d", keycode);
Linnan Li13bf76a2024-05-05 19:18:02 +0800454 asprintf(&bufDisplayId, "%d", displayId.val());
Vishnu Nair16a938f2021-09-24 07:14:54 -0700455 if (fork() == 0) {
456 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
Robert Carr1c4c5592018-09-24 13:18:43 -0700457 }
458}
459
Vishnu Naira066d902021-09-13 18:40:17 -0700460void injectKey(uint32_t keycode) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700461 injectKeyOnDisplay(keycode, ui::LogicalDisplayId::INVALID);
Vishnu Naira066d902021-09-13 18:40:17 -0700462}
463
Robert Carr1c4c5592018-09-24 13:18:43 -0700464TEST_F(InputSurfacesTest, can_receive_input) {
465 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
466 surface->showAt(100, 100);
467
468 injectTap(101, 101);
469
Siarhei Vishniakou1d038d42024-10-29 11:25:58 -0700470 surface->expectTap(1, 1);
Robert Carr1c4c5592018-09-24 13:18:43 -0700471}
472
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100473/**
474 * Set up two surfaces side-by-side. Tap each surface.
475 * Next, swap the positions of the two surfaces. Inject tap into the two
476 * original locations. Ensure that the tap is received by the surfaces in the
477 * reverse order.
478 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700479TEST_F(InputSurfacesTest, input_respects_positioning) {
Arpit Singha9ec7d42024-12-04 14:34:05 +0000480 std::unique_ptr<InputSurface> surface = makeSurface(100, 100, "Left Surface");
Robert Carr1c4c5592018-09-24 13:18:43 -0700481 surface->showAt(100, 100);
482
Arpit Singha9ec7d42024-12-04 14:34:05 +0000483 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100, "Right Surface");
Robert Carr1c4c5592018-09-24 13:18:43 -0700484 surface2->showAt(200, 200);
485
486 injectTap(201, 201);
487 surface2->expectTap(1, 1);
488
489 injectTap(101, 101);
490 surface->expectTap(1, 1);
491
Linnan Li13bf76a2024-05-05 19:18:02 +0800492 surface2->doTransaction([](auto& t, auto& sc) { t.setPosition(sc, 100, 100); });
493 surface->doTransaction([](auto& t, auto& sc) { t.setPosition(sc, 200, 200); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700494
495 injectTap(101, 101);
496 surface2->expectTap(1, 1);
497
498 injectTap(201, 201);
499 surface->expectTap(1, 1);
500}
501
502TEST_F(InputSurfacesTest, input_respects_layering) {
Arpit Singha9ec7d42024-12-04 14:34:05 +0000503 std::unique_ptr<InputSurface> surface = makeSurface(100, 100, "Test Surface 1");
504 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100, "Test Surface 2");
Robert Carr1c4c5592018-09-24 13:18:43 -0700505
506 surface->showAt(10, 10);
507 surface2->showAt(10, 10);
508
Linnan Li13bf76a2024-05-05 19:18:02 +0800509 surface->doTransaction([](auto& t, auto& sc) { t.setLayer(sc, LAYER_BASE + 1); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700510
511 injectTap(11, 11);
512 surface->expectTap(1, 1);
513
Linnan Li13bf76a2024-05-05 19:18:02 +0800514 surface2->doTransaction([](auto& t, auto& sc) { t.setLayer(sc, LAYER_BASE + 1); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700515
516 injectTap(11, 11);
517 surface2->expectTap(1, 1);
518
Linnan Li13bf76a2024-05-05 19:18:02 +0800519 surface2->doTransaction([](auto& t, auto& sc) { t.hide(sc); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700520
521 injectTap(11, 11);
522 surface->expectTap(1, 1);
523}
524
Vishnu Nairde19f852018-12-18 16:11:53 -0800525// Surface Insets are set to offset the client content and draw a border around the client surface
526// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
527// of the client content.
528TEST_F(InputSurfacesTest, input_respects_surface_insets) {
Arpit Singha9ec7d42024-12-04 14:34:05 +0000529 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100, "Background Surface");
530 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100, "Foreground Surface");
Vishnu Nairde19f852018-12-18 16:11:53 -0800531 bgSurface->showAt(100, 100);
532
Patrick Williams87602162024-11-05 10:13:19 -0600533 fgSurface->mInputInfo->editInfo()->surfaceInset = 5;
Vishnu Nairde19f852018-12-18 16:11:53 -0800534 fgSurface->showAt(100, 100);
535
536 injectTap(106, 106);
537 fgSurface->expectTap(1, 1);
538
539 injectTap(101, 101);
540 bgSurface->expectTap(1, 1);
541}
542
Vishnu Nairfed7c122023-03-18 01:54:43 +0000543TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) {
Arpit Singha9ec7d42024-12-04 14:34:05 +0000544 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100, "Background Surface");
545 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100, "Foreground Surface");
Vishnu Nairfed7c122023-03-18 01:54:43 +0000546 bgSurface->showAt(100, 100);
547
Patrick Williams87602162024-11-05 10:13:19 -0600548 fgSurface->mInputInfo->editInfo()->surfaceInset = 5;
549 fgSurface->mInputInfo->editInfo()->replaceTouchableRegionWithCrop = true;
Vishnu Nairfed7c122023-03-18 01:54:43 +0000550 fgSurface->showAt(100, 100);
551
552 injectTap(106, 106);
553 fgSurface->expectTap(1, 1);
554
555 injectTap(101, 101);
556 bgSurface->expectTap(1, 1);
557}
558
Vishnu Nairde19f852018-12-18 16:11:53 -0800559// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
560TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
Arpit Singha9ec7d42024-12-04 14:34:05 +0000561 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100, "Parent Surface");
562 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100, "Child Surface");
Vishnu Nairde19f852018-12-18 16:11:53 -0800563 parentSurface->showAt(100, 100);
564
Patrick Williams87602162024-11-05 10:13:19 -0600565 childSurface->mInputInfo->editInfo()->surfaceInset = 10;
Vishnu Nairde19f852018-12-18 16:11:53 -0800566 childSurface->showAt(100, 100);
567
Linnan Li13bf76a2024-05-05 19:18:02 +0800568 childSurface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800569 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000570 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800571 });
572
573 injectTap(106, 106);
574 childSurface->expectTap(1, 1);
575
576 injectTap(101, 101);
577 parentSurface->expectTap(1, 1);
578}
579
Arthur Hung118b1142019-05-08 21:25:59 +0800580// Ensure a surface whose insets are scaled, handles the touch offset correctly.
581TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
Arpit Singha9ec7d42024-12-04 14:34:05 +0000582 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100, "Background Surface");
583 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100, "Foreground Surface");
Arthur Hung118b1142019-05-08 21:25:59 +0800584 bgSurface->showAt(100, 100);
585
Patrick Williams87602162024-11-05 10:13:19 -0600586 fgSurface->mInputInfo->editInfo()->surfaceInset = 5;
Arthur Hung118b1142019-05-08 21:25:59 +0800587 fgSurface->showAt(100, 100);
588
Linnan Li13bf76a2024-05-05 19:18:02 +0800589 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
Arthur Hung118b1142019-05-08 21:25:59 +0800590
591 // expect = touch / scale - inset
592 injectTap(112, 124);
593 fgSurface->expectTap(1, 1);
594
595 injectTap(101, 101);
596 bgSurface->expectTap(1, 1);
597}
598
Ady Abraham282f1d72019-07-24 18:05:56 -0700599TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
Arpit Singha9ec7d42024-12-04 14:34:05 +0000600 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100, "Background Surface");
601 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100, "Foreground Surface");
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700602 bgSurface->showAt(100, 100);
603
Ady Abraham282f1d72019-07-24 18:05:56 -0700604 // In case we pass the very big inset without any checking.
Patrick Williams87602162024-11-05 10:13:19 -0600605 fgSurface->mInputInfo->editInfo()->surfaceInset = INT32_MAX;
Ady Abraham282f1d72019-07-24 18:05:56 -0700606 fgSurface->showAt(100, 100);
607
Linnan Li13bf76a2024-05-05 19:18:02 +0800608 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
Ady Abraham282f1d72019-07-24 18:05:56 -0700609
610 // expect no crash for overflow, and inset size to be clamped to surface size
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700611 injectTap(112, 124);
612 bgSurface->expectTap(12, 24);
Ady Abraham282f1d72019-07-24 18:05:56 -0700613}
614
Prabir Pradhan33da9462022-06-14 14:55:57 +0000615TEST_F(InputSurfacesTest, touchable_region) {
616 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
617
Patrick Williams87602162024-11-05 10:13:19 -0600618 surface->mInputInfo->editInfo()->touchableRegion.set(Rect{19, 29, 21, 31});
Prabir Pradhan33da9462022-06-14 14:55:57 +0000619
620 surface->showAt(11, 22);
621
622 // A tap within the surface but outside the touchable region should not be sent to the surface.
623 injectTap(20, 30);
Siarhei Vishniakou1d038d42024-10-29 11:25:58 -0700624 surface->assertNoEvent();
Prabir Pradhan33da9462022-06-14 14:55:57 +0000625
626 injectTap(31, 52);
627 surface->expectTap(20, 30);
628}
629
630TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
Arpit Singha9ec7d42024-12-04 14:34:05 +0000631 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100, "Background Surface");
632 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100, "Foreground Surface");
Prabir Pradhan33da9462022-06-14 14:55:57 +0000633 bgSurface->showAt(100, 100);
634
635 // Set the touchable region to the values at the limit of its corresponding type.
636 // Since the surface is offset from the origin, the touchable region will be transformed into
637 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
638 // against such a situation.
Patrick Williams87602162024-11-05 10:13:19 -0600639 fgSurface->mInputInfo->editInfo()->touchableRegion.orSelf(
640 Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
Prabir Pradhan33da9462022-06-14 14:55:57 +0000641
642 fgSurface->showAt(100, 100);
643
644 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
645 // surface receives touch.
646 injectTap(112, 124);
647 bgSurface->expectTap(12, 24);
648}
649
650TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
Arpit Singha9ec7d42024-12-04 14:34:05 +0000651 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100, "Background Surface");
652 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100, "Foreground Surface");
Prabir Pradhan33da9462022-06-14 14:55:57 +0000653 bgSurface->showAt(0, 0);
654
Patrick Williams87602162024-11-05 10:13:19 -0600655 fgSurface->mInputInfo->editInfo()->touchableRegion.orSelf(
656 Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
Prabir Pradhan33da9462022-06-14 14:55:57 +0000657 fgSurface->showAt(0, 0);
658
Linnan Li13bf76a2024-05-05 19:18:02 +0800659 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
Prabir Pradhan33da9462022-06-14 14:55:57 +0000660
661 // Expect no crash for overflow.
662 injectTap(12, 24);
Prabir Pradhana4c59bd2023-04-10 20:54:04 +0000663 bgSurface->expectTap(12, 24);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000664}
665
Vishnu Nairde19f852018-12-18 16:11:53 -0800666// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
667TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
668 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +0800669 surface->doTransaction([](auto& t, auto& sc) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800670 Region transparentRegion(Rect(0, 0, 10, 10));
671 t.setTransparentRegionHint(sc, transparentRegion);
672 });
673 surface->showAt(100, 100);
674 injectTap(101, 101);
675 surface->expectTap(1, 1);
676}
677
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700678// TODO(b/139494112) update tests once we define expected behavior
679// Ensure we still send input to the surface regardless of surface visibility changes due to the
680// first buffer being submitted or alpha changes.
681// Original bug ref: b/120839715
682TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800683 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500684 std::unique_ptr<BlastInputSurface> bufferSurface =
685 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800686
687 bgSurface->showAt(10, 10);
688 bufferSurface->showAt(10, 10);
689
690 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700691 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800692
chaviw39d01472021-04-08 14:26:24 -0500693 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800694 injectTap(11, 11);
695 bufferSurface->expectTap(1, 1);
696}
697
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000698TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800699 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500700 std::unique_ptr<BlastInputSurface> bufferSurface =
701 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
702 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800703
704 bgSurface->showAt(10, 10);
705 bufferSurface->showAt(10, 10);
706
707 injectTap(11, 11);
708 bufferSurface->expectTap(1, 1);
709
Linnan Li13bf76a2024-05-05 19:18:02 +0800710 bufferSurface->doTransaction([](auto& t, auto& sc) { t.setAlpha(sc, 0.0); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800711
712 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000713 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800714}
715
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700716TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Arpit Singha9ec7d42024-12-04 14:34:05 +0000717 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100, "Background Surface");
718 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100, "Foreground Surface");
Vishnu Nairde19f852018-12-18 16:11:53 -0800719
720 bgSurface->showAt(10, 10);
721 fgSurface->showAt(10, 10);
722
723 injectTap(11, 11);
724 fgSurface->expectTap(1, 1);
725
Linnan Li13bf76a2024-05-05 19:18:02 +0800726 fgSurface->doTransaction([](auto& t, auto& sc) { t.setAlpha(sc, 0.0); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800727
728 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700729 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800730}
731
732TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
733 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
734 std::unique_ptr<InputSurface> containerSurface =
735 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
736
737 bgSurface->showAt(10, 10);
738 containerSurface->showAt(10, 10);
739
740 injectTap(11, 11);
741 containerSurface->expectTap(1, 1);
742
Linnan Li13bf76a2024-05-05 19:18:02 +0800743 containerSurface->doTransaction([](auto& t, auto& sc) { t.hide(sc); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800744
745 injectTap(11, 11);
746 bgSurface->expectTap(1, 1);
747}
chaviwfbe5d9c2018-12-26 12:23:37 -0800748
Arthur Hungd20b2702019-01-14 18:16:16 +0800749TEST_F(InputSurfacesTest, input_respects_outscreen) {
750 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
751 surface->showAt(-1, -1);
752
753 injectTap(0, 0);
754 surface->expectTap(1, 1);
755}
arthurhungb4a0f852020-06-16 11:02:50 +0800756
757TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
758 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
759 std::unique_ptr<InputSurface> cursorSurface =
760 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
761
762 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800763 cursorSurface->showAt(10, 10);
764
765 injectTap(11, 11);
766 surface->expectTap(1, 1);
767}
Vishnu Nair958da932020-08-21 17:12:37 -0700768
769TEST_F(InputSurfacesTest, can_be_focused) {
770 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
771 surface->showAt(100, 100);
772 surface->requestFocus();
773
774 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700775
776 injectKey(AKEYCODE_V);
777 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700778}
chaviw44a6d2b2020-09-08 17:14:16 -0700779
780TEST_F(InputSurfacesTest, rotate_surface) {
781 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
782 surface->showAt(10, 10);
Linnan Li13bf76a2024-05-05 19:18:02 +0800783 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700784 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
785 });
786 injectTap(8, 11);
787 surface->expectTap(1, 2);
788
Linnan Li13bf76a2024-05-05 19:18:02 +0800789 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700790 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
791 });
792 injectTap(9, 8);
793 surface->expectTap(1, 2);
794
Linnan Li13bf76a2024-05-05 19:18:02 +0800795 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700796 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
797 });
798 injectTap(12, 9);
799 surface->expectTap(1, 2);
800}
801
802TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
803 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
804 surface->showAt(10, 10);
Linnan Li13bf76a2024-05-05 19:18:02 +0800805 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700806 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
807 });
808 injectTap(2, 12);
809 surface->expectTap(1, 2);
810
Linnan Li13bf76a2024-05-05 19:18:02 +0800811 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700812 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
813 });
814 injectTap(8, 2);
815 surface->expectTap(1, 2);
816
Linnan Li13bf76a2024-05-05 19:18:02 +0800817 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700818 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
819 });
820 injectTap(18, 8);
821 surface->expectTap(1, 2);
822}
823
824TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
825 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -0600826 surface->mInputInfo->editInfo()->surfaceInset = 5;
chaviw44a6d2b2020-09-08 17:14:16 -0700827 surface->showAt(100, 100);
828
Linnan Li13bf76a2024-05-05 19:18:02 +0800829 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700830 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
831 });
832 injectTap(40, 120);
833 surface->expectTap(5, 10);
834
Linnan Li13bf76a2024-05-05 19:18:02 +0800835 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700836 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
837 });
838 injectTap(80, 40);
839 surface->expectTap(5, 10);
840
Linnan Li13bf76a2024-05-05 19:18:02 +0800841 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700842 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
843 });
844 injectTap(160, 80);
845 surface->expectTap(5, 10);
846}
847
chaviw39cfa2e2020-11-04 14:19:02 -0800848TEST_F(InputSurfacesTest, touch_flag_obscured) {
Arpit Singha9ec7d42024-12-04 14:34:05 +0000849 std::unique_ptr<InputSurface> surface = makeSurface(100, 100, "Touchable Surface");
chaviw39cfa2e2020-11-04 14:19:02 -0800850 surface->showAt(100, 100);
851
852 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
853 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
Arpit Singha9ec7d42024-12-04 14:34:05 +0000854 std::unique_ptr<InputSurface> nonTouchableSurface =
855 makeSurface(100, 100, "Non-Touchable Surface");
Patrick Williams87602162024-11-05 10:13:19 -0600856 nonTouchableSurface->mInputInfo->editInfo()
857 ->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
858 nonTouchableSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
Bernardo Rufino602ef712020-12-21 11:02:18 +0000859 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
860 // the default obscured/untrusted touch filter introduced in S.
Patrick Williams87602162024-11-05 10:13:19 -0600861 nonTouchableSurface->mInputInfo->editInfo()->touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800862 nonTouchableSurface->showAt(100, 100);
863
864 injectTap(190, 199);
865 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
866}
867
868TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
Arpit Singha9ec7d42024-12-04 14:34:05 +0000869 std::unique_ptr<InputSurface> surface = makeSurface(100, 100, "Test Surface");
chaviw39cfa2e2020-11-04 14:19:02 -0800870 surface->showAt(100, 100);
871
872 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
873 // that will be tapped. Window behind gets touch, but with flag
874 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
Arpit Singha9ec7d42024-12-04 14:34:05 +0000875 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100, "Parent Surface");
876 std::unique_ptr<InputSurface> nonTouchableSurface =
877 makeSurface(100, 100, "Non-Touchable Surface");
Patrick Williams87602162024-11-05 10:13:19 -0600878 nonTouchableSurface->mInputInfo->editInfo()
879 ->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
880 parentSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
881 true);
882 nonTouchableSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
883 parentSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800884 nonTouchableSurface->showAt(0, 0);
885 parentSurface->showAt(100, 100);
886
Linnan Li13bf76a2024-05-05 19:18:02 +0800887 nonTouchableSurface->doTransaction([&](auto& t, auto& sc) {
chaviw25714502021-02-11 10:01:08 -0800888 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800889 t.reparent(sc, parentSurface->mSurfaceControl);
890 });
891
892 injectTap(190, 199);
893 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
894}
895
896TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
Arpit Singha9ec7d42024-12-04 14:34:05 +0000897 std::unique_ptr<InputSurface> surface = makeSurface(100, 100, "Test Surface");
chaviw39cfa2e2020-11-04 14:19:02 -0800898 surface->showAt(100, 100);
899
900 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
901 // the touchable window. Window behind gets touch with no obscured flags.
Arpit Singha9ec7d42024-12-04 14:34:05 +0000902 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100, "Parent Surface");
903 std::unique_ptr<InputSurface> nonTouchableSurface =
904 makeSurface(100, 100, "Non-Touchable Surface");
Patrick Williams87602162024-11-05 10:13:19 -0600905 nonTouchableSurface->mInputInfo->editInfo()
906 ->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
907 parentSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
908 true);
909 nonTouchableSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
910 parentSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800911 nonTouchableSurface->showAt(0, 0);
912 parentSurface->showAt(50, 50);
913
Linnan Li13bf76a2024-05-05 19:18:02 +0800914 nonTouchableSurface->doTransaction([&](auto& t, auto& sc) {
chaviw25714502021-02-11 10:01:08 -0800915 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800916 t.reparent(sc, parentSurface->mSurfaceControl);
917 });
918
919 injectTap(101, 110);
920 surface->expectTap(1, 10);
921}
922
chaviw7e72caf2020-12-02 16:50:43 -0800923TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
924 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
925
926 std::unique_ptr<InputSurface> bufferSurface =
927 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Patrick Williams87602162024-11-05 10:13:19 -0600928 bufferSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
929 true);
930 bufferSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800931
932 surface->showAt(10, 10);
933 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
934
935 injectTap(11, 11);
936 surface->expectTap(1, 1);
937}
938
939TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
940 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
941
chaviw39d01472021-04-08 14:26:24 -0500942 std::unique_ptr<BlastInputSurface> bufferSurface =
943 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Patrick Williams87602162024-11-05 10:13:19 -0600944 bufferSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
945 true);
946 bufferSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800947
948 surface->showAt(10, 10);
949 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
950
951 injectTap(11, 11);
952 surface->expectTap(1, 1);
953}
954
Vishnu Naira066d902021-09-13 18:40:17 -0700955TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
956 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
957 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800958 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -0700959 surface->showAt(100, 100);
960
961 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700962 surface->expectTap(1, 1);
Vishnu Naira066d902021-09-13 18:40:17 -0700963
964 surface->requestFocus();
965 surface->assertFocusChange(true);
966 injectKey(AKEYCODE_V);
967 surface->expectKey(AKEYCODE_V);
968}
969
970TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
971 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +0800972 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -0700973 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
974 t.setMatrix(sc, 2.0, 0, 0, 2.0);
975 });
976 surface->showAt(100, 100);
977
978 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700979 surface->expectTap(.5, .5);
Vishnu Naira066d902021-09-13 18:40:17 -0700980
981 surface->requestFocus();
982 surface->assertFocusChange(true);
983 injectKey(AKEYCODE_V);
984 surface->expectKey(AKEYCODE_V);
985}
986
987TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
Arpit Singha9ec7d42024-12-04 14:34:05 +0000988 std::unique_ptr<InputSurface> surface = makeSurface(100, 100, "Test Surface");
Patrick Williams87602162024-11-05 10:13:19 -0600989 surface->mInputInfo->editInfo()->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);
Arpit Singha9ec7d42024-12-04 14:34:05 +0000993 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100, "Obscuring Surface");
Patrick Williams87602162024-11-05 10:13:19 -0600994 obscuringSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
995 true);
996 obscuringSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700997 obscuringSurface->showAt(100, 100);
998 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700999 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001000
1001 surface->requestFocus();
1002 surface->assertFocusChange(true);
1003 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001004 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001005}
1006
1007TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
Arpit Singha9ec7d42024-12-04 14:34:05 +00001008 std::unique_ptr<InputSurface> surface = makeSurface(100, 100, "Test Surface");
Patrick Williams87602162024-11-05 10:13:19 -06001009 surface->mInputInfo->editInfo()->ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -07001010 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001011 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -07001012 surface->showAt(100, 100);
Arpit Singha9ec7d42024-12-04 14:34:05 +00001013 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100, "Obscuring Surface");
Patrick Williams87602162024-11-05 10:13:19 -06001014 obscuringSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
1015 true);
1016 obscuringSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -07001017 obscuringSurface->showAt(190, 190);
1018
1019 injectTap(101, 101);
1020
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001021 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001022
1023 surface->requestFocus();
1024 surface->assertFocusChange(true);
1025 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001026 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001027}
1028
1029TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
Arpit Singha9ec7d42024-12-04 14:34:05 +00001030 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300, "Parent Surface");
Vishnu Naira066d902021-09-13 18:40:17 -07001031 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1032
Arpit Singha9ec7d42024-12-04 14:34:05 +00001033 std::unique_ptr<InputSurface> surface = makeSurface(100, 100, "Test Surface");
Vishnu Naira066d902021-09-13 18:40:17 -07001034 surface->showAt(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.setAlpha(parentSurface->mSurfaceControl, 0.9f);
1039 });
1040
1041 injectTap(101, 101);
1042
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001043 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001044
1045 surface->requestFocus();
1046 surface->assertFocusChange(true);
1047 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001048 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001049}
1050
1051TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
Arpit Singha9ec7d42024-12-04 14:34:05 +00001052 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300, "Parent Surface");
Vishnu Naira066d902021-09-13 18:40:17 -07001053 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1054
Arpit Singha9ec7d42024-12-04 14:34:05 +00001055 std::unique_ptr<InputSurface> surface = makeSurface(100, 100, "Test Surface");
Linnan Li13bf76a2024-05-05 19:18:02 +08001056 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -07001057 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1058 t.reparent(sc, parentSurface->mSurfaceControl);
1059 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1060 });
1061 surface->showAt(100, 100);
1062
1063 injectTap(111, 111);
1064
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001065 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001066
1067 surface->requestFocus();
1068 surface->assertFocusChange(true);
1069 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001070 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001071}
1072
Arthur Hung49d525a2021-11-19 15:11:51 +00001073TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1074 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1075
1076 std::unique_ptr<BlastInputSurface> bufferSurface =
1077 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1078
1079 surface->showAt(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -06001080 bufferSurface->mInputInfo->editInfo()->touchableRegion.orSelf(Rect(0, 0, 200, 200));
Arthur Hung49d525a2021-11-19 15:11:51 +00001081 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1082
1083 injectTap(101, 101);
1084 surface->expectTap(1, 1);
1085}
1086
Vishnu Naira066d902021-09-13 18:40:17 -07001087TEST_F(InputSurfacesTest, drop_input_policy) {
1088 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1089 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001090 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
Vishnu Naira066d902021-09-13 18:40:17 -07001091 surface->showAt(100, 100);
1092
1093 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001094 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001095
1096 surface->requestFocus();
1097 surface->assertFocusChange(true);
1098 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001099 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001100}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001101
Prabir Pradhan8c285982022-01-28 09:19:39 -08001102TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1103 std::unique_ptr<InputSurface> bufferSurface =
1104 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1105
1106 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1107
1108 bufferSurface->requestFocus();
1109 bufferSurface->assertFocusChange(true);
1110}
1111
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001112/**
1113 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1114 * its own crop.
1115 */
1116TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1117 std::unique_ptr<InputSurface> parentContainer =
Arpit Singha9ec7d42024-12-04 14:34:05 +00001118 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0,
1119 "Parent Container Surface");
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001120 std::unique_ptr<InputSurface> containerSurface =
Arpit Singha9ec7d42024-12-04 14:34:05 +00001121 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100,
1122 "Test Container Surface");
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001123 containerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001124 [&](auto& t, auto& sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
Patrick Williams87602162024-11-05 10:13:19 -06001125 containerSurface->mInputInfo->editInfo()->replaceTouchableRegionWithCrop = true;
1126 containerSurface->mInputInfo->editInfo()->touchableRegionCropHandle = nullptr;
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001127 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1128 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1129
1130 // Receives events inside its own crop
1131 injectTap(21, 21);
1132 containerSurface->expectTap(1, 1); // Event is in layer space
1133
1134 // Does not receive events outside its crop
1135 injectTap(26, 26);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001136 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001137}
1138
1139/**
1140 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1141 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1142 */
1143TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
Prabir Pradhan56d55bc2024-10-28 21:00:30 +00001144 std::unique_ptr<InputSurface> bgContainer =
Arpit Singha9ec7d42024-12-04 14:34:05 +00001145 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0,
1146 "Background Container Surface");
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001147 std::unique_ptr<InputSurface> parentContainer =
Arpit Singha9ec7d42024-12-04 14:34:05 +00001148 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0,
1149 "Parent Container Surface");
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001150 std::unique_ptr<InputSurface> containerSurface =
Arpit Singha9ec7d42024-12-04 14:34:05 +00001151 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100,
1152 "Test Container Surface");
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001153 containerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001154 [&](auto& t, auto& sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
Patrick Williams87602162024-11-05 10:13:19 -06001155 containerSurface->mInputInfo->editInfo()->replaceTouchableRegionWithCrop = true;
1156 containerSurface->mInputInfo->editInfo()->touchableRegionCropHandle = nullptr;
Prabir Pradhan56d55bc2024-10-28 21:00:30 +00001157 parentContainer->doTransaction(
1158 [&](auto& t, auto& sc) { t.reparent(sc, bgContainer->mSurfaceControl); });
1159 bgContainer->showAt(0, 0, Rect(0, 0, 100, 100));
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001160 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1161 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1162
1163 // Receives events inside parent bounds
1164 injectTap(21, 21);
1165 containerSurface->expectTap(1, 1); // Event is in layer space
1166
1167 // Does not receive events outside parent bounds
1168 injectTap(31, 31);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001169 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001170}
1171
1172/**
1173 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1174 * layer's bounds. The input events should be in the layer's coordinate space.
1175 */
1176TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1177 std::unique_ptr<InputSurface> cropLayer =
Arpit Singha9ec7d42024-12-04 14:34:05 +00001178 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0, "Crop Layer Surface");
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001179 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1180
1181 std::unique_ptr<InputSurface> containerSurface =
Arpit Singha9ec7d42024-12-04 14:34:05 +00001182 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100, "Container Surface");
Patrick Williams87602162024-11-05 10:13:19 -06001183 containerSurface->mInputInfo->editInfo()->replaceTouchableRegionWithCrop = true;
1184 containerSurface->mInputInfo->editInfo()->touchableRegionCropHandle =
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001185 cropLayer->mSurfaceControl->getHandle();
1186 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1187
1188 // Receives events inside crop layer bounds
1189 injectTap(51, 51);
1190 containerSurface->expectTap(41, 41); // Event is in layer space
1191
1192 // Does not receive events outside crop layer bounds
1193 injectTap(21, 21);
1194 injectTap(71, 71);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001195 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001196}
1197
Linus Tufvessona1858822022-03-04 09:32:07 +00001198TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1199 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1200
1201 parent->showAt(100, 100);
1202 injectTap(101, 101);
1203 parent->expectTap(1, 1);
1204
1205 std::unique_ptr<InputSurface> childContainerSurface =
1206 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1207 childContainerSurface->showAt(0, 0);
1208 childContainerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001209 [&](auto& t, auto& sc) { t.reparent(sc, parent->mSurfaceControl); });
Linus Tufvessona1858822022-03-04 09:32:07 +00001210 injectTap(101, 101);
1211
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001212 parent->assertNoEvent();
Linus Tufvessona1858822022-03-04 09:32:07 +00001213}
1214
Vishnu Nair16a938f2021-09-24 07:14:54 -07001215class MultiDisplayTests : public InputSurfacesTest {
1216public:
1217 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Linnan Li13bf76a2024-05-05 19:18:02 +08001218
Prabir Pradhand0aba782021-12-14 00:44:21 -08001219 void TearDown() override {
Alan Dingd53801c2024-05-08 16:45:29 -07001220 std::for_each(mVirtualDisplays.begin(), mVirtualDisplays.end(),
1221 SurfaceComposerClient::destroyVirtualDisplay);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001222 InputSurfacesTest::TearDown();
1223 }
1224
Prabir Pradhand0aba782021-12-14 00:44:21 -08001225 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1226 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001227 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001228 sp<IGraphicBufferProducer> producer;
1229 BufferQueue::createBufferQueue(&producer, &consumer);
Jim Shargoa2ab5be2024-12-04 18:30:35 +00001230 consumer->setConsumerName(String8("Virtual disp consumer (MultiDisplayTests)"));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001231 consumer->setDefaultBufferSize(width, height);
Jim Shargof48bef82024-12-04 18:56:59 +00001232
1233 class StubConsumerListener : public BnConsumerListener {
1234 virtual void onFrameAvailable(const BufferItem&) override {}
1235 virtual void onBuffersReleased() override {}
1236 virtual void onSidebandStreamChanged() override {}
1237 };
1238
1239 consumer->consumerConnect(sp<StubConsumerListener>::make(), true);
1240 mBufferQueues.push_back({consumer, producer});
Vishnu Nair16a938f2021-09-24 07:14:54 -07001241
Prabir Pradhand0aba782021-12-14 00:44:21 -08001242 std::string name = "VirtualDisplay";
1243 name += std::to_string(mVirtualDisplays.size());
Alan Dingd53801c2024-05-08 16:45:29 -07001244 sp<IBinder> token = SurfaceComposerClient::createVirtualDisplay(name, isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001245 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001246 t.setDisplaySurface(token, producer);
1247 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1248 t.setDisplayLayerStack(token, layerStack);
1249 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1250 {offsetX, offsetY, offsetX + width, offsetY + height});
Prabir Pradhana97fd942024-10-28 21:21:53 +00001251 t.apply(/*synchronously=*/true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001252
1253 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001254 }
1255
Prabir Pradhand0aba782021-12-14 00:44:21 -08001256 std::vector<sp<IBinder>> mVirtualDisplays;
Jim Shargof48bef82024-12-04 18:56:59 +00001257 std::vector<std::tuple<sp<IGraphicBufferConsumer>, sp<IGraphicBufferProducer>>> mBufferQueues;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001258};
1259
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001260TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001261 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1262 // Do not create a display associated with the LayerStack.
1263 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001264 surface->doTransaction([&](auto& t, auto& sc) { t.setLayerStack(sc, layerStack); });
Prabir Pradhand0aba782021-12-14 00:44:21 -08001265 surface->showAt(100, 100);
1266
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001267 // Touches should be dropped if the layer is on an invalid display.
Linnan Li13bf76a2024-05-05 19:18:02 +08001268 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001269 surface->assertNoEvent();
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001270
1271 // However, we still let the window be focused and receive keys.
Linnan Li13bf76a2024-05-05 19:18:02 +08001272 surface->requestFocus(toDisplayId(layerStack));
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001273 surface->assertFocusChange(true);
1274
Linnan Li13bf76a2024-05-05 19:18:02 +08001275 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001276 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001277}
1278
1279TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1280 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1281 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1282 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001283 surface->doTransaction([&](auto& t, auto& sc) { t.setLayerStack(sc, layerStack); });
Prabir Pradhand0aba782021-12-14 00:44:21 -08001284 surface->showAt(100, 100);
1285
Linnan Li13bf76a2024-05-05 19:18:02 +08001286 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001287 surface->expectTap(1, 1);
1288
Linnan Li13bf76a2024-05-05 19:18:02 +08001289 surface->requestFocus(toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001290 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001291 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001292 surface->expectKey(AKEYCODE_V);
1293}
1294
Vishnu Nair16a938f2021-09-24 07:14:54 -07001295TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1296 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1297 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1298 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001299 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001300 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1301 t.setLayerStack(sc, layerStack);
1302 });
1303 surface->showAt(100, 100);
1304
Linnan Li13bf76a2024-05-05 19:18:02 +08001305 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001306
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001307 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001308
Linnan Li13bf76a2024-05-05 19:18:02 +08001309 surface->requestFocus(toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001310 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001311 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001312 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001313}
1314
1315TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1316 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001317
1318 // Create the secure display as system, because only certain users can create secure displays.
1319 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001320 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001321 // Change the uid back to root.
1322 seteuid(AID_ROOT);
1323
Vishnu Nair16a938f2021-09-24 07:14:54 -07001324 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001325 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001326 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1327 t.setLayerStack(sc, layerStack);
1328 });
1329 surface->showAt(100, 100);
1330
Linnan Li13bf76a2024-05-05 19:18:02 +08001331 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001332 surface->expectTap(1, 1);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001333
Linnan Li13bf76a2024-05-05 19:18:02 +08001334 surface->requestFocus(toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001335 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001336 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001337
1338 surface->expectKey(AKEYCODE_V);
1339}
1340
Linnan Li13bf76a2024-05-05 19:18:02 +08001341} // namespace test
1342} // namespace android