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