• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_hub_test.h"
17 
18 #include <condition_variable>
19 #include <mutex>
20 
21 #include "constants.h"
22 #include "mock_thermal_mgr_client.h"
23 #include "thermal_log.h"
24 #include "thermal_mgr_client.h"
25 
26 #define private   public
27 #define protected public
28 #include "thermal_service.h"
29 #include "thermal_srv_config_parser.h"
30 #include "v1_1/ithermal_interface.h"
31 #include "v1_1/thermal_types.h"
32 #undef private
33 #undef protected
34 
35 using namespace testing::ext;
36 using namespace OHOS::PowerMgr;
37 using namespace OHOS;
38 using namespace std;
39 using namespace OHOS::HDI::Thermal::V1_1;
40 
41 namespace {
42 std::vector<std::string> g_typeList;
43 std::condition_variable g_callbackCV;
44 std::mutex g_mutex;
45 constexpr int64_t TIME_OUT = 1;
46 bool g_callbackTriggered = false;
47 const std::string SYSTEM_THERMAL_SERVICE_CONFIG_PATH = "/system/etc/thermal_config/thermal_service_config.xml";
48 sptr<ThermalService> g_service = nullptr;
49 auto& g_thermalMgrClient = ThermalMgrClient::GetInstance();
50 
Notify()51 void Notify()
52 {
53     std::unique_lock<std::mutex> lock(g_mutex);
54     g_callbackTriggered = true;
55     lock.unlock();
56     g_callbackCV.notify_one();
57 }
58 
Wait()59 void Wait()
60 {
61     std::unique_lock<std::mutex> lock(g_mutex);
62     g_callbackCV.wait_for(lock, std::chrono::seconds(TIME_OUT), [] {
63         return g_callbackTriggered;
64     });
65     EXPECT_TRUE(g_callbackTriggered);
66     g_callbackTriggered = false;
67 }
68 } // namespace
69 
TearDown()70 void ThermalActionHubTest::TearDown()
71 {
72     g_callbackTriggered = false;
73 }
74 
SetUpTestCase()75 void ThermalActionHubTest::SetUpTestCase()
76 {
77     g_service = DelayedSpSingleton<ThermalService>::GetInstance();
78     g_service->InitSystemTestModules();
79     g_service->OnStart();
80     g_service->InitStateMachine();
81     g_service->InitActionManager();
82 }
83 
TearDownTestCase()84 void ThermalActionHubTest::TearDownTestCase()
85 {
86     g_service->OnStop();
87 }
88 
InitData()89 void ThermalActionHubTest::InitData()
90 {
91     g_typeList.push_back(BATTERY);
92     g_typeList.push_back(SOC);
93 }
94 
OnThermalActionChanged(ActionCallbackMap & actionCbMap)95 bool ThermalActionHubTest::ThermalActionTest1Callback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
96 {
97     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest1Callback::OnThermalActionChanged Enter");
98     int32_t cpuBigFreq = 1992000;
99     bool isFind = false;
100     for (auto iter : actionCbMap) {
101         if (iter.first == "cpu_big") {
102             EXPECT_EQ(std::stoi(iter.second), cpuBigFreq);
103             isFind = true;
104         }
105         GTEST_LOG_(INFO) << "actionName: " << iter.first << " actionValue: " << iter.second;
106     }
107     EXPECT_TRUE(isFind);
108     Notify();
109     return true;
110 }
111 
OnThermalActionChanged(ActionCallbackMap & actionCbMap)112 bool ThermalActionHubTest::ThermalActionTest2Callback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
113 {
114     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest2Callback::OnThermalActionChanged Enter");
115     std::string lcd = "0.9";
116     bool isFind = false;
117     for (auto iter : actionCbMap) {
118         if (iter.first == "lcd") {
119             // 0: begin position; 3: end position
120             EXPECT_EQ(iter.second.substr(0, 3), lcd);
121             isFind = true;
122         }
123         GTEST_LOG_(INFO) << "actionName: " << iter.first << " actionValue: " << iter.second;
124     }
125     EXPECT_TRUE(isFind);
126     Notify();
127     return true;
128 }
129 
OnThermalActionChanged(ActionCallbackMap & actionCbMap)130 bool ThermalActionHubTest::ThermalActionTest3Callback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
131 {
132     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest3Callback::OnThermalActionChanged Enter");
133     std::string cpuMedFreq = "1989500";
134     std::string lcd = "0.8";
135     bool isFindCpuMed = false;
136     bool isFindLcd = false;
137     for (auto iter : actionCbMap) {
138         if (iter.first == "cpu_med") {
139             EXPECT_EQ(iter.second, cpuMedFreq);
140             isFindCpuMed = true;
141         }
142         if (iter.first == "lcd") {
143             // 0: begin position; 3: end position
144             EXPECT_EQ(iter.second.substr(0, 3), lcd);
145             isFindLcd = true;
146         }
147         GTEST_LOG_(INFO) << "actionName: " << iter.first << " actionValue: " << iter.second;
148     }
149     EXPECT_TRUE(isFindCpuMed);
150     EXPECT_TRUE(isFindLcd);
151     Notify();
152     return true;
153 }
154 
OnThermalActionChanged(ActionCallbackMap & actionCbMap)155 bool ThermalActionHubTest::ThermalActionTest4Callback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
156 {
157     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest4Callback::OnThermalActionChanged Enter");
158     std::string lcd = "0.99";
159     bool isFind = false;
160     for (auto iter : actionCbMap) {
161         if (iter.first == "lcd") {
162             // 0: begin position; 4: end position
163             EXPECT_EQ(iter.second.substr(0, 4), lcd);
164             isFind = true;
165         }
166         GTEST_LOG_(INFO) << "actionName: " << iter.first << " actionValue: " << iter.second;
167     }
168     EXPECT_TRUE(isFind);
169     Notify();
170     return true;
171 }
172 
OnThermalActionChanged(ActionCallbackMap & actionCbMap)173 bool ThermalActionHubTest::ThermalActionTest5Callback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
174 {
175     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest5Callback::OnThermalActionChanged Enter");
176     std::string lcd = "0.88";
177     bool isFind = false;
178     for (auto iter : actionCbMap) {
179         if (iter.first == "lcd") {
180             // 0: begin position; 4: end position
181             EXPECT_EQ(iter.second.substr(0, 4), lcd);
182             isFind = true;
183         }
184         GTEST_LOG_(INFO) << "actionName: " << iter.first << " actionValue: " << iter.second;
185     }
186     EXPECT_TRUE(isFind);
187     Notify();
188     return true;
189 }
190 
OnThermalActionChanged(ActionCallbackMap & actionCbMap)191 bool ThermalActionHubTest::ThermalActionTest6Callback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
192 {
193     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest6Callback::OnThermalActionChanged Enter");
194     std::string lcd = "0.77";
195     bool isFind = false;
196     for (auto iter : actionCbMap) {
197         if (iter.first == "lcd") {
198             // 0: begin position; 4: end position
199             EXPECT_EQ(iter.second.substr(0, 4), lcd);
200             isFind = true;
201         }
202         GTEST_LOG_(INFO) << "actionName: " << iter.first << " actionValue: " << iter.second;
203     }
204     EXPECT_TRUE(isFind);
205     Notify();
206     return true;
207 }
208 
OnThermalActionChanged(ActionCallbackMap & actionCbMap)209 bool ThermalActionHubTest::ThermalActionTest7Callback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
210 {
211     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest7Callback::OnThermalActionChanged Enter");
212     bool isFind = false;
213     for (auto iter : actionCbMap) {
214         if (iter.first == "boost") {
215             EXPECT_TRUE(static_cast<bool>(std::stoi(iter.second)));
216             isFind = true;
217         }
218         GTEST_LOG_(INFO) << "actionName: " << iter.first << " actionValue: " << iter.second;
219     }
220     EXPECT_TRUE(isFind);
221     Notify();
222     return true;
223 }
224 
OnThermalActionChanged(ActionCallbackMap & actionCbMap)225 bool ThermalActionHubTest::ThermalActionTest8Callback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
226 {
227     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest8Callback::OnThermalActionChanged Enter");
228     bool isFind = false;
229     for (auto iter : actionCbMap) {
230         if (iter.first == "isolate") {
231             EXPECT_TRUE(static_cast<bool>(std::stoi(iter.second)));
232             isFind = true;
233         }
234         GTEST_LOG_(INFO) << "actionName: " << iter.first << " actionValue: " << iter.second;
235     }
236     EXPECT_TRUE(isFind);
237     Notify();
238     return true;
239 }
240 
241 namespace {
242 /**
243  * @tc.name: ThermalActionHubTest001
244  * @tc.desc: register action is cpu_big test
245  * @tc.type: FUNC
246  */
247 HWTEST_F(ThermalActionHubTest, ThermalActionHubTest001, TestSize.Level0)
248 {
249     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest001 start");
250     std::vector<std::string> actionList;
251     actionList.push_back("cpu_big");
252     std::string desc = "";
253     InitData();
254     const sptr<IThermalActionCallback> cb1 = new ThermalActionTest1Callback();
255     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest001 start register");
256     g_thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, cb1);
257     g_service->SubscribeThermalActionCallback(actionList, desc, cb1);
258     HdfThermalCallbackInfo event;
259     ThermalZoneInfo info1;
260     info1.type = "battery";
261     info1.temp = 40100;
262     event.info.push_back(info1);
263     g_service->HandleThermalCallbackEvent(event);
264     Wait();
265     g_thermalMgrClient.UnSubscribeThermalActionCallback(cb1);
266     g_service->UnSubscribeThermalActionCallback(cb1);
267     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest001 end");
268 }
269 
270 /**
271  * @tc.name: ThermalActionHubTest002
272  * @tc.desc: register action is lcd test
273  * @tc.type: FUNC
274  */
275 HWTEST_F(ThermalActionHubTest, ThermalActionHubTest002, TestSize.Level0)
276 {
277     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest002 start");
278     std::vector<std::string> actionList;
279     actionList.push_back("lcd");
280     std::string desc = "";
281     InitData();
282     const sptr<IThermalActionCallback> cb2 = new ThermalActionTest2Callback();
283     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest002 start register");
284     g_thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, cb2);
285     g_service->SubscribeThermalActionCallback(actionList, desc, cb2);
286     HdfThermalCallbackInfo event;
287     ThermalZoneInfo info1;
288     info1.type = "battery";
289     info1.temp = 43100;
290     event.info.push_back(info1);
291     g_service->HandleThermalCallbackEvent(event);
292     Wait();
293     g_thermalMgrClient.UnSubscribeThermalActionCallback(cb2);
294     g_service->UnSubscribeThermalActionCallback(cb2);
295     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest002 end");
296 }
297 
298 /**
299  * @tc.name: ThermalActionHubTest003
300  * @tc.desc: register action is cpu_med and lcd test
301  * @tc.type: FUNC
302  */
303 HWTEST_F(ThermalActionHubTest, ThermalActionHubTest003, TestSize.Level0)
304 {
305     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest003 start");
306     std::vector<std::string> actionList;
307     actionList.push_back("cpu_med");
308     actionList.push_back("lcd");
309     std::string desc = "";
310     InitData();
311     const sptr<IThermalActionCallback> cb3 = new ThermalActionTest3Callback();
312     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest003 start register");
313     g_thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, cb3);
314     g_service->SubscribeThermalActionCallback(actionList, desc, cb3);
315     HdfThermalCallbackInfo event;
316     ThermalZoneInfo info1;
317     info1.type = "battery";
318     info1.temp = 46100;
319     event.info.push_back(info1);
320     g_service->HandleThermalCallbackEvent(event);
321     Wait();
322     g_thermalMgrClient.UnSubscribeThermalActionCallback(cb3);
323     g_service->UnSubscribeThermalActionCallback(cb3);
324     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest003 end");
325 }
326 
327 /**
328  * @tc.name: ThermalActionHubTest004
329  * @tc.desc: register action is lcd test, scene cam, level 1
330  * @tc.type: FUNC
331  */
332 HWTEST_F(ThermalActionHubTest, ThermalActionHubTest004, TestSize.Level0)
333 {
334     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest004 start");
335     std::vector<std::string> actionList;
336     actionList.push_back("lcd");
337     std::string desc = "";
338     InitData();
339     const sptr<IThermalActionCallback> cb4 = new ThermalActionTest4Callback();
340     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest004 start register");
341     g_thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, cb4);
342     g_service->SubscribeThermalActionCallback(actionList, desc, cb4);
343     g_thermalMgrClient.SetScene("cam");
344     g_service->SetScene("cam");
345     HdfThermalCallbackInfo event;
346     ThermalZoneInfo info1;
347     info1.type = "battery";
348     info1.temp = 40100;
349     event.info.push_back(info1);
350     g_service->HandleThermalCallbackEvent(event);
351     Wait();
352     g_thermalMgrClient.UnSubscribeThermalActionCallback(cb4);
353     g_service->UnSubscribeThermalActionCallback(cb4);
354     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest004 end");
355 }
356 
357 /**
358  * @tc.name: ThermalActionHubTest005
359  * @tc.desc: register action is lcd test, scene call, level 2
360  * @tc.type: FUNC
361  */
362 HWTEST_F(ThermalActionHubTest, ThermalActionHubTest005, TestSize.Level0)
363 {
364     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest005 start");
365     std::vector<std::string> actionList;
366     actionList.push_back("lcd");
367     std::string desc = "";
368     InitData();
369     const sptr<IThermalActionCallback> cb5 = new ThermalActionTest5Callback();
370     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest005 start register");
371     g_thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, cb5);
372     g_service->SubscribeThermalActionCallback(actionList, desc, cb5);
373     g_thermalMgrClient.SetScene("call");
374     g_service->SetScene("call");
375     HdfThermalCallbackInfo event;
376     ThermalZoneInfo info1;
377     info1.type = "battery";
378     info1.temp = 43100;
379     event.info.push_back(info1);
380     g_service->HandleThermalCallbackEvent(event);
381     Wait();
382     g_thermalMgrClient.UnSubscribeThermalActionCallback(cb5);
383     g_service->UnSubscribeThermalActionCallback(cb5);
384     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest005 end");
385 }
386 
387 /**
388  * @tc.name: ThermalActionHubTest006
389  * @tc.desc: register action is lcd test, scene game, level 3
390  * @tc.type: FUNC
391  */
392 HWTEST_F(ThermalActionHubTest, ThermalActionHubTest006, TestSize.Level0)
393 {
394     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest006 start");
395     std::vector<std::string> actionList;
396     actionList.push_back("lcd");
397     std::string desc = "";
398     InitData();
399     const sptr<IThermalActionCallback> cb6 = new ThermalActionTest6Callback();
400     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest006 start register");
401     g_thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, cb6);
402     g_service->SubscribeThermalActionCallback(actionList, desc, cb6);
403     g_thermalMgrClient.SetScene("game");
404     g_service->SetScene("game");
405     HdfThermalCallbackInfo event;
406     ThermalZoneInfo info1;
407     info1.type = "battery";
408     info1.temp = 46100;
409     event.info.push_back(info1);
410     g_service->HandleThermalCallbackEvent(event);
411     Wait();
412     g_thermalMgrClient.UnSubscribeThermalActionCallback(cb6);
413     g_service->UnSubscribeThermalActionCallback(cb6);
414     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest006 end");
415 }
416 
417 /**
418  * @tc.name: ThermalActionHubTest007
419  * @tc.desc: register action is boost test
420  * @tc.type: FUNC
421  * @tc.require: issueI6JSQD
422  */
423 HWTEST_F(ThermalActionHubTest, ThermalActionHubTest007, TestSize.Level0)
424 {
425     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest007 start");
426     std::vector<std::string> actionList;
427     actionList.push_back("boost");
428     std::string desc = "";
429     InitData();
430     const sptr<IThermalActionCallback> cbBoost = new ThermalActionTest7Callback();
431     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest001 start register");
432     g_thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, cbBoost);
433     g_service->SubscribeThermalActionCallback(actionList, desc, cbBoost);
434     HdfThermalCallbackInfo event;
435     ThermalZoneInfo info1;
436     info1.type = "battery";
437     info1.temp = 40100;
438     event.info.push_back(info1);
439     g_service->HandleThermalCallbackEvent(event);
440     Wait();
441     g_thermalMgrClient.UnSubscribeThermalActionCallback(cbBoost);
442     g_service->UnSubscribeThermalActionCallback(cbBoost);
443     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest001 end");
444 }
445 
446 /**
447  * @tc.name: ThermalActionHubTest008
448  * @tc.desc: register action is isolate cpu test
449  * @tc.type: FUNC
450  */
451 HWTEST_F(ThermalActionHubTest, ThermalActionHubTest008, TestSize.Level0)
452 {
453     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest008 start");
454     std::vector<std::string> actionList;
455     actionList.push_back("isolate");
456     std::string desc = "";
457     InitData();
458     const sptr<IThermalActionCallback> cbIsolateCpu = new ThermalActionTest8Callback();
459     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest008 start register");
460     g_thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, cbIsolateCpu);
461     g_service->SubscribeThermalActionCallback(actionList, desc, cbIsolateCpu);
462     HdfThermalCallbackInfo event;
463     ThermalZoneInfo info1;
464     info1.type = "battery";
465     info1.temp = 43100;
466     event.info.push_back(info1);
467     g_service->HandleThermalCallbackEvent(event);
468     Wait();
469     g_thermalMgrClient.UnSubscribeThermalActionCallback(cbIsolateCpu);
470     g_service->UnSubscribeThermalActionCallback(cbIsolateCpu);
471     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest008 end");
472 }
473 } // namespace
474