blob: a1c7cbf4162e4db650a6da48daf0eee0d075b9c4 [file] [log] [blame]
Robert Carr1c4c5592018-09-24 13:18:43 -07001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <sys/time.h>
21#include <sys/types.h>
22#include <stdio.h>
23#include <poll.h>
24
25#include <memory>
26
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070027#include <android-base/thread_annotations.h>
Patrick Williams0a4981a2023-07-28 15:08:52 -050028#include <android/gui/BnWindowInfosReportedListener.h>
Vishnu Naira066d902021-09-13 18:40:17 -070029#include <android/keycodes.h>
Vishnu Nairde19f852018-12-18 16:11:53 -080030#include <android/native_window.h>
31
Robert Carr1c4c5592018-09-24 13:18:43 -070032#include <binder/Binder.h>
33#include <binder/IServiceManager.h>
34#include <binder/Parcel.h>
35#include <binder/ProcessState.h>
36
Vishnu Nairde19f852018-12-18 16:11:53 -080037#include <gui/ISurfaceComposer.h>
38#include <gui/Surface.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070039#include <gui/SurfaceComposerClient.h>
40#include <gui/SurfaceControl.h>
41
Chris Ye0783e992020-06-02 21:34:49 -070042#include <android/os/IInputFlinger.h>
chaviw98318de2021-05-19 16:45:23 -050043#include <gui/WindowInfo.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070044#include <input/Input.h>
Siarhei Vishniakou0438ca82024-03-12 14:27:25 -070045#include <input/InputConsumer.h>
Chris Ye0783e992020-06-02 21:34:49 -070046#include <input/InputTransport.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070047
Marin Shalamanova7fe3042021-01-29 21:02:08 +010048#include <ui/DisplayMode.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070049#include <ui/Rect.h>
50#include <ui/Region.h>
51
Prabir Pradhand0aba782021-12-14 00:44:21 -080052#include <private/android_filesystem_config.h>
53
Chris Ye0783e992020-06-02 21:34:49 -070054using android::os::IInputFlinger;
Robert Carr1c4c5592018-09-24 13:18:43 -070055
chaviw39d01472021-04-08 14:26:24 -050056using android::hardware::graphics::common::V1_1::BufferUsage;
57
chaviw98318de2021-05-19 16:45:23 -050058using android::gui::FocusRequest;
59using android::gui::InputApplicationInfo;
60using android::gui::TouchOcclusionMode;
61using android::gui::WindowInfo;
62
Linnan Li13bf76a2024-05-05 19:18:02 +080063namespace android {
64namespace {
65ui::LogicalDisplayId toDisplayId(ui::LayerStack layerStack) {
66 return ui::LogicalDisplayId{static_cast<int32_t>(layerStack.id)};
67}
68} // namespace
69namespace test {
Robert Carr1c4c5592018-09-24 13:18:43 -070070
Vishnu Nairde19f852018-12-18 16:11:53 -080071using Transaction = SurfaceComposerClient::Transaction;
72
Robert Carr1c4c5592018-09-24 13:18:43 -070073sp<IInputFlinger> getInputFlinger() {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -080074 sp<IBinder> input(defaultServiceManager()->waitForService(String16("inputflinger")));
Robert Carr1c4c5592018-09-24 13:18:43 -070075 if (input == nullptr) {
76 ALOGE("Failed to link to input service");
Linnan Li13bf76a2024-05-05 19:18:02 +080077 } else {
78 ALOGE("Linked to input");
79 }
Robert Carr1c4c5592018-09-24 13:18:43 -070080 return interface_cast<IInputFlinger>(input);
81}
82
83// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
84static const int LAYER_BASE = INT32_MAX - 10;
Chris Ye6c4243b2020-07-22 12:07:12 -070085static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070086
Patrick Williams0a4981a2023-07-28 15:08:52 -050087class SynchronousWindowInfosReportedListener : public gui::BnWindowInfosReportedListener {
88public:
89 binder::Status onWindowInfosReported() override {
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070090 std::scoped_lock lock{mLock};
Patrick Williams0a4981a2023-07-28 15:08:52 -050091 mWindowInfosReported = true;
92 mConditionVariable.notify_one();
93 return binder::Status::ok();
94 }
95
96 void wait() {
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070097 std::unique_lock lock{mLock};
98 android::base::ScopedLockAssertion assumeLocked(mLock);
99 mConditionVariable.wait(lock, [&]() REQUIRES(mLock) { return mWindowInfosReported; });
Patrick Williams0a4981a2023-07-28 15:08:52 -0500100 }
101
102private:
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -0700103 std::mutex mLock;
Patrick Williams0a4981a2023-07-28 15:08:52 -0500104 std::condition_variable mConditionVariable;
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -0700105 bool mWindowInfosReported GUARDED_BY(mLock){false};
Patrick Williams0a4981a2023-07-28 15:08:52 -0500106};
107
Robert Carr1c4c5592018-09-24 13:18:43 -0700108class InputSurface {
109public:
Linnan Li13bf76a2024-05-05 19:18:02 +0800110 InputSurface(const sp<SurfaceControl>& sc, int width, int height, bool noInputChannel = false) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800111 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -0700112
113 mInputFlinger = getInputFlinger();
Linus Tufvessona1858822022-03-04 09:32:07 +0000114 if (noInputChannel) {
Patrick Williams87602162024-11-05 10:13:19 -0600115 mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, true);
Linus Tufvessona1858822022-03-04 09:32:07 +0000116 } else {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800117 android::os::InputChannelCore tempChannel;
118 android::binder::Status result =
119 mInputFlinger->createInputChannel("testchannels", &tempChannel);
120 if (!result.isOk()) {
121 ADD_FAILURE() << "binder call to createInputChannel failed";
122 }
123 mClientChannel = InputChannel::create(std::move(tempChannel));
Patrick Williams87602162024-11-05 10:13:19 -0600124 mInputInfo->editInfo()->token = mClientChannel->getConnectionToken();
Linus Tufvessona1858822022-03-04 09:32:07 +0000125 mInputConsumer = new InputConsumer(mClientChannel);
126 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700127
Patrick Williams87602162024-11-05 10:13:19 -0600128 mInputInfo->editInfo()->name = "Test info";
129 mInputInfo->editInfo()->dispatchingTimeout = 5s;
130 mInputInfo->editInfo()->globalScaleFactor = 1.0;
131 mInputInfo->editInfo()->touchableRegion.orSelf(Rect(0, 0, width, height));
Robert Carr1c4c5592018-09-24 13:18:43 -0700132
Linus Tufvessona1858822022-03-04 09:32:07 +0000133 InputApplicationInfo aInfo;
134 aInfo.token = new BBinder();
135 aInfo.name = "Test app info";
136 aInfo.dispatchingTimeoutMillis =
137 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Patrick Williams87602162024-11-05 10:13:19 -0600138 mInputInfo->editInfo()->applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700139 }
140
Linnan Li13bf76a2024-05-05 19:18:02 +0800141 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient>& scc,
Vishnu Nairde19f852018-12-18 16:11:53 -0800142 int width, int height) {
143 sp<SurfaceControl> surfaceControl =
144 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800145 PIXEL_FORMAT_RGBA_8888,
146 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -0800147 return std::make_unique<InputSurface>(surfaceControl, width, height);
148 }
149
150 static std::unique_ptr<InputSurface> makeBufferInputSurface(
Linnan Li13bf76a2024-05-05 19:18:02 +0800151 const sp<SurfaceComposerClient>& scc, int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800152 sp<SurfaceControl> surfaceControl =
153 scc->createSurface(String8("Test Buffer Surface"), width, height,
154 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
155 return std::make_unique<InputSurface>(surfaceControl, width, height);
156 }
157
158 static std::unique_ptr<InputSurface> makeContainerInputSurface(
Linnan Li13bf76a2024-05-05 19:18:02 +0800159 const sp<SurfaceComposerClient>& scc, int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800160 sp<SurfaceControl> surfaceControl =
161 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
162 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
163 ISurfaceComposerClient::eFXSurfaceContainer);
164 return std::make_unique<InputSurface>(surfaceControl, width, height);
165 }
166
Linus Tufvessona1858822022-03-04 09:32:07 +0000167 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
Linnan Li13bf76a2024-05-05 19:18:02 +0800168 const sp<SurfaceComposerClient>& scc, int width, int height) {
Linus Tufvessona1858822022-03-04 09:32:07 +0000169 sp<SurfaceControl> surfaceControl =
170 scc->createSurface(String8("Test Container Surface"), 100 /* height */,
171 100 /* width */, PIXEL_FORMAT_RGBA_8888,
172 ISurfaceComposerClient::eFXSurfaceContainer);
173 return std::make_unique<InputSurface>(surfaceControl, width, height,
174 true /* noInputChannel */);
175 }
176
arthurhungb4a0f852020-06-16 11:02:50 +0800177 static std::unique_ptr<InputSurface> makeCursorInputSurface(
Linnan Li13bf76a2024-05-05 19:18:02 +0800178 const sp<SurfaceComposerClient>& scc, int width, int height) {
arthurhungb4a0f852020-06-16 11:02:50 +0800179 sp<SurfaceControl> surfaceControl =
180 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
181 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
182 ISurfaceComposerClient::eCursorWindow);
183 return std::make_unique<InputSurface>(surfaceControl, width, height);
184 }
185
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100186 void assertFocusChange(bool hasFocus) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800187 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100188 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700189 ASSERT_EQ(InputEventType::FOCUS, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800190 FocusEvent* focusEvent = static_cast<FocusEvent*>(ev);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100191 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
192 }
193
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700194 void expectTap(float x, float y) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700195 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100196 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700197 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700198 MotionEvent* mev = static_cast<MotionEvent*>(ev);
199 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
200 EXPECT_EQ(x, mev->getX(0));
201 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800202 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700203
204 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100205 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700206 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700207 mev = static_cast<MotionEvent*>(ev);
208 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800209 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700210 }
211
chaviw39cfa2e2020-11-04 14:19:02 -0800212 void expectTapWithFlag(int x, int y, int32_t flags) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800213 InputEvent* ev = consumeEvent();
chaviw39cfa2e2020-11-04 14:19:02 -0800214 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700215 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800216 MotionEvent* mev = static_cast<MotionEvent*>(ev);
chaviw39cfa2e2020-11-04 14:19:02 -0800217 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
218 EXPECT_EQ(x, mev->getX(0));
219 EXPECT_EQ(y, mev->getY(0));
220 EXPECT_EQ(flags, mev->getFlags() & flags);
221
222 ev = consumeEvent();
223 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700224 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800225 mev = static_cast<MotionEvent*>(ev);
chaviw39cfa2e2020-11-04 14:19:02 -0800226 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
227 EXPECT_EQ(flags, mev->getFlags() & flags);
228 }
229
Prabir Pradhand0aba782021-12-14 00:44:21 -0800230 void expectTapInDisplayCoordinates(int displayX, int displayY) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800231 InputEvent* ev = consumeEvent();
Prabir Pradhand0aba782021-12-14 00:44:21 -0800232 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700233 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800234 MotionEvent* mev = static_cast<MotionEvent*>(ev);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800235 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
Linnan Li13bf76a2024-05-05 19:18:02 +0800236 const PointerCoords& coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800237 EXPECT_EQ(displayX, coords.getX());
238 EXPECT_EQ(displayY, coords.getY());
239 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
240
241 ev = consumeEvent();
242 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700243 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800244 mev = static_cast<MotionEvent*>(ev);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800245 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
246 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
247 }
248
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -0700249 void expectKey(int32_t keycode) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800250 InputEvent* ev = consumeEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700251 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700252 ASSERT_EQ(InputEventType::KEY, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800253 KeyEvent* keyEvent = static_cast<KeyEvent*>(ev);
Vishnu Naira066d902021-09-13 18:40:17 -0700254 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
255 EXPECT_EQ(keycode, keyEvent->getKeyCode());
256 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
257
258 ev = consumeEvent();
259 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700260 ASSERT_EQ(InputEventType::KEY, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800261 keyEvent = static_cast<KeyEvent*>(ev);
Vishnu Naira066d902021-09-13 18:40:17 -0700262 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
263 EXPECT_EQ(keycode, keyEvent->getKeyCode());
264 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
265 }
266
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700267 void assertNoEvent() {
268 InputEvent* event = consumeEvent(/*timeout=*/100ms);
269 ASSERT_EQ(event, nullptr) << "Expected no event, but got " << *event;
270 }
271
chaviw39d01472021-04-08 14:26:24 -0500272 virtual ~InputSurface() {
Linus Tufvessona1858822022-03-04 09:32:07 +0000273 if (mClientChannel) {
274 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
275 }
chaviw39d01472021-04-08 14:26:24 -0500276 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700277
chaviw39d01472021-04-08 14:26:24 -0500278 virtual void doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800279 std::function<void(SurfaceComposerClient::Transaction&, const sp<SurfaceControl>&)>
chaviw39d01472021-04-08 14:26:24 -0500280 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700281 SurfaceComposerClient::Transaction t;
282 transactionBody(t, mSurfaceControl);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000283 t.apply(/*synchronously=*/true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700284 }
285
chaviw39d01472021-04-08 14:26:24 -0500286 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700287 SurfaceComposerClient::Transaction t;
288 t.show(mSurfaceControl);
289 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
290 t.setLayer(mSurfaceControl, LAYER_BASE);
291 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800292 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700293 t.setAlpha(mSurfaceControl, 1);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500294 auto reportedListener = sp<SynchronousWindowInfosReportedListener>::make();
295 t.addWindowInfosReportedListener(reportedListener);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000296 t.apply(/*synchronously=*/true);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500297 reportedListener->wait();
Robert Carr1c4c5592018-09-24 13:18:43 -0700298 }
299
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700300 void requestFocus(ui::LogicalDisplayId displayId = ui::LogicalDisplayId::DEFAULT) {
Vishnu Nair958da932020-08-21 17:12:37 -0700301 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800302 FocusRequest request;
Patrick Williams87602162024-11-05 10:13:19 -0600303 request.token = mInputInfo->getInfo()->token;
304 request.windowName = mInputInfo->getInfo()->name;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800305 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Linnan Li13bf76a2024-05-05 19:18:02 +0800306 request.displayId = displayId.val();
Vishnu Nair9ad01462021-01-15 12:55:14 -0800307 t.setFocusedWindow(request);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000308 t.apply(/*synchronously=*/true);
Vishnu Nair958da932020-08-21 17:12:37 -0700309 }
310
Robert Carr1c4c5592018-09-24 13:18:43 -0700311public:
Siarhei Vishniakou1d038d42024-10-29 11:25:58 -0700312 // But should be private
Patrick Williams87602162024-11-05 10:13:19 -0600313 sp<gui::WindowInfoHandle> mInputInfo = sp<gui::WindowInfoHandle>::make();
Robert Carr1c4c5592018-09-24 13:18:43 -0700314 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakou1d038d42024-10-29 11:25:58 -0700315
316private:
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500317 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700318 sp<IInputFlinger> mInputFlinger;
319
Robert Carr1c4c5592018-09-24 13:18:43 -0700320 PreallocatedInputEventFactory mInputEventFactory;
321 InputConsumer* mInputConsumer;
Siarhei Vishniakou1d038d42024-10-29 11:25:58 -0700322
323 InputEvent* consumeEvent(std::chrono::milliseconds timeout = 3000ms) {
324 mClientChannel->waitForMessage(timeout);
325
326 InputEvent* ev;
327 uint32_t seqId;
328 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
329 if (consumed != OK) {
330 return nullptr;
331 }
332 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
333 EXPECT_EQ(OK, status) << "Could not send finished signal";
334 return ev;
335 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700336};
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);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000368 t.apply(/*synchronously=*/true);
chaviw39d01472021-04-08 14:26:24 -0500369 }
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);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000382 t.apply(/*synchronously=*/true);
chaviw39d01472021-04-08 14:26:24 -0500383 }
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");
Prabir Pradhana97fd942024-10-28 21:21:53 +0000422 Transaction().setBuffer(layer, buffer).apply(/*synchronously=*/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) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700441 injectTapOnDisplay(x, y, ui::LogicalDisplayId::DEFAULT);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700442}
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) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700454 injectKeyOnDisplay(keycode, ui::LogicalDisplayId::INVALID);
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 Vishniakou1d038d42024-10-29 11:25:58 -0700463 surface->expectTap(1, 1);
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
Patrick Williams87602162024-11-05 10:13:19 -0600526 fgSurface->mInputInfo->editInfo()->surfaceInset = 5;
Vishnu Nairde19f852018-12-18 16:11:53 -0800527 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
Patrick Williams87602162024-11-05 10:13:19 -0600541 fgSurface->mInputInfo->editInfo()->surfaceInset = 5;
542 fgSurface->mInputInfo->editInfo()->replaceTouchableRegionWithCrop = true;
Vishnu Nairfed7c122023-03-18 01:54:43 +0000543 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
Patrick Williams87602162024-11-05 10:13:19 -0600558 childSurface->mInputInfo->editInfo()->surfaceInset = 10;
Vishnu Nairde19f852018-12-18 16:11:53 -0800559 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
Patrick Williams87602162024-11-05 10:13:19 -0600579 fgSurface->mInputInfo->editInfo()->surfaceInset = 5;
Arthur Hung118b1142019-05-08 21:25:59 +0800580 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.
Patrick Williams87602162024-11-05 10:13:19 -0600598 fgSurface->mInputInfo->editInfo()->surfaceInset = INT32_MAX;
Ady Abraham282f1d72019-07-24 18:05:56 -0700599 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
Patrick Williams87602162024-11-05 10:13:19 -0600611 surface->mInputInfo->editInfo()->touchableRegion.set(Rect{19, 29, 21, 31});
Prabir Pradhan33da9462022-06-14 14:55:57 +0000612
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);
Siarhei Vishniakou1d038d42024-10-29 11:25:58 -0700617 surface->assertNoEvent();
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.
Patrick Williams87602162024-11-05 10:13:19 -0600632 fgSurface->mInputInfo->editInfo()->touchableRegion.orSelf(
633 Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
Prabir Pradhan33da9462022-06-14 14:55:57 +0000634
635 fgSurface->showAt(100, 100);
636
637 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
638 // surface receives touch.
639 injectTap(112, 124);
640 bgSurface->expectTap(12, 24);
641}
642
643TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
644 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
645 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
646 bgSurface->showAt(0, 0);
647
Patrick Williams87602162024-11-05 10:13:19 -0600648 fgSurface->mInputInfo->editInfo()->touchableRegion.orSelf(
649 Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
Prabir Pradhan33da9462022-06-14 14:55:57 +0000650 fgSurface->showAt(0, 0);
651
Linnan Li13bf76a2024-05-05 19:18:02 +0800652 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
Prabir Pradhan33da9462022-06-14 14:55:57 +0000653
654 // Expect no crash for overflow.
655 injectTap(12, 24);
Prabir Pradhana4c59bd2023-04-10 20:54:04 +0000656 bgSurface->expectTap(12, 24);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000657}
658
Vishnu Nairde19f852018-12-18 16:11:53 -0800659// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
660TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
661 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +0800662 surface->doTransaction([](auto& t, auto& sc) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800663 Region transparentRegion(Rect(0, 0, 10, 10));
664 t.setTransparentRegionHint(sc, transparentRegion);
665 });
666 surface->showAt(100, 100);
667 injectTap(101, 101);
668 surface->expectTap(1, 1);
669}
670
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700671// TODO(b/139494112) update tests once we define expected behavior
672// Ensure we still send input to the surface regardless of surface visibility changes due to the
673// first buffer being submitted or alpha changes.
674// Original bug ref: b/120839715
675TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800676 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500677 std::unique_ptr<BlastInputSurface> bufferSurface =
678 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800679
680 bgSurface->showAt(10, 10);
681 bufferSurface->showAt(10, 10);
682
683 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700684 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800685
chaviw39d01472021-04-08 14:26:24 -0500686 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800687 injectTap(11, 11);
688 bufferSurface->expectTap(1, 1);
689}
690
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000691TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800692 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500693 std::unique_ptr<BlastInputSurface> bufferSurface =
694 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
695 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800696
697 bgSurface->showAt(10, 10);
698 bufferSurface->showAt(10, 10);
699
700 injectTap(11, 11);
701 bufferSurface->expectTap(1, 1);
702
Linnan Li13bf76a2024-05-05 19:18:02 +0800703 bufferSurface->doTransaction([](auto& t, auto& sc) { t.setAlpha(sc, 0.0); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800704
705 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000706 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800707}
708
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700709TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800710 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
711 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
712
713 bgSurface->showAt(10, 10);
714 fgSurface->showAt(10, 10);
715
716 injectTap(11, 11);
717 fgSurface->expectTap(1, 1);
718
Linnan Li13bf76a2024-05-05 19:18:02 +0800719 fgSurface->doTransaction([](auto& t, auto& sc) { t.setAlpha(sc, 0.0); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800720
721 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700722 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800723}
724
725TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
726 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
727 std::unique_ptr<InputSurface> containerSurface =
728 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
729
730 bgSurface->showAt(10, 10);
731 containerSurface->showAt(10, 10);
732
733 injectTap(11, 11);
734 containerSurface->expectTap(1, 1);
735
Linnan Li13bf76a2024-05-05 19:18:02 +0800736 containerSurface->doTransaction([](auto& t, auto& sc) { t.hide(sc); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800737
738 injectTap(11, 11);
739 bgSurface->expectTap(1, 1);
740}
chaviwfbe5d9c2018-12-26 12:23:37 -0800741
Arthur Hungd20b2702019-01-14 18:16:16 +0800742TEST_F(InputSurfacesTest, input_respects_outscreen) {
743 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
744 surface->showAt(-1, -1);
745
746 injectTap(0, 0);
747 surface->expectTap(1, 1);
748}
arthurhungb4a0f852020-06-16 11:02:50 +0800749
750TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
751 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
752 std::unique_ptr<InputSurface> cursorSurface =
753 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
754
755 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800756 cursorSurface->showAt(10, 10);
757
758 injectTap(11, 11);
759 surface->expectTap(1, 1);
760}
Vishnu Nair958da932020-08-21 17:12:37 -0700761
762TEST_F(InputSurfacesTest, can_be_focused) {
763 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
764 surface->showAt(100, 100);
765 surface->requestFocus();
766
767 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700768
769 injectKey(AKEYCODE_V);
770 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700771}
chaviw44a6d2b2020-09-08 17:14:16 -0700772
773TEST_F(InputSurfacesTest, rotate_surface) {
774 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
775 surface->showAt(10, 10);
Linnan Li13bf76a2024-05-05 19:18:02 +0800776 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700777 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
778 });
779 injectTap(8, 11);
780 surface->expectTap(1, 2);
781
Linnan Li13bf76a2024-05-05 19:18:02 +0800782 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700783 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
784 });
785 injectTap(9, 8);
786 surface->expectTap(1, 2);
787
Linnan Li13bf76a2024-05-05 19:18:02 +0800788 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700789 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
790 });
791 injectTap(12, 9);
792 surface->expectTap(1, 2);
793}
794
795TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
796 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
797 surface->showAt(10, 10);
Linnan Li13bf76a2024-05-05 19:18:02 +0800798 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700799 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
800 });
801 injectTap(2, 12);
802 surface->expectTap(1, 2);
803
Linnan Li13bf76a2024-05-05 19:18:02 +0800804 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700805 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
806 });
807 injectTap(8, 2);
808 surface->expectTap(1, 2);
809
Linnan Li13bf76a2024-05-05 19:18:02 +0800810 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700811 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
812 });
813 injectTap(18, 8);
814 surface->expectTap(1, 2);
815}
816
817TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
818 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -0600819 surface->mInputInfo->editInfo()->surfaceInset = 5;
chaviw44a6d2b2020-09-08 17:14:16 -0700820 surface->showAt(100, 100);
821
Linnan Li13bf76a2024-05-05 19:18:02 +0800822 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700823 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
824 });
825 injectTap(40, 120);
826 surface->expectTap(5, 10);
827
Linnan Li13bf76a2024-05-05 19:18:02 +0800828 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700829 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
830 });
831 injectTap(80, 40);
832 surface->expectTap(5, 10);
833
Linnan Li13bf76a2024-05-05 19:18:02 +0800834 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700835 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
836 });
837 injectTap(160, 80);
838 surface->expectTap(5, 10);
839}
840
chaviw39cfa2e2020-11-04 14:19:02 -0800841TEST_F(InputSurfacesTest, touch_flag_obscured) {
842 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
843 surface->showAt(100, 100);
844
845 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
846 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
847 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -0600848 nonTouchableSurface->mInputInfo->editInfo()
849 ->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
850 nonTouchableSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
Bernardo Rufino602ef712020-12-21 11:02:18 +0000851 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
852 // the default obscured/untrusted touch filter introduced in S.
Patrick Williams87602162024-11-05 10:13:19 -0600853 nonTouchableSurface->mInputInfo->editInfo()->touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800854 nonTouchableSurface->showAt(100, 100);
855
856 injectTap(190, 199);
857 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
858}
859
860TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
861 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
862 surface->showAt(100, 100);
863
864 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
865 // that will be tapped. Window behind gets touch, but with flag
866 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
867 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
868 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -0600869 nonTouchableSurface->mInputInfo->editInfo()
870 ->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
871 parentSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
872 true);
873 nonTouchableSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
874 parentSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800875 nonTouchableSurface->showAt(0, 0);
876 parentSurface->showAt(100, 100);
877
Linnan Li13bf76a2024-05-05 19:18:02 +0800878 nonTouchableSurface->doTransaction([&](auto& t, auto& sc) {
chaviw25714502021-02-11 10:01:08 -0800879 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800880 t.reparent(sc, parentSurface->mSurfaceControl);
881 });
882
883 injectTap(190, 199);
884 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
885}
886
887TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
888 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
889 surface->showAt(100, 100);
890
891 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
892 // the touchable window. Window behind gets touch with no obscured flags.
893 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
894 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -0600895 nonTouchableSurface->mInputInfo->editInfo()
896 ->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
897 parentSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
898 true);
899 nonTouchableSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
900 parentSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800901 nonTouchableSurface->showAt(0, 0);
902 parentSurface->showAt(50, 50);
903
Linnan Li13bf76a2024-05-05 19:18:02 +0800904 nonTouchableSurface->doTransaction([&](auto& t, auto& sc) {
chaviw25714502021-02-11 10:01:08 -0800905 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800906 t.reparent(sc, parentSurface->mSurfaceControl);
907 });
908
909 injectTap(101, 110);
910 surface->expectTap(1, 10);
911}
912
chaviw7e72caf2020-12-02 16:50:43 -0800913TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
914 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
915
916 std::unique_ptr<InputSurface> bufferSurface =
917 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Patrick Williams87602162024-11-05 10:13:19 -0600918 bufferSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
919 true);
920 bufferSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800921
922 surface->showAt(10, 10);
923 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
924
925 injectTap(11, 11);
926 surface->expectTap(1, 1);
927}
928
929TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
930 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
931
chaviw39d01472021-04-08 14:26:24 -0500932 std::unique_ptr<BlastInputSurface> bufferSurface =
933 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Patrick Williams87602162024-11-05 10:13:19 -0600934 bufferSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
935 true);
936 bufferSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800937
938 surface->showAt(10, 10);
939 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
940
941 injectTap(11, 11);
942 surface->expectTap(1, 1);
943}
944
Vishnu Naira066d902021-09-13 18:40:17 -0700945TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
946 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
947 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800948 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -0700949 surface->showAt(100, 100);
950
951 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700952 surface->expectTap(1, 1);
Vishnu Naira066d902021-09-13 18:40:17 -0700953
954 surface->requestFocus();
955 surface->assertFocusChange(true);
956 injectKey(AKEYCODE_V);
957 surface->expectKey(AKEYCODE_V);
958}
959
960TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
961 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +0800962 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -0700963 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
964 t.setMatrix(sc, 2.0, 0, 0, 2.0);
965 });
966 surface->showAt(100, 100);
967
968 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700969 surface->expectTap(.5, .5);
Vishnu Naira066d902021-09-13 18:40:17 -0700970
971 surface->requestFocus();
972 surface->assertFocusChange(true);
973 injectKey(AKEYCODE_V);
974 surface->expectKey(AKEYCODE_V);
975}
976
977TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
978 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -0600979 surface->mInputInfo->editInfo()->ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700980 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800981 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -0700982 surface->showAt(100, 100);
983 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -0600984 obscuringSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
985 true);
986 obscuringSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700987 obscuringSurface->showAt(100, 100);
988 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700989 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700990
991 surface->requestFocus();
992 surface->assertFocusChange(true);
993 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700994 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700995}
996
997TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
998 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -0600999 surface->mInputInfo->editInfo()->ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -07001000 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001001 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -07001002 surface->showAt(100, 100);
1003 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -06001004 obscuringSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
1005 true);
1006 obscuringSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -07001007 obscuringSurface->showAt(190, 190);
1008
1009 injectTap(101, 101);
1010
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001011 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001012
1013 surface->requestFocus();
1014 surface->assertFocusChange(true);
1015 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001016 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001017}
1018
1019TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
1020 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1021 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1022
1023 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1024 surface->showAt(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001025 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -07001026 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1027 t.reparent(sc, parentSurface->mSurfaceControl);
1028 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
1029 });
1030
1031 injectTap(101, 101);
1032
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001033 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001034
1035 surface->requestFocus();
1036 surface->assertFocusChange(true);
1037 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001038 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001039}
1040
1041TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
1042 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1043 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1044
1045 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001046 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -07001047 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1048 t.reparent(sc, parentSurface->mSurfaceControl);
1049 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1050 });
1051 surface->showAt(100, 100);
1052
1053 injectTap(111, 111);
1054
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001055 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001056
1057 surface->requestFocus();
1058 surface->assertFocusChange(true);
1059 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001060 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001061}
1062
Arthur Hung49d525a2021-11-19 15:11:51 +00001063TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1064 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1065
1066 std::unique_ptr<BlastInputSurface> bufferSurface =
1067 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1068
1069 surface->showAt(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -06001070 bufferSurface->mInputInfo->editInfo()->touchableRegion.orSelf(Rect(0, 0, 200, 200));
Arthur Hung49d525a2021-11-19 15:11:51 +00001071 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1072
1073 injectTap(101, 101);
1074 surface->expectTap(1, 1);
1075}
1076
Vishnu Naira066d902021-09-13 18:40:17 -07001077TEST_F(InputSurfacesTest, drop_input_policy) {
1078 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1079 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001080 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
Vishnu Naira066d902021-09-13 18:40:17 -07001081 surface->showAt(100, 100);
1082
1083 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001084 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001085
1086 surface->requestFocus();
1087 surface->assertFocusChange(true);
1088 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001089 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001090}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001091
Prabir Pradhan8c285982022-01-28 09:19:39 -08001092TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1093 std::unique_ptr<InputSurface> bufferSurface =
1094 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1095
1096 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1097
1098 bufferSurface->requestFocus();
1099 bufferSurface->assertFocusChange(true);
1100}
1101
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001102/**
1103 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1104 * its own crop.
1105 */
1106TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1107 std::unique_ptr<InputSurface> parentContainer =
1108 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1109 std::unique_ptr<InputSurface> containerSurface =
1110 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1111 containerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001112 [&](auto& t, auto& sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
Patrick Williams87602162024-11-05 10:13:19 -06001113 containerSurface->mInputInfo->editInfo()->replaceTouchableRegionWithCrop = true;
1114 containerSurface->mInputInfo->editInfo()->touchableRegionCropHandle = nullptr;
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001115 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1116 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1117
1118 // Receives events inside its own crop
1119 injectTap(21, 21);
1120 containerSurface->expectTap(1, 1); // Event is in layer space
1121
1122 // Does not receive events outside its crop
1123 injectTap(26, 26);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001124 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001125}
1126
1127/**
1128 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1129 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1130 */
1131TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
Prabir Pradhan56d55bc2024-10-28 21:00:30 +00001132 std::unique_ptr<InputSurface> bgContainer =
1133 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001134 std::unique_ptr<InputSurface> parentContainer =
1135 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1136 std::unique_ptr<InputSurface> containerSurface =
1137 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1138 containerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001139 [&](auto& t, auto& sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
Patrick Williams87602162024-11-05 10:13:19 -06001140 containerSurface->mInputInfo->editInfo()->replaceTouchableRegionWithCrop = true;
1141 containerSurface->mInputInfo->editInfo()->touchableRegionCropHandle = nullptr;
Prabir Pradhan56d55bc2024-10-28 21:00:30 +00001142 parentContainer->doTransaction(
1143 [&](auto& t, auto& sc) { t.reparent(sc, bgContainer->mSurfaceControl); });
1144 bgContainer->showAt(0, 0, Rect(0, 0, 100, 100));
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001145 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1146 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1147
1148 // Receives events inside parent bounds
1149 injectTap(21, 21);
1150 containerSurface->expectTap(1, 1); // Event is in layer space
1151
1152 // Does not receive events outside parent bounds
1153 injectTap(31, 31);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001154 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001155}
1156
1157/**
1158 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1159 * layer's bounds. The input events should be in the layer's coordinate space.
1160 */
1161TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1162 std::unique_ptr<InputSurface> cropLayer =
1163 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1164 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1165
1166 std::unique_ptr<InputSurface> containerSurface =
1167 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
Patrick Williams87602162024-11-05 10:13:19 -06001168 containerSurface->mInputInfo->editInfo()->replaceTouchableRegionWithCrop = true;
1169 containerSurface->mInputInfo->editInfo()->touchableRegionCropHandle =
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001170 cropLayer->mSurfaceControl->getHandle();
1171 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1172
1173 // Receives events inside crop layer bounds
1174 injectTap(51, 51);
1175 containerSurface->expectTap(41, 41); // Event is in layer space
1176
1177 // Does not receive events outside crop layer bounds
1178 injectTap(21, 21);
1179 injectTap(71, 71);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001180 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001181}
1182
Linus Tufvessona1858822022-03-04 09:32:07 +00001183TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1184 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1185
1186 parent->showAt(100, 100);
1187 injectTap(101, 101);
1188 parent->expectTap(1, 1);
1189
1190 std::unique_ptr<InputSurface> childContainerSurface =
1191 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1192 childContainerSurface->showAt(0, 0);
1193 childContainerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001194 [&](auto& t, auto& sc) { t.reparent(sc, parent->mSurfaceControl); });
Linus Tufvessona1858822022-03-04 09:32:07 +00001195 injectTap(101, 101);
1196
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001197 parent->assertNoEvent();
Linus Tufvessona1858822022-03-04 09:32:07 +00001198}
1199
Vishnu Nair16a938f2021-09-24 07:14:54 -07001200class MultiDisplayTests : public InputSurfacesTest {
1201public:
1202 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Linnan Li13bf76a2024-05-05 19:18:02 +08001203
Prabir Pradhand0aba782021-12-14 00:44:21 -08001204 void TearDown() override {
Alan Dingd53801c2024-05-08 16:45:29 -07001205 std::for_each(mVirtualDisplays.begin(), mVirtualDisplays.end(),
1206 SurfaceComposerClient::destroyVirtualDisplay);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001207 InputSurfacesTest::TearDown();
1208 }
1209
Prabir Pradhand0aba782021-12-14 00:44:21 -08001210 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1211 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001212 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001213 sp<IGraphicBufferProducer> producer;
1214 BufferQueue::createBufferQueue(&producer, &consumer);
Jim Shargoa2ab5be2024-12-04 18:30:35 +00001215 consumer->setConsumerName(String8("Virtual disp consumer (MultiDisplayTests)"));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001216 consumer->setDefaultBufferSize(width, height);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001217 mProducers.push_back(producer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001218
Prabir Pradhand0aba782021-12-14 00:44:21 -08001219 std::string name = "VirtualDisplay";
1220 name += std::to_string(mVirtualDisplays.size());
Alan Dingd53801c2024-05-08 16:45:29 -07001221 sp<IBinder> token = SurfaceComposerClient::createVirtualDisplay(name, isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001222 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001223 t.setDisplaySurface(token, producer);
1224 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1225 t.setDisplayLayerStack(token, layerStack);
1226 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1227 {offsetX, offsetY, offsetX + width, offsetY + height});
Prabir Pradhana97fd942024-10-28 21:21:53 +00001228 t.apply(/*synchronously=*/true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001229
1230 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001231 }
1232
Prabir Pradhand0aba782021-12-14 00:44:21 -08001233 std::vector<sp<IBinder>> mVirtualDisplays;
1234 std::vector<sp<IGraphicBufferProducer>> mProducers;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001235};
1236
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001237TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001238 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1239 // Do not create a display associated with the LayerStack.
1240 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001241 surface->doTransaction([&](auto& t, auto& sc) { t.setLayerStack(sc, layerStack); });
Prabir Pradhand0aba782021-12-14 00:44:21 -08001242 surface->showAt(100, 100);
1243
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001244 // Touches should be dropped if the layer is on an invalid display.
Linnan Li13bf76a2024-05-05 19:18:02 +08001245 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001246 surface->assertNoEvent();
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001247
1248 // However, we still let the window be focused and receive keys.
Linnan Li13bf76a2024-05-05 19:18:02 +08001249 surface->requestFocus(toDisplayId(layerStack));
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001250 surface->assertFocusChange(true);
1251
Linnan Li13bf76a2024-05-05 19:18:02 +08001252 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001253 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001254}
1255
1256TEST_F(MultiDisplayTests, virtual_display_receives_input) {
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) { t.setLayerStack(sc, layerStack); });
Prabir Pradhand0aba782021-12-14 00:44:21 -08001261 surface->showAt(100, 100);
1262
Linnan Li13bf76a2024-05-05 19:18:02 +08001263 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001264 surface->expectTap(1, 1);
1265
Linnan Li13bf76a2024-05-05 19:18:02 +08001266 surface->requestFocus(toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001267 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001268 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001269 surface->expectKey(AKEYCODE_V);
1270}
1271
Vishnu Nair16a938f2021-09-24 07:14:54 -07001272TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1273 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1274 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1275 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001276 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001277 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1278 t.setLayerStack(sc, layerStack);
1279 });
1280 surface->showAt(100, 100);
1281
Linnan Li13bf76a2024-05-05 19:18:02 +08001282 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001283
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001284 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001285
Linnan Li13bf76a2024-05-05 19:18:02 +08001286 surface->requestFocus(toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001287 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001288 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001289 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001290}
1291
1292TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1293 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001294
1295 // Create the secure display as system, because only certain users can create secure displays.
1296 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001297 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001298 // Change the uid back to root.
1299 seteuid(AID_ROOT);
1300
Vishnu Nair16a938f2021-09-24 07:14:54 -07001301 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001302 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001303 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1304 t.setLayerStack(sc, layerStack);
1305 });
1306 surface->showAt(100, 100);
1307
Linnan Li13bf76a2024-05-05 19:18:02 +08001308 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001309 surface->expectTap(1, 1);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001310
Linnan Li13bf76a2024-05-05 19:18:02 +08001311 surface->requestFocus(toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001312 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001313 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001314
1315 surface->expectKey(AKEYCODE_V);
1316}
1317
Linnan Li13bf76a2024-05-05 19:18:02 +08001318} // namespace test
1319} // namespace android