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