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