blob: c77a30a5ffabd8b6f324df663eb9dbd237e28cec [file] [log] [blame]
Robert Carr1c4c5592018-09-24 13:18:43 -07001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <sys/time.h>
21#include <sys/types.h>
22#include <stdio.h>
23#include <poll.h>
24
25#include <memory>
26
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070027#include <android-base/thread_annotations.h>
Patrick Williams0a4981a2023-07-28 15:08:52 -050028#include <android/gui/BnWindowInfosReportedListener.h>
Vishnu Naira066d902021-09-13 18:40:17 -070029#include <android/keycodes.h>
Vishnu Nairde19f852018-12-18 16:11:53 -080030#include <android/native_window.h>
31
Robert Carr1c4c5592018-09-24 13:18:43 -070032#include <binder/Binder.h>
33#include <binder/IServiceManager.h>
34#include <binder/Parcel.h>
35#include <binder/ProcessState.h>
36
Jim Shargof48bef82024-12-04 18:56:59 +000037#include <gui/IConsumerListener.h>
38#include <gui/IGraphicBufferConsumer.h>
Vishnu Nairde19f852018-12-18 16:11:53 -080039#include <gui/ISurfaceComposer.h>
40#include <gui/Surface.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070041#include <gui/SurfaceComposerClient.h>
42#include <gui/SurfaceControl.h>
43
Chris Ye0783e992020-06-02 21:34:49 -070044#include <android/os/IInputFlinger.h>
chaviw98318de2021-05-19 16:45:23 -050045#include <gui/WindowInfo.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070046#include <input/Input.h>
Siarhei Vishniakou0438ca82024-03-12 14:27:25 -070047#include <input/InputConsumer.h>
Chris Ye0783e992020-06-02 21:34:49 -070048#include <input/InputTransport.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070049
Marin Shalamanova7fe3042021-01-29 21:02:08 +010050#include <ui/DisplayMode.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070051#include <ui/Rect.h>
52#include <ui/Region.h>
53
Prabir Pradhand0aba782021-12-14 00:44:21 -080054#include <private/android_filesystem_config.h>
55
Chris Ye0783e992020-06-02 21:34:49 -070056using android::os::IInputFlinger;
Robert Carr1c4c5592018-09-24 13:18:43 -070057
chaviw39d01472021-04-08 14:26:24 -050058using android::hardware::graphics::common::V1_1::BufferUsage;
59
chaviw98318de2021-05-19 16:45:23 -050060using android::gui::FocusRequest;
61using android::gui::InputApplicationInfo;
62using android::gui::TouchOcclusionMode;
63using android::gui::WindowInfo;
64
Linnan Li13bf76a2024-05-05 19:18:02 +080065namespace android {
66namespace {
67ui::LogicalDisplayId toDisplayId(ui::LayerStack layerStack) {
68 return ui::LogicalDisplayId{static_cast<int32_t>(layerStack.id)};
69}
70} // namespace
71namespace test {
Robert Carr1c4c5592018-09-24 13:18:43 -070072
Vishnu Nairde19f852018-12-18 16:11:53 -080073using Transaction = SurfaceComposerClient::Transaction;
74
Robert Carr1c4c5592018-09-24 13:18:43 -070075sp<IInputFlinger> getInputFlinger() {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -080076 sp<IBinder> input(defaultServiceManager()->waitForService(String16("inputflinger")));
Robert Carr1c4c5592018-09-24 13:18:43 -070077 if (input == nullptr) {
78 ALOGE("Failed to link to input service");
Linnan Li13bf76a2024-05-05 19:18:02 +080079 } else {
80 ALOGE("Linked to input");
81 }
Robert Carr1c4c5592018-09-24 13:18:43 -070082 return interface_cast<IInputFlinger>(input);
83}
84
85// We use the top 10 layers as a way to haphazardly place ourselves above anything else.
86static const int LAYER_BASE = INT32_MAX - 10;
Chris Ye6c4243b2020-07-22 12:07:12 -070087static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s;
Robert Carr1c4c5592018-09-24 13:18:43 -070088
Patrick Williams0a4981a2023-07-28 15:08:52 -050089class SynchronousWindowInfosReportedListener : public gui::BnWindowInfosReportedListener {
90public:
91 binder::Status onWindowInfosReported() override {
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070092 std::scoped_lock lock{mLock};
Patrick Williams0a4981a2023-07-28 15:08:52 -050093 mWindowInfosReported = true;
94 mConditionVariable.notify_one();
95 return binder::Status::ok();
96 }
97
98 void wait() {
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -070099 std::unique_lock lock{mLock};
100 android::base::ScopedLockAssertion assumeLocked(mLock);
101 mConditionVariable.wait(lock, [&]() REQUIRES(mLock) { return mWindowInfosReported; });
Patrick Williams0a4981a2023-07-28 15:08:52 -0500102 }
103
104private:
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -0700105 std::mutex mLock;
Patrick Williams0a4981a2023-07-28 15:08:52 -0500106 std::condition_variable mConditionVariable;
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -0700107 bool mWindowInfosReported GUARDED_BY(mLock){false};
Patrick Williams0a4981a2023-07-28 15:08:52 -0500108};
109
Robert Carr1c4c5592018-09-24 13:18:43 -0700110class InputSurface {
111public:
Linnan Li13bf76a2024-05-05 19:18:02 +0800112 InputSurface(const sp<SurfaceControl>& sc, int width, int height, bool noInputChannel = false) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800113 mSurfaceControl = sc;
Robert Carr1c4c5592018-09-24 13:18:43 -0700114
115 mInputFlinger = getInputFlinger();
Linus Tufvessona1858822022-03-04 09:32:07 +0000116 if (noInputChannel) {
Patrick Williams87602162024-11-05 10:13:19 -0600117 mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, true);
Linus Tufvessona1858822022-03-04 09:32:07 +0000118 } else {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800119 android::os::InputChannelCore tempChannel;
120 android::binder::Status result =
121 mInputFlinger->createInputChannel("testchannels", &tempChannel);
122 if (!result.isOk()) {
123 ADD_FAILURE() << "binder call to createInputChannel failed";
124 }
125 mClientChannel = InputChannel::create(std::move(tempChannel));
Patrick Williams87602162024-11-05 10:13:19 -0600126 mInputInfo->editInfo()->token = mClientChannel->getConnectionToken();
Linus Tufvessona1858822022-03-04 09:32:07 +0000127 mInputConsumer = new InputConsumer(mClientChannel);
128 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700129
Patrick Williams87602162024-11-05 10:13:19 -0600130 mInputInfo->editInfo()->name = "Test info";
131 mInputInfo->editInfo()->dispatchingTimeout = 5s;
132 mInputInfo->editInfo()->globalScaleFactor = 1.0;
133 mInputInfo->editInfo()->touchableRegion.orSelf(Rect(0, 0, width, height));
Robert Carr1c4c5592018-09-24 13:18:43 -0700134
Linus Tufvessona1858822022-03-04 09:32:07 +0000135 InputApplicationInfo aInfo;
136 aInfo.token = new BBinder();
137 aInfo.name = "Test app info";
138 aInfo.dispatchingTimeoutMillis =
139 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Patrick Williams87602162024-11-05 10:13:19 -0600140 mInputInfo->editInfo()->applicationInfo = aInfo;
Robert Carr1c4c5592018-09-24 13:18:43 -0700141 }
142
Linnan Li13bf76a2024-05-05 19:18:02 +0800143 static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient>& scc,
Vishnu Nairde19f852018-12-18 16:11:53 -0800144 int width, int height) {
145 sp<SurfaceControl> surfaceControl =
146 scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */,
Vishnu Nairfa247b12020-02-11 08:58:26 -0800147 PIXEL_FORMAT_RGBA_8888,
148 ISurfaceComposerClient::eFXSurfaceEffect);
Vishnu Nairde19f852018-12-18 16:11:53 -0800149 return std::make_unique<InputSurface>(surfaceControl, width, height);
150 }
151
152 static std::unique_ptr<InputSurface> makeBufferInputSurface(
Linnan Li13bf76a2024-05-05 19:18:02 +0800153 const sp<SurfaceComposerClient>& scc, int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800154 sp<SurfaceControl> surfaceControl =
155 scc->createSurface(String8("Test Buffer Surface"), width, height,
156 PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
157 return std::make_unique<InputSurface>(surfaceControl, width, height);
158 }
159
160 static std::unique_ptr<InputSurface> makeContainerInputSurface(
Linnan Li13bf76a2024-05-05 19:18:02 +0800161 const sp<SurfaceComposerClient>& scc, int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800162 sp<SurfaceControl> surfaceControl =
163 scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */,
164 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
165 ISurfaceComposerClient::eFXSurfaceContainer);
166 return std::make_unique<InputSurface>(surfaceControl, width, height);
167 }
168
Linus Tufvessona1858822022-03-04 09:32:07 +0000169 static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel(
Linnan Li13bf76a2024-05-05 19:18:02 +0800170 const sp<SurfaceComposerClient>& scc, int width, int height) {
Linus Tufvessona1858822022-03-04 09:32:07 +0000171 sp<SurfaceControl> surfaceControl =
172 scc->createSurface(String8("Test Container Surface"), 100 /* height */,
173 100 /* width */, PIXEL_FORMAT_RGBA_8888,
174 ISurfaceComposerClient::eFXSurfaceContainer);
175 return std::make_unique<InputSurface>(surfaceControl, width, height,
176 true /* noInputChannel */);
177 }
178
arthurhungb4a0f852020-06-16 11:02:50 +0800179 static std::unique_ptr<InputSurface> makeCursorInputSurface(
Linnan Li13bf76a2024-05-05 19:18:02 +0800180 const sp<SurfaceComposerClient>& scc, int width, int height) {
arthurhungb4a0f852020-06-16 11:02:50 +0800181 sp<SurfaceControl> surfaceControl =
182 scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */,
183 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
184 ISurfaceComposerClient::eCursorWindow);
185 return std::make_unique<InputSurface>(surfaceControl, width, height);
186 }
187
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100188 void assertFocusChange(bool hasFocus) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800189 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100190 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700191 ASSERT_EQ(InputEventType::FOCUS, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800192 FocusEvent* focusEvent = static_cast<FocusEvent*>(ev);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100193 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
194 }
195
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700196 void expectTap(float x, float y) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700197 InputEvent* ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100198 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700199 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700200 MotionEvent* mev = static_cast<MotionEvent*>(ev);
201 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
202 EXPECT_EQ(x, mev->getX(0));
203 EXPECT_EQ(y, mev->getY(0));
arthurhungb4a0f852020-06-16 11:02:50 +0800204 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700205
206 ev = consumeEvent();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100207 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700208 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Robert Carr1c4c5592018-09-24 13:18:43 -0700209 mev = static_cast<MotionEvent*>(ev);
210 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
arthurhungb4a0f852020-06-16 11:02:50 +0800211 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
Robert Carr1c4c5592018-09-24 13:18:43 -0700212 }
213
chaviw39cfa2e2020-11-04 14:19:02 -0800214 void expectTapWithFlag(int x, int y, int32_t flags) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800215 InputEvent* ev = consumeEvent();
chaviw39cfa2e2020-11-04 14:19:02 -0800216 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700217 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800218 MotionEvent* mev = static_cast<MotionEvent*>(ev);
chaviw39cfa2e2020-11-04 14:19:02 -0800219 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
220 EXPECT_EQ(x, mev->getX(0));
221 EXPECT_EQ(y, mev->getY(0));
222 EXPECT_EQ(flags, mev->getFlags() & flags);
223
224 ev = consumeEvent();
225 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700226 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800227 mev = static_cast<MotionEvent*>(ev);
chaviw39cfa2e2020-11-04 14:19:02 -0800228 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
229 EXPECT_EQ(flags, mev->getFlags() & flags);
230 }
231
Prabir Pradhand0aba782021-12-14 00:44:21 -0800232 void expectTapInDisplayCoordinates(int displayX, int displayY) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800233 InputEvent* ev = consumeEvent();
Prabir Pradhand0aba782021-12-14 00:44:21 -0800234 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700235 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800236 MotionEvent* mev = static_cast<MotionEvent*>(ev);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800237 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction());
Linnan Li13bf76a2024-05-05 19:18:02 +0800238 const PointerCoords& coords = *mev->getRawPointerCoords(0 /*pointerIndex*/);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800239 EXPECT_EQ(displayX, coords.getX());
240 EXPECT_EQ(displayY, coords.getY());
241 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
242
243 ev = consumeEvent();
244 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700245 ASSERT_EQ(InputEventType::MOTION, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800246 mev = static_cast<MotionEvent*>(ev);
Prabir Pradhand0aba782021-12-14 00:44:21 -0800247 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction());
248 EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS);
249 }
250
Siarhei Vishniakoubf98a572024-03-29 13:34:42 -0700251 void expectKey(int32_t keycode) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800252 InputEvent* ev = consumeEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700253 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700254 ASSERT_EQ(InputEventType::KEY, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800255 KeyEvent* keyEvent = static_cast<KeyEvent*>(ev);
Vishnu Naira066d902021-09-13 18:40:17 -0700256 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction());
257 EXPECT_EQ(keycode, keyEvent->getKeyCode());
258 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
259
260 ev = consumeEvent();
261 ASSERT_NE(ev, nullptr);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700262 ASSERT_EQ(InputEventType::KEY, ev->getType());
Linnan Li13bf76a2024-05-05 19:18:02 +0800263 keyEvent = static_cast<KeyEvent*>(ev);
Vishnu Naira066d902021-09-13 18:40:17 -0700264 EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction());
265 EXPECT_EQ(keycode, keyEvent->getKeyCode());
266 EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS);
267 }
268
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700269 void assertNoEvent() {
270 InputEvent* event = consumeEvent(/*timeout=*/100ms);
271 ASSERT_EQ(event, nullptr) << "Expected no event, but got " << *event;
272 }
273
chaviw39d01472021-04-08 14:26:24 -0500274 virtual ~InputSurface() {
Linus Tufvessona1858822022-03-04 09:32:07 +0000275 if (mClientChannel) {
276 mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken());
277 }
chaviw39d01472021-04-08 14:26:24 -0500278 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700279
chaviw39d01472021-04-08 14:26:24 -0500280 virtual void doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800281 std::function<void(SurfaceComposerClient::Transaction&, const sp<SurfaceControl>&)>
chaviw39d01472021-04-08 14:26:24 -0500282 transactionBody) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700283 SurfaceComposerClient::Transaction t;
284 transactionBody(t, mSurfaceControl);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000285 t.apply(/*synchronously=*/true);
Robert Carr1c4c5592018-09-24 13:18:43 -0700286 }
287
chaviw39d01472021-04-08 14:26:24 -0500288 virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700289 SurfaceComposerClient::Transaction t;
290 t.show(mSurfaceControl);
291 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
292 t.setLayer(mSurfaceControl, LAYER_BASE);
293 t.setPosition(mSurfaceControl, x, y);
chaviw25714502021-02-11 10:01:08 -0800294 t.setCrop(mSurfaceControl, crop);
Robert Carr1c4c5592018-09-24 13:18:43 -0700295 t.setAlpha(mSurfaceControl, 1);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500296 auto reportedListener = sp<SynchronousWindowInfosReportedListener>::make();
297 t.addWindowInfosReportedListener(reportedListener);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000298 t.apply(/*synchronously=*/true);
Patrick Williams0a4981a2023-07-28 15:08:52 -0500299 reportedListener->wait();
Robert Carr1c4c5592018-09-24 13:18:43 -0700300 }
301
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700302 void requestFocus(ui::LogicalDisplayId displayId = ui::LogicalDisplayId::DEFAULT) {
Vishnu Nair958da932020-08-21 17:12:37 -0700303 SurfaceComposerClient::Transaction t;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800304 FocusRequest request;
Patrick Williams87602162024-11-05 10:13:19 -0600305 request.token = mInputInfo->getInfo()->token;
306 request.windowName = mInputInfo->getInfo()->name;
Vishnu Nair9ad01462021-01-15 12:55:14 -0800307 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Linnan Li13bf76a2024-05-05 19:18:02 +0800308 request.displayId = displayId.val();
Vishnu Nair9ad01462021-01-15 12:55:14 -0800309 t.setFocusedWindow(request);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000310 t.apply(/*synchronously=*/true);
Vishnu Nair958da932020-08-21 17:12:37 -0700311 }
312
Robert Carr1c4c5592018-09-24 13:18:43 -0700313public:
Siarhei Vishniakou1d038d42024-10-29 11:25:58 -0700314 // But should be private
Patrick Williams87602162024-11-05 10:13:19 -0600315 sp<gui::WindowInfoHandle> mInputInfo = sp<gui::WindowInfoHandle>::make();
Robert Carr1c4c5592018-09-24 13:18:43 -0700316 sp<SurfaceControl> mSurfaceControl;
Siarhei Vishniakou1d038d42024-10-29 11:25:58 -0700317
318private:
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500319 std::shared_ptr<InputChannel> mClientChannel;
Robert Carr1c4c5592018-09-24 13:18:43 -0700320 sp<IInputFlinger> mInputFlinger;
321
Robert Carr1c4c5592018-09-24 13:18:43 -0700322 PreallocatedInputEventFactory mInputEventFactory;
323 InputConsumer* mInputConsumer;
Siarhei Vishniakou1d038d42024-10-29 11:25:58 -0700324
325 InputEvent* consumeEvent(std::chrono::milliseconds timeout = 3000ms) {
326 mClientChannel->waitForMessage(timeout);
327
328 InputEvent* ev;
329 uint32_t seqId;
330 status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev);
331 if (consumed != OK) {
332 return nullptr;
333 }
334 status_t status = mInputConsumer->sendFinishedSignal(seqId, true);
335 EXPECT_EQ(OK, status) << "Could not send finished signal";
336 return ev;
337 }
Robert Carr1c4c5592018-09-24 13:18:43 -0700338};
339
chaviw39d01472021-04-08 14:26:24 -0500340class BlastInputSurface : public InputSurface {
341public:
Linnan Li13bf76a2024-05-05 19:18:02 +0800342 BlastInputSurface(const sp<SurfaceControl>& sc, const sp<SurfaceControl>& parentSc, int width,
chaviw39d01472021-04-08 14:26:24 -0500343 int height)
344 : InputSurface(sc, width, height) {
345 mParentSurfaceControl = parentSc;
346 }
347
348 ~BlastInputSurface() = default;
349
350 static std::unique_ptr<BlastInputSurface> makeBlastInputSurface(
Linnan Li13bf76a2024-05-05 19:18:02 +0800351 const sp<SurfaceComposerClient>& scc, int width, int height) {
chaviw39d01472021-04-08 14:26:24 -0500352 sp<SurfaceControl> parentSc =
353 scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */,
354 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888,
355 ISurfaceComposerClient::eFXSurfaceContainer);
356
357 sp<SurfaceControl> surfaceControl =
358 scc->createSurface(String8("Test Buffer Surface"), width, height,
359 PIXEL_FORMAT_RGBA_8888,
360 ISurfaceComposerClient::eFXSurfaceBufferState,
361 parentSc->getHandle());
362 return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height);
363 }
364
365 void doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800366 std::function<void(SurfaceComposerClient::Transaction&, const sp<SurfaceControl>&)>
chaviw39d01472021-04-08 14:26:24 -0500367 transactionBody) override {
368 SurfaceComposerClient::Transaction t;
369 transactionBody(t, mParentSurfaceControl);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000370 t.apply(/*synchronously=*/true);
chaviw39d01472021-04-08 14:26:24 -0500371 }
372
373 void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override {
374 SurfaceComposerClient::Transaction t;
375 t.show(mParentSurfaceControl);
376 t.setLayer(mParentSurfaceControl, LAYER_BASE);
377 t.setPosition(mParentSurfaceControl, x, y);
378 t.setCrop(mParentSurfaceControl, crop);
379
380 t.show(mSurfaceControl);
381 t.setInputWindowInfo(mSurfaceControl, mInputInfo);
382 t.setCrop(mSurfaceControl, crop);
383 t.setAlpha(mSurfaceControl, 1);
Prabir Pradhana97fd942024-10-28 21:21:53 +0000384 t.apply(/*synchronously=*/true);
chaviw39d01472021-04-08 14:26:24 -0500385 }
386
387private:
388 sp<SurfaceControl> mParentSurfaceControl;
389};
390
Robert Carr1c4c5592018-09-24 13:18:43 -0700391class InputSurfacesTest : public ::testing::Test {
392public:
Linnan Li13bf76a2024-05-05 19:18:02 +0800393 InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Robert Carr1c4c5592018-09-24 13:18:43 -0700394
395 void SetUp() {
396 mComposerClient = new SurfaceComposerClient;
397 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
Huihong Luo31b5ac22022-08-15 20:38:10 -0700398 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
399 ASSERT_FALSE(ids.empty());
400 // display 0 is picked for now, can extend to support all displays if needed
401 const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100402 ASSERT_NE(display, nullptr);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800403
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100404 ui::DisplayMode mode;
405 ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode));
Vishnu Nairde19f852018-12-18 16:11:53 -0800406
407 // After a new buffer is queued, SurfaceFlinger is notified and will
408 // latch the new buffer on next vsync. Let's heuristically wait for 3
409 // vsyncs.
Alec Mouri55e31032023-10-02 20:34:18 +0000410 mBufferPostDelay = static_cast<int32_t>(1e6 / mode.peakRefreshRate) * 3;
Robert Carr1c4c5592018-09-24 13:18:43 -0700411 }
412
Linnan Li13bf76a2024-05-05 19:18:02 +0800413 void TearDown() { mComposerClient->dispose(); }
Robert Carr1c4c5592018-09-24 13:18:43 -0700414
415 std::unique_ptr<InputSurface> makeSurface(int width, int height) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800416 return InputSurface::makeColorInputSurface(mComposerClient, width, height);
417 }
418
Linnan Li13bf76a2024-05-05 19:18:02 +0800419 void postBuffer(const sp<SurfaceControl>& layer, int32_t w, int32_t h) {
chaviw39d01472021-04-08 14:26:24 -0500420 int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
421 BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
422 sp<GraphicBuffer> buffer =
423 new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test");
Prabir Pradhana97fd942024-10-28 21:21:53 +0000424 Transaction().setBuffer(layer, buffer).apply(/*synchronously=*/true);
Vishnu Nairde19f852018-12-18 16:11:53 -0800425 usleep(mBufferPostDelay);
Robert Carr1c4c5592018-09-24 13:18:43 -0700426 }
427
428 sp<SurfaceComposerClient> mComposerClient;
Vishnu Nairde19f852018-12-18 16:11:53 -0800429 int32_t mBufferPostDelay;
Robert Carr1c4c5592018-09-24 13:18:43 -0700430};
431
Linnan Li13bf76a2024-05-05 19:18:02 +0800432void injectTapOnDisplay(int x, int y, ui::LogicalDisplayId displayId) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700433 char *buf1, *buf2, *bufDisplayId;
Robert Carr1c4c5592018-09-24 13:18:43 -0700434 asprintf(&buf1, "%d", x);
435 asprintf(&buf2, "%d", y);
Linnan Li13bf76a2024-05-05 19:18:02 +0800436 asprintf(&bufDisplayId, "%d", displayId.val());
Robert Carr1c4c5592018-09-24 13:18:43 -0700437 if (fork() == 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700438 execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL);
439 }
440}
441
442void injectTap(int x, int y) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700443 injectTapOnDisplay(x, y, ui::LogicalDisplayId::DEFAULT);
Vishnu Nair16a938f2021-09-24 07:14:54 -0700444}
445
Linnan Li13bf76a2024-05-05 19:18:02 +0800446void injectKeyOnDisplay(uint32_t keycode, ui::LogicalDisplayId displayId) {
Vishnu Nair16a938f2021-09-24 07:14:54 -0700447 char *buf1, *bufDisplayId;
448 asprintf(&buf1, "%d", keycode);
Linnan Li13bf76a2024-05-05 19:18:02 +0800449 asprintf(&bufDisplayId, "%d", displayId.val());
Vishnu Nair16a938f2021-09-24 07:14:54 -0700450 if (fork() == 0) {
451 execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL);
Robert Carr1c4c5592018-09-24 13:18:43 -0700452 }
453}
454
Vishnu Naira066d902021-09-13 18:40:17 -0700455void injectKey(uint32_t keycode) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700456 injectKeyOnDisplay(keycode, ui::LogicalDisplayId::INVALID);
Vishnu Naira066d902021-09-13 18:40:17 -0700457}
458
Robert Carr1c4c5592018-09-24 13:18:43 -0700459TEST_F(InputSurfacesTest, can_receive_input) {
460 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
461 surface->showAt(100, 100);
462
463 injectTap(101, 101);
464
Siarhei Vishniakou1d038d42024-10-29 11:25:58 -0700465 surface->expectTap(1, 1);
Robert Carr1c4c5592018-09-24 13:18:43 -0700466}
467
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100468/**
469 * Set up two surfaces side-by-side. Tap each surface.
470 * Next, swap the positions of the two surfaces. Inject tap into the two
471 * original locations. Ensure that the tap is received by the surfaces in the
472 * reverse order.
473 */
Robert Carr1c4c5592018-09-24 13:18:43 -0700474TEST_F(InputSurfacesTest, input_respects_positioning) {
475 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
476 surface->showAt(100, 100);
477
478 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
479 surface2->showAt(200, 200);
480
481 injectTap(201, 201);
482 surface2->expectTap(1, 1);
483
484 injectTap(101, 101);
485 surface->expectTap(1, 1);
486
Linnan Li13bf76a2024-05-05 19:18:02 +0800487 surface2->doTransaction([](auto& t, auto& sc) { t.setPosition(sc, 100, 100); });
488 surface->doTransaction([](auto& t, auto& sc) { t.setPosition(sc, 200, 200); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700489
490 injectTap(101, 101);
491 surface2->expectTap(1, 1);
492
493 injectTap(201, 201);
494 surface->expectTap(1, 1);
495}
496
497TEST_F(InputSurfacesTest, input_respects_layering) {
498 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
499 std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100);
500
501 surface->showAt(10, 10);
502 surface2->showAt(10, 10);
503
Linnan Li13bf76a2024-05-05 19:18:02 +0800504 surface->doTransaction([](auto& t, auto& sc) { t.setLayer(sc, LAYER_BASE + 1); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700505
506 injectTap(11, 11);
507 surface->expectTap(1, 1);
508
Linnan Li13bf76a2024-05-05 19:18:02 +0800509 surface2->doTransaction([](auto& t, auto& sc) { t.setLayer(sc, LAYER_BASE + 1); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700510
511 injectTap(11, 11);
512 surface2->expectTap(1, 1);
513
Linnan Li13bf76a2024-05-05 19:18:02 +0800514 surface2->doTransaction([](auto& t, auto& sc) { t.hide(sc); });
Robert Carr1c4c5592018-09-24 13:18:43 -0700515
516 injectTap(11, 11);
517 surface->expectTap(1, 1);
518}
519
Vishnu Nairde19f852018-12-18 16:11:53 -0800520// Surface Insets are set to offset the client content and draw a border around the client surface
521// (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start
522// of the client content.
523TEST_F(InputSurfacesTest, input_respects_surface_insets) {
524 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
525 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
526 bgSurface->showAt(100, 100);
527
Patrick Williams87602162024-11-05 10:13:19 -0600528 fgSurface->mInputInfo->editInfo()->surfaceInset = 5;
Vishnu Nairde19f852018-12-18 16:11:53 -0800529 fgSurface->showAt(100, 100);
530
531 injectTap(106, 106);
532 fgSurface->expectTap(1, 1);
533
534 injectTap(101, 101);
535 bgSurface->expectTap(1, 1);
536}
537
Vishnu Nairfed7c122023-03-18 01:54:43 +0000538TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) {
539 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
540 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
541 bgSurface->showAt(100, 100);
542
Patrick Williams87602162024-11-05 10:13:19 -0600543 fgSurface->mInputInfo->editInfo()->surfaceInset = 5;
544 fgSurface->mInputInfo->editInfo()->replaceTouchableRegionWithCrop = true;
Vishnu Nairfed7c122023-03-18 01:54:43 +0000545 fgSurface->showAt(100, 100);
546
547 injectTap(106, 106);
548 fgSurface->expectTap(1, 1);
549
550 injectTap(101, 101);
551 bgSurface->expectTap(1, 1);
552}
553
Vishnu Nairde19f852018-12-18 16:11:53 -0800554// Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463
555TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) {
556 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
557 std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100);
558 parentSurface->showAt(100, 100);
559
Patrick Williams87602162024-11-05 10:13:19 -0600560 childSurface->mInputInfo->editInfo()->surfaceInset = 10;
Vishnu Nairde19f852018-12-18 16:11:53 -0800561 childSurface->showAt(100, 100);
562
Linnan Li13bf76a2024-05-05 19:18:02 +0800563 childSurface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800564 t.setPosition(sc, -5, -5);
Pablo Gamito11dcc222020-09-12 15:49:39 +0000565 t.reparent(sc, parentSurface->mSurfaceControl);
Vishnu Nairde19f852018-12-18 16:11:53 -0800566 });
567
568 injectTap(106, 106);
569 childSurface->expectTap(1, 1);
570
571 injectTap(101, 101);
572 parentSurface->expectTap(1, 1);
573}
574
Arthur Hung118b1142019-05-08 21:25:59 +0800575// Ensure a surface whose insets are scaled, handles the touch offset correctly.
576TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) {
577 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
578 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
579 bgSurface->showAt(100, 100);
580
Patrick Williams87602162024-11-05 10:13:19 -0600581 fgSurface->mInputInfo->editInfo()->surfaceInset = 5;
Arthur Hung118b1142019-05-08 21:25:59 +0800582 fgSurface->showAt(100, 100);
583
Linnan Li13bf76a2024-05-05 19:18:02 +0800584 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); });
Arthur Hung118b1142019-05-08 21:25:59 +0800585
586 // expect = touch / scale - inset
587 injectTap(112, 124);
588 fgSurface->expectTap(1, 1);
589
590 injectTap(101, 101);
591 bgSurface->expectTap(1, 1);
592}
593
Ady Abraham282f1d72019-07-24 18:05:56 -0700594TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) {
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700595 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
Ady Abraham282f1d72019-07-24 18:05:56 -0700596 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700597 bgSurface->showAt(100, 100);
598
Ady Abraham282f1d72019-07-24 18:05:56 -0700599 // In case we pass the very big inset without any checking.
Patrick Williams87602162024-11-05 10:13:19 -0600600 fgSurface->mInputInfo->editInfo()->surfaceInset = INT32_MAX;
Ady Abraham282f1d72019-07-24 18:05:56 -0700601 fgSurface->showAt(100, 100);
602
Linnan Li13bf76a2024-05-05 19:18:02 +0800603 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
Ady Abraham282f1d72019-07-24 18:05:56 -0700604
605 // expect no crash for overflow, and inset size to be clamped to surface size
Prabir Pradhanc9589c12021-09-22 06:11:43 -0700606 injectTap(112, 124);
607 bgSurface->expectTap(12, 24);
Ady Abraham282f1d72019-07-24 18:05:56 -0700608}
609
Prabir Pradhan33da9462022-06-14 14:55:57 +0000610TEST_F(InputSurfacesTest, touchable_region) {
611 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
612
Patrick Williams87602162024-11-05 10:13:19 -0600613 surface->mInputInfo->editInfo()->touchableRegion.set(Rect{19, 29, 21, 31});
Prabir Pradhan33da9462022-06-14 14:55:57 +0000614
615 surface->showAt(11, 22);
616
617 // A tap within the surface but outside the touchable region should not be sent to the surface.
618 injectTap(20, 30);
Siarhei Vishniakou1d038d42024-10-29 11:25:58 -0700619 surface->assertNoEvent();
Prabir Pradhan33da9462022-06-14 14:55:57 +0000620
621 injectTap(31, 52);
622 surface->expectTap(20, 30);
623}
624
625TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) {
626 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
627 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
628 bgSurface->showAt(100, 100);
629
630 // Set the touchable region to the values at the limit of its corresponding type.
631 // Since the surface is offset from the origin, the touchable region will be transformed into
632 // display space, which would trigger an overflow or an underflow. Ensure that we are protected
633 // against such a situation.
Patrick Williams87602162024-11-05 10:13:19 -0600634 fgSurface->mInputInfo->editInfo()->touchableRegion.orSelf(
635 Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
Prabir Pradhan33da9462022-06-14 14:55:57 +0000636
637 fgSurface->showAt(100, 100);
638
639 // Expect no crash for overflow. The overflowed touchable region is ignored, so the background
640 // surface receives touch.
641 injectTap(112, 124);
642 bgSurface->expectTap(12, 24);
643}
644
645TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) {
646 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
647 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
648 bgSurface->showAt(0, 0);
649
Patrick Williams87602162024-11-05 10:13:19 -0600650 fgSurface->mInputInfo->editInfo()->touchableRegion.orSelf(
651 Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX});
Prabir Pradhan33da9462022-06-14 14:55:57 +0000652 fgSurface->showAt(0, 0);
653
Linnan Li13bf76a2024-05-05 19:18:02 +0800654 fgSurface->doTransaction([&](auto& t, auto& sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); });
Prabir Pradhan33da9462022-06-14 14:55:57 +0000655
656 // Expect no crash for overflow.
657 injectTap(12, 24);
Prabir Pradhana4c59bd2023-04-10 20:54:04 +0000658 bgSurface->expectTap(12, 24);
Prabir Pradhan33da9462022-06-14 14:55:57 +0000659}
660
Vishnu Nairde19f852018-12-18 16:11:53 -0800661// Ensure we ignore transparent region when getting screen bounds when positioning input frame.
662TEST_F(InputSurfacesTest, input_ignores_transparent_region) {
663 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +0800664 surface->doTransaction([](auto& t, auto& sc) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800665 Region transparentRegion(Rect(0, 0, 10, 10));
666 t.setTransparentRegionHint(sc, transparentRegion);
667 });
668 surface->showAt(100, 100);
669 injectTap(101, 101);
670 surface->expectTap(1, 1);
671}
672
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700673// TODO(b/139494112) update tests once we define expected behavior
674// Ensure we still send input to the surface regardless of surface visibility changes due to the
675// first buffer being submitted or alpha changes.
676// Original bug ref: b/120839715
677TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800678 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500679 std::unique_ptr<BlastInputSurface> bufferSurface =
680 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800681
682 bgSurface->showAt(10, 10);
683 bufferSurface->showAt(10, 10);
684
685 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700686 bufferSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800687
chaviw39d01472021-04-08 14:26:24 -0500688 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800689 injectTap(11, 11);
690 bufferSurface->expectTap(1, 1);
691}
692
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000693TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800694 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
chaviw39d01472021-04-08 14:26:24 -0500695 std::unique_ptr<BlastInputSurface> bufferSurface =
696 BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100);
697 postBuffer(bufferSurface->mSurfaceControl, 100, 100);
Vishnu Nairde19f852018-12-18 16:11:53 -0800698
699 bgSurface->showAt(10, 10);
700 bufferSurface->showAt(10, 10);
701
702 injectTap(11, 11);
703 bufferSurface->expectTap(1, 1);
704
Linnan Li13bf76a2024-05-05 19:18:02 +0800705 bufferSurface->doTransaction([](auto& t, auto& sc) { t.setAlpha(sc, 0.0); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800706
707 injectTap(11, 11);
Arthur Hungfb2ebce2021-10-04 14:08:48 +0000708 bgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800709}
710
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700711TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) {
Vishnu Nairde19f852018-12-18 16:11:53 -0800712 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
713 std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100);
714
715 bgSurface->showAt(10, 10);
716 fgSurface->showAt(10, 10);
717
718 injectTap(11, 11);
719 fgSurface->expectTap(1, 1);
720
Linnan Li13bf76a2024-05-05 19:18:02 +0800721 fgSurface->doTransaction([](auto& t, auto& sc) { t.setAlpha(sc, 0.0); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800722
723 injectTap(11, 11);
Vishnu Nairf8678ba2019-10-11 18:11:26 -0700724 fgSurface->expectTap(1, 1);
Vishnu Nairde19f852018-12-18 16:11:53 -0800725}
726
727TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) {
728 std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100);
729 std::unique_ptr<InputSurface> containerSurface =
730 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
731
732 bgSurface->showAt(10, 10);
733 containerSurface->showAt(10, 10);
734
735 injectTap(11, 11);
736 containerSurface->expectTap(1, 1);
737
Linnan Li13bf76a2024-05-05 19:18:02 +0800738 containerSurface->doTransaction([](auto& t, auto& sc) { t.hide(sc); });
Vishnu Nairde19f852018-12-18 16:11:53 -0800739
740 injectTap(11, 11);
741 bgSurface->expectTap(1, 1);
742}
chaviwfbe5d9c2018-12-26 12:23:37 -0800743
Arthur Hungd20b2702019-01-14 18:16:16 +0800744TEST_F(InputSurfacesTest, input_respects_outscreen) {
745 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
746 surface->showAt(-1, -1);
747
748 injectTap(0, 0);
749 surface->expectTap(1, 1);
750}
arthurhungb4a0f852020-06-16 11:02:50 +0800751
752TEST_F(InputSurfacesTest, input_ignores_cursor_layer) {
753 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
754 std::unique_ptr<InputSurface> cursorSurface =
755 InputSurface::makeCursorInputSurface(mComposerClient, 10, 10);
756
757 surface->showAt(10, 10);
arthurhungb4a0f852020-06-16 11:02:50 +0800758 cursorSurface->showAt(10, 10);
759
760 injectTap(11, 11);
761 surface->expectTap(1, 1);
762}
Vishnu Nair958da932020-08-21 17:12:37 -0700763
764TEST_F(InputSurfacesTest, can_be_focused) {
765 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
766 surface->showAt(100, 100);
767 surface->requestFocus();
768
769 surface->assertFocusChange(true);
Vishnu Naira066d902021-09-13 18:40:17 -0700770
771 injectKey(AKEYCODE_V);
772 surface->expectKey(AKEYCODE_V);
Robert Carr1c4c5592018-09-24 13:18:43 -0700773}
chaviw44a6d2b2020-09-08 17:14:16 -0700774
775TEST_F(InputSurfacesTest, rotate_surface) {
776 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
777 surface->showAt(10, 10);
Linnan Li13bf76a2024-05-05 19:18:02 +0800778 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700779 t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees
780 });
781 injectTap(8, 11);
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, -1, 0, 0, -1); // 180 degrees
786 });
787 injectTap(9, 8);
788 surface->expectTap(1, 2);
789
Linnan Li13bf76a2024-05-05 19:18:02 +0800790 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700791 t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees
792 });
793 injectTap(12, 9);
794 surface->expectTap(1, 2);
795}
796
797TEST_F(InputSurfacesTest, rotate_surface_with_scale) {
798 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
799 surface->showAt(10, 10);
Linnan Li13bf76a2024-05-05 19:18:02 +0800800 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700801 t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees
802 });
803 injectTap(2, 12);
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, -2, 0, 0, -4); // 180 degrees
808 });
809 injectTap(8, 2);
810 surface->expectTap(1, 2);
811
Linnan Li13bf76a2024-05-05 19:18:02 +0800812 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700813 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
814 });
815 injectTap(18, 8);
816 surface->expectTap(1, 2);
817}
818
819TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) {
820 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -0600821 surface->mInputInfo->editInfo()->surfaceInset = 5;
chaviw44a6d2b2020-09-08 17:14:16 -0700822 surface->showAt(100, 100);
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, 0, 2, -4, 0); // 90 degrees
826 });
827 injectTap(40, 120);
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, -2, 0, 0, -4); // 180 degrees
832 });
833 injectTap(80, 40);
834 surface->expectTap(5, 10);
835
Linnan Li13bf76a2024-05-05 19:18:02 +0800836 surface->doTransaction([](auto& t, auto& sc) {
chaviw44a6d2b2020-09-08 17:14:16 -0700837 t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees
838 });
839 injectTap(160, 80);
840 surface->expectTap(5, 10);
841}
842
chaviw39cfa2e2020-11-04 14:19:02 -0800843TEST_F(InputSurfacesTest, touch_flag_obscured) {
844 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
845 surface->showAt(100, 100);
846
847 // Add non touchable window to fully cover touchable window. Window behind gets touch, but
848 // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED
849 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -0600850 nonTouchableSurface->mInputInfo->editInfo()
851 ->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
852 nonTouchableSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
Bernardo Rufino602ef712020-12-21 11:02:18 +0000853 // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by
854 // the default obscured/untrusted touch filter introduced in S.
Patrick Williams87602162024-11-05 10:13:19 -0600855 nonTouchableSurface->mInputInfo->editInfo()->touchOcclusionMode = TouchOcclusionMode::ALLOW;
chaviw39cfa2e2020-11-04 14:19:02 -0800856 nonTouchableSurface->showAt(100, 100);
857
858 injectTap(190, 199);
859 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED);
860}
861
862TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) {
863 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
864 surface->showAt(100, 100);
865
866 // Add non touchable window to cover touchable window, but parent is cropped to not cover area
867 // that will be tapped. Window behind gets touch, but with flag
868 // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED
869 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
870 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -0600871 nonTouchableSurface->mInputInfo->editInfo()
872 ->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
873 parentSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
874 true);
875 nonTouchableSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
876 parentSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800877 nonTouchableSurface->showAt(0, 0);
878 parentSurface->showAt(100, 100);
879
Linnan Li13bf76a2024-05-05 19:18:02 +0800880 nonTouchableSurface->doTransaction([&](auto& t, auto& sc) {
chaviw25714502021-02-11 10:01:08 -0800881 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800882 t.reparent(sc, parentSurface->mSurfaceControl);
883 });
884
885 injectTap(190, 199);
886 surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
887}
888
889TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) {
890 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
891 surface->showAt(100, 100);
892
893 // Add non touchable window to cover touchable window, but parent is cropped to avoid covering
894 // the touchable window. Window behind gets touch with no obscured flags.
895 std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100);
896 std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -0600897 nonTouchableSurface->mInputInfo->editInfo()
898 ->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true);
899 parentSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
900 true);
901 nonTouchableSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
902 parentSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
chaviw39cfa2e2020-11-04 14:19:02 -0800903 nonTouchableSurface->showAt(0, 0);
904 parentSurface->showAt(50, 50);
905
Linnan Li13bf76a2024-05-05 19:18:02 +0800906 nonTouchableSurface->doTransaction([&](auto& t, auto& sc) {
chaviw25714502021-02-11 10:01:08 -0800907 t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50));
chaviw39cfa2e2020-11-04 14:19:02 -0800908 t.reparent(sc, parentSurface->mSurfaceControl);
909 });
910
911 injectTap(101, 110);
912 surface->expectTap(1, 10);
913}
914
chaviw7e72caf2020-12-02 16:50:43 -0800915TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) {
916 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
917
918 std::unique_ptr<InputSurface> bufferSurface =
919 InputSurface::makeBufferInputSurface(mComposerClient, 0, 0);
Patrick Williams87602162024-11-05 10:13:19 -0600920 bufferSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
921 true);
922 bufferSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800923
924 surface->showAt(10, 10);
925 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
926
927 injectTap(11, 11);
928 surface->expectTap(1, 1);
929}
930
931TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) {
932 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
933
chaviw39d01472021-04-08 14:26:24 -0500934 std::unique_ptr<BlastInputSurface> bufferSurface =
935 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
Patrick Williams87602162024-11-05 10:13:19 -0600936 bufferSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
937 true);
938 bufferSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
chaviw7e72caf2020-12-02 16:50:43 -0800939
940 surface->showAt(10, 10);
941 bufferSurface->showAt(50, 50, Rect::EMPTY_RECT);
942
943 injectTap(11, 11);
944 surface->expectTap(1, 1);
945}
946
Vishnu Naira066d902021-09-13 18:40:17 -0700947TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) {
948 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
949 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800950 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -0700951 surface->showAt(100, 100);
952
953 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700954 surface->expectTap(1, 1);
Vishnu Naira066d902021-09-13 18:40:17 -0700955
956 surface->requestFocus();
957 surface->assertFocusChange(true);
958 injectKey(AKEYCODE_V);
959 surface->expectKey(AKEYCODE_V);
960}
961
962TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) {
963 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +0800964 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -0700965 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
966 t.setMatrix(sc, 2.0, 0, 0, 2.0);
967 });
968 surface->showAt(100, 100);
969
970 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700971 surface->expectTap(.5, .5);
Vishnu Naira066d902021-09-13 18:40:17 -0700972
973 surface->requestFocus();
974 surface->assertFocusChange(true);
975 injectKey(AKEYCODE_V);
976 surface->expectKey(AKEYCODE_V);
977}
978
979TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) {
980 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -0600981 surface->mInputInfo->editInfo()->ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -0700982 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +0800983 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -0700984 surface->showAt(100, 100);
985 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -0600986 obscuringSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
987 true);
988 obscuringSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -0700989 obscuringSurface->showAt(100, 100);
990 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700991 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700992
993 surface->requestFocus();
994 surface->assertFocusChange(true);
995 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -0700996 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -0700997}
998
999TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) {
1000 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -06001001 surface->mInputInfo->editInfo()->ownerUid = gui::Uid{11111};
Vishnu Naira066d902021-09-13 18:40:17 -07001002 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001003 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); });
Vishnu Naira066d902021-09-13 18:40:17 -07001004 surface->showAt(100, 100);
1005 std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -06001006 obscuringSurface->mInputInfo->editInfo()->setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE,
1007 true);
1008 obscuringSurface->mInputInfo->editInfo()->ownerUid = gui::Uid{22222};
Vishnu Naira066d902021-09-13 18:40:17 -07001009 obscuringSurface->showAt(190, 190);
1010
1011 injectTap(101, 101);
1012
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001013 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001014
1015 surface->requestFocus();
1016 surface->assertFocusChange(true);
1017 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001018 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001019}
1020
1021TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) {
1022 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1023 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1024
1025 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1026 surface->showAt(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001027 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -07001028 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1029 t.reparent(sc, parentSurface->mSurfaceControl);
1030 t.setAlpha(parentSurface->mSurfaceControl, 0.9f);
1031 });
1032
1033 injectTap(101, 101);
1034
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001035 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001036
1037 surface->requestFocus();
1038 surface->assertFocusChange(true);
1039 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001040 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001041}
1042
1043TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) {
1044 std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300);
1045 parentSurface->showAt(0, 0, Rect(0, 0, 300, 300));
1046
1047 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001048 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Naira066d902021-09-13 18:40:17 -07001049 t.setDropInputMode(sc, gui::DropInputMode::OBSCURED);
1050 t.reparent(sc, parentSurface->mSurfaceControl);
1051 t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100));
1052 });
1053 surface->showAt(100, 100);
1054
1055 injectTap(111, 111);
1056
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001057 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001058
1059 surface->requestFocus();
1060 surface->assertFocusChange(true);
1061 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001062 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001063}
1064
Arthur Hung49d525a2021-11-19 15:11:51 +00001065TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) {
1066 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1067
1068 std::unique_ptr<BlastInputSurface> bufferSurface =
1069 BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0);
1070
1071 surface->showAt(100, 100);
Patrick Williams87602162024-11-05 10:13:19 -06001072 bufferSurface->mInputInfo->editInfo()->touchableRegion.orSelf(Rect(0, 0, 200, 200));
Arthur Hung49d525a2021-11-19 15:11:51 +00001073 bufferSurface->showAt(100, 100, Rect::EMPTY_RECT);
1074
1075 injectTap(101, 101);
1076 surface->expectTap(1, 1);
1077}
1078
Vishnu Naira066d902021-09-13 18:40:17 -07001079TEST_F(InputSurfacesTest, drop_input_policy) {
1080 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
1081 surface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001082 [&](auto& t, auto& sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); });
Vishnu Naira066d902021-09-13 18:40:17 -07001083 surface->showAt(100, 100);
1084
1085 injectTap(101, 101);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001086 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001087
1088 surface->requestFocus();
1089 surface->assertFocusChange(true);
1090 injectKey(AKEYCODE_V);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001091 surface->assertNoEvent();
Vishnu Naira066d902021-09-13 18:40:17 -07001092}
Vishnu Nair16a938f2021-09-24 07:14:54 -07001093
Prabir Pradhan8c285982022-01-28 09:19:39 -08001094TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) {
1095 std::unique_ptr<InputSurface> bufferSurface =
1096 InputSurface::makeBufferInputSurface(mComposerClient, 100, 100);
1097
1098 bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100});
1099
1100 bufferSurface->requestFocus();
1101 bufferSurface->assertFocusChange(true);
1102}
1103
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001104/**
1105 * If a cropped layer's touchable region is replaced with a null crop, it should receive input in
1106 * its own crop.
1107 */
1108TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) {
1109 std::unique_ptr<InputSurface> parentContainer =
1110 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1111 std::unique_ptr<InputSurface> containerSurface =
1112 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1113 containerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001114 [&](auto& t, auto& sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
Patrick Williams87602162024-11-05 10:13:19 -06001115 containerSurface->mInputInfo->editInfo()->replaceTouchableRegionWithCrop = true;
1116 containerSurface->mInputInfo->editInfo()->touchableRegionCropHandle = nullptr;
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001117 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1118 containerSurface->showAt(10, 10, Rect(0, 0, 5, 5));
1119
1120 // Receives events inside its own crop
1121 injectTap(21, 21);
1122 containerSurface->expectTap(1, 1); // Event is in layer space
1123
1124 // Does not receive events outside its crop
1125 injectTap(26, 26);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001126 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001127}
1128
1129/**
1130 * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input
1131 * in its parent's touchable region. The input events should be in the layer's coordinate space.
1132 */
1133TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) {
Prabir Pradhan56d55bc2024-10-28 21:00:30 +00001134 std::unique_ptr<InputSurface> bgContainer =
1135 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001136 std::unique_ptr<InputSurface> parentContainer =
1137 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1138 std::unique_ptr<InputSurface> containerSurface =
1139 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
1140 containerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001141 [&](auto& t, auto& sc) { t.reparent(sc, parentContainer->mSurfaceControl); });
Patrick Williams87602162024-11-05 10:13:19 -06001142 containerSurface->mInputInfo->editInfo()->replaceTouchableRegionWithCrop = true;
1143 containerSurface->mInputInfo->editInfo()->touchableRegionCropHandle = nullptr;
Prabir Pradhan56d55bc2024-10-28 21:00:30 +00001144 parentContainer->doTransaction(
1145 [&](auto& t, auto& sc) { t.reparent(sc, bgContainer->mSurfaceControl); });
1146 bgContainer->showAt(0, 0, Rect(0, 0, 100, 100));
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001147 parentContainer->showAt(10, 10, Rect(0, 0, 20, 20));
1148 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1149
1150 // Receives events inside parent bounds
1151 injectTap(21, 21);
1152 containerSurface->expectTap(1, 1); // Event is in layer space
1153
1154 // Does not receive events outside parent bounds
1155 injectTap(31, 31);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001156 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001157}
1158
1159/**
1160 * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop
1161 * layer's bounds. The input events should be in the layer's coordinate space.
1162 */
1163TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) {
1164 std::unique_ptr<InputSurface> cropLayer =
1165 InputSurface::makeContainerInputSurface(mComposerClient, 0, 0);
1166 cropLayer->showAt(50, 50, Rect(0, 0, 20, 20));
1167
1168 std::unique_ptr<InputSurface> containerSurface =
1169 InputSurface::makeContainerInputSurface(mComposerClient, 100, 100);
Patrick Williams87602162024-11-05 10:13:19 -06001170 containerSurface->mInputInfo->editInfo()->replaceTouchableRegionWithCrop = true;
1171 containerSurface->mInputInfo->editInfo()->touchableRegionCropHandle =
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001172 cropLayer->mSurfaceControl->getHandle();
1173 containerSurface->showAt(10, 10, Rect::INVALID_RECT);
1174
1175 // Receives events inside crop layer bounds
1176 injectTap(51, 51);
1177 containerSurface->expectTap(41, 41); // Event is in layer space
1178
1179 // Does not receive events outside crop layer bounds
1180 injectTap(21, 21);
1181 injectTap(71, 71);
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001182 containerSurface->assertNoEvent();
Prabir Pradhan6fa425a2021-12-16 07:16:04 -08001183}
1184
Linus Tufvessona1858822022-03-04 09:32:07 +00001185TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) {
1186 std::unique_ptr<InputSurface> parent = makeSurface(100, 100);
1187
1188 parent->showAt(100, 100);
1189 injectTap(101, 101);
1190 parent->expectTap(1, 1);
1191
1192 std::unique_ptr<InputSurface> childContainerSurface =
1193 InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100);
1194 childContainerSurface->showAt(0, 0);
1195 childContainerSurface->doTransaction(
Linnan Li13bf76a2024-05-05 19:18:02 +08001196 [&](auto& t, auto& sc) { t.reparent(sc, parent->mSurfaceControl); });
Linus Tufvessona1858822022-03-04 09:32:07 +00001197 injectTap(101, 101);
1198
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001199 parent->assertNoEvent();
Linus Tufvessona1858822022-03-04 09:32:07 +00001200}
1201
Vishnu Nair16a938f2021-09-24 07:14:54 -07001202class MultiDisplayTests : public InputSurfacesTest {
1203public:
1204 MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); }
Linnan Li13bf76a2024-05-05 19:18:02 +08001205
Prabir Pradhand0aba782021-12-14 00:44:21 -08001206 void TearDown() override {
Alan Dingd53801c2024-05-08 16:45:29 -07001207 std::for_each(mVirtualDisplays.begin(), mVirtualDisplays.end(),
1208 SurfaceComposerClient::destroyVirtualDisplay);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001209 InputSurfacesTest::TearDown();
1210 }
1211
Prabir Pradhand0aba782021-12-14 00:44:21 -08001212 void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack,
1213 bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001214 sp<IGraphicBufferConsumer> consumer;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001215 sp<IGraphicBufferProducer> producer;
1216 BufferQueue::createBufferQueue(&producer, &consumer);
Jim Shargoa2ab5be2024-12-04 18:30:35 +00001217 consumer->setConsumerName(String8("Virtual disp consumer (MultiDisplayTests)"));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001218 consumer->setDefaultBufferSize(width, height);
Jim Shargof48bef82024-12-04 18:56:59 +00001219
1220 class StubConsumerListener : public BnConsumerListener {
1221 virtual void onFrameAvailable(const BufferItem&) override {}
1222 virtual void onBuffersReleased() override {}
1223 virtual void onSidebandStreamChanged() override {}
1224 };
1225
1226 consumer->consumerConnect(sp<StubConsumerListener>::make(), true);
1227 mBufferQueues.push_back({consumer, producer});
Vishnu Nair16a938f2021-09-24 07:14:54 -07001228
Prabir Pradhand0aba782021-12-14 00:44:21 -08001229 std::string name = "VirtualDisplay";
1230 name += std::to_string(mVirtualDisplays.size());
Alan Dingd53801c2024-05-08 16:45:29 -07001231 sp<IBinder> token = SurfaceComposerClient::createVirtualDisplay(name, isSecure);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001232 SurfaceComposerClient::Transaction t;
Prabir Pradhand0aba782021-12-14 00:44:21 -08001233 t.setDisplaySurface(token, producer);
1234 t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0);
1235 t.setDisplayLayerStack(token, layerStack);
1236 t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height},
1237 {offsetX, offsetY, offsetX + width, offsetY + height});
Prabir Pradhana97fd942024-10-28 21:21:53 +00001238 t.apply(/*synchronously=*/true);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001239
1240 mVirtualDisplays.push_back(token);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001241 }
1242
Prabir Pradhand0aba782021-12-14 00:44:21 -08001243 std::vector<sp<IBinder>> mVirtualDisplays;
Jim Shargof48bef82024-12-04 18:56:59 +00001244 std::vector<std::tuple<sp<IGraphicBufferConsumer>, sp<IGraphicBufferProducer>>> mBufferQueues;
Vishnu Nair16a938f2021-09-24 07:14:54 -07001245};
1246
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001247TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) {
Prabir Pradhand0aba782021-12-14 00:44:21 -08001248 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1249 // Do not create a display associated with the LayerStack.
1250 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001251 surface->doTransaction([&](auto& t, auto& sc) { t.setLayerStack(sc, layerStack); });
Prabir Pradhand0aba782021-12-14 00:44:21 -08001252 surface->showAt(100, 100);
1253
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001254 // Touches should be dropped if the layer is on an invalid display.
Linnan Li13bf76a2024-05-05 19:18:02 +08001255 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001256 surface->assertNoEvent();
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001257
1258 // However, we still let the window be focused and receive keys.
Linnan Li13bf76a2024-05-05 19:18:02 +08001259 surface->requestFocus(toDisplayId(layerStack));
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001260 surface->assertFocusChange(true);
1261
Linnan Li13bf76a2024-05-05 19:18:02 +08001262 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Prabir Pradhanda0f62c2022-07-22 19:53:04 +00001263 surface->expectKey(AKEYCODE_V);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001264}
1265
1266TEST_F(MultiDisplayTests, virtual_display_receives_input) {
1267 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1268 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1269 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001270 surface->doTransaction([&](auto& t, auto& sc) { t.setLayerStack(sc, layerStack); });
Prabir Pradhand0aba782021-12-14 00:44:21 -08001271 surface->showAt(100, 100);
1272
Linnan Li13bf76a2024-05-05 19:18:02 +08001273 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001274 surface->expectTap(1, 1);
1275
Linnan Li13bf76a2024-05-05 19:18:02 +08001276 surface->requestFocus(toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001277 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001278 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Prabir Pradhand0aba782021-12-14 00:44:21 -08001279 surface->expectKey(AKEYCODE_V);
1280}
1281
Vishnu Nair16a938f2021-09-24 07:14:54 -07001282TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) {
1283 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
1284 createDisplay(1000, 1000, false /*isSecure*/, layerStack);
1285 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001286 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001287 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1288 t.setLayerStack(sc, layerStack);
1289 });
1290 surface->showAt(100, 100);
1291
Linnan Li13bf76a2024-05-05 19:18:02 +08001292 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001293
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001294 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001295
Linnan Li13bf76a2024-05-05 19:18:02 +08001296 surface->requestFocus(toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001297 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001298 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001299 surface->assertNoEvent();
Vishnu Nair16a938f2021-09-24 07:14:54 -07001300}
1301
1302TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) {
1303 ui::LayerStack layerStack = ui::LayerStack::fromValue(42);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001304
1305 // Create the secure display as system, because only certain users can create secure displays.
1306 seteuid(AID_SYSTEM);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001307 createDisplay(1000, 1000, true /*isSecure*/, layerStack);
Prabir Pradhand0aba782021-12-14 00:44:21 -08001308 // Change the uid back to root.
1309 seteuid(AID_ROOT);
1310
Vishnu Nair16a938f2021-09-24 07:14:54 -07001311 std::unique_ptr<InputSurface> surface = makeSurface(100, 100);
Linnan Li13bf76a2024-05-05 19:18:02 +08001312 surface->doTransaction([&](auto& t, auto& sc) {
Vishnu Nair16a938f2021-09-24 07:14:54 -07001313 t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure);
1314 t.setLayerStack(sc, layerStack);
1315 });
1316 surface->showAt(100, 100);
1317
Linnan Li13bf76a2024-05-05 19:18:02 +08001318 injectTapOnDisplay(101, 101, toDisplayId(layerStack));
Siarhei Vishniakou7b3fda32024-03-29 14:27:49 -07001319 surface->expectTap(1, 1);
Vishnu Nair16a938f2021-09-24 07:14:54 -07001320
Linnan Li13bf76a2024-05-05 19:18:02 +08001321 surface->requestFocus(toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001322 surface->assertFocusChange(true);
Linnan Li13bf76a2024-05-05 19:18:02 +08001323 injectKeyOnDisplay(AKEYCODE_V, toDisplayId(layerStack));
Vishnu Nair16a938f2021-09-24 07:14:54 -07001324
1325 surface->expectKey(AKEYCODE_V);
1326}
1327
Linnan Li13bf76a2024-05-05 19:18:02 +08001328} // namespace test
1329} // namespace android