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