• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <cstdio>
19 #include <cstdlib>
20 #include <datetime_ex.h>
21 #include <fcntl.h>
22 #include <gtest/gtest.h>
23 #include <iostream>
24 #include <ipc_skeleton.h>
25 #include <string>
26 #include <string_ex.h>
27 
28 #include "constants.h"
29 #include "ithermal_srv.h"
30 #include "mock_thermal_mgr_client.h"
31 #include "securec.h"
32 #include "thermal_common.h"
33 #include "thermal_mgr_client.h"
34 #include "thermal_srv_sensor_info.h"
35 
36 using namespace testing::ext;
37 using namespace OHOS::PowerMgr;
38 using namespace OHOS;
39 using namespace std;
40 
41 namespace {
42 std::vector<std::string> g_typeList;
43 }
44 
WriteFile(std::string path,std::string buf,size_t size)45 int32_t ThermalActionHubTest::WriteFile(std::string path, std::string buf, size_t size)
46 {
47     FILE* stream = fopen(path.c_str(), "w+");
48     if (stream == nullptr) {
49         return ERR_INVALID_VALUE;
50     }
51     size_t ret = fwrite(buf.c_str(), strlen(buf.c_str()), 1, stream);
52     if (ret == ERR_OK) {
53         THERMAL_HILOGE(LABEL_TEST, "ret=%{public}zu", ret);
54     }
55     int32_t state = fseek(stream, 0, SEEK_SET);
56     if (state != ERR_OK) {
57         fclose(stream);
58         return state;
59     }
60     state = fclose(stream);
61     if (state != ERR_OK) {
62         return state;
63     }
64     return ERR_OK;
65 }
66 
ReadFile(const char * path,char * buf,size_t size)67 int32_t ThermalActionHubTest::ReadFile(const char* path, char* buf, size_t size)
68 {
69     int32_t ret;
70 
71     int32_t fd = open(path, O_RDONLY, S_IRUSR | S_IRGRP | S_IROTH);
72     if (fd < ERR_OK) {
73         THERMAL_HILOGD(LABEL_TEST, "WriteFile: failed to open file fd: %{public}d", fd);
74         return ERR_INVALID_VALUE;
75     }
76 
77     ret = read(fd, buf, size);
78     if (ret < ERR_OK) {
79         THERMAL_HILOGD(LABEL_TEST, "WriteFile: failed to read file ret: %{public}d", ret);
80         close(fd);
81         return ERR_INVALID_VALUE;
82     }
83 
84     close(fd);
85     buf[size - 1] = '\0';
86     return ERR_OK;
87 }
88 
SetUpTestCase()89 void ThermalActionHubTest::SetUpTestCase() {}
90 
TearDownTestCase()91 void ThermalActionHubTest::TearDownTestCase()
92 {
93     auto& thermalMgrClient = ThermalMgrClient::GetInstance();
94     thermalMgrClient.SetScene("");
95 }
96 
SetUp()97 void ThermalActionHubTest::SetUp() {}
98 
TearDown()99 void ThermalActionHubTest::TearDown() {}
100 
InitData()101 void ThermalActionHubTest::InitData()
102 {
103     g_typeList.push_back(BATTERY);
104     g_typeList.push_back(SOC);
105 }
106 
OnThermalActionChanged(ActionCallbackMap & actionCbMap)107 bool ThermalActionHubTest::ThermalActionTest1Callback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
108 {
109     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest1Callback::OnThermalActionChanged Enter");
110     int32_t cpuBigFreq = 1992000;
111     for (auto iter : actionCbMap) {
112         if (iter.first == "cpu_big") {
113             EXPECT_EQ(iter.second, cpuBigFreq);
114         }
115         GTEST_LOG_(INFO) << "actionName: " << iter.first << " actionValue: " << iter.second;
116     }
117     return true;
118 }
119 
OnThermalActionChanged(ActionCallbackMap & actionCbMap)120 bool ThermalActionHubTest::ThermalActionTest2Callback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
121 {
122     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest2Callback::OnThermalActionChanged Enter");
123     std::string lcd = "0.9";
124     for (auto iter : actionCbMap) {
125         if (iter.first == "lcd") {
126             // 0: begin position; 3: end position
127             EXPECT_EQ(std::to_string(iter.second).substr(0, 3), lcd);
128         }
129         GTEST_LOG_(INFO) << "actionName: " << iter.first << " actionValue: " << iter.second;
130     }
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     int32_t cpuMedFreq = 1989500;
138     std::string lcd = "0.8";
139     for (auto iter : actionCbMap) {
140         if (iter.first == "cpu_med") {
141             EXPECT_EQ(iter.second, cpuMedFreq);
142         }
143         if (iter.first == "lcd") {
144             // 0: begin position; 3: end position
145             EXPECT_EQ(std::to_string(iter.second).substr(0, 3), lcd);
146         }
147         GTEST_LOG_(INFO) << "actionName: " << iter.first << " actionValue: " << iter.second;
148     }
149     return true;
150 }
151 
OnThermalActionChanged(ActionCallbackMap & actionCbMap)152 bool ThermalActionHubTest::ThermalActionTest4Callback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
153 {
154     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest4Callback::OnThermalActionChanged Enter");
155     std::string lcd = "0.99";
156     for (auto iter : actionCbMap) {
157         if (iter.first == "lcd") {
158             // 0: begin position; 4: end position
159             EXPECT_EQ(std::to_string(iter.second).substr(0, 4), lcd);
160         }
161         GTEST_LOG_(INFO) << "actionName: " << iter.first << " actionValue: " << iter.second;
162     }
163     return true;
164 }
165 
OnThermalActionChanged(ActionCallbackMap & actionCbMap)166 bool ThermalActionHubTest::ThermalActionTest5Callback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
167 {
168     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest5Callback::OnThermalActionChanged Enter");
169     std::string lcd = "0.88";
170     for (auto iter : actionCbMap) {
171         if (iter.first == "lcd") {
172             // 0: begin position; 4: end position
173             EXPECT_EQ(std::to_string(iter.second).substr(0, 4), lcd);
174         }
175         GTEST_LOG_(INFO) << "actionName: " << iter.first << " actionValue: " << iter.second;
176     }
177     return true;
178 }
179 
OnThermalActionChanged(ActionCallbackMap & actionCbMap)180 bool ThermalActionHubTest::ThermalActionTest6Callback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
181 {
182     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest6Callback::OnThermalActionChanged Enter");
183     std::string lcd = "0.77";
184     for (auto iter : actionCbMap) {
185         if (iter.first == "lcd") {
186             // 0: begin position; 4: end position
187             EXPECT_EQ(std::to_string(iter.second).substr(0, 4), lcd);
188         }
189         GTEST_LOG_(INFO) << "actionName: " << iter.first << " actionValue: " << iter.second;
190     }
191     return true;
192 }
193 
194 namespace {
195 /**
196  * @tc.name: ThermalActionHubTest001
197  * @tc.desc: register action is cpu_big test
198  * @tc.type: FUNC
199  */
200 HWTEST_F(ThermalActionHubTest, ThermalActionHubTest001, TestSize.Level0)
201 {
202     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest001 start.");
203     std::vector<std::string> actionList;
204     actionList.push_back("cpu_big");
205 
206     std::string desc = "";
207     char batteryTempBuf[MAX_PATH] = {0};
208     char socTempBuf[MAX_PATH] = {0};
209     int32_t ret = -1;
210     InitData();
211     ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, BATTERY_PATH.c_str());
212     EXPECT_EQ(true, ret >= EOK);
213 
214     ret = snprintf_s(socTempBuf, MAX_PATH, sizeof(socTempBuf) - 1, SOC_PATH.c_str());
215     EXPECT_EQ(true, ret >= EOK);
216     auto& thermalMgrClient = ThermalMgrClient::GetInstance();
217     const sptr<IThermalActionCallback> cb1 = new ThermalActionTest1Callback();
218 
219     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest001 start register");
220     thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, cb1);
221 
222     int32_t batteryTemp = 40100;
223     std::string sTemp = to_string(batteryTemp) + "\n";
224     ret = ThermalActionHubTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
225     EXPECT_EQ(true, ret == ERR_OK);
226 
227     MockThermalMgrClient::GetInstance().GetThermalInfo();
228     thermalMgrClient.UnSubscribeThermalActionCallback(cb1);
229     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest001 end.");
230 }
231 
232 /**
233  * @tc.name: ThermalActionHubTest002
234  * @tc.desc: register action is lcd test
235  * @tc.type: FUNC
236  */
237 HWTEST_F(ThermalActionHubTest, ThermalActionHubTest002, TestSize.Level0)
238 {
239     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest002 start.");
240     const std::string LCD = "lcd";
241     std::vector<std::string> actionList;
242     actionList.push_back(LCD);
243 
244     std::string desc = "";
245     char batteryTempBuf[MAX_PATH] = {0};
246     char socTempBuf[MAX_PATH] = {0};
247     int32_t ret = -1;
248     InitData();
249     ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, BATTERY_PATH.c_str());
250     EXPECT_EQ(true, ret >= EOK);
251 
252     ret = snprintf_s(socTempBuf, MAX_PATH, sizeof(socTempBuf) - 1, SOC_PATH.c_str());
253     EXPECT_EQ(true, ret >= EOK);
254     auto& thermalMgrClient = ThermalMgrClient::GetInstance();
255     const sptr<IThermalActionCallback> cb2 = new ThermalActionTest2Callback();
256 
257     int32_t batteryTemp = 0;
258     std::string sTemp = to_string(batteryTemp) + "\n";
259     ret = ThermalActionHubTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
260     EXPECT_EQ(true, ret == ERR_OK);
261 
262     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest002 start register");
263     thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, cb2);
264 
265     batteryTemp = 43100;
266     sTemp = to_string(batteryTemp) + "\n";
267     ret = ThermalActionHubTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
268     EXPECT_EQ(true, ret == ERR_OK);
269 
270     MockThermalMgrClient::GetInstance().GetThermalInfo();
271     thermalMgrClient.UnSubscribeThermalActionCallback(cb2);
272     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest002 end.");
273 }
274 
275 /**
276  * @tc.name: ThermalActionHubTest003
277  * @tc.desc: register action is cpu_med and lcd test
278  * @tc.type: FUNC
279  */
280 HWTEST_F(ThermalActionHubTest, ThermalActionHubTest003, TestSize.Level0)
281 {
282     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest003 start.");
283     const std::string LCD = "lcd";
284     std::vector<std::string> actionList;
285     actionList.push_back("cpu_med");
286     actionList.push_back(LCD);
287 
288     std::string desc = "";
289     char batteryTempBuf[MAX_PATH] = {0};
290     char socTempBuf[MAX_PATH] = {0};
291     int32_t ret = -1;
292     InitData();
293     ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, BATTERY_PATH.c_str());
294     EXPECT_EQ(true, ret >= EOK);
295 
296     ret = snprintf_s(socTempBuf, MAX_PATH, sizeof(socTempBuf) - 1, SOC_PATH.c_str());
297     EXPECT_EQ(true, ret >= EOK);
298     auto& thermalMgrClient = ThermalMgrClient::GetInstance();
299     const sptr<IThermalActionCallback> cb3 = new ThermalActionTest3Callback();
300 
301     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest003 start register");
302     thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, cb3);
303 
304     int32_t batteryTemp = 46100;
305     std::string sTemp = to_string(batteryTemp) + "\n";
306     ret = ThermalActionHubTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
307     EXPECT_EQ(true, ret == ERR_OK);
308 
309     MockThermalMgrClient::GetInstance().GetThermalInfo();
310     thermalMgrClient.UnSubscribeThermalActionCallback(cb3);
311     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest003 end.");
312 }
313 
314 /**
315  * @tc.name: ThermalActionHubTest004
316  * @tc.desc: register action is lcd test, scene cam, level 1
317  * @tc.type: FUNC
318  */
319 HWTEST_F(ThermalActionHubTest, ThermalActionHubTest004, TestSize.Level0)
320 {
321     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest004 start.");
322     const std::string LCD = "lcd";
323     std::vector<std::string> actionList;
324     actionList.push_back(LCD);
325 
326     std::string desc = "";
327     char batteryTempBuf[MAX_PATH] = {0};
328     char socTempBuf[MAX_PATH] = {0};
329     int32_t ret = -1;
330     InitData();
331     ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, BATTERY_PATH.c_str());
332     EXPECT_EQ(true, ret >= EOK);
333 
334     ret = snprintf_s(socTempBuf, MAX_PATH, sizeof(socTempBuf) - 1, SOC_PATH.c_str());
335     EXPECT_EQ(true, ret >= EOK);
336     auto& thermalMgrClient = ThermalMgrClient::GetInstance();
337     const sptr<IThermalActionCallback> cb4 = new ThermalActionTest4Callback();
338 
339     int32_t batteryTemp = 0;
340     std::string sTemp = to_string(batteryTemp) + "\n";
341     ret = ThermalActionHubTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
342     EXPECT_EQ(true, ret == ERR_OK);
343 
344     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest004 start register");
345     thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, cb4);
346 
347     thermalMgrClient.SetScene("cam");
348     batteryTemp = 40100;
349     sTemp = to_string(batteryTemp) + "\n";
350     ret = ThermalActionHubTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
351     EXPECT_EQ(true, ret == ERR_OK);
352 
353     MockThermalMgrClient::GetInstance().GetThermalInfo();
354     thermalMgrClient.UnSubscribeThermalActionCallback(cb4);
355     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest004 end.");
356 }
357 
358 /**
359  * @tc.name: ThermalActionHubTest005
360  * @tc.desc: register action is lcd test, scene call, level 2
361  * @tc.type: FUNC
362  */
363 HWTEST_F(ThermalActionHubTest, ThermalActionHubTest005, TestSize.Level0)
364 {
365     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest005 start.");
366     const std::string LCD = "lcd";
367     std::vector<std::string> actionList;
368     actionList.push_back(LCD);
369 
370     std::string desc = "";
371     char batteryTempBuf[MAX_PATH] = {0};
372     char socTempBuf[MAX_PATH] = {0};
373     int32_t ret = -1;
374     InitData();
375     ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, BATTERY_PATH.c_str());
376     EXPECT_EQ(true, ret >= EOK);
377 
378     ret = snprintf_s(socTempBuf, MAX_PATH, sizeof(socTempBuf) - 1, SOC_PATH.c_str());
379     EXPECT_EQ(true, ret >= EOK);
380     auto& thermalMgrClient = ThermalMgrClient::GetInstance();
381     const sptr<IThermalActionCallback> cb5 = new ThermalActionTest5Callback();
382 
383     int32_t batteryTemp = 0;
384     std::string sTemp = to_string(batteryTemp) + "\n";
385     ret = ThermalActionHubTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
386     EXPECT_EQ(true, ret == ERR_OK);
387 
388     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest005 start register");
389     thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, cb5);
390 
391     thermalMgrClient.SetScene("call");
392     batteryTemp = 43100;
393     sTemp = to_string(batteryTemp) + "\n";
394     ret = ThermalActionHubTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
395     EXPECT_EQ(true, ret == ERR_OK);
396 
397     MockThermalMgrClient::GetInstance().GetThermalInfo();
398     thermalMgrClient.UnSubscribeThermalActionCallback(cb5);
399     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest005 end.");
400 }
401 
402 /**
403  * @tc.name: ThermalActionHubTest006
404  * @tc.desc: register action is lcd test, scene game, level 3
405  * @tc.type: FUNC
406  */
407 HWTEST_F(ThermalActionHubTest, ThermalActionHubTest006, TestSize.Level0)
408 {
409     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest006 start.");
410     const std::string LCD = "lcd";
411     std::vector<std::string> actionList;
412     actionList.push_back(LCD);
413 
414     std::string desc = "";
415     char batteryTempBuf[MAX_PATH] = {0};
416     char socTempBuf[MAX_PATH] = {0};
417     int32_t ret = -1;
418     InitData();
419     ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, BATTERY_PATH.c_str());
420     EXPECT_EQ(true, ret >= EOK);
421 
422     ret = snprintf_s(socTempBuf, MAX_PATH, sizeof(socTempBuf) - 1, SOC_PATH.c_str());
423     EXPECT_EQ(true, ret >= EOK);
424     auto& thermalMgrClient = ThermalMgrClient::GetInstance();
425     const sptr<IThermalActionCallback> cb6 = new ThermalActionTest6Callback();
426 
427     int32_t batteryTemp = 0;
428     std::string sTemp = to_string(batteryTemp) + "\n";
429     ret = ThermalActionHubTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
430     EXPECT_EQ(true, ret == ERR_OK);
431 
432     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest006 start register");
433     thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, cb6);
434 
435     thermalMgrClient.SetScene("game");
436     batteryTemp = 46100;
437     sTemp = to_string(batteryTemp) + "\n";
438     ret = ThermalActionHubTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
439     EXPECT_EQ(true, ret == ERR_OK);
440 
441     MockThermalMgrClient::GetInstance().GetThermalInfo();
442     thermalMgrClient.UnSubscribeThermalActionCallback(cb6);
443     THERMAL_HILOGD(LABEL_TEST, "ThermalActionHubTest006 end.");
444 }
445 } // namespace
446