blob: d6543f2de78edd90fa1c8d6b4cb67ac3df9e6db5 [file] [log] [blame]
Chris Ye0783e992020-06-02 21:34:49 -07001/*
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
47using android::os::BnInputFlinger;
48using android::os::BnSetInputWindowsListener;
49using android::os::IInputFlinger;
50using android::os::ISetInputWindowsListener;
51
52using std::chrono_literals::operator""ms;
53using std::chrono_literals::operator""s;
54
55namespace android {
56
57static const sp<IBinder> TestInfoToken = new BBinder();
Vishnu Naire798b472020-07-23 13:52:21 -070058static const sp<IBinder> FocusedTestInfoToken = new BBinder();
Chris Ye0783e992020-06-02 21:34:49 -070059static constexpr int32_t TestInfoId = 1;
60static const std::string TestInfoName = "InputFlingerServiceTestInputWindowInfo";
Michael Wright44753b12020-07-08 13:48:11 +010061static constexpr Flags<InputWindowInfo::Flag> TestInfoFlags = InputWindowInfo::Flag::NOT_FOCUSABLE;
62static constexpr InputWindowInfo::Type TestInfoType = InputWindowInfo::Type::INPUT_METHOD;
Chris Ye0783e992020-06-02 21:34:49 -070063static constexpr std::chrono::duration TestInfoDispatchingTimeout = 2532ms;
64static constexpr int32_t TestInfoFrameLeft = 93;
65static constexpr int32_t TestInfoFrameTop = 34;
66static constexpr int32_t TestInfoFrameRight = 16;
67static constexpr int32_t TestInfoFrameBottom = 19;
68static constexpr int32_t TestInfoSurfaceInset = 17;
69static constexpr float TestInfoGlobalScaleFactor = 0.3;
70static constexpr float TestInfoWindowXScale = 0.4;
71static constexpr float TestInfoWindowYScale = 0.5;
72static const Rect TestInfoTouchableRegionRect = {100 /* left */, 150 /* top */, 400 /* right */,
73 450 /* bottom */};
74static const Region TestInfoTouchableRegion(TestInfoTouchableRegionRect);
75static constexpr bool TestInfoVisible = false;
76static constexpr bool TestInfoCanReceiveKeys = false;
77static constexpr bool TestInfoTrustedOverlay = true;
78static constexpr bool TestInfoHasFocus = false;
79static constexpr bool TestInfoHasWallpaper = false;
80static constexpr bool TestInfoPaused = false;
81static constexpr int32_t TestInfoOwnerPid = 19;
82static constexpr int32_t TestInfoOwnerUid = 24;
Michael Wright44753b12020-07-08 13:48:11 +010083static constexpr InputWindowInfo::Feature TestInfoInputFeatures =
84 InputWindowInfo::Feature::NO_INPUT_CHANNEL;
Chris Ye0783e992020-06-02 21:34:49 -070085static constexpr int32_t TestInfoDisplayId = 34;
86static constexpr int32_t TestInfoPortalToDisplayId = 2;
87static constexpr bool TestInfoReplaceTouchableRegionWithCrop = true;
88static const sp<IBinder> TestInfoTouchableRegionCropHandle = new BBinder();
89
90static const std::string TestAppInfoName = "InputFlingerServiceTestInputApplicationInfo";
91static const sp<IBinder> TestAppInfoToken = new BBinder();
92static constexpr std::chrono::duration TestAppInfoDispatchingTimeout = 12345678ms;
93
94static const String16 kTestServiceName = String16("InputFlingerService");
95static const String16 kQueryServiceName = String16("InputFlingerQueryService");
96
97struct SetInputWindowsListener;
98// --- InputFlingerServiceTest ---
99class InputFlingerServiceTest : public testing::Test {
100public:
101 void SetUp() override;
102 void TearDown() override;
103
104protected:
105 void InitializeInputFlinger();
Vishnu Naire798b472020-07-23 13:52:21 -0700106 void setInputWindowsByInfos(const std::vector<InputWindowInfo>& infos);
107 void setFocusedWindow(const sp<IBinder> token, const sp<IBinder> focusedToken,
108 nsecs_t timestampNanos);
Chris Ye0783e992020-06-02 21:34:49 -0700109
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
117private:
118 sp<SetInputWindowsListener> mSetInputWindowsListener;
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500119 std::unique_ptr<InputChannel> mServerChannel, mClientChannel;
Chris Ye0783e992020-06-02 21:34:49 -0700120 InputWindowInfo mInfo;
121 std::mutex mLock;
122 std::condition_variable mSetInputWindowsFinishedCondition;
123};
124
125struct 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
133class TestInputManager : public BnInputFlinger {
134protected:
135 virtual ~TestInputManager(){};
136
137public:
138 TestInputManager(){};
139 void checkFdFlags(const android::base::unique_fd& fd);
140
141 binder::Status getInputWindows(std::vector<::android::InputWindowInfo>* inputHandles);
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500142 binder::Status getInputChannels(std::vector<::android::InputChannel>* channels);
Vishnu Naire798b472020-07-23 13:52:21 -0700143 binder::Status getLastFocusRequest(FocusRequest*);
Chris Ye0783e992020-06-02 21:34:49 -0700144
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 Vishniakouce5ab082020-07-09 17:03:21 -0500151 binder::Status registerInputChannel(const InputChannel& channel) override;
152 binder::Status unregisterInputChannel(const InputChannel& channel) override;
Vishnu Naire798b472020-07-23 13:52:21 -0700153 binder::Status setFocusedWindow(const FocusRequest&) override;
Chris Ye0783e992020-06-02 21:34:49 -0700154
155private:
156 mutable Mutex mLock;
157 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> mHandlesPerDisplay;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500158 std::vector<std::shared_ptr<InputChannel>> mInputChannels;
Vishnu Naire798b472020-07-23 13:52:21 -0700159 FocusRequest mFocusRequest;
Chris Ye0783e992020-06-02 21:34:49 -0700160};
161
162class TestInputQuery : public BnInputFlingerQuery {
163public:
164 TestInputQuery(sp<android::TestInputManager> manager) : mManager(manager){};
165 binder::Status getInputWindows(std::vector<::android::InputWindowInfo>* inputHandles) override;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500166 binder::Status getInputChannels(std::vector<::android::InputChannel>* channels) override;
Vishnu Naire798b472020-07-23 13:52:21 -0700167 binder::Status getLastFocusRequest(FocusRequest*) override;
Chris Ye0783e992020-06-02 21:34:49 -0700168
169private:
170 sp<android::TestInputManager> mManager;
171};
172
173binder::Status TestInputQuery::getInputWindows(
174 std::vector<::android::InputWindowInfo>* inputHandles) {
175 return mManager->getInputWindows(inputHandles);
176}
177
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500178binder::Status TestInputQuery::getInputChannels(std::vector<::android::InputChannel>* channels) {
179 return mManager->getInputChannels(channels);
Chris Ye0783e992020-06-02 21:34:49 -0700180}
181
Vishnu Naire798b472020-07-23 13:52:21 -0700182binder::Status TestInputQuery::getLastFocusRequest(FocusRequest* request) {
183 return mManager->getLastFocusRequest(request);
184}
185
Chris Ye0783e992020-06-02 21:34:49 -0700186binder::Status SetInputWindowsListener::onSetInputWindowsFinished() {
187 if (mCbFunc != nullptr) {
188 mCbFunc();
189 }
190 return binder::Status::ok();
191}
192
193binder::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
208void 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 Vishniakouce5ab082020-07-09 17:03:21 -0500214binder::Status TestInputManager::registerInputChannel(const InputChannel& channel) {
Chris Ye0783e992020-06-02 21:34:49 -0700215 AutoMutex _l(mLock);
216 // check Fd flags
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500217 checkFdFlags(channel.getFd());
Chris Ye0783e992020-06-02 21:34:49 -0700218
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500219 mInputChannels.push_back(channel.dup());
Chris Ye0783e992020-06-02 21:34:49 -0700220
221 return binder::Status::ok();
222}
223
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500224binder::Status TestInputManager::unregisterInputChannel(const InputChannel& channel) {
Chris Ye0783e992020-06-02 21:34:49 -0700225 AutoMutex _l(mLock);
226 // check Fd flags
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500227 checkFdFlags(channel.getFd());
Chris Ye0783e992020-06-02 21:34:49 -0700228
229 auto it = std::find_if(mInputChannels.begin(), mInputChannels.end(),
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500230 [&](std::shared_ptr<InputChannel>& c) { return *c == channel; });
Chris Ye0783e992020-06-02 21:34:49 -0700231 if (it != mInputChannels.end()) {
232 mInputChannels.erase(it);
233 }
234
235 return binder::Status::ok();
236}
237
238status_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
247binder::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 Vishniakouce5ab082020-07-09 17:03:21 -0500257binder::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 Ye0783e992020-06-02 21:34:49 -0700261 }
262 return binder::Status::ok();
263}
264
Vishnu Naire798b472020-07-23 13:52:21 -0700265binder::Status TestInputManager::getLastFocusRequest(FocusRequest* request) {
266 *request = mFocusRequest;
267 return binder::Status::ok();
268}
269
270binder::Status TestInputManager::setFocusedWindow(const FocusRequest& request) {
271 mFocusRequest = request;
272 return binder::Status::ok();
273}
274
Chris Ye0783e992020-06-02 21:34:49 -0700275void 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 Wright44753b12020-07-08 13:48:11 +0100285 mInfo.flags = TestInfoFlags;
286 mInfo.type = TestInfoType;
Chris Ye0783e992020-06-02 21:34:49 -0700287 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;
chaviw1ff3d1e2020-07-01 15:53:47 -0700294 mInfo.transform.set(std::array<float, 9>{TestInfoWindowXScale, 0, TestInfoFrameLeft, 0,
295 TestInfoWindowYScale, TestInfoFrameTop, 0, 0, 1});
Chris Ye0783e992020-06-02 21:34:49 -0700296 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;
313 mInfo.applicationInfo.dispatchingTimeout = TestAppInfoDispatchingTimeout;
314
315 InitializeInputFlinger();
316}
317
318void InputFlingerServiceTest::TearDown() {}
319
320void InputFlingerServiceTest::verifyInputWindowInfo(const InputWindowInfo& info) const {
321 EXPECT_EQ(mInfo, info);
322}
323
324void 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 Naire798b472020-07-23 13:52:21 -0700334void InputFlingerServiceTest::setInputWindowsByInfos(const std::vector<InputWindowInfo>& infos) {
Chris Ye0783e992020-06-02 21:34:49 -0700335 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 Ye0783e992020-06-02 21:34:49 -0700339}
340
Vishnu Naire798b472020-07-23 13:52:21 -0700341void 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 Ye0783e992020-06-02 21:34:49 -0700353/**
354 * Test InputFlinger service interface SetInputWindows
355 */
356TEST_F(InputFlingerServiceTest, InputWindow_SetInputWindows) {
357 std::vector<InputWindowInfo> infos = {getInfo()};
358 setInputWindowsByInfos(infos);
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500359
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 Ye0783e992020-06-02 21:34:49 -0700366}
367
368/**
369 * Test InputFlinger service interface registerInputChannel
370 */
371TEST_F(InputFlingerServiceTest, InputWindow_RegisterInputChannel) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500372 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Chris Ye0783e992020-06-02 21:34:49 -0700373
374 InputChannel::openInputChannelPair("testchannels", serverChannel, clientChannel);
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500375 mService->registerInputChannel(*serverChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700376
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500377 std::vector<::android::InputChannel> channels;
378 mQuery->getInputChannels(&channels);
379 ASSERT_EQ(channels.size(), 1UL);
380 EXPECT_EQ(channels[0], *serverChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700381
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500382 mService->unregisterInputChannel(*serverChannel);
383 mQuery->getInputChannels(&channels);
384 EXPECT_EQ(channels.size(), 0UL);
Chris Ye0783e992020-06-02 21:34:49 -0700385}
386
387/**
388 * Test InputFlinger service interface registerInputChannel with invalid cases
389 */
390TEST_F(InputFlingerServiceTest, InputWindow_RegisterInputChannelInvalid) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500391 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Chris Ye0783e992020-06-02 21:34:49 -0700392 InputChannel::openInputChannelPair("testchannels", serverChannel, clientChannel);
393
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500394 std::vector<::android::InputChannel> channels;
395 mQuery->getInputChannels(&channels);
396 EXPECT_EQ(channels.size(), 0UL);
Chris Ye0783e992020-06-02 21:34:49 -0700397
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500398 mService->registerInputChannel(InputChannel());
399 mService->unregisterInputChannel(*clientChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700400
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500401 mService->registerInputChannel(*serverChannel);
402 mService->registerInputChannel(*clientChannel);
403 mQuery->getInputChannels(&channels);
404 EXPECT_EQ(channels.size(), 2UL);
Chris Ye0783e992020-06-02 21:34:49 -0700405
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500406 mService->unregisterInputChannel(*clientChannel);
407 mService->unregisterInputChannel(*serverChannel);
408 mQuery->getInputChannels(&channels);
409 EXPECT_EQ(channels.size(), 0UL);
Chris Ye0783e992020-06-02 21:34:49 -0700410}
411
Vishnu Naire798b472020-07-23 13:52:21 -0700412TEST_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
424TEST_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 Ye0783e992020-06-02 21:34:49 -0700436} // namespace android
437
438int 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 Yec4669842020-07-14 17:10:09 -0700455 int result = RUN_ALL_TESTS();
456 kill(forkPid, SIGKILL);
457 return result;
Chris Ye0783e992020-06-02 21:34:49 -0700458 }
459 return 0;
460}