Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | #include <sys/time.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <stdio.h> |
| 23 | #include <poll.h> |
| 24 | |
| 25 | #include <memory> |
| 26 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 27 | #include <android/native_window.h> |
| 28 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 29 | #include <binder/Binder.h> |
| 30 | #include <binder/IServiceManager.h> |
| 31 | #include <binder/Parcel.h> |
| 32 | #include <binder/ProcessState.h> |
| 33 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 34 | #include <gui/ISurfaceComposer.h> |
| 35 | #include <gui/Surface.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 36 | #include <gui/SurfaceComposerClient.h> |
| 37 | #include <gui/SurfaceControl.h> |
| 38 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 39 | #include <android/os/IInputFlinger.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 40 | #include <input/Input.h> |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 41 | #include <input/InputTransport.h> |
| 42 | #include <input/InputWindow.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 43 | |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 44 | #include <ui/DisplayConfig.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 45 | #include <ui/Rect.h> |
| 46 | #include <ui/Region.h> |
| 47 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 48 | using android::os::IInputFlinger; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 49 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 50 | namespace android::test { |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 51 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 52 | using Transaction = SurfaceComposerClient::Transaction; |
| 53 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 54 | sp<IInputFlinger> getInputFlinger() { |
| 55 | sp<IBinder> input(defaultServiceManager()->getService( |
| 56 | String16("inputflinger"))); |
| 57 | if (input == nullptr) { |
| 58 | ALOGE("Failed to link to input service"); |
| 59 | } else { ALOGE("Linked to input"); } |
| 60 | return interface_cast<IInputFlinger>(input); |
| 61 | } |
| 62 | |
| 63 | // We use the top 10 layers as a way to haphazardly place ourselves above anything else. |
| 64 | static const int LAYER_BASE = INT32_MAX - 10; |
Chris Ye | 6c4243b | 2020-07-22 12:07:12 -0700 | [diff] [blame] | 65 | static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 66 | |
| 67 | class InputSurface { |
| 68 | public: |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 69 | InputSurface(const sp<SurfaceControl> &sc, int width, int height) { |
| 70 | mSurfaceControl = sc; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 71 | |
| 72 | mInputFlinger = getInputFlinger(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 73 | mClientChannel = std::make_shared<InputChannel>(); |
| 74 | mInputFlinger->createInputChannel("testchannels", mClientChannel.get()); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 75 | |
| 76 | populateInputInfo(width, height); |
| 77 | |
| 78 | mInputConsumer = new InputConsumer(mClientChannel); |
| 79 | } |
| 80 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 81 | static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc, |
| 82 | int width, int height) { |
| 83 | sp<SurfaceControl> surfaceControl = |
| 84 | scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */, |
Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 85 | PIXEL_FORMAT_RGBA_8888, |
| 86 | ISurfaceComposerClient::eFXSurfaceEffect); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 87 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 88 | } |
| 89 | |
| 90 | static std::unique_ptr<InputSurface> makeBufferInputSurface( |
| 91 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 92 | sp<SurfaceControl> surfaceControl = |
| 93 | scc->createSurface(String8("Test Buffer Surface"), width, height, |
| 94 | PIXEL_FORMAT_RGBA_8888, 0 /* flags */); |
| 95 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 96 | } |
| 97 | |
| 98 | static std::unique_ptr<InputSurface> makeContainerInputSurface( |
| 99 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 100 | sp<SurfaceControl> surfaceControl = |
| 101 | scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */, |
| 102 | 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888, |
| 103 | ISurfaceComposerClient::eFXSurfaceContainer); |
| 104 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 105 | } |
| 106 | |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 107 | static std::unique_ptr<InputSurface> makeCursorInputSurface( |
| 108 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 109 | sp<SurfaceControl> surfaceControl = |
| 110 | scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */, |
| 111 | 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888, |
| 112 | ISurfaceComposerClient::eCursorWindow); |
| 113 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 114 | } |
| 115 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 116 | InputEvent* consumeEvent() { |
| 117 | waitForEventAvailable(); |
| 118 | |
| 119 | InputEvent *ev; |
| 120 | uint32_t seqId; |
| 121 | status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev); |
| 122 | if (consumed != OK) { |
| 123 | return nullptr; |
| 124 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 125 | status_t status = mInputConsumer->sendFinishedSignal(seqId, true); |
| 126 | EXPECT_EQ(OK, status) << "Could not send finished signal"; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 127 | return ev; |
| 128 | } |
| 129 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 130 | void assertFocusChange(bool hasFocus) { |
| 131 | InputEvent *ev = consumeEvent(); |
| 132 | ASSERT_NE(ev, nullptr); |
| 133 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, ev->getType()); |
| 134 | FocusEvent *focusEvent = static_cast<FocusEvent *>(ev); |
| 135 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
| 136 | } |
| 137 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 138 | void expectTap(int x, int y) { |
| 139 | InputEvent* ev = consumeEvent(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 140 | ASSERT_NE(ev, nullptr); |
| 141 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType()); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 142 | MotionEvent* mev = static_cast<MotionEvent*>(ev); |
| 143 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction()); |
| 144 | EXPECT_EQ(x, mev->getX(0)); |
| 145 | EXPECT_EQ(y, mev->getY(0)); |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 146 | EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 147 | |
| 148 | ev = consumeEvent(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 149 | ASSERT_NE(ev, nullptr); |
| 150 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType()); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 151 | mev = static_cast<MotionEvent*>(ev); |
| 152 | EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction()); |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 153 | EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 154 | } |
| 155 | |
chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame^] | 156 | void expectTapWithFlag(int x, int y, int32_t flags) { |
| 157 | InputEvent *ev = consumeEvent(); |
| 158 | ASSERT_NE(ev, nullptr); |
| 159 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType()); |
| 160 | MotionEvent *mev = static_cast<MotionEvent *>(ev); |
| 161 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction()); |
| 162 | EXPECT_EQ(x, mev->getX(0)); |
| 163 | EXPECT_EQ(y, mev->getY(0)); |
| 164 | EXPECT_EQ(flags, mev->getFlags() & flags); |
| 165 | |
| 166 | ev = consumeEvent(); |
| 167 | ASSERT_NE(ev, nullptr); |
| 168 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType()); |
| 169 | mev = static_cast<MotionEvent *>(ev); |
| 170 | EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction()); |
| 171 | EXPECT_EQ(flags, mev->getFlags() & flags); |
| 172 | } |
| 173 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 174 | ~InputSurface() { mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken()); } |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 175 | |
| 176 | void doTransaction(std::function<void(SurfaceComposerClient::Transaction&, |
| 177 | const sp<SurfaceControl>&)> transactionBody) { |
| 178 | SurfaceComposerClient::Transaction t; |
| 179 | transactionBody(t, mSurfaceControl); |
| 180 | t.apply(true); |
| 181 | } |
| 182 | |
| 183 | void showAt(int x, int y) { |
| 184 | SurfaceComposerClient::Transaction t; |
| 185 | t.show(mSurfaceControl); |
| 186 | t.setInputWindowInfo(mSurfaceControl, mInputInfo); |
| 187 | t.setLayer(mSurfaceControl, LAYER_BASE); |
| 188 | t.setPosition(mSurfaceControl, x, y); |
| 189 | t.setCrop_legacy(mSurfaceControl, Rect(0, 0, 100, 100)); |
| 190 | t.setAlpha(mSurfaceControl, 1); |
| 191 | t.apply(true); |
| 192 | } |
| 193 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 194 | void requestFocus() { |
| 195 | SurfaceComposerClient::Transaction t; |
| 196 | t.setFocusedWindow(mInputInfo.token, nullptr, systemTime(SYSTEM_TIME_MONOTONIC), |
| 197 | 0 /* displayId */); |
| 198 | t.apply(true); |
| 199 | } |
| 200 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 201 | private: |
| 202 | void waitForEventAvailable() { |
| 203 | struct pollfd fd; |
| 204 | |
| 205 | fd.fd = mClientChannel->getFd(); |
| 206 | fd.events = POLLIN; |
| 207 | poll(&fd, 1, 3000); |
| 208 | } |
| 209 | |
| 210 | void populateInputInfo(int width, int height) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 211 | mInputInfo.token = mClientChannel->getConnectionToken(); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 212 | mInputInfo.name = "Test info"; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 213 | mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL; |
| 214 | mInputInfo.type = InputWindowInfo::Type::BASE_APPLICATION; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 215 | mInputInfo.dispatchingTimeout = 5s; |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 216 | mInputInfo.globalScaleFactor = 1.0; |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 217 | mInputInfo.focusable = true; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 218 | mInputInfo.hasWallpaper = false; |
| 219 | mInputInfo.paused = false; |
| 220 | |
| 221 | mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height)); |
| 222 | |
| 223 | // TODO: Fill in from SF? |
| 224 | mInputInfo.ownerPid = 11111; |
| 225 | mInputInfo.ownerUid = 11111; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 226 | mInputInfo.displayId = 0; |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 227 | |
| 228 | InputApplicationInfo aInfo; |
| 229 | aInfo.token = new BBinder(); |
| 230 | aInfo.name = "Test app info"; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 231 | aInfo.dispatchingTimeoutMillis = |
| 232 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 233 | |
| 234 | mInputInfo.applicationInfo = aInfo; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 235 | } |
| 236 | public: |
| 237 | sp<SurfaceControl> mSurfaceControl; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 238 | std::shared_ptr<InputChannel> mClientChannel; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 239 | sp<IInputFlinger> mInputFlinger; |
| 240 | |
| 241 | InputWindowInfo mInputInfo; |
| 242 | |
| 243 | PreallocatedInputEventFactory mInputEventFactory; |
| 244 | InputConsumer* mInputConsumer; |
| 245 | }; |
| 246 | |
| 247 | class InputSurfacesTest : public ::testing::Test { |
| 248 | public: |
| 249 | InputSurfacesTest() { |
| 250 | ProcessState::self()->startThreadPool(); |
| 251 | } |
| 252 | |
| 253 | void SetUp() { |
| 254 | mComposerClient = new SurfaceComposerClient; |
| 255 | ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 256 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 257 | const auto display = mComposerClient->getInternalDisplayToken(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 258 | ASSERT_NE(display, nullptr); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 259 | |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 260 | DisplayConfig config; |
| 261 | ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayConfig(display, &config)); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 262 | |
| 263 | // After a new buffer is queued, SurfaceFlinger is notified and will |
| 264 | // latch the new buffer on next vsync. Let's heuristically wait for 3 |
| 265 | // vsyncs. |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 266 | mBufferPostDelay = static_cast<int32_t>(1e6 / config.refreshRate) * 3; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void TearDown() { |
| 270 | mComposerClient->dispose(); |
| 271 | } |
| 272 | |
| 273 | std::unique_ptr<InputSurface> makeSurface(int width, int height) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 274 | return InputSurface::makeColorInputSurface(mComposerClient, width, height); |
| 275 | } |
| 276 | |
| 277 | void postBuffer(const sp<SurfaceControl> &layer) { |
| 278 | // wait for previous transactions (such as setSize) to complete |
| 279 | Transaction().apply(true); |
| 280 | ANativeWindow_Buffer buffer = {}; |
| 281 | EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr)); |
| 282 | ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost()); |
| 283 | // Request an empty transaction to get applied synchronously to ensure the buffer is |
| 284 | // latched. |
| 285 | Transaction().apply(true); |
| 286 | usleep(mBufferPostDelay); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | sp<SurfaceComposerClient> mComposerClient; |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 290 | int32_t mBufferPostDelay; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | void injectTap(int x, int y) { |
| 294 | char *buf1, *buf2; |
| 295 | asprintf(&buf1, "%d", x); |
| 296 | asprintf(&buf2, "%d", y); |
| 297 | if (fork() == 0) { |
| 298 | execlp("input", "input", "tap", buf1, buf2, NULL); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | TEST_F(InputSurfacesTest, can_receive_input) { |
| 303 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 304 | surface->showAt(100, 100); |
| 305 | |
| 306 | injectTap(101, 101); |
| 307 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 308 | EXPECT_NE(surface->consumeEvent(), nullptr); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 311 | /** |
| 312 | * Set up two surfaces side-by-side. Tap each surface. |
| 313 | * Next, swap the positions of the two surfaces. Inject tap into the two |
| 314 | * original locations. Ensure that the tap is received by the surfaces in the |
| 315 | * reverse order. |
| 316 | */ |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 317 | TEST_F(InputSurfacesTest, input_respects_positioning) { |
| 318 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 319 | surface->showAt(100, 100); |
| 320 | |
| 321 | std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100); |
| 322 | surface2->showAt(200, 200); |
| 323 | |
| 324 | injectTap(201, 201); |
| 325 | surface2->expectTap(1, 1); |
| 326 | |
| 327 | injectTap(101, 101); |
| 328 | surface->expectTap(1, 1); |
| 329 | |
| 330 | surface2->doTransaction([](auto &t, auto &sc) { |
| 331 | t.setPosition(sc, 100, 100); |
| 332 | }); |
| 333 | surface->doTransaction([](auto &t, auto &sc) { |
| 334 | t.setPosition(sc, 200, 200); |
| 335 | }); |
| 336 | |
| 337 | injectTap(101, 101); |
| 338 | surface2->expectTap(1, 1); |
| 339 | |
| 340 | injectTap(201, 201); |
| 341 | surface->expectTap(1, 1); |
| 342 | } |
| 343 | |
| 344 | TEST_F(InputSurfacesTest, input_respects_layering) { |
| 345 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 346 | std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100); |
| 347 | |
| 348 | surface->showAt(10, 10); |
| 349 | surface2->showAt(10, 10); |
| 350 | |
| 351 | surface->doTransaction([](auto &t, auto &sc) { |
| 352 | t.setLayer(sc, LAYER_BASE + 1); |
| 353 | }); |
| 354 | |
| 355 | injectTap(11, 11); |
| 356 | surface->expectTap(1, 1); |
| 357 | |
| 358 | surface2->doTransaction([](auto &t, auto &sc) { |
| 359 | t.setLayer(sc, LAYER_BASE + 1); |
| 360 | }); |
| 361 | |
| 362 | injectTap(11, 11); |
| 363 | surface2->expectTap(1, 1); |
| 364 | |
| 365 | surface2->doTransaction([](auto &t, auto &sc) { |
| 366 | t.hide(sc); |
| 367 | }); |
| 368 | |
| 369 | injectTap(11, 11); |
| 370 | surface->expectTap(1, 1); |
| 371 | } |
| 372 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 373 | // Surface Insets are set to offset the client content and draw a border around the client surface |
| 374 | // (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start |
| 375 | // of the client content. |
| 376 | TEST_F(InputSurfacesTest, input_respects_surface_insets) { |
| 377 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 378 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 379 | bgSurface->showAt(100, 100); |
| 380 | |
| 381 | fgSurface->mInputInfo.surfaceInset = 5; |
| 382 | fgSurface->showAt(100, 100); |
| 383 | |
| 384 | injectTap(106, 106); |
| 385 | fgSurface->expectTap(1, 1); |
| 386 | |
| 387 | injectTap(101, 101); |
| 388 | bgSurface->expectTap(1, 1); |
| 389 | } |
| 390 | |
| 391 | // Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463 |
| 392 | TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) { |
| 393 | std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100); |
| 394 | std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100); |
| 395 | parentSurface->showAt(100, 100); |
| 396 | |
| 397 | childSurface->mInputInfo.surfaceInset = 10; |
| 398 | childSurface->showAt(100, 100); |
| 399 | |
| 400 | childSurface->doTransaction([&](auto &t, auto &sc) { |
| 401 | t.setPosition(sc, -5, -5); |
Pablo Gamito | 11dcc22 | 2020-09-12 15:49:39 +0000 | [diff] [blame] | 402 | t.reparent(sc, parentSurface->mSurfaceControl); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 403 | }); |
| 404 | |
| 405 | injectTap(106, 106); |
| 406 | childSurface->expectTap(1, 1); |
| 407 | |
| 408 | injectTap(101, 101); |
| 409 | parentSurface->expectTap(1, 1); |
| 410 | } |
| 411 | |
Arthur Hung | 118b114 | 2019-05-08 21:25:59 +0800 | [diff] [blame] | 412 | // Ensure a surface whose insets are scaled, handles the touch offset correctly. |
| 413 | TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) { |
| 414 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 415 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 416 | bgSurface->showAt(100, 100); |
| 417 | |
| 418 | fgSurface->mInputInfo.surfaceInset = 5; |
| 419 | fgSurface->showAt(100, 100); |
| 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); |
| 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); |
| 452 | injectTap(101, 101); |
| 453 | surface->expectTap(1, 1); |
| 454 | } |
| 455 | |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 456 | // TODO(b/139494112) update tests once we define expected behavior |
| 457 | // Ensure we still send input to the surface regardless of surface visibility changes due to the |
| 458 | // first buffer being submitted or alpha changes. |
| 459 | // Original bug ref: b/120839715 |
| 460 | TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 461 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 462 | std::unique_ptr<InputSurface> bufferSurface = |
| 463 | InputSurface::makeBufferInputSurface(mComposerClient, 100, 100); |
| 464 | |
| 465 | bgSurface->showAt(10, 10); |
| 466 | bufferSurface->showAt(10, 10); |
| 467 | |
| 468 | injectTap(11, 11); |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 469 | bufferSurface->expectTap(1, 1); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 470 | |
| 471 | postBuffer(bufferSurface->mSurfaceControl); |
| 472 | injectTap(11, 11); |
| 473 | bufferSurface->expectTap(1, 1); |
| 474 | } |
| 475 | |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 476 | TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 477 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 478 | std::unique_ptr<InputSurface> bufferSurface = |
| 479 | InputSurface::makeBufferInputSurface(mComposerClient, 100, 100); |
| 480 | postBuffer(bufferSurface->mSurfaceControl); |
| 481 | |
| 482 | bgSurface->showAt(10, 10); |
| 483 | bufferSurface->showAt(10, 10); |
| 484 | |
| 485 | injectTap(11, 11); |
| 486 | bufferSurface->expectTap(1, 1); |
| 487 | |
| 488 | bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); }); |
| 489 | |
| 490 | injectTap(11, 11); |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 491 | bufferSurface->expectTap(1, 1); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 494 | TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 495 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 496 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 497 | |
| 498 | bgSurface->showAt(10, 10); |
| 499 | fgSurface->showAt(10, 10); |
| 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); |
| 516 | containerSurface->showAt(10, 10); |
| 517 | |
| 518 | injectTap(11, 11); |
| 519 | containerSurface->expectTap(1, 1); |
| 520 | |
| 521 | containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); }); |
| 522 | |
| 523 | injectTap(11, 11); |
| 524 | bgSurface->expectTap(1, 1); |
| 525 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 526 | |
Arthur Hung | d20b270 | 2019-01-14 18:16:16 +0800 | [diff] [blame] | 527 | TEST_F(InputSurfacesTest, input_respects_outscreen) { |
| 528 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 529 | surface->showAt(-1, -1); |
| 530 | |
| 531 | injectTap(0, 0); |
| 532 | surface->expectTap(1, 1); |
| 533 | } |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 534 | |
| 535 | TEST_F(InputSurfacesTest, input_ignores_cursor_layer) { |
| 536 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 537 | std::unique_ptr<InputSurface> cursorSurface = |
| 538 | InputSurface::makeCursorInputSurface(mComposerClient, 10, 10); |
| 539 | |
| 540 | surface->showAt(10, 10); |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 541 | cursorSurface->showAt(10, 10); |
| 542 | |
| 543 | injectTap(11, 11); |
| 544 | surface->expectTap(1, 1); |
| 545 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 546 | |
| 547 | TEST_F(InputSurfacesTest, can_be_focused) { |
| 548 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 549 | surface->showAt(100, 100); |
| 550 | surface->requestFocus(); |
| 551 | |
| 552 | surface->assertFocusChange(true); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 553 | } |
chaviw | 44a6d2b | 2020-09-08 17:14:16 -0700 | [diff] [blame] | 554 | |
| 555 | TEST_F(InputSurfacesTest, rotate_surface) { |
| 556 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 557 | surface->showAt(10, 10); |
| 558 | surface->doTransaction([](auto &t, auto &sc) { |
| 559 | t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees |
| 560 | }); |
| 561 | injectTap(8, 11); |
| 562 | surface->expectTap(1, 2); |
| 563 | |
| 564 | surface->doTransaction([](auto &t, auto &sc) { |
| 565 | t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees |
| 566 | }); |
| 567 | injectTap(9, 8); |
| 568 | surface->expectTap(1, 2); |
| 569 | |
| 570 | surface->doTransaction([](auto &t, auto &sc) { |
| 571 | t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees |
| 572 | }); |
| 573 | injectTap(12, 9); |
| 574 | surface->expectTap(1, 2); |
| 575 | } |
| 576 | |
| 577 | TEST_F(InputSurfacesTest, rotate_surface_with_scale) { |
| 578 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 579 | surface->showAt(10, 10); |
| 580 | surface->doTransaction([](auto &t, auto &sc) { |
| 581 | t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees |
| 582 | }); |
| 583 | injectTap(2, 12); |
| 584 | surface->expectTap(1, 2); |
| 585 | |
| 586 | surface->doTransaction([](auto &t, auto &sc) { |
| 587 | t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees |
| 588 | }); |
| 589 | injectTap(8, 2); |
| 590 | surface->expectTap(1, 2); |
| 591 | |
| 592 | surface->doTransaction([](auto &t, auto &sc) { |
| 593 | t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees |
| 594 | }); |
| 595 | injectTap(18, 8); |
| 596 | surface->expectTap(1, 2); |
| 597 | } |
| 598 | |
| 599 | TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) { |
| 600 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 601 | surface->mInputInfo.surfaceInset = 5; |
| 602 | surface->showAt(100, 100); |
| 603 | |
| 604 | surface->doTransaction([](auto &t, auto &sc) { |
| 605 | t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees |
| 606 | }); |
| 607 | injectTap(40, 120); |
| 608 | surface->expectTap(5, 10); |
| 609 | |
| 610 | surface->doTransaction([](auto &t, auto &sc) { |
| 611 | t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees |
| 612 | }); |
| 613 | injectTap(80, 40); |
| 614 | surface->expectTap(5, 10); |
| 615 | |
| 616 | surface->doTransaction([](auto &t, auto &sc) { |
| 617 | t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees |
| 618 | }); |
| 619 | injectTap(160, 80); |
| 620 | surface->expectTap(5, 10); |
| 621 | } |
| 622 | |
chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame^] | 623 | TEST_F(InputSurfacesTest, touch_flag_obscured) { |
| 624 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 625 | surface->showAt(100, 100); |
| 626 | |
| 627 | // Add non touchable window to fully cover touchable window. Window behind gets touch, but |
| 628 | // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED |
| 629 | std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100); |
| 630 | nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE; |
| 631 | nonTouchableSurface->mInputInfo.ownerUid = 22222; |
| 632 | nonTouchableSurface->showAt(100, 100); |
| 633 | |
| 634 | injectTap(190, 199); |
| 635 | surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED); |
| 636 | } |
| 637 | |
| 638 | TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) { |
| 639 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 640 | surface->showAt(100, 100); |
| 641 | |
| 642 | // Add non touchable window to cover touchable window, but parent is cropped to not cover area |
| 643 | // that will be tapped. Window behind gets touch, but with flag |
| 644 | // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED |
| 645 | std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100); |
| 646 | std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100); |
| 647 | nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE; |
| 648 | parentSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE; |
| 649 | nonTouchableSurface->mInputInfo.ownerUid = 22222; |
| 650 | parentSurface->mInputInfo.ownerUid = 22222; |
| 651 | nonTouchableSurface->showAt(0, 0); |
| 652 | parentSurface->showAt(100, 100); |
| 653 | |
| 654 | nonTouchableSurface->doTransaction([&](auto &t, auto &sc) { |
| 655 | t.setCrop_legacy(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50)); |
| 656 | t.reparent(sc, parentSurface->mSurfaceControl); |
| 657 | }); |
| 658 | |
| 659 | injectTap(190, 199); |
| 660 | surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 661 | } |
| 662 | |
| 663 | TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) { |
| 664 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 665 | surface->showAt(100, 100); |
| 666 | |
| 667 | // Add non touchable window to cover touchable window, but parent is cropped to avoid covering |
| 668 | // the touchable window. Window behind gets touch with no obscured flags. |
| 669 | std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100); |
| 670 | std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100); |
| 671 | nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE; |
| 672 | parentSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE; |
| 673 | nonTouchableSurface->mInputInfo.ownerUid = 22222; |
| 674 | parentSurface->mInputInfo.ownerUid = 22222; |
| 675 | nonTouchableSurface->showAt(0, 0); |
| 676 | parentSurface->showAt(50, 50); |
| 677 | |
| 678 | nonTouchableSurface->doTransaction([&](auto &t, auto &sc) { |
| 679 | t.setCrop_legacy(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50)); |
| 680 | t.reparent(sc, parentSurface->mSurfaceControl); |
| 681 | }); |
| 682 | |
| 683 | injectTap(101, 110); |
| 684 | surface->expectTap(1, 10); |
| 685 | } |
| 686 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 687 | } // namespace android::test |