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