1 /* 2 * Copyright (c) 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 BATTERY_THREAD_TEST_H 17 #define BATTERY_THREAD_TEST_H 18 19 #include "battery_thread.h" 20 #include "charger_thread.h" 21 22 namespace OHOS { 23 namespace HDI { 24 namespace Battery { 25 namespace V1_1 { 26 namespace { 27 struct BatteryThreadUnitTest {}; 28 struct BatteryConfigUnitTest {}; 29 struct ChargerThreadUnitTest {}; 30 } 31 32 int32_t OpenUeventSocketTest(BatteryThread& bthread); 33 void UpdateEpollIntervalTest(const int32_t chargeState, BatteryThread& bthread); 34 int GetEpollIntervalTest(BatteryThread& bthread); 35 int32_t InitTest(void* service, BatteryThread& bthread); 36 int32_t GetEpollFdTest(BatteryThread& bthread); 37 int32_t InitUeventTest(BatteryThread& bthread); 38 int32_t GetUeventFdTest(BatteryThread& bthread); 39 void ParseConfigTest(const std::string filename, BatteryConfig& bconfig); 40 void ChargerThreadInitTest(ChargerThread& cthread); 41 void CycleMattersTest(ChargerThread& cthread); 42 bool GetBatteryInfoTest(ChargerThread& cthread); 43 void SetChargeStateTest(const int32_t state, ChargerThread& cthread); 44 int32_t GetChargeStateTest(ChargerThread& cthread); 45 void HandleCapacityTest(const int32_t& capacity, ChargerThread& cthread); 46 void ChargerThreadHandleTemperatureTest(const int32_t& temperature, ChargerThread& cthread); 47 48 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 49 class OpenUeventSocketImplement { OpenUeventSocketTest(BatteryThread & bthread)50 friend int32_t OpenUeventSocketTest(BatteryThread& bthread) 51 { 52 return (bthread.*privateFun)(); 53 } 54 }; 55 56 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 57 class UpdateEpollIntervalImplement { UpdateEpollIntervalTest(const int32_t chargeState,BatteryThread & bthread)58 friend void UpdateEpollIntervalTest(const int32_t chargeState, BatteryThread& bthread) 59 { 60 (bthread.*privateFun)(chargeState); 61 } 62 }; 63 64 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 65 class GetEpollIntervalImplement { GetEpollIntervalTest(BatteryThread & bthread)66 friend int GetEpollIntervalTest(BatteryThread& bthread) 67 { 68 return (bthread.*privateFun); 69 } 70 }; 71 72 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 73 class InitImplement { InitTest(void * service,BatteryThread & bthread)74 friend int32_t InitTest(void* service, BatteryThread& bthread) 75 { 76 return (bthread.*privateFun)(service); 77 } 78 }; 79 80 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 81 class GetEpollFdImplement { GetEpollFdTest(BatteryThread & bthread)82 friend int32_t GetEpollFdTest(BatteryThread& bthread) 83 { 84 return (bthread.*privateFun); 85 } 86 }; 87 88 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 89 class InitUeventImplement { InitUeventTest(BatteryThread & bthread)90 friend int32_t InitUeventTest(BatteryThread& bthread) 91 { 92 return (bthread.*privateFun)(); 93 } 94 }; 95 96 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 97 class ChargerThreadInitImplement { ChargerThreadInitTest(ChargerThread & cthread)98 friend void ChargerThreadInitTest(ChargerThread& cthread) 99 { 100 (cthread.*privateFun)(); 101 } 102 }; 103 104 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 105 class GetUeventFdImplement { GetUeventFdTest(BatteryThread & bthread)106 friend int32_t GetUeventFdTest(BatteryThread& bthread) 107 { 108 return (bthread.*privateFun); 109 } 110 }; 111 112 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 113 class ParseConfigImplement { ParseConfigTest(const std::string filename,BatteryConfig & bconfig)114 friend void ParseConfigTest(const std::string filename, BatteryConfig& bconfig) 115 { 116 return (bconfig.*privateFun)(filename); 117 } 118 }; 119 120 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 121 class CycleMattersImplement { CycleMattersTest(ChargerThread & cthread)122 friend void CycleMattersTest(ChargerThread& cthread) 123 { 124 (cthread.*privateFun)(); 125 } 126 }; 127 128 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 129 class GetBatteryInfoImplement { GetBatteryInfoTest(ChargerThread & cthread)130 friend bool GetBatteryInfoTest(ChargerThread& cthread) 131 { 132 return (cthread.*privateFun); 133 } 134 }; 135 136 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 137 class HandleCapacityImplement { HandleCapacityTest(const int32_t & capacity,ChargerThread & cthread)138 friend void HandleCapacityTest(const int32_t& capacity, ChargerThread& cthread) 139 { 140 (cthread.*privateFun)(capacity); 141 } 142 }; 143 144 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 145 class GetChargeStateImplement { GetChargeStateTest(ChargerThread & cthread)146 friend int32_t GetChargeStateTest(ChargerThread& cthread) 147 { 148 return (cthread.*privateFun); 149 } 150 }; 151 152 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 153 class SetChargeStateImplement { SetChargeStateTest(const int32_t state,ChargerThread & cthread)154 friend void SetChargeStateTest(const int32_t state, ChargerThread& cthread) 155 { 156 (cthread.*privateFun) = state; 157 } 158 }; 159 160 template<typename Tag, typename PrivateFun, PrivateFun privateFun> 161 class ChargerThreadHandleTemperatureImplement { ChargerThreadHandleTemperatureTest(const int32_t & temperature,ChargerThread & cthread)162 friend void ChargerThreadHandleTemperatureTest(const int32_t& temperature, ChargerThread& cthread) 163 { 164 (cthread.*privateFun)(temperature); 165 } 166 }; 167 168 template class OpenUeventSocketImplement < 169 BatteryThreadUnitTest, 170 decltype(&BatteryThread::OpenUeventSocket), 171 &BatteryThread::OpenUeventSocket 172 >; 173 174 template class UpdateEpollIntervalImplement < 175 BatteryThreadUnitTest, 176 decltype(&BatteryThread::UpdateEpollInterval), 177 &BatteryThread::UpdateEpollInterval 178 >; 179 180 template class GetEpollIntervalImplement < 181 BatteryThreadUnitTest, 182 decltype(&BatteryThread::epollInterval_), 183 &BatteryThread::epollInterval_ 184 >; 185 186 template class InitImplement < 187 BatteryThreadUnitTest, 188 decltype(&BatteryThread::Init), 189 &BatteryThread::Init 190 >; 191 192 template class GetEpollFdImplement < 193 BatteryThreadUnitTest, 194 decltype(&BatteryThread::epFd_), 195 &BatteryThread::epFd_ 196 >; 197 198 template class InitUeventImplement < 199 BatteryThreadUnitTest, 200 decltype(&BatteryThread::InitUevent), 201 &BatteryThread::InitUevent 202 >; 203 204 template class ChargerThreadInitImplement < 205 ChargerThreadUnitTest, 206 decltype(&ChargerThread::Init), 207 &ChargerThread::Init 208 >; 209 210 template class CycleMattersImplement < 211 ChargerThreadUnitTest, 212 decltype(&ChargerThread::CycleMatters), 213 &ChargerThread::CycleMatters 214 >; 215 216 template class GetUeventFdImplement < 217 BatteryThreadUnitTest, 218 decltype(&BatteryThread::ueventFd_), 219 &BatteryThread::ueventFd_ 220 >; 221 222 template class ParseConfigImplement < 223 BatteryConfigUnitTest, 224 decltype(&BatteryConfig::ParseConfig), 225 &BatteryConfig::ParseConfig 226 >; 227 228 template class GetBatteryInfoImplement < 229 ChargerThreadUnitTest, 230 decltype(&ChargerThread::started_), 231 &ChargerThread::started_ 232 >; 233 234 template class HandleCapacityImplement < 235 ChargerThreadUnitTest, 236 decltype(&ChargerThread::HandleCapacity), 237 &ChargerThread::HandleCapacity 238 >; 239 240 template class GetChargeStateImplement < 241 ChargerThreadUnitTest, 242 decltype(&ChargerThread::chargeState_), 243 &ChargerThread::chargeState_ 244 >; 245 246 template class SetChargeStateImplement < 247 ChargerThreadUnitTest, 248 decltype(&ChargerThread::chargeState_), 249 &ChargerThread::chargeState_ 250 >; 251 252 template class ChargerThreadHandleTemperatureImplement < 253 ChargerThreadUnitTest, 254 decltype(&ChargerThread::HandleTemperature), 255 &ChargerThread::HandleTemperature 256 >; 257 258 class BatteryThreadTest { 259 public: SetKeyState(int code,int value,int64_t now,ChargerThread & ct)260 void SetKeyState(int code, int value, int64_t now, ChargerThread& ct) 261 { 262 ct.SetKeyState(code, value, now); 263 } 264 HandlePowerKey(int keycode,int64_t now,ChargerThread & ct)265 void HandlePowerKey(int keycode, int64_t now, ChargerThread& ct) 266 { 267 ct.HandlePowerKey(keycode, now); 268 } 269 UpdateBatteryInfo(void * arg,ChargerThread & ct)270 void UpdateBatteryInfo(void* arg, ChargerThread& ct) 271 { 272 ct.UpdateBatteryInfo(arg); 273 } 274 Init(ChargerThread & ct)275 void Init(ChargerThread& ct) 276 { 277 ct.Init(); 278 } 279 }; 280 } // namespace V1_1 281 } // namespace Battery 282 } // namespace HDI 283 } // namespace OHOS 284 #endif // BATTERY_THREAD_TEST_H 285