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_mgr_interface_test.h"
17
18 #include <atomic>
19 #include <condition_variable>
20 #include <mutex>
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> typelist;
44 std::condition_variable g_callbackCV;
45 std::mutex g_mutex;
46 constexpr int64_t TIME_OUT = 3;
47 std::atomic_bool g_callbackTriggered = false;
48 std::atomic_bool g_levelCallBack4 = false;
49 std::atomic_bool g_levelCallBack5 = 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 auto& g_thermalMgrClient = ThermalMgrClient::GetInstance();
53
Notify()54 void Notify()
55 {
56 g_callbackTriggered = true;
57 g_callbackCV.notify_one();
58 }
59
Wait()60 void Wait()
61 {
62 std::unique_lock<std::mutex> lock(g_mutex);
63 g_callbackCV.wait_for(lock, std::chrono::seconds(TIME_OUT), [] {
64 return g_callbackTriggered.load();
65 });
66 EXPECT_TRUE(g_callbackTriggered);
67 g_callbackTriggered = false;
68 }
69 } // namespace
70
TearDown()71 void ThermalMgrInterfaceTest::TearDown()
72 {
73 g_service->SetScene("");
74 HdfThermalCallbackInfo event;
75 ThermalZoneInfo info1;
76 info1.type = "battery";
77 info1.temp = 0;
78 event.info.push_back(info1);
79 info1.type = "ap";
80 event.info.push_back(info1);
81 info1.type = "pa";
82 event.info.push_back(info1);
83 info1.type = "shell";
84 event.info.push_back(info1);
85 info1.type = "ambient";
86 event.info.push_back(info1);
87 g_service->HandleThermalCallbackEvent(event);
88 g_callbackTriggered = false;
89 }
90
SetUpTestCase()91 void ThermalMgrInterfaceTest::SetUpTestCase()
92 {
93 g_service = ThermalService::GetInstance();
94 g_service->InitSystemTestModules();
95 g_service->OnStart();
96 g_service->GetBaseinfoObj()->Init();
97 g_service->GetObserver()->InitSensorTypeMap();
98 }
99
TearDownTestCase()100 void ThermalMgrInterfaceTest::TearDownTestCase()
101 {
102 g_service->OnStop();
103 }
104
InitData()105 void ThermalMgrInterfaceTest::InitData()
106 {
107 typelist.push_back(BATTERY);
108 typelist.push_back(SOC);
109 }
110
OnThermalTempChanged(TempCallbackMap & tempCbMap)111 bool ThermalMgrInterfaceTest::ThermalTempTest1Callback::OnThermalTempChanged(TempCallbackMap& tempCbMap)
112 {
113 int assertValue = 0;
114 for (auto iter : tempCbMap) {
115 THERMAL_HILOGI(LABEL_TEST, "type: %{public}s, temp: %{public}d", iter.first.c_str(), iter.second);
116 EXPECT_EQ(true, iter.second >= assertValue) << "Test Failed";
117 }
118 Notify();
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_HILOGI(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 Notify();
130 return true;
131 }
132
OnThermalLevelChanged(ThermalLevel level)133 bool ThermalMgrInterfaceTest::ThermalLevelTest1Callback::OnThermalLevelChanged(ThermalLevel level)
134 {
135 int assertMin = -1;
136 int assertMax = 7;
137 int32_t levelValue = static_cast<int32_t>(level);
138 THERMAL_HILOGI(LABEL_TEST, "level: %{public}d", levelValue);
139 EXPECT_EQ(true, levelValue >= assertMin && levelValue <= assertMax) << "Test Failed";
140 Notify();
141 return true;
142 }
143
OnThermalLevelChanged(ThermalLevel level)144 bool ThermalMgrInterfaceTest::ThermalLevelTest2Callback::OnThermalLevelChanged(ThermalLevel level)
145 {
146 int assertMin = -1;
147 int assertMax = 7;
148 int32_t levelValue = static_cast<int32_t>(level);
149 THERMAL_HILOGI(LABEL_TEST, "level: %{public}d", levelValue);
150 EXPECT_EQ(true, levelValue >= assertMin && levelValue <= assertMax) << "Test Failed";
151 Notify();
152 return true;
153 }
154
OnThermalLevelChanged(ThermalLevel level)155 bool ThermalMgrInterfaceTest::ThermalLevelTest3Callback::OnThermalLevelChanged(ThermalLevel level)
156 {
157 int assertMin = -1;
158 int assertMax = 7;
159 int32_t levelValue = static_cast<int32_t>(level);
160 THERMAL_HILOGI(LABEL_TEST, "level: %{public}d", levelValue);
161 EXPECT_EQ(true, levelValue >= assertMin && levelValue <= assertMax) << "Test Failed";
162 Notify();
163 return true;
164 }
165
OnThermalLevelChanged(ThermalLevel level)166 bool ThermalMgrInterfaceTest::ThermalLevelTest4Callback::OnThermalLevelChanged(ThermalLevel level)
167 {
168 int assertMin = -1;
169 int assertMax = 7;
170 int32_t levelValue = static_cast<int32_t>(level);
171 THERMAL_HILOGI(LABEL_TEST, "level: %{public}d", levelValue);
172 EXPECT_EQ(true, levelValue >= assertMin && levelValue <= assertMax) << "Test Failed";
173 g_levelCallBack4 = true;
174 g_callbackCV.notify_one();
175 return true;
176 }
177
OnThermalLevelChanged(ThermalLevel level)178 bool ThermalMgrInterfaceTest::ThermalLevelTest5Callback::OnThermalLevelChanged(ThermalLevel level)
179 {
180 int assertMin = -1;
181 int assertMax = 7;
182 int32_t levelValue = static_cast<int32_t>(level);
183 THERMAL_HILOGI(LABEL_TEST, "level: %{public}d", levelValue);
184 EXPECT_EQ(true, levelValue >= assertMin && levelValue <= assertMax) << "Test Failed";
185 g_levelCallBack5 = true;
186 g_callbackCV.notify_one();
187 return true;
188 }
189
190 namespace {
191 /**
192 * @tc.name: ThermalMgrInterfaceTest001
193 * @tc.desc: test get sensor temp
194 * @tc.type: FUNC
195 */
196 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest001, TestSize.Level0)
197 {
198 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest001 function start!");
199 sleep(1);
200 HdfThermalCallbackInfo event;
201 ThermalZoneInfo info1;
202 info1.type = "battery";
203 info1.temp = 41000;
204 event.info.push_back(info1);
205 g_service->HandleThermalCallbackEvent(event);
206 ThermalSrvSensorInfo info;
207 bool thermalInfoRet = false;
208 g_service->GetThermalSrvSensorInfo(static_cast<int32_t>(SensorType::BATTERY), info, thermalInfoRet);
209 g_thermalMgrClient.GetThermalSensorTemp(SensorType::BATTERY);
210 EXPECT_EQ(info1.temp, info.GetTemp()) << "ThermalMgrInterfaceTest001 Failed";
211 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest001 function end!");
212 }
213
214 /**
215 * @tc.name: ThermalMgrInterfaceTest002
216 * @tc.desc: test get sensor temp
217 * @tc.type: FUNC
218 */
219 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest002, TestSize.Level0)
220 {
221 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest002 function start!");
222 HdfThermalCallbackInfo event;
223 ThermalZoneInfo info1;
224 info1.type = "soc";
225 info1.temp = 10000;
226 event.info.push_back(info1);
227 g_service->HandleThermalCallbackEvent(event);
228 ThermalSrvSensorInfo info;
229 bool thermalInfoRet = false;
230 g_service->GetThermalSrvSensorInfo(static_cast<int32_t>(SensorType::SOC), info, thermalInfoRet);
231 g_thermalMgrClient.GetThermalSensorTemp(SensorType::SOC);
232 EXPECT_EQ(info1.temp, info.GetTemp()) << "ThermalMgrInterfaceTest002 Failed";
233 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest002 function end!");
234 }
235
236 /**
237 * @tc.name: ThermalMgrInterfaceTest003
238 * @tc.desc: test get sensor temp
239 * @tc.type: FUNC
240 */
241 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest003, TestSize.Level0)
242 {
243 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest003 function start!");
244 HdfThermalCallbackInfo event;
245 ThermalZoneInfo info1;
246 info1.type = "shell";
247 info1.temp = 11000;
248 event.info.push_back(info1);
249 g_service->HandleThermalCallbackEvent(event);
250 ThermalSrvSensorInfo info;
251 bool thermalInfoRet = false;
252 g_service->GetThermalSrvSensorInfo(static_cast<int32_t>(SensorType::SHELL), info, thermalInfoRet);
253 g_thermalMgrClient.GetThermalSensorTemp(SensorType::SHELL);
254 EXPECT_EQ(info1.temp, info.GetTemp()) << "ThermalMgrInterfaceTest003 Failed";
255 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest003 function end!");
256 }
257
258 /**
259 * @tc.name: ThermalMgrInterfaceTest004
260 * @tc.desc: test get sensor temp
261 * @tc.type: FUNC
262 */
263 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest004, TestSize.Level0)
264 {
265 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest004 function start!");
266 HdfThermalCallbackInfo event;
267 ThermalZoneInfo info1;
268 info1.type = "cpu";
269 info1.temp = 12000;
270 event.info.push_back(info1);
271 g_service->HandleThermalCallbackEvent(event);
272 ThermalSrvSensorInfo info;
273 bool thermalInfoRet = false;
274 g_service->GetThermalSrvSensorInfo(static_cast<int32_t>(SensorType::SENSOR1), info, thermalInfoRet);
275 g_thermalMgrClient.GetThermalSensorTemp(SensorType::SENSOR1);
276 EXPECT_EQ(info1.temp, info.GetTemp()) << "ThermalMgrInterfaceTest004 Failed";
277 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest004 function end!");
278 }
279
280 /**
281 * @tc.name: ThermalMgrInterfaceTest005
282 * @tc.desc: test get sensor temp
283 * @tc.type: FUNC
284 */
285 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest005, TestSize.Level0)
286 {
287 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest005 function start!");
288 HdfThermalCallbackInfo event;
289 ThermalZoneInfo info1;
290 info1.type = "charger";
291 info1.temp = 13000;
292 event.info.push_back(info1);
293 g_service->HandleThermalCallbackEvent(event);
294 ThermalSrvSensorInfo info;
295 bool thermalInfoRet = false;
296 g_service->GetThermalSrvSensorInfo(static_cast<int32_t>(SensorType::SENSOR2), info, thermalInfoRet);
297 g_thermalMgrClient.GetThermalSensorTemp(SensorType::SENSOR2);
298 EXPECT_EQ(info1.temp, info.GetTemp()) << "ThermalMgrInterfaceTest005 Failed";
299 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest005 function end!");
300 }
301
302 /**
303 * @tc.name: ThermalMgrInterfaceTest006
304 * @tc.desc: register callback and get temp list
305 * @tc.type: FUNC
306 */
307 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest006, TestSize.Level0)
308 {
309 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest006 function start!");
310 InitData();
311 const sptr<IThermalTempCallback> cb1 = new ThermalTempTest1Callback();
312 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest006 start register");
313 g_service->SubscribeThermalTempCallback(typelist, cb1);
314 g_thermalMgrClient.SubscribeThermalTempCallback(typelist, cb1);
315 int32_t temp = 10000;
316 for (int i = 0; i < 10; i++) {
317 HdfThermalCallbackInfo event;
318 ThermalZoneInfo info1;
319 info1.type = "soc";
320 info1.temp = temp;
321 event.info.push_back(info1);
322
323 ThermalZoneInfo info2;
324 info2.type = "battery";
325 info2.temp = temp;
326 event.info.push_back(info2);
327 g_service->HandleThermalCallbackEvent(event);
328 Wait();
329 }
330 g_service->UnSubscribeThermalTempCallback(cb1);
331 g_thermalMgrClient.UnSubscribeThermalTempCallback(cb1);
332 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest006 function end!");
333 }
334
335 /**
336 * @tc.name: ThermalMgrInterfaceTest007
337 * @tc.desc: test register callback and get thermal level
338 * @tc.type: FUNC
339 */
340 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest007, TestSize.Level0)
341 {
342 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest007 function start!");
343 const sptr<IThermalLevelCallback> cb4 = new ThermalLevelTest4Callback();
344 const sptr<IThermalLevelCallback> cb5 = new ThermalLevelTest5Callback();
345 g_thermalMgrClient.SubscribeThermalLevelCallback(cb4);
346 g_service->SubscribeThermalLevelCallback(cb4);
347 g_thermalMgrClient.SubscribeThermalLevelCallback(cb5);
348 g_service->SubscribeThermalLevelCallback(cb5);
349 // thermal Level callback will be triggered when subscribed
350 std::unique_lock<std::mutex> lock(g_mutex);
__anon6f7ab4d90402null351 g_callbackCV.wait_for(lock, std::chrono::seconds(TIME_OUT), [] {
352 return (g_levelCallBack4.load() && g_levelCallBack5.load());
353 });
354 EXPECT_TRUE(g_levelCallBack4);
355 EXPECT_TRUE(g_levelCallBack5);
356 g_levelCallBack4 = false;
357 g_levelCallBack5 = false;
358
359 HdfThermalCallbackInfo event;
360 ThermalZoneInfo info1;
361 info1.type = "battery";
362 info1.temp = -20000;
363 event.info.push_back(info1);
364 g_service->HandleThermalCallbackEvent(event);
365 EXPECT_FALSE(g_levelCallBack4);
366 EXPECT_FALSE(g_levelCallBack5);
367
368 g_thermalMgrClient.UnSubscribeThermalLevelCallback(cb5);
369 g_service->UnSubscribeThermalLevelCallback(cb5);
370 g_thermalMgrClient.UnSubscribeThermalLevelCallback(cb4);
371 g_service->UnSubscribeThermalLevelCallback(cb4);
372 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest007 function end!");
373 }
374
375 /**
376 * @tc.name: ThermalMgrInterfaceTest008
377 * @tc.desc: test register callback and get thermal level
378 * @tc.type: FUNC
379 */
380 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest008, TestSize.Level0)
381 {
382 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest008 function start!");
383 const sptr<IThermalLevelCallback> cb2 = new ThermalLevelTest2Callback();
384 g_thermalMgrClient.SubscribeThermalLevelCallback(cb2);
385 g_service->SubscribeThermalLevelCallback(cb2);
386 Wait(); // thermal Level callback will be triggered when subscribed
387
388 int32_t temp = -25100;
389 for (uint32_t i = 0; i < 5; i++) {
390 THERMAL_HILOGI(LABEL_TEST, "temp: %{public}d", temp);
391 HdfThermalCallbackInfo event;
392 ThermalZoneInfo info1;
393 info1.type = "battery";
394 info1.temp = temp;
395 event.info.push_back(info1);
396 g_service->HandleThermalCallbackEvent(event);
397 temp += 5000;
398 EXPECT_FALSE(g_callbackTriggered);
399 }
400 temp = 40100;
401 for (uint32_t i = 0; i < 3; i++) {
402 HdfThermalCallbackInfo event;
403 ThermalZoneInfo info1;
404 info1.type = "battery";
405 info1.temp = temp;
406 event.info.push_back(info1);
407 g_service->HandleThermalCallbackEvent(event);
408 temp += 3000;
409 Wait();
410 }
411 g_thermalMgrClient.UnSubscribeThermalLevelCallback(cb2);
412 g_service->SubscribeThermalLevelCallback(cb2);
413 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest008 function end!");
414 }
415
416 /**
417 * @tc.name: ThermalMgrInterfaceTest009
418 * @tc.desc: test register callback and get thermal level
419 * @tc.type: FUNC
420 */
421 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest009, TestSize.Level0)
422 {
423 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest009 function start!");
424 const sptr<IThermalLevelCallback> cb1 = new ThermalLevelTest1Callback();
425 g_thermalMgrClient.SubscribeThermalLevelCallback(cb1);
426 g_service->SubscribeThermalLevelCallback(cb1);
427 Wait(); // thermal Level callback will be triggered when subscribed
428
429 HdfThermalCallbackInfo event;
430 ThermalZoneInfo info1;
431 info1.type = "battery";
432 info1.temp = -20000;
433 event.info.push_back(info1);
434 g_service->HandleThermalCallbackEvent(event);
435 EXPECT_FALSE(g_callbackTriggered);
436 event.info.clear();
437
438 info1.temp = 40100;
439 event.info.push_back(info1);
440 g_service->HandleThermalCallbackEvent(event);
441 Wait();
442 event.info.clear();
443
444 info1.temp = -10000;
445 event.info.push_back(info1);
446 g_service->HandleThermalCallbackEvent(event);
447 Wait();
448 event.info.clear();
449
450 info1.temp = 46000;
451 event.info.push_back(info1);
452 g_service->HandleThermalCallbackEvent(event);
453 Wait();
454
455 g_thermalMgrClient.UnSubscribeThermalLevelCallback(cb1);
456 g_service->UnSubscribeThermalLevelCallback(cb1);
457 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest009 function end!");
458 }
459
460 /**
461 * @tc.name: ThermalMgrInterfaceTest010
462 * @tc.desc: test register callback and get thermal level
463 * @tc.type: FUNC
464 */
465 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest010, TestSize.Level0)
466 {
467 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest010 function start!");
468 const sptr<IThermalLevelCallback> cb1 = new ThermalLevelTest1Callback();
469 g_thermalMgrClient.SubscribeThermalLevelCallback(cb1);
470 g_service->SubscribeThermalLevelCallback(cb1);
471 Wait(); // thermal Level callback will be triggered when subscribed
472
473 HdfThermalCallbackInfo event;
474 ThermalZoneInfo info1;
475 info1.type = "battery";
476 info1.temp = -20000;
477 event.info.push_back(info1);
478 g_service->HandleThermalCallbackEvent(event);
479 EXPECT_FALSE(g_callbackTriggered);
480 g_thermalMgrClient.UnSubscribeThermalLevelCallback(cb1);
481 g_service->UnSubscribeThermalLevelCallback(cb1);
482
483 g_service->HandleThermalCallbackEvent(event);
484 g_thermalMgrClient.SubscribeThermalLevelCallback(cb1);
485 g_service->SubscribeThermalLevelCallback(cb1);
486 Wait(); // thermal Level callback will be triggered when subscribed
487
488 event.info.clear();
489 info1.temp = 48000;
490 event.info.push_back(info1);
491 g_service->HandleThermalCallbackEvent(event);
492 Wait();
493
494 g_thermalMgrClient.UnSubscribeThermalLevelCallback(cb1);
495 g_service->UnSubscribeThermalLevelCallback(cb1);
496 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest020 function end!");
497 }
498
499 /**
500 * @tc.name: ThermalMgrInterfaceTest011
501 * @tc.desc: register callback and get temp list
502 * @tc.type: FUNC
503 */
504 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest011, TestSize.Level0)
505 {
506 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest011 function start!");
507 InitData();
508 const sptr<IThermalTempCallback> cb1 = new ThermalTempTest1Callback();
509 g_service->SubscribeThermalTempCallback(typelist, cb1);
510 g_thermalMgrClient.SubscribeThermalTempCallback(typelist, cb1);
511 int32_t temp = 10000;
512 for (int i = 0; i < 10; i++) {
513 HdfThermalCallbackInfo event;
514 ThermalZoneInfo info1;
515 info1.type = "soc";
516 info1.temp = temp;
517 event.info.push_back(info1);
518
519 ThermalZoneInfo info2;
520 info2.type = "battery";
521 info2.temp = temp;
522 event.info.push_back(info2);
523 g_service->HandleThermalCallbackEvent(event);
524 temp += 100;
525 Wait();
526 }
527 g_service->UnSubscribeThermalTempCallback(cb1);
528 g_thermalMgrClient.UnSubscribeThermalTempCallback(cb1);
529 const sptr<IThermalTempCallback> cb2 = new ThermalTempTest2Callback();
530 g_service->SubscribeThermalTempCallback(typelist, cb1);
531 g_thermalMgrClient.SubscribeThermalTempCallback(typelist, cb1);
532 for (int i = 0; i < 10; i++) {
533 HdfThermalCallbackInfo event;
534 ThermalZoneInfo info1;
535 info1.type = "soc";
536 info1.temp = temp;
537 event.info.push_back(info1);
538
539 ThermalZoneInfo info2;
540 info2.type = "battery";
541 info2.temp = temp;
542 event.info.push_back(info2);
543 g_service->HandleThermalCallbackEvent(event);
544 temp += 100;
545 Wait();
546 }
547 g_service->UnSubscribeThermalTempCallback(cb1);
548 g_thermalMgrClient.UnSubscribeThermalTempCallback(cb1);
549 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest011 function end!");
550 }
551
552 /**
553 * @tc.name: ThermalMgrInterfaceTest012
554 * @tc.desc: test get invaild temp
555 * @tc.type: FUNC
556 */
557 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest012, TestSize.Level0)
558 {
559 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest012 function start!");
560 HdfThermalCallbackInfo event;
561 ThermalZoneInfo info1;
562 info1.type = "battery";
563 info1.temp = INVAILD_TEMP;
564 event.info.push_back(info1);
565 g_service->HandleThermalCallbackEvent(event);
566 g_thermalMgrClient.GetThermalSensorTemp(SensorType::BATTERY);
567 ThermalSrvSensorInfo info;
568 bool thermalInfoRet = false;
569 g_service->GetThermalSrvSensorInfo(static_cast<int32_t>(SensorType::BATTERY), info, thermalInfoRet);
570 EXPECT_EQ(INVAILD_TEMP, info.GetTemp()) << "ThermalMgrInterfaceTest012 Failed";
571 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest012 function end!");
572 }
573
574 /**
575 * @tc.name: ThermalMgrInterfaceTest013
576 * @tc.desc: test get invaild temp
577 * @tc.type: FUNC
578 */
579 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest013, TestSize.Level0)
580 {
581 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest013 function start!");
582 const sptr<IThermalTempCallback> cb = nullptr;
583 InitData();
584 g_thermalMgrClient.SubscribeThermalTempCallback(typelist, cb);
585 g_service->SubscribeThermalTempCallback(typelist, cb);
586 HdfThermalCallbackInfo event;
587 ThermalZoneInfo info1;
588 info1.type = "battery";
589 info1.temp = INVAILD_TEMP;
590 event.info.push_back(info1);
591 g_service->HandleThermalCallbackEvent(event);
592 g_thermalMgrClient.UnSubscribeThermalTempCallback(cb);
593 EXPECT_EQ(g_service->UnSubscribeThermalTempCallback(cb), ERR_OK);
594 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest013 function end!");
595 }
596
597 /**
598 * @tc.name: ThermalMgrInterfaceTest014
599 * @tc.desc: test register null callback
600 * @tc.type: FUNC
601 */
602 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest014, TestSize.Level0)
603 {
604 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest014 function start!");
605 const sptr<IThermalLevelCallback> cb = nullptr;
606 g_thermalMgrClient.SubscribeThermalLevelCallback(cb);
607 g_service->SubscribeThermalLevelCallback(cb);
608 HdfThermalCallbackInfo event;
609 ThermalZoneInfo info1;
610 info1.type = "battery";
611 info1.temp = INVAILD_TEMP;
612 event.info.push_back(info1);
613 g_service->HandleThermalCallbackEvent(event);
614 g_thermalMgrClient.UnSubscribeThermalLevelCallback(cb);
615 EXPECT_EQ(g_service->UnSubscribeThermalLevelCallback(cb), ERR_OK);
616 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest014 function end!");
617 }
618
619 /**
620 * @tc.name: ThermalMgrInterfaceTest015
621 * @tc.desc: test get level
622 * @tc.type: FUNC
623 */
624 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest015, TestSize.Level0)
625 {
626 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest015 function start!");
627 std::vector<int32_t> temps {-1000, 40100, 43100, 46100};
628 std::vector<ThermalLevel> levels {ThermalLevel::COOL, ThermalLevel::NORMAL, ThermalLevel::WARM, ThermalLevel::HOT};
629 for (uint32_t i = 0; i < temps.size(); ++i) {
630 HdfThermalCallbackInfo event;
631 ThermalZoneInfo info1;
632 info1.type = "battery";
633 info1.temp = temps[i];
634 event.info.push_back(info1);
635 g_service->HandleThermalCallbackEvent(event);
636 g_thermalMgrClient.GetThermalLevel();
637 int32_t levelValue;
638 g_service->GetThermalLevel(levelValue);
639 ThermalLevel level = static_cast<ThermalLevel>(levelValue);
640 GTEST_LOG_(INFO) << "test thermal temp: " << temps[i];
641 EXPECT_EQ(level, levels[i]) << "ThermalMgrInterfaceTest015 Failed";
642 }
643 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest015 function end!");
644 }
645
646 /**
647 * @tc.name: ThermalMgrInterfaceTest016
648 * @tc.desc: test get level
649 * @tc.type: FUNC
650 */
651 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest016, TestSize.Level0)
652 {
653 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest016 function start!");
654 HdfThermalCallbackInfo event;
655 ThermalZoneInfo info1;
656 info1.type = "pa";
657 info1.temp = 40100;
658 event.info.push_back(info1);
659 info1.type = "ambient";
660 info1.temp = 20000;
661 event.info.push_back(info1);
662 g_service->HandleThermalCallbackEvent(event);
663 g_thermalMgrClient.GetThermalLevel();
664 int32_t levelValue;
665 g_service->GetThermalLevel(levelValue);
666 ThermalLevel level = static_cast<ThermalLevel>(levelValue);
667 EXPECT_EQ(level, ThermalLevel::OVERHEATED) << "ThermalMgrInterfaceTest016 Failed";
668 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest016 function end!");
669 }
670
671 /**
672 * @tc.name: ThermalMgrInterfaceTest017
673 * @tc.desc: test get level
674 * @tc.type: FUNC
675 */
676 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest017, TestSize.Level0)
677 {
678 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest017 function start!");
679 HdfThermalCallbackInfo event;
680 ThermalZoneInfo info1;
681 info1.type = "pa";
682 info1.temp = 44100;
683 event.info.push_back(info1);
684 info1.type = "ambient";
685 info1.temp = 20000;
686 event.info.push_back(info1);
687 g_service->HandleThermalCallbackEvent(event);
688 g_thermalMgrClient.GetThermalLevel();
689 int32_t levelValue;
690 g_service->GetThermalLevel(levelValue);
691 ThermalLevel level = static_cast<ThermalLevel>(levelValue);
692 EXPECT_EQ(level, ThermalLevel::WARNING) << "ThermalMgrInterfaceTest017 Failed";
693 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest017 function end!");
694 }
695
696 /**
697 * @tc.name: ThermalMgrInterfaceTest018
698 * @tc.desc: Get Thermal Level
699 * @tc.type: FUNC
700 * @tc.result: level get min
701 */
702 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest018, TestSize.Level0)
703 {
704 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest018 function start!");
705 HdfThermalCallbackInfo event;
706 ThermalZoneInfo info1;
707 info1.type = "ap";
708 info1.temp = 79000;
709 event.info.push_back(info1);
710 info1.type = "ambient";
711 info1.temp = 60000;
712 event.info.push_back(info1);
713 info1.type = "shell";
714 info1.temp = 30000;
715 event.info.push_back(info1);
716 g_service->HandleThermalCallbackEvent(event);
717 g_thermalMgrClient.GetThermalLevel();
718 int32_t levelValue;
719 g_service->GetThermalLevel(levelValue);
720 ThermalLevel level = static_cast<ThermalLevel>(levelValue);
721 EXPECT_EQ(level, ThermalLevel::EMERGENCY) << "ThermalMgrInterfaceTest018 Failed";
722 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest018 function end!");
723 }
724
725 /**
726 * @tc.name: ThermalMgrInterfaceTest019
727 * @tc.desc: test get ESCAPE level
728 * @tc.type: FUNC
729 */
730 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest019, TestSize.Level0)
731 {
732 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest019 function start!");
733 HdfThermalCallbackInfo event;
734 ThermalZoneInfo info1;
735 info1.type = "pa";
736 info1.temp = 46100;
737 event.info.push_back(info1);
738 info1.type = "ambient";
739 info1.temp = 20000;
740 event.info.push_back(info1);
741 g_service->HandleThermalCallbackEvent(event);
742 g_thermalMgrClient.GetThermalLevel();
743 int32_t levelValue;
744 g_service->GetThermalLevel(levelValue);
745 ThermalLevel level = static_cast<ThermalLevel>(levelValue);
746 EXPECT_EQ(level, ThermalLevel::ESCAPE) << "ThermalMgrInterfaceTest019 Failed";
747 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest019 function end!");
748 }
749
750 /**
751 * @tc.name: ThermalMgrInterfaceTest020
752 * @tc.desc: Update Thermal State
753 * @tc.type: FUNC
754 * @tc.result: state changed
755 */
756 HWTEST_F(ThermalMgrInterfaceTest, ThermalMgrInterfaceTest020, TestSize.Level0)
757 {
758 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest020 function start!");
759 std::string tag1 = "modeid";
760 std::string val1 = "100";
761 bool ret = g_thermalMgrClient.UpdateThermalState(tag1, val1, false);
762 EXPECT_TRUE(ret == true);
763 g_service->UpdateThermalState(tag1, val1, false);
764 std::map<std::string, std::string> stateMap {{tag1, val1}};
765 bool result = g_service->GetPolicy()->StateMachineDecision(stateMap);
766 EXPECT_TRUE(result == true);
767
768 HdfThermalCallbackInfo event;
769 ThermalZoneInfo info1;
770 info1.type = "battery";
771 info1.temp = 40100;
772 event.info.push_back(info1);
773 g_service->HandleThermalCallbackEvent(event);
774 int32_t value = ConvertInt(GetNodeValue(CONFIG_LEVEL_PATH));
775 EXPECT_TRUE(value == 1);
776 THERMAL_HILOGI(LABEL_TEST, "ThermalMgrInterfaceTest020 function end!");
777 }
778
779 } // namespace
780