1 /*
2 * Copyright (c) 2022-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 <cmath>
17 #include <cstdio>
18 #include <fcntl.h>
19 #include <functional>
20 #include <gtest/gtest.h>
21 #include <mutex>
22 #include <securec.h>
23 #include <unistd.h>
24
25 #include "hdf_base.h"
26 #include "osal_time.h"
27 #include "v1_1/ifan_callback.h"
28 #include "v1_1/ithermal_callback.h"
29 #include "v1_1/ithermal_interface.h"
30 #include "v1_1/thermal_types.h"
31
32 using namespace OHOS::HDI;
33 using namespace OHOS::HDI::Thermal::V1_1;
34 using namespace testing::ext;
35
36 namespace {
37 int g_onFanDataEventCount = 0;
38 int g_onThermalDataEventCount = 0;
39 class ThermalCallbackMock : public IThermalCallback {
40 public:
~ThermalCallbackMock()41 virtual ~ThermalCallbackMock() {}
42 using ThermalEventCallback = std::function<int32_t(const HdfThermalCallbackInfo &event)>;
RegisterThermalEvent(const ThermalEventCallback & eventCb)43 static int32_t RegisterThermalEvent(const ThermalEventCallback &eventCb)
44 {
45 (void)eventCb;
46 return 0;
47 }
OnThermalDataEvent(const HdfThermalCallbackInfo & event)48 int32_t OnThermalDataEvent(const HdfThermalCallbackInfo &event) override
49 {
50 (void)event;
51 g_onThermalDataEventCount++;
52 return 0;
53 }
54 };
55
56 class FanCallbackMock : public IFanCallback {
57 public:
~FanCallbackMock()58 virtual ~FanCallbackMock() {}
59 using FanEventCallback = std::function<int32_t(const HdfThermalCallbackInfo &event)>;
RegisterFanEvent(const FanEventCallback & eventCb)60 static int32_t RegisterFanEvent(const FanEventCallback &eventCb)
61 {
62 (void)eventCb;
63 return 0;
64 }
OnFanDataEvent(const HdfThermalCallbackInfo & event)65 int32_t OnFanDataEvent(const HdfThermalCallbackInfo &event) override
66 {
67 (void)event;
68 g_onFanDataEventCount++;
69 return 0;
70 }
71 };
72
73 sptr<IThermalInterface> g_thermalInterface = nullptr;
74 sptr<IThermalCallback> g_callback = new ThermalCallbackMock();
75 sptr<IFanCallback> g_fanCallback = new FanCallbackMock();
76 std::mutex g_mutex;
77
78 class HdfThermalHdiTestAdditional : public testing::Test {
79 public:
80 static void SetUpTestCase();
81 static void TearDownTestCase();
82 void SetUp();
83 void TearDown();
84 };
85
SetUpTestCase()86 void HdfThermalHdiTestAdditional::SetUpTestCase() { g_thermalInterface = IThermalInterface::Get(true); }
87
TearDownTestCase()88 void HdfThermalHdiTestAdditional::TearDownTestCase() {}
89
SetUp()90 void HdfThermalHdiTestAdditional::SetUp() {}
91
TearDown()92 void HdfThermalHdiTestAdditional::TearDown() {}
93
94 } // namespace
95
96 namespace {
97 /**
98 * @tc.number SUB_Powermgr_Thermal_SetCpuFreq_0200
99 * @tc.name testSetCpuFreq001
100 * @tc.desc Reliability of function(SetCpuFreq)
101 */
102 HWTEST_F(HdfThermalHdiTestAdditional, testSetCpuFreq001, Function | MediumTest | Level1)
103 {
104 int32_t cpuFreq = 1994100;
105 int32_t ret = 0;
106 int i = 0;
107 for (i = 0; i < 1000; i++) {
108 ret |= g_thermalInterface->SetCpuFreq(cpuFreq);
109 }
110 EXPECT_EQ(0, ret);
111 }
112 /**
113 * @tc.number SUB_Powermgr_Thermal_SetCpuFreq_0300
114 * @tc.name testSetCpuFreq002
115 * @tc.desc Test parameters with abnormal input
116 */
117 HWTEST_F(HdfThermalHdiTestAdditional, testSetCpuFreq002, Function | MediumTest | Level2)
118 {
119 int32_t cpuFreq = -1;
120 int32_t ret = g_thermalInterface->SetCpuFreq(cpuFreq);
121 EXPECT_NE(0, ret);
122 }
123 /**
124 * @tc.number SUB_Powermgr_Thermal_SetCpuFreq_0400
125 * @tc.name testSetCpuFreq003
126 * @tc.desc Test input param
127 */
128 HWTEST_F(HdfThermalHdiTestAdditional, testSetCpuFreq003, Function | MediumTest | Level1)
129 {
130 int32_t cpuFreq = 0x7fffffff;
131 int32_t ret = g_thermalInterface->SetCpuFreq(cpuFreq);
132 EXPECT_EQ(0, ret);
133 }
134 /**
135 * @tc.number SUB_Powermgr_Thermal_SetCpuFreq_0500
136 * @tc.name testSetCpuFreq004
137 * @tc.desc Test input param
138 */
139 HWTEST_F(HdfThermalHdiTestAdditional, testSetCpuFreq004, Function | MediumTest | Level1)
140 {
141 int32_t cpuFreq = 1024;
142 int32_t ret = g_thermalInterface->SetCpuFreq(cpuFreq);
143 EXPECT_EQ(0, ret);
144 }
145 /**
146 * @tc.number SUB_Powermgr_Thermal_SetCpuFreq_0600
147 * @tc.name testSetCpuFreq005
148 * @tc.desc Test parameters with abnormal input
149 */
150 HWTEST_F(HdfThermalHdiTestAdditional, testSetCpuFreq005, Function | MediumTest | Level2)
151 {
152 int32_t cpuFreq = 0;
153 int32_t ret = g_thermalInterface->SetCpuFreq(cpuFreq);
154 EXPECT_NE(0, ret);
155 }
156 /**
157 * @tc.number SUB_Powermgr_Thermal_SetGpuFreq_0200
158 * @tc.name testSetGpuFreq001
159 * @tc.desc Reliability of function(SetGpuFreq)
160 */
161 HWTEST_F(HdfThermalHdiTestAdditional, testSetGpuFreq001, Function | MediumTest | Level1)
162 {
163 int32_t gpuFreq = 1994100;
164 int32_t ret = 0;
165 int i = 0;
166 for (i = 0; i < 1000; i++) {
167 ret |= g_thermalInterface->SetGpuFreq(gpuFreq);
168 }
169 EXPECT_EQ(0, ret);
170 }
171 /**
172 * @tc.number SUB_Powermgr_Thermal_SetGpuFreq_0300
173 * @tc.name testSetGpuFreq002
174 * @tc.desc Test parameters with abnormal input
175 */
176 HWTEST_F(HdfThermalHdiTestAdditional, testSetGpuFreq002, Function | MediumTest | Level2)
177 {
178 int32_t gpuFreq = -1;
179 int32_t ret = g_thermalInterface->SetGpuFreq(gpuFreq);
180 EXPECT_NE(0, ret);
181 }
182 /**
183 * @tc.number SUB_Powermgr_Thermal_SetGpuFreq_0400
184 * @tc.name testSetGpuFreq003
185 * @tc.desc Test input param
186 */
187 HWTEST_F(HdfThermalHdiTestAdditional, testSetGpuFreq003, Function | MediumTest | Level1)
188 {
189 int32_t gpuFreq = 0x7fffffff;
190 int32_t ret = g_thermalInterface->SetGpuFreq(gpuFreq);
191 EXPECT_EQ(0, ret);
192 }
193 /**
194 * @tc.number SUB_Powermgr_Thermal_SetGpuFreq_0500
195 * @tc.name testSetGpuFreq004
196 * @tc.desc Test input param
197 */
198 HWTEST_F(HdfThermalHdiTestAdditional, testSetGpuFreq004, Function | MediumTest | Level1)
199 {
200 int32_t gpuFreq = 1024;
201 int32_t ret = g_thermalInterface->SetGpuFreq(gpuFreq);
202 EXPECT_EQ(0, ret);
203 }
204 /**
205 * @tc.number SUB_Powermgr_Thermal_SetGpuFreq_0600
206 * @tc.name testSetGpuFreq005
207 * @tc.desc Test parameters with abnormal input
208 */
209 HWTEST_F(HdfThermalHdiTestAdditional, testSetGpuFreq005, Function | MediumTest | Level2)
210 {
211 int32_t gpuFreq = 0;
212 int32_t ret = g_thermalInterface->SetGpuFreq(gpuFreq);
213 EXPECT_NE(0, ret);
214 }
215 /**
216 * @tc.number SUB_Powermgr_Thermal_SetBatteryCurrent_0200
217 * @tc.name testSetBatteryCurrent001
218 * @tc.desc Reliability of function(SetBatteryCurrent)
219 */
220 HWTEST_F(HdfThermalHdiTestAdditional, testSetBatteryCurrent001, Function | MediumTest | Level1)
221 {
222 int32_t ret = 0;
223 int32_t batteryCurrent = 1000;
224 int i = 0;
225 for (i = 0; i < 1000; i++) {
226 ret |= g_thermalInterface->SetBatteryCurrent(batteryCurrent);
227 }
228 EXPECT_EQ(0, ret);
229 }
230 /**
231 * @tc.number SUB_Powermgr_Thermal_SetBatteryCurrent_0300
232 * @tc.name testSetBatteryCurrent002
233 * @tc.desc Test parameters with abnormal input
234 */
235 HWTEST_F(HdfThermalHdiTestAdditional, testSetBatteryCurrent002, Function | MediumTest | Level2)
236 {
237 int32_t ret = 0;
238 int32_t batteryCurrent = -1;
239 ret = g_thermalInterface->SetBatteryCurrent(batteryCurrent);
240 EXPECT_NE(0, ret);
241 }
242 /**
243 * @tc.number SUB_Powermgr_Thermal_SetBatteryCurrent_0400
244 * @tc.name testSetBatteryCurrent003
245 * @tc.desc Test input param
246 */
247 HWTEST_F(HdfThermalHdiTestAdditional, testSetBatteryCurrent003, Function | MediumTest | Level1)
248 {
249 int32_t ret = 0;
250 int32_t batteryCurrent = 0x7fffffff;
251 ret = g_thermalInterface->SetBatteryCurrent(batteryCurrent);
252 EXPECT_EQ(0, ret);
253 }
254 /**
255 * @tc.number SUB_Powermgr_Thermal_SetBatteryCurrent_0500
256 * @tc.name testSetBatteryCurrent004
257 * @tc.desc Test input param
258 */
259 HWTEST_F(HdfThermalHdiTestAdditional, testSetBatteryCurrent004, Function | MediumTest | Level1)
260 {
261 int32_t ret = 0;
262 int32_t batteryCurrent = 1024;
263 ret = g_thermalInterface->SetBatteryCurrent(batteryCurrent);
264 EXPECT_EQ(0, ret);
265 }
266 /**
267 * @tc.number SUB_Powermgr_Thermal_SetBatteryCurrent_0600
268 * @tc.name testSetBatteryCurrent005
269 * @tc.desc Test input param
270 */
271 HWTEST_F(HdfThermalHdiTestAdditional, testSetBatteryCurrent005, Function | MediumTest | Level2)
272 {
273 int32_t ret = 0;
274 int32_t batteryCurrent = 0;
275 ret = g_thermalInterface->SetBatteryCurrent(batteryCurrent);
276 EXPECT_NE(0, ret);
277 }
278 /**
279 * @tc.number SUB_Powermgr_Thermal_GetThermalZoneInfo_0200
280 * @tc.name testGetThermalZoneInfo001
281 * @tc.desc Reliability of function(GetThermalZoneInfo)
282 */
283 HWTEST_F(HdfThermalHdiTestAdditional, testGetThermalZoneInfo001, Function | MediumTest | Level1)
284 {
285 HdfThermalCallbackInfo event;
286 int i = 0;
287 int32_t ret = 0;
288 for (i = 0; i < 1000; i++) {
289 ret |= g_thermalInterface->GetThermalZoneInfo(event);
290 }
291 EXPECT_EQ(0, ret);
292 }
293 /**
294 * @tc.number SUB_Powermgr_Thermal_IsolateCpu_0200
295 * @tc.name testIsolateCpu001
296 * @tc.desc Reliability of function(IsolateCpu)
297 */
298 HWTEST_F(HdfThermalHdiTestAdditional, testIsolateCpu001, Function | MediumTest | Level1)
299 {
300 int i = 0;
301 int32_t isolateNum = 2;
302 int32_t ret = 0;
303 for (i = 0; i < 1000; i++) {
304 ret |= g_thermalInterface->IsolateCpu(isolateNum);
305 }
306 EXPECT_EQ(0, ret);
307 }
308 /**
309 * @tc.number SUB_Powermgr_Thermal_IsolateCpu_0300
310 * @tc.name testIsolateCpu002
311 * @tc.desc Test parameters with abnormal input
312 */
313 HWTEST_F(HdfThermalHdiTestAdditional, testIsolateCpu002, Function | MediumTest | Level2)
314 {
315 int32_t isolateNum = -1;
316 int32_t ret = g_thermalInterface->IsolateCpu(isolateNum);
317 EXPECT_NE(0, ret);
318 }
319 /**
320 * @tc.number SUB_Powermgr_Thermal_IsolateCpu_0400
321 * @tc.name testIsolateCpu003
322 * @tc.desc Test parameters with abnormal input
323 */
324 HWTEST_F(HdfThermalHdiTestAdditional, testIsolateCpu003, Function | MediumTest | Level2)
325 {
326 int32_t isolateNum = 0;
327 int32_t ret = g_thermalInterface->IsolateCpu(isolateNum);
328 EXPECT_NE(0, ret);
329 }
330 /**
331 * @tc.number SUB_Powermgr_Thermal_IsolateCpu_0500
332 * @tc.name testIsolateCpu004
333 * @tc.desc Test input param
334 */
335 HWTEST_F(HdfThermalHdiTestAdditional, testIsolateCpu004, Function | MediumTest | Level1)
336 {
337 int32_t isolateNum = 0x7fffffff;
338 int32_t ret = g_thermalInterface->IsolateCpu(isolateNum);
339 EXPECT_EQ(0, ret);
340 }
341 /**
342 * @tc.number SUB_Powermgr_Thermal_IsolateCpu_0600
343 * @tc.name testIsolateCpu005
344 * @tc.desc Test input param
345 */
346 HWTEST_F(HdfThermalHdiTestAdditional, testIsolateCpu005, Function | MediumTest | Level1)
347 {
348 int32_t isolateNum = 3;
349 int32_t ret = g_thermalInterface->IsolateCpu(isolateNum);
350 EXPECT_EQ(0, ret);
351 }
352 /**
353 * @tc.number SUB_Powermgr_Thermal_Register_0200
354 * @tc.name testRegister001
355 * @tc.desc Reliability of function(Register)
356 */
357 HWTEST_F(HdfThermalHdiTestAdditional, testRegister001, Function | MediumTest | Level1)
358 {
359 int i = 0;
360 int32_t ret = 0;
361 for (i = 0; i < 1000; i++) {
362 ret |= g_thermalInterface->Register(g_callback);
363 g_thermalInterface->Unregister();
364 }
365 EXPECT_EQ(0, ret);
366 }
367 /**
368 * @tc.number SUB_Powermgr_Thermal_Register_0200
369 * @tc.name testRegister002
370 * @tc.desc Test parameters with abnormal input
371 */
372 HWTEST_F(HdfThermalHdiTestAdditional, testRegister002, Function | MediumTest | Level2)
373 {
374 int32_t ret;
375 ret = g_thermalInterface->Register(nullptr);
376 EXPECT_NE(0, ret);
377 }
378 /**
379 * @tc.number SUB_Powermgr_Thermal_Unregister_0200
380 * @tc.name testUnregister001
381 * @tc.desc Reliability of function(Unregister)
382 */
383 HWTEST_F(HdfThermalHdiTestAdditional, testUnregister001, Function | MediumTest | Level1)
384 {
385 int i = 0;
386 int32_t ret = 0;
387 for (i = 0; i < 1000; i++) {
388 g_thermalInterface->Register(g_callback);
389 ret |= g_thermalInterface->Unregister();
390 }
391 EXPECT_EQ(0, ret);
392 }
393 /**
394 * @tc.number SUB_Powermgr_Thermal_Unregister_0300
395 * @tc.name testUnregister002
396 * @tc.desc Test parameters with abnormal input
397 */
398 HWTEST_F(HdfThermalHdiTestAdditional, testUnregister002, Function | MediumTest | Level2)
399 {
400 int32_t ret = g_thermalInterface->Unregister();
401 EXPECT_NE(0, ret);
402 }
403 /**
404 * @tc.number SUB_Powermgr_Thermal_RegisterFanCallback_0200
405 * @tc.name testRegisterFanCallback001
406 * @tc.desc Reliability of function(RegisterFanCallback)
407 */
408 HWTEST_F(HdfThermalHdiTestAdditional, testRegisterFanCallback001, Function | MediumTest | Level1)
409 {
410 int i = 0;
411 int32_t ret = 0;
412 for (i = 0; i < 1000; i++) {
413 ret |= g_thermalInterface->RegisterFanCallback(g_fanCallback);
414 g_thermalInterface->UnregisterFanCallback();
415 }
416 EXPECT_EQ(0, ret);
417 }
418 /**
419 * @tc.number SUB_Powermgr_Thermal_RegisterFanCallback_0300
420 * @tc.name testRegisterFanCallback002
421 * @tc.desc Test parameters with abnormal input
422 */
423 HWTEST_F(HdfThermalHdiTestAdditional, testRegisterFanCallback002, Function | MediumTest | Level2)
424 {
425 int32_t ret = g_thermalInterface->RegisterFanCallback(nullptr);
426 EXPECT_NE(0, ret);
427 }
428 /**
429 * @tc.number SUB_Powermgr_Thermal_UnregisterFanCallback_0200
430 * @tc.name testUnregisterFanCallback001
431 * @tc.desc Test input param
432 */
433 HWTEST_F(HdfThermalHdiTestAdditional, testUnregisterFanCallback001, Function | MediumTest | Level1)
434 {
435 int i = 0;
436 int32_t ret = 0;
437 for (i = 0; i < 1000; i++) {
438 g_thermalInterface->RegisterFanCallback(g_fanCallback);
439 ret |= g_thermalInterface->UnregisterFanCallback();
440 }
441 EXPECT_EQ(0, ret);
442 }
443 /**
444 * @tc.number SUB_Powermgr_Thermal_UnregisterFanCallback_0300
445 * @tc.name testUnregisterFanCallback002
446 * @tc.desc Test parameters with abnormal input
447 */
448 HWTEST_F(HdfThermalHdiTestAdditional, testUnregisterFanCallback002, Function | MediumTest | Level2)
449 {
450 int32_t ret = g_thermalInterface->UnregisterFanCallback();
451 EXPECT_NE(0, ret);
452 }
453 /**
454 * @tc.number SUB_Powermgr_Thermal_OnThermalDataEvent_0100
455 * @tc.name testOnThermalDataEvent001
456 * @tc.desc Test callback
457 */
458 HWTEST_F(HdfThermalHdiTestAdditional, testOnThermalDataEvent001, Function | MediumTest | Level1)
459 {
460 int32_t ret = g_thermalInterface->Register(g_callback);
461 EXPECT_EQ(0, ret);
462 sleep(35);
463 EXPECT_EQ(true, (g_onThermalDataEventCount != 0));
464 g_thermalInterface->Unregister();
465 g_onThermalDataEventCount = 0;
466 }
467 /**
468 * @tc.number SUB_Powermgr_Thermal_OnThermalDataEvent_0200
469 * @tc.name testOnThermalDataEvent002
470 * @tc.desc Test callback
471 */
472 HWTEST_F(HdfThermalHdiTestAdditional, testOnThermalDataEvent002, Function | MediumTest | Level2)
473 {
474 sleep(35);
475 EXPECT_EQ(false, (g_onThermalDataEventCount != 0));
476 g_onThermalDataEventCount = 0;
477 }
478 /**
479 * @tc.number SUB_Powermgr_Thermal_OnFanDataEvent_0100
480 * @tc.name testOnFanDataEvent001
481 * @tc.desc Test callback
482 */
483 HWTEST_F(HdfThermalHdiTestAdditional, testOnFanDataEvent001, Function | MediumTest | Level1)
484 {
485 int32_t ret = g_thermalInterface->RegisterFanCallback(g_fanCallback);
486 EXPECT_EQ(0, ret);
487 sleep(35);
488 EXPECT_EQ(true, (g_onFanDataEventCount != 0));
489 g_thermalInterface->UnregisterFanCallback();
490 g_onFanDataEventCount = 0;
491 }
492 /**
493 * @tc.number SUB_Powermgr_Thermal_OnFanDataEvent_0200
494 * @tc.name testOnFanDataEvent002
495 * @tc.desc Test callback
496 */
497 HWTEST_F(HdfThermalHdiTestAdditional, testOnFanDataEvent002, Function | MediumTest | Level2)
498 {
499 sleep(35);
500 EXPECT_EQ(false, (g_onFanDataEventCount != 0));
501 g_onFanDataEventCount = 0;
502 }
503 } // namespace
504