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