Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 27 | #include <android/native_window.h> |
| 28 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 29 | #include <binder/Binder.h> |
| 30 | #include <binder/IServiceManager.h> |
| 31 | #include <binder/Parcel.h> |
| 32 | #include <binder/ProcessState.h> |
| 33 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 34 | #include <gui/ISurfaceComposer.h> |
| 35 | #include <gui/Surface.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 36 | #include <gui/SurfaceComposerClient.h> |
| 37 | #include <gui/SurfaceControl.h> |
| 38 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 39 | #include <android/os/IInputFlinger.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 40 | #include <input/Input.h> |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 41 | #include <input/InputTransport.h> |
| 42 | #include <input/InputWindow.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 43 | |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 44 | #include <ui/DisplayConfig.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 45 | #include <ui/Rect.h> |
| 46 | #include <ui/Region.h> |
| 47 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 48 | using android::os::IInputFlinger; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 49 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 50 | namespace android::test { |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 51 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 52 | using Transaction = SurfaceComposerClient::Transaction; |
| 53 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 54 | sp<IInputFlinger> getInputFlinger() { |
| 55 | sp<IBinder> input(defaultServiceManager()->getService( |
| 56 | String16("inputflinger"))); |
| 57 | if (input == nullptr) { |
| 58 | ALOGE("Failed to link to input service"); |
| 59 | } else { ALOGE("Linked to input"); } |
| 60 | return interface_cast<IInputFlinger>(input); |
| 61 | } |
| 62 | |
| 63 | // We use the top 10 layers as a way to haphazardly place ourselves above anything else. |
| 64 | static const int LAYER_BASE = INT32_MAX - 10; |
Chris Ye | 6c4243b | 2020-07-22 12:07:12 -0700 | [diff] [blame] | 65 | static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 66 | |
| 67 | class InputSurface { |
| 68 | public: |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 69 | InputSurface(const sp<SurfaceControl> &sc, int width, int height) { |
| 70 | mSurfaceControl = sc; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 71 | |
| 72 | mInputFlinger = getInputFlinger(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 73 | mClientChannel = std::make_shared<InputChannel>(); |
| 74 | mInputFlinger->createInputChannel("testchannels", mClientChannel.get()); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 75 | |
| 76 | populateInputInfo(width, height); |
| 77 | |
| 78 | mInputConsumer = new InputConsumer(mClientChannel); |
| 79 | } |
| 80 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 81 | static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc, |
| 82 | int width, int height) { |
| 83 | sp<SurfaceControl> surfaceControl = |
| 84 | scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */, |
Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 85 | PIXEL_FORMAT_RGBA_8888, |
| 86 | ISurfaceComposerClient::eFXSurfaceEffect); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 87 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 88 | } |
| 89 | |
| 90 | static std::unique_ptr<InputSurface> makeBufferInputSurface( |
| 91 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 92 | sp<SurfaceControl> surfaceControl = |
| 93 | scc->createSurface(String8("Test Buffer Surface"), width, height, |
| 94 | PIXEL_FORMAT_RGBA_8888, 0 /* flags */); |
| 95 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 96 | } |
| 97 | |
| 98 | static std::unique_ptr<InputSurface> makeContainerInputSurface( |
| 99 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 100 | sp<SurfaceControl> surfaceControl = |
| 101 | scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */, |
| 102 | 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888, |
| 103 | ISurfaceComposerClient::eFXSurfaceContainer); |
| 104 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 105 | } |
| 106 | |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 107 | static std::unique_ptr<InputSurface> makeCursorInputSurface( |
| 108 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 109 | sp<SurfaceControl> surfaceControl = |
| 110 | scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */, |
| 111 | 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888, |
| 112 | ISurfaceComposerClient::eCursorWindow); |
| 113 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 114 | } |
| 115 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 116 | InputEvent* consumeEvent() { |
| 117 | waitForEventAvailable(); |
| 118 | |
| 119 | InputEvent *ev; |
| 120 | uint32_t seqId; |
| 121 | status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev); |
| 122 | if (consumed != OK) { |
| 123 | return nullptr; |
| 124 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 125 | status_t status = mInputConsumer->sendFinishedSignal(seqId, true); |
| 126 | EXPECT_EQ(OK, status) << "Could not send finished signal"; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 127 | return ev; |
| 128 | } |
| 129 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 130 | void assertFocusChange(bool hasFocus) { |
| 131 | InputEvent *ev = consumeEvent(); |
| 132 | ASSERT_NE(ev, nullptr); |
| 133 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, ev->getType()); |
| 134 | FocusEvent *focusEvent = static_cast<FocusEvent *>(ev); |
| 135 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
| 136 | } |
| 137 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 138 | void expectTap(int x, int y) { |
| 139 | InputEvent* ev = consumeEvent(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 140 | ASSERT_NE(ev, nullptr); |
| 141 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType()); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 142 | MotionEvent* mev = static_cast<MotionEvent*>(ev); |
| 143 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction()); |
| 144 | EXPECT_EQ(x, mev->getX(0)); |
| 145 | EXPECT_EQ(y, mev->getY(0)); |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 146 | EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 147 | |
| 148 | ev = consumeEvent(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 149 | ASSERT_NE(ev, nullptr); |
| 150 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType()); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 151 | mev = static_cast<MotionEvent*>(ev); |
| 152 | EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction()); |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 153 | EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 156 | ~InputSurface() { mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken()); } |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 157 | |
| 158 | void doTransaction(std::function<void(SurfaceComposerClient::Transaction&, |
| 159 | const sp<SurfaceControl>&)> transactionBody) { |
| 160 | SurfaceComposerClient::Transaction t; |
| 161 | transactionBody(t, mSurfaceControl); |
| 162 | t.apply(true); |
| 163 | } |
| 164 | |
| 165 | void showAt(int x, int y) { |
| 166 | SurfaceComposerClient::Transaction t; |
| 167 | t.show(mSurfaceControl); |
| 168 | t.setInputWindowInfo(mSurfaceControl, mInputInfo); |
| 169 | t.setLayer(mSurfaceControl, LAYER_BASE); |
| 170 | t.setPosition(mSurfaceControl, x, y); |
| 171 | t.setCrop_legacy(mSurfaceControl, Rect(0, 0, 100, 100)); |
| 172 | t.setAlpha(mSurfaceControl, 1); |
| 173 | t.apply(true); |
| 174 | } |
| 175 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 176 | void requestFocus() { |
| 177 | SurfaceComposerClient::Transaction t; |
| 178 | t.setFocusedWindow(mInputInfo.token, nullptr, systemTime(SYSTEM_TIME_MONOTONIC), |
| 179 | 0 /* displayId */); |
| 180 | t.apply(true); |
| 181 | } |
| 182 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 183 | private: |
| 184 | void waitForEventAvailable() { |
| 185 | struct pollfd fd; |
| 186 | |
| 187 | fd.fd = mClientChannel->getFd(); |
| 188 | fd.events = POLLIN; |
| 189 | poll(&fd, 1, 3000); |
| 190 | } |
| 191 | |
| 192 | void populateInputInfo(int width, int height) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 193 | mInputInfo.token = mClientChannel->getConnectionToken(); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 194 | mInputInfo.name = "Test info"; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 195 | mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL; |
| 196 | mInputInfo.type = InputWindowInfo::Type::BASE_APPLICATION; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 197 | mInputInfo.dispatchingTimeout = 5s; |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 198 | mInputInfo.globalScaleFactor = 1.0; |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 199 | mInputInfo.focusable = true; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 200 | mInputInfo.hasWallpaper = false; |
| 201 | mInputInfo.paused = false; |
| 202 | |
| 203 | mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height)); |
| 204 | |
| 205 | // TODO: Fill in from SF? |
| 206 | mInputInfo.ownerPid = 11111; |
| 207 | mInputInfo.ownerUid = 11111; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 208 | mInputInfo.displayId = 0; |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 209 | |
| 210 | InputApplicationInfo aInfo; |
| 211 | aInfo.token = new BBinder(); |
| 212 | aInfo.name = "Test app info"; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 213 | aInfo.dispatchingTimeoutMillis = |
| 214 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 215 | |
| 216 | mInputInfo.applicationInfo = aInfo; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 217 | } |
| 218 | public: |
| 219 | sp<SurfaceControl> mSurfaceControl; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 220 | std::shared_ptr<InputChannel> mClientChannel; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 221 | sp<IInputFlinger> mInputFlinger; |
| 222 | |
| 223 | InputWindowInfo mInputInfo; |
| 224 | |
| 225 | PreallocatedInputEventFactory mInputEventFactory; |
| 226 | InputConsumer* mInputConsumer; |
| 227 | }; |
| 228 | |
| 229 | class InputSurfacesTest : public ::testing::Test { |
| 230 | public: |
| 231 | InputSurfacesTest() { |
| 232 | ProcessState::self()->startThreadPool(); |
| 233 | } |
| 234 | |
| 235 | void SetUp() { |
| 236 | mComposerClient = new SurfaceComposerClient; |
| 237 | ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 238 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 239 | const auto display = mComposerClient->getInternalDisplayToken(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 240 | ASSERT_NE(display, nullptr); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 241 | |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 242 | DisplayConfig config; |
| 243 | ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayConfig(display, &config)); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 244 | |
| 245 | // After a new buffer is queued, SurfaceFlinger is notified and will |
| 246 | // latch the new buffer on next vsync. Let's heuristically wait for 3 |
| 247 | // vsyncs. |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 248 | mBufferPostDelay = static_cast<int32_t>(1e6 / config.refreshRate) * 3; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void TearDown() { |
| 252 | mComposerClient->dispose(); |
| 253 | } |
| 254 | |
| 255 | std::unique_ptr<InputSurface> makeSurface(int width, int height) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 256 | return InputSurface::makeColorInputSurface(mComposerClient, width, height); |
| 257 | } |
| 258 | |
| 259 | void postBuffer(const sp<SurfaceControl> &layer) { |
| 260 | // wait for previous transactions (such as setSize) to complete |
| 261 | Transaction().apply(true); |
| 262 | ANativeWindow_Buffer buffer = {}; |
| 263 | EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr)); |
| 264 | ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost()); |
| 265 | // Request an empty transaction to get applied synchronously to ensure the buffer is |
| 266 | // latched. |
| 267 | Transaction().apply(true); |
| 268 | usleep(mBufferPostDelay); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | sp<SurfaceComposerClient> mComposerClient; |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 272 | int32_t mBufferPostDelay; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | void injectTap(int x, int y) { |
| 276 | char *buf1, *buf2; |
| 277 | asprintf(&buf1, "%d", x); |
| 278 | asprintf(&buf2, "%d", y); |
| 279 | if (fork() == 0) { |
| 280 | execlp("input", "input", "tap", buf1, buf2, NULL); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | TEST_F(InputSurfacesTest, can_receive_input) { |
| 285 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 286 | surface->showAt(100, 100); |
| 287 | |
| 288 | injectTap(101, 101); |
| 289 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 290 | EXPECT_NE(surface->consumeEvent(), nullptr); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 293 | /** |
| 294 | * Set up two surfaces side-by-side. Tap each surface. |
| 295 | * Next, swap the positions of the two surfaces. Inject tap into the two |
| 296 | * original locations. Ensure that the tap is received by the surfaces in the |
| 297 | * reverse order. |
| 298 | */ |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 299 | TEST_F(InputSurfacesTest, input_respects_positioning) { |
| 300 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 301 | surface->showAt(100, 100); |
| 302 | |
| 303 | std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100); |
| 304 | surface2->showAt(200, 200); |
| 305 | |
| 306 | injectTap(201, 201); |
| 307 | surface2->expectTap(1, 1); |
| 308 | |
| 309 | injectTap(101, 101); |
| 310 | surface->expectTap(1, 1); |
| 311 | |
| 312 | surface2->doTransaction([](auto &t, auto &sc) { |
| 313 | t.setPosition(sc, 100, 100); |
| 314 | }); |
| 315 | surface->doTransaction([](auto &t, auto &sc) { |
| 316 | t.setPosition(sc, 200, 200); |
| 317 | }); |
| 318 | |
| 319 | injectTap(101, 101); |
| 320 | surface2->expectTap(1, 1); |
| 321 | |
| 322 | injectTap(201, 201); |
| 323 | surface->expectTap(1, 1); |
| 324 | } |
| 325 | |
| 326 | TEST_F(InputSurfacesTest, input_respects_layering) { |
| 327 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 328 | std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100); |
| 329 | |
| 330 | surface->showAt(10, 10); |
| 331 | surface2->showAt(10, 10); |
| 332 | |
| 333 | surface->doTransaction([](auto &t, auto &sc) { |
| 334 | t.setLayer(sc, LAYER_BASE + 1); |
| 335 | }); |
| 336 | |
| 337 | injectTap(11, 11); |
| 338 | surface->expectTap(1, 1); |
| 339 | |
| 340 | surface2->doTransaction([](auto &t, auto &sc) { |
| 341 | t.setLayer(sc, LAYER_BASE + 1); |
| 342 | }); |
| 343 | |
| 344 | injectTap(11, 11); |
| 345 | surface2->expectTap(1, 1); |
| 346 | |
| 347 | surface2->doTransaction([](auto &t, auto &sc) { |
| 348 | t.hide(sc); |
| 349 | }); |
| 350 | |
| 351 | injectTap(11, 11); |
| 352 | surface->expectTap(1, 1); |
| 353 | } |
| 354 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 355 | // Surface Insets are set to offset the client content and draw a border around the client surface |
| 356 | // (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start |
| 357 | // of the client content. |
| 358 | TEST_F(InputSurfacesTest, input_respects_surface_insets) { |
| 359 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 360 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 361 | bgSurface->showAt(100, 100); |
| 362 | |
| 363 | fgSurface->mInputInfo.surfaceInset = 5; |
| 364 | fgSurface->showAt(100, 100); |
| 365 | |
| 366 | injectTap(106, 106); |
| 367 | fgSurface->expectTap(1, 1); |
| 368 | |
| 369 | injectTap(101, 101); |
| 370 | bgSurface->expectTap(1, 1); |
| 371 | } |
| 372 | |
| 373 | // Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463 |
| 374 | TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) { |
| 375 | std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100); |
| 376 | std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100); |
| 377 | parentSurface->showAt(100, 100); |
| 378 | |
| 379 | childSurface->mInputInfo.surfaceInset = 10; |
| 380 | childSurface->showAt(100, 100); |
| 381 | |
| 382 | childSurface->doTransaction([&](auto &t, auto &sc) { |
| 383 | t.setPosition(sc, -5, -5); |
| 384 | t.reparent(sc, parentSurface->mSurfaceControl->getHandle()); |
| 385 | }); |
| 386 | |
| 387 | injectTap(106, 106); |
| 388 | childSurface->expectTap(1, 1); |
| 389 | |
| 390 | injectTap(101, 101); |
| 391 | parentSurface->expectTap(1, 1); |
| 392 | } |
| 393 | |
Arthur Hung | 118b114 | 2019-05-08 21:25:59 +0800 | [diff] [blame] | 394 | // Ensure a surface whose insets are scaled, handles the touch offset correctly. |
| 395 | TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) { |
| 396 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 397 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 398 | bgSurface->showAt(100, 100); |
| 399 | |
| 400 | fgSurface->mInputInfo.surfaceInset = 5; |
| 401 | fgSurface->showAt(100, 100); |
| 402 | |
| 403 | fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); }); |
| 404 | |
| 405 | // expect = touch / scale - inset |
| 406 | injectTap(112, 124); |
| 407 | fgSurface->expectTap(1, 1); |
| 408 | |
| 409 | injectTap(101, 101); |
| 410 | bgSurface->expectTap(1, 1); |
| 411 | } |
| 412 | |
Ady Abraham | 282f1d7 | 2019-07-24 18:05:56 -0700 | [diff] [blame] | 413 | TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) { |
| 414 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 415 | // In case we pass the very big inset without any checking. |
| 416 | fgSurface->mInputInfo.surfaceInset = INT32_MAX; |
| 417 | fgSurface->showAt(100, 100); |
| 418 | |
| 419 | fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); }); |
| 420 | |
| 421 | // expect no crash for overflow, and inset size to be clamped to surface size |
| 422 | injectTap(202, 202); |
| 423 | fgSurface->expectTap(1, 1); |
| 424 | } |
| 425 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 426 | // Ensure we ignore transparent region when getting screen bounds when positioning input frame. |
| 427 | TEST_F(InputSurfacesTest, input_ignores_transparent_region) { |
| 428 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 429 | surface->doTransaction([](auto &t, auto &sc) { |
| 430 | Region transparentRegion(Rect(0, 0, 10, 10)); |
| 431 | t.setTransparentRegionHint(sc, transparentRegion); |
| 432 | }); |
| 433 | surface->showAt(100, 100); |
| 434 | injectTap(101, 101); |
| 435 | surface->expectTap(1, 1); |
| 436 | } |
| 437 | |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 438 | // TODO(b/139494112) update tests once we define expected behavior |
| 439 | // Ensure we still send input to the surface regardless of surface visibility changes due to the |
| 440 | // first buffer being submitted or alpha changes. |
| 441 | // Original bug ref: b/120839715 |
| 442 | TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 443 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 444 | std::unique_ptr<InputSurface> bufferSurface = |
| 445 | InputSurface::makeBufferInputSurface(mComposerClient, 100, 100); |
| 446 | |
| 447 | bgSurface->showAt(10, 10); |
| 448 | bufferSurface->showAt(10, 10); |
| 449 | |
| 450 | injectTap(11, 11); |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 451 | bufferSurface->expectTap(1, 1); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 452 | |
| 453 | postBuffer(bufferSurface->mSurfaceControl); |
| 454 | injectTap(11, 11); |
| 455 | bufferSurface->expectTap(1, 1); |
| 456 | } |
| 457 | |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 458 | TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 459 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 460 | std::unique_ptr<InputSurface> bufferSurface = |
| 461 | InputSurface::makeBufferInputSurface(mComposerClient, 100, 100); |
| 462 | postBuffer(bufferSurface->mSurfaceControl); |
| 463 | |
| 464 | bgSurface->showAt(10, 10); |
| 465 | bufferSurface->showAt(10, 10); |
| 466 | |
| 467 | injectTap(11, 11); |
| 468 | bufferSurface->expectTap(1, 1); |
| 469 | |
| 470 | bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); }); |
| 471 | |
| 472 | injectTap(11, 11); |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 473 | bufferSurface->expectTap(1, 1); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 476 | TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 477 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 478 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 479 | |
| 480 | bgSurface->showAt(10, 10); |
| 481 | fgSurface->showAt(10, 10); |
| 482 | |
| 483 | injectTap(11, 11); |
| 484 | fgSurface->expectTap(1, 1); |
| 485 | |
| 486 | fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); }); |
| 487 | |
| 488 | injectTap(11, 11); |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 489 | fgSurface->expectTap(1, 1); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) { |
| 493 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 494 | std::unique_ptr<InputSurface> containerSurface = |
| 495 | InputSurface::makeContainerInputSurface(mComposerClient, 100, 100); |
| 496 | |
| 497 | bgSurface->showAt(10, 10); |
| 498 | containerSurface->showAt(10, 10); |
| 499 | |
| 500 | injectTap(11, 11); |
| 501 | containerSurface->expectTap(1, 1); |
| 502 | |
| 503 | containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); }); |
| 504 | |
| 505 | injectTap(11, 11); |
| 506 | bgSurface->expectTap(1, 1); |
| 507 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 508 | |
Arthur Hung | d20b270 | 2019-01-14 18:16:16 +0800 | [diff] [blame] | 509 | TEST_F(InputSurfacesTest, input_respects_outscreen) { |
| 510 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 511 | surface->showAt(-1, -1); |
| 512 | |
| 513 | injectTap(0, 0); |
| 514 | surface->expectTap(1, 1); |
| 515 | } |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 516 | |
| 517 | TEST_F(InputSurfacesTest, input_ignores_cursor_layer) { |
| 518 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 519 | std::unique_ptr<InputSurface> cursorSurface = |
| 520 | InputSurface::makeCursorInputSurface(mComposerClient, 10, 10); |
| 521 | |
| 522 | surface->showAt(10, 10); |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 523 | cursorSurface->showAt(10, 10); |
| 524 | |
| 525 | injectTap(11, 11); |
| 526 | surface->expectTap(1, 1); |
| 527 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 528 | |
| 529 | TEST_F(InputSurfacesTest, can_be_focused) { |
| 530 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 531 | surface->showAt(100, 100); |
| 532 | surface->requestFocus(); |
| 533 | |
| 534 | surface->assertFocusChange(true); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 535 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 536 | } // namespace android::test |