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 | |
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 | |
| 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 */, |
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 | |
| 156 | ~InputSurface() { |
| 157 | mInputFlinger->unregisterInputChannel(mServerChannel); |
| 158 | } |
| 159 | |
| 160 | void doTransaction(std::function<void(SurfaceComposerClient::Transaction&, |
| 161 | const sp<SurfaceControl>&)> transactionBody) { |
| 162 | SurfaceComposerClient::Transaction t; |
| 163 | transactionBody(t, mSurfaceControl); |
| 164 | t.apply(true); |
| 165 | } |
| 166 | |
| 167 | void showAt(int x, int y) { |
| 168 | SurfaceComposerClient::Transaction t; |
| 169 | t.show(mSurfaceControl); |
| 170 | t.setInputWindowInfo(mSurfaceControl, mInputInfo); |
| 171 | t.setLayer(mSurfaceControl, LAYER_BASE); |
| 172 | t.setPosition(mSurfaceControl, x, y); |
| 173 | t.setCrop_legacy(mSurfaceControl, Rect(0, 0, 100, 100)); |
| 174 | t.setAlpha(mSurfaceControl, 1); |
| 175 | t.apply(true); |
| 176 | } |
| 177 | |
| 178 | private: |
| 179 | void waitForEventAvailable() { |
| 180 | struct pollfd fd; |
| 181 | |
| 182 | fd.fd = mClientChannel->getFd(); |
| 183 | fd.events = POLLIN; |
| 184 | poll(&fd, 1, 3000); |
| 185 | } |
| 186 | |
| 187 | void populateInputInfo(int width, int height) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 188 | mInputInfo.token = mServerChannel->getConnectionToken(); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 189 | mInputInfo.name = "Test info"; |
| 190 | mInputInfo.layoutParamsFlags = InputWindowInfo::FLAG_NOT_TOUCH_MODAL; |
| 191 | mInputInfo.layoutParamsType = InputWindowInfo::TYPE_BASE_APPLICATION; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 192 | mInputInfo.dispatchingTimeout = 5s; |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 193 | mInputInfo.globalScaleFactor = 1.0; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 194 | mInputInfo.canReceiveKeys = true; |
| 195 | mInputInfo.hasFocus = true; |
| 196 | mInputInfo.hasWallpaper = false; |
| 197 | mInputInfo.paused = false; |
| 198 | |
| 199 | mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height)); |
| 200 | |
| 201 | // TODO: Fill in from SF? |
| 202 | mInputInfo.ownerPid = 11111; |
| 203 | mInputInfo.ownerUid = 11111; |
| 204 | mInputInfo.inputFeatures = 0; |
| 205 | mInputInfo.displayId = 0; |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 206 | |
| 207 | InputApplicationInfo aInfo; |
| 208 | aInfo.token = new BBinder(); |
| 209 | aInfo.name = "Test app info"; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 210 | aInfo.dispatchingTimeout = 5s; |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 211 | |
| 212 | mInputInfo.applicationInfo = aInfo; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 213 | } |
| 214 | public: |
| 215 | sp<SurfaceControl> mSurfaceControl; |
| 216 | sp<InputChannel> mServerChannel, mClientChannel; |
| 217 | sp<IInputFlinger> mInputFlinger; |
| 218 | |
| 219 | InputWindowInfo mInputInfo; |
| 220 | |
| 221 | PreallocatedInputEventFactory mInputEventFactory; |
| 222 | InputConsumer* mInputConsumer; |
| 223 | }; |
| 224 | |
| 225 | class InputSurfacesTest : public ::testing::Test { |
| 226 | public: |
| 227 | InputSurfacesTest() { |
| 228 | ProcessState::self()->startThreadPool(); |
| 229 | } |
| 230 | |
| 231 | void SetUp() { |
| 232 | mComposerClient = new SurfaceComposerClient; |
| 233 | ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 234 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 235 | const auto display = mComposerClient->getInternalDisplayToken(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 236 | ASSERT_NE(display, nullptr); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 237 | |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 238 | DisplayConfig config; |
| 239 | ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayConfig(display, &config)); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 240 | |
| 241 | // After a new buffer is queued, SurfaceFlinger is notified and will |
| 242 | // latch the new buffer on next vsync. Let's heuristically wait for 3 |
| 243 | // vsyncs. |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 244 | mBufferPostDelay = static_cast<int32_t>(1e6 / config.refreshRate) * 3; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void TearDown() { |
| 248 | mComposerClient->dispose(); |
| 249 | } |
| 250 | |
| 251 | std::unique_ptr<InputSurface> makeSurface(int width, int height) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 252 | return InputSurface::makeColorInputSurface(mComposerClient, width, height); |
| 253 | } |
| 254 | |
| 255 | void postBuffer(const sp<SurfaceControl> &layer) { |
| 256 | // wait for previous transactions (such as setSize) to complete |
| 257 | Transaction().apply(true); |
| 258 | ANativeWindow_Buffer buffer = {}; |
| 259 | EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr)); |
| 260 | ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost()); |
| 261 | // Request an empty transaction to get applied synchronously to ensure the buffer is |
| 262 | // latched. |
| 263 | Transaction().apply(true); |
| 264 | usleep(mBufferPostDelay); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | sp<SurfaceComposerClient> mComposerClient; |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 268 | int32_t mBufferPostDelay; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | void injectTap(int x, int y) { |
| 272 | char *buf1, *buf2; |
| 273 | asprintf(&buf1, "%d", x); |
| 274 | asprintf(&buf2, "%d", y); |
| 275 | if (fork() == 0) { |
| 276 | execlp("input", "input", "tap", buf1, buf2, NULL); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | TEST_F(InputSurfacesTest, can_receive_input) { |
| 281 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 282 | surface->showAt(100, 100); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 283 | surface->assertFocusChange(true); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 284 | |
| 285 | injectTap(101, 101); |
| 286 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 287 | EXPECT_NE(surface->consumeEvent(), nullptr); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 290 | /** |
| 291 | * Set up two surfaces side-by-side. Tap each surface. |
| 292 | * Next, swap the positions of the two surfaces. Inject tap into the two |
| 293 | * original locations. Ensure that the tap is received by the surfaces in the |
| 294 | * reverse order. |
| 295 | */ |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 296 | TEST_F(InputSurfacesTest, input_respects_positioning) { |
| 297 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 298 | surface->showAt(100, 100); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 299 | surface->assertFocusChange(true); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 300 | |
| 301 | std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100); |
| 302 | surface2->showAt(200, 200); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 303 | surface->assertFocusChange(false); |
| 304 | surface2->assertFocusChange(true); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 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); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 331 | surface->assertFocusChange(true); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 332 | surface2->showAt(10, 10); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 333 | surface->assertFocusChange(false); |
| 334 | surface2->assertFocusChange(true); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 335 | |
| 336 | surface->doTransaction([](auto &t, auto &sc) { |
| 337 | t.setLayer(sc, LAYER_BASE + 1); |
| 338 | }); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 339 | surface2->assertFocusChange(false); |
| 340 | surface->assertFocusChange(true); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 341 | |
| 342 | injectTap(11, 11); |
| 343 | surface->expectTap(1, 1); |
| 344 | |
| 345 | surface2->doTransaction([](auto &t, auto &sc) { |
| 346 | t.setLayer(sc, LAYER_BASE + 1); |
| 347 | }); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 348 | surface2->assertFocusChange(true); |
| 349 | surface->assertFocusChange(false); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 350 | |
| 351 | injectTap(11, 11); |
| 352 | surface2->expectTap(1, 1); |
| 353 | |
| 354 | surface2->doTransaction([](auto &t, auto &sc) { |
| 355 | t.hide(sc); |
| 356 | }); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 357 | surface2->assertFocusChange(false); |
| 358 | surface->assertFocusChange(true); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 359 | |
| 360 | injectTap(11, 11); |
| 361 | surface->expectTap(1, 1); |
| 362 | } |
| 363 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 364 | // Surface Insets are set to offset the client content and draw a border around the client surface |
| 365 | // (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start |
| 366 | // of the client content. |
| 367 | TEST_F(InputSurfacesTest, input_respects_surface_insets) { |
| 368 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 369 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 370 | bgSurface->showAt(100, 100); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 371 | bgSurface->assertFocusChange(true); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 372 | |
| 373 | fgSurface->mInputInfo.surfaceInset = 5; |
| 374 | fgSurface->showAt(100, 100); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 375 | fgSurface->assertFocusChange(true); |
| 376 | bgSurface->assertFocusChange(false); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 377 | |
| 378 | injectTap(106, 106); |
| 379 | fgSurface->expectTap(1, 1); |
| 380 | |
| 381 | injectTap(101, 101); |
| 382 | bgSurface->expectTap(1, 1); |
| 383 | } |
| 384 | |
| 385 | // Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463 |
| 386 | TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) { |
| 387 | std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100); |
| 388 | std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100); |
| 389 | parentSurface->showAt(100, 100); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 390 | parentSurface->assertFocusChange(true); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 391 | |
| 392 | childSurface->mInputInfo.surfaceInset = 10; |
| 393 | childSurface->showAt(100, 100); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 394 | childSurface->assertFocusChange(true); |
| 395 | parentSurface->assertFocusChange(false); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 396 | |
| 397 | childSurface->doTransaction([&](auto &t, auto &sc) { |
| 398 | t.setPosition(sc, -5, -5); |
| 399 | t.reparent(sc, parentSurface->mSurfaceControl->getHandle()); |
| 400 | }); |
| 401 | |
| 402 | injectTap(106, 106); |
| 403 | childSurface->expectTap(1, 1); |
| 404 | |
| 405 | injectTap(101, 101); |
| 406 | parentSurface->expectTap(1, 1); |
| 407 | } |
| 408 | |
Arthur Hung | 118b114 | 2019-05-08 21:25:59 +0800 | [diff] [blame] | 409 | // Ensure a surface whose insets are scaled, handles the touch offset correctly. |
| 410 | TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) { |
| 411 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 412 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 413 | bgSurface->showAt(100, 100); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 414 | bgSurface->assertFocusChange(true); |
Arthur Hung | 118b114 | 2019-05-08 21:25:59 +0800 | [diff] [blame] | 415 | |
| 416 | fgSurface->mInputInfo.surfaceInset = 5; |
| 417 | fgSurface->showAt(100, 100); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 418 | bgSurface->assertFocusChange(false); |
| 419 | fgSurface->assertFocusChange(true); |
Arthur Hung | 118b114 | 2019-05-08 21:25:59 +0800 | [diff] [blame] | 420 | |
| 421 | fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); }); |
| 422 | |
| 423 | // expect = touch / scale - inset |
| 424 | injectTap(112, 124); |
| 425 | fgSurface->expectTap(1, 1); |
| 426 | |
| 427 | injectTap(101, 101); |
| 428 | bgSurface->expectTap(1, 1); |
| 429 | } |
| 430 | |
Ady Abraham | 282f1d7 | 2019-07-24 18:05:56 -0700 | [diff] [blame] | 431 | TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) { |
| 432 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 433 | // In case we pass the very big inset without any checking. |
| 434 | fgSurface->mInputInfo.surfaceInset = INT32_MAX; |
| 435 | fgSurface->showAt(100, 100); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 436 | fgSurface->assertFocusChange(true); |
Ady Abraham | 282f1d7 | 2019-07-24 18:05:56 -0700 | [diff] [blame] | 437 | |
| 438 | fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); }); |
| 439 | |
| 440 | // expect no crash for overflow, and inset size to be clamped to surface size |
| 441 | injectTap(202, 202); |
| 442 | fgSurface->expectTap(1, 1); |
| 443 | } |
| 444 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 445 | // Ensure we ignore transparent region when getting screen bounds when positioning input frame. |
| 446 | TEST_F(InputSurfacesTest, input_ignores_transparent_region) { |
| 447 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 448 | surface->doTransaction([](auto &t, auto &sc) { |
| 449 | Region transparentRegion(Rect(0, 0, 10, 10)); |
| 450 | t.setTransparentRegionHint(sc, transparentRegion); |
| 451 | }); |
| 452 | surface->showAt(100, 100); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 453 | surface->assertFocusChange(true); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 454 | injectTap(101, 101); |
| 455 | surface->expectTap(1, 1); |
| 456 | } |
| 457 | |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 458 | // TODO(b/139494112) update tests once we define expected behavior |
| 459 | // Ensure we still send input to the surface regardless of surface visibility changes due to the |
| 460 | // first buffer being submitted or alpha changes. |
| 461 | // Original bug ref: b/120839715 |
| 462 | TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 463 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 464 | std::unique_ptr<InputSurface> bufferSurface = |
| 465 | InputSurface::makeBufferInputSurface(mComposerClient, 100, 100); |
| 466 | |
| 467 | bgSurface->showAt(10, 10); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 468 | bgSurface->assertFocusChange(true); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 469 | bufferSurface->showAt(10, 10); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 470 | bgSurface->assertFocusChange(false); |
| 471 | bufferSurface->assertFocusChange(true); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 472 | |
| 473 | injectTap(11, 11); |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 474 | bufferSurface->expectTap(1, 1); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 475 | |
| 476 | postBuffer(bufferSurface->mSurfaceControl); |
| 477 | injectTap(11, 11); |
| 478 | bufferSurface->expectTap(1, 1); |
| 479 | } |
| 480 | |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 481 | TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 482 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 483 | std::unique_ptr<InputSurface> bufferSurface = |
| 484 | InputSurface::makeBufferInputSurface(mComposerClient, 100, 100); |
| 485 | postBuffer(bufferSurface->mSurfaceControl); |
| 486 | |
| 487 | bgSurface->showAt(10, 10); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 488 | bgSurface->assertFocusChange(true); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 489 | bufferSurface->showAt(10, 10); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 490 | bufferSurface->assertFocusChange(true); |
| 491 | bgSurface->assertFocusChange(false); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 492 | |
| 493 | injectTap(11, 11); |
| 494 | bufferSurface->expectTap(1, 1); |
| 495 | |
| 496 | bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); }); |
| 497 | |
| 498 | injectTap(11, 11); |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 499 | bufferSurface->expectTap(1, 1); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 502 | TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 503 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 504 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 505 | |
| 506 | bgSurface->showAt(10, 10); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 507 | bgSurface->assertFocusChange(true); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 508 | fgSurface->showAt(10, 10); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 509 | bgSurface->assertFocusChange(false); |
| 510 | fgSurface->assertFocusChange(true); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 511 | |
| 512 | injectTap(11, 11); |
| 513 | fgSurface->expectTap(1, 1); |
| 514 | |
| 515 | fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); }); |
| 516 | |
| 517 | injectTap(11, 11); |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 518 | fgSurface->expectTap(1, 1); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) { |
| 522 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 523 | std::unique_ptr<InputSurface> containerSurface = |
| 524 | InputSurface::makeContainerInputSurface(mComposerClient, 100, 100); |
| 525 | |
| 526 | bgSurface->showAt(10, 10); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 527 | bgSurface->assertFocusChange(true); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 528 | containerSurface->showAt(10, 10); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 529 | bgSurface->assertFocusChange(false); |
| 530 | containerSurface->assertFocusChange(true); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 531 | |
| 532 | injectTap(11, 11); |
| 533 | containerSurface->expectTap(1, 1); |
| 534 | |
| 535 | containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); }); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 536 | containerSurface->assertFocusChange(false); |
| 537 | bgSurface->assertFocusChange(true); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 538 | |
| 539 | injectTap(11, 11); |
| 540 | bgSurface->expectTap(1, 1); |
| 541 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 542 | |
Arthur Hung | d20b270 | 2019-01-14 18:16:16 +0800 | [diff] [blame] | 543 | TEST_F(InputSurfacesTest, input_respects_outscreen) { |
| 544 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 545 | surface->showAt(-1, -1); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 546 | surface->assertFocusChange(true); |
Arthur Hung | d20b270 | 2019-01-14 18:16:16 +0800 | [diff] [blame] | 547 | |
| 548 | injectTap(0, 0); |
| 549 | surface->expectTap(1, 1); |
| 550 | } |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 551 | |
| 552 | TEST_F(InputSurfacesTest, input_ignores_cursor_layer) { |
| 553 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 554 | std::unique_ptr<InputSurface> cursorSurface = |
| 555 | InputSurface::makeCursorInputSurface(mComposerClient, 10, 10); |
| 556 | |
| 557 | surface->showAt(10, 10); |
| 558 | surface->assertFocusChange(true); |
| 559 | cursorSurface->showAt(10, 10); |
| 560 | |
| 561 | injectTap(11, 11); |
| 562 | surface->expectTap(1, 1); |
| 563 | } |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 564 | } |
| 565 | } |