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