blob: 2f58a6cc8f4eca264e10059d1dfa6a0b5d7d1420 [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
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;
303 request.token = mInputInfo.token;
304 request.windowName = mInputInfo.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
313 WindowInfo mInputInfo;
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
526 fgSurface->mInputInfo.surfaceInset = 5;
527 fgSurface->showAt(100, 100);
528
529 injectTap(106, 106);
530 fgSurface->expectTap(1, 1);
531
532 injectTap(101, 101);
533 bgSurface->expectTap(1, 1);
534}
535
Vishnu Nairfed7c122023-03-18 01:54:43 +0000536TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) {
537 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
538 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
539 bgSurface->showAt(100, 100);
540
541 fgSurface->mInputInfo.surfaceInset = 5;
542 fgSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
543 fgSurface->showAt(100, 100);
544
545 injectTap(106, 106);
546 fgSurface->expectTap(1, 1);
547
548 injectTap(101, 101);
549 bgSurface->expectTap(1, 1);
550}
551
Vishnu Nairde19f852018-12-18 16:11:53 -0800552// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
553TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
554 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
555 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
556 parentSurface->showAt(100, 100);
557
558 childSurface->mInputInfo.surfaceInset = 10;
559 childSurface->showAt(100, 100);
560
Linnan Li13bf76a2024-05-05 19:18:02 +0800561 childSurface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800562 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000563 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800564 });
565
566 injectTap(106, 106);
567 childSurface->expectTap(1, 1);
568
569 injectTap(101, 101);
570 parentSurface->expectTap(1, 1);
571}
572
Arthur Hung118b1142019-05-08 21:25:59 +0800573// Ensure a surface whose insets are scaled, handles the touch offset correctly.
574TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
575 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
576 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
577 bgSurface->showAt(100, 100);
578
579 fgSurface->mInputInfo.surfaceInset = 5;
580 fgSurface->showAt(100, 100);
581
Linnan Li13bf76a2024-05-05 19:18:02 +0800582 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
Arthur Hung118b1142019-05-08 21:25:59 +0800583
584 // expect = touch / scale - inset
585 injectTap(112, 124);
586 fgSurface->expectTap(1, 1);
587
588 injectTap(101, 101);
589 bgSurface->expectTap(1, 1);
590}
591
Ady Abraham282f1d72019-07-24 18:05:56 -0700592TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700593 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
Ady Abraham282f1d72019-07-24 18:05:56 -0700594 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700595 bgSurface->showAt(100, 100);
596
Ady Abraham282f1d72019-07-24 18:05:56 -0700597 // In case we pass the very big inset without any checking.
598 fgSurface->mInputInfo.surfaceInset = INT32_MAX;
599 fgSurface->showAt(100, 100);
600
Linnan Li13bf76a2024-05-05 19:18:02 +0800601 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
Ady Abraham282f1d72019-07-24 18:05:56 -0700602
603 // expect no crash for overflow, and inset size to be clamped to surface size
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700604 injectTap(112, 124);
605 bgSurface->expectTap(12, 24);
Ady Abraham282f1d72019-07-24 18:05:56 -0700606}
607
Prabir Pradhan33da9462022-06-14 14:55:57 +0000608TEST_F(InputSurfacesTest, touchable_region) {
609 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
610
611 surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31});
612
613 surface->showAt(11, 22);
614
615 // A tap within the surface but outside the touchable region should not be sent to the surface.
616 injectTap(20, 30);
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.
632 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
633
634 fgSurface->showAt(100, 100);
635
636 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
637 // surface receives touch.
638 injectTap(112, 124);
639 bgSurface->expectTap(12, 24);
640}
641
642TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
643 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
644 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
645 bgSurface->showAt(0, 0);
646
647 fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
648 fgSurface->showAt(0, 0);
649
Linnan Li13bf76a2024-05-05 19:18:02 +0800650 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
Prabir Pradhan33da9462022-06-14 14:55:57 +0000651
652 // Expect no crash for overflow.
653 injectTap(12, 24);
Prabir Pradhana4c59bd2023-04-10 20:54:04 +0000654 bgSurface->expectTap(12, 24);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000655}
656
Vishnu Nairde19f852018-12-18 16:11:53 -0800657// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
658TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
659 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +0800660 surface->doTransaction([](auto& t, auto& sc) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800661 Region transparentRegion(Rect(0, 0, 10, 10));
662 t.setTransparentRegionHint(sc, transparentRegion);
663 });
664 surface->showAt(100, 100);
665 injectTap(101, 101);
666 surface->expectTap(1, 1);
667}
668
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700669// TODO(b/139494112) update tests once we define expected behavior
670// Ensure we still send input to the surface regardless of surface visibility changes due to the
671// first buffer being submitted or alpha changes.
672// Original bug ref: b/120839715
673TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800674 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500675 std::unique_ptr<BlastInputSurface> bufferSurface =
676 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800677
678 bgSurface->showAt(10, 10);
679 bufferSurface->showAt(10, 10);
680
681 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700682 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800683
chaviw39d01472021-04-08 14:26:24 -0500684 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800685 injectTap(11, 11);
686 bufferSurface->expectTap(1, 1);
687}
688
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000689TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800690 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500691 std::unique_ptr<BlastInputSurface> bufferSurface =
692 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
693 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800694
695 bgSurface->showAt(10, 10);
696 bufferSurface->showAt(10, 10);
697
698 injectTap(11, 11);
699 bufferSurface->expectTap(1, 1);
700
Linnan Li13bf76a2024-05-05 19:18:02 +0800701 bufferSurface->doTransaction([](auto& t, auto& sc) { t.setAlpha(sc, 0.0); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800702
703 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000704 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800705}
706
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700707TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800708 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
709 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
710
711 bgSurface->showAt(10, 10);
712 fgSurface->showAt(10, 10);
713
714 injectTap(11, 11);
715 fgSurface->expectTap(1, 1);
716
Linnan Li13bf76a2024-05-05 19:18:02 +0800717 fgSurface->doTransaction([](auto& t, auto& sc) { t.setAlpha(sc, 0.0); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800718
719 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700720 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800721}
722
723TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
724 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
725 std::unique_ptr<InputSurface> containerSurface =
726 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
727
728 bgSurface->showAt(10, 10);
729 containerSurface->showAt(10, 10);
730
731 injectTap(11, 11);
732 containerSurface->expectTap(1, 1);
733
Linnan Li13bf76a2024-05-05 19:18:02 +0800734 containerSurface->doTransaction([](auto& t, auto& sc) { t.hide(sc); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800735
736 injectTap(11, 11);
737 bgSurface->expectTap(1, 1);
738}
chaviwfbe5d9c2018-12-26 12:23:37 -0800739
Arthur Hungd20b2702019-01-14 18:16:16 +0800740TEST_F(InputSurfacesTest, input_respects_outscreen) {
741 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
742 surface->showAt(-1, -1);
743
744 injectTap(0, 0);
745 surface->expectTap(1, 1);
746}
arthurhungb4a0f852020-06-16 11:02:50 +0800747
748TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
749 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
750 std::unique_ptr<InputSurface> cursorSurface =
751 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
752
753 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800754 cursorSurface->showAt(10, 10);
755
756 injectTap(11, 11);
757 surface->expectTap(1, 1);
758}
Vishnu Nair958da932020-08-21 17:12:37 -0700759
760TEST_F(InputSurfacesTest, can_be_focused) {
761 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
762 surface->showAt(100, 100);
763 surface->requestFocus();
764
765 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700766
767 injectKey(AKEYCODE_V);
768 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700769}
chaviw44a6d2b2020-09-08 17:14:16 -0700770
771TEST_F(InputSurfacesTest, rotate_surface) {
772 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
773 surface->showAt(10, 10);
Linnan Li13bf76a2024-05-05 19:18:02 +0800774 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700775 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
776 });
777 injectTap(8, 11);
778 surface->expectTap(1, 2);
779
Linnan Li13bf76a2024-05-05 19:18:02 +0800780 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700781 t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees
782 });
783 injectTap(9, 8);
784 surface->expectTap(1, 2);
785
Linnan Li13bf76a2024-05-05 19:18:02 +0800786 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700787 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
788 });
789 injectTap(12, 9);
790 surface->expectTap(1, 2);
791}
792
793TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
794 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
795 surface->showAt(10, 10);
Linnan Li13bf76a2024-05-05 19:18:02 +0800796 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700797 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
798 });
799 injectTap(2, 12);
800 surface->expectTap(1, 2);
801
Linnan Li13bf76a2024-05-05 19:18:02 +0800802 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700803 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
804 });
805 injectTap(8, 2);
806 surface->expectTap(1, 2);
807
Linnan Li13bf76a2024-05-05 19:18:02 +0800808 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700809 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
810 });
811 injectTap(18, 8);
812 surface->expectTap(1, 2);
813}
814
815TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
816 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
817 surface->mInputInfo.surfaceInset = 5;
818 surface->showAt(100, 100);
819
Linnan Li13bf76a2024-05-05 19:18:02 +0800820 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700821 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
822 });
823 injectTap(40, 120);
824 surface->expectTap(5, 10);
825
Linnan Li13bf76a2024-05-05 19:18:02 +0800826 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700827 t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees
828 });
829 injectTap(80, 40);
830 surface->expectTap(5, 10);
831
Linnan Li13bf76a2024-05-05 19:18:02 +0800832 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700833 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
834 });
835 injectTap(160, 80);
836 surface->expectTap(5, 10);
837}
838
chaviw39cfa2e2020-11-04 14:19:02 -0800839TEST_F(InputSurfacesTest, touch_flag_obscured) {
840 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
841 surface->showAt(100, 100);
842
843 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
844 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
845 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800846 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000847 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
Bernardo Rufino602ef712020-12-21 11:02:18 +0000848 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
849 // the default obscured/untrusted touch filter introduced in S.
850 nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800851 nonTouchableSurface->showAt(100, 100);
852
853 injectTap(190, 199);
854 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
855}
856
857TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
858 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
859 surface->showAt(100, 100);
860
861 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
862 // that will be tapped. Window behind gets touch, but with flag
863 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
864 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
865 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800866 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
867 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000868 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
869 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800870 nonTouchableSurface->showAt(0, 0);
871 parentSurface->showAt(100, 100);
872
Linnan Li13bf76a2024-05-05 19:18:02 +0800873 nonTouchableSurface->doTransaction([&](auto& t, auto& sc) {
chaviw25714502021-02-11 10:01:08 -0800874 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800875 t.reparent(sc, parentSurface->mSurfaceControl);
876 });
877
878 injectTap(190, 199);
879 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
880}
881
882TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
883 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
884 surface->showAt(100, 100);
885
886 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
887 // the touchable window. Window behind gets touch with no obscured flags.
888 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
889 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800890 nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
891 parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000892 nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222};
893 parentSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800894 nonTouchableSurface->showAt(0, 0);
895 parentSurface->showAt(50, 50);
896
Linnan Li13bf76a2024-05-05 19:18:02 +0800897 nonTouchableSurface->doTransaction([&](auto& t, auto& sc) {
chaviw25714502021-02-11 10:01:08 -0800898 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800899 t.reparent(sc, parentSurface->mSurfaceControl);
900 });
901
902 injectTap(101, 110);
903 surface->expectTap(1, 10);
904}
905
chaviw7e72caf2020-12-02 16:50:43 -0800906TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
907 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
908
909 std::unique_ptr<InputSurface> bufferSurface =
910 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800911 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000912 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800913
914 surface->showAt(10, 10);
915 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
916
917 injectTap(11, 11);
918 surface->expectTap(1, 1);
919}
920
921TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
922 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
923
chaviw39d01472021-04-08 14:26:24 -0500924 std::unique_ptr<BlastInputSurface> bufferSurface =
925 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800926 bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000927 bufferSurface->mInputInfo.ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800928
929 surface->showAt(10, 10);
930 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
931
932 injectTap(11, 11);
933 surface->expectTap(1, 1);
934}
935
Vishnu Naira066d902021-09-13 18:40:17 -0700936TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
937 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
938 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800939 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -0700940 surface->showAt(100, 100);
941
942 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700943 surface->expectTap(1, 1);
Vishnu Naira066d902021-09-13 18:40:17 -0700944
945 surface->requestFocus();
946 surface->assertFocusChange(true);
947 injectKey(AKEYCODE_V);
948 surface->expectKey(AKEYCODE_V);
949}
950
951TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
952 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +0800953 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -0700954 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
955 t.setMatrix(sc, 2.0, 0, 0, 2.0);
956 });
957 surface->showAt(100, 100);
958
959 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700960 surface->expectTap(.5, .5);
Vishnu Naira066d902021-09-13 18:40:17 -0700961
962 surface->requestFocus();
963 surface->assertFocusChange(true);
964 injectKey(AKEYCODE_V);
965 surface->expectKey(AKEYCODE_V);
966}
967
968TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
969 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000970 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700971 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800972 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -0700973 surface->showAt(100, 100);
974 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800975 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000976 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700977 obscuringSurface->showAt(100, 100);
978 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700979 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700980
981 surface->requestFocus();
982 surface->assertFocusChange(true);
983 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700984 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700985}
986
987TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
988 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000989 surface->mInputInfo.ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700990 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800991 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -0700992 surface->showAt(100, 100);
993 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800994 obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000995 obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700996 obscuringSurface->showAt(190, 190);
997
998 injectTap(101, 101);
999
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001000 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001001
1002 surface->requestFocus();
1003 surface->assertFocusChange(true);
1004 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001005 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001006}
1007
1008TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
1009 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1010 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1011
1012 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1013 surface->showAt(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001014 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -07001015 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1016 t.reparent(sc, parentSurface->mSurfaceControl);
1017 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
1018 });
1019
1020 injectTap(101, 101);
1021
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001022 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001023
1024 surface->requestFocus();
1025 surface->assertFocusChange(true);
1026 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001027 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001028}
1029
1030TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
1031 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1032 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1033
1034 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001035 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -07001036 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1037 t.reparent(sc, parentSurface->mSurfaceControl);
1038 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1039 });
1040 surface->showAt(100, 100);
1041
1042 injectTap(111, 111);
1043
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001044 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001045
1046 surface->requestFocus();
1047 surface->assertFocusChange(true);
1048 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001049 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001050}
1051
Arthur Hung49d525a2021-11-19 15:11:51 +00001052TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1053 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1054
1055 std::unique_ptr<BlastInputSurface> bufferSurface =
1056 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1057
1058 surface->showAt(100, 100);
1059 bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200));
1060 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1061
1062 injectTap(101, 101);
1063 surface->expectTap(1, 1);
1064}
1065
Vishnu Naira066d902021-09-13 18:40:17 -07001066TEST_F(InputSurfacesTest, drop_input_policy) {
1067 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1068 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001069 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
Vishnu Naira066d902021-09-13 18:40:17 -07001070 surface->showAt(100, 100);
1071
1072 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001073 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001074
1075 surface->requestFocus();
1076 surface->assertFocusChange(true);
1077 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001078 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001079}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001080
Prabir Pradhan8c285982022-01-28 09:19:39 -08001081TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1082 std::unique_ptr<InputSurface> bufferSurface =
1083 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1084
1085 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1086
1087 bufferSurface->requestFocus();
1088 bufferSurface->assertFocusChange(true);
1089}
1090
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001091/**
1092 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1093 * its own crop.
1094 */
1095TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1096 std::unique_ptr<InputSurface> parentContainer =
1097 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1098 std::unique_ptr<InputSurface> containerSurface =
1099 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1100 containerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001101 [&](auto& t, auto& sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001102 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1103 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
1104 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1105 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1106
1107 // Receives events inside its own crop
1108 injectTap(21, 21);
1109 containerSurface->expectTap(1, 1); // Event is in layer space
1110
1111 // Does not receive events outside its crop
1112 injectTap(26, 26);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001113 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001114}
1115
1116/**
1117 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1118 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1119 */
1120TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
Prabir Pradhan56d55bc2024-10-28 21:00:30 +00001121 std::unique_ptr<InputSurface> bgContainer =
1122 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001123 std::unique_ptr<InputSurface> parentContainer =
1124 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1125 std::unique_ptr<InputSurface> containerSurface =
1126 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1127 containerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001128 [&](auto& t, auto& sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001129 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1130 containerSurface->mInputInfo.touchableRegionCropHandle = nullptr;
Prabir Pradhan56d55bc2024-10-28 21:00:30 +00001131 parentContainer->doTransaction(
1132 [&](auto& t, auto& sc) { t.reparent(sc, bgContainer->mSurfaceControl); });
1133 bgContainer->showAt(0, 0, Rect(0, 0, 100, 100));
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001134 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1135 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1136
1137 // Receives events inside parent bounds
1138 injectTap(21, 21);
1139 containerSurface->expectTap(1, 1); // Event is in layer space
1140
1141 // Does not receive events outside parent bounds
1142 injectTap(31, 31);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001143 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001144}
1145
1146/**
1147 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1148 * layer's bounds. The input events should be in the layer's coordinate space.
1149 */
1150TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1151 std::unique_ptr<InputSurface> cropLayer =
1152 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1153 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1154
1155 std::unique_ptr<InputSurface> containerSurface =
1156 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1157 containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true;
1158 containerSurface->mInputInfo.touchableRegionCropHandle =
1159 cropLayer->mSurfaceControl->getHandle();
1160 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1161
1162 // Receives events inside crop layer bounds
1163 injectTap(51, 51);
1164 containerSurface->expectTap(41, 41); // Event is in layer space
1165
1166 // Does not receive events outside crop layer bounds
1167 injectTap(21, 21);
1168 injectTap(71, 71);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001169 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001170}
1171
Linus Tufvessona1858822022-03-04 09:32:07 +00001172TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1173 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1174
1175 parent->showAt(100, 100);
1176 injectTap(101, 101);
1177 parent->expectTap(1, 1);
1178
1179 std::unique_ptr<InputSurface> childContainerSurface =
1180 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1181 childContainerSurface->showAt(0, 0);
1182 childContainerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001183 [&](auto& t, auto& sc) { t.reparent(sc, parent->mSurfaceControl); });
Linus Tufvessona1858822022-03-04 09:32:07 +00001184 injectTap(101, 101);
1185
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001186 parent->assertNoEvent();
Linus Tufvessona1858822022-03-04 09:32:07 +00001187}
1188
Vishnu Nair16a938f2021-09-24 07:14:54 -07001189class MultiDisplayTests : public InputSurfacesTest {
1190public:
1191 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Linnan Li13bf76a2024-05-05 19:18:02 +08001192
Prabir Pradhand0aba782021-12-14 00:44:21 -08001193 void TearDown() override {
Alan Dingd53801c2024-05-08 16:45:29 -07001194 std::for_each(mVirtualDisplays.begin(), mVirtualDisplays.end(),
1195 SurfaceComposerClient::destroyVirtualDisplay);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001196 InputSurfacesTest::TearDown();
1197 }
1198
Prabir Pradhand0aba782021-12-14 00:44:21 -08001199 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1200 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001201 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001202 sp<IGraphicBufferProducer> producer;
1203 BufferQueue::createBufferQueue(&producer, &consumer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001204 consumer->setConsumerName(String8("Virtual disp consumer"));
1205 consumer->setDefaultBufferSize(width, height);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001206 mProducers.push_back(producer);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001207
Prabir Pradhand0aba782021-12-14 00:44:21 -08001208 std::string name = "VirtualDisplay";
1209 name += std::to_string(mVirtualDisplays.size());
Alan Dingd53801c2024-05-08 16:45:29 -07001210 sp<IBinder> token = SurfaceComposerClient::createVirtualDisplay(name, isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001211 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001212 t.setDisplaySurface(token, producer);
1213 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1214 t.setDisplayLayerStack(token, layerStack);
1215 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1216 {offsetX, offsetY, offsetX + width, offsetY + height});
Prabir Pradhana97fd942024-10-28 21:21:53 +00001217 t.apply(/*synchronously=*/true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001218
1219 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001220 }
1221
Prabir Pradhand0aba782021-12-14 00:44:21 -08001222 std::vector<sp<IBinder>> mVirtualDisplays;
1223 std::vector<sp<IGraphicBufferProducer>> mProducers;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001224};
1225
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001226TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001227 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1228 // Do not create a display associated with the LayerStack.
1229 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001230 surface->doTransaction([&](auto& t, auto& sc) { t.setLayerStack(sc, layerStack); });
Prabir Pradhand0aba782021-12-14 00:44:21 -08001231 surface->showAt(100, 100);
1232
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001233 // Touches should be dropped if the layer is on an invalid display.
Linnan Li13bf76a2024-05-05 19:18:02 +08001234 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001235 surface->assertNoEvent();
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001236
1237 // However, we still let the window be focused and receive keys.
Linnan Li13bf76a2024-05-05 19:18:02 +08001238 surface->requestFocus(toDisplayId(layerStack));
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001239 surface->assertFocusChange(true);
1240
Linnan Li13bf76a2024-05-05 19:18:02 +08001241 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001242 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001243}
1244
1245TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1246 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1247 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1248 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001249 surface->doTransaction([&](auto& t, auto& sc) { t.setLayerStack(sc, layerStack); });
Prabir Pradhand0aba782021-12-14 00:44:21 -08001250 surface->showAt(100, 100);
1251
Linnan Li13bf76a2024-05-05 19:18:02 +08001252 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001253 surface->expectTap(1, 1);
1254
Linnan Li13bf76a2024-05-05 19:18:02 +08001255 surface->requestFocus(toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001256 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001257 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001258 surface->expectKey(AKEYCODE_V);
1259}
1260
Vishnu Nair16a938f2021-09-24 07:14:54 -07001261TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1262 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1263 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1264 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001265 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001266 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1267 t.setLayerStack(sc, layerStack);
1268 });
1269 surface->showAt(100, 100);
1270
Linnan Li13bf76a2024-05-05 19:18:02 +08001271 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001272
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001273 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001274
Linnan Li13bf76a2024-05-05 19:18:02 +08001275 surface->requestFocus(toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001276 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001277 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001278 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001279}
1280
1281TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1282 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001283
1284 // Create the secure display as system, because only certain users can create secure displays.
1285 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001286 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001287 // Change the uid back to root.
1288 seteuid(AID_ROOT);
1289
Vishnu Nair16a938f2021-09-24 07:14:54 -07001290 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001291 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001292 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1293 t.setLayerStack(sc, layerStack);
1294 });
1295 surface->showAt(100, 100);
1296
Linnan Li13bf76a2024-05-05 19:18:02 +08001297 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001298 surface->expectTap(1, 1);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001299
Linnan Li13bf76a2024-05-05 19:18:02 +08001300 surface->requestFocus(toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001301 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001302 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001303
1304 surface->expectKey(AKEYCODE_V);
1305}
1306
Linnan Li13bf76a2024-05-05 19:18:02 +08001307} // namespace test
1308} // namespace android