1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <BnInputFlingerQuery.h>
#include <IInputFlingerQuery.h>
#include <android/os/BnInputFlinger.h>
#include <android/os/IInputFlinger.h>
#include <binder/Binder.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/Parcel.h>
#include <binder/ProcessState.h>
#include <input/Input.h>
#include <input/InputTransport.h>
#include <gtest/gtest.h>
#include <inttypes.h>
#include <linux/uinput.h>
#include <log/log.h>
#include <ui/Rect.h>
#include <ui/Region.h>
#include <chrono>
#include <thread>
#include <unordered_map>
#define TAG "InputFlingerServiceTest"
using android::gui::FocusRequest;
using android::os::BnInputFlinger;
using android::os::IInputFlinger;
using std::chrono_literals::operator""ms;
using std::chrono_literals::operator""s;
namespace android {
static const String16 kTestServiceName = String16("InputFlingerService");
static const String16 kQueryServiceName = String16("InputFlingerQueryService");
// --- InputFlingerServiceTest ---
class InputFlingerServiceTest : public testing::Test {
public:
void SetUp() override;
void TearDown() override;
protected:
void InitializeInputFlinger();
sp<IInputFlinger> mService;
sp<IInputFlingerQuery> mQuery;
private:
std::unique_ptr<InputChannel> mServerChannel, mClientChannel;
std::mutex mLock;
};
class TestInputManager : public BnInputFlinger {
protected:
virtual ~TestInputManager(){};
public:
TestInputManager(){};
binder::Status getInputChannels(std::vector<::android::InputChannel>* channels);
status_t dump(int fd, const Vector<String16>& args) override;
binder::Status createInputChannel(const std::string& name, InputChannel* outChannel) override;
binder::Status removeInputChannel(const sp<IBinder>& connectionToken) override;
binder::Status setFocusedWindow(const FocusRequest&) override;
void reset();
private:
mutable Mutex mLock;
std::vector<std::shared_ptr<InputChannel>> mInputChannels;
};
class TestInputQuery : public BnInputFlingerQuery {
public:
TestInputQuery(sp<android::TestInputManager> manager) : mManager(manager){};
binder::Status getInputChannels(std::vector<::android::InputChannel>* channels) override;
binder::Status resetInputManager() override;
private:
sp<android::TestInputManager> mManager;
};
binder::Status TestInputQuery::getInputChannels(std::vector<::android::InputChannel>* channels) {
return mManager->getInputChannels(channels);
}
binder::Status TestInputQuery::resetInputManager() {
mManager->reset();
return binder::Status::ok();
}
binder::Status TestInputManager::createInputChannel(const std::string& name,
InputChannel* outChannel) {
AutoMutex _l(mLock);
std::unique_ptr<InputChannel> serverChannel;
std::unique_ptr<InputChannel> clientChannel;
InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
clientChannel->copyTo(*outChannel);
mInputChannels.emplace_back(std::move(serverChannel));
return binder::Status::ok();
}
binder::Status TestInputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
AutoMutex _l(mLock);
auto it = std::find_if(mInputChannels.begin(), mInputChannels.end(),
[&](std::shared_ptr<InputChannel>& c) {
return c->getConnectionToken() == connectionToken;
});
if (it != mInputChannels.end()) {
mInputChannels.erase(it);
}
return binder::Status::ok();
}
status_t TestInputManager::dump(int fd, const Vector<String16>& args) {
std::string dump;
dump += " InputFlinger dump\n";
::write(fd, dump.c_str(), dump.size());
return NO_ERROR;
}
binder::Status TestInputManager::getInputChannels(std::vector<::android::InputChannel>* channels) {
channels->clear();
for (std::shared_ptr<InputChannel>& channel : mInputChannels) {
channels->push_back(*channel);
}
return binder::Status::ok();
}
binder::Status TestInputManager::setFocusedWindow(const FocusRequest& request) {
return binder::Status::ok();
}
void TestInputManager::reset() {
mInputChannels.clear();
}
void InputFlingerServiceTest::SetUp() {
InputChannel::openInputChannelPair("testchannels", mServerChannel, mClientChannel);
InitializeInputFlinger();
}
void InputFlingerServiceTest::TearDown() {
mQuery->resetInputManager();
}
void InputFlingerServiceTest::InitializeInputFlinger() {
sp<IBinder> input(defaultServiceManager()->waitForService(kTestServiceName));
ASSERT_TRUE(input != nullptr);
mService = interface_cast<IInputFlinger>(input);
input = defaultServiceManager()->waitForService(kQueryServiceName);
ASSERT_TRUE(input != nullptr);
mQuery = interface_cast<IInputFlingerQuery>(input);
}
/**
* Test InputFlinger service interface createInputChannel
*/
TEST_F(InputFlingerServiceTest, CreateInputChannelReturnsUnblockedFd) {
// Test that the unblocked file descriptor flag is kept across processes over binder
// transactions.
InputChannel channel;
ASSERT_TRUE(mService->createInputChannel("testchannels", &channel).isOk());
const base::unique_fd& fd = channel.getFd();
ASSERT_TRUE(fd.ok());
const int result = fcntl(fd, F_GETFL);
EXPECT_NE(result, -1);
EXPECT_EQ(result & O_NONBLOCK, O_NONBLOCK);
}
TEST_F(InputFlingerServiceTest, CreateInputChannel) {
InputChannel channel;
ASSERT_TRUE(mService->createInputChannel("testchannels", &channel).isOk());
std::vector<::android::InputChannel> channels;
mQuery->getInputChannels(&channels);
ASSERT_EQ(channels.size(), 1UL);
EXPECT_EQ(channels[0].getConnectionToken(), channel.getConnectionToken());
mService->removeInputChannel(channel.getConnectionToken());
mQuery->getInputChannels(&channels);
EXPECT_EQ(channels.size(), 0UL);
}
} // namespace android
int main(int argc, char** argv) {
pid_t forkPid = fork();
if (forkPid == 0) {
// Server process
android::sp<android::TestInputManager> manager = new android::TestInputManager();
android::sp<android::TestInputQuery> query = new android::TestInputQuery(manager);
android::defaultServiceManager()->addService(android::kTestServiceName, manager,
false /*allowIsolated*/);
android::defaultServiceManager()->addService(android::kQueryServiceName, query,
false /*allowIsolated*/);
android::ProcessState::self()->startThreadPool();
android::IPCThreadState::self()->joinThreadPool();
} else {
android::ProcessState::self()->startThreadPool();
::testing::InitGoogleTest(&argc, argv);
int result = RUN_ALL_TESTS();
kill(forkPid, SIGKILL);
return result;
}
return 0;
}
|