1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "device_manager_test.h"
17
18 #include <unistd.h>
19 #include "ddm_adapter.h"
20
21 #undef LOG_TAG
22 #define LOG_TAG "IntentionDeviceManagerTest"
23
24 namespace OHOS {
25 namespace Msdp {
26 namespace DeviceStatus {
27
28 using namespace testing::ext;
29 namespace {
30 struct device_status_epoll_event {
31 int32_t fd { -1 };
32 EpollEventType event_type { EPOLL_EVENT_BEGIN };
33 };
34
35 ContextService *g_instance = nullptr;
36 constexpr int32_t TIME_WAIT_FOR_OP_MS { 100 };
37 constexpr int32_t DEFAULT_WAIT_TIME_MS { 1000 };
38 constexpr int32_t WAIT_FOR_ONCE { 1 };
39 constexpr int32_t MAX_N_RETRIES { 100 };
40 const std::string TEST_DEV_NODE { "/dev/input/TestDeviceNode" };
41 } // namespace
42
ContextService()43 ContextService::ContextService()
44 {
45 ddm_ = std::make_unique<DDMAdapter>();
46 FI_HILOGI("OHOS_BUILD_ENABLE_INTENTION_FRAMEWORK is on");
47 OnStart();
48 }
49
~ContextService()50 ContextService::~ContextService()
51 {
52 OnStop();
53 }
54
GetDelegateTasks()55 IDelegateTasks& ContextService::GetDelegateTasks()
56 {
57 return delegateTasks_;
58 }
59
GetDeviceManager()60 IDeviceManager& ContextService::GetDeviceManager()
61 {
62 return devMgr_;
63 }
64
GetTimerManager()65 ITimerManager& ContextService::GetTimerManager()
66 {
67 return timerMgr_;
68 }
69
GetDragManager()70 IDragManager& ContextService::GetDragManager()
71 {
72 return dragMgr_;
73 }
74
GetInstance()75 ContextService* ContextService::GetInstance()
76 {
77 static std::once_flag flag;
78 std::call_once(flag, [&]() {
79 ContextService *cooContext = new (std::nothrow) ContextService();
80 CHKPL(cooContext);
81 g_instance = cooContext;
82 });
83 return g_instance;
84 }
85
GetSocketSessionManager()86 ISocketSessionManager& ContextService::GetSocketSessionManager()
87 {
88 return socketSessionMgr_;
89 }
90
GetDDM()91 IDDMAdapter& ContextService::GetDDM()
92 {
93 return *ddm_;
94 }
95
GetPluginManager()96 IPluginManager& ContextService::GetPluginManager()
97 {
98 return *pluginMgr_;
99 }
100
GetInput()101 IInputAdapter& ContextService::GetInput()
102 {
103 return *input_;
104 }
105
GetDSoftbus()106 IDSoftbusAdapter& ContextService::GetDSoftbus()
107 {
108 return *dsoftbusAda_;
109 }
110
Init()111 bool ContextService::Init()
112 {
113 CALL_DEBUG_ENTER;
114 if (EpollCreate() != RET_OK) {
115 FI_HILOGE("Create epoll failed");
116 return false;
117 }
118 if (InitDelegateTasks() != RET_OK) {
119 FI_HILOGE("Delegate tasks init failed");
120 goto INIT_FAIL;
121 }
122 if (InitTimerMgr() != RET_OK) {
123 FI_HILOGE("TimerMgr init failed");
124 goto INIT_FAIL;
125 }
126 if (InitDevMgr() != RET_OK) {
127 FI_HILOGE("DevMgr init failed");
128 goto INIT_FAIL;
129 }
130
131 return true;
132
133 INIT_FAIL:
134 EpollClose();
135 return false;
136 }
InitDevMgr()137 int32_t ContextService::InitDevMgr()
138 {
139 CALL_DEBUG_ENTER;
140 int32_t ret = devMgr_.Init(this);
141 if (ret != RET_OK) {
142 FI_HILOGE("DevMgr init failed");
143 return ret;
144 }
145 return ret;
146 }
147
InitTimerMgr()148 int32_t ContextService::InitTimerMgr()
149 {
150 CALL_DEBUG_ENTER;
151 int32_t ret = timerMgr_.Init(this);
152 if (ret != RET_OK) {
153 FI_HILOGE("TimerMgr init failed");
154 return ret;
155 }
156
157 ret = AddEpoll(EPOLL_EVENT_TIMER, timerMgr_.GetTimerFd());
158 if (ret != RET_OK) {
159 FI_HILOGE("AddEpoll for timer failed");
160 }
161 return ret;
162 }
163
InitDelegateTasks()164 int32_t ContextService::InitDelegateTasks()
165 {
166 CALL_DEBUG_ENTER;
167 if (!delegateTasks_.Init()) {
168 FI_HILOGE("The delegate task init failed");
169 return RET_ERR;
170 }
171 int32_t ret = AddEpoll(EPOLL_EVENT_ETASK, delegateTasks_.GetReadFd());
172 if (ret != RET_OK) {
173 FI_HILOGE("AddEpoll error ret:%{public}d", ret);
174 }
175 FI_HILOGI("AddEpoll, epollfd:%{public}d, fd:%{public}d", epollFd_, delegateTasks_.GetReadFd());
176 return ret;
177 }
178
EpollCreate()179 int32_t ContextService::EpollCreate()
180 {
181 CALL_DEBUG_ENTER;
182 epollFd_ = ::epoll_create1(EPOLL_CLOEXEC);
183 if (epollFd_ < 0) {
184 FI_HILOGE("epoll_create1 failed:%{public}s", ::strerror(errno));
185 return RET_ERR;
186 }
187 return RET_OK;
188 }
189
AddEpoll(EpollEventType type,int32_t fd)190 int32_t ContextService::AddEpoll(EpollEventType type, int32_t fd)
191 {
192 CALL_DEBUG_ENTER;
193 if (!(type >= EPOLL_EVENT_BEGIN && type < EPOLL_EVENT_END)) {
194 FI_HILOGE("Invalid type:%{public}d", type);
195 return RET_ERR;
196 }
197 if (fd < 0) {
198 FI_HILOGE("Invalid fd:%{public}d", fd);
199 return RET_ERR;
200 }
201 auto eventData = static_cast<device_status_epoll_event*>(malloc(sizeof(device_status_epoll_event)));
202 if (!eventData) {
203 FI_HILOGE("Malloc failed");
204 return RET_ERR;
205 }
206 eventData->fd = fd;
207 eventData->event_type = type;
208 FI_HILOGD("EventData:[fd:%{public}d, type:%{public}d]", eventData->fd, eventData->event_type);
209
210 struct epoll_event ev {};
211 ev.events = EPOLLIN;
212 ev.data.ptr = eventData;
213 if (EpollCtl(fd, EPOLL_CTL_ADD, ev) != RET_OK) {
214 free(eventData);
215 eventData = nullptr;
216 ev.data.ptr = nullptr;
217 FI_HILOGE("EpollCtl failed");
218 return RET_ERR;
219 }
220 return RET_OK;
221 }
222
DelEpoll(EpollEventType type,int32_t fd)223 int32_t ContextService::DelEpoll(EpollEventType type, int32_t fd)
224 {
225 CALL_DEBUG_ENTER;
226 if (!(type >= EPOLL_EVENT_BEGIN && type < EPOLL_EVENT_END)) {
227 FI_HILOGE("Invalid type:%{public}d", type);
228 return RET_ERR;
229 }
230 if (fd < 0) {
231 FI_HILOGE("Invalid fd:%{public}d", fd);
232 return RET_ERR;
233 }
234 struct epoll_event ev {};
235 if (EpollCtl(fd, EPOLL_CTL_DEL, ev) != RET_OK) {
236 FI_HILOGE("DelEpoll failed");
237 return RET_ERR;
238 }
239 return RET_OK;
240 }
241
EpollClose()242 void ContextService::EpollClose()
243 {
244 CALL_DEBUG_ENTER;
245 if (epollFd_ >= 0) {
246 if (close(epollFd_) < 0) {
247 FI_HILOGE("Close epoll fd failed, error:%{public}s, epollFd_:%{public}d", strerror(errno), epollFd_);
248 }
249 epollFd_ = -1;
250 }
251 }
252
EpollCtl(int32_t fd,int32_t op,struct epoll_event & event)253 int32_t ContextService::EpollCtl(int32_t fd, int32_t op, struct epoll_event &event)
254 {
255 CALL_DEBUG_ENTER;
256 if (fd < 0) {
257 FI_HILOGE("Invalid fd:%{public}d", fd);
258 return RET_ERR;
259 }
260 if (epollFd_ < 0) {
261 FI_HILOGE("Invalid epollFd:%{public}d", epollFd_);
262 return RET_ERR;
263 }
264 if (::epoll_ctl(epollFd_, op, fd, &event) != 0) {
265 FI_HILOGE("epoll_ctl(%{public}d,%{public}d,%{public}d) failed:%{public}s", epollFd_, op, fd, ::strerror(errno));
266 return RET_ERR;
267 }
268 return RET_OK;
269 }
270
EpollWait(int32_t maxevents,int32_t timeout,struct epoll_event & events)271 int32_t ContextService::EpollWait(int32_t maxevents, int32_t timeout, struct epoll_event &events)
272 {
273 if (epollFd_ < 0) {
274 FI_HILOGE("Invalid epollFd:%{public}d", epollFd_);
275 return RET_ERR;
276 }
277 return epoll_wait(epollFd_, &events, maxevents, timeout);
278 }
279
OnTimeout(const struct epoll_event & ev)280 void ContextService::OnTimeout(const struct epoll_event &ev)
281 {
282 CALL_DEBUG_ENTER;
283 if ((ev.events & EPOLLIN) == EPOLLIN) {
284 uint64_t expiration {};
285 ssize_t ret = read(timerMgr_.GetTimerFd(), &expiration, sizeof(expiration));
286 if (ret < 0) {
287 FI_HILOGE("Read expiration failed:%{public}s", strerror(errno));
288 }
289 timerMgr_.ProcessTimers();
290 } else if ((ev.events & (EPOLLHUP | EPOLLERR)) != 0) {
291 FI_HILOGE("Epoll hangup:%{public}s", strerror(errno));
292 }
293 }
294
OnDeviceMgr(const struct epoll_event & ev)295 void ContextService::OnDeviceMgr(const struct epoll_event &ev)
296 {
297 CALL_DEBUG_ENTER;
298 if ((ev.events & EPOLLIN) == EPOLLIN) {
299 devMgr_.Dispatch(ev);
300 } else if ((ev.events & (EPOLLHUP | EPOLLERR)) != 0) {
301 FI_HILOGE("Epoll hangup:%{public}s", strerror(errno));
302 }
303 }
304
EnableDevMgr(int32_t nRetries)305 int32_t ContextService::EnableDevMgr(int32_t nRetries)
306 {
307 CALL_INFO_TRACE;
308 static int32_t timerId { -1 };
309 int32_t ret = devMgr_.Enable();
310 if (ret != RET_OK) {
311 FI_HILOGE("Failed to enable device manager");
312 if (nRetries > 0) {
313 timerId = timerMgr_.AddTimer(DEFAULT_WAIT_TIME_MS, WAIT_FOR_ONCE,
314 [this, nRetries] { return this->EnableDevMgr(nRetries - 1); });
315 if (timerId < 0) {
316 FI_HILOGE("AddTimer failed, Failed to enable device manager");
317 }
318 } else {
319 FI_HILOGE("Maximum number of retries exceeded, Failed to enable device manager");
320 }
321 return ret;
322 }
323 AddEpoll(EPOLL_EVENT_DEVICE_MGR, devMgr_.GetFd());
324 if (timerId >= 0) {
325 timerMgr_.RemoveTimer(timerId);
326 timerId = -1;
327 }
328 return RET_OK;
329 }
330
DisableDevMgr()331 void ContextService::DisableDevMgr()
332 {
333 DelEpoll(EPOLL_EVENT_DEVICE_MGR, devMgr_.GetFd());
334 devMgr_.Disable();
335 }
336
OnStart()337 void ContextService::OnStart()
338 {
339 CALL_DEBUG_ENTER;
340 uint64_t tid = GetThisThreadId();
341 delegateTasks_.SetWorkerThreadId(tid);
342
343 if (!Init()) {
344 FI_HILOGE("On start call init failed");
345 return;
346 }
347 state_ = ServiceRunningState::STATE_RUNNING;
348 ready_ = true;
349
350 worker_ = std::thread(std::bind(&ContextService::OnThread, this));
351 }
352
OnStop()353 void ContextService::OnStop()
354 {
355 CALL_DEBUG_ENTER;
356 if (timerMgr_.GetTimerFd() >= 0) {
357 if (close(timerMgr_.GetTimerFd()) < 0) {
358 FI_HILOGE("Close timer fd failed, error:%{public}s", strerror(errno));
359 }
360 }
361 if (!ready_) {
362 FI_HILOGI("ready state is false");
363 return;
364 }
365 ready_ = false;
366 state_ = ServiceRunningState::STATE_EXIT;
367
368 delegateTasks_.PostAsyncTask([]() -> int32_t {
369 FI_HILOGD("No asynchronous operations");
370 return RET_OK;
371 });
372 if (worker_.joinable()) {
373 worker_.join();
374 }
375 DisableDevMgr();
376 EpollClose();
377 FI_HILOGI("OnStop leave");
378 }
379
OnThread()380 void ContextService::OnThread()
381 {
382 CALL_DEBUG_ENTER;
383 SetThreadName(std::string("os_ds_service"));
384 uint64_t tid = GetThisThreadId();
385 delegateTasks_.SetWorkerThreadId(tid);
386 EnableDevMgr(MAX_N_RETRIES);
387 FI_HILOGD("Main worker thread start, tid:%{public}" PRId64 "", tid);
388
389 while (state_ == ServiceRunningState::STATE_RUNNING) {
390 struct epoll_event ev[MAX_EVENT_SIZE] {};
391 int32_t count = EpollWait(MAX_EVENT_SIZE, -1, ev[0]);
392 for (int32_t i = 0; i < count && state_ == ServiceRunningState::STATE_RUNNING; i++) {
393 auto epollEvent = reinterpret_cast<device_status_epoll_event*>(ev[i].data.ptr);
394 CHKPC(epollEvent);
395 if (epollEvent->event_type == EPOLL_EVENT_TIMER) {
396 OnTimeout(ev[i]);
397 } else if (epollEvent->event_type == EPOLL_EVENT_ETASK) {
398 OnDelegateTask(ev[i]);
399 } else if (epollEvent->event_type == EPOLL_EVENT_DEVICE_MGR) {
400 OnDeviceMgr(ev[i]);
401 } else {
402 FI_HILOGW("Unknown epoll event type:%{public}d", epollEvent->event_type);
403 }
404 }
405 }
406 FI_HILOGD("Main worker thread stop, tid:%{public}" PRId64 "", tid);
407 }
408
OnDelegateTask(const struct epoll_event & ev)409 void ContextService::OnDelegateTask(const struct epoll_event &ev)
410 {
411 if ((ev.events & EPOLLIN) == 0) {
412 FI_HILOGW("Not epollin");
413 return;
414 }
415 DelegateTasks::TaskData data {};
416 ssize_t res = read(delegateTasks_.GetReadFd(), &data, sizeof(data));
417 if (res == -1) {
418 FI_HILOGW("Read failed erron:%{public}d", errno);
419 }
420 FI_HILOGD("RemoteRequest notify td:%{public}" PRId64 ", std:%{public}" PRId64 ""
421 ", taskId:%{public}d", GetThisThreadId(), data.tid, data.taskId);
422 delegateTasks_.ProcessTasks();
423 }
424
SetUpTestCase()425 void IntentionDeviceManagerTest::SetUpTestCase() {}
426
TearDownTestCase()427 void IntentionDeviceManagerTest::TearDownTestCase()
428 {
429 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
430 }
431
SetUp()432 void IntentionDeviceManagerTest::SetUp() {}
433
TearDown()434 void IntentionDeviceManagerTest::TearDown() {}
435
436 /**
437 * @tc.name: IntentionDeviceManagerTest01
438 * @tc.desc: Test the founction AddDevice
439 * @tc.type: FUNC
440 */
441 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest01, TestSize.Level1)
442 {
443 CALL_TEST_DEBUG;
444 auto env = ContextService::GetInstance();
445 ASSERT_NE(env, nullptr);
446 env->devMgr_.RemoveDevice(TEST_DEV_NODE);
447 env->devMgr_.FindDevice(TEST_DEV_NODE);
448 auto ret = env->devMgr_.AddDevice(TEST_DEV_NODE);
449 ASSERT_EQ(ret, nullptr);
450 ret = env->devMgr_.FindDevice(TEST_DEV_NODE);
451 ASSERT_EQ(ret, nullptr);
452 ret = env->devMgr_.RemoveDevice(TEST_DEV_NODE);
453 ASSERT_EQ(ret, nullptr);
454 }
455
456 /**
457 * @tc.name: IntentionDeviceManagerTest02
458 * @tc.desc: Test the founction Dispatch
459 * @tc.type: FUNC
460 */
461 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest02, TestSize.Level1)
462 {
463 CALL_TEST_DEBUG;
464 auto env = ContextService::GetInstance();
465 ASSERT_NE(env, nullptr);
466 auto eventData = static_cast<device_status_epoll_event*>(malloc(sizeof(device_status_epoll_event)));
467 eventData->fd = 1;
468 eventData->event_type = EPOLL_EVENT_BEGIN;
469 struct epoll_event ev {};
470 ev.events = EPOLLIN;
471 ev.data.ptr = eventData;
472 ASSERT_NO_FATAL_FAILURE(env->devMgr_.Dispatch(ev));
473 }
474
475 /**
476 * @tc.name: IntentionDeviceManagerTest03
477 * @tc.desc: Test the founction GetDevice
478 * @tc.type: FUNC
479 */
480 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest03, TestSize.Level1)
481 {
482 CALL_TEST_DEBUG;
483 auto env = ContextService::GetInstance();
484 ASSERT_NE(env, nullptr);
485 ASSERT_NO_FATAL_FAILURE(env->GetDeviceManager().GetDevice(1));
486 }
487
488
489 /**
490 * @tc.name: IntentionDeviceManagerTest04
491 * @tc.desc: Test the founction RetriggerHotplug
492 * @tc.type: FUNC
493 */
494 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest04, TestSize.Level1)
495 {
496 CALL_TEST_DEBUG;
497 auto env = ContextService::GetInstance();
498 ASSERT_NE(env, nullptr);
499 std::weak_ptr<IDeviceObserver> weakObserver = std::weak_ptr<IDeviceObserver>();
500 ASSERT_NO_FATAL_FAILURE(env->GetDeviceManager().RetriggerHotplug(weakObserver));
501 }
502
503 /**
504 * @tc.name: IntentionDeviceManagerTest05
505 * @tc.desc: Test the founction AddDeviceObserver
506 * @tc.type: FUNC
507 */
508 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest05, TestSize.Level1)
509 {
510 CALL_TEST_DEBUG;
511 auto env = ContextService::GetInstance();
512 ASSERT_NE(env, nullptr);
513 std::weak_ptr<IDeviceObserver> weakObserver = std::weak_ptr<IDeviceObserver>();
514 int32_t ret = env->GetDeviceManager().AddDeviceObserver(weakObserver);
515 EXPECT_EQ(ret, RET_ERR);
516 ASSERT_NO_FATAL_FAILURE(env->GetDeviceManager().RemoveDeviceObserver(weakObserver));
517 }
518
519 /**
520 * @tc.name: IntentionDeviceManagerTest06
521 * @tc.desc: Test the founction GetKeyboard
522 * @tc.type: FUNC
523 */
524 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest06, TestSize.Level1)
525 {
526 CALL_TEST_DEBUG;
527 auto env = ContextService::GetInstance();
528 ASSERT_NE(env, nullptr);
529 env->devMgr_.AddDevice(TEST_DEV_NODE);
530 std::vector<std::shared_ptr<IDevice>> keyboards;
531 keyboards = env->devMgr_.GetKeyboard();
532 bool ret = env->devMgr_.HasKeyboard();
533 ASSERT_FALSE(ret);
534 }
535
536 /**
537 * @tc.name: IntentionDeviceManagerTest07
538 * @tc.desc: Test the founction Disable
539 * @tc.type: FUNC
540 */
541 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest07, TestSize.Level1)
542 {
543 CALL_TEST_DEBUG;
544 auto env = ContextService::GetInstance();
545 ASSERT_NE(env, nullptr);
546 ASSERT_NO_FATAL_FAILURE(env->devMgr_.Disable());
547 }
548
549 /**
550 * @tc.name: IntentionDeviceManagerTest08
551 * @tc.desc: Test the founction OnRetriggerHotplug
552 * @tc.type: FUNC
553 */
554 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest08, TestSize.Level1)
555 {
556 CALL_TEST_DEBUG;
557 auto env = ContextService::GetInstance();
558 ASSERT_NE(env, nullptr);
559 std::shared_ptr<DeviceObserverTest> observer = std::make_shared<DeviceObserverTest>();
560 std::weak_ptr<DeviceObserverTest> weakObserver = observer;
561 auto ret = env->devMgr_.AddDevice(TEST_DEV_NODE);
562 ASSERT_EQ(ret, nullptr);
563 ASSERT_NO_FATAL_FAILURE(env->devMgr_.OnRetriggerHotplug(weakObserver));
564 }
565
566 /**
567 * @tc.name: IntentionDeviceManagerTest08
568 * @tc.desc: Test the founction OnAddDeviceObserver
569 * @tc.type: FUNC
570 */
571 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest09, TestSize.Level1)
572 {
573 CALL_TEST_DEBUG;
574 auto env = ContextService::GetInstance();
575 ASSERT_NE(env, nullptr);
576 std::shared_ptr<DeviceObserverTest> observer = std::make_shared<DeviceObserverTest>();
577 std::weak_ptr<DeviceObserverTest> weakObserver = observer;
578 ASSERT_NO_FATAL_FAILURE(env->devMgr_.OnAddDeviceObserver(weakObserver));
579 }
580
581 /**
582 * @tc.name: IntentionDeviceManagerTest08
583 * @tc.desc: Test the founction OnRemoveDeviceObserver
584 * @tc.type: FUNC
585 */
586 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest010, TestSize.Level1)
587 {
588 CALL_TEST_DEBUG;
589 auto env = ContextService::GetInstance();
590 ASSERT_NE(env, nullptr);
591 std::shared_ptr<DeviceObserverTest> observer = std::make_shared<DeviceObserverTest>();
592 std::weak_ptr<DeviceObserverTest> weakObserver = observer;
593 env->devMgr_.OnAddDeviceObserver(weakObserver);
594 ASSERT_NO_FATAL_FAILURE(env->devMgr_.OnRemoveDeviceObserver(weakObserver));
595 }
596
597 /**
598 * @tc.name: IntentionDeviceManagerTest011
599 * @tc.desc: Test the founction SetPencilAirMouse
600 * @tc.type: FUNC
601 */
602 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest011, TestSize.Level1)
603 {
604 CALL_TEST_DEBUG;
605 auto env = ContextService::GetInstance();
606 ASSERT_NE(env, nullptr);
607 env->devMgr_.SetPencilAirMouse(true);
608 EXPECT_TRUE(env->devMgr_.hasPencilAirMouse_);
609 }
610
611 /**
612 * @tc.name: IntentionDeviceManagerTest012
613 * @tc.desc: Test the founction SetPencilAirMouse
614 * @tc.type: FUNC
615 */
616 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest012, TestSize.Level1)
617 {
618 CALL_TEST_DEBUG;
619 auto env = ContextService::GetInstance();
620 ASSERT_NE(env, nullptr);
621 env->devMgr_.SetPencilAirMouse(false);
622 EXPECT_FALSE(env->devMgr_.hasPencilAirMouse_);
623 }
624
625 /**
626 * @tc.name: IntentionDeviceManagerTest013
627 * @tc.desc: Test the founction SetPencilAirMouse
628 * @tc.type: FUNC
629 */
630 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest013, TestSize.Level1)
631 {
632 CALL_TEST_DEBUG;
633 auto env = ContextService::GetInstance();
634 ASSERT_NE(env, nullptr);
635 env->devMgr_.hasPencilAirMouse_ = true;
636 bool result = env->devMgr_.HasPencilAirMouse();
637 EXPECT_TRUE(result);
638 }
639
640 /**
641 * @tc.name: IntentionDeviceManagerTest014
642 * @tc.desc: Test the founction SetPencilAirMouse
643 * @tc.type: FUNC
644 */
645 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest014, TestSize.Level1)
646 {
647 CALL_TEST_DEBUG;
648 auto env = ContextService::GetInstance();
649 ASSERT_NE(env, nullptr);
650 env->devMgr_.hasPencilAirMouse_ = false;
651 bool result = env->devMgr_.HasPencilAirMouse();
652 EXPECT_FALSE(result);
653 }
654
655 /**
656 * @tc.name: IntentionDeviceManagerTest015
657 * @tc.desc: Test the founction OnRemoveDeviceObserver
658 * @tc.type: FUNC
659 */
660 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest015, TestSize.Level1)
661 {
662 CALL_TEST_DEBUG;
663 auto env = ContextService::GetInstance();
664 ASSERT_NE(env, nullptr);
665 int32_t deviceId = 0;
666 std::string name = {"fingerprint_mouse"};
667 std::shared_ptr<IDevice> dev = std::make_shared<Device>(deviceId);
668 dev->SetId(deviceId);
669 dev->SetName(name);
670 dev->SetKeyboardType(IDevice::KeyboardType::KEYBOARD_TYPE_ALPHABETICKEYBOARD);
671 dev->AddCapability(Device::DEVICE_CAP_KEYBOARD);
672 env->devMgr_.devices_.insert_or_assign(dev->GetId(), dev);
673 int32_t deviceId1 = 1;
674 std::string name1 = {"DistributedInput"};
675 std::shared_ptr<IDevice> dev1 = std::make_shared<Device>(deviceId1);
676 dev1->SetId(deviceId1);
677 dev1->SetName(name1);
678 dev1->SetKeyboardType(IDevice::KeyboardType::KEYBOARD_TYPE_DIGITALKEYBOARD);
679 dev1->AddCapability(Device::DEVICE_CAP_POINTER);
680 env->devMgr_.devices_.insert_or_assign(dev1->GetId(), dev1);
681 int32_t deviceId2 = 2;
682 std::string name2 = {"fingerprint_mouse"};
683 std::shared_ptr<IDevice> dev2 = std::make_shared<Device>(deviceId2);
684 dev2->SetId(deviceId2);
685 dev2->SetName(name2);
686 dev2->AddCapability(Device::DEVICE_CAP_KEYBOARD);
687 dev2->AddCapability(Device::DEVICE_CAP_POINTER);
688 env->devMgr_.devices_.insert_or_assign(dev2->GetId(), dev2);
689 ASSERT_NO_FATAL_FAILURE(env->devMgr_.GetKeyboard());
690 }
691
692 /**
693 * @tc.name: IntentionDeviceManagerTest016
694 * @tc.desc: Test the founction OnRemoveDeviceObserver
695 * @tc.type: FUNC
696 */
697 HWTEST_F(IntentionDeviceManagerTest, IntentionDeviceManagerTest016, TestSize.Level1)
698 {
699 CALL_TEST_DEBUG;
700 auto env = ContextService::GetInstance();
701 ASSERT_NE(env, nullptr);
702 int32_t deviceId = 0;
703 std::string name = {"fingerprint_mouse"};
704 std::shared_ptr<IDevice> dev = std::make_shared<Device>(deviceId);
705 dev->SetId(deviceId);
706 dev->SetName(name);
707 dev->AddCapability(Device::DEVICE_CAP_KEYBOARD);
708 env->devMgr_.devices_.insert_or_assign(dev->GetId(), dev);
709 int32_t deviceId1 = 1;
710 std::string name1 = {"DistributedInput"};
711 std::shared_ptr<IDevice> dev1 = std::make_shared<Device>(deviceId1);
712 dev1->SetId(deviceId1);
713 dev1->SetName(name1);
714 dev1->AddCapability(Device::DEVICE_CAP_POINTER);
715 env->devMgr_.devices_.insert_or_assign(dev1->GetId(), dev1);
716 int32_t deviceId2 = 2;
717 std::string name2 = {"fingerprint_mouse"};
718 std::shared_ptr<IDevice> dev2 = std::make_shared<Device>(deviceId2);
719 dev2->SetId(deviceId2);
720 dev2->SetName(name2);
721 dev2->AddCapability(Device::DEVICE_CAP_KEYBOARD);
722 dev2->AddCapability(Device::DEVICE_CAP_POINTER);
723 env->devMgr_.devices_.insert_or_assign(dev2->GetId(), dev2);
724 ASSERT_NO_FATAL_FAILURE(env->devMgr_.GetPointerDevice());
725 }
726 } // namespace DeviceStatus
727 } // namespace Msdp
728 } // namespace OHOS