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 #ifndef TIMER_MANAGER_TEST_H
17 #define TIMER_MANAGER_TEST_H
18 
19 #include <gtest/gtest.h>
20 #include <memory>
21 #include <string>
22 
23 #include <fcntl.h>
24 #include "nocopyable.h"
25 
26 #include "cooperate_events.h"
27 #include "delegate_tasks.h"
28 #include "device_manager.h"
29 #include "devicestatus_define.h"
30 #include "devicestatus_delayed_sp_singleton.h"
31 #include "drag_manager.h"
32 #include "i_context.h"
33 #include "timer_manager.h"
34 
35 #include "intention_service.h"
36 #include "socket_session_manager.h"
37 
38 namespace OHOS {
39 namespace Msdp {
40 namespace DeviceStatus {
41 enum EpollEventType {
42     EPOLL_EVENT_BEGIN = 0,
43     EPOLL_EVENT_INPUT = EPOLL_EVENT_BEGIN,
44     EPOLL_EVENT_SOCKET,
45     EPOLL_EVENT_ETASK,
46     EPOLL_EVENT_TIMER,
47     EPOLL_EVENT_DEVICE_MGR,
48     EPOLL_EVENT_END
49 };
50 
51 struct TimerInfo {
52     int32_t times { 0 };
53     int32_t timerId { 0 };
54 };
55 
56 enum class ServiceRunningState {STATE_NOT_START, STATE_RUNNING, STATE_EXIT};
57 class ContextService final : public IContext {
58     ContextService();
59     ~ContextService();
60     DISALLOW_COPY_AND_MOVE(ContextService);
61 public:
62     IDelegateTasks& GetDelegateTasks() override;
63     IDeviceManager& GetDeviceManager() override;
64     ITimerManager& GetTimerManager() override;
65     IDragManager& GetDragManager() override;
66 
67     IPluginManager& GetPluginManager() override;
68     ISocketSessionManager& GetSocketSessionManager() override;
69     IInputAdapter& GetInput() override;
70     IDSoftbusAdapter& GetDSoftbus() override;
71 private:
72     void OnStart();
73     void OnStop();
74     bool Init();
75     int32_t EpollCreate();
76     int32_t AddEpoll(EpollEventType type, int32_t fd);
77     int32_t DelEpoll(EpollEventType type, int32_t fd);
78     int32_t EpollCtl(int32_t fd, int32_t op, struct epoll_event &event);
79     int32_t EpollWait(int32_t maxevents, int32_t timeout, struct epoll_event &events);
80     void EpollClose();
81     int32_t InitTimerMgr();
82     int32_t InitDevMgr();
83     void OnThread();
84     void OnTimeout(const epoll_event &ev);
85     void OnDeviceMgr(const epoll_event &ev);
86     int32_t EnableDevMgr(int32_t nRetries);
87     void DisableDevMgr();
88     int32_t InitDelegateTasks();
89     void OnDelegateTask(const struct epoll_event &ev);
90     static ContextService* GetInstance();
91 private:
92     std::atomic<ServiceRunningState> state_ { ServiceRunningState::STATE_NOT_START };
93     std::thread worker_;
94     DelegateTasks delegateTasks_;
95     DeviceManager devMgr_;
96     TimerManager timerMgr_;
97     std::atomic<bool> ready_ { false };
98     DragManager dragMgr_;
99     int32_t epollFd_ { -1 };
100     SocketSessionManager socketSessionMgr_;
101     std::unique_ptr<IInputAdapter> input_;
102     std::unique_ptr<IPluginManager> pluginMgr_;
103     std::unique_ptr<IDSoftbusAdapter> dsoftbusAda_;
104 };
105 
106 class IntentionDeviceManagerTest : public testing::Test {
107 public:
108     static void SetUpTestCase();
109     static void TearDownTestCase();
110     void SetUp();
111     void TearDown();
112 
113 private:
114     TimerInfo timerInfo_;
115     int32_t timerId_ { -1 };
116 };
117 } // namespace DeviceStatus
118 } // namespace Msdp
119 } // namespace OHOS
120 #endif // TIMER_MANAGER_TEST_H