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