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