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