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