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