| 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 | 9b0d13d | 2022-01-27 22:07:50 +0000 | [diff] [blame] | 27 | #include <android/keycodes.h> |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 28 | #include <android/native_window.h> |
| 29 | |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 30 | #include <binder/Binder.h> |
| 31 | #include <binder/IServiceManager.h> |
| 32 | #include <binder/Parcel.h> |
| 33 | #include <binder/ProcessState.h> |
| 34 | |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 35 | #include <gui/ISurfaceComposer.h> |
| 36 | #include <gui/Surface.h> |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 37 | #include <gui/SurfaceComposerClient.h> |
| 38 | #include <gui/SurfaceControl.h> |
| 39 | |
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 40 | #include <android/os/IInputFlinger.h> |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 41 | #include <input/Input.h> |
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 42 | #include <input/InputTransport.h> |
| 43 | #include <input/InputWindow.h> |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 44 | |
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 45 | #include <ui/DisplayMode.h> |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 46 | #include <ui/Rect.h> |
| 47 | #include <ui/Region.h> |
| 48 | |
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 49 | using android::os::IInputFlinger; |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 50 | |
| chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 51 | using android::hardware::graphics::common::V1_1::BufferUsage; |
| 52 | |
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 53 | namespace android::test { |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 54 | |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 55 | using Transaction = SurfaceComposerClient::Transaction; |
| 56 | |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 57 | sp<IInputFlinger> getInputFlinger() { |
| 58 | sp<IBinder> input(defaultServiceManager()->getService( |
| 59 | String16("inputflinger"))); |
| 60 | if (input == nullptr) { |
| 61 | ALOGE("Failed to link to input service"); |
| 62 | } else { ALOGE("Linked to input"); } |
| 63 | return interface_cast<IInputFlinger>(input); |
| 64 | } |
| 65 | |
| 66 | // We use the top 10 layers as a way to haphazardly place ourselves above anything else. |
| 67 | static const int LAYER_BASE = INT32_MAX - 10; |
| Chris Ye | 6c4243b | 2020-07-22 12:07:12 -0700 | [diff] [blame] | 68 | static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s; |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 69 | |
| 70 | class InputSurface { |
| 71 | public: |
| Linus Tufvesson | 4c0e431 | 2022-03-04 09:32:07 +0000 | [diff] [blame^] | 72 | InputSurface(const sp<SurfaceControl> &sc, int width, int height, bool noInputChannel = false) { |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 73 | mSurfaceControl = sc; |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 74 | |
| 75 | mInputFlinger = getInputFlinger(); |
| Linus Tufvesson | 4c0e431 | 2022-03-04 09:32:07 +0000 | [diff] [blame^] | 76 | if (noInputChannel) { |
| 77 | mInputInfo.inputFeatures = InputWindowInfo::Feature::NO_INPUT_CHANNEL; |
| 78 | } else { |
| 79 | mClientChannel = std::make_shared<InputChannel>(); |
| 80 | mInputFlinger->createInputChannel("testchannels", mClientChannel.get()); |
| 81 | mInputInfo.token = mClientChannel->getConnectionToken(); |
| 82 | mInputConsumer = new InputConsumer(mClientChannel); |
| 83 | } |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 84 | |
| Linus Tufvesson | 4c0e431 | 2022-03-04 09:32:07 +0000 | [diff] [blame^] | 85 | mInputInfo.name = "Test info"; |
| 86 | mInputInfo.dispatchingTimeout = 5s; |
| 87 | mInputInfo.globalScaleFactor = 1.0; |
| 88 | mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL; |
| 89 | mInputInfo.type = InputWindowInfo::Type::BASE_APPLICATION; |
| 90 | mInputInfo.focusable = true; |
| 91 | mInputInfo.hasWallpaper = false; |
| 92 | mInputInfo.paused = false; |
| 93 | // TODO: Fill in from SF? |
| 94 | mInputInfo.ownerPid = 11111; |
| 95 | mInputInfo.ownerUid = 11111; |
| 96 | mInputInfo.displayId = 0; |
| 97 | mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height)); |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 98 | |
| Linus Tufvesson | 4c0e431 | 2022-03-04 09:32:07 +0000 | [diff] [blame^] | 99 | InputApplicationInfo aInfo; |
| 100 | aInfo.token = new BBinder(); |
| 101 | aInfo.name = "Test app info"; |
| 102 | aInfo.dispatchingTimeoutMillis = |
| 103 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
| 104 | mInputInfo.applicationInfo = aInfo; |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 107 | static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc, |
| 108 | int width, int height) { |
| 109 | sp<SurfaceControl> surfaceControl = |
| 110 | scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */, |
| Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 111 | PIXEL_FORMAT_RGBA_8888, |
| 112 | ISurfaceComposerClient::eFXSurfaceEffect); |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 113 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 114 | } |
| 115 | |
| 116 | static std::unique_ptr<InputSurface> makeBufferInputSurface( |
| 117 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 118 | sp<SurfaceControl> surfaceControl = |
| 119 | scc->createSurface(String8("Test Buffer Surface"), width, height, |
| 120 | PIXEL_FORMAT_RGBA_8888, 0 /* flags */); |
| 121 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 122 | } |
| 123 | |
| 124 | static std::unique_ptr<InputSurface> makeContainerInputSurface( |
| 125 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 126 | sp<SurfaceControl> surfaceControl = |
| 127 | scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */, |
| 128 | 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888, |
| 129 | ISurfaceComposerClient::eFXSurfaceContainer); |
| 130 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 131 | } |
| 132 | |
| Linus Tufvesson | 4c0e431 | 2022-03-04 09:32:07 +0000 | [diff] [blame^] | 133 | static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel( |
| 134 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 135 | sp<SurfaceControl> surfaceControl = |
| 136 | scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */, |
| 137 | 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888, |
| 138 | ISurfaceComposerClient::eFXSurfaceContainer); |
| 139 | return std::make_unique<InputSurface>(surfaceControl, width, height, |
| 140 | true /* noInputChannel */); |
| 141 | } |
| 142 | |
| arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 143 | static std::unique_ptr<InputSurface> makeCursorInputSurface( |
| 144 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 145 | sp<SurfaceControl> surfaceControl = |
| 146 | scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */, |
| 147 | 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888, |
| 148 | ISurfaceComposerClient::eCursorWindow); |
| 149 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 150 | } |
| 151 | |
| Vishnu Nair | 9b0d13d | 2022-01-27 22:07:50 +0000 | [diff] [blame] | 152 | InputEvent *consumeEvent(int timeoutMs = 3000) { |
| 153 | waitForEventAvailable(timeoutMs); |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 154 | |
| 155 | InputEvent *ev; |
| 156 | uint32_t seqId; |
| 157 | status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev); |
| 158 | if (consumed != OK) { |
| 159 | return nullptr; |
| 160 | } |
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 161 | status_t status = mInputConsumer->sendFinishedSignal(seqId, true); |
| 162 | EXPECT_EQ(OK, status) << "Could not send finished signal"; |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 163 | return ev; |
| 164 | } |
| 165 | |
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 166 | void assertFocusChange(bool hasFocus) { |
| 167 | InputEvent *ev = consumeEvent(); |
| 168 | ASSERT_NE(ev, nullptr); |
| 169 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, ev->getType()); |
| 170 | FocusEvent *focusEvent = static_cast<FocusEvent *>(ev); |
| 171 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
| 172 | } |
| 173 | |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 174 | void expectTap(int x, int y) { |
| 175 | InputEvent* ev = consumeEvent(); |
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 176 | ASSERT_NE(ev, nullptr); |
| 177 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType()); |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 178 | MotionEvent* mev = static_cast<MotionEvent*>(ev); |
| 179 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction()); |
| 180 | EXPECT_EQ(x, mev->getX(0)); |
| 181 | EXPECT_EQ(y, mev->getY(0)); |
| arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 182 | EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS); |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 183 | |
| 184 | ev = consumeEvent(); |
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 185 | ASSERT_NE(ev, nullptr); |
| 186 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType()); |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 187 | mev = static_cast<MotionEvent*>(ev); |
| 188 | EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction()); |
| arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 189 | EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS); |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| Vishnu Nair | 9b0d13d | 2022-01-27 22:07:50 +0000 | [diff] [blame] | 192 | void expectKey(uint32_t keycode) { |
| 193 | InputEvent *ev = consumeEvent(); |
| 194 | ASSERT_NE(ev, nullptr); |
| 195 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, ev->getType()); |
| 196 | KeyEvent *keyEvent = static_cast<KeyEvent *>(ev); |
| 197 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction()); |
| 198 | EXPECT_EQ(keycode, keyEvent->getKeyCode()); |
| 199 | EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS); |
| 200 | |
| 201 | ev = consumeEvent(); |
| 202 | ASSERT_NE(ev, nullptr); |
| 203 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, ev->getType()); |
| 204 | keyEvent = static_cast<KeyEvent *>(ev); |
| 205 | EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction()); |
| 206 | EXPECT_EQ(keycode, keyEvent->getKeyCode()); |
| 207 | EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS); |
| 208 | } |
| 209 | |
| chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame] | 210 | void expectTapWithFlag(int x, int y, int32_t flags) { |
| 211 | InputEvent *ev = consumeEvent(); |
| 212 | ASSERT_NE(ev, nullptr); |
| 213 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType()); |
| 214 | MotionEvent *mev = static_cast<MotionEvent *>(ev); |
| 215 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction()); |
| 216 | EXPECT_EQ(x, mev->getX(0)); |
| 217 | EXPECT_EQ(y, mev->getY(0)); |
| 218 | EXPECT_EQ(flags, mev->getFlags() & flags); |
| 219 | |
| 220 | ev = consumeEvent(); |
| 221 | ASSERT_NE(ev, nullptr); |
| 222 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, ev->getType()); |
| 223 | mev = static_cast<MotionEvent *>(ev); |
| 224 | EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction()); |
| 225 | EXPECT_EQ(flags, mev->getFlags() & flags); |
| 226 | } |
| 227 | |
| chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 228 | virtual ~InputSurface() { |
| Linus Tufvesson | 4c0e431 | 2022-03-04 09:32:07 +0000 | [diff] [blame^] | 229 | if (mClientChannel) { |
| 230 | mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken()); |
| 231 | } |
| chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 232 | } |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 233 | |
| chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 234 | virtual void doTransaction( |
| 235 | std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)> |
| 236 | transactionBody) { |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 237 | SurfaceComposerClient::Transaction t; |
| 238 | transactionBody(t, mSurfaceControl); |
| 239 | t.apply(true); |
| 240 | } |
| 241 | |
| chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 242 | virtual void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) { |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 243 | SurfaceComposerClient::Transaction t; |
| 244 | t.show(mSurfaceControl); |
| 245 | t.setInputWindowInfo(mSurfaceControl, mInputInfo); |
| 246 | t.setLayer(mSurfaceControl, LAYER_BASE); |
| 247 | t.setPosition(mSurfaceControl, x, y); |
| chaviw | 2571450 | 2021-02-11 10:01:08 -0800 | [diff] [blame] | 248 | t.setCrop(mSurfaceControl, crop); |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 249 | t.setAlpha(mSurfaceControl, 1); |
| 250 | t.apply(true); |
| 251 | } |
| 252 | |
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 253 | void requestFocus() { |
| 254 | SurfaceComposerClient::Transaction t; |
| Vishnu Nair | 9ad0146 | 2021-01-15 12:55:14 -0800 | [diff] [blame] | 255 | FocusRequest request; |
| 256 | request.token = mInputInfo.token; |
| 257 | request.windowName = mInputInfo.name; |
| 258 | request.focusedToken = nullptr; |
| 259 | request.focusedWindowName = ""; |
| 260 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 261 | request.displayId = 0; |
| 262 | t.setFocusedWindow(request); |
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 263 | t.apply(true); |
| 264 | } |
| 265 | |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 266 | private: |
| Vishnu Nair | 9b0d13d | 2022-01-27 22:07:50 +0000 | [diff] [blame] | 267 | void waitForEventAvailable(int timeoutMs) { |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 268 | struct pollfd fd; |
| 269 | |
| 270 | fd.fd = mClientChannel->getFd(); |
| 271 | fd.events = POLLIN; |
| Vishnu Nair | 9b0d13d | 2022-01-27 22:07:50 +0000 | [diff] [blame] | 272 | poll(&fd, 1, timeoutMs); |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 275 | public: |
| 276 | sp<SurfaceControl> mSurfaceControl; |
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 277 | std::shared_ptr<InputChannel> mClientChannel; |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 278 | sp<IInputFlinger> mInputFlinger; |
| 279 | |
| 280 | InputWindowInfo mInputInfo; |
| 281 | |
| 282 | PreallocatedInputEventFactory mInputEventFactory; |
| 283 | InputConsumer* mInputConsumer; |
| 284 | }; |
| 285 | |
| chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 286 | class BlastInputSurface : public InputSurface { |
| 287 | public: |
| 288 | BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width, |
| 289 | int height) |
| 290 | : InputSurface(sc, width, height) { |
| 291 | mParentSurfaceControl = parentSc; |
| 292 | } |
| 293 | |
| 294 | ~BlastInputSurface() = default; |
| 295 | |
| 296 | static std::unique_ptr<BlastInputSurface> makeBlastInputSurface( |
| 297 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 298 | sp<SurfaceControl> parentSc = |
| 299 | scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */, |
| 300 | 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888, |
| 301 | ISurfaceComposerClient::eFXSurfaceContainer); |
| 302 | |
| 303 | sp<SurfaceControl> surfaceControl = |
| 304 | scc->createSurface(String8("Test Buffer Surface"), width, height, |
| 305 | PIXEL_FORMAT_RGBA_8888, |
| 306 | ISurfaceComposerClient::eFXSurfaceBufferState, |
| 307 | parentSc->getHandle()); |
| 308 | return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height); |
| 309 | } |
| 310 | |
| 311 | void doTransaction( |
| 312 | std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)> |
| 313 | transactionBody) override { |
| 314 | SurfaceComposerClient::Transaction t; |
| 315 | transactionBody(t, mParentSurfaceControl); |
| 316 | t.apply(true); |
| 317 | } |
| 318 | |
| 319 | void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override { |
| 320 | SurfaceComposerClient::Transaction t; |
| 321 | t.show(mParentSurfaceControl); |
| 322 | t.setLayer(mParentSurfaceControl, LAYER_BASE); |
| 323 | t.setPosition(mParentSurfaceControl, x, y); |
| 324 | t.setCrop(mParentSurfaceControl, crop); |
| 325 | |
| 326 | t.show(mSurfaceControl); |
| 327 | t.setInputWindowInfo(mSurfaceControl, mInputInfo); |
| 328 | t.setCrop(mSurfaceControl, crop); |
| 329 | t.setAlpha(mSurfaceControl, 1); |
| 330 | t.apply(true); |
| 331 | } |
| 332 | |
| 333 | private: |
| 334 | sp<SurfaceControl> mParentSurfaceControl; |
| 335 | }; |
| 336 | |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 337 | class InputSurfacesTest : public ::testing::Test { |
| 338 | public: |
| 339 | InputSurfacesTest() { |
| 340 | ProcessState::self()->startThreadPool(); |
| 341 | } |
| 342 | |
| 343 | void SetUp() { |
| 344 | mComposerClient = new SurfaceComposerClient; |
| 345 | ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 346 | |
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 347 | const auto display = mComposerClient->getInternalDisplayToken(); |
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 348 | ASSERT_NE(display, nullptr); |
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 349 | |
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 350 | ui::DisplayMode mode; |
| 351 | ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode)); |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 352 | |
| 353 | // After a new buffer is queued, SurfaceFlinger is notified and will |
| 354 | // latch the new buffer on next vsync. Let's heuristically wait for 3 |
| 355 | // vsyncs. |
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 356 | mBufferPostDelay = static_cast<int32_t>(1e6 / mode.refreshRate) * 3; |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void TearDown() { |
| 360 | mComposerClient->dispose(); |
| 361 | } |
| 362 | |
| 363 | std::unique_ptr<InputSurface> makeSurface(int width, int height) { |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 364 | return InputSurface::makeColorInputSurface(mComposerClient, width, height); |
| 365 | } |
| 366 | |
| chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 367 | void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) { |
| 368 | int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN | |
| 369 | BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE; |
| 370 | sp<GraphicBuffer> buffer = |
| 371 | new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test"); |
| 372 | Transaction().setBuffer(layer, buffer).apply(true); |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 373 | usleep(mBufferPostDelay); |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | sp<SurfaceComposerClient> mComposerClient; |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 377 | int32_t mBufferPostDelay; |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 378 | }; |
| 379 | |
| 380 | void injectTap(int x, int y) { |
| 381 | char *buf1, *buf2; |
| 382 | asprintf(&buf1, "%d", x); |
| 383 | asprintf(&buf2, "%d", y); |
| 384 | if (fork() == 0) { |
| 385 | execlp("input", "input", "tap", buf1, buf2, NULL); |
| 386 | } |
| 387 | } |
| 388 | |
| Vishnu Nair | 9b0d13d | 2022-01-27 22:07:50 +0000 | [diff] [blame] | 389 | void injectKey(uint32_t keycode) { |
| 390 | char *buf1; |
| 391 | asprintf(&buf1, "%d", keycode); |
| 392 | if (fork() == 0) { |
| 393 | execlp("input", "input", "keyevent", buf1, NULL); |
| 394 | } |
| 395 | } |
| 396 | |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 397 | TEST_F(InputSurfacesTest, can_receive_input) { |
| 398 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 399 | surface->showAt(100, 100); |
| 400 | |
| 401 | injectTap(101, 101); |
| 402 | |
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 403 | EXPECT_NE(surface->consumeEvent(), nullptr); |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 406 | /** |
| 407 | * Set up two surfaces side-by-side. Tap each surface. |
| 408 | * Next, swap the positions of the two surfaces. Inject tap into the two |
| 409 | * original locations. Ensure that the tap is received by the surfaces in the |
| 410 | * reverse order. |
| 411 | */ |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 412 | TEST_F(InputSurfacesTest, input_respects_positioning) { |
| 413 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 414 | surface->showAt(100, 100); |
| 415 | |
| 416 | std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100); |
| 417 | surface2->showAt(200, 200); |
| 418 | |
| 419 | injectTap(201, 201); |
| 420 | surface2->expectTap(1, 1); |
| 421 | |
| 422 | injectTap(101, 101); |
| 423 | surface->expectTap(1, 1); |
| 424 | |
| 425 | surface2->doTransaction([](auto &t, auto &sc) { |
| 426 | t.setPosition(sc, 100, 100); |
| 427 | }); |
| 428 | surface->doTransaction([](auto &t, auto &sc) { |
| 429 | t.setPosition(sc, 200, 200); |
| 430 | }); |
| 431 | |
| 432 | injectTap(101, 101); |
| 433 | surface2->expectTap(1, 1); |
| 434 | |
| 435 | injectTap(201, 201); |
| 436 | surface->expectTap(1, 1); |
| 437 | } |
| 438 | |
| 439 | TEST_F(InputSurfacesTest, input_respects_layering) { |
| 440 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 441 | std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100); |
| 442 | |
| 443 | surface->showAt(10, 10); |
| 444 | surface2->showAt(10, 10); |
| 445 | |
| 446 | surface->doTransaction([](auto &t, auto &sc) { |
| 447 | t.setLayer(sc, LAYER_BASE + 1); |
| 448 | }); |
| 449 | |
| 450 | injectTap(11, 11); |
| 451 | surface->expectTap(1, 1); |
| 452 | |
| 453 | surface2->doTransaction([](auto &t, auto &sc) { |
| 454 | t.setLayer(sc, LAYER_BASE + 1); |
| 455 | }); |
| 456 | |
| 457 | injectTap(11, 11); |
| 458 | surface2->expectTap(1, 1); |
| 459 | |
| 460 | surface2->doTransaction([](auto &t, auto &sc) { |
| 461 | t.hide(sc); |
| 462 | }); |
| 463 | |
| 464 | injectTap(11, 11); |
| 465 | surface->expectTap(1, 1); |
| 466 | } |
| 467 | |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 468 | // Surface Insets are set to offset the client content and draw a border around the client surface |
| 469 | // (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start |
| 470 | // of the client content. |
| 471 | TEST_F(InputSurfacesTest, input_respects_surface_insets) { |
| 472 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 473 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 474 | bgSurface->showAt(100, 100); |
| 475 | |
| 476 | fgSurface->mInputInfo.surfaceInset = 5; |
| 477 | fgSurface->showAt(100, 100); |
| 478 | |
| 479 | injectTap(106, 106); |
| 480 | fgSurface->expectTap(1, 1); |
| 481 | |
| 482 | injectTap(101, 101); |
| 483 | bgSurface->expectTap(1, 1); |
| 484 | } |
| 485 | |
| 486 | // Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463 |
| 487 | TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) { |
| 488 | std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100); |
| 489 | std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100); |
| 490 | parentSurface->showAt(100, 100); |
| 491 | |
| 492 | childSurface->mInputInfo.surfaceInset = 10; |
| 493 | childSurface->showAt(100, 100); |
| 494 | |
| 495 | childSurface->doTransaction([&](auto &t, auto &sc) { |
| 496 | t.setPosition(sc, -5, -5); |
| Pablo Gamito | 11dcc22 | 2020-09-12 15:49:39 +0000 | [diff] [blame] | 497 | t.reparent(sc, parentSurface->mSurfaceControl); |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 498 | }); |
| 499 | |
| 500 | injectTap(106, 106); |
| 501 | childSurface->expectTap(1, 1); |
| 502 | |
| 503 | injectTap(101, 101); |
| 504 | parentSurface->expectTap(1, 1); |
| 505 | } |
| 506 | |
| Arthur Hung | 118b114 | 2019-05-08 21:25:59 +0800 | [diff] [blame] | 507 | // Ensure a surface whose insets are scaled, handles the touch offset correctly. |
| 508 | TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) { |
| 509 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 510 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 511 | bgSurface->showAt(100, 100); |
| 512 | |
| 513 | fgSurface->mInputInfo.surfaceInset = 5; |
| 514 | fgSurface->showAt(100, 100); |
| 515 | |
| 516 | fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); }); |
| 517 | |
| 518 | // expect = touch / scale - inset |
| 519 | injectTap(112, 124); |
| 520 | fgSurface->expectTap(1, 1); |
| 521 | |
| 522 | injectTap(101, 101); |
| 523 | bgSurface->expectTap(1, 1); |
| 524 | } |
| 525 | |
| Ady Abraham | 282f1d7 | 2019-07-24 18:05:56 -0700 | [diff] [blame] | 526 | TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) { |
| 527 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 528 | // In case we pass the very big inset without any checking. |
| 529 | fgSurface->mInputInfo.surfaceInset = INT32_MAX; |
| 530 | fgSurface->showAt(100, 100); |
| 531 | |
| 532 | fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); }); |
| 533 | |
| 534 | // expect no crash for overflow, and inset size to be clamped to surface size |
| 535 | injectTap(202, 202); |
| 536 | fgSurface->expectTap(1, 1); |
| 537 | } |
| 538 | |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 539 | // Ensure we ignore transparent region when getting screen bounds when positioning input frame. |
| 540 | TEST_F(InputSurfacesTest, input_ignores_transparent_region) { |
| 541 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 542 | surface->doTransaction([](auto &t, auto &sc) { |
| 543 | Region transparentRegion(Rect(0, 0, 10, 10)); |
| 544 | t.setTransparentRegionHint(sc, transparentRegion); |
| 545 | }); |
| 546 | surface->showAt(100, 100); |
| 547 | injectTap(101, 101); |
| 548 | surface->expectTap(1, 1); |
| 549 | } |
| 550 | |
| Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 551 | // TODO(b/139494112) update tests once we define expected behavior |
| 552 | // Ensure we still send input to the surface regardless of surface visibility changes due to the |
| 553 | // first buffer being submitted or alpha changes. |
| 554 | // Original bug ref: b/120839715 |
| 555 | TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) { |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 556 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 557 | std::unique_ptr<BlastInputSurface> bufferSurface = |
| 558 | BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100); |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 559 | |
| 560 | bgSurface->showAt(10, 10); |
| 561 | bufferSurface->showAt(10, 10); |
| 562 | |
| 563 | injectTap(11, 11); |
| Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 564 | bufferSurface->expectTap(1, 1); |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 565 | |
| chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 566 | postBuffer(bufferSurface->mSurfaceControl, 100, 100); |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 567 | injectTap(11, 11); |
| 568 | bufferSurface->expectTap(1, 1); |
| 569 | } |
| 570 | |
| Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 571 | TEST_F(InputSurfacesTest, input_ignores_buffer_layer_alpha) { |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 572 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 573 | std::unique_ptr<BlastInputSurface> bufferSurface = |
| 574 | BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100); |
| 575 | postBuffer(bufferSurface->mSurfaceControl, 100, 100); |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 576 | |
| 577 | bgSurface->showAt(10, 10); |
| 578 | bufferSurface->showAt(10, 10); |
| 579 | |
| 580 | injectTap(11, 11); |
| 581 | bufferSurface->expectTap(1, 1); |
| 582 | |
| 583 | bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); }); |
| 584 | |
| 585 | injectTap(11, 11); |
| Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 586 | bufferSurface->expectTap(1, 1); |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 589 | TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) { |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 590 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 591 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 592 | |
| 593 | bgSurface->showAt(10, 10); |
| 594 | fgSurface->showAt(10, 10); |
| 595 | |
| 596 | injectTap(11, 11); |
| 597 | fgSurface->expectTap(1, 1); |
| 598 | |
| 599 | fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); }); |
| 600 | |
| 601 | injectTap(11, 11); |
| Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 602 | fgSurface->expectTap(1, 1); |
| Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) { |
| 606 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 607 | std::unique_ptr<InputSurface> containerSurface = |
| 608 | InputSurface::makeContainerInputSurface(mComposerClient, 100, 100); |
| 609 | |
| 610 | bgSurface->showAt(10, 10); |
| 611 | containerSurface->showAt(10, 10); |
| 612 | |
| 613 | injectTap(11, 11); |
| 614 | containerSurface->expectTap(1, 1); |
| 615 | |
| 616 | containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); }); |
| 617 | |
| 618 | injectTap(11, 11); |
| 619 | bgSurface->expectTap(1, 1); |
| 620 | } |
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 621 | |
| Arthur Hung | d20b270 | 2019-01-14 18:16:16 +0800 | [diff] [blame] | 622 | TEST_F(InputSurfacesTest, input_respects_outscreen) { |
| 623 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 624 | surface->showAt(-1, -1); |
| 625 | |
| 626 | injectTap(0, 0); |
| 627 | surface->expectTap(1, 1); |
| 628 | } |
| arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 629 | |
| 630 | TEST_F(InputSurfacesTest, input_ignores_cursor_layer) { |
| 631 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 632 | std::unique_ptr<InputSurface> cursorSurface = |
| 633 | InputSurface::makeCursorInputSurface(mComposerClient, 10, 10); |
| 634 | |
| 635 | surface->showAt(10, 10); |
| arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 636 | cursorSurface->showAt(10, 10); |
| 637 | |
| 638 | injectTap(11, 11); |
| 639 | surface->expectTap(1, 1); |
| 640 | } |
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 641 | |
| Vishnu Nair | 9b0d13d | 2022-01-27 22:07:50 +0000 | [diff] [blame] | 642 | TEST_F(InputSurfacesTest, drop_input_policy) { |
| 643 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 644 | surface->doTransaction( |
| 645 | [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); }); |
| 646 | surface->showAt(100, 100); |
| 647 | surface->requestFocus(); |
| 648 | surface->assertFocusChange(true); |
| 649 | |
| 650 | injectTap(101, 101); |
| 651 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
| 652 | |
| 653 | injectKey(AKEYCODE_V); |
| 654 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
| 655 | } |
| 656 | |
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 657 | TEST_F(InputSurfacesTest, can_be_focused) { |
| 658 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 659 | surface->showAt(100, 100); |
| 660 | surface->requestFocus(); |
| 661 | |
| 662 | surface->assertFocusChange(true); |
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 663 | } |
| chaviw | 44a6d2b | 2020-09-08 17:14:16 -0700 | [diff] [blame] | 664 | |
| 665 | TEST_F(InputSurfacesTest, rotate_surface) { |
| 666 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 667 | surface->showAt(10, 10); |
| 668 | surface->doTransaction([](auto &t, auto &sc) { |
| 669 | t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees |
| 670 | }); |
| 671 | injectTap(8, 11); |
| 672 | surface->expectTap(1, 2); |
| 673 | |
| 674 | surface->doTransaction([](auto &t, auto &sc) { |
| 675 | t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees |
| 676 | }); |
| 677 | injectTap(9, 8); |
| 678 | surface->expectTap(1, 2); |
| 679 | |
| 680 | surface->doTransaction([](auto &t, auto &sc) { |
| 681 | t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees |
| 682 | }); |
| 683 | injectTap(12, 9); |
| 684 | surface->expectTap(1, 2); |
| 685 | } |
| 686 | |
| 687 | TEST_F(InputSurfacesTest, rotate_surface_with_scale) { |
| 688 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 689 | surface->showAt(10, 10); |
| 690 | surface->doTransaction([](auto &t, auto &sc) { |
| 691 | t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees |
| 692 | }); |
| 693 | injectTap(2, 12); |
| 694 | surface->expectTap(1, 2); |
| 695 | |
| 696 | surface->doTransaction([](auto &t, auto &sc) { |
| 697 | t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees |
| 698 | }); |
| 699 | injectTap(8, 2); |
| 700 | surface->expectTap(1, 2); |
| 701 | |
| 702 | surface->doTransaction([](auto &t, auto &sc) { |
| 703 | t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees |
| 704 | }); |
| 705 | injectTap(18, 8); |
| 706 | surface->expectTap(1, 2); |
| 707 | } |
| 708 | |
| 709 | TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) { |
| 710 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 711 | surface->mInputInfo.surfaceInset = 5; |
| 712 | surface->showAt(100, 100); |
| 713 | |
| 714 | surface->doTransaction([](auto &t, auto &sc) { |
| 715 | t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees |
| 716 | }); |
| 717 | injectTap(40, 120); |
| 718 | surface->expectTap(5, 10); |
| 719 | |
| 720 | surface->doTransaction([](auto &t, auto &sc) { |
| 721 | t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees |
| 722 | }); |
| 723 | injectTap(80, 40); |
| 724 | surface->expectTap(5, 10); |
| 725 | |
| 726 | surface->doTransaction([](auto &t, auto &sc) { |
| 727 | t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees |
| 728 | }); |
| 729 | injectTap(160, 80); |
| 730 | surface->expectTap(5, 10); |
| 731 | } |
| 732 | |
| chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame] | 733 | TEST_F(InputSurfacesTest, touch_flag_obscured) { |
| 734 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 735 | surface->showAt(100, 100); |
| 736 | |
| 737 | // Add non touchable window to fully cover touchable window. Window behind gets touch, but |
| 738 | // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED |
| 739 | std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100); |
| 740 | nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE; |
| 741 | nonTouchableSurface->mInputInfo.ownerUid = 22222; |
| Bernardo Rufino | 602ef71 | 2020-12-21 11:02:18 +0000 | [diff] [blame] | 742 | // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by |
| 743 | // the default obscured/untrusted touch filter introduced in S. |
| 744 | nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW; |
| chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame] | 745 | nonTouchableSurface->showAt(100, 100); |
| 746 | |
| 747 | injectTap(190, 199); |
| 748 | surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED); |
| 749 | } |
| 750 | |
| 751 | TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) { |
| 752 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 753 | surface->showAt(100, 100); |
| 754 | |
| 755 | // Add non touchable window to cover touchable window, but parent is cropped to not cover area |
| 756 | // that will be tapped. Window behind gets touch, but with flag |
| 757 | // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED |
| 758 | std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100); |
| 759 | std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100); |
| 760 | nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE; |
| 761 | parentSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE; |
| 762 | nonTouchableSurface->mInputInfo.ownerUid = 22222; |
| 763 | parentSurface->mInputInfo.ownerUid = 22222; |
| 764 | nonTouchableSurface->showAt(0, 0); |
| 765 | parentSurface->showAt(100, 100); |
| 766 | |
| 767 | nonTouchableSurface->doTransaction([&](auto &t, auto &sc) { |
| chaviw | 2571450 | 2021-02-11 10:01:08 -0800 | [diff] [blame] | 768 | t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50)); |
| chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame] | 769 | t.reparent(sc, parentSurface->mSurfaceControl); |
| 770 | }); |
| 771 | |
| 772 | injectTap(190, 199); |
| 773 | surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 774 | } |
| 775 | |
| 776 | TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) { |
| 777 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 778 | surface->showAt(100, 100); |
| 779 | |
| 780 | // Add non touchable window to cover touchable window, but parent is cropped to avoid covering |
| 781 | // the touchable window. Window behind gets touch with no obscured flags. |
| 782 | std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100); |
| 783 | std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100); |
| 784 | nonTouchableSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE; |
| 785 | parentSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE; |
| 786 | nonTouchableSurface->mInputInfo.ownerUid = 22222; |
| 787 | parentSurface->mInputInfo.ownerUid = 22222; |
| 788 | nonTouchableSurface->showAt(0, 0); |
| 789 | parentSurface->showAt(50, 50); |
| 790 | |
| 791 | nonTouchableSurface->doTransaction([&](auto &t, auto &sc) { |
| chaviw | 2571450 | 2021-02-11 10:01:08 -0800 | [diff] [blame] | 792 | t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50)); |
| chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame] | 793 | t.reparent(sc, parentSurface->mSurfaceControl); |
| 794 | }); |
| 795 | |
| 796 | injectTap(101, 110); |
| 797 | surface->expectTap(1, 10); |
| 798 | } |
| 799 | |
| chaviw | 7e72caf | 2020-12-02 16:50:43 -0800 | [diff] [blame] | 800 | TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) { |
| 801 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 802 | |
| 803 | std::unique_ptr<InputSurface> bufferSurface = |
| 804 | InputSurface::makeBufferInputSurface(mComposerClient, 0, 0); |
| 805 | bufferSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE; |
| 806 | bufferSurface->mInputInfo.ownerUid = 22222; |
| 807 | |
| 808 | surface->showAt(10, 10); |
| 809 | bufferSurface->showAt(50, 50, Rect::EMPTY_RECT); |
| 810 | |
| 811 | injectTap(11, 11); |
| 812 | surface->expectTap(1, 1); |
| 813 | } |
| 814 | |
| 815 | TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) { |
| 816 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 817 | |
| chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 818 | std::unique_ptr<BlastInputSurface> bufferSurface = |
| 819 | BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0); |
| chaviw | 7e72caf | 2020-12-02 16:50:43 -0800 | [diff] [blame] | 820 | bufferSurface->mInputInfo.flags = InputWindowInfo::Flag::NOT_TOUCHABLE; |
| 821 | bufferSurface->mInputInfo.ownerUid = 22222; |
| 822 | |
| 823 | surface->showAt(10, 10); |
| 824 | bufferSurface->showAt(50, 50, Rect::EMPTY_RECT); |
| 825 | |
| 826 | injectTap(11, 11); |
| 827 | surface->expectTap(1, 1); |
| 828 | } |
| 829 | |
| Linus Tufvesson | 4c0e431 | 2022-03-04 09:32:07 +0000 | [diff] [blame^] | 830 | TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) { |
| 831 | std::unique_ptr<InputSurface> parent = makeSurface(100, 100); |
| 832 | |
| 833 | parent->showAt(100, 100); |
| 834 | injectTap(101, 101); |
| 835 | parent->expectTap(1, 1); |
| 836 | |
| 837 | std::unique_ptr<InputSurface> childContainerSurface = |
| 838 | InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100); |
| 839 | childContainerSurface->showAt(0, 0); |
| 840 | childContainerSurface->doTransaction( |
| 841 | [&](auto &t, auto &sc) { t.reparent(sc, parent->mSurfaceControl); }); |
| 842 | injectTap(101, 101); |
| 843 | |
| 844 | EXPECT_EQ(parent->consumeEvent(100), nullptr); |
| 845 | } |
| 846 | |
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 847 | } // namespace android::test |