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> |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 21 | #include <android/os/IInputFlinger.h> |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 22 | |
| 23 | #include <binder/Binder.h> |
| 24 | #include <binder/IPCThreadState.h> |
| 25 | #include <binder/IServiceManager.h> |
| 26 | #include <binder/Parcel.h> |
| 27 | #include <binder/ProcessState.h> |
| 28 | |
| 29 | #include <input/Input.h> |
| 30 | #include <input/InputTransport.h> |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 31 | |
| 32 | #include <gtest/gtest.h> |
| 33 | #include <inttypes.h> |
| 34 | #include <linux/uinput.h> |
| 35 | #include <log/log.h> |
| 36 | #include <ui/Rect.h> |
| 37 | #include <ui/Region.h> |
| 38 | #include <chrono> |
| 39 | #include <thread> |
| 40 | #include <unordered_map> |
| 41 | |
| 42 | #define TAG "InputFlingerServiceTest" |
| 43 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 44 | using android::gui::FocusRequest; |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 45 | using android::os::BnInputFlinger; |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 46 | using android::os::IInputFlinger; |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 47 | |
| 48 | using std::chrono_literals::operator""ms; |
| 49 | using std::chrono_literals::operator""s; |
| 50 | |
| 51 | namespace android { |
| 52 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 53 | static const String16 kTestServiceName = String16("InputFlingerService"); |
| 54 | static const String16 kQueryServiceName = String16("InputFlingerQueryService"); |
| 55 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 56 | // --- InputFlingerServiceTest --- |
| 57 | class InputFlingerServiceTest : public testing::Test { |
| 58 | public: |
| 59 | void SetUp() override; |
| 60 | void TearDown() override; |
| 61 | |
| 62 | protected: |
| 63 | void InitializeInputFlinger(); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 64 | |
| 65 | sp<IInputFlinger> mService; |
| 66 | sp<IInputFlingerQuery> mQuery; |
| 67 | |
| 68 | private: |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 69 | std::unique_ptr<InputChannel> mServerChannel, mClientChannel; |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 70 | std::mutex mLock; |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 73 | |
| 74 | class TestInputManager : public BnInputFlinger { |
| 75 | protected: |
| 76 | virtual ~TestInputManager(){}; |
| 77 | |
| 78 | public: |
| 79 | TestInputManager(){}; |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 80 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 81 | binder::Status getInputChannels(std::vector<::android::InputChannel>* channels); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 82 | |
| 83 | status_t dump(int fd, const Vector<String16>& args) override; |
| 84 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 85 | binder::Status createInputChannel(const std::string& name, InputChannel* outChannel) override; |
| 86 | binder::Status removeInputChannel(const sp<IBinder>& connectionToken) override; |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 87 | binder::Status setFocusedWindow(const FocusRequest&) override; |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 88 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 89 | void reset(); |
| 90 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 91 | private: |
| 92 | mutable Mutex mLock; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 93 | std::vector<std::shared_ptr<InputChannel>> mInputChannels; |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | class TestInputQuery : public BnInputFlingerQuery { |
| 97 | public: |
| 98 | TestInputQuery(sp<android::TestInputManager> manager) : mManager(manager){}; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 99 | binder::Status getInputChannels(std::vector<::android::InputChannel>* channels) override; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 100 | binder::Status resetInputManager() override; |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 101 | |
| 102 | private: |
| 103 | sp<android::TestInputManager> mManager; |
| 104 | }; |
| 105 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 106 | binder::Status TestInputQuery::getInputChannels(std::vector<::android::InputChannel>* channels) { |
| 107 | return mManager->getInputChannels(channels); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 110 | binder::Status TestInputQuery::resetInputManager() { |
| 111 | mManager->reset(); |
| 112 | return binder::Status::ok(); |
| 113 | } |
| 114 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 115 | binder::Status TestInputManager::createInputChannel(const std::string& name, |
| 116 | InputChannel* outChannel) { |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 117 | AutoMutex _l(mLock); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 118 | std::unique_ptr<InputChannel> serverChannel; |
| 119 | std::unique_ptr<InputChannel> clientChannel; |
| 120 | InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 121 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 122 | clientChannel->copyTo(*outChannel); |
| 123 | |
| 124 | mInputChannels.emplace_back(std::move(serverChannel)); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 125 | |
| 126 | return binder::Status::ok(); |
| 127 | } |
| 128 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 129 | binder::Status TestInputManager::removeInputChannel(const sp<IBinder>& connectionToken) { |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 130 | AutoMutex _l(mLock); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 131 | |
| 132 | auto it = std::find_if(mInputChannels.begin(), mInputChannels.end(), |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 133 | [&](std::shared_ptr<InputChannel>& c) { |
| 134 | return c->getConnectionToken() == connectionToken; |
| 135 | }); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 136 | if (it != mInputChannels.end()) { |
| 137 | mInputChannels.erase(it); |
| 138 | } |
| 139 | |
| 140 | return binder::Status::ok(); |
| 141 | } |
| 142 | |
| 143 | status_t TestInputManager::dump(int fd, const Vector<String16>& args) { |
| 144 | std::string dump; |
| 145 | |
| 146 | dump += " InputFlinger dump\n"; |
| 147 | |
| 148 | ::write(fd, dump.c_str(), dump.size()); |
| 149 | return NO_ERROR; |
| 150 | } |
| 151 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 152 | binder::Status TestInputManager::getInputChannels(std::vector<::android::InputChannel>* channels) { |
| 153 | channels->clear(); |
| 154 | for (std::shared_ptr<InputChannel>& channel : mInputChannels) { |
| 155 | channels->push_back(*channel); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 156 | } |
| 157 | return binder::Status::ok(); |
| 158 | } |
| 159 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 160 | binder::Status TestInputManager::setFocusedWindow(const FocusRequest& request) { |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 161 | return binder::Status::ok(); |
| 162 | } |
| 163 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 164 | void TestInputManager::reset() { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 165 | mInputChannels.clear(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 168 | void InputFlingerServiceTest::SetUp() { |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 169 | InputChannel::openInputChannelPair("testchannels", mServerChannel, mClientChannel); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 170 | InitializeInputFlinger(); |
| 171 | } |
| 172 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 173 | void InputFlingerServiceTest::TearDown() { |
| 174 | mQuery->resetInputManager(); |
| 175 | } |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 176 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 177 | void InputFlingerServiceTest::InitializeInputFlinger() { |
| 178 | sp<IBinder> input(defaultServiceManager()->waitForService(kTestServiceName)); |
| 179 | ASSERT_TRUE(input != nullptr); |
| 180 | mService = interface_cast<IInputFlinger>(input); |
| 181 | |
| 182 | input = defaultServiceManager()->waitForService(kQueryServiceName); |
| 183 | ASSERT_TRUE(input != nullptr); |
| 184 | mQuery = interface_cast<IInputFlingerQuery>(input); |
| 185 | } |
| 186 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 187 | /** |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 188 | * Test InputFlinger service interface createInputChannel |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 189 | */ |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 190 | TEST_F(InputFlingerServiceTest, CreateInputChannelReturnsUnblockedFd) { |
| 191 | // Test that the unblocked file descriptor flag is kept across processes over binder |
| 192 | // transactions. |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 193 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 194 | InputChannel channel; |
| 195 | ASSERT_TRUE(mService->createInputChannel("testchannels", &channel).isOk()); |
| 196 | |
| 197 | const base::unique_fd& fd = channel.getFd(); |
| 198 | ASSERT_TRUE(fd.ok()); |
| 199 | |
| 200 | const int result = fcntl(fd, F_GETFL); |
| 201 | EXPECT_NE(result, -1); |
| 202 | EXPECT_EQ(result & O_NONBLOCK, O_NONBLOCK); |
| 203 | } |
| 204 | |
chaviw | bf023a6 | 2021-06-07 16:00:06 -0500 | [diff] [blame^] | 205 | TEST_F(InputFlingerServiceTest, CreateInputChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 206 | InputChannel channel; |
| 207 | ASSERT_TRUE(mService->createInputChannel("testchannels", &channel).isOk()); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 208 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 209 | std::vector<::android::InputChannel> channels; |
| 210 | mQuery->getInputChannels(&channels); |
| 211 | ASSERT_EQ(channels.size(), 1UL); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 212 | EXPECT_EQ(channels[0].getConnectionToken(), channel.getConnectionToken()); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 213 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 214 | mService->removeInputChannel(channel.getConnectionToken()); |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 215 | mQuery->getInputChannels(&channels); |
| 216 | EXPECT_EQ(channels.size(), 0UL); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | } // namespace android |
| 220 | |
| 221 | int main(int argc, char** argv) { |
| 222 | pid_t forkPid = fork(); |
| 223 | |
| 224 | if (forkPid == 0) { |
| 225 | // Server process |
| 226 | android::sp<android::TestInputManager> manager = new android::TestInputManager(); |
| 227 | android::sp<android::TestInputQuery> query = new android::TestInputQuery(manager); |
| 228 | |
| 229 | android::defaultServiceManager()->addService(android::kTestServiceName, manager, |
| 230 | false /*allowIsolated*/); |
| 231 | android::defaultServiceManager()->addService(android::kQueryServiceName, query, |
| 232 | false /*allowIsolated*/); |
| 233 | android::ProcessState::self()->startThreadPool(); |
| 234 | android::IPCThreadState::self()->joinThreadPool(); |
| 235 | } else { |
| 236 | android::ProcessState::self()->startThreadPool(); |
| 237 | ::testing::InitGoogleTest(&argc, argv); |
Chris Ye | c466984 | 2020-07-14 17:10:09 -0700 | [diff] [blame] | 238 | int result = RUN_ALL_TESTS(); |
| 239 | kill(forkPid, SIGKILL); |
| 240 | return result; |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 241 | } |
| 242 | return 0; |
| 243 | } |