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