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