1 /* 2 * Copyright (c) 2021-2022 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 FOUNDATION_APPEXECFWK_LIBS_TEST_MODULETEST_COMMON_EVENT_HANDLER_EVENT_HANDLER_TEST_COMMON_H 17 #define FOUNDATION_APPEXECFWK_LIBS_TEST_MODULETEST_COMMON_EVENT_HANDLER_EVENT_HANDLER_TEST_COMMON_H 18 19 #include <atomic> 20 21 #include <vector> 22 #include "nocopyable.h" 23 24 #include "event_handler.h" 25 #include "event_runner.h" 26 27 namespace OHOS { 28 namespace AppExecFwk { 29 const uint32_t STOP_EVENT_ID = 0; 30 const uint32_t RUN_EVENT_ID = 10; 31 32 /** 33 * Function: Create random number for uint32_t type. 34 */ Random()35inline uint32_t Random() 36 { 37 return static_cast<uint32_t>(std::rand()); 38 } 39 40 enum class SmartPointerType { 41 SHARED_PTR = 0, 42 WEAK_PTR, 43 LVALUE_REFERENCE_UNIQUE_PTR, 44 RVALUE_REFERENCE_UNIQUE_PTR, 45 }; 46 47 class CommonUtils { 48 public: 49 CommonUtils() = delete; 50 ~CommonUtils() = delete; 51 DISALLOW_COPY_AND_MOVE(CommonUtils); 52 53 /** 54 * Function: Get the processed result of event. 55 * @return Returns true if event has been processed. 56 */ EventRunGet()57 static inline bool EventRunGet() 58 { 59 return eventRun_.load(); 60 } 61 62 /** 63 * Function: Set the processed result of event. 64 * @param run Set false or true for processed result. 65 */ EventRunSet(bool run)66 static inline void EventRunSet(bool run) 67 { 68 eventRun_.store(run); 69 } 70 71 /** 72 * Function: Get the called result of task. 73 * @return Returns true if task has been called. 74 */ TaskCalledGet()75 static inline bool TaskCalledGet() 76 { 77 return taskCalled_.load(); 78 } 79 80 /** 81 * Function: Set the called result of task. 82 * @param run Set false or true for called result. 83 */ TaskCalledSet(bool called)84 static inline void TaskCalledSet(bool called) 85 { 86 taskCalled_.store(called); 87 } 88 89 /** 90 * Function: Get the processed times of event. 91 * @return Returns processed times. 92 */ EventRunCount()93 static inline uint32_t EventRunCount() 94 { 95 return eventRunTimes_; 96 } 97 98 /** 99 * Function: Increment of event processed times. 100 */ EventRunCountIncrement()101 static inline void EventRunCountIncrement() 102 { 103 eventRunTimes_++; 104 } 105 106 /** 107 * Function: Reset the count of event processed to 0. 108 */ EventRunCountReset()109 static inline void EventRunCountReset() 110 { 111 eventRunTimes_ = 0; 112 } 113 114 /** 115 * Function: Reset the thread of vector. 116 */ ThreadIdsReset()117 static inline void ThreadIdsReset() 118 { 119 threads_.clear(); 120 } 121 122 /** 123 * Function: push back thread Id. 124 */ PushbackThread(const std::string threadId)125 static inline void PushbackThread(const std::string threadId) 126 { 127 threads_.push_back(threadId); 128 } 129 130 /** 131 * Get thread Id. 132 */ GetThreads()133 static inline std::vector<std::string> GetThreads() 134 { 135 return threads_; 136 } 137 138 private: 139 static std::vector<std::string> threads_; 140 static std::atomic<bool> eventRun_; 141 static std::atomic<bool> taskCalled_; 142 static std::atomic<uint32_t> eventRunTimes_; 143 }; 144 145 class MyEventHandler : public EventHandler { 146 public: 147 explicit MyEventHandler(const std::shared_ptr<EventRunner> &runner); 148 ~MyEventHandler(); 149 150 /** 151 * Function: Process the event. Override the method of base class. 152 * @param event The event need to be processed. 153 */ 154 void ProcessEvent(const InnerEvent::Pointer &event) override; 155 }; 156 } // namespace AppExecFwk 157 } // namespace OHOS 158 #endif // #ifndef FOUNDATION_APPEXECFWK_LIBS_TEST_MODULETEST_COMMON_EVENT_HANDLER_EVENT_HANDLER_TEST_COMMON_H 159