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_mgr_interface_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 <list>
26 #include <mutex>
27 #include <string>
28 #include <string_ex.h>
29 #include <unistd.h>
30
31 #include "constants.h"
32 #include "ithermal_srv.h"
33 #include "securec.h"
34 #include "mock_thermal_mgr_client.h"
35 #include "thermal_common.h"
36 #include "thermal_mgr_client.h"
37 #include "thermal_srv_sensor_info.h"
38
39 using namespace testing::ext;
40 using namespace OHOS::PowerMgr;
41 using namespace OHOS;
42 using namespace std;
43
44 namespace {
45 static std::mutex g_mtx;
46 std::vector<std::string> typelist;
47 } // namespace
48
WriteFile(std::string path,std::string buf,size_t size)49 int32_t ThermalMgrInterfaceTest::WriteFile(std::string path, std::string buf, size_t size)
50 {
51 FILE* stream = fopen(path.c_str(), "w+");
52 if (stream == nullptr) {
53 return ERR_INVALID_VALUE;
54 }
55 size_t ret = fwrite(buf.c_str(), strlen(buf.c_str()), 1, stream);
56 if (ret == ERR_OK) {
57 THERMAL_HILOGE(COMP_SVC, "ret=%{public}zu", ret);
58 }
59 int32_t state = fseek(stream, 0, SEEK_SET);
60 if (state != ERR_OK) {
61 fclose(stream);
62 return state;
63 }
64 state = fclose(stream);
65 if (state != ERR_OK) {
66 return state;
67 }
68 return ERR_OK;
69 }
70
ReadFile(const char * path,char * buf,size_t size)71 int32_t ThermalMgrInterfaceTest::ReadFile(const char* path, char* buf, size_t size)
72 {
73 int32_t ret;
74
75 int32_t fd = open(path, O_RDONLY, S_IRUSR | S_IRGRP | S_IROTH);
76 if (fd < ERR_OK) {
77 THERMAL_HILOGE(LABEL_TEST, "WriteFile: failed to open file, fd: %{public}d", fd);
78 return ERR_INVALID_VALUE;
79 }
80
81 ret = read(fd, buf, size);
82 if (ret < ERR_OK) {
83 THERMAL_HILOGE(LABEL_TEST, "WriteFile: failed to read filed, ret: %{public}d", ret);
84 close(fd);
85 return ERR_INVALID_VALUE;
86 }
87
88 close(fd);
89 buf[size - 1] = '\0';
90 return ERR_OK;
91 }
92
ConvertInt(const std::string & value)93 int32_t ThermalMgrInterfaceTest::ConvertInt(const std::string& value)
94 {
95 return std::stoi(value);
96 }
97
SetUpTestCase()98 void ThermalMgrInterfaceTest::SetUpTestCase() {}
99
TearDownTestCase()100 void ThermalMgrInterfaceTest::TearDownTestCase() {}
101
SetUp()102 void ThermalMgrInterfaceTest::SetUp() {}
103
TearDown()104 void ThermalMgrInterfaceTest::TearDown() {}
105
InitData()106 void ThermalMgrInterfaceTest::InitData()
107 {
108 typelist.push_back(BATTERY);
109 typelist.push_back(SOC);
110 }
111
OnThermalTempChanged(TempCallbackMap & tempCbMap)112 bool ThermalMgrInterfaceTest::ThermalTempTest1Callback::OnThermalTempChanged(TempCallbackMap& tempCbMap)
113 {
114 int assertValue = 0;
115 for (auto iter : tempCbMap) {
116 THERMAL_HILOGD(LABEL_TEST, "type: %{public}s, temp: %{public}d", iter.first.c_str(), iter.second);
117 EXPECT_EQ(true, iter.second > assertValue) << "Test Failed";
118 }
119 return true;
120 }
121
OnThermalTempChanged(TempCallbackMap & tempCbMap)122 bool ThermalMgrInterfaceTest::ThermalTempTest2Callback::OnThermalTempChanged(TempCallbackMap& tempCbMap)
123 {
124 int assertValue = 0;
125 for (auto iter : tempCbMap) {
126 THERMAL_HILOGD(LABEL_TEST, "type: %{public}s, temp: %{public}d", iter.first.c_str(), iter.second);
127 EXPECT_EQ(true, iter.second > assertValue) << "Test Failed";
128 }
129 return true;
130 }
131
GetThermalLevel(ThermalLevel level)132 bool ThermalMgrInterfaceTest::ThermalLevelTest1Callback::GetThermalLevel(ThermalLevel level)
133 {
134 int assertMin = -1;
135 int assertMax = 6;
136 int32_t levelValue = static_cast<int32_t>(level);
137 THERMAL_HILOGD(LABEL_TEST, "level: %{public}d", levelValue);
138 EXPECT_EQ(true, levelValue >= assertMin && levelValue <= assertMax) << "Test Failed";
139 return true;
140 }
141
GetThermalLevel(ThermalLevel level)142 bool ThermalMgrInterfaceTest::ThermalLevelTest2Callback::GetThermalLevel(ThermalLevel level)
143 {
144 int assertMin = -1;
145 int assertMax = 6;
146 int32_t levelValue = static_cast<int32_t>(level);
147 THERMAL_HILOGD(LABEL_TEST, "level: %{public}d", levelValue);
148 EXPECT_EQ(true, levelValue >= assertMin && levelValue <= assertMax) << "Test Failed";
149 return true;
150 }
151
GetThermalLevel(ThermalLevel level)152 bool ThermalMgrInterfaceTest::ThermalLevelTest3Callback::GetThermalLevel(ThermalLevel level)
153 {
154 int assertMin = -1;
155 int assertMax = 6;
156 int32_t levelValue = static_cast<int32_t>(level);
157 THERMAL_HILOGD(LABEL_TEST, "level: %{public}d", levelValue);
158 EXPECT_EQ(true, levelValue >= assertMin && levelValue <= assertMax) << "Test Failed";
159 return true;
160 }
161
162 namespace {
163 /**
164 * @tc.name: ThermalMgrInterfaceTest001
165 * @tc.desc: test get sensor temp
166 * @tc.type: FUNC
167 */
168 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest001, TestSize.Level0)
169 {
170 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest001 start.");
171 char batteryTempBuf[MAX_PATH] = {0};
172 int32_t temp = 41000;
173 int32_t ret = -1;
174 std::string sTemp = to_string(temp) + "\n";
175 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
176 EXPECT_EQ(true, ret >= EOK);
177 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
178 EXPECT_EQ(true, ret == ERR_OK);
179
180 sleep(10 * SLEEP_WAIT_TIME_S);
181 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
182 int32_t out = thermalMgrClient.GetThermalSensorTemp(SensorType::BATTERY);
183 THERMAL_HILOGD(LABEL_TEST, "battry temp: %{public}d", out);
184 EXPECT_EQ(true, temp == out) << "ThermalMgrInterfaceTest001 Failed";
185 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest001 end.");
186 }
187
188 /**
189 * @tc.name: ThermalMgrInterfaceTest002
190 * @tc.desc: test get sensor temp
191 * @tc.type: FUNC
192 */
193 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest002, TestSize.Level0)
194 {
195 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest002 start.");
196 char socTempBuf[MAX_PATH] = {0};
197 int32_t temp = 10000;
198 int32_t ret = -1;
199 std::string sTemp = to_string(temp) + "\n";
200 ret = snprintf_s(socTempBuf, MAX_PATH, sizeof(socTempBuf) - 1, socPath.c_str());
201 EXPECT_EQ(true, ret >= EOK);
202 ret = ThermalMgrInterfaceTest::WriteFile(socTempBuf, sTemp, sTemp.length());
203 EXPECT_EQ(true, ret == ERR_OK);
204
205 sleep(10 * SLEEP_WAIT_TIME_S);
206 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
207 int32_t out = thermalMgrClient.GetThermalSensorTemp(SensorType::SOC);
208 THERMAL_HILOGD(LABEL_TEST, "soc temp:%{public}d", out);
209 EXPECT_EQ(true, temp == out) << "ThermalMgrInterfaceTest002 Failed";
210 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest002 end.");
211 }
212
213 /**
214 * @tc.name: ThermalMgrInterfaceTest003
215 * @tc.desc: test get sensor temp
216 * @tc.type: FUNC
217 */
218 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest003, TestSize.Level0)
219 {
220 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest003 start.");
221 char shellTempBuf[MAX_PATH] = {0};
222 int32_t temp = 11000;
223 int32_t ret = -1;
224 std::string sTemp = to_string(temp) + "\n";
225 ret = snprintf_s(shellTempBuf, MAX_PATH, sizeof(shellTempBuf) - 1, shellPath.c_str());
226 EXPECT_EQ(true, ret >= EOK);
227 ret = ThermalMgrInterfaceTest::WriteFile(shellTempBuf, sTemp, sTemp.length());
228 EXPECT_EQ(true, ret == ERR_OK);
229
230 sleep(10 * SLEEP_WAIT_TIME_S);
231 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
232 int32_t out = thermalMgrClient.GetThermalSensorTemp(SensorType::SHELL);
233 THERMAL_HILOGD(LABEL_TEST, "shell temp: %{public}d", out);
234 EXPECT_EQ(true, temp == out) << "ThermalMgrInterfaceTest003 Failed";
235 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest003 end.");
236 }
237
238 /**
239 * @tc.name: ThermalMgrInterfaceTest004
240 * @tc.desc: test get sensor temp
241 * @tc.type: FUNC
242 */
243 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest004, TestSize.Level0)
244 {
245 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest004 start.");
246 char cpuTempBuf[MAX_PATH] = {0};
247 int32_t temp = 12000;
248 int32_t ret = -1;
249 std::string sTemp = to_string(temp) + "\n";
250 ret = snprintf_s(cpuTempBuf, MAX_PATH, sizeof(cpuTempBuf) - 1, cpuPath.c_str());
251 EXPECT_EQ(true, ret >= EOK);
252 ret = ThermalMgrInterfaceTest::WriteFile(cpuTempBuf, sTemp, sTemp.length());
253 EXPECT_EQ(true, ret == ERR_OK);
254
255 sleep(10 * SLEEP_WAIT_TIME_S);
256 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
257 int32_t out = thermalMgrClient.GetThermalSensorTemp(SensorType::SENSOR1);
258 THERMAL_HILOGD(LABEL_TEST, "shell temp: %{public}d", out);
259 EXPECT_EQ(true, temp == out) << "ThermalMgrInterfaceTest004 Failed";
260 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest004 end.");
261 }
262
263 /**
264 * @tc.name: ThermalMgrInterfaceTest005
265 * @tc.desc: test get sensor temp
266 * @tc.type: FUNC
267 */
268 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest005, TestSize.Level0)
269 {
270 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest005 start.");
271 char chargerTempBuf[MAX_PATH] = {0};
272 int32_t temp = 13000;
273 int32_t ret = -1;
274 std::string sTemp = to_string(temp) + "\n";
275 ret = snprintf_s(chargerTempBuf, MAX_PATH, sizeof(chargerTempBuf) - 1, chargerPath.c_str());
276 EXPECT_EQ(true, ret >= EOK);
277 ret = ThermalMgrInterfaceTest::WriteFile(chargerTempBuf, sTemp, sTemp.length());
278 EXPECT_EQ(true, ret == ERR_OK);
279
280 sleep(10 * SLEEP_WAIT_TIME_S);
281 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
282 int32_t out = thermalMgrClient.GetThermalSensorTemp(SensorType::SENSOR2);
283 THERMAL_HILOGD(LABEL_TEST, "shell temp: %{public}d", out);
284 EXPECT_EQ(true, temp == out) << "ThermalMgrInterfaceTest005 Failed";
285 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest005 end.");
286 }
287
288 /**
289 * @tc.name: ThermalMgrInterfaceTest006
290 * @tc.desc: register callback and get temp list
291 * @tc.type: FUNC
292 */
293 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest006, TestSize.Level0)
294 {
295 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest006 start.");
296 char batteryTempBuf[MAX_PATH] = {0};
297 char socTempBuf[MAX_PATH] = {0};
298 int32_t ret = -1;
299 InitData();
300 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
301 EXPECT_EQ(true, ret >= EOK);
302 ret = snprintf_s(socTempBuf, MAX_PATH, sizeof(socTempBuf) - 1, socPath.c_str());
303 EXPECT_EQ(true, ret >= EOK);
304 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
305 const sptr<IThermalTempCallback> cb1 = new ThermalTempTest1Callback();
306
307 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest006 start register");
308 thermalMgrClient.SubscribeThermalTempCallback(typelist, cb1);
309 int32_t temp = 10000;
310 for (int i = 0; i < 10; i++) {
311 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest006 start change temp");
312 temp += 100;
313 std::string sTemp = to_string(temp) + "\n";
314 ret = ThermalMgrInterfaceTest::WriteFile(socTempBuf, sTemp, sTemp.length());
315 EXPECT_EQ(true, ret == ERR_OK);
316 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
317 EXPECT_EQ(true, ret == ERR_OK);
318 sleep(SLEEP_WAIT_TIME_S * 10);
319 }
320 thermalMgrClient.UnSubscribeThermalTempCallback(cb1);
321 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest006 end.");
322 }
323
324 /**
325 * @tc.name: ThermalMgrInterfaceTest007
326 * @tc.desc: register callback and get temp list
327 * @tc.type: FUNC
328 */
329 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest007, TestSize.Level0)
330 {
331 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest007 start.");
332 char batteryTempBuf[MAX_PATH] = {0};
333 char socTempBuf[MAX_PATH] = {0};
334 int32_t ret = -1;
335 InitData();
336 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
337 EXPECT_EQ(true, ret >= EOK);
338 ret = snprintf_s(socTempBuf, MAX_PATH, sizeof(socTempBuf) - 1, socPath.c_str());
339 EXPECT_EQ(true, ret >= EOK);
340 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
341 const sptr<IThermalTempCallback> cb1 = new ThermalTempTest1Callback();
342 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest007 start register");
343 thermalMgrClient.SubscribeThermalTempCallback(typelist, cb1);
344 int32_t temp = 10000;
345 for (int i = 0; i < 10; i++) {
346 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest007 start change temp");
347 temp += 100;
348 std::string sTemp = to_string(temp) + "\n";
349 ret = ThermalMgrInterfaceTest::WriteFile(socTempBuf, sTemp, sTemp.length());
350 EXPECT_EQ(true, ret == ERR_OK);
351 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
352 EXPECT_EQ(true, ret == ERR_OK);
353 sleep(SLEEP_WAIT_TIME_S * 10);
354 }
355 thermalMgrClient.UnSubscribeThermalTempCallback(cb1);
356 sleep(SLEEP_WAIT_TIME_S * 10);
357 const sptr<IThermalTempCallback> cb2 = new ThermalTempTest2Callback();
358 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest007 start register");
359 thermalMgrClient.SubscribeThermalTempCallback(typelist, cb1);
360 for (int i = 0; i < 10; i++) {
361 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest007 start change temp");
362 temp += 100;
363 std::string sTemp = to_string(temp) + "\n";
364 ret = ThermalMgrInterfaceTest::WriteFile(socTempBuf, sTemp, sTemp.length());
365 EXPECT_EQ(true, ret == ERR_OK);
366 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
367 EXPECT_EQ(true, ret == ERR_OK);
368 sleep(SLEEP_WAIT_TIME_S * 10);
369 }
370 thermalMgrClient.UnSubscribeThermalTempCallback(cb1);
371 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest007 end.");
372 }
373
374 /**
375 * @tc.name: ThermalMgrInterfaceTest008
376 * @tc.desc: test get level
377 * @tc.type: FUNC
378 */
379 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest008, TestSize.Level0)
380 {
381 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest008 start.");
382 char batteryTempBuf[MAX_PATH] = {0};
383 int32_t temp = 40100;
384 int32_t ret = -1;
385 std::string sTemp = to_string(temp) + "\n";
386 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
387 EXPECT_EQ(true, ret >= EOK);
388 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
389 EXPECT_EQ(true, ret == ERR_OK);
390
391 sleep(10 * SLEEP_WAIT_TIME_S);
392 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
393 ThermalLevel level = thermalMgrClient.GetThermalLevel();
394 int32_t levelValue = static_cast<int32_t>(level);
395 THERMAL_HILOGD(LABEL_TEST, "levelValue: %{public}d", levelValue);
396 EXPECT_EQ(true, level == ThermalLevel::NORMAL) << "ThermalMgrInterfaceTest008 Failed";
397 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest008 end.");
398 }
399
400 /**
401 * @tc.name: ThermalMgrInterfaceTest009
402 * @tc.desc: test get level
403 * @tc.type: FUNC
404 */
405 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest009, TestSize.Level0)
406 {
407 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest009 start.");
408 char batteryTempBuf[MAX_PATH] = {0};
409 int32_t temp = 43100;
410 int32_t ret = -1;
411 std::string sTemp = to_string(temp) + "\n";
412 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
413 EXPECT_EQ(true, ret >= EOK);
414 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
415 EXPECT_EQ(true, ret == ERR_OK);
416
417 sleep(10 * SLEEP_WAIT_TIME_S);
418 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
419 ThermalLevel level = thermalMgrClient.GetThermalLevel();
420 int32_t levelValue = static_cast<int32_t>(level);
421 THERMAL_HILOGD(LABEL_TEST, "levelValue: %{public}d", levelValue);
422 EXPECT_EQ(true, level == ThermalLevel::WARM) << "ThermalMgrInterfaceTest009 Failed";
423 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest009 end.");
424 }
425
426 /**
427 * @tc.name: ThermalMgrInterfaceTest010
428 * @tc.desc: test get level
429 * @tc.type: FUNC
430 */
431 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest010, TestSize.Level0)
432 {
433 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest010 start.");
434 char batteryTempBuf[MAX_PATH] = {0};
435 int32_t temp = 46100;
436 int32_t ret = -1;
437 std::string sTemp = to_string(temp) + "\n";
438 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
439 EXPECT_EQ(true, ret >= EOK);
440 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
441 EXPECT_EQ(true, ret == ERR_OK);
442
443 sleep(10 * SLEEP_WAIT_TIME_S);
444 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
445 ThermalLevel level = thermalMgrClient.GetThermalLevel();
446 int32_t levelValue = static_cast<int32_t>(level);
447 THERMAL_HILOGD(LABEL_TEST, "levelValue: %{public}d", levelValue);
448 EXPECT_EQ(true, level == ThermalLevel::HOT) << "ThermalMgrInterfaceTest010 Failed";
449 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest010 end.");
450 }
451
452 /**
453 * @tc.name: ThermalMgrInterfaceTest011
454 * @tc.desc: test get level
455 * @tc.type: FUNC
456 */
457 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest011, TestSize.Level0)
458 {
459 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest011 start.");
460 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
461 char paTempBuf[MAX_PATH] = {0};
462 char amTempBuf[MAX_PATH] = {0};
463 int32_t ret = -1;
464 ret = snprintf_s(paTempBuf, MAX_PATH, sizeof(paTempBuf) - 1, paPath.c_str());
465 EXPECT_EQ(true, ret >= EOK);
466 ret = snprintf_s(amTempBuf, MAX_PATH, sizeof(amTempBuf) - 1, ambientPath.c_str());
467 EXPECT_EQ(true, ret >= EOK);
468 int32_t paTemp = 44100;
469 int32_t amTemp = 20000;
470
471 std::string sTemp = to_string(paTemp) + "\n";
472 ret = ThermalMgrInterfaceTest::WriteFile(paTempBuf, sTemp, sTemp.length());
473 EXPECT_EQ(true, ret == ERR_OK);
474 sTemp = to_string(amTemp) + "\n";
475 ret = ThermalMgrInterfaceTest::WriteFile(amTempBuf, sTemp, sTemp.length());
476 EXPECT_EQ(true, ret == ERR_OK);
477
478 sleep(10 * SLEEP_WAIT_TIME_S);
479 ThermalLevel level = thermalMgrClient.GetThermalLevel();
480 int32_t levelValue = static_cast<int32_t>(level);
481 THERMAL_HILOGD(LABEL_TEST, "levelValue: %{public}d", levelValue);
482 EXPECT_EQ(true, level == ThermalLevel::HOT) << "ThermalMgrInterfaceTest011 Failed";
483 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest011 end.");
484 }
485
486 /**
487 * @tc.name: ThermalMgrInterfaceTest012
488 * @tc.desc: Get Thermal Level
489 * @tc.type: FUNC
490 * @tc.result: level get min
491 */
492 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest012, TestSize.Level0)
493 {
494 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest012 start.");
495 char apTempBuf[MAX_PATH] = {0};
496 char amTempBuf[MAX_PATH] = {0};
497 char shellTempBuf[MAX_PATH] = {0};
498 int32_t ret = -1;
499 ret = snprintf_s(apTempBuf, MAX_PATH, sizeof(apTempBuf) - 1, apPath.c_str());
500 EXPECT_EQ(true, ret >= EOK);
501 ret = snprintf_s(amTempBuf, MAX_PATH, sizeof(amTempBuf) - 1, ambientPath.c_str());
502 EXPECT_EQ(true, ret >= EOK);
503 ret = snprintf_s(shellTempBuf, MAX_PATH, sizeof(shellTempBuf) - 1, shellPath.c_str());
504 EXPECT_EQ(true, ret >= EOK);
505 int32_t apTemp = 79000;
506 int32_t amTemp = 60000;
507 int32_t shellTemp = 50000;
508 std::string sTemp = to_string(apTemp) + "\n";
509 ret = ThermalMgrInterfaceTest::WriteFile(apTempBuf, sTemp, sTemp.length());
510 EXPECT_EQ(true, ret == ERR_OK);
511 sTemp = to_string(amTemp) + "\n";
512 ret = ThermalMgrInterfaceTest::WriteFile(amTempBuf, sTemp, sTemp.length());
513 EXPECT_EQ(true, ret == ERR_OK);
514 sTemp = to_string(shellTemp) + "\n";
515 ret = ThermalMgrInterfaceTest::WriteFile(shellTempBuf, sTemp, sTemp.length());
516 EXPECT_EQ(true, ret == ERR_OK);
517
518 sleep(10 * SLEEP_WAIT_TIME_S);
519 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
520 ThermalLevel level = thermalMgrClient.GetThermalLevel();
521 int32_t levelValue = static_cast<int32_t>(level);
522 THERMAL_HILOGD(LABEL_TEST, "levelValue: %{public}d", levelValue);
523 EXPECT_EQ(true, level == ThermalLevel::HOT) << "ThermalMgrInterfaceTest012 Failed";
524 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest012 end.");
525 }
526
527 /**
528 * @tc.name: ThermalMgrInterfaceTest0013
529 * @tc.desc: test get level
530 * @tc.type: FUNC
531 */
532 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest0013, TestSize.Level0)
533 {
534 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest0013 start.");
535 char batteryTempBuf[MAX_PATH] = {0};
536 int32_t temp = -19100;
537 int32_t ret = -1;
538 std::string sTemp = to_string(temp) + "\n";
539 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
540 EXPECT_EQ(true, ret >= EOK);
541 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
542 EXPECT_EQ(true, ret == ERR_OK);
543
544 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
545 sleep(10 * SLEEP_WAIT_TIME_S);
546 ThermalLevel level = thermalMgrClient.GetThermalLevel();
547 int32_t levelValue = static_cast<int32_t>(level);
548 THERMAL_HILOGD(LABEL_TEST, "levelValue: %{public}d", levelValue);
549 EXPECT_EQ(true, level == ThermalLevel::COOL) << "ThermalMgrInterfaceTest0013 Failed";
550 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest0013 end.");
551 }
552
553 /**
554 * @tc.name: ThermalMgrInterfaceTest014
555 * @tc.desc: test get level
556 * @tc.type: FUNC
557 */
558 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest014, TestSize.Level0)
559 {
560 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest014 start.");
561 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
562 char paTempBuf[MAX_PATH] = {0};
563 char amTempBuf[MAX_PATH] = {0};
564 int32_t ret = -1;
565 ret = snprintf_s(paTempBuf, MAX_PATH, sizeof(paTempBuf) - 1, paPath.c_str());
566 EXPECT_EQ(true, ret >= EOK);
567 ret = snprintf_s(amTempBuf, MAX_PATH, sizeof(amTempBuf) - 1, ambientPath.c_str());
568 EXPECT_EQ(true, ret >= EOK);
569 int32_t paTemp = 40100;
570 int32_t amTemp = 20000;
571
572 std::string sTemp = to_string(paTemp) + "\n";
573 ret = ThermalMgrInterfaceTest::WriteFile(paTempBuf, sTemp, sTemp.length());
574 EXPECT_EQ(true, ret == ERR_OK);
575 sTemp = to_string(amTemp) + "\n";
576 ret = ThermalMgrInterfaceTest::WriteFile(amTempBuf, sTemp, sTemp.length());
577 EXPECT_EQ(true, ret == ERR_OK);
578
579 sleep(10 * SLEEP_WAIT_TIME_S);
580 ThermalLevel level = thermalMgrClient.GetThermalLevel();
581 int32_t levelValue = static_cast<int32_t>(level);
582 THERMAL_HILOGD(LABEL_TEST, "levelValue: %{public}d", levelValue);
583 EXPECT_EQ(true, level == ThermalLevel::COOL) << "ThermalMgrInterfaceTest014 Failed";
584 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest014 end.");
585 }
586
587 /**
588 * @tc.name: ThermalMgrInterfaceTest015
589 * @tc.desc: test register callback and get thermal level
590 * @tc.type: FUNC
591 */
592 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest015, TestSize.Level0)
593 {
594 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest015 start.");
595 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
596 int32_t ret = -1;
597 char batteryTempBuf[MAX_PATH] = {0};
598 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
599 EXPECT_EQ(true, ret >= EOK);
600
601 const sptr<IThermalLevelCallback> cb1 = new ThermalLevelTest1Callback();
602 thermalMgrClient.SubscribeThermalLevelCallback(cb1);
603
604 int32_t temp = -20000;
605 std::string sTemp = to_string(temp) + "\n";
606 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
607 EXPECT_EQ(true, ret == ERR_OK);
608 sleep(10 * SLEEP_WAIT_TIME_S);
609
610 temp = 40100;
611 sTemp = to_string(temp) + "\n";
612 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
613 EXPECT_EQ(true, ret == ERR_OK);
614 sleep(10 * SLEEP_WAIT_TIME_S);
615
616 temp = -10000;
617 sTemp = to_string(temp) + "\n";
618 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
619 EXPECT_EQ(true, ret == ERR_OK);
620 sleep(10 * SLEEP_WAIT_TIME_S);
621
622 temp = 46000;
623 sTemp = to_string(temp) + "\n";
624 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
625 EXPECT_EQ(true, ret == ERR_OK);
626 sleep(10 * SLEEP_WAIT_TIME_S);
627 thermalMgrClient.UnSubscribeThermalLevelCallback(cb1);
628 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest015 end.");
629 }
630
631 /**
632 * @tc.name: ThermalMgrInterfaceTest016
633 * @tc.desc: test register callback and get thermal level
634 * @tc.type: FUNC
635 */
636 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest016, TestSize.Level0)
637 {
638 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest016 start.");
639 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
640 const sptr<IThermalLevelCallback> cb2 = new ThermalLevelTest2Callback();
641 thermalMgrClient.SubscribeThermalLevelCallback(cb2);
642
643 int32_t ret = -1;
644 char batteryTempBuf[MAX_PATH] = {0};
645 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
646 EXPECT_EQ(true, ret >= EOK);
647
648 int32_t temp = -20000;
649 for (uint32_t i = 0; i < 10; i++) {
650 THERMAL_HILOGD(LABEL_TEST, "change temp.");
651 std::string sTemp = to_string(temp) + "\n";
652 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
653 EXPECT_EQ(true, ret == ERR_OK);
654 sleep(10 * SLEEP_WAIT_TIME_S);
655 temp += 10000;
656 }
657 thermalMgrClient.UnSubscribeThermalLevelCallback(cb2);
658 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest016 end.");
659 }
660
661 /**
662 * @tc.name: ThermalMgrInterfaceTest017
663 * @tc.desc: test register callback and get thermal level
664 * @tc.type: FUNC
665 */
666 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest017, TestSize.Level0)
667 {
668 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest017 start.");
669 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
670 const sptr<IThermalLevelCallback> cb1 = new ThermalLevelTest1Callback();
671 const sptr<IThermalLevelCallback> cb2 = new ThermalLevelTest2Callback();
672 thermalMgrClient.SubscribeThermalLevelCallback(cb1);
673 thermalMgrClient.SubscribeThermalLevelCallback(cb2);
674
675 int32_t ret = -1;
676 char batteryTempBuf[MAX_PATH] = {0};
677 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
678 EXPECT_EQ(true, ret >= EOK);
679 int32_t temp = -20000;
680 std::string sTemp = to_string(temp) + "\n";
681 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
682 EXPECT_EQ(true, ret == ERR_OK);
683 sleep(10 * SLEEP_WAIT_TIME_S);
684
685 thermalMgrClient.UnSubscribeThermalLevelCallback(cb2);
686 thermalMgrClient.UnSubscribeThermalLevelCallback(cb1);
687 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest017 end.");
688 }
689
690 /**
691 * @tc.name: ThermalMgrInterfaceTest018
692 * @tc.desc: test register callback and get thermal level
693 * @tc.type: FUNC
694 */
695 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest018, TestSize.Level0)
696 {
697 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest018 start.");
698 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
699 const sptr<IThermalLevelCallback> cb1 = new ThermalLevelTest1Callback();
700 thermalMgrClient.SubscribeThermalLevelCallback(cb1);
701
702 int32_t ret = -1;
703 char batteryTempBuf[MAX_PATH] = {0};
704 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
705 EXPECT_EQ(true, ret >= EOK);
706 int32_t temp = -20000;
707 std::string sTemp = to_string(temp) + "\n";
708 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
709 EXPECT_EQ(true, ret == ERR_OK);
710 sleep(10 * SLEEP_WAIT_TIME_S);
711 thermalMgrClient.UnSubscribeThermalLevelCallback(cb1);
712 sleep(SLEEP_WAIT_TIME_S);
713 thermalMgrClient.SubscribeThermalLevelCallback(cb1);
714 temp = 48000;
715 sTemp = to_string(temp) + "\n";
716 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
717 EXPECT_EQ(true, ret == ERR_OK);
718 sleep(10 * SLEEP_WAIT_TIME_S);
719 thermalMgrClient.UnSubscribeThermalLevelCallback(cb1);
720 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest020 end.");
721 }
722
723 /**
724 * @tc.name: ThermalMgrInterfaceTest021
725 * @tc.desc: test get invaild temp
726 * @tc.type: FUNC
727 */
728 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest021, TestSize.Level0)
729 {
730 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest021 start.");
731 int32_t ret = -1;
732 char batteryTempBuf[MAX_PATH] = {0};
733 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
734 EXPECT_EQ(true, ret >= EOK);
735 int32_t temp = INVAILD_TEMP;
736 std::string sTemp = to_string(temp) + "\n";
737 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
738 EXPECT_EQ(true, ret == ERR_OK);
739
740 sleep(10 * SLEEP_WAIT_TIME_S);
741 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
742 int32_t out = thermalMgrClient.GetThermalSensorTemp(SensorType::BATTERY);
743 THERMAL_HILOGD(LABEL_TEST, "battry temp: %{public}d", out);
744 EXPECT_EQ(true, temp == out) << "ThermalMgrInterfaceTest021 Failed";
745 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest021 end.");
746 }
747
748 /**
749 * @tc.name: ThermalMgrInterfaceTest022
750 * @tc.desc: test get invaild temp
751 * @tc.type: FUNC
752 */
753 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest022, TestSize.Level0)
754 {
755 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest022 start.");
756 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
757 const sptr<IThermalTempCallback> cb = nullptr;
758 InitData();
759 thermalMgrClient.SubscribeThermalTempCallback(typelist, cb);
760
761 int32_t ret = -1;
762 char batteryTempBuf[MAX_PATH] = {0};
763 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
764 EXPECT_EQ(true, ret >= EOK);
765 int32_t temp = INVAILD_TEMP;
766 std::string sTemp = to_string(temp) + "\n";
767 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
768 EXPECT_EQ(true, ret == ERR_OK);
769
770 sleep(10 * SLEEP_WAIT_TIME_S);
771 thermalMgrClient.UnSubscribeThermalTempCallback(cb);
772 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest022 end.");
773 }
774
775 /**
776 * @tc.name: ThermalMgrInterfaceTest023
777 * @tc.desc: test get level
778 * @tc.type: FUNC
779 */
780 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest023, TestSize.Level0)
781 {
782 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest023 start.");
783 int32_t ret = -1;
784 char batteryTempBuf[MAX_PATH] = {0};
785 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
786 EXPECT_EQ(true, ret >= EOK);
787 int32_t temp = -1000;
788 std::string sTemp = to_string(temp) + "\n";
789 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
790 EXPECT_EQ(true, ret == ERR_OK);
791
792 sleep(10 * SLEEP_WAIT_TIME_S);
793 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
794 ThermalLevel level = thermalMgrClient.GetThermalLevel();
795 int32_t levelValue = static_cast<int32_t>(level);
796 THERMAL_HILOGD(LABEL_TEST, "levelValue: %{public}d", levelValue);
797 EXPECT_EQ(true, level == ThermalLevel::OVERHEATED) << "ThermalMgrInterfaceTest023 Failed";
798 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest023 end.");
799 }
800
801 /**
802 * @tc.name: ThermalMgrInterfaceTest024
803 * @tc.desc: test register null callback
804 * @tc.type: FUNC
805 */
806 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest024, TestSize.Level0)
807 {
808 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest024 start.");
809 auto& thermalMgrClient = ThermalMgrClient::GetInstance();
810 const sptr<IThermalLevelCallback> cb = nullptr;
811 thermalMgrClient.SubscribeThermalLevelCallback(cb);
812 int32_t ret = -1;
813 char batteryTempBuf[MAX_PATH] = {0};
814 ret = snprintf_s(batteryTempBuf, MAX_PATH, sizeof(batteryTempBuf) - 1, batteryPath.c_str());
815 EXPECT_EQ(true, ret >= EOK);
816 int32_t temp = INVAILD_TEMP;
817 std::string sTemp = to_string(temp) + "\n";
818 ret = ThermalMgrInterfaceTest::WriteFile(batteryTempBuf, sTemp, sTemp.length());
819 EXPECT_EQ(true, ret == ERR_OK);
820
821 sleep(10 * SLEEP_WAIT_TIME_S);
822 thermalMgrClient.UnSubscribeThermalLevelCallback(cb);
823 THERMAL_HILOGD(LABEL_TEST, "ThermalMgrInterfaceTest024 end.");
824 }
825 } // namespace