1 /*
2  * Copyright (c) 2022-2023 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 #include "thermal_action_report_test.h"
17 
18 #include "securec.h"
19 #include <fcntl.h>
20 #include <string>
21 #include <unistd.h>
22 #ifdef BATTERY_MANAGER_ENABLE
23 #include "battery_srv_client.h"
24 #endif
25 #ifdef BATTERY_STATS_ENABLE
26 #include "battery_stats_client.h"
27 #endif
28 #include "power_mgr_client.h"
29 #include "thermal_config_file_parser.h"
30 #include "thermal_mgr_client.h"
31 
32 #define private   public
33 #define protected public
34 #include "thermal_service.h"
35 #include "thermal_srv_config_parser.h"
36 #include "v1_1/ithermal_interface.h"
37 #include "v1_1/thermal_types.h"
38 #undef private
39 #undef protected
40 
41 using namespace testing::ext;
42 using namespace OHOS::PowerMgr;
43 using namespace OHOS;
44 using namespace std;
45 using namespace OHOS::HDI::Thermal::V1_1;
46 
47 namespace {
48 static std::shared_ptr<ThermalConfigFileParser> g_configParser = nullptr;
49 static std::vector<std::string> g_dumpArgs;
50 static std::string g_sceneState;
51 static const std::string POLICY_CFG_NAME = "base_safe";
52 constexpr int32_t THERMAL_RATIO_BEGIN = 0;
53 constexpr int32_t THERMAL_RATIO_LENGTH = 4;
54 const std::string SYSTEM_THERMAL_SERVICE_CONFIG_PATH = "/system/etc/thermal_config/thermal_service_config.xml";
55 sptr<ThermalService> g_service = nullptr;
56 } // namespace
57 
ParserThermalSrvConfigFile()58 void ThermalActionReportTest::ParserThermalSrvConfigFile()
59 {
60     if (g_configParser == nullptr) {
61         g_configParser = std::make_shared<ThermalConfigFileParser>();
62         if (!g_configParser->Init()) {
63             THERMAL_HILOGE(LABEL_TEST, "Thermal service config file parser initialization failed");
64         }
65     }
66 }
67 
SetCondition(int32_t value,const std::string & scene)68 int32_t ThermalActionReportTest::SetCondition(int32_t value, const std::string& scene)
69 {
70     THERMAL_HILOGD(LABEL_TEST, "battery = %{public}d, scene = %{public}s", value, scene.c_str());
71     HdfThermalCallbackInfo event;
72     ThermalZoneInfo info1;
73     info1.type = "battery";
74     info1.temp = value;
75     event.info.push_back(info1);
76     g_sceneState = scene;
77     g_service->SetScene(g_sceneState);
78     return g_service->HandleThermalCallbackEvent(event);
79 }
80 
GetThermalLevel(int32_t expectValue)81 int32_t ThermalActionReportTest::GetThermalLevel(int32_t expectValue)
82 {
83     ThermalLevel level;
84     g_service->GetThermalLevel(level);
85     int32_t value = static_cast<int32_t>(level);
86     THERMAL_HILOGD(LABEL_TEST, "value: %{public}d", value);
87     EXPECT_EQ(value, expectValue) << "Thermal action policy failed";
88     return value;
89 }
90 
GetActionValue(const std::string & actionName,uint32_t level)91 std::string ThermalActionReportTest::GetActionValue(const std::string& actionName, uint32_t level)
92 {
93     THERMAL_HILOGD(LABEL_TEST, "action name = %{public}s, level = %{public}d", actionName.c_str(), level);
94     std::string value = "0";
95     std::vector<PolicyAction> vAction;
96     if (!g_configParser->GetActionPolicy(POLICY_CFG_NAME, level, vAction)) {
97         THERMAL_HILOGD(LABEL_TEST, "Get policy failed name = %{public}s, return", POLICY_CFG_NAME.c_str());
98         return value;
99     }
100     return ActionDecision(actionName, vAction);
101 }
102 
ActionDecision(const std::string & actionName,std::vector<PolicyAction> & vAction)103 std::string ThermalActionReportTest::ActionDecision(const std::string& actionName, std::vector<PolicyAction>& vAction)
104 {
105     THERMAL_HILOGD(LABEL_TEST, "action name = %{public}s", actionName.c_str());
106     if (actionName == LCD_ACTION_NAME) {
107         return LcdValueDecision(actionName, vAction);
108     } else {
109         return ActionValueDecision(actionName, vAction);
110     }
111 }
112 
ActionValueDecision(const std::string & actionName,std::vector<PolicyAction> & vAction)113 std::string ThermalActionReportTest::ActionValueDecision(
114     const std::string& actionName, std::vector<PolicyAction>& vAction)
115 {
116     THERMAL_HILOGD(LABEL_TEST, "action name = %{public}s", actionName.c_str());
117     int32_t value = -1;
118     std::vector<uint32_t> valueList;
119     for (auto actionIter : vAction) {
120         if (actionIter.actionName == actionName) {
121             if (actionIter.isProp) {
122                 if (StateDecision(actionIter.actionPropMap)) {
123                     valueList.push_back(stoi(actionIter.actionValue));
124                 }
125             } else {
126                 valueList.push_back(stoi(actionIter.actionValue));
127             }
128         }
129     }
130 
131     bool strict = g_configParser->GetActionStrict(actionName);
132     if (valueList.empty()) {
133         value = 0;
134     } else {
135         if (strict) {
136             value = *max_element(valueList.begin(), valueList.end());
137         } else {
138             value = *min_element(valueList.begin(), valueList.end());
139         }
140     }
141     std::string strValue = to_string(value);
142     return strValue;
143 }
144 
LcdValueDecision(const std::string & actionName,std::vector<PolicyAction> & vAction)145 std::string ThermalActionReportTest::LcdValueDecision(const std::string& actionName, std::vector<PolicyAction>& vAction)
146 {
147     THERMAL_HILOGD(LABEL_TEST, "action name = %{public}s", actionName.c_str());
148     float value = -1.0;
149     std::vector<float> valueList;
150     std::map<std::string, std::string> sceneMap;
151     for (auto actionIter : vAction) {
152         if (actionIter.actionName == actionName) {
153             if (actionIter.isProp) {
154                 if (StateDecision(actionIter.actionPropMap)) {
155                     valueList.push_back(stof(actionIter.actionValue));
156                     sceneMap.emplace(std::pair(actionIter.actionPropMap.begin()->second, actionIter.actionValue));
157                 }
158             } else {
159                 valueList.push_back(stof(actionIter.actionValue));
160             }
161         }
162     }
163 
164     for (auto sceneIter : sceneMap) {
165         if (g_sceneState == sceneIter.first) {
166             return sceneIter.second.c_str();
167         }
168     }
169 
170     bool strict = g_configParser->GetActionStrict(actionName);
171     if (valueList.empty()) {
172         value = 0;
173     } else {
174         if (strict) {
175             value = *max_element(valueList.begin(), valueList.end());
176         } else {
177             value = *min_element(valueList.begin(), valueList.end());
178         }
179     }
180     std::string strValue = to_string(value).substr(THERMAL_RATIO_BEGIN, THERMAL_RATIO_LENGTH);
181     return strValue;
182 }
183 
StateDecision(std::map<std::string,std::string> & actionPropMap)184 bool ThermalActionReportTest::StateDecision(std::map<std::string, std::string>& actionPropMap)
185 {
186     bool ret = true;
187     std::map<std::string, std::string> stateMap;
188     GetStateMap(stateMap);
189     for (auto prop : actionPropMap) {
190         auto stateIter = stateMap.find(prop.first);
191         THERMAL_HILOGD(LABEL_TEST, "state = %{public}s, value = %{public}s", prop.first.c_str(), prop.second.c_str());
192         if (stateIter != stateMap.end()) {
193             THERMAL_HILOGD(LABEL_TEST, "state iter = %{public}s, iter value = %{public}s", stateIter->first.c_str(),
194                 stateIter->second.c_str());
195             if (stateIter->second.compare(prop.second) == 0) {
196                 continue;
197             } else {
198                 ret = false;
199                 break;
200             }
201         }
202     }
203     return ret;
204 }
205 
GetScreenState()206 std::string ThermalActionReportTest::GetScreenState()
207 {
208     std::string state = "0";
209     auto& powerMgrClient = PowerMgrClient::GetInstance();
210     if (powerMgrClient.IsScreenOn()) {
211         state = "1";
212     }
213     return state;
214 }
215 
GetChargeState()216 std::string ThermalActionReportTest::GetChargeState()
217 {
218     std::string state = "";
219 #ifdef BATTERY_MANAGER_ENABLE
220     auto& batterySrvClient = BatterySrvClient::GetInstance();
221     BatteryChargeState chargeState = batterySrvClient.GetChargingStatus();
222     if (chargeState == BatteryChargeState::CHARGE_STATE_ENABLE) {
223         state = "1";
224     } else if (chargeState == BatteryChargeState::CHARGE_STATE_NONE) {
225         state = "0";
226     }
227 #endif
228     return state;
229 }
230 
GetStateMap(std::map<std::string,std::string> & stateMap)231 void ThermalActionReportTest::GetStateMap(std::map<std::string, std::string>& stateMap)
232 {
233     std::vector<StateItem> stateItem = g_configParser->GetStateItem();
234     for (auto stateIter : stateItem) {
235         std::string state = "";
236         if (stateIter.name == "scene") {
237             state = g_sceneState;
238         } else if (stateIter.name == "screen") {
239             state = GetScreenState();
240         } else if (stateIter.name == "charge") {
241             state = GetChargeState();
242         }
243         stateMap.emplace(std::pair(stateIter.name, state));
244     }
245     for (auto iter : stateMap) {
246         THERMAL_HILOGD(
247             LABEL_TEST, "stateMap name = %{public}s, value = %{public}s", iter.first.c_str(), iter.second.c_str());
248     }
249 }
250 
ThermalActionTriggered(const std::string & actionName,int32_t level,const std::string & dumpInfo,bool isReversed)251 void ThermalActionReportTest::ThermalActionTriggered(
252     const std::string& actionName, int32_t level, const std::string& dumpInfo, bool isReversed)
253 {
254     bool enableEvent = g_configParser->GetActionEnableEvent(actionName);
255     THERMAL_HILOGD(LABEL_TEST, "action name = %{public}s, event flag = %{public}d", actionName.c_str(), enableEvent);
256     if (!enableEvent) {
257         GTEST_LOG_(INFO) << __func__ << " action name: " << actionName << " enalbe event flag is false, return";
258         return;
259     }
260     std::string value = GetActionValue(actionName, level);
261     std::string expectedDumpInfo;
262     std::string valueLabel = " Value = ";
263     if (actionName == LCD_ACTION_NAME) {
264         valueLabel = " Ratio = ";
265     }
266     expectedDumpInfo.append("Additional debug info: ")
267         .append("Event name = ACTION_TRIGGERED")
268         .append(" Action name = ")
269         .append(actionName)
270         .append(valueLabel)
271         .append(value);
272     THERMAL_HILOGD(LABEL_TEST, "value: %{public}s", value.c_str());
273     GTEST_LOG_(INFO) << __func__ << " action name: " << actionName << " expected debug info: " << expectedDumpInfo;
274     auto index = dumpInfo.find(expectedDumpInfo);
275     if (!isReversed) {
276         EXPECT_TRUE(index != string::npos) << " Thermal action fail due to not found related debug info."
277                                            << " action name = " << actionName;
278     } else {
279         EXPECT_TRUE(index == string::npos) << " Thermal action fail due to not found related debug info."
280                                            << " action name = " << actionName;
281     }
282 }
283 
SetUpTestCase()284 void ThermalActionReportTest::SetUpTestCase()
285 {
286     ParserThermalSrvConfigFile();
287     g_dumpArgs.push_back("-batterystats");
288     g_service = ThermalService::GetInstance();
289     g_service->InitSystemTestModules();
290     g_service->OnStart();
291     g_service->InitStateMachine();
292     g_service->InitActionManager();
293 }
294 
TearDownTestCase()295 void ThermalActionReportTest::TearDownTestCase()
296 {
297     g_service->OnStop();
298 }
299 
TearDown()300 void ThermalActionReportTest::TearDown()
301 {
302     g_service->SetScene("");
303     HdfThermalCallbackInfo event;
304     ThermalZoneInfo info1;
305     info1.type = "battery";
306     info1.temp = 0;
307     event.info.push_back(info1);
308     g_service->HandleThermalCallbackEvent(event);
309 }
310 
311 #ifdef BATTERY_STATS_ENABLE
312 namespace {
313 /**
314  * @tc.name: ThermalActionReportTest001
315  * @tc.desc: test dump info when thermal action is triggered
316  * @tc.type: FEATURE
317  * @tc.cond: Set battery temp = 40100, scence = cam
318  * @tc.result battery stats dump info
319  */
320 HWTEST_F(ThermalActionReportTest, ThermalActionReportTest001, TestSize.Level0)
321 {
322     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.001 start");
323     auto& statsClient = BatteryStatsClient::GetInstance();
324     statsClient.Reset();
325 
326     int32_t ret = -1;
327     int32_t batteryTemp = 40100;
328     std::string sceneState = "cam";
329     int32_t expectLevel = 1;
330     ret = SetCondition(batteryTemp, sceneState);
331     EXPECT_EQ(ret, ERR_OK) << " Thermal action fail due to set condition error";
332     int32_t level = ThermalActionReportTest::GetThermalLevel(expectLevel);
333     // sleep 100ms for hisys_event handle
334     usleep(100000);
335     std::string actualDumpInfo = statsClient.Dump(g_dumpArgs);
336     GTEST_LOG_(INFO) << __func__ << ": actual dump info: " << actualDumpInfo;
337     ThermalActionReportTest::ThermalActionTriggered(CPU_BIG_ACTION_NAME, level, actualDumpInfo);
338     ThermalActionReportTest::ThermalActionTriggered(CPU_MED_ACTION_NAME, level, actualDumpInfo);
339     ThermalActionReportTest::ThermalActionTriggered(CPU_LIT_ACTION_NAME, level, actualDumpInfo);
340     ThermalActionReportTest::ThermalActionTriggered(GPU_ACTION_NAME, level, actualDumpInfo);
341     ThermalActionReportTest::ThermalActionTriggered(LCD_ACTION_NAME, level, actualDumpInfo);
342     ThermalActionReportTest::ThermalActionTriggered(PROCESS_ACTION_NAME, level, actualDumpInfo);
343     ThermalActionReportTest::ThermalActionTriggered(THERMAL_LEVEL_NAME, level, actualDumpInfo);
344     ThermalActionReportTest::ThermalActionTriggered(CURRENT_SC_ACTION_NAME, level, actualDumpInfo);
345     ThermalActionReportTest::ThermalActionTriggered(CURRENT_BUCK_ACTION_NAME, level, actualDumpInfo);
346     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_SC_ACTION_NAME, level, actualDumpInfo);
347     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_BUCK_ACTION_NAME, level, actualDumpInfo);
348     ThermalActionReportTest::ThermalActionTriggered(CPU_BOOST_ACTION_NAME, level, actualDumpInfo);
349     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.001 end");
350 }
351 
352 /**
353  * @tc.name: ThermalActionReportTest002
354  * @tc.desc: test dump info when thermal action is triggered
355  * @tc.type: FEATURE
356  * @tc.cond: Set battery temp = 40100, scence = call
357  * @tc.result battery stats dump info
358  */
359 HWTEST_F(ThermalActionReportTest, ThermalActionReportTest002, TestSize.Level0)
360 {
361     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.002 start");
362     auto& statsClient = BatteryStatsClient::GetInstance();
363     statsClient.Reset();
364 
365     int32_t ret = -1;
366     int32_t batteryTemp = 40100;
367     std::string sceneState = "call";
368     int32_t expectLevel = 1;
369     ret = SetCondition(batteryTemp, sceneState);
370     EXPECT_EQ(ret, ERR_OK) << " Thermal action fail due to set condition error";
371     int32_t level = ThermalActionReportTest::GetThermalLevel(expectLevel);
372     // sleep 100ms for hisys_event handle
373     usleep(100000);
374     std::string actualDumpInfo = statsClient.Dump(g_dumpArgs);
375     GTEST_LOG_(INFO) << __func__ << ": actual dump info: " << actualDumpInfo;
376     ThermalActionReportTest::ThermalActionTriggered(CPU_BIG_ACTION_NAME, level, actualDumpInfo);
377     ThermalActionReportTest::ThermalActionTriggered(CPU_MED_ACTION_NAME, level, actualDumpInfo);
378     ThermalActionReportTest::ThermalActionTriggered(CPU_LIT_ACTION_NAME, level, actualDumpInfo);
379     ThermalActionReportTest::ThermalActionTriggered(GPU_ACTION_NAME, level, actualDumpInfo);
380     ThermalActionReportTest::ThermalActionTriggered(LCD_ACTION_NAME, level, actualDumpInfo);
381     ThermalActionReportTest::ThermalActionTriggered(PROCESS_ACTION_NAME, level, actualDumpInfo);
382     ThermalActionReportTest::ThermalActionTriggered(THERMAL_LEVEL_NAME, level, actualDumpInfo);
383     ThermalActionReportTest::ThermalActionTriggered(CURRENT_SC_ACTION_NAME, level, actualDumpInfo);
384     ThermalActionReportTest::ThermalActionTriggered(CURRENT_BUCK_ACTION_NAME, level, actualDumpInfo);
385     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_SC_ACTION_NAME, level, actualDumpInfo);
386     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_BUCK_ACTION_NAME, level, actualDumpInfo);
387     ThermalActionReportTest::ThermalActionTriggered(CPU_BOOST_ACTION_NAME, level, actualDumpInfo);
388     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.002 end");
389 }
390 
391 /**
392  * @tc.name: ThermalActionReportTest003
393  * @tc.desc: test dump info when thermal action is triggered
394  * @tc.type: FEATURE
395  * @tc.cond: Set battery temp = 40100, scence = game
396  * @tc.result battery stats dump info
397  */
398 HWTEST_F(ThermalActionReportTest, ThermalActionReportTest003, TestSize.Level0)
399 {
400     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.003 start");
401     auto& statsClient = BatteryStatsClient::GetInstance();
402     statsClient.Reset();
403 
404     int32_t ret = -1;
405     int32_t batteryTemp = 40100;
406     std::string sceneState = "game";
407     int32_t expectLevel = 1;
408     ret = SetCondition(batteryTemp, sceneState);
409     EXPECT_EQ(ret, ERR_OK) << " Thermal action fail due to set condition error";
410 
411     int32_t level = ThermalActionReportTest::GetThermalLevel(expectLevel);
412     // sleep 100ms for hisys_event handle
413     usleep(100000);
414     std::string actualDumpInfo = statsClient.Dump(g_dumpArgs);
415     GTEST_LOG_(INFO) << __func__ << ": actual dump info: " << actualDumpInfo;
416     ThermalActionReportTest::ThermalActionTriggered(CPU_BIG_ACTION_NAME, level, actualDumpInfo);
417     ThermalActionReportTest::ThermalActionTriggered(CPU_MED_ACTION_NAME, level, actualDumpInfo);
418     ThermalActionReportTest::ThermalActionTriggered(CPU_LIT_ACTION_NAME, level, actualDumpInfo);
419     ThermalActionReportTest::ThermalActionTriggered(GPU_ACTION_NAME, level, actualDumpInfo);
420     ThermalActionReportTest::ThermalActionTriggered(LCD_ACTION_NAME, level, actualDumpInfo);
421     ThermalActionReportTest::ThermalActionTriggered(PROCESS_ACTION_NAME, level, actualDumpInfo);
422     ThermalActionReportTest::ThermalActionTriggered(THERMAL_LEVEL_NAME, level, actualDumpInfo);
423     ThermalActionReportTest::ThermalActionTriggered(CURRENT_SC_ACTION_NAME, level, actualDumpInfo);
424     ThermalActionReportTest::ThermalActionTriggered(CURRENT_BUCK_ACTION_NAME, level, actualDumpInfo);
425     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_SC_ACTION_NAME, level, actualDumpInfo);
426     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_BUCK_ACTION_NAME, level, actualDumpInfo);
427     ThermalActionReportTest::ThermalActionTriggered(CPU_BOOST_ACTION_NAME, level, actualDumpInfo);
428     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.003 end");
429 }
430 
431 /**
432  * @tc.name: ThermalActionReportTest004
433  * @tc.desc: test dump info when thermal action is triggered
434  * @tc.type: FEATURE
435  * @tc.cond: Set battery temp = 40100, scence = ""
436  * @tc.result battery stats dump info
437  */
438 HWTEST_F(ThermalActionReportTest, ThermalActionReportTest004, TestSize.Level0)
439 {
440     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.004 start");
441     auto& statsClient = BatteryStatsClient::GetInstance();
442     statsClient.Reset();
443 
444     int32_t ret = -1;
445     int32_t batteryTemp = 40100;
446     std::string sceneState = "";
447     int32_t expectLevel = 1;
448     ret = SetCondition(batteryTemp, sceneState);
449     EXPECT_EQ(ret, ERR_OK) << " Thermal action fail due to set condition error";
450 
451     int32_t level = ThermalActionReportTest::GetThermalLevel(expectLevel);
452     // sleep 100ms for hisys_event handle
453     usleep(100000);
454     std::string actualDumpInfo = statsClient.Dump(g_dumpArgs);
455     GTEST_LOG_(INFO) << __func__ << ": actual dump info: " << actualDumpInfo;
456     ThermalActionReportTest::ThermalActionTriggered(CPU_BIG_ACTION_NAME, level, actualDumpInfo);
457     ThermalActionReportTest::ThermalActionTriggered(CPU_MED_ACTION_NAME, level, actualDumpInfo);
458     ThermalActionReportTest::ThermalActionTriggered(CPU_LIT_ACTION_NAME, level, actualDumpInfo);
459     ThermalActionReportTest::ThermalActionTriggered(GPU_ACTION_NAME, level, actualDumpInfo);
460     ThermalActionReportTest::ThermalActionTriggered(LCD_ACTION_NAME, level, actualDumpInfo);
461     ThermalActionReportTest::ThermalActionTriggered(PROCESS_ACTION_NAME, level, actualDumpInfo);
462     ThermalActionReportTest::ThermalActionTriggered(THERMAL_LEVEL_NAME, level, actualDumpInfo);
463     ThermalActionReportTest::ThermalActionTriggered(CURRENT_SC_ACTION_NAME, level, actualDumpInfo);
464     ThermalActionReportTest::ThermalActionTriggered(CURRENT_BUCK_ACTION_NAME, level, actualDumpInfo);
465     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_SC_ACTION_NAME, level, actualDumpInfo);
466     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_BUCK_ACTION_NAME, level, actualDumpInfo);
467     ThermalActionReportTest::ThermalActionTriggered(CPU_BOOST_ACTION_NAME, level, actualDumpInfo);
468     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.004 end");
469 }
470 
471 /**
472  * @tc.name: ThermalActionReportTest005
473  * @tc.desc: test dump info when thermal action is triggered
474  * @tc.type: FEATURE
475  * @tc.cond: Set battery temp = 43100, scence = cam
476  * @tc.result battery stats dump info
477  */
478 HWTEST_F(ThermalActionReportTest, ThermalActionReportTest005, TestSize.Level0)
479 {
480     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.005 start");
481     auto& statsClient = BatteryStatsClient::GetInstance();
482     statsClient.Reset();
483 
484     int32_t ret = -1;
485     int32_t batteryTemp = 43100;
486     std::string sceneState = "cam";
487     int32_t expectLevel = 2;
488     ret = SetCondition(batteryTemp, sceneState);
489     EXPECT_EQ(ret, ERR_OK) << " Thermal action fail due to set condition error";
490 
491     int32_t level = ThermalActionReportTest::GetThermalLevel(expectLevel);
492     // sleep 100ms for hisys_event handle
493     usleep(100000);
494     std::string actualDumpInfo = statsClient.Dump(g_dumpArgs);
495     GTEST_LOG_(INFO) << __func__ << ": actual dump info: " << actualDumpInfo;
496     ThermalActionReportTest::ThermalActionTriggered(CPU_BIG_ACTION_NAME, level, actualDumpInfo);
497     ThermalActionReportTest::ThermalActionTriggered(CPU_MED_ACTION_NAME, level, actualDumpInfo);
498     ThermalActionReportTest::ThermalActionTriggered(CPU_LIT_ACTION_NAME, level, actualDumpInfo);
499     ThermalActionReportTest::ThermalActionTriggered(GPU_ACTION_NAME, level, actualDumpInfo);
500     ThermalActionReportTest::ThermalActionTriggered(LCD_ACTION_NAME, level, actualDumpInfo);
501     ThermalActionReportTest::ThermalActionTriggered(PROCESS_ACTION_NAME, level, actualDumpInfo);
502     ThermalActionReportTest::ThermalActionTriggered(THERMAL_LEVEL_NAME, level, actualDumpInfo);
503     ThermalActionReportTest::ThermalActionTriggered(CURRENT_SC_ACTION_NAME, level, actualDumpInfo);
504     ThermalActionReportTest::ThermalActionTriggered(CURRENT_BUCK_ACTION_NAME, level, actualDumpInfo);
505     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_SC_ACTION_NAME, level, actualDumpInfo);
506     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_BUCK_ACTION_NAME, level, actualDumpInfo);
507     ThermalActionReportTest::ThermalActionTriggered(CPU_BOOST_ACTION_NAME, level, actualDumpInfo);
508     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.005 end");
509 }
510 
511 /**
512  * @tc.name: ThermalActionReportTest006
513  * @tc.desc: test dump info when thermal action is triggered
514  * @tc.type: FEATURE
515  * @tc.cond: Set battery temp = 43100, scence = call
516  * @tc.result battery stats dump info
517  */
518 HWTEST_F(ThermalActionReportTest, ThermalActionReportTest006, TestSize.Level0)
519 {
520     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.006 start");
521     auto& statsClient = BatteryStatsClient::GetInstance();
522     statsClient.Reset();
523 
524     int32_t ret = -1;
525     int32_t batteryTemp = 43100;
526     std::string sceneState = "call";
527     int32_t expectLevel = 2;
528     ret = SetCondition(batteryTemp, sceneState);
529     EXPECT_EQ(ret, ERR_OK) << " Thermal action fail due to set condition error";
530 
531     int32_t level = ThermalActionReportTest::GetThermalLevel(expectLevel);
532     // sleep 100ms for hisys_event handle
533     usleep(100000);
534     std::string actualDumpInfo = statsClient.Dump(g_dumpArgs);
535     GTEST_LOG_(INFO) << __func__ << ": actual dump info: " << actualDumpInfo;
536     ThermalActionReportTest::ThermalActionTriggered(CPU_BIG_ACTION_NAME, level, actualDumpInfo);
537     ThermalActionReportTest::ThermalActionTriggered(CPU_MED_ACTION_NAME, level, actualDumpInfo);
538     ThermalActionReportTest::ThermalActionTriggered(CPU_LIT_ACTION_NAME, level, actualDumpInfo);
539     ThermalActionReportTest::ThermalActionTriggered(GPU_ACTION_NAME, level, actualDumpInfo);
540     ThermalActionReportTest::ThermalActionTriggered(LCD_ACTION_NAME, level, actualDumpInfo);
541     ThermalActionReportTest::ThermalActionTriggered(PROCESS_ACTION_NAME, level, actualDumpInfo);
542     ThermalActionReportTest::ThermalActionTriggered(THERMAL_LEVEL_NAME, level, actualDumpInfo);
543     ThermalActionReportTest::ThermalActionTriggered(CURRENT_SC_ACTION_NAME, level, actualDumpInfo);
544     ThermalActionReportTest::ThermalActionTriggered(CURRENT_BUCK_ACTION_NAME, level, actualDumpInfo);
545     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_SC_ACTION_NAME, level, actualDumpInfo);
546     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_BUCK_ACTION_NAME, level, actualDumpInfo);
547     ThermalActionReportTest::ThermalActionTriggered(CPU_BOOST_ACTION_NAME, level, actualDumpInfo);
548     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.006 end");
549 }
550 
551 /**
552  * @tc.name: ThermalActionReportTest007
553  * @tc.desc: test dump info when thermal action is triggered
554  * @tc.type: FEATURE
555  * @tc.cond: Set battery temp = 43100, scence = game
556  * @tc.result battery stats dump info
557  */
558 HWTEST_F(ThermalActionReportTest, ThermalActionReportTest007, TestSize.Level0)
559 {
560     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.007 start");
561     auto& statsClient = BatteryStatsClient::GetInstance();
562     statsClient.Reset();
563 
564     int32_t ret = -1;
565     int32_t batteryTemp = 43100;
566     std::string sceneState = "game";
567     int32_t expectLevel = 2;
568     ret = SetCondition(batteryTemp, sceneState);
569     EXPECT_EQ(ret, ERR_OK) << " Thermal action fail due to set condition error";
570 
571     int32_t level = ThermalActionReportTest::GetThermalLevel(expectLevel);
572     // sleep 100ms for hisys_event handle
573     usleep(100000);
574     std::string actualDumpInfo = statsClient.Dump(g_dumpArgs);
575     GTEST_LOG_(INFO) << __func__ << ": actual dump info: " << actualDumpInfo;
576     ThermalActionReportTest::ThermalActionTriggered(CPU_BIG_ACTION_NAME, level, actualDumpInfo);
577     ThermalActionReportTest::ThermalActionTriggered(CPU_MED_ACTION_NAME, level, actualDumpInfo);
578     ThermalActionReportTest::ThermalActionTriggered(CPU_LIT_ACTION_NAME, level, actualDumpInfo);
579     ThermalActionReportTest::ThermalActionTriggered(GPU_ACTION_NAME, level, actualDumpInfo);
580     ThermalActionReportTest::ThermalActionTriggered(LCD_ACTION_NAME, level, actualDumpInfo);
581     ThermalActionReportTest::ThermalActionTriggered(PROCESS_ACTION_NAME, level, actualDumpInfo);
582     ThermalActionReportTest::ThermalActionTriggered(THERMAL_LEVEL_NAME, level, actualDumpInfo);
583     ThermalActionReportTest::ThermalActionTriggered(CURRENT_SC_ACTION_NAME, level, actualDumpInfo);
584     ThermalActionReportTest::ThermalActionTriggered(CURRENT_BUCK_ACTION_NAME, level, actualDumpInfo);
585     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_SC_ACTION_NAME, level, actualDumpInfo);
586     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_BUCK_ACTION_NAME, level, actualDumpInfo);
587     ThermalActionReportTest::ThermalActionTriggered(CPU_BOOST_ACTION_NAME, level, actualDumpInfo);
588     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.007 end");
589 }
590 
591 /**
592  * @tc.name: ThermalActionReportTest008
593  * @tc.desc: test dump info when thermal action is triggered
594  * @tc.type: FEATURE
595  * @tc.cond: Set battery temp = 43100, scence = ""
596  * @tc.result battery stats dump info
597  */
598 HWTEST_F(ThermalActionReportTest, ThermalActionReportTest008, TestSize.Level0)
599 {
600     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.008 start");
601     auto& statsClient = BatteryStatsClient::GetInstance();
602     statsClient.Reset();
603 
604     int32_t ret = -1;
605     int32_t batteryTemp = 43100;
606     std::string sceneState = "";
607     int32_t expectLevel = 2;
608     ret = SetCondition(batteryTemp, sceneState);
609     EXPECT_EQ(ret, ERR_OK) << " Thermal action fail due to set condition error";
610 
611     int32_t level = ThermalActionReportTest::GetThermalLevel(expectLevel);
612     // sleep 100ms for hisys_event handle
613     usleep(100000);
614     std::string actualDumpInfo = statsClient.Dump(g_dumpArgs);
615     GTEST_LOG_(INFO) << __func__ << ": actual dump info: " << actualDumpInfo;
616     ThermalActionReportTest::ThermalActionTriggered(CPU_BIG_ACTION_NAME, level, actualDumpInfo);
617     ThermalActionReportTest::ThermalActionTriggered(CPU_MED_ACTION_NAME, level, actualDumpInfo);
618     ThermalActionReportTest::ThermalActionTriggered(CPU_LIT_ACTION_NAME, level, actualDumpInfo);
619     ThermalActionReportTest::ThermalActionTriggered(GPU_ACTION_NAME, level, actualDumpInfo);
620     ThermalActionReportTest::ThermalActionTriggered(LCD_ACTION_NAME, level, actualDumpInfo);
621     ThermalActionReportTest::ThermalActionTriggered(PROCESS_ACTION_NAME, level, actualDumpInfo);
622     ThermalActionReportTest::ThermalActionTriggered(THERMAL_LEVEL_NAME, level, actualDumpInfo);
623     ThermalActionReportTest::ThermalActionTriggered(CURRENT_SC_ACTION_NAME, level, actualDumpInfo);
624     ThermalActionReportTest::ThermalActionTriggered(CURRENT_BUCK_ACTION_NAME, level, actualDumpInfo);
625     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_SC_ACTION_NAME, level, actualDumpInfo);
626     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_BUCK_ACTION_NAME, level, actualDumpInfo);
627     ThermalActionReportTest::ThermalActionTriggered(CPU_BOOST_ACTION_NAME, level, actualDumpInfo);
628     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.008 end");
629 }
630 
631 /**
632  * @tc.name: ThermalActionReportTest009
633  * @tc.desc: test dump info when thermal action is triggered
634  * @tc.type: FEATURE
635  * @tc.cond: Set battery temp = 46100, scence = cam
636  * @tc.result battery stats dump info
637  */
638 HWTEST_F(ThermalActionReportTest, ThermalActionReportTest009, TestSize.Level0)
639 {
640     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.009 start");
641     auto& statsClient = BatteryStatsClient::GetInstance();
642     statsClient.Reset();
643 
644     int32_t ret = -1;
645     int32_t batteryTemp = 46100;
646     std::string sceneState = "cam";
647     int32_t expectLevel = 3;
648     ret = SetCondition(batteryTemp, sceneState);
649     EXPECT_EQ(ret, ERR_OK) << " Thermal action fail due to set condition error";
650 
651     int32_t level = ThermalActionReportTest::GetThermalLevel(expectLevel);
652     // sleep 100ms for hisys_event handle
653     usleep(100000);
654     std::string actualDumpInfo = statsClient.Dump(g_dumpArgs);
655     GTEST_LOG_(INFO) << __func__ << ": actual dump info: " << actualDumpInfo;
656     ThermalActionReportTest::ThermalActionTriggered(CPU_BIG_ACTION_NAME, level, actualDumpInfo);
657     ThermalActionReportTest::ThermalActionTriggered(CPU_MED_ACTION_NAME, level, actualDumpInfo);
658     ThermalActionReportTest::ThermalActionTriggered(CPU_LIT_ACTION_NAME, level, actualDumpInfo);
659     ThermalActionReportTest::ThermalActionTriggered(GPU_ACTION_NAME, level, actualDumpInfo);
660     ThermalActionReportTest::ThermalActionTriggered(LCD_ACTION_NAME, level, actualDumpInfo);
661     ThermalActionReportTest::ThermalActionTriggered(PROCESS_ACTION_NAME, level, actualDumpInfo);
662     ThermalActionReportTest::ThermalActionTriggered(THERMAL_LEVEL_NAME, level, actualDumpInfo);
663     ThermalActionReportTest::ThermalActionTriggered(CURRENT_SC_ACTION_NAME, level, actualDumpInfo);
664     ThermalActionReportTest::ThermalActionTriggered(CURRENT_BUCK_ACTION_NAME, level, actualDumpInfo);
665     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_SC_ACTION_NAME, level, actualDumpInfo);
666     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_BUCK_ACTION_NAME, level, actualDumpInfo);
667     ThermalActionReportTest::ThermalActionTriggered(SHUTDOWN_ACTION_NAME, level, actualDumpInfo);
668     ThermalActionReportTest::ThermalActionTriggered(CPU_BOOST_ACTION_NAME, level, actualDumpInfo, true);
669     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.009 end");
670 }
671 
672 /**
673  * @tc.name: ThermalActionReportTest010
674  * @tc.desc: test dump info when thermal action is triggered
675  * @tc.type: FEATURE
676  * @tc.cond: Set battery temp = 46100, scence = call
677  * @tc.result battery stats dump info
678  */
679 HWTEST_F(ThermalActionReportTest, ThermalActionReportTest010, TestSize.Level0)
680 {
681     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.010 start");
682     auto& statsClient = BatteryStatsClient::GetInstance();
683     statsClient.Reset();
684 
685     int32_t ret = -1;
686     int32_t batteryTemp = 46100;
687     std::string sceneState = "call";
688     int32_t expectLevel = 3;
689     ret = SetCondition(batteryTemp, sceneState);
690     EXPECT_EQ(ret, ERR_OK) << " Thermal action fail due to set condition error";
691 
692     int32_t level = ThermalActionReportTest::GetThermalLevel(expectLevel);
693     // sleep 100ms for hisys_event handle
694     usleep(100000);
695     std::string actualDumpInfo = statsClient.Dump(g_dumpArgs);
696     GTEST_LOG_(INFO) << __func__ << ": actual dump info: " << actualDumpInfo;
697     ThermalActionReportTest::ThermalActionTriggered(CPU_BIG_ACTION_NAME, level, actualDumpInfo);
698     ThermalActionReportTest::ThermalActionTriggered(CPU_MED_ACTION_NAME, level, actualDumpInfo);
699     ThermalActionReportTest::ThermalActionTriggered(CPU_LIT_ACTION_NAME, level, actualDumpInfo);
700     ThermalActionReportTest::ThermalActionTriggered(GPU_ACTION_NAME, level, actualDumpInfo);
701     ThermalActionReportTest::ThermalActionTriggered(LCD_ACTION_NAME, level, actualDumpInfo);
702     ThermalActionReportTest::ThermalActionTriggered(PROCESS_ACTION_NAME, level, actualDumpInfo);
703     ThermalActionReportTest::ThermalActionTriggered(THERMAL_LEVEL_NAME, level, actualDumpInfo);
704     ThermalActionReportTest::ThermalActionTriggered(CURRENT_SC_ACTION_NAME, level, actualDumpInfo);
705     ThermalActionReportTest::ThermalActionTriggered(CURRENT_BUCK_ACTION_NAME, level, actualDumpInfo);
706     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_SC_ACTION_NAME, level, actualDumpInfo);
707     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_BUCK_ACTION_NAME, level, actualDumpInfo);
708     ThermalActionReportTest::ThermalActionTriggered(SHUTDOWN_ACTION_NAME, level, actualDumpInfo);
709     ThermalActionReportTest::ThermalActionTriggered(CPU_BOOST_ACTION_NAME, level, actualDumpInfo, true);
710     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.010 end");
711 }
712 
713 /**
714  * @tc.name: ThermalActionReportTest011
715  * @tc.desc: test dump info when thermal action is triggered
716  * @tc.type: FEATURE
717  * @tc.cond: Set battery temp = 46100, scence = game
718  * @tc.result battery stats dump info
719  */
720 HWTEST_F(ThermalActionReportTest, ThermalActionReportTest011, TestSize.Level0)
721 {
722     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.011 start");
723     auto& statsClient = BatteryStatsClient::GetInstance();
724     statsClient.Reset();
725 
726     int32_t ret = -1;
727     int32_t batteryTemp = 46100;
728     std::string sceneState = "game";
729     int32_t expectLevel = 3;
730     ret = SetCondition(batteryTemp, sceneState);
731     EXPECT_EQ(ret, ERR_OK) << " Thermal action fail due to set condition error";
732 
733     int32_t level = ThermalActionReportTest::GetThermalLevel(expectLevel);
734     // sleep 100ms for hisys_event handle
735     usleep(100000);
736     std::string actualDumpInfo = statsClient.Dump(g_dumpArgs);
737     GTEST_LOG_(INFO) << __func__ << ": actual dump info: " << actualDumpInfo;
738     ThermalActionReportTest::ThermalActionTriggered(CPU_BIG_ACTION_NAME, level, actualDumpInfo);
739     ThermalActionReportTest::ThermalActionTriggered(CPU_MED_ACTION_NAME, level, actualDumpInfo);
740     ThermalActionReportTest::ThermalActionTriggered(CPU_LIT_ACTION_NAME, level, actualDumpInfo);
741     ThermalActionReportTest::ThermalActionTriggered(GPU_ACTION_NAME, level, actualDumpInfo);
742     ThermalActionReportTest::ThermalActionTriggered(LCD_ACTION_NAME, level, actualDumpInfo);
743     ThermalActionReportTest::ThermalActionTriggered(PROCESS_ACTION_NAME, level, actualDumpInfo);
744     ThermalActionReportTest::ThermalActionTriggered(THERMAL_LEVEL_NAME, level, actualDumpInfo);
745     ThermalActionReportTest::ThermalActionTriggered(CURRENT_SC_ACTION_NAME, level, actualDumpInfo);
746     ThermalActionReportTest::ThermalActionTriggered(CURRENT_BUCK_ACTION_NAME, level, actualDumpInfo);
747     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_SC_ACTION_NAME, level, actualDumpInfo);
748     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_BUCK_ACTION_NAME, level, actualDumpInfo);
749     ThermalActionReportTest::ThermalActionTriggered(SHUTDOWN_ACTION_NAME, level, actualDumpInfo);
750     ThermalActionReportTest::ThermalActionTriggered(CPU_BOOST_ACTION_NAME, level, actualDumpInfo, true);
751     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.011 end");
752 }
753 
754 /**
755  * @tc.name: ThermalActionReportTest012
756  * @tc.desc: test dump info when thermal action is triggered
757  * @tc.type: FEATURE
758  * @tc.cond: Set battery temp = 46100, scence = ""
759  * @tc.result battery stats dump info
760  */
761 HWTEST_F(ThermalActionReportTest, ThermalActionReportTest012, TestSize.Level0)
762 {
763     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.012 start");
764     auto& statsClient = BatteryStatsClient::GetInstance();
765     statsClient.Reset();
766 
767     int32_t ret = -1;
768     int32_t batteryTemp = 46100;
769     std::string sceneState = "";
770     int32_t expectLevel = 3;
771     ret = SetCondition(batteryTemp, sceneState);
772     EXPECT_EQ(ret, ERR_OK) << " Thermal action fail due to set condition error";
773 
774     int32_t level = ThermalActionReportTest::GetThermalLevel(expectLevel);
775     // sleep 100ms for hisys_event handle
776     usleep(100000);
777     std::string actualDumpInfo = statsClient.Dump(g_dumpArgs);
778     GTEST_LOG_(INFO) << __func__ << ": actual dump info: " << actualDumpInfo;
779     ThermalActionReportTest::ThermalActionTriggered(CPU_BIG_ACTION_NAME, level, actualDumpInfo);
780     ThermalActionReportTest::ThermalActionTriggered(CPU_MED_ACTION_NAME, level, actualDumpInfo);
781     ThermalActionReportTest::ThermalActionTriggered(CPU_LIT_ACTION_NAME, level, actualDumpInfo);
782     ThermalActionReportTest::ThermalActionTriggered(GPU_ACTION_NAME, level, actualDumpInfo);
783     ThermalActionReportTest::ThermalActionTriggered(LCD_ACTION_NAME, level, actualDumpInfo);
784     ThermalActionReportTest::ThermalActionTriggered(PROCESS_ACTION_NAME, level, actualDumpInfo);
785     ThermalActionReportTest::ThermalActionTriggered(THERMAL_LEVEL_NAME, level, actualDumpInfo);
786     ThermalActionReportTest::ThermalActionTriggered(CURRENT_SC_ACTION_NAME, level, actualDumpInfo);
787     ThermalActionReportTest::ThermalActionTriggered(CURRENT_BUCK_ACTION_NAME, level, actualDumpInfo);
788     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_SC_ACTION_NAME, level, actualDumpInfo);
789     ThermalActionReportTest::ThermalActionTriggered(VOLATAGE_BUCK_ACTION_NAME, level, actualDumpInfo);
790     ThermalActionReportTest::ThermalActionTriggered(SHUTDOWN_ACTION_NAME, level, actualDumpInfo);
791     ThermalActionReportTest::ThermalActionTriggered(CPU_BOOST_ACTION_NAME, level, actualDumpInfo, true);
792     THERMAL_HILOGD(LABEL_TEST, "Thermal action report test No.012 end");
793 }
794 } // namespace
795 #endif // BATTERY_STATS_ENABLE
796