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