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