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 | |
Patrick Williams | 0a4981a | 2023-07-28 15:08:52 -0500 | [diff] [blame] | 27 | #include <android/gui/BnWindowInfosReportedListener.h> |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 28 | #include <android/keycodes.h> |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 29 | #include <android/native_window.h> |
| 30 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 31 | #include <binder/Binder.h> |
| 32 | #include <binder/IServiceManager.h> |
| 33 | #include <binder/Parcel.h> |
| 34 | #include <binder/ProcessState.h> |
| 35 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 36 | #include <gui/ISurfaceComposer.h> |
| 37 | #include <gui/Surface.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 38 | #include <gui/SurfaceComposerClient.h> |
| 39 | #include <gui/SurfaceControl.h> |
| 40 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 41 | #include <android/os/IInputFlinger.h> |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 42 | #include <gui/WindowInfo.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 43 | #include <input/Input.h> |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 44 | #include <input/InputTransport.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 45 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 46 | #include <ui/DisplayMode.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 47 | #include <ui/Rect.h> |
| 48 | #include <ui/Region.h> |
| 49 | |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 50 | #include <private/android_filesystem_config.h> |
| 51 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 52 | using android::os::IInputFlinger; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 53 | |
chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 54 | using android::hardware::graphics::common::V1_1::BufferUsage; |
| 55 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 56 | using android::gui::FocusRequest; |
| 57 | using android::gui::InputApplicationInfo; |
| 58 | using android::gui::TouchOcclusionMode; |
| 59 | using android::gui::WindowInfo; |
| 60 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 61 | namespace android::test { |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 62 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 63 | using Transaction = SurfaceComposerClient::Transaction; |
| 64 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 65 | sp<IInputFlinger> getInputFlinger() { |
| 66 | sp<IBinder> input(defaultServiceManager()->getService( |
| 67 | String16("inputflinger"))); |
| 68 | if (input == nullptr) { |
| 69 | ALOGE("Failed to link to input service"); |
| 70 | } else { ALOGE("Linked to input"); } |
| 71 | return interface_cast<IInputFlinger>(input); |
| 72 | } |
| 73 | |
| 74 | // We use the top 10 layers as a way to haphazardly place ourselves above anything else. |
| 75 | static const int LAYER_BASE = INT32_MAX - 10; |
Chris Ye | 6c4243b | 2020-07-22 12:07:12 -0700 | [diff] [blame] | 76 | static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 77 | |
Patrick Williams | 0a4981a | 2023-07-28 15:08:52 -0500 | [diff] [blame] | 78 | class SynchronousWindowInfosReportedListener : public gui::BnWindowInfosReportedListener { |
| 79 | public: |
| 80 | binder::Status onWindowInfosReported() override { |
| 81 | std::lock_guard<std::mutex> lock{mMutex}; |
| 82 | mWindowInfosReported = true; |
| 83 | mConditionVariable.notify_one(); |
| 84 | return binder::Status::ok(); |
| 85 | } |
| 86 | |
| 87 | void wait() { |
| 88 | std::unique_lock<std::mutex> lock{mMutex}; |
| 89 | mConditionVariable.wait(lock, [&] { return mWindowInfosReported; }); |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | std::mutex mMutex; |
| 94 | std::condition_variable mConditionVariable; |
| 95 | bool mWindowInfosReported{false}; |
| 96 | }; |
| 97 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 98 | class InputSurface { |
| 99 | public: |
Linus Tufvesson | a185882 | 2022-03-04 09:32:07 +0000 | [diff] [blame] | 100 | InputSurface(const sp<SurfaceControl> &sc, int width, int height, bool noInputChannel = false) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 101 | mSurfaceControl = sc; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 102 | |
| 103 | mInputFlinger = getInputFlinger(); |
Linus Tufvesson | a185882 | 2022-03-04 09:32:07 +0000 | [diff] [blame] | 104 | if (noInputChannel) { |
| 105 | mInputInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, true); |
| 106 | } else { |
| 107 | mClientChannel = std::make_shared<InputChannel>(); |
| 108 | mInputFlinger->createInputChannel("testchannels", mClientChannel.get()); |
| 109 | mInputInfo.token = mClientChannel->getConnectionToken(); |
| 110 | mInputConsumer = new InputConsumer(mClientChannel); |
| 111 | } |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 112 | |
Linus Tufvesson | a185882 | 2022-03-04 09:32:07 +0000 | [diff] [blame] | 113 | mInputInfo.name = "Test info"; |
| 114 | mInputInfo.dispatchingTimeout = 5s; |
| 115 | mInputInfo.globalScaleFactor = 1.0; |
| 116 | mInputInfo.touchableRegion.orSelf(Rect(0, 0, width, height)); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 117 | |
Linus Tufvesson | a185882 | 2022-03-04 09:32:07 +0000 | [diff] [blame] | 118 | InputApplicationInfo aInfo; |
| 119 | aInfo.token = new BBinder(); |
| 120 | aInfo.name = "Test app info"; |
| 121 | aInfo.dispatchingTimeoutMillis = |
| 122 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
| 123 | mInputInfo.applicationInfo = aInfo; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 126 | static std::unique_ptr<InputSurface> makeColorInputSurface(const sp<SurfaceComposerClient> &scc, |
| 127 | int width, int height) { |
| 128 | sp<SurfaceControl> surfaceControl = |
| 129 | scc->createSurface(String8("Test Surface"), 0 /* bufHeight */, 0 /* bufWidth */, |
Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 130 | PIXEL_FORMAT_RGBA_8888, |
| 131 | ISurfaceComposerClient::eFXSurfaceEffect); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 132 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 133 | } |
| 134 | |
| 135 | static std::unique_ptr<InputSurface> makeBufferInputSurface( |
| 136 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 137 | sp<SurfaceControl> surfaceControl = |
| 138 | scc->createSurface(String8("Test Buffer Surface"), width, height, |
| 139 | PIXEL_FORMAT_RGBA_8888, 0 /* flags */); |
| 140 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 141 | } |
| 142 | |
| 143 | static std::unique_ptr<InputSurface> makeContainerInputSurface( |
| 144 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 145 | sp<SurfaceControl> surfaceControl = |
| 146 | scc->createSurface(String8("Test Container Surface"), 0 /* bufHeight */, |
| 147 | 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888, |
| 148 | ISurfaceComposerClient::eFXSurfaceContainer); |
| 149 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 150 | } |
| 151 | |
Linus Tufvesson | a185882 | 2022-03-04 09:32:07 +0000 | [diff] [blame] | 152 | static std::unique_ptr<InputSurface> makeContainerInputSurfaceNoInputChannel( |
| 153 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 154 | sp<SurfaceControl> surfaceControl = |
| 155 | scc->createSurface(String8("Test Container Surface"), 100 /* height */, |
| 156 | 100 /* width */, PIXEL_FORMAT_RGBA_8888, |
| 157 | ISurfaceComposerClient::eFXSurfaceContainer); |
| 158 | return std::make_unique<InputSurface>(surfaceControl, width, height, |
| 159 | true /* noInputChannel */); |
| 160 | } |
| 161 | |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 162 | static std::unique_ptr<InputSurface> makeCursorInputSurface( |
| 163 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 164 | sp<SurfaceControl> surfaceControl = |
| 165 | scc->createSurface(String8("Test Cursor Surface"), 0 /* bufHeight */, |
| 166 | 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888, |
| 167 | ISurfaceComposerClient::eCursorWindow); |
| 168 | return std::make_unique<InputSurface>(surfaceControl, width, height); |
| 169 | } |
| 170 | |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 171 | InputEvent *consumeEvent(int timeoutMs = 3000) { |
| 172 | waitForEventAvailable(timeoutMs); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 173 | |
| 174 | InputEvent *ev; |
| 175 | uint32_t seqId; |
| 176 | status_t consumed = mInputConsumer->consume(&mInputEventFactory, true, -1, &seqId, &ev); |
| 177 | if (consumed != OK) { |
| 178 | return nullptr; |
| 179 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 180 | status_t status = mInputConsumer->sendFinishedSignal(seqId, true); |
| 181 | EXPECT_EQ(OK, status) << "Could not send finished signal"; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 182 | return ev; |
| 183 | } |
| 184 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 185 | void assertFocusChange(bool hasFocus) { |
| 186 | InputEvent *ev = consumeEvent(); |
| 187 | ASSERT_NE(ev, nullptr); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 188 | ASSERT_EQ(InputEventType::FOCUS, ev->getType()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 189 | FocusEvent *focusEvent = static_cast<FocusEvent *>(ev); |
| 190 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
| 191 | } |
| 192 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 193 | void expectTap(int x, int y) { |
| 194 | InputEvent* ev = consumeEvent(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 195 | ASSERT_NE(ev, nullptr); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 196 | ASSERT_EQ(InputEventType::MOTION, ev->getType()); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 197 | MotionEvent* mev = static_cast<MotionEvent*>(ev); |
| 198 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction()); |
| 199 | EXPECT_EQ(x, mev->getX(0)); |
| 200 | EXPECT_EQ(y, mev->getY(0)); |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 201 | EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 202 | |
| 203 | ev = consumeEvent(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 204 | ASSERT_NE(ev, nullptr); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 205 | ASSERT_EQ(InputEventType::MOTION, ev->getType()); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 206 | mev = static_cast<MotionEvent*>(ev); |
| 207 | EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction()); |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 208 | EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 209 | } |
| 210 | |
chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame] | 211 | void expectTapWithFlag(int x, int y, int32_t flags) { |
| 212 | InputEvent *ev = consumeEvent(); |
| 213 | ASSERT_NE(ev, nullptr); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 214 | ASSERT_EQ(InputEventType::MOTION, ev->getType()); |
chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame] | 215 | MotionEvent *mev = static_cast<MotionEvent *>(ev); |
| 216 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction()); |
| 217 | EXPECT_EQ(x, mev->getX(0)); |
| 218 | EXPECT_EQ(y, mev->getY(0)); |
| 219 | EXPECT_EQ(flags, mev->getFlags() & flags); |
| 220 | |
| 221 | ev = consumeEvent(); |
| 222 | ASSERT_NE(ev, nullptr); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 223 | ASSERT_EQ(InputEventType::MOTION, ev->getType()); |
chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame] | 224 | mev = static_cast<MotionEvent *>(ev); |
| 225 | EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction()); |
| 226 | EXPECT_EQ(flags, mev->getFlags() & flags); |
| 227 | } |
| 228 | |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 229 | void expectTapInDisplayCoordinates(int displayX, int displayY) { |
| 230 | InputEvent *ev = consumeEvent(); |
| 231 | ASSERT_NE(ev, nullptr); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 232 | ASSERT_EQ(InputEventType::MOTION, ev->getType()); |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 233 | MotionEvent *mev = static_cast<MotionEvent *>(ev); |
| 234 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, mev->getAction()); |
| 235 | const PointerCoords &coords = *mev->getRawPointerCoords(0 /*pointerIndex*/); |
| 236 | EXPECT_EQ(displayX, coords.getX()); |
| 237 | EXPECT_EQ(displayY, coords.getY()); |
| 238 | EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS); |
| 239 | |
| 240 | ev = consumeEvent(); |
| 241 | ASSERT_NE(ev, nullptr); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 242 | ASSERT_EQ(InputEventType::MOTION, ev->getType()); |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 243 | mev = static_cast<MotionEvent *>(ev); |
| 244 | EXPECT_EQ(AMOTION_EVENT_ACTION_UP, mev->getAction()); |
| 245 | EXPECT_EQ(0, mev->getFlags() & VERIFIED_MOTION_EVENT_FLAGS); |
| 246 | } |
| 247 | |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 248 | void expectKey(uint32_t keycode) { |
| 249 | InputEvent *ev = consumeEvent(); |
| 250 | ASSERT_NE(ev, nullptr); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 251 | ASSERT_EQ(InputEventType::KEY, ev->getType()); |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 252 | KeyEvent *keyEvent = static_cast<KeyEvent *>(ev); |
| 253 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, keyEvent->getAction()); |
| 254 | EXPECT_EQ(keycode, keyEvent->getKeyCode()); |
| 255 | EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS); |
| 256 | |
| 257 | ev = consumeEvent(); |
| 258 | ASSERT_NE(ev, nullptr); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 259 | ASSERT_EQ(InputEventType::KEY, ev->getType()); |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 260 | keyEvent = static_cast<KeyEvent *>(ev); |
| 261 | EXPECT_EQ(AMOTION_EVENT_ACTION_UP, keyEvent->getAction()); |
| 262 | EXPECT_EQ(keycode, keyEvent->getKeyCode()); |
| 263 | EXPECT_EQ(0, keyEvent->getFlags() & VERIFIED_KEY_EVENT_FLAGS); |
| 264 | } |
| 265 | |
chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 266 | virtual ~InputSurface() { |
Linus Tufvesson | a185882 | 2022-03-04 09:32:07 +0000 | [diff] [blame] | 267 | if (mClientChannel) { |
| 268 | mInputFlinger->removeInputChannel(mClientChannel->getConnectionToken()); |
| 269 | } |
chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 270 | } |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 271 | |
chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 272 | virtual void doTransaction( |
| 273 | std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)> |
| 274 | transactionBody) { |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 275 | SurfaceComposerClient::Transaction t; |
| 276 | transactionBody(t, mSurfaceControl); |
| 277 | t.apply(true); |
| 278 | } |
| 279 | |
chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 280 | 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] | 281 | SurfaceComposerClient::Transaction t; |
| 282 | t.show(mSurfaceControl); |
| 283 | t.setInputWindowInfo(mSurfaceControl, mInputInfo); |
| 284 | t.setLayer(mSurfaceControl, LAYER_BASE); |
| 285 | t.setPosition(mSurfaceControl, x, y); |
chaviw | 2571450 | 2021-02-11 10:01:08 -0800 | [diff] [blame] | 286 | t.setCrop(mSurfaceControl, crop); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 287 | t.setAlpha(mSurfaceControl, 1); |
Patrick Williams | 0a4981a | 2023-07-28 15:08:52 -0500 | [diff] [blame] | 288 | auto reportedListener = sp<SynchronousWindowInfosReportedListener>::make(); |
| 289 | t.addWindowInfosReportedListener(reportedListener); |
| 290 | t.apply(); |
| 291 | reportedListener->wait(); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 294 | void requestFocus(int displayId = ADISPLAY_ID_DEFAULT) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 295 | SurfaceComposerClient::Transaction t; |
Vishnu Nair | 9ad0146 | 2021-01-15 12:55:14 -0800 | [diff] [blame] | 296 | FocusRequest request; |
| 297 | request.token = mInputInfo.token; |
| 298 | request.windowName = mInputInfo.name; |
Vishnu Nair | 9ad0146 | 2021-01-15 12:55:14 -0800 | [diff] [blame] | 299 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 300 | request.displayId = displayId; |
Vishnu Nair | 9ad0146 | 2021-01-15 12:55:14 -0800 | [diff] [blame] | 301 | t.setFocusedWindow(request); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 302 | t.apply(true); |
| 303 | } |
| 304 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 305 | private: |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 306 | void waitForEventAvailable(int timeoutMs) { |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 307 | struct pollfd fd; |
| 308 | |
| 309 | fd.fd = mClientChannel->getFd(); |
| 310 | fd.events = POLLIN; |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 311 | poll(&fd, 1, timeoutMs); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 314 | public: |
| 315 | sp<SurfaceControl> mSurfaceControl; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 316 | std::shared_ptr<InputChannel> mClientChannel; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 317 | sp<IInputFlinger> mInputFlinger; |
| 318 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 319 | WindowInfo mInputInfo; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 320 | |
| 321 | PreallocatedInputEventFactory mInputEventFactory; |
| 322 | InputConsumer* mInputConsumer; |
| 323 | }; |
| 324 | |
chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 325 | class BlastInputSurface : public InputSurface { |
| 326 | public: |
| 327 | BlastInputSurface(const sp<SurfaceControl> &sc, const sp<SurfaceControl> &parentSc, int width, |
| 328 | int height) |
| 329 | : InputSurface(sc, width, height) { |
| 330 | mParentSurfaceControl = parentSc; |
| 331 | } |
| 332 | |
| 333 | ~BlastInputSurface() = default; |
| 334 | |
| 335 | static std::unique_ptr<BlastInputSurface> makeBlastInputSurface( |
| 336 | const sp<SurfaceComposerClient> &scc, int width, int height) { |
| 337 | sp<SurfaceControl> parentSc = |
| 338 | scc->createSurface(String8("Test Parent Surface"), 0 /* bufHeight */, |
| 339 | 0 /* bufWidth */, PIXEL_FORMAT_RGBA_8888, |
| 340 | ISurfaceComposerClient::eFXSurfaceContainer); |
| 341 | |
| 342 | sp<SurfaceControl> surfaceControl = |
| 343 | scc->createSurface(String8("Test Buffer Surface"), width, height, |
| 344 | PIXEL_FORMAT_RGBA_8888, |
| 345 | ISurfaceComposerClient::eFXSurfaceBufferState, |
| 346 | parentSc->getHandle()); |
| 347 | return std::make_unique<BlastInputSurface>(surfaceControl, parentSc, width, height); |
| 348 | } |
| 349 | |
| 350 | void doTransaction( |
| 351 | std::function<void(SurfaceComposerClient::Transaction &, const sp<SurfaceControl> &)> |
| 352 | transactionBody) override { |
| 353 | SurfaceComposerClient::Transaction t; |
| 354 | transactionBody(t, mParentSurfaceControl); |
| 355 | t.apply(true); |
| 356 | } |
| 357 | |
| 358 | void showAt(int x, int y, Rect crop = Rect(0, 0, 100, 100)) override { |
| 359 | SurfaceComposerClient::Transaction t; |
| 360 | t.show(mParentSurfaceControl); |
| 361 | t.setLayer(mParentSurfaceControl, LAYER_BASE); |
| 362 | t.setPosition(mParentSurfaceControl, x, y); |
| 363 | t.setCrop(mParentSurfaceControl, crop); |
| 364 | |
| 365 | t.show(mSurfaceControl); |
| 366 | t.setInputWindowInfo(mSurfaceControl, mInputInfo); |
| 367 | t.setCrop(mSurfaceControl, crop); |
| 368 | t.setAlpha(mSurfaceControl, 1); |
| 369 | t.apply(true); |
| 370 | } |
| 371 | |
| 372 | private: |
| 373 | sp<SurfaceControl> mParentSurfaceControl; |
| 374 | }; |
| 375 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 376 | class InputSurfacesTest : public ::testing::Test { |
| 377 | public: |
| 378 | InputSurfacesTest() { |
| 379 | ProcessState::self()->startThreadPool(); |
| 380 | } |
| 381 | |
| 382 | void SetUp() { |
| 383 | mComposerClient = new SurfaceComposerClient; |
| 384 | ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 385 | const auto ids = SurfaceComposerClient::getPhysicalDisplayIds(); |
| 386 | ASSERT_FALSE(ids.empty()); |
| 387 | // display 0 is picked for now, can extend to support all displays if needed |
| 388 | const auto display = SurfaceComposerClient::getPhysicalDisplayToken(ids.front()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 389 | ASSERT_NE(display, nullptr); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 390 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 391 | ui::DisplayMode mode; |
| 392 | ASSERT_EQ(NO_ERROR, mComposerClient->getActiveDisplayMode(display, &mode)); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 393 | |
| 394 | // After a new buffer is queued, SurfaceFlinger is notified and will |
| 395 | // latch the new buffer on next vsync. Let's heuristically wait for 3 |
| 396 | // vsyncs. |
Alec Mouri | 55e3103 | 2023-10-02 20:34:18 +0000 | [diff] [blame] | 397 | mBufferPostDelay = static_cast<int32_t>(1e6 / mode.peakRefreshRate) * 3; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void TearDown() { |
| 401 | mComposerClient->dispose(); |
| 402 | } |
| 403 | |
| 404 | std::unique_ptr<InputSurface> makeSurface(int width, int height) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 405 | return InputSurface::makeColorInputSurface(mComposerClient, width, height); |
| 406 | } |
| 407 | |
chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 408 | void postBuffer(const sp<SurfaceControl> &layer, int32_t w, int32_t h) { |
| 409 | int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN | |
| 410 | BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE; |
| 411 | sp<GraphicBuffer> buffer = |
| 412 | new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test"); |
| 413 | Transaction().setBuffer(layer, buffer).apply(true); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 414 | usleep(mBufferPostDelay); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | sp<SurfaceComposerClient> mComposerClient; |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 418 | int32_t mBufferPostDelay; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 419 | }; |
| 420 | |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 421 | void injectTapOnDisplay(int x, int y, int displayId) { |
| 422 | char *buf1, *buf2, *bufDisplayId; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 423 | asprintf(&buf1, "%d", x); |
| 424 | asprintf(&buf2, "%d", y); |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 425 | asprintf(&bufDisplayId, "%d", displayId); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 426 | if (fork() == 0) { |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 427 | execlp("input", "input", "-d", bufDisplayId, "tap", buf1, buf2, NULL); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | void injectTap(int x, int y) { |
| 432 | injectTapOnDisplay(x, y, ADISPLAY_ID_DEFAULT); |
| 433 | } |
| 434 | |
| 435 | void injectKeyOnDisplay(uint32_t keycode, int displayId) { |
| 436 | char *buf1, *bufDisplayId; |
| 437 | asprintf(&buf1, "%d", keycode); |
| 438 | asprintf(&bufDisplayId, "%d", displayId); |
| 439 | if (fork() == 0) { |
| 440 | execlp("input", "input", "-d", bufDisplayId, "keyevent", buf1, NULL); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 444 | void injectKey(uint32_t keycode) { |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 445 | injectKeyOnDisplay(keycode, ADISPLAY_ID_NONE); |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 448 | TEST_F(InputSurfacesTest, can_receive_input) { |
| 449 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 450 | surface->showAt(100, 100); |
| 451 | |
| 452 | injectTap(101, 101); |
| 453 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 454 | EXPECT_NE(surface->consumeEvent(), nullptr); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 457 | /** |
| 458 | * Set up two surfaces side-by-side. Tap each surface. |
| 459 | * Next, swap the positions of the two surfaces. Inject tap into the two |
| 460 | * original locations. Ensure that the tap is received by the surfaces in the |
| 461 | * reverse order. |
| 462 | */ |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 463 | TEST_F(InputSurfacesTest, input_respects_positioning) { |
| 464 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 465 | surface->showAt(100, 100); |
| 466 | |
| 467 | std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100); |
| 468 | surface2->showAt(200, 200); |
| 469 | |
| 470 | injectTap(201, 201); |
| 471 | surface2->expectTap(1, 1); |
| 472 | |
| 473 | injectTap(101, 101); |
| 474 | surface->expectTap(1, 1); |
| 475 | |
| 476 | surface2->doTransaction([](auto &t, auto &sc) { |
| 477 | t.setPosition(sc, 100, 100); |
| 478 | }); |
| 479 | surface->doTransaction([](auto &t, auto &sc) { |
| 480 | t.setPosition(sc, 200, 200); |
| 481 | }); |
| 482 | |
| 483 | injectTap(101, 101); |
| 484 | surface2->expectTap(1, 1); |
| 485 | |
| 486 | injectTap(201, 201); |
| 487 | surface->expectTap(1, 1); |
| 488 | } |
| 489 | |
| 490 | TEST_F(InputSurfacesTest, input_respects_layering) { |
| 491 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 492 | std::unique_ptr<InputSurface> surface2 = makeSurface(100, 100); |
| 493 | |
| 494 | surface->showAt(10, 10); |
| 495 | surface2->showAt(10, 10); |
| 496 | |
| 497 | surface->doTransaction([](auto &t, auto &sc) { |
| 498 | t.setLayer(sc, LAYER_BASE + 1); |
| 499 | }); |
| 500 | |
| 501 | injectTap(11, 11); |
| 502 | surface->expectTap(1, 1); |
| 503 | |
| 504 | surface2->doTransaction([](auto &t, auto &sc) { |
| 505 | t.setLayer(sc, LAYER_BASE + 1); |
| 506 | }); |
| 507 | |
| 508 | injectTap(11, 11); |
| 509 | surface2->expectTap(1, 1); |
| 510 | |
| 511 | surface2->doTransaction([](auto &t, auto &sc) { |
| 512 | t.hide(sc); |
| 513 | }); |
| 514 | |
| 515 | injectTap(11, 11); |
| 516 | surface->expectTap(1, 1); |
| 517 | } |
| 518 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 519 | // Surface Insets are set to offset the client content and draw a border around the client surface |
| 520 | // (such as shadows in dialogs). Inputs sent to the client are offset such that 0,0 is the start |
| 521 | // of the client content. |
| 522 | TEST_F(InputSurfacesTest, input_respects_surface_insets) { |
| 523 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 524 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 525 | bgSurface->showAt(100, 100); |
| 526 | |
| 527 | fgSurface->mInputInfo.surfaceInset = 5; |
| 528 | fgSurface->showAt(100, 100); |
| 529 | |
| 530 | injectTap(106, 106); |
| 531 | fgSurface->expectTap(1, 1); |
| 532 | |
| 533 | injectTap(101, 101); |
| 534 | bgSurface->expectTap(1, 1); |
| 535 | } |
| 536 | |
Vishnu Nair | fed7c12 | 2023-03-18 01:54:43 +0000 | [diff] [blame] | 537 | TEST_F(InputSurfacesTest, input_respects_surface_insets_with_replaceTouchableRegionWithCrop) { |
| 538 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 539 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 540 | bgSurface->showAt(100, 100); |
| 541 | |
| 542 | fgSurface->mInputInfo.surfaceInset = 5; |
| 543 | fgSurface->mInputInfo.replaceTouchableRegionWithCrop = true; |
| 544 | fgSurface->showAt(100, 100); |
| 545 | |
| 546 | injectTap(106, 106); |
| 547 | fgSurface->expectTap(1, 1); |
| 548 | |
| 549 | injectTap(101, 101); |
| 550 | bgSurface->expectTap(1, 1); |
| 551 | } |
| 552 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 553 | // Ensure a surface whose insets are cropped, handles the touch offset correctly. ref:b/120413463 |
| 554 | TEST_F(InputSurfacesTest, input_respects_cropped_surface_insets) { |
| 555 | std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100); |
| 556 | std::unique_ptr<InputSurface> childSurface = makeSurface(100, 100); |
| 557 | parentSurface->showAt(100, 100); |
| 558 | |
| 559 | childSurface->mInputInfo.surfaceInset = 10; |
| 560 | childSurface->showAt(100, 100); |
| 561 | |
| 562 | childSurface->doTransaction([&](auto &t, auto &sc) { |
| 563 | t.setPosition(sc, -5, -5); |
Pablo Gamito | 11dcc22 | 2020-09-12 15:49:39 +0000 | [diff] [blame] | 564 | t.reparent(sc, parentSurface->mSurfaceControl); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 565 | }); |
| 566 | |
| 567 | injectTap(106, 106); |
| 568 | childSurface->expectTap(1, 1); |
| 569 | |
| 570 | injectTap(101, 101); |
| 571 | parentSurface->expectTap(1, 1); |
| 572 | } |
| 573 | |
Arthur Hung | 118b114 | 2019-05-08 21:25:59 +0800 | [diff] [blame] | 574 | // Ensure a surface whose insets are scaled, handles the touch offset correctly. |
| 575 | TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets) { |
| 576 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 577 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 578 | bgSurface->showAt(100, 100); |
| 579 | |
| 580 | fgSurface->mInputInfo.surfaceInset = 5; |
| 581 | fgSurface->showAt(100, 100); |
| 582 | |
| 583 | fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 4.0); }); |
| 584 | |
| 585 | // expect = touch / scale - inset |
| 586 | injectTap(112, 124); |
| 587 | fgSurface->expectTap(1, 1); |
| 588 | |
| 589 | injectTap(101, 101); |
| 590 | bgSurface->expectTap(1, 1); |
| 591 | } |
| 592 | |
Ady Abraham | 282f1d7 | 2019-07-24 18:05:56 -0700 | [diff] [blame] | 593 | TEST_F(InputSurfacesTest, input_respects_scaled_surface_insets_overflow) { |
Prabir Pradhan | c9589c1 | 2021-09-22 06:11:43 -0700 | [diff] [blame] | 594 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
Ady Abraham | 282f1d7 | 2019-07-24 18:05:56 -0700 | [diff] [blame] | 595 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
Prabir Pradhan | c9589c1 | 2021-09-22 06:11:43 -0700 | [diff] [blame] | 596 | bgSurface->showAt(100, 100); |
| 597 | |
Ady Abraham | 282f1d7 | 2019-07-24 18:05:56 -0700 | [diff] [blame] | 598 | // In case we pass the very big inset without any checking. |
| 599 | fgSurface->mInputInfo.surfaceInset = INT32_MAX; |
| 600 | fgSurface->showAt(100, 100); |
| 601 | |
| 602 | fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); }); |
| 603 | |
| 604 | // expect no crash for overflow, and inset size to be clamped to surface size |
Prabir Pradhan | c9589c1 | 2021-09-22 06:11:43 -0700 | [diff] [blame] | 605 | injectTap(112, 124); |
| 606 | bgSurface->expectTap(12, 24); |
Ady Abraham | 282f1d7 | 2019-07-24 18:05:56 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Prabir Pradhan | 33da946 | 2022-06-14 14:55:57 +0000 | [diff] [blame] | 609 | TEST_F(InputSurfacesTest, touchable_region) { |
| 610 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 611 | |
| 612 | surface->mInputInfo.touchableRegion.set(Rect{19, 29, 21, 31}); |
| 613 | |
| 614 | surface->showAt(11, 22); |
| 615 | |
| 616 | // A tap within the surface but outside the touchable region should not be sent to the surface. |
| 617 | injectTap(20, 30); |
| 618 | EXPECT_EQ(surface->consumeEvent(200 /*timeoutMs*/), nullptr); |
| 619 | |
| 620 | injectTap(31, 52); |
| 621 | surface->expectTap(20, 30); |
| 622 | } |
| 623 | |
| 624 | TEST_F(InputSurfacesTest, input_respects_touchable_region_offset_overflow) { |
| 625 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 626 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 627 | bgSurface->showAt(100, 100); |
| 628 | |
| 629 | // Set the touchable region to the values at the limit of its corresponding type. |
| 630 | // Since the surface is offset from the origin, the touchable region will be transformed into |
| 631 | // display space, which would trigger an overflow or an underflow. Ensure that we are protected |
| 632 | // against such a situation. |
| 633 | fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX}); |
| 634 | |
| 635 | fgSurface->showAt(100, 100); |
| 636 | |
| 637 | // Expect no crash for overflow. The overflowed touchable region is ignored, so the background |
| 638 | // surface receives touch. |
| 639 | injectTap(112, 124); |
| 640 | bgSurface->expectTap(12, 24); |
| 641 | } |
| 642 | |
| 643 | TEST_F(InputSurfacesTest, input_respects_scaled_touchable_region_overflow) { |
| 644 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 645 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 646 | bgSurface->showAt(0, 0); |
| 647 | |
| 648 | fgSurface->mInputInfo.touchableRegion.orSelf(Rect{INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX}); |
| 649 | fgSurface->showAt(0, 0); |
| 650 | |
| 651 | fgSurface->doTransaction([&](auto &t, auto &sc) { t.setMatrix(sc, 2.0, 0, 0, 2.0); }); |
| 652 | |
| 653 | // Expect no crash for overflow. |
| 654 | injectTap(12, 24); |
Prabir Pradhan | a4c59bd | 2023-04-10 20:54:04 +0000 | [diff] [blame] | 655 | bgSurface->expectTap(12, 24); |
Prabir Pradhan | 33da946 | 2022-06-14 14:55:57 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 658 | // Ensure we ignore transparent region when getting screen bounds when positioning input frame. |
| 659 | TEST_F(InputSurfacesTest, input_ignores_transparent_region) { |
| 660 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 661 | surface->doTransaction([](auto &t, auto &sc) { |
| 662 | Region transparentRegion(Rect(0, 0, 10, 10)); |
| 663 | t.setTransparentRegionHint(sc, transparentRegion); |
| 664 | }); |
| 665 | surface->showAt(100, 100); |
| 666 | injectTap(101, 101); |
| 667 | surface->expectTap(1, 1); |
| 668 | } |
| 669 | |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 670 | // TODO(b/139494112) update tests once we define expected behavior |
| 671 | // Ensure we still send input to the surface regardless of surface visibility changes due to the |
| 672 | // first buffer being submitted or alpha changes. |
| 673 | // Original bug ref: b/120839715 |
| 674 | TEST_F(InputSurfacesTest, input_ignores_buffer_layer_buffer) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 675 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 676 | std::unique_ptr<BlastInputSurface> bufferSurface = |
| 677 | BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 678 | |
| 679 | bgSurface->showAt(10, 10); |
| 680 | bufferSurface->showAt(10, 10); |
| 681 | |
| 682 | injectTap(11, 11); |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 683 | bufferSurface->expectTap(1, 1); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 684 | |
chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 685 | postBuffer(bufferSurface->mSurfaceControl, 100, 100); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 686 | injectTap(11, 11); |
| 687 | bufferSurface->expectTap(1, 1); |
| 688 | } |
| 689 | |
Arthur Hung | fb2ebce | 2021-10-04 14:08:48 +0000 | [diff] [blame] | 690 | TEST_F(InputSurfacesTest, input_respects_buffer_layer_alpha) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 691 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 692 | std::unique_ptr<BlastInputSurface> bufferSurface = |
| 693 | BlastInputSurface::makeBlastInputSurface(mComposerClient, 100, 100); |
| 694 | postBuffer(bufferSurface->mSurfaceControl, 100, 100); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 695 | |
| 696 | bgSurface->showAt(10, 10); |
| 697 | bufferSurface->showAt(10, 10); |
| 698 | |
| 699 | injectTap(11, 11); |
| 700 | bufferSurface->expectTap(1, 1); |
| 701 | |
| 702 | bufferSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); }); |
| 703 | |
| 704 | injectTap(11, 11); |
Arthur Hung | fb2ebce | 2021-10-04 14:08:48 +0000 | [diff] [blame] | 705 | bgSurface->expectTap(1, 1); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 706 | } |
| 707 | |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 708 | TEST_F(InputSurfacesTest, input_ignores_color_layer_alpha) { |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 709 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 710 | std::unique_ptr<InputSurface> fgSurface = makeSurface(100, 100); |
| 711 | |
| 712 | bgSurface->showAt(10, 10); |
| 713 | fgSurface->showAt(10, 10); |
| 714 | |
| 715 | injectTap(11, 11); |
| 716 | fgSurface->expectTap(1, 1); |
| 717 | |
| 718 | fgSurface->doTransaction([](auto &t, auto &sc) { t.setAlpha(sc, 0.0); }); |
| 719 | |
| 720 | injectTap(11, 11); |
Vishnu Nair | f8678ba | 2019-10-11 18:11:26 -0700 | [diff] [blame] | 721 | fgSurface->expectTap(1, 1); |
Vishnu Nair | de19f85 | 2018-12-18 16:11:53 -0800 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | TEST_F(InputSurfacesTest, input_respects_container_layer_visiblity) { |
| 725 | std::unique_ptr<InputSurface> bgSurface = makeSurface(100, 100); |
| 726 | std::unique_ptr<InputSurface> containerSurface = |
| 727 | InputSurface::makeContainerInputSurface(mComposerClient, 100, 100); |
| 728 | |
| 729 | bgSurface->showAt(10, 10); |
| 730 | containerSurface->showAt(10, 10); |
| 731 | |
| 732 | injectTap(11, 11); |
| 733 | containerSurface->expectTap(1, 1); |
| 734 | |
| 735 | containerSurface->doTransaction([](auto &t, auto &sc) { t.hide(sc); }); |
| 736 | |
| 737 | injectTap(11, 11); |
| 738 | bgSurface->expectTap(1, 1); |
| 739 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 740 | |
Arthur Hung | d20b270 | 2019-01-14 18:16:16 +0800 | [diff] [blame] | 741 | TEST_F(InputSurfacesTest, input_respects_outscreen) { |
| 742 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 743 | surface->showAt(-1, -1); |
| 744 | |
| 745 | injectTap(0, 0); |
| 746 | surface->expectTap(1, 1); |
| 747 | } |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 748 | |
| 749 | TEST_F(InputSurfacesTest, input_ignores_cursor_layer) { |
| 750 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 751 | std::unique_ptr<InputSurface> cursorSurface = |
| 752 | InputSurface::makeCursorInputSurface(mComposerClient, 10, 10); |
| 753 | |
| 754 | surface->showAt(10, 10); |
arthurhung | b4a0f85 | 2020-06-16 11:02:50 +0800 | [diff] [blame] | 755 | cursorSurface->showAt(10, 10); |
| 756 | |
| 757 | injectTap(11, 11); |
| 758 | surface->expectTap(1, 1); |
| 759 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 760 | |
| 761 | TEST_F(InputSurfacesTest, can_be_focused) { |
| 762 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 763 | surface->showAt(100, 100); |
| 764 | surface->requestFocus(); |
| 765 | |
| 766 | surface->assertFocusChange(true); |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 767 | |
| 768 | injectKey(AKEYCODE_V); |
| 769 | surface->expectKey(AKEYCODE_V); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 770 | } |
chaviw | 44a6d2b | 2020-09-08 17:14:16 -0700 | [diff] [blame] | 771 | |
| 772 | TEST_F(InputSurfacesTest, rotate_surface) { |
| 773 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 774 | surface->showAt(10, 10); |
| 775 | surface->doTransaction([](auto &t, auto &sc) { |
| 776 | t.setMatrix(sc, 0, 1, -1, 0); // 90 degrees |
| 777 | }); |
| 778 | injectTap(8, 11); |
| 779 | surface->expectTap(1, 2); |
| 780 | |
| 781 | surface->doTransaction([](auto &t, auto &sc) { |
| 782 | t.setMatrix(sc, -1, 0, 0, -1); // 180 degrees |
| 783 | }); |
| 784 | injectTap(9, 8); |
| 785 | surface->expectTap(1, 2); |
| 786 | |
| 787 | surface->doTransaction([](auto &t, auto &sc) { |
| 788 | t.setMatrix(sc, 0, -1, 1, 0); // 270 degrees |
| 789 | }); |
| 790 | injectTap(12, 9); |
| 791 | surface->expectTap(1, 2); |
| 792 | } |
| 793 | |
| 794 | TEST_F(InputSurfacesTest, rotate_surface_with_scale) { |
| 795 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 796 | surface->showAt(10, 10); |
| 797 | surface->doTransaction([](auto &t, auto &sc) { |
| 798 | t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees |
| 799 | }); |
| 800 | injectTap(2, 12); |
| 801 | surface->expectTap(1, 2); |
| 802 | |
| 803 | surface->doTransaction([](auto &t, auto &sc) { |
| 804 | t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees |
| 805 | }); |
| 806 | injectTap(8, 2); |
| 807 | surface->expectTap(1, 2); |
| 808 | |
| 809 | surface->doTransaction([](auto &t, auto &sc) { |
| 810 | t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees |
| 811 | }); |
| 812 | injectTap(18, 8); |
| 813 | surface->expectTap(1, 2); |
| 814 | } |
| 815 | |
| 816 | TEST_F(InputSurfacesTest, rotate_surface_with_scale_and_insets) { |
| 817 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 818 | surface->mInputInfo.surfaceInset = 5; |
| 819 | surface->showAt(100, 100); |
| 820 | |
| 821 | surface->doTransaction([](auto &t, auto &sc) { |
| 822 | t.setMatrix(sc, 0, 2, -4, 0); // 90 degrees |
| 823 | }); |
| 824 | injectTap(40, 120); |
| 825 | surface->expectTap(5, 10); |
| 826 | |
| 827 | surface->doTransaction([](auto &t, auto &sc) { |
| 828 | t.setMatrix(sc, -2, 0, 0, -4); // 180 degrees |
| 829 | }); |
| 830 | injectTap(80, 40); |
| 831 | surface->expectTap(5, 10); |
| 832 | |
| 833 | surface->doTransaction([](auto &t, auto &sc) { |
| 834 | t.setMatrix(sc, 0, -2, 4, 0); // 270 degrees |
| 835 | }); |
| 836 | injectTap(160, 80); |
| 837 | surface->expectTap(5, 10); |
| 838 | } |
| 839 | |
chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame] | 840 | TEST_F(InputSurfacesTest, touch_flag_obscured) { |
| 841 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 842 | surface->showAt(100, 100); |
| 843 | |
| 844 | // Add non touchable window to fully cover touchable window. Window behind gets touch, but |
| 845 | // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED |
| 846 | std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 847 | nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 848 | nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222}; |
Bernardo Rufino | 602ef71 | 2020-12-21 11:02:18 +0000 | [diff] [blame] | 849 | // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by |
| 850 | // the default obscured/untrusted touch filter introduced in S. |
| 851 | nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW; |
chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame] | 852 | nonTouchableSurface->showAt(100, 100); |
| 853 | |
| 854 | injectTap(190, 199); |
| 855 | surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED); |
| 856 | } |
| 857 | |
| 858 | TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) { |
| 859 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 860 | surface->showAt(100, 100); |
| 861 | |
| 862 | // Add non touchable window to cover touchable window, but parent is cropped to not cover area |
| 863 | // that will be tapped. Window behind gets touch, but with flag |
| 864 | // AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED |
| 865 | std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100); |
| 866 | std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 867 | nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); |
| 868 | parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 869 | nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222}; |
| 870 | parentSurface->mInputInfo.ownerUid = gui::Uid{22222}; |
chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame] | 871 | nonTouchableSurface->showAt(0, 0); |
| 872 | parentSurface->showAt(100, 100); |
| 873 | |
| 874 | nonTouchableSurface->doTransaction([&](auto &t, auto &sc) { |
chaviw | 2571450 | 2021-02-11 10:01:08 -0800 | [diff] [blame] | 875 | t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50)); |
chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame] | 876 | t.reparent(sc, parentSurface->mSurfaceControl); |
| 877 | }); |
| 878 | |
| 879 | injectTap(190, 199); |
| 880 | surface->expectTapWithFlag(90, 99, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 881 | } |
| 882 | |
| 883 | TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) { |
| 884 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 885 | surface->showAt(100, 100); |
| 886 | |
| 887 | // Add non touchable window to cover touchable window, but parent is cropped to avoid covering |
| 888 | // the touchable window. Window behind gets touch with no obscured flags. |
| 889 | std::unique_ptr<InputSurface> parentSurface = makeSurface(100, 100); |
| 890 | std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 891 | nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); |
| 892 | parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 893 | nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222}; |
| 894 | parentSurface->mInputInfo.ownerUid = gui::Uid{22222}; |
chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame] | 895 | nonTouchableSurface->showAt(0, 0); |
| 896 | parentSurface->showAt(50, 50); |
| 897 | |
| 898 | nonTouchableSurface->doTransaction([&](auto &t, auto &sc) { |
chaviw | 2571450 | 2021-02-11 10:01:08 -0800 | [diff] [blame] | 899 | t.setCrop(parentSurface->mSurfaceControl, Rect(0, 0, 50, 50)); |
chaviw | 39cfa2e | 2020-11-04 14:19:02 -0800 | [diff] [blame] | 900 | t.reparent(sc, parentSurface->mSurfaceControl); |
| 901 | }); |
| 902 | |
| 903 | injectTap(101, 110); |
| 904 | surface->expectTap(1, 10); |
| 905 | } |
| 906 | |
chaviw | 7e72caf | 2020-12-02 16:50:43 -0800 | [diff] [blame] | 907 | TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) { |
| 908 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 909 | |
| 910 | std::unique_ptr<InputSurface> bufferSurface = |
| 911 | InputSurface::makeBufferInputSurface(mComposerClient, 0, 0); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 912 | bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 913 | bufferSurface->mInputInfo.ownerUid = gui::Uid{22222}; |
chaviw | 7e72caf | 2020-12-02 16:50:43 -0800 | [diff] [blame] | 914 | |
| 915 | surface->showAt(10, 10); |
| 916 | bufferSurface->showAt(50, 50, Rect::EMPTY_RECT); |
| 917 | |
| 918 | injectTap(11, 11); |
| 919 | surface->expectTap(1, 1); |
| 920 | } |
| 921 | |
| 922 | TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) { |
| 923 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 924 | |
chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 925 | std::unique_ptr<BlastInputSurface> bufferSurface = |
| 926 | BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 927 | bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 928 | bufferSurface->mInputInfo.ownerUid = gui::Uid{22222}; |
chaviw | 7e72caf | 2020-12-02 16:50:43 -0800 | [diff] [blame] | 929 | |
| 930 | surface->showAt(10, 10); |
| 931 | bufferSurface->showAt(50, 50, Rect::EMPTY_RECT); |
| 932 | |
| 933 | injectTap(11, 11); |
| 934 | surface->expectTap(1, 1); |
| 935 | } |
| 936 | |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 937 | TEST_F(InputSurfacesTest, strict_unobscured_input_unobscured_window) { |
| 938 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 939 | surface->doTransaction( |
| 940 | [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); }); |
| 941 | surface->showAt(100, 100); |
| 942 | |
| 943 | injectTap(101, 101); |
| 944 | |
| 945 | EXPECT_NE(surface->consumeEvent(), nullptr); |
| 946 | EXPECT_NE(surface->consumeEvent(), nullptr); |
| 947 | |
| 948 | surface->requestFocus(); |
| 949 | surface->assertFocusChange(true); |
| 950 | injectKey(AKEYCODE_V); |
| 951 | surface->expectKey(AKEYCODE_V); |
| 952 | } |
| 953 | |
| 954 | TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) { |
| 955 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 956 | surface->doTransaction([&](auto &t, auto &sc) { |
| 957 | t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); |
| 958 | t.setMatrix(sc, 2.0, 0, 0, 2.0); |
| 959 | }); |
| 960 | surface->showAt(100, 100); |
| 961 | |
| 962 | injectTap(101, 101); |
| 963 | |
| 964 | EXPECT_NE(surface->consumeEvent(), nullptr); |
| 965 | EXPECT_NE(surface->consumeEvent(), nullptr); |
| 966 | |
| 967 | surface->requestFocus(); |
| 968 | surface->assertFocusChange(true); |
| 969 | injectKey(AKEYCODE_V); |
| 970 | surface->expectKey(AKEYCODE_V); |
| 971 | } |
| 972 | |
| 973 | TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) { |
| 974 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 975 | surface->mInputInfo.ownerUid = gui::Uid{11111}; |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 976 | surface->doTransaction( |
| 977 | [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); }); |
| 978 | surface->showAt(100, 100); |
| 979 | std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 980 | obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 981 | obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222}; |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 982 | obscuringSurface->showAt(100, 100); |
| 983 | injectTap(101, 101); |
| 984 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
| 985 | |
| 986 | surface->requestFocus(); |
| 987 | surface->assertFocusChange(true); |
| 988 | injectKey(AKEYCODE_V); |
| 989 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
| 990 | } |
| 991 | |
| 992 | TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) { |
| 993 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 994 | surface->mInputInfo.ownerUid = gui::Uid{11111}; |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 995 | surface->doTransaction( |
| 996 | [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); }); |
| 997 | surface->showAt(100, 100); |
| 998 | std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 999 | obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1000 | obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222}; |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 1001 | obscuringSurface->showAt(190, 190); |
| 1002 | |
| 1003 | injectTap(101, 101); |
| 1004 | |
| 1005 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
| 1006 | |
| 1007 | surface->requestFocus(); |
| 1008 | surface->assertFocusChange(true); |
| 1009 | injectKey(AKEYCODE_V); |
| 1010 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
| 1011 | } |
| 1012 | |
| 1013 | TEST_F(InputSurfacesTest, strict_unobscured_input_alpha_window) { |
| 1014 | std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300); |
| 1015 | parentSurface->showAt(0, 0, Rect(0, 0, 300, 300)); |
| 1016 | |
| 1017 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 1018 | surface->showAt(100, 100); |
| 1019 | surface->doTransaction([&](auto &t, auto &sc) { |
| 1020 | t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); |
| 1021 | t.reparent(sc, parentSurface->mSurfaceControl); |
| 1022 | t.setAlpha(parentSurface->mSurfaceControl, 0.9f); |
| 1023 | }); |
| 1024 | |
| 1025 | injectTap(101, 101); |
| 1026 | |
| 1027 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
| 1028 | |
| 1029 | surface->requestFocus(); |
| 1030 | surface->assertFocusChange(true); |
| 1031 | injectKey(AKEYCODE_V); |
| 1032 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
| 1033 | } |
| 1034 | |
| 1035 | TEST_F(InputSurfacesTest, strict_unobscured_input_cropped_window) { |
| 1036 | std::unique_ptr<InputSurface> parentSurface = makeSurface(300, 300); |
| 1037 | parentSurface->showAt(0, 0, Rect(0, 0, 300, 300)); |
| 1038 | |
| 1039 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 1040 | surface->doTransaction([&](auto &t, auto &sc) { |
| 1041 | t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); |
| 1042 | t.reparent(sc, parentSurface->mSurfaceControl); |
| 1043 | t.setCrop(parentSurface->mSurfaceControl, Rect(10, 10, 100, 100)); |
| 1044 | }); |
| 1045 | surface->showAt(100, 100); |
| 1046 | |
| 1047 | injectTap(111, 111); |
| 1048 | |
| 1049 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
| 1050 | |
| 1051 | surface->requestFocus(); |
| 1052 | surface->assertFocusChange(true); |
| 1053 | injectKey(AKEYCODE_V); |
| 1054 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
| 1055 | } |
| 1056 | |
Arthur Hung | 49d525a | 2021-11-19 15:11:51 +0000 | [diff] [blame] | 1057 | TEST_F(InputSurfacesTest, ignore_touch_region_with_zero_sized_blast) { |
| 1058 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 1059 | |
| 1060 | std::unique_ptr<BlastInputSurface> bufferSurface = |
| 1061 | BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0); |
| 1062 | |
| 1063 | surface->showAt(100, 100); |
| 1064 | bufferSurface->mInputInfo.touchableRegion.orSelf(Rect(0, 0, 200, 200)); |
| 1065 | bufferSurface->showAt(100, 100, Rect::EMPTY_RECT); |
| 1066 | |
| 1067 | injectTap(101, 101); |
| 1068 | surface->expectTap(1, 1); |
| 1069 | } |
| 1070 | |
Vishnu Nair | a066d90 | 2021-09-13 18:40:17 -0700 | [diff] [blame] | 1071 | TEST_F(InputSurfacesTest, drop_input_policy) { |
| 1072 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 1073 | surface->doTransaction( |
| 1074 | [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::ALL); }); |
| 1075 | surface->showAt(100, 100); |
| 1076 | |
| 1077 | injectTap(101, 101); |
| 1078 | |
| 1079 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
| 1080 | |
| 1081 | surface->requestFocus(); |
| 1082 | surface->assertFocusChange(true); |
| 1083 | injectKey(AKEYCODE_V); |
| 1084 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
| 1085 | } |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 1086 | |
Prabir Pradhan | 8c28598 | 2022-01-28 09:19:39 -0800 | [diff] [blame] | 1087 | TEST_F(InputSurfacesTest, layer_with_valid_crop_can_be_focused) { |
| 1088 | std::unique_ptr<InputSurface> bufferSurface = |
| 1089 | InputSurface::makeBufferInputSurface(mComposerClient, 100, 100); |
| 1090 | |
| 1091 | bufferSurface->showAt(50, 50, Rect{0, 0, 100, 100}); |
| 1092 | |
| 1093 | bufferSurface->requestFocus(); |
| 1094 | bufferSurface->assertFocusChange(true); |
| 1095 | } |
| 1096 | |
Prabir Pradhan | 6fa425a | 2021-12-16 07:16:04 -0800 | [diff] [blame] | 1097 | /** |
| 1098 | * If a cropped layer's touchable region is replaced with a null crop, it should receive input in |
| 1099 | * its own crop. |
| 1100 | */ |
| 1101 | TEST_F(InputSurfacesTest, cropped_container_replaces_touchable_region_with_null_crop) { |
| 1102 | std::unique_ptr<InputSurface> parentContainer = |
| 1103 | InputSurface::makeContainerInputSurface(mComposerClient, 0, 0); |
| 1104 | std::unique_ptr<InputSurface> containerSurface = |
| 1105 | InputSurface::makeContainerInputSurface(mComposerClient, 100, 100); |
| 1106 | containerSurface->doTransaction( |
| 1107 | [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); }); |
| 1108 | containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true; |
| 1109 | containerSurface->mInputInfo.touchableRegionCropHandle = nullptr; |
| 1110 | parentContainer->showAt(10, 10, Rect(0, 0, 20, 20)); |
| 1111 | containerSurface->showAt(10, 10, Rect(0, 0, 5, 5)); |
| 1112 | |
| 1113 | // Receives events inside its own crop |
| 1114 | injectTap(21, 21); |
| 1115 | containerSurface->expectTap(1, 1); // Event is in layer space |
| 1116 | |
| 1117 | // Does not receive events outside its crop |
| 1118 | injectTap(26, 26); |
| 1119 | EXPECT_EQ(containerSurface->consumeEvent(100), nullptr); |
| 1120 | } |
| 1121 | |
| 1122 | /** |
| 1123 | * If an un-cropped layer's touchable region is replaced with a null crop, it should receive input |
| 1124 | * in its parent's touchable region. The input events should be in the layer's coordinate space. |
| 1125 | */ |
| 1126 | TEST_F(InputSurfacesTest, uncropped_container_replaces_touchable_region_with_null_crop) { |
| 1127 | std::unique_ptr<InputSurface> parentContainer = |
| 1128 | InputSurface::makeContainerInputSurface(mComposerClient, 0, 0); |
| 1129 | std::unique_ptr<InputSurface> containerSurface = |
| 1130 | InputSurface::makeContainerInputSurface(mComposerClient, 100, 100); |
| 1131 | containerSurface->doTransaction( |
| 1132 | [&](auto &t, auto &sc) { t.reparent(sc, parentContainer->mSurfaceControl); }); |
| 1133 | containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true; |
| 1134 | containerSurface->mInputInfo.touchableRegionCropHandle = nullptr; |
| 1135 | parentContainer->showAt(10, 10, Rect(0, 0, 20, 20)); |
| 1136 | containerSurface->showAt(10, 10, Rect::INVALID_RECT); |
| 1137 | |
| 1138 | // Receives events inside parent bounds |
| 1139 | injectTap(21, 21); |
| 1140 | containerSurface->expectTap(1, 1); // Event is in layer space |
| 1141 | |
| 1142 | // Does not receive events outside parent bounds |
| 1143 | injectTap(31, 31); |
| 1144 | EXPECT_EQ(containerSurface->consumeEvent(100), nullptr); |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * If a layer's touchable region is replaced with a layer crop, it should receive input in the crop |
| 1149 | * layer's bounds. The input events should be in the layer's coordinate space. |
| 1150 | */ |
| 1151 | TEST_F(InputSurfacesTest, replace_touchable_region_with_crop) { |
| 1152 | std::unique_ptr<InputSurface> cropLayer = |
| 1153 | InputSurface::makeContainerInputSurface(mComposerClient, 0, 0); |
| 1154 | cropLayer->showAt(50, 50, Rect(0, 0, 20, 20)); |
| 1155 | |
| 1156 | std::unique_ptr<InputSurface> containerSurface = |
| 1157 | InputSurface::makeContainerInputSurface(mComposerClient, 100, 100); |
| 1158 | containerSurface->mInputInfo.replaceTouchableRegionWithCrop = true; |
| 1159 | containerSurface->mInputInfo.touchableRegionCropHandle = |
| 1160 | cropLayer->mSurfaceControl->getHandle(); |
| 1161 | containerSurface->showAt(10, 10, Rect::INVALID_RECT); |
| 1162 | |
| 1163 | // Receives events inside crop layer bounds |
| 1164 | injectTap(51, 51); |
| 1165 | containerSurface->expectTap(41, 41); // Event is in layer space |
| 1166 | |
| 1167 | // Does not receive events outside crop layer bounds |
| 1168 | injectTap(21, 21); |
| 1169 | injectTap(71, 71); |
| 1170 | EXPECT_EQ(containerSurface->consumeEvent(100), nullptr); |
| 1171 | } |
| 1172 | |
Linus Tufvesson | a185882 | 2022-03-04 09:32:07 +0000 | [diff] [blame] | 1173 | TEST_F(InputSurfacesTest, child_container_with_no_input_channel_blocks_parent) { |
| 1174 | std::unique_ptr<InputSurface> parent = makeSurface(100, 100); |
| 1175 | |
| 1176 | parent->showAt(100, 100); |
| 1177 | injectTap(101, 101); |
| 1178 | parent->expectTap(1, 1); |
| 1179 | |
| 1180 | std::unique_ptr<InputSurface> childContainerSurface = |
| 1181 | InputSurface::makeContainerInputSurfaceNoInputChannel(mComposerClient, 100, 100); |
| 1182 | childContainerSurface->showAt(0, 0); |
| 1183 | childContainerSurface->doTransaction( |
| 1184 | [&](auto &t, auto &sc) { t.reparent(sc, parent->mSurfaceControl); }); |
| 1185 | injectTap(101, 101); |
| 1186 | |
| 1187 | EXPECT_EQ(parent->consumeEvent(100), nullptr); |
| 1188 | } |
| 1189 | |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 1190 | class MultiDisplayTests : public InputSurfacesTest { |
| 1191 | public: |
| 1192 | MultiDisplayTests() : InputSurfacesTest() { ProcessState::self()->startThreadPool(); } |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1193 | void TearDown() override { |
| 1194 | for (auto &token : mVirtualDisplays) { |
| 1195 | SurfaceComposerClient::destroyDisplay(token); |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 1196 | } |
| 1197 | InputSurfacesTest::TearDown(); |
| 1198 | } |
| 1199 | |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1200 | void createDisplay(int32_t width, int32_t height, bool isSecure, ui::LayerStack layerStack, |
| 1201 | bool receivesInput = true, int32_t offsetX = 0, int32_t offsetY = 0) { |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 1202 | sp<IGraphicBufferConsumer> consumer; |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1203 | sp<IGraphicBufferProducer> producer; |
| 1204 | BufferQueue::createBufferQueue(&producer, &consumer); |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 1205 | consumer->setConsumerName(String8("Virtual disp consumer")); |
| 1206 | consumer->setDefaultBufferSize(width, height); |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1207 | mProducers.push_back(producer); |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 1208 | |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1209 | std::string name = "VirtualDisplay"; |
| 1210 | name += std::to_string(mVirtualDisplays.size()); |
| 1211 | sp<IBinder> token = SurfaceComposerClient::createDisplay(String8(name.c_str()), isSecure); |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 1212 | SurfaceComposerClient::Transaction t; |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1213 | t.setDisplaySurface(token, producer); |
| 1214 | t.setDisplayFlags(token, receivesInput ? 0x01 /* DisplayDevice::eReceivesInput */ : 0); |
| 1215 | t.setDisplayLayerStack(token, layerStack); |
| 1216 | t.setDisplayProjection(token, ui::ROTATION_0, {0, 0, width, height}, |
| 1217 | {offsetX, offsetY, offsetX + width, offsetY + height}); |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 1218 | t.apply(true); |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1219 | |
| 1220 | mVirtualDisplays.push_back(token); |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1223 | std::vector<sp<IBinder>> mVirtualDisplays; |
| 1224 | std::vector<sp<IGraphicBufferProducer>> mProducers; |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 1225 | }; |
| 1226 | |
Prabir Pradhan | da0f62c | 2022-07-22 19:53:04 +0000 | [diff] [blame] | 1227 | TEST_F(MultiDisplayTests, drop_touch_if_layer_on_invalid_display) { |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1228 | ui::LayerStack layerStack = ui::LayerStack::fromValue(42); |
| 1229 | // Do not create a display associated with the LayerStack. |
| 1230 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 1231 | surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); }); |
| 1232 | surface->showAt(100, 100); |
| 1233 | |
Prabir Pradhan | da0f62c | 2022-07-22 19:53:04 +0000 | [diff] [blame] | 1234 | // Touches should be dropped if the layer is on an invalid display. |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1235 | injectTapOnDisplay(101, 101, layerStack.id); |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1236 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
Prabir Pradhan | da0f62c | 2022-07-22 19:53:04 +0000 | [diff] [blame] | 1237 | |
| 1238 | // However, we still let the window be focused and receive keys. |
| 1239 | surface->requestFocus(layerStack.id); |
| 1240 | surface->assertFocusChange(true); |
| 1241 | |
| 1242 | injectKeyOnDisplay(AKEYCODE_V, layerStack.id); |
| 1243 | surface->expectKey(AKEYCODE_V); |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | TEST_F(MultiDisplayTests, virtual_display_receives_input) { |
| 1247 | ui::LayerStack layerStack = ui::LayerStack::fromValue(42); |
| 1248 | createDisplay(1000, 1000, false /*isSecure*/, layerStack); |
| 1249 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 1250 | surface->doTransaction([&](auto &t, auto &sc) { t.setLayerStack(sc, layerStack); }); |
| 1251 | surface->showAt(100, 100); |
| 1252 | |
| 1253 | injectTapOnDisplay(101, 101, layerStack.id); |
| 1254 | surface->expectTap(1, 1); |
| 1255 | |
| 1256 | surface->requestFocus(layerStack.id); |
| 1257 | surface->assertFocusChange(true); |
| 1258 | injectKeyOnDisplay(AKEYCODE_V, layerStack.id); |
| 1259 | surface->expectKey(AKEYCODE_V); |
| 1260 | } |
| 1261 | |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 1262 | TEST_F(MultiDisplayTests, drop_input_for_secure_layer_on_nonsecure_display) { |
| 1263 | ui::LayerStack layerStack = ui::LayerStack::fromValue(42); |
| 1264 | createDisplay(1000, 1000, false /*isSecure*/, layerStack); |
| 1265 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 1266 | surface->doTransaction([&](auto &t, auto &sc) { |
| 1267 | t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure); |
| 1268 | t.setLayerStack(sc, layerStack); |
| 1269 | }); |
| 1270 | surface->showAt(100, 100); |
| 1271 | |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1272 | injectTapOnDisplay(101, 101, layerStack.id); |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 1273 | |
| 1274 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
| 1275 | |
| 1276 | surface->requestFocus(layerStack.id); |
| 1277 | surface->assertFocusChange(true); |
| 1278 | injectKeyOnDisplay(AKEYCODE_V, layerStack.id); |
| 1279 | EXPECT_EQ(surface->consumeEvent(100), nullptr); |
| 1280 | } |
| 1281 | |
| 1282 | TEST_F(MultiDisplayTests, dont_drop_input_for_secure_layer_on_secure_display) { |
| 1283 | ui::LayerStack layerStack = ui::LayerStack::fromValue(42); |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1284 | |
| 1285 | // Create the secure display as system, because only certain users can create secure displays. |
| 1286 | seteuid(AID_SYSTEM); |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 1287 | createDisplay(1000, 1000, true /*isSecure*/, layerStack); |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 1288 | // Change the uid back to root. |
| 1289 | seteuid(AID_ROOT); |
| 1290 | |
Vishnu Nair | 16a938f | 2021-09-24 07:14:54 -0700 | [diff] [blame] | 1291 | std::unique_ptr<InputSurface> surface = makeSurface(100, 100); |
| 1292 | surface->doTransaction([&](auto &t, auto &sc) { |
| 1293 | t.setFlags(sc, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure); |
| 1294 | t.setLayerStack(sc, layerStack); |
| 1295 | }); |
| 1296 | surface->showAt(100, 100); |
| 1297 | |
| 1298 | injectTapOnDisplay(101, 101, layerStack.id); |
| 1299 | EXPECT_NE(surface->consumeEvent(), nullptr); |
| 1300 | EXPECT_NE(surface->consumeEvent(), nullptr); |
| 1301 | |
| 1302 | surface->requestFocus(layerStack.id); |
| 1303 | surface->assertFocusChange(true); |
| 1304 | injectKeyOnDisplay(AKEYCODE_V, layerStack.id); |
| 1305 | |
| 1306 | surface->expectKey(AKEYCODE_V); |
| 1307 | } |
| 1308 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1309 | } // namespace android::test |