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 #include "thermal_policy_test.h"
17 
18 #ifdef THERMAL_GTEST
19 #define private   public
20 #define protected public
21 #define final
22 #endif
23 
24 #include <map>
25 #include <string>
26 #include <vector>
27 
28 #include "action_cpu_big.h"
29 #include "config_policy_utils.h"
30 #include "power_mgr_client.h"
31 #include "screen_state_collection.h"
32 #include "thermal_log.h"
33 #include "thermal_policy.h"
34 #include "thermal_service.h"
35 
36 using namespace OHOS::PowerMgr;
37 using namespace OHOS;
38 using namespace testing::ext;
39 using namespace std;
40 
41 namespace {
42 sptr<ThermalService> g_service = nullptr;
43 } // namespace
44 
GetOneCfgFile(const char * pathSuffix,char * buf,unsigned int bufLength)45 char* GetOneCfgFile(const char *pathSuffix, char *buf, unsigned int bufLength)
46 {
47     THERMAL_HILOGI(LABEL_TEST, "mock GetOneCfgFile.");
48     return nullptr;
49 }
50 
SetUpTestCase()51 void ThermalPolicyTest::SetUpTestCase()
52 {
53     g_service = ThermalService::GetInstance();
54     g_service->InitSystemTestModules();
55     g_service->OnStart();
56 }
57 
58 namespace {
59 /**
60  * @tc.name: ThermalPolicyTest001
61  * @tc.desc: test GetClusterLevelMap
62  * @tc.type: FUNC
63  * @tc.require: issueI6KRS8
64  */
65 HWTEST_F(ThermalPolicyTest, ThermalPolicyTest001, TestSize.Level0)
66 {
67     THERMAL_HILOGI(LABEL_TEST, "ThermalPolicyTest001 start.");
68     ThermalPolicy policy;
69     EXPECT_TRUE(policy.GetClusterLevelMap().empty());
70     THERMAL_HILOGI(LABEL_TEST, "ThermalPolicyTest001 end.");
71 }
72 
73 /**
74  * @tc.name: ThermalPolicyTest002
75  * @tc.desc: test PolicyDecision continue in a loop
76  * @tc.type: FUNC
77  * @tc.require: issueI6KRS8
78  */
79 HWTEST_F(ThermalPolicyTest, ThermalPolicyTest002, TestSize.Level0)
80 {
81     THERMAL_HILOGI(LABEL_TEST, "ThermalPolicyTest002 start.");
82     ThermalPolicy policy;
83     EXPECT_TRUE(policy.Init());
84     std::vector<PolicyConfig> vecConfig;
85     policy.clusterPolicyMap_[""] = vecConfig;
86     policy.clusterPolicyMap_["test"] = vecConfig;
87     PolicyConfig config;
88     vecConfig.push_back(config);
89     policy.clusterPolicyMap_["test1"] = vecConfig;
90     policy.PolicyDecision();
91 
92     // ActionExecution Let the function return false
93     auto mgrTmp = g_service->actionMgr_;
94     g_service->actionMgr_ = nullptr;
95     policy.PolicyDecision();
96     g_service->actionMgr_ = mgrTmp;
97     EXPECT_TRUE(g_service->actionMgr_ != nullptr);
98     THERMAL_HILOGI(LABEL_TEST, "ThermalPolicyTest002 end.");
99 }
100 
101 /**
102  * @tc.name: ThermalPolicyTest003
103  * @tc.desc: test ActionDecision continue in a loop
104  * @tc.type: FUNC
105  * @tc.require: issueI6KRS8
106  */
107 HWTEST_F(ThermalPolicyTest, ThermalPolicyTest003, TestSize.Level0)
108 {
109     THERMAL_HILOGI(LABEL_TEST, "ThermalPolicyTest003 start.");
110     // No matching item
111     ThermalPolicy policy;
112     std::vector<PolicyAction> actionList;
113     PolicyAction action;
114     action.actionName = "test";
115     actionList.push_back(action);
116     EXPECT_FALSE(actionList.empty());
117     policy.ActionDecision(actionList);
118 
119     // second is nullptr
120     auto& actionMap = g_service->actionMgr_->actionMap_;
121     actionMap["test"] = nullptr;
122     policy.ActionDecision(actionList);
123     EXPECT_FALSE(actionMap.empty());
124     THERMAL_HILOGI(LABEL_TEST, "ThermalPolicyTest003 end.");
125 }
126 
127 /**
128  * @tc.name: ThermalPolicyTest004
129  * @tc.desc: test ActionDecision Execute the if else branch
130  * @tc.type: FUNC
131  * @tc.require: issueI6KRS8
132  */
133 HWTEST_F(ThermalPolicyTest, ThermalPolicyTest004, TestSize.Level0)
134 {
135     THERMAL_HILOGI(LABEL_TEST, "ThermalPolicyTest004 start.");
136     ThermalPolicy policy;
137     std::vector<PolicyAction> actionList;
138     PolicyAction action1;
139     action1.actionName = "test";
140     action1.isProp = true;
141     PolicyAction action2;
142     action2.actionName = "test1";
143     action1.isProp = false;
144     PolicyAction action3;
145     action2.actionName = "test2";
146     action1.isProp = true;
147     actionList.push_back(action1);
148     actionList.push_back(action2);
149     actionList.push_back(action3);
150     EXPECT_FALSE(actionList.empty());
151 
152     auto& actionMap = g_service->actionMgr_->actionMap_;
153     actionMap["test"] = std::make_shared<ActionCpuBig>(CPU_BIG_ACTION_NAME);
154     actionMap["test1"] = std::make_shared<ActionCpuBig>(CPU_BIG_ACTION_NAME);
155     actionMap["test2"] = std::make_shared<ActionCpuBig>(CPU_BIG_ACTION_NAME);
156     policy.ActionDecision(actionList);
157     EXPECT_FALSE(actionMap.empty());
158     THERMAL_HILOGI(LABEL_TEST, "ThermalPolicyTest004 end.");
159 }
160 
161 /**
162  * @tc.name: ThermalPolicyTest005
163  * @tc.desc: test StateMachineDecision
164  * @tc.type: FUNC
165  */
166 HWTEST_F(ThermalPolicyTest, ThermalPolicyTest005, TestSize.Level0)
167 {
168     THERMAL_HILOGI(LABEL_TEST, "ThermalPolicyTest005 start.");
169     // No match
170     ThermalPolicy policy;
171     g_service->observer_ = nullptr;
172     policy.FindSubscribeActionValue();
173 
174     std::map<std::string, std::string> stateMap;
175     stateMap["test"] = "test";
176     EXPECT_FALSE(policy.StateMachineDecision(stateMap));
177 
178     // The second term is nullptr
179     auto& collectionMap = g_service->state_->stateCollectionMap_;
180     collectionMap["test"] = nullptr;
181     EXPECT_FALSE(policy.StateMachineDecision(stateMap));
182 
183     // for loop return false
184     collectionMap["test"] = std::make_shared<ScreenStateCollection>();
185     EXPECT_FALSE(policy.StateMachineDecision(stateMap));
186 
187     // for loop continue or retrun true
188     stateMap["test"] = "1";
189     auto& powerMgrClient = PowerMgrClient::GetInstance();
190     if (powerMgrClient.IsScreenOn()) {
191         EXPECT_TRUE(policy.StateMachineDecision(stateMap));
192     } else {
193         EXPECT_FALSE(policy.StateMachineDecision(stateMap));
194     }
195     stateMap["test1"] = "0";
196     collectionMap["test1"] = std::make_shared<ScreenStateCollection>();
197     EXPECT_FALSE(policy.StateMachineDecision(stateMap));
198     THERMAL_HILOGI(LABEL_TEST, "ThermalPolicyTest005 end.");
199 }
200 } // namespace
201