| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2020 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 <BnInputFlingerQuery.h> | 
|  | 18 | #include <IInputFlingerQuery.h> | 
|  | 19 |  | 
|  | 20 | #include <android/os/BnInputFlinger.h> | 
|  | 21 | #include <android/os/BnSetInputWindowsListener.h> | 
|  | 22 | #include <android/os/IInputFlinger.h> | 
|  | 23 | #include <android/os/ISetInputWindowsListener.h> | 
|  | 24 |  | 
|  | 25 | #include <binder/Binder.h> | 
|  | 26 | #include <binder/IPCThreadState.h> | 
|  | 27 | #include <binder/IServiceManager.h> | 
|  | 28 | #include <binder/Parcel.h> | 
|  | 29 | #include <binder/ProcessState.h> | 
|  | 30 |  | 
|  | 31 | #include <input/Input.h> | 
|  | 32 | #include <input/InputTransport.h> | 
|  | 33 | #include <input/InputWindow.h> | 
|  | 34 |  | 
|  | 35 | #include <gtest/gtest.h> | 
|  | 36 | #include <inttypes.h> | 
|  | 37 | #include <linux/uinput.h> | 
|  | 38 | #include <log/log.h> | 
|  | 39 | #include <ui/Rect.h> | 
|  | 40 | #include <ui/Region.h> | 
|  | 41 | #include <chrono> | 
|  | 42 | #include <thread> | 
|  | 43 | #include <unordered_map> | 
|  | 44 |  | 
|  | 45 | #define TAG "InputFlingerServiceTest" | 
|  | 46 |  | 
|  | 47 | using android::os::BnInputFlinger; | 
|  | 48 | using android::os::BnSetInputWindowsListener; | 
|  | 49 | using android::os::IInputFlinger; | 
|  | 50 | using android::os::ISetInputWindowsListener; | 
|  | 51 |  | 
|  | 52 | using std::chrono_literals::operator""ms; | 
|  | 53 | using std::chrono_literals::operator""s; | 
|  | 54 |  | 
|  | 55 | namespace android { | 
|  | 56 |  | 
|  | 57 | static const sp<IBinder> TestInfoToken = new BBinder(); | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 58 | static const sp<IBinder> FocusedTestInfoToken = new BBinder(); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 59 | static constexpr int32_t TestInfoId = 1; | 
|  | 60 | static const std::string TestInfoName = "InputFlingerServiceTestInputWindowInfo"; | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 61 | static constexpr Flags<InputWindowInfo::Flag> TestInfoFlags = InputWindowInfo::Flag::NOT_FOCUSABLE; | 
|  | 62 | static constexpr InputWindowInfo::Type TestInfoType = InputWindowInfo::Type::INPUT_METHOD; | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 63 | static constexpr std::chrono::duration TestInfoDispatchingTimeout = 2532ms; | 
|  | 64 | static constexpr int32_t TestInfoFrameLeft = 93; | 
|  | 65 | static constexpr int32_t TestInfoFrameTop = 34; | 
|  | 66 | static constexpr int32_t TestInfoFrameRight = 16; | 
|  | 67 | static constexpr int32_t TestInfoFrameBottom = 19; | 
|  | 68 | static constexpr int32_t TestInfoSurfaceInset = 17; | 
|  | 69 | static constexpr float TestInfoGlobalScaleFactor = 0.3; | 
|  | 70 | static constexpr float TestInfoWindowXScale = 0.4; | 
|  | 71 | static constexpr float TestInfoWindowYScale = 0.5; | 
|  | 72 | static const Rect TestInfoTouchableRegionRect = {100 /* left */, 150 /* top */, 400 /* right */, | 
|  | 73 | 450 /* bottom */}; | 
|  | 74 | static const Region TestInfoTouchableRegion(TestInfoTouchableRegionRect); | 
|  | 75 | static constexpr bool TestInfoVisible = false; | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 76 | static constexpr bool TestInfoTrustedOverlay = true; | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 77 | static constexpr bool TestInfoFocusable = false; | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 78 | static constexpr bool TestInfoHasWallpaper = false; | 
|  | 79 | static constexpr bool TestInfoPaused = false; | 
|  | 80 | static constexpr int32_t TestInfoOwnerPid = 19; | 
|  | 81 | static constexpr int32_t TestInfoOwnerUid = 24; | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 82 | static constexpr InputWindowInfo::Feature TestInfoInputFeatures = | 
|  | 83 | InputWindowInfo::Feature::NO_INPUT_CHANNEL; | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 84 | static constexpr int32_t TestInfoDisplayId = 34; | 
|  | 85 | static constexpr int32_t TestInfoPortalToDisplayId = 2; | 
|  | 86 | static constexpr bool TestInfoReplaceTouchableRegionWithCrop = true; | 
|  | 87 | static const sp<IBinder> TestInfoTouchableRegionCropHandle = new BBinder(); | 
|  | 88 |  | 
|  | 89 | static const std::string TestAppInfoName = "InputFlingerServiceTestInputApplicationInfo"; | 
|  | 90 | static const sp<IBinder> TestAppInfoToken = new BBinder(); | 
|  | 91 | static constexpr std::chrono::duration TestAppInfoDispatchingTimeout = 12345678ms; | 
|  | 92 |  | 
|  | 93 | static const String16 kTestServiceName = String16("InputFlingerService"); | 
|  | 94 | static const String16 kQueryServiceName = String16("InputFlingerQueryService"); | 
|  | 95 |  | 
|  | 96 | struct SetInputWindowsListener; | 
|  | 97 | // --- InputFlingerServiceTest --- | 
|  | 98 | class InputFlingerServiceTest : public testing::Test { | 
|  | 99 | public: | 
|  | 100 | void SetUp() override; | 
|  | 101 | void TearDown() override; | 
|  | 102 |  | 
|  | 103 | protected: | 
|  | 104 | void InitializeInputFlinger(); | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 105 | void setInputWindowsByInfos(const std::vector<InputWindowInfo>& infos); | 
|  | 106 | void setFocusedWindow(const sp<IBinder> token, const sp<IBinder> focusedToken, | 
|  | 107 | nsecs_t timestampNanos); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 108 |  | 
|  | 109 | void setInputWindowsFinished(); | 
|  | 110 | void verifyInputWindowInfo(const InputWindowInfo& info) const; | 
|  | 111 | InputWindowInfo& getInfo() const { return const_cast<InputWindowInfo&>(mInfo); } | 
|  | 112 |  | 
|  | 113 | sp<IInputFlinger> mService; | 
|  | 114 | sp<IInputFlingerQuery> mQuery; | 
|  | 115 |  | 
|  | 116 | private: | 
|  | 117 | sp<SetInputWindowsListener> mSetInputWindowsListener; | 
| Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 118 | std::unique_ptr<InputChannel> mServerChannel, mClientChannel; | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 119 | InputWindowInfo mInfo; | 
|  | 120 | std::mutex mLock; | 
|  | 121 | std::condition_variable mSetInputWindowsFinishedCondition; | 
|  | 122 | }; | 
|  | 123 |  | 
|  | 124 | struct SetInputWindowsListener : BnSetInputWindowsListener { | 
|  | 125 | explicit SetInputWindowsListener(std::function<void()> cbFunc) : mCbFunc(cbFunc) {} | 
|  | 126 |  | 
|  | 127 | binder::Status onSetInputWindowsFinished() override; | 
|  | 128 |  | 
|  | 129 | std::function<void()> mCbFunc; | 
|  | 130 | }; | 
|  | 131 |  | 
|  | 132 | class TestInputManager : public BnInputFlinger { | 
|  | 133 | protected: | 
|  | 134 | virtual ~TestInputManager(){}; | 
|  | 135 |  | 
|  | 136 | public: | 
|  | 137 | TestInputManager(){}; | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 138 |  | 
|  | 139 | binder::Status getInputWindows(std::vector<::android::InputWindowInfo>* inputHandles); | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 140 | binder::Status getInputChannels(std::vector<::android::InputChannel>* channels); | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 141 | binder::Status getLastFocusRequest(FocusRequest*); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 142 |  | 
|  | 143 | status_t dump(int fd, const Vector<String16>& args) override; | 
|  | 144 |  | 
|  | 145 | binder::Status setInputWindows( | 
|  | 146 | const std::vector<InputWindowInfo>& handles, | 
|  | 147 | const sp<ISetInputWindowsListener>& setInputWindowsListener) override; | 
|  | 148 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 149 | binder::Status createInputChannel(const std::string& name, InputChannel* outChannel) override; | 
|  | 150 | binder::Status removeInputChannel(const sp<IBinder>& connectionToken) override; | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 151 | binder::Status setFocusedWindow(const FocusRequest&) override; | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 152 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 153 | void reset(); | 
|  | 154 |  | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 155 | private: | 
|  | 156 | mutable Mutex mLock; | 
|  | 157 | std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> mHandlesPerDisplay; | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 158 | std::vector<std::shared_ptr<InputChannel>> mInputChannels; | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 159 | FocusRequest mFocusRequest; | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 160 | }; | 
|  | 161 |  | 
|  | 162 | class TestInputQuery : public BnInputFlingerQuery { | 
|  | 163 | public: | 
|  | 164 | TestInputQuery(sp<android::TestInputManager> manager) : mManager(manager){}; | 
|  | 165 | binder::Status getInputWindows(std::vector<::android::InputWindowInfo>* inputHandles) override; | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 166 | binder::Status getInputChannels(std::vector<::android::InputChannel>* channels) override; | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 167 | binder::Status getLastFocusRequest(FocusRequest*) override; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 168 | binder::Status resetInputManager() override; | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 169 |  | 
|  | 170 | private: | 
|  | 171 | sp<android::TestInputManager> mManager; | 
|  | 172 | }; | 
|  | 173 |  | 
|  | 174 | binder::Status TestInputQuery::getInputWindows( | 
|  | 175 | std::vector<::android::InputWindowInfo>* inputHandles) { | 
|  | 176 | return mManager->getInputWindows(inputHandles); | 
|  | 177 | } | 
|  | 178 |  | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 179 | binder::Status TestInputQuery::getInputChannels(std::vector<::android::InputChannel>* channels) { | 
|  | 180 | return mManager->getInputChannels(channels); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 181 | } | 
|  | 182 |  | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 183 | binder::Status TestInputQuery::getLastFocusRequest(FocusRequest* request) { | 
|  | 184 | return mManager->getLastFocusRequest(request); | 
|  | 185 | } | 
|  | 186 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 187 | binder::Status TestInputQuery::resetInputManager() { | 
|  | 188 | mManager->reset(); | 
|  | 189 | return binder::Status::ok(); | 
|  | 190 | } | 
|  | 191 |  | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 192 | binder::Status SetInputWindowsListener::onSetInputWindowsFinished() { | 
|  | 193 | if (mCbFunc != nullptr) { | 
|  | 194 | mCbFunc(); | 
|  | 195 | } | 
|  | 196 | return binder::Status::ok(); | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | binder::Status TestInputManager::setInputWindows( | 
|  | 200 | const std::vector<InputWindowInfo>& infos, | 
|  | 201 | const sp<ISetInputWindowsListener>& setInputWindowsListener) { | 
|  | 202 | AutoMutex _l(mLock); | 
|  | 203 |  | 
|  | 204 | for (const auto& info : infos) { | 
|  | 205 | mHandlesPerDisplay.emplace(info.displayId, std::vector<sp<InputWindowHandle>>()); | 
|  | 206 | mHandlesPerDisplay[info.displayId].push_back(new InputWindowHandle(info)); | 
|  | 207 | } | 
|  | 208 | if (setInputWindowsListener) { | 
|  | 209 | setInputWindowsListener->onSetInputWindowsFinished(); | 
|  | 210 | } | 
|  | 211 | return binder::Status::ok(); | 
|  | 212 | } | 
|  | 213 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 214 | binder::Status TestInputManager::createInputChannel(const std::string& name, | 
|  | 215 | InputChannel* outChannel) { | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 216 | AutoMutex _l(mLock); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 217 | std::unique_ptr<InputChannel> serverChannel; | 
|  | 218 | std::unique_ptr<InputChannel> clientChannel; | 
|  | 219 | InputChannel::openInputChannelPair(name, serverChannel, clientChannel); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 220 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 221 | clientChannel->copyTo(*outChannel); | 
|  | 222 |  | 
|  | 223 | mInputChannels.emplace_back(std::move(serverChannel)); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 224 |  | 
|  | 225 | return binder::Status::ok(); | 
|  | 226 | } | 
|  | 227 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 228 | binder::Status TestInputManager::removeInputChannel(const sp<IBinder>& connectionToken) { | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 229 | AutoMutex _l(mLock); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 230 |  | 
|  | 231 | auto it = std::find_if(mInputChannels.begin(), mInputChannels.end(), | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 232 | [&](std::shared_ptr<InputChannel>& c) { | 
|  | 233 | return c->getConnectionToken() == connectionToken; | 
|  | 234 | }); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 235 | if (it != mInputChannels.end()) { | 
|  | 236 | mInputChannels.erase(it); | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | return binder::Status::ok(); | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | status_t TestInputManager::dump(int fd, const Vector<String16>& args) { | 
|  | 243 | std::string dump; | 
|  | 244 |  | 
|  | 245 | dump += " InputFlinger dump\n"; | 
|  | 246 |  | 
|  | 247 | ::write(fd, dump.c_str(), dump.size()); | 
|  | 248 | return NO_ERROR; | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | binder::Status TestInputManager::getInputWindows( | 
|  | 252 | std::vector<::android::InputWindowInfo>* inputInfos) { | 
|  | 253 | for (auto& [displayId, inputHandles] : mHandlesPerDisplay) { | 
|  | 254 | for (auto& inputHandle : inputHandles) { | 
|  | 255 | inputInfos->push_back(*inputHandle->getInfo()); | 
|  | 256 | } | 
|  | 257 | } | 
|  | 258 | return binder::Status::ok(); | 
|  | 259 | } | 
|  | 260 |  | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 261 | binder::Status TestInputManager::getInputChannels(std::vector<::android::InputChannel>* channels) { | 
|  | 262 | channels->clear(); | 
|  | 263 | for (std::shared_ptr<InputChannel>& channel : mInputChannels) { | 
|  | 264 | channels->push_back(*channel); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 265 | } | 
|  | 266 | return binder::Status::ok(); | 
|  | 267 | } | 
|  | 268 |  | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 269 | binder::Status TestInputManager::getLastFocusRequest(FocusRequest* request) { | 
|  | 270 | *request = mFocusRequest; | 
|  | 271 | return binder::Status::ok(); | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | binder::Status TestInputManager::setFocusedWindow(const FocusRequest& request) { | 
|  | 275 | mFocusRequest = request; | 
|  | 276 | return binder::Status::ok(); | 
|  | 277 | } | 
|  | 278 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 279 | void TestInputManager::reset() { | 
|  | 280 | mHandlesPerDisplay.clear(); | 
|  | 281 | mInputChannels.clear(); | 
|  | 282 | mFocusRequest = FocusRequest(); | 
|  | 283 | } | 
|  | 284 |  | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 285 | void InputFlingerServiceTest::SetUp() { | 
|  | 286 | mSetInputWindowsListener = new SetInputWindowsListener([&]() { | 
|  | 287 | std::unique_lock<std::mutex> lock(mLock); | 
|  | 288 | mSetInputWindowsFinishedCondition.notify_all(); | 
|  | 289 | }); | 
|  | 290 | InputChannel::openInputChannelPair("testchannels", mServerChannel, mClientChannel); | 
|  | 291 |  | 
|  | 292 | mInfo.token = TestInfoToken; | 
|  | 293 | mInfo.id = TestInfoId; | 
|  | 294 | mInfo.name = TestInfoName; | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 295 | mInfo.flags = TestInfoFlags; | 
|  | 296 | mInfo.type = TestInfoType; | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 297 | mInfo.dispatchingTimeout = TestInfoDispatchingTimeout; | 
|  | 298 | mInfo.frameLeft = TestInfoFrameLeft; | 
|  | 299 | mInfo.frameTop = TestInfoFrameTop; | 
|  | 300 | mInfo.frameRight = TestInfoFrameRight; | 
|  | 301 | mInfo.frameBottom = TestInfoFrameBottom; | 
|  | 302 | mInfo.surfaceInset = TestInfoSurfaceInset; | 
|  | 303 | mInfo.globalScaleFactor = TestInfoGlobalScaleFactor; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 304 | mInfo.transform.set({TestInfoWindowXScale, 0, TestInfoFrameLeft, 0, TestInfoWindowYScale, | 
|  | 305 | TestInfoFrameTop, 0, 0, 1}); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 306 | mInfo.touchableRegion = TestInfoTouchableRegion; | 
|  | 307 | mInfo.visible = TestInfoVisible; | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 308 | mInfo.trustedOverlay = TestInfoTrustedOverlay; | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 309 | mInfo.focusable = TestInfoFocusable; | 
|  | 310 |  | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 311 | mInfo.hasWallpaper = TestInfoHasWallpaper; | 
|  | 312 | mInfo.paused = TestInfoPaused; | 
|  | 313 | mInfo.ownerPid = TestInfoOwnerPid; | 
|  | 314 | mInfo.ownerUid = TestInfoOwnerUid; | 
|  | 315 | mInfo.inputFeatures = TestInfoInputFeatures; | 
|  | 316 | mInfo.displayId = TestInfoDisplayId; | 
|  | 317 | mInfo.portalToDisplayId = TestInfoPortalToDisplayId; | 
|  | 318 | mInfo.replaceTouchableRegionWithCrop = TestInfoReplaceTouchableRegionWithCrop; | 
|  | 319 | mInfo.touchableRegionCropHandle = TestInfoTouchableRegionCropHandle; | 
|  | 320 |  | 
|  | 321 | mInfo.applicationInfo.name = TestAppInfoName; | 
|  | 322 | mInfo.applicationInfo.token = TestAppInfoToken; | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 323 | mInfo.applicationInfo.dispatchingTimeoutMillis = | 
|  | 324 | std::chrono::duration_cast<std::chrono::milliseconds>(TestAppInfoDispatchingTimeout) | 
|  | 325 | .count(); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 326 |  | 
|  | 327 | InitializeInputFlinger(); | 
|  | 328 | } | 
|  | 329 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 330 | void InputFlingerServiceTest::TearDown() { | 
|  | 331 | mQuery->resetInputManager(); | 
|  | 332 | } | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 333 |  | 
|  | 334 | void InputFlingerServiceTest::verifyInputWindowInfo(const InputWindowInfo& info) const { | 
|  | 335 | EXPECT_EQ(mInfo, info); | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | void InputFlingerServiceTest::InitializeInputFlinger() { | 
|  | 339 | sp<IBinder> input(defaultServiceManager()->waitForService(kTestServiceName)); | 
|  | 340 | ASSERT_TRUE(input != nullptr); | 
|  | 341 | mService = interface_cast<IInputFlinger>(input); | 
|  | 342 |  | 
|  | 343 | input = defaultServiceManager()->waitForService(kQueryServiceName); | 
|  | 344 | ASSERT_TRUE(input != nullptr); | 
|  | 345 | mQuery = interface_cast<IInputFlingerQuery>(input); | 
|  | 346 | } | 
|  | 347 |  | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 348 | void InputFlingerServiceTest::setInputWindowsByInfos(const std::vector<InputWindowInfo>& infos) { | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 349 | std::unique_lock<std::mutex> lock(mLock); | 
|  | 350 | mService->setInputWindows(infos, mSetInputWindowsListener); | 
|  | 351 | // Verify listener call | 
|  | 352 | EXPECT_NE(mSetInputWindowsFinishedCondition.wait_for(lock, 1s), std::cv_status::timeout); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 353 | } | 
|  | 354 |  | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 355 | void InputFlingerServiceTest::setFocusedWindow(const sp<IBinder> token, | 
|  | 356 | const sp<IBinder> focusedToken, | 
|  | 357 | nsecs_t timestampNanos) { | 
|  | 358 | FocusRequest request; | 
|  | 359 | request.token = TestInfoToken; | 
|  | 360 | request.focusedToken = focusedToken; | 
|  | 361 | request.timestamp = timestampNanos; | 
|  | 362 | mService->setFocusedWindow(request); | 
|  | 363 | // call set input windows and wait for the callback to drain the queue. | 
|  | 364 | setInputWindowsByInfos(std::vector<InputWindowInfo>()); | 
|  | 365 | } | 
|  | 366 |  | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 367 | /** | 
|  | 368 | *  Test InputFlinger service interface SetInputWindows | 
|  | 369 | */ | 
|  | 370 | TEST_F(InputFlingerServiceTest, InputWindow_SetInputWindows) { | 
|  | 371 | std::vector<InputWindowInfo> infos = {getInfo()}; | 
|  | 372 | setInputWindowsByInfos(infos); | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 373 |  | 
|  | 374 | // Verify input windows from service | 
|  | 375 | std::vector<::android::InputWindowInfo> windowInfos; | 
|  | 376 | mQuery->getInputWindows(&windowInfos); | 
|  | 377 | for (const ::android::InputWindowInfo& windowInfo : windowInfos) { | 
|  | 378 | verifyInputWindowInfo(windowInfo); | 
|  | 379 | } | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 380 | } | 
|  | 381 |  | 
|  | 382 | /** | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 383 | *  Test InputFlinger service interface createInputChannel | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 384 | */ | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 385 | TEST_F(InputFlingerServiceTest, CreateInputChannelReturnsUnblockedFd) { | 
|  | 386 | // Test that the unblocked file descriptor flag is kept across processes over binder | 
|  | 387 | // transactions. | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 388 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 389 | InputChannel channel; | 
|  | 390 | ASSERT_TRUE(mService->createInputChannel("testchannels", &channel).isOk()); | 
|  | 391 |  | 
|  | 392 | const base::unique_fd& fd = channel.getFd(); | 
|  | 393 | ASSERT_TRUE(fd.ok()); | 
|  | 394 |  | 
|  | 395 | const int result = fcntl(fd, F_GETFL); | 
|  | 396 | EXPECT_NE(result, -1); | 
|  | 397 | EXPECT_EQ(result & O_NONBLOCK, O_NONBLOCK); | 
|  | 398 | } | 
|  | 399 |  | 
|  | 400 | TEST_F(InputFlingerServiceTest, InputWindow_CreateInputChannel) { | 
|  | 401 | InputChannel channel; | 
|  | 402 | ASSERT_TRUE(mService->createInputChannel("testchannels", &channel).isOk()); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 403 |  | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 404 | std::vector<::android::InputChannel> channels; | 
|  | 405 | mQuery->getInputChannels(&channels); | 
|  | 406 | ASSERT_EQ(channels.size(), 1UL); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 407 | EXPECT_EQ(channels[0].getConnectionToken(), channel.getConnectionToken()); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 408 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 409 | mService->removeInputChannel(channel.getConnectionToken()); | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 410 | mQuery->getInputChannels(&channels); | 
|  | 411 | EXPECT_EQ(channels.size(), 0UL); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 412 | } | 
|  | 413 |  | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 414 | TEST_F(InputFlingerServiceTest, InputWindow_setFocusedWindow) { | 
|  | 415 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 416 | setFocusedWindow(TestInfoToken, nullptr /* focusedToken */, now); | 
|  | 417 |  | 
|  | 418 | FocusRequest request; | 
|  | 419 | mQuery->getLastFocusRequest(&request); | 
|  | 420 |  | 
|  | 421 | EXPECT_EQ(request.token, TestInfoToken); | 
|  | 422 | EXPECT_EQ(request.focusedToken, nullptr); | 
|  | 423 | EXPECT_EQ(request.timestamp, now); | 
|  | 424 | } | 
|  | 425 |  | 
|  | 426 | TEST_F(InputFlingerServiceTest, InputWindow_setFocusedWindowWithFocusedToken) { | 
|  | 427 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 428 | setFocusedWindow(TestInfoToken, FocusedTestInfoToken, now); | 
|  | 429 |  | 
|  | 430 | FocusRequest request; | 
|  | 431 | mQuery->getLastFocusRequest(&request); | 
|  | 432 |  | 
|  | 433 | EXPECT_EQ(request.token, TestInfoToken); | 
|  | 434 | EXPECT_EQ(request.focusedToken, FocusedTestInfoToken); | 
|  | 435 | EXPECT_EQ(request.timestamp, now); | 
|  | 436 | } | 
|  | 437 |  | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 438 | } // namespace android | 
|  | 439 |  | 
|  | 440 | int main(int argc, char** argv) { | 
|  | 441 | pid_t forkPid = fork(); | 
|  | 442 |  | 
|  | 443 | if (forkPid == 0) { | 
|  | 444 | // Server process | 
|  | 445 | android::sp<android::TestInputManager> manager = new android::TestInputManager(); | 
|  | 446 | android::sp<android::TestInputQuery> query = new android::TestInputQuery(manager); | 
|  | 447 |  | 
|  | 448 | android::defaultServiceManager()->addService(android::kTestServiceName, manager, | 
|  | 449 | false /*allowIsolated*/); | 
|  | 450 | android::defaultServiceManager()->addService(android::kQueryServiceName, query, | 
|  | 451 | false /*allowIsolated*/); | 
|  | 452 | android::ProcessState::self()->startThreadPool(); | 
|  | 453 | android::IPCThreadState::self()->joinThreadPool(); | 
|  | 454 | } else { | 
|  | 455 | android::ProcessState::self()->startThreadPool(); | 
|  | 456 | ::testing::InitGoogleTest(&argc, argv); | 
| Chris Ye | c466984 | 2020-07-14 17:10:09 -0700 | [diff] [blame] | 457 | int result = RUN_ALL_TESTS(); | 
|  | 458 | kill(forkPid, SIGKILL); | 
|  | 459 | return result; | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 460 | } | 
|  | 461 | return 0; | 
|  | 462 | } |