blob: 17630e304f11a74e0ec02d53e3e45e543c28bfce [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) {
115 mInputInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, true);
116 } 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));
Linus Tufvessona1858822022-03-04 09:32:07 +0000124 mInputInfo.token = mClientChannel->getConnectionToken();
125 mInputConsumer = new InputConsumer(mClientChannel);
126 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700127
Linus Tufvessona1858822022-03-04 09:32:07 +0000128 mInputInfo.name = "Test info";
129 mInputInfo.dispatchingTimeout = 5s;
130 mInputInfo.globalScaleFactor = 1.0;
131 mInputInfo.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();
138 mInputInfo.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
Egor Pasko5a67a562024-01-16 16:46:45 +0100186 InputEvent* consumeEvent(std::chrono::milliseconds timeout = 3000ms) {
187 mClientChannel->waitForMessage(timeout);
Robert Carr1c4c5592018-09-24 13:18:43 -0700188
Linnan Li13bf76a2024-05-05 19:18:02 +0800189 InputEvent* ev;
Robert Carr1c4c5592018-09-24 13:18:43 -0700190 uint32_t seqId;
191 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
192 if (consumed != OK) {
193 return nullptr;
194 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100195 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
196 EXPECT_EQ(OK, status) << "Could not send finished signal";
Robert Carr1c4c5592018-09-24 13:18:43 -0700197 return ev;
198 }
199
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100200 void assertFocusChange(bool hasFocus) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800201 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::FOCUS, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800204 FocusEvent* focusEvent = static_cast<FocusEvent*>(ev);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100205 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
206 }
207
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700208 void expectTap(float x, float y) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700209 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100210 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700211 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700212 MotionEvent* mev = static_cast<MotionEvent*>(ev);
213 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
214 EXPECT_EQ(x, mev->getX(0));
215 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800216 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700217
218 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100219 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700220 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700221 mev = static_cast<MotionEvent*>(ev);
222 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800223 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700224 }
225
chaviw39cfa2e2020-11-04 14:19:02 -0800226 void expectTapWithFlag(int x, int y, int32_t flags) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800227 InputEvent* ev = consumeEvent();
chaviw39cfa2e2020-11-04 14:19:02 -0800228 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700229 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800230 MotionEvent* mev = static_cast<MotionEvent*>(ev);
chaviw39cfa2e2020-11-04 14:19:02 -0800231 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
232 EXPECT_EQ(x, mev->getX(0));
233 EXPECT_EQ(y, mev->getY(0));
234 EXPECT_EQ(flags, mev->getFlags() & flags);
235
236 ev = consumeEvent();
237 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700238 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800239 mev = static_cast<MotionEvent*>(ev);
chaviw39cfa2e2020-11-04 14:19:02 -0800240 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
241 EXPECT_EQ(flags, mev->getFlags() & flags);
242 }
243
Prabir Pradhand0aba782021-12-14 00:44:21 -0800244 void expectTapInDisplayCoordinates(int displayX, int displayY) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800245 InputEvent* ev = consumeEvent();
Prabir Pradhand0aba782021-12-14 00:44:21 -0800246 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700247 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800248 MotionEvent* mev = static_cast<MotionEvent*>(ev);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800249 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
Linnan Li13bf76a2024-05-05 19:18:02 +0800250 const PointerCoords& coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800251 EXPECT_EQ(displayX, coords.getX());
252 EXPECT_EQ(displayY, coords.getY());
253 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
254
255 ev = consumeEvent();
256 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700257 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800258 mev = static_cast<MotionEvent*>(ev);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800259 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
260 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
261 }
262
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -0700263 void expectKey(int32_t keycode) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800264 InputEvent* ev = consumeEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700265 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* keyEvent = static_cast<KeyEvent*>(ev);
Vishnu Naira066d902021-09-13 18:40:17 -0700268 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
269 EXPECT_EQ(keycode, keyEvent->getKeyCode());
270 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
271
272 ev = consumeEvent();
273 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700274 ASSERT_EQ(InputEventType::KEY, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800275 keyEvent = static_cast<KeyEvent*>(ev);
Vishnu Naira066d902021-09-13 18:40:17 -0700276 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
277 EXPECT_EQ(keycode, keyEvent->getKeyCode());
278 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
279 }
280
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700281 void assertNoEvent() {
282 InputEvent* event = consumeEvent(/*timeout=*/100ms);
283 ASSERT_EQ(event, nullptr) << "Expected no event, but got " << *event;
284 }
285
chaviw39d01472021-04-08 14:26:24 -0500286 virtual ~InputSurface() {
Linus Tufvessona1858822022-03-04 09:32:07 +0000287 if (mClientChannel) {
288 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
289 }
chaviw39d01472021-04-08 14:26:24 -0500290 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700291
chaviw39d01472021-04-08 14:26:24 -0500292 virtual void doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800293 std::function<void(SurfaceComposerClient::Transaction&, const sp<SurfaceControl>&)>
chaviw39d01472021-04-08 14:26:24 -0500294 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700295 SurfaceComposerClient::Transaction t;
296 transactionBody(t, mSurfaceControl);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000297 t.apply(/*synchronously=*/true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700298 }
299
chaviw39d01472021-04-08 14:26:24 -0500300 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700301 SurfaceComposerClient::Transaction t;
302 t.show(mSurfaceControl);
303 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
304 t.setLayer(mSurfaceControl, LAYER_BASE);
305 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800306 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700307 t.setAlpha(mSurfaceControl, 1);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500308 auto reportedListener = sp<SynchronousWindowInfosReportedListener>::make();
309 t.addWindowInfosReportedListener(reportedListener);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000310 t.apply(/*synchronously=*/true);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500311 reportedListener->wait();
Robert Carr1c4c5592018-09-24 13:18:43 -0700312 }
313
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700314 void requestFocus(ui::LogicalDisplayId displayId = ui::LogicalDisplayId::DEFAULT) {
Vishnu Nair958da932020-08-21 17:12:37 -0700315 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800316 FocusRequest request;
317 request.token = mInputInfo.token;
318 request.windowName = mInputInfo.name;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800319 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Linnan Li13bf76a2024-05-05 19:18:02 +0800320 request.displayId = displayId.val();
Vishnu Nair9ad01462021-01-15 12:55:14 -0800321 t.setFocusedWindow(request);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000322 t.apply(/*synchronously=*/true);
Vishnu Nair958da932020-08-21 17:12:37 -0700323 }
324
Robert Carr1c4c5592018-09-24 13:18:43 -0700325public:
326 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500327 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700328 sp<IInputFlinger> mInputFlinger;
329
chaviw98318de2021-05-19 16:45:23 -0500330 WindowInfo mInputInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700331
332 PreallocatedInputEventFactory mInputEventFactory;
333 InputConsumer* mInputConsumer;
334};
335
chaviw39d01472021-04-08 14:26:24 -0500336class BlastInputSurface : public InputSurface {
337public:
Linnan Li13bf76a2024-05-05 19:18:02 +0800338 BlastInputSurface(const sp<SurfaceControl>& sc, const sp<SurfaceControl>& parentSc, int width,
chaviw39d01472021-04-08 14:26:24 -0500339 int height)
340 : InputSurface(sc, width, height) {
341 mParentSurfaceControl = parentSc;
342 }
343
344 ~BlastInputSurface() = default;
345
346 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
Linnan Li13bf76a2024-05-05 19:18:02 +0800347 const sp<SurfaceComposerClient>& scc, int width, int height) {
chaviw39d01472021-04-08 14:26:24 -0500348 sp<SurfaceControl> parentSc =
349 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
350 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
351 ISurfaceComposerClient::eFXSurfaceContainer);
352
353 sp<SurfaceControl> surfaceControl =
354 scc->createSurface(String8("Test Buffer Surface"), width, height,
355 PIXEL_FORMAT_RGBA_8888,
356 ISurfaceComposerClient::eFXSurfaceBufferState,
357 parentSc->getHandle());
358 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
359 }
360
361 void doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800362 std::function<void(SurfaceComposerClient::Transaction&, const sp<SurfaceControl>&)>
chaviw39d01472021-04-08 14:26:24 -0500363 transactionBody) override {
364 SurfaceComposerClient::Transaction t;
365 transactionBody(t, mParentSurfaceControl);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000366 t.apply(/*synchronously=*/true);
chaviw39d01472021-04-08 14:26:24 -0500367 }
368
369 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
370 SurfaceComposerClient::Transaction t;
371 t.show(mParentSurfaceControl);
372 t.setLayer(mParentSurfaceControl, LAYER_BASE);
373 t.setPosition(mParentSurfaceControl, x, y);
374 t.setCrop(mParentSurfaceControl, crop);
375
376 t.show(mSurfaceControl);
377 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
378 t.setCrop(mSurfaceControl, crop);
379 t.setAlpha(mSurfaceControl, 1);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000380 t.apply(/*synchronously=*/true);
chaviw39d01472021-04-08 14:26:24 -0500381 }
382
383private:
384 sp<SurfaceControl> mParentSurfaceControl;
385};
386
Robert Carr1c4c5592018-09-24 13:18:43 -0700387class InputSurfacesTest : public ::testing::Test {
388public:
Linnan Li13bf76a2024-05-05 19:18:02 +0800389 InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Robert Carr1c4c5592018-09-24 13:18:43 -0700390
391 void SetUp() {
392 mComposerClient = new SurfaceComposerClient;
393 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Huihong Luo31b5ac22022-08-15 20:38:10 -0700394 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
395 ASSERT_FALSE(ids.empty());
396 // display 0 is picked for now, can extend to support all displays if needed
397 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100398 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800399
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100400 ui::DisplayMode mode;
401 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800402
403 // After a new buffer is queued, SurfaceFlinger is notified and will
404 // latch the new buffer on next vsync. Let's heuristically wait for 3
405 // vsyncs.
Alec Mouri55e31032023-10-02 20:34:18 +0000406 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.peakRefreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700407 }
408
Linnan Li13bf76a2024-05-05 19:18:02 +0800409 void TearDown() { mComposerClient->dispose(); }
Robert Carr1c4c5592018-09-24 13:18:43 -0700410
411 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800412 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
413 }
414
Linnan Li13bf76a2024-05-05 19:18:02 +0800415 void postBuffer(const sp<SurfaceControl>& layer, int32_t w, int32_t h) {
chaviw39d01472021-04-08 14:26:24 -0500416 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
417 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
418 sp<GraphicBuffer> buffer =
419 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
Prabir Pradhana97fd942024-10-28 21:21:53 +0000420 Transaction().setBuffer(layer, buffer).apply(/*synchronously=*/true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800421 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700422 }
423
424 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800425 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700426};
427
Linnan Li13bf76a2024-05-05 19:18:02 +0800428void injectTapOnDisplay(int x, int y, ui::LogicalDisplayId displayId) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700429 char *buf1, *buf2, *bufDisplayId;
Robert Carr1c4c5592018-09-24 13:18:43 -0700430 asprintf(&buf1, "%d", x);
431 asprintf(&buf2, "%d", y);
Linnan Li13bf76a2024-05-05 19:18:02 +0800432 asprintf(&bufDisplayId, "%d", displayId.val());
Robert Carr1c4c5592018-09-24 13:18:43 -0700433 if (fork() == 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700434 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
435 }
436}
437
438void injectTap(int x, int y) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700439 injectTapOnDisplay(x, y, ui::LogicalDisplayId::DEFAULT);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700440}
441
Linnan Li13bf76a2024-05-05 19:18:02 +0800442void injectKeyOnDisplay(uint32_t keycode, ui::LogicalDisplayId displayId) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700443 char *buf1, *bufDisplayId;
444 asprintf(&buf1, "%d", keycode);
Linnan Li13bf76a2024-05-05 19:18:02 +0800445 asprintf(&bufDisplayId, "%d", displayId.val());
Vishnu Nair16a938f2021-09-24 07:14:54 -0700446 if (fork() == 0) {
447 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
Robert Carr1c4c5592018-09-24 13:18:43 -0700448 }
449}
450
Vishnu Naira066d902021-09-13 18:40:17 -0700451void injectKey(uint32_t keycode) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700452 injectKeyOnDisplay(keycode, ui::LogicalDisplayId::INVALID);
Vishnu Naira066d902021-09-13 18:40:17 -0700453}
454
Robert Carr1c4c5592018-09-24 13:18:43 -0700455TEST_F(InputSurfacesTest, can_receive_input) {
456 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
457 surface->showAt(100, 100);
458
459 injectTap(101, 101);
460
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100461 EXPECT_NE(surface->consumeEvent(), nullptr);
Robert Carr1c4c5592018-09-24 13:18:43 -0700462}
463
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100464/**
465 * Set up two surfaces side-by-side. Tap each surface.
466 * Next, swap the positions of the two surfaces. Inject tap into the two
467 * original locations. Ensure that the tap is received by the surfaces in the
468 * reverse order.
469 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700470TEST_F(InputSurfacesTest, input_respects_positioning) {
471 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
472 surface->showAt(100, 100);
473
474 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
475 surface2->showAt(200, 200);
476
477 injectTap(201, 201);
478 surface2->expectTap(1, 1);
479
480 injectTap(101, 101);
481 surface->expectTap(1, 1);
482
Linnan Li13bf76a2024-05-05 19:18:02 +0800483 surface2->doTransaction([](auto& t, auto& sc) { t.setPosition(sc, 100, 100); });
484 surface->doTransaction([](auto& t, auto& sc) { t.setPosition(sc, 200, 200); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700485
486 injectTap(101, 101);
487 surface2->expectTap(1, 1);
488
489 injectTap(201, 201);
490 surface->expectTap(1, 1);
491}
492
493TEST_F(InputSurfacesTest, input_respects_layering) {
494 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
495 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
496
497 surface->showAt(10, 10);
498 surface2->showAt(10, 10);
499
Linnan Li13bf76a2024-05-05 19:18:02 +0800500 surface->doTransaction([](auto& t, auto& sc) { t.setLayer(sc, LAYER_BASE + 1); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700501
502 injectTap(11, 11);
503 surface->expectTap(1, 1);
504
Linnan Li13bf76a2024-05-05 19:18:02 +0800505 surface2->doTransaction([](auto& t, auto& sc) { t.setLayer(sc, LAYER_BASE + 1); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700506
507 injectTap(11, 11);
508 surface2->expectTap(1, 1);
509
Linnan Li13bf76a2024-05-05 19:18:02 +0800510 surface2->doTransaction([](auto& t, auto& sc) { t.hide(sc); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700511
512 injectTap(11, 11);
513 surface->expectTap(1, 1);
514}
515
Vishnu Nairde19f852018-12-18 16:11:53 -0800516// Surface Insets are set to offset the client content and draw a border around the client surface
517// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
518// of the client content.
519TEST_F(InputSurfacesTest, input_respects_surface_insets) {
520 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
521 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
522 bgSurface->showAt(100, 100);
523
524 fgSurface->mInputInfo.surfaceInset = 5;
525 fgSurface->showAt(100, 100);
526
527 injectTap(106, 106);
528 fgSurface->expectTap(1, 1);
529
530 injectTap(101, 101);
531 bgSurface->expectTap(1, 1);
532}
533
Vishnu Nairfed7c122023-03-18 01:54:43 +0000534TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) {
535 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
536 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
537 bgSurface->showAt(100, 100);
538
539 fgSurface->mInputInfo.surfaceInset = 5;
540 fgSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
541 fgSurface->showAt(100, 100);
542
543 injectTap(106, 106);
544 fgSurface->expectTap(1, 1);
545
546 injectTap(101, 101);
547 bgSurface->expectTap(1, 1);
548}
549
Vishnu Nairde19f852018-12-18 16:11:53 -0800550// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
551TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
552 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
553 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
554 parentSurface->showAt(100, 100);
555
556 childSurface->mInputInfo.surfaceInset = 10;
557 childSurface->showAt(100, 100);
558
Linnan Li13bf76a2024-05-05 19:18:02 +0800559 childSurface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800560 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000561 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800562 });
563
564 injectTap(106, 106);
565 childSurface->expectTap(1, 1);
566
567 injectTap(101, 101);
568 parentSurface->expectTap(1, 1);
569}
570
Arthur Hung118b1142019-05-08 21:25:59 +0800571// Ensure a surface whose insets are scaled, handles the touch offset correctly.
572TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
573 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
574 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
575 bgSurface->showAt(100, 100);
576
577 fgSurface->mInputInfo.surfaceInset = 5;
578 fgSurface->showAt(100, 100);
579
Linnan Li13bf76a2024-05-05 19:18:02 +0800580 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
Arthur Hung118b1142019-05-08 21:25:59 +0800581
582 // expect = touch / scale - inset
583 injectTap(112, 124);
584 fgSurface->expectTap(1, 1);
585
586 injectTap(101, 101);
587 bgSurface->expectTap(1, 1);
588}
589
Ady Abraham282f1d72019-07-24 18:05:56 -0700590TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700591 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
Ady Abraham282f1d72019-07-24 18:05:56 -0700592 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700593 bgSurface->showAt(100, 100);
594
Ady Abraham282f1d72019-07-24 18:05:56 -0700595 // In case we pass the very big inset without any checking.
596 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
597 fgSurface->showAt(100, 100);
598
Linnan Li13bf76a2024-05-05 19:18:02 +0800599 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
Ady Abraham282f1d72019-07-24 18:05:56 -0700600
601 // expect no crash for overflow, and inset size to be clamped to surface size
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700602 injectTap(112, 124);
603 bgSurface->expectTap(12, 24);
Ady Abraham282f1d72019-07-24 18:05:56 -0700604}
605
Prabir Pradhan33da9462022-06-14 14:55:57 +0000606TEST_F(InputSurfacesTest, touchable_region) {
607 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
608
609 surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31});
610
611 surface->showAt(11, 22);
612
613 // A tap within the surface but outside the touchable region should not be sent to the surface.
614 injectTap(20, 30);
Egor Pasko5a67a562024-01-16 16:46:45 +0100615 EXPECT_EQ(surface->consumeEvent(/*timeout=*/200ms), nullptr);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000616
617 injectTap(31, 52);
618 surface->expectTap(20, 30);
619}
620
621TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
622 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
623 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
624 bgSurface->showAt(100, 100);
625
626 // Set the touchable region to the values at the limit of its corresponding type.
627 // Since the surface is offset from the origin, the touchable region will be transformed into
628 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
629 // against such a situation.
630 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
631
632 fgSurface->showAt(100, 100);
633
634 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
635 // surface receives touch.
636 injectTap(112, 124);
637 bgSurface->expectTap(12, 24);
638}
639
640TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
641 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
642 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
643 bgSurface->showAt(0, 0);
644
645 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
646 fgSurface->showAt(0, 0);
647
Linnan Li13bf76a2024-05-05 19:18:02 +0800648 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
Prabir Pradhan33da9462022-06-14 14:55:57 +0000649
650 // Expect no crash for overflow.
651 injectTap(12, 24);
Prabir Pradhana4c59bd2023-04-10 20:54:04 +0000652 bgSurface->expectTap(12, 24);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000653}
654
Vishnu Nairde19f852018-12-18 16:11:53 -0800655// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
656TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
657 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +0800658 surface->doTransaction([](auto& t, auto& sc) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800659 Region transparentRegion(Rect(0, 0, 10, 10));
660 t.setTransparentRegionHint(sc, transparentRegion);
661 });
662 surface->showAt(100, 100);
663 injectTap(101, 101);
664 surface->expectTap(1, 1);
665}
666
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700667// TODO(b/139494112) update tests once we define expected behavior
668// Ensure we still send input to the surface regardless of surface visibility changes due to the
669// first buffer being submitted or alpha changes.
670// Original bug ref: b/120839715
671TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800672 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500673 std::unique_ptr<BlastInputSurface> bufferSurface =
674 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800675
676 bgSurface->showAt(10, 10);
677 bufferSurface->showAt(10, 10);
678
679 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700680 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800681
chaviw39d01472021-04-08 14:26:24 -0500682 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800683 injectTap(11, 11);
684 bufferSurface->expectTap(1, 1);
685}
686
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000687TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800688 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500689 std::unique_ptr<BlastInputSurface> bufferSurface =
690 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
691 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800692
693 bgSurface->showAt(10, 10);
694 bufferSurface->showAt(10, 10);
695
696 injectTap(11, 11);
697 bufferSurface->expectTap(1, 1);
698
Linnan Li13bf76a2024-05-05 19:18:02 +0800699 bufferSurface->doTransaction([](auto& t, auto& sc) { t.setAlpha(sc, 0.0); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800700
701 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000702 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800703}
704
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700705TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800706 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
707 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
708
709 bgSurface->showAt(10, 10);
710 fgSurface->showAt(10, 10);
711
712 injectTap(11, 11);
713 fgSurface->expectTap(1, 1);
714
Linnan Li13bf76a2024-05-05 19:18:02 +0800715 fgSurface->doTransaction([](auto& t, auto& sc) { t.setAlpha(sc, 0.0); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800716
717 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700718 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800719}
720
721TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
722 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
723 std::unique_ptr<InputSurface> containerSurface =
724 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
725
726 bgSurface->showAt(10, 10);
727 containerSurface->showAt(10, 10);
728
729 injectTap(11, 11);
730 containerSurface->expectTap(1, 1);
731
Linnan Li13bf76a2024-05-05 19:18:02 +0800732 containerSurface->doTransaction([](auto& t, auto& sc) { t.hide(sc); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800733
734 injectTap(11, 11);
735 bgSurface->expectTap(1, 1);
736}
chaviwfbe5d9c2018-12-26 12:23:37 -0800737
Arthur Hungd20b2702019-01-14 18:16:16 +0800738TEST_F(InputSurfacesTest, input_respects_outscreen) {
739 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
740 surface->showAt(-1, -1);
741
742 injectTap(0, 0);
743 surface->expectTap(1, 1);
744}
arthurhungb4a0f852020-06-16 11:02:50 +0800745
746TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
747 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
748 std::unique_ptr<InputSurface> cursorSurface =
749 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
750
751 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800752 cursorSurface->showAt(10, 10);
753
754 injectTap(11, 11);
755 surface->expectTap(1, 1);
756}
Vishnu Nair958da932020-08-21 17:12:37 -0700757
758TEST_F(InputSurfacesTest, can_be_focused) {
759 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
760 surface->showAt(100, 100);
761 surface->requestFocus();
762
763 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700764
765 injectKey(AKEYCODE_V);
766 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700767}
chaviw44a6d2b2020-09-08 17:14:16 -0700768
769TEST_F(InputSurfacesTest, rotate_surface) {
770 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
771 surface->showAt(10, 10);
Linnan Li13bf76a2024-05-05 19:18:02 +0800772 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700773 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
774 });
775 injectTap(8, 11);
776 surface->expectTap(1, 2);
777
Linnan Li13bf76a2024-05-05 19:18:02 +0800778 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700779 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
780 });
781 injectTap(9, 8);
782 surface->expectTap(1, 2);
783
Linnan Li13bf76a2024-05-05 19:18:02 +0800784 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700785 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
786 });
787 injectTap(12, 9);
788 surface->expectTap(1, 2);
789}
790
791TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
792 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
793 surface->showAt(10, 10);
Linnan Li13bf76a2024-05-05 19:18:02 +0800794 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700795 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
796 });
797 injectTap(2, 12);
798 surface->expectTap(1, 2);
799
Linnan Li13bf76a2024-05-05 19:18:02 +0800800 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700801 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
802 });
803 injectTap(8, 2);
804 surface->expectTap(1, 2);
805
Linnan Li13bf76a2024-05-05 19:18:02 +0800806 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700807 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
808 });
809 injectTap(18, 8);
810 surface->expectTap(1, 2);
811}
812
813TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
814 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
815 surface->mInputInfo.surfaceInset = 5;
816 surface->showAt(100, 100);
817
Linnan Li13bf76a2024-05-05 19:18:02 +0800818 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700819 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
820 });
821 injectTap(40, 120);
822 surface->expectTap(5, 10);
823
Linnan Li13bf76a2024-05-05 19:18:02 +0800824 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700825 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
826 });
827 injectTap(80, 40);
828 surface->expectTap(5, 10);
829
Linnan Li13bf76a2024-05-05 19:18:02 +0800830 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700831 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
832 });
833 injectTap(160, 80);
834 surface->expectTap(5, 10);
835}
836
chaviw39cfa2e2020-11-04 14:19:02 -0800837TEST_F(InputSurfacesTest, touch_flag_obscured) {
838 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
839 surface->showAt(100, 100);
840
841 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
842 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
843 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800844 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000845 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
Bernardo Rufino602ef712020-12-21 11:02:18 +0000846 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
847 // the default obscured/untrusted touch filter introduced in S.
848 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800849 nonTouchableSurface->showAt(100, 100);
850
851 injectTap(190, 199);
852 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
853}
854
855TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
856 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
857 surface->showAt(100, 100);
858
859 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
860 // that will be tapped. Window behind gets touch, but with flag
861 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
862 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
863 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800864 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
865 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000866 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
867 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800868 nonTouchableSurface->showAt(0, 0);
869 parentSurface->showAt(100, 100);
870
Linnan Li13bf76a2024-05-05 19:18:02 +0800871 nonTouchableSurface->doTransaction([&](auto& t, auto& sc) {
chaviw25714502021-02-11 10:01:08 -0800872 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800873 t.reparent(sc, parentSurface->mSurfaceControl);
874 });
875
876 injectTap(190, 199);
877 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
878}
879
880TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
881 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
882 surface->showAt(100, 100);
883
884 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
885 // the touchable window. Window behind gets touch with no obscured flags.
886 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
887 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800888 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
889 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000890 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
891 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800892 nonTouchableSurface->showAt(0, 0);
893 parentSurface->showAt(50, 50);
894
Linnan Li13bf76a2024-05-05 19:18:02 +0800895 nonTouchableSurface->doTransaction([&](auto& t, auto& sc) {
chaviw25714502021-02-11 10:01:08 -0800896 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800897 t.reparent(sc, parentSurface->mSurfaceControl);
898 });
899
900 injectTap(101, 110);
901 surface->expectTap(1, 10);
902}
903
chaviw7e72caf2020-12-02 16:50:43 -0800904TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
905 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
906
907 std::unique_ptr<InputSurface> bufferSurface =
908 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800909 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000910 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800911
912 surface->showAt(10, 10);
913 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
914
915 injectTap(11, 11);
916 surface->expectTap(1, 1);
917}
918
919TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
920 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
921
chaviw39d01472021-04-08 14:26:24 -0500922 std::unique_ptr<BlastInputSurface> bufferSurface =
923 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800924 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000925 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800926
927 surface->showAt(10, 10);
928 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
929
930 injectTap(11, 11);
931 surface->expectTap(1, 1);
932}
933
Vishnu Naira066d902021-09-13 18:40:17 -0700934TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
935 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
936 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800937 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -0700938 surface->showAt(100, 100);
939
940 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700941 surface->expectTap(1, 1);
Vishnu Naira066d902021-09-13 18:40:17 -0700942
943 surface->requestFocus();
944 surface->assertFocusChange(true);
945 injectKey(AKEYCODE_V);
946 surface->expectKey(AKEYCODE_V);
947}
948
949TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
950 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +0800951 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -0700952 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
953 t.setMatrix(sc, 2.0, 0, 0, 2.0);
954 });
955 surface->showAt(100, 100);
956
957 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700958 surface->expectTap(.5, .5);
Vishnu Naira066d902021-09-13 18:40:17 -0700959
960 surface->requestFocus();
961 surface->assertFocusChange(true);
962 injectKey(AKEYCODE_V);
963 surface->expectKey(AKEYCODE_V);
964}
965
966TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
967 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000968 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700969 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800970 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -0700971 surface->showAt(100, 100);
972 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800973 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000974 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700975 obscuringSurface->showAt(100, 100);
976 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700977 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700978
979 surface->requestFocus();
980 surface->assertFocusChange(true);
981 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700982 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700983}
984
985TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
986 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000987 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700988 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800989 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -0700990 surface->showAt(100, 100);
991 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800992 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000993 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700994 obscuringSurface->showAt(190, 190);
995
996 injectTap(101, 101);
997
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700998 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700999
1000 surface->requestFocus();
1001 surface->assertFocusChange(true);
1002 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001003 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001004}
1005
1006TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
1007 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1008 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1009
1010 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1011 surface->showAt(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001012 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -07001013 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1014 t.reparent(sc, parentSurface->mSurfaceControl);
1015 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
1016 });
1017
1018 injectTap(101, 101);
1019
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001020 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001021
1022 surface->requestFocus();
1023 surface->assertFocusChange(true);
1024 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001025 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001026}
1027
1028TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
1029 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1030 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1031
1032 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001033 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -07001034 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1035 t.reparent(sc, parentSurface->mSurfaceControl);
1036 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1037 });
1038 surface->showAt(100, 100);
1039
1040 injectTap(111, 111);
1041
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001042 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001043
1044 surface->requestFocus();
1045 surface->assertFocusChange(true);
1046 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001047 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001048}
1049
Arthur Hung49d525a2021-11-19 15:11:51 +00001050TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1051 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1052
1053 std::unique_ptr<BlastInputSurface> bufferSurface =
1054 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1055
1056 surface->showAt(100, 100);
1057 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
1058 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1059
1060 injectTap(101, 101);
1061 surface->expectTap(1, 1);
1062}
1063
Vishnu Naira066d902021-09-13 18:40:17 -07001064TEST_F(InputSurfacesTest, drop_input_policy) {
1065 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1066 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001067 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
Vishnu Naira066d902021-09-13 18:40:17 -07001068 surface->showAt(100, 100);
1069
1070 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001071 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001072
1073 surface->requestFocus();
1074 surface->assertFocusChange(true);
1075 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001076 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001077}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001078
Prabir Pradhan8c285982022-01-28 09:19:39 -08001079TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1080 std::unique_ptr<InputSurface> bufferSurface =
1081 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1082
1083 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1084
1085 bufferSurface->requestFocus();
1086 bufferSurface->assertFocusChange(true);
1087}
1088
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001089/**
1090 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1091 * its own crop.
1092 */
1093TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1094 std::unique_ptr<InputSurface> parentContainer =
1095 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1096 std::unique_ptr<InputSurface> containerSurface =
1097 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1098 containerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001099 [&](auto& t, auto& sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001100 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1101 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1102 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1103 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1104
1105 // Receives events inside its own crop
1106 injectTap(21, 21);
1107 containerSurface->expectTap(1, 1); // Event is in layer space
1108
1109 // Does not receive events outside its crop
1110 injectTap(26, 26);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001111 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001112}
1113
1114/**
1115 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1116 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1117 */
1118TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
Prabir Pradhan56d55bc2024-10-28 21:00:30 +00001119 std::unique_ptr<InputSurface> bgContainer =
1120 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001121 std::unique_ptr<InputSurface> parentContainer =
1122 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1123 std::unique_ptr<InputSurface> containerSurface =
1124 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1125 containerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001126 [&](auto& t, auto& sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001127 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1128 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
Prabir Pradhan56d55bc2024-10-28 21:00:30 +00001129 parentContainer->doTransaction(
1130 [&](auto& t, auto& sc) { t.reparent(sc, bgContainer->mSurfaceControl); });
1131 bgContainer->showAt(0, 0, Rect(0, 0, 100, 100));
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001132 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1133 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1134
1135 // Receives events inside parent bounds
1136 injectTap(21, 21);
1137 containerSurface->expectTap(1, 1); // Event is in layer space
1138
1139 // Does not receive events outside parent bounds
1140 injectTap(31, 31);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001141 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001142}
1143
1144/**
1145 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1146 * layer's bounds. The input events should be in the layer's coordinate space.
1147 */
1148TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1149 std::unique_ptr<InputSurface> cropLayer =
1150 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1151 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1152
1153 std::unique_ptr<InputSurface> containerSurface =
1154 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1155 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1156 containerSurface->mInputInfo.touchableRegionCropHandle =
1157 cropLayer->mSurfaceControl->getHandle();
1158 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1159
1160 // Receives events inside crop layer bounds
1161 injectTap(51, 51);
1162 containerSurface->expectTap(41, 41); // Event is in layer space
1163
1164 // Does not receive events outside crop layer bounds
1165 injectTap(21, 21);
1166 injectTap(71, 71);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001167 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001168}
1169
Linus Tufvessona1858822022-03-04 09:32:07 +00001170TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1171 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1172
1173 parent->showAt(100, 100);
1174 injectTap(101, 101);
1175 parent->expectTap(1, 1);
1176
1177 std::unique_ptr<InputSurface> childContainerSurface =
1178 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1179 childContainerSurface->showAt(0, 0);
1180 childContainerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001181 [&](auto& t, auto& sc) { t.reparent(sc, parent->mSurfaceControl); });
Linus Tufvessona1858822022-03-04 09:32:07 +00001182 injectTap(101, 101);
1183
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001184 parent->assertNoEvent();
Linus Tufvessona1858822022-03-04 09:32:07 +00001185}
1186
Vishnu Nair16a938f2021-09-24 07:14:54 -07001187class MultiDisplayTests : public InputSurfacesTest {
1188public:
1189 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Linnan Li13bf76a2024-05-05 19:18:02 +08001190
Prabir Pradhand0aba782021-12-14 00:44:21 -08001191 void TearDown() override {
Alan Dingd53801c2024-05-08 16:45:29 -07001192 std::for_each(mVirtualDisplays.begin(), mVirtualDisplays.end(),
1193 SurfaceComposerClient::destroyVirtualDisplay);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001194 InputSurfacesTest::TearDown();
1195 }
1196
Prabir Pradhand0aba782021-12-14 00:44:21 -08001197 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1198 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001199 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001200 sp<IGraphicBufferProducer> producer;
1201 BufferQueue::createBufferQueue(&producer, &consumer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001202 consumer->setConsumerName(String8("Virtual disp consumer"));
1203 consumer->setDefaultBufferSize(width, height);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001204 mProducers.push_back(producer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001205
Prabir Pradhand0aba782021-12-14 00:44:21 -08001206 std::string name = "VirtualDisplay";
1207 name += std::to_string(mVirtualDisplays.size());
Alan Dingd53801c2024-05-08 16:45:29 -07001208 sp<IBinder> token = SurfaceComposerClient::createVirtualDisplay(name, isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001209 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001210 t.setDisplaySurface(token, producer);
1211 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1212 t.setDisplayLayerStack(token, layerStack);
1213 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1214 {offsetX, offsetY, offsetX + width, offsetY + height});
Prabir Pradhana97fd942024-10-28 21:21:53 +00001215 t.apply(/*synchronously=*/true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001216
1217 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001218 }
1219
Prabir Pradhand0aba782021-12-14 00:44:21 -08001220 std::vector<sp<IBinder>> mVirtualDisplays;
1221 std::vector<sp<IGraphicBufferProducer>> mProducers;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001222};
1223
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001224TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001225 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1226 // Do not create a display associated with the LayerStack.
1227 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001228 surface->doTransaction([&](auto& t, auto& sc) { t.setLayerStack(sc, layerStack); });
Prabir Pradhand0aba782021-12-14 00:44:21 -08001229 surface->showAt(100, 100);
1230
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001231 // Touches should be dropped if the layer is on an invalid display.
Linnan Li13bf76a2024-05-05 19:18:02 +08001232 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001233 surface->assertNoEvent();
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001234
1235 // However, we still let the window be focused and receive keys.
Linnan Li13bf76a2024-05-05 19:18:02 +08001236 surface->requestFocus(toDisplayId(layerStack));
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001237 surface->assertFocusChange(true);
1238
Linnan Li13bf76a2024-05-05 19:18:02 +08001239 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001240 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001241}
1242
1243TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1244 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1245 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1246 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001247 surface->doTransaction([&](auto& t, auto& sc) { t.setLayerStack(sc, layerStack); });
Prabir Pradhand0aba782021-12-14 00:44:21 -08001248 surface->showAt(100, 100);
1249
Linnan Li13bf76a2024-05-05 19:18:02 +08001250 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001251 surface->expectTap(1, 1);
1252
Linnan Li13bf76a2024-05-05 19:18:02 +08001253 surface->requestFocus(toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001254 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001255 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001256 surface->expectKey(AKEYCODE_V);
1257}
1258
Vishnu Nair16a938f2021-09-24 07:14:54 -07001259TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1260 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1261 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1262 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001263 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001264 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1265 t.setLayerStack(sc, layerStack);
1266 });
1267 surface->showAt(100, 100);
1268
Linnan Li13bf76a2024-05-05 19:18:02 +08001269 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001270
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001271 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001272
Linnan Li13bf76a2024-05-05 19:18:02 +08001273 surface->requestFocus(toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001274 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001275 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001276 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001277}
1278
1279TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1280 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001281
1282 // Create the secure display as system, because only certain users can create secure displays.
1283 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001284 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001285 // Change the uid back to root.
1286 seteuid(AID_ROOT);
1287
Vishnu Nair16a938f2021-09-24 07:14:54 -07001288 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001289 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001290 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1291 t.setLayerStack(sc, layerStack);
1292 });
1293 surface->showAt(100, 100);
1294
Linnan Li13bf76a2024-05-05 19:18:02 +08001295 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001296 surface->expectTap(1, 1);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001297
Linnan Li13bf76a2024-05-05 19:18:02 +08001298 surface->requestFocus(toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001299 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001300 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001301
1302 surface->expectKey(AKEYCODE_V);
1303}
1304
Linnan Li13bf76a2024-05-05 19:18:02 +08001305} // namespace test
1306} // namespace android