• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifdef THERMAL_GTEST
17 #define private   public
18 #endif
19 
20 #include "thermal_mock_action_test.h"
21 
22 #include <cstdio>
23 #include <cstdlib>
24 #include <dirent.h>
25 #include <fcntl.h>
26 #include "securec.h"
27 #ifdef BATTERY_MANAGER_ENABLE
28 #include "battery_srv_client.h"
29 #endif
30 #include "constants.h"
31 #include "mock_socperf_action.h"
32 #include "power_mgr_client.h"
33 #include "thermal_service.h"
34 #include "v1_1/ithermal_interface.h"
35 #include "v1_1/thermal_types.h"
36 #include "action_cpu_boost.h"
37 #include "action_cpu_big.h"
38 #include "action_cpu_med.h"
39 #include "action_cpu_lit.h"
40 #include "action_gpu.h"
41 
42 using namespace testing::ext;
43 using namespace OHOS::PowerMgr;
44 using namespace OHOS;
45 using namespace std;
46 using namespace OHOS::HDI::Thermal::V1_1;
47 
48 namespace {
49 static sptr<ThermalService> g_service = nullptr;
50 const std::string SYSTEM_THERMAL_SERVICE_CONFIG_PATH = "/system/etc/thermal_config/thermal_service_config.xml";
51 }
52 
53 class MockActionCpuBoost : public ActionCpuBoost {
54 public:
MockActionCpuBoost()55     MockActionCpuBoost() : ActionCpuBoost("boost") {}
56     virtual ~MockActionCpuBoost() = default;
57 
58     virtual void SocLimitRequest(int32_t tag, int64_t value);
59     virtual void SetSocPerfThermalLevel(uint32_t level);
60 };
61 
62 class MockActionCpuBig : public ActionCpuBig {
63 public:
MockActionCpuBig()64     MockActionCpuBig() : ActionCpuBig("cpu_big") {}
65     virtual ~MockActionCpuBig() = default;
66 
67     virtual void SocLimitRequest(int32_t tag, int64_t value);
68 };
69 
70 class MockActionCpuMed : public ActionCpuMed {
71 public:
MockActionCpuMed()72     MockActionCpuMed() : ActionCpuMed("cpu_med") {}
73     virtual ~MockActionCpuMed() = default;
74 
75     virtual void SocLimitRequest(int32_t tag, int64_t value);
76 };
77 
78 class MockActionCpuLit : public ActionCpuLit {
79 public:
MockActionCpuLit()80     MockActionCpuLit() : ActionCpuLit("cpu_lit") {}
81     virtual ~MockActionCpuLit() = default;
82 
83     virtual void SocLimitRequest(int32_t tag, int64_t value);
84 };
85 
86 class MockActionGpu : public ActionGpu {
87 public:
MockActionGpu()88     MockActionGpu() : ActionGpu("gpu") {}
89     virtual ~MockActionGpu() = default;
90 
91     virtual void SocLimitRequest(int32_t tag, int64_t value);
92 };
93 
SocLimitRequest(int32_t tag,int64_t value)94 void MockActionCpuBig::SocLimitRequest(int32_t tag, int64_t value)
95 {
96     MockSocPerfAction::LimitRequest(tag, value);
97 }
98 
SocLimitRequest(int32_t tag,int64_t value)99 void MockActionCpuMed::SocLimitRequest(int32_t tag, int64_t value)
100 {
101     MockSocPerfAction::LimitRequest(tag, value);
102 }
103 
SocLimitRequest(int32_t tag,int64_t value)104 void MockActionCpuLit::SocLimitRequest(int32_t tag, int64_t value)
105 {
106     MockSocPerfAction::LimitRequest(tag, value);
107 }
108 
SocLimitRequest(int32_t tag,int64_t value)109 void MockActionGpu::SocLimitRequest(int32_t tag, int64_t value)
110 {
111     MockSocPerfAction::LimitRequest(tag, value);
112 }
113 
SocLimitRequest(int32_t tag,int64_t value)114 void MockActionCpuBoost::SocLimitRequest(int32_t tag, int64_t value)
115 {
116     MockSocPerfAction::LimitRequest(tag, value);
117 }
118 
SetSocPerfThermalLevel(uint32_t level)119 void MockActionCpuBoost::SetSocPerfThermalLevel(uint32_t level)
120 {
121     MockSocPerfAction::BoostRequest();
122 }
123 
EnableMock()124 void EnableMock()
125 {
126     const string needMockBoost = "boost";
127     const string needMockBig = "cpu_big";
128     const string needMockMed = "cpu_med";
129     const string needMockLit = "cpu_lit";
130     const string needMockGpu = "gpu";
131     MockActionCpuBoost *mockActionCpuBoost = new MockActionCpuBoost();
132     MockActionCpuBig *mockActionCpuBig = new MockActionCpuBig();
133     MockActionCpuMed *mockActionCpuMed = new MockActionCpuMed();
134     MockActionCpuLit *mockActionCpuLit = new MockActionCpuLit();
135     MockActionGpu *mockActionGpu = new MockActionGpu();
136     g_service->EnableMock(needMockBoost, mockActionCpuBoost);
137     g_service->EnableMock(needMockBig, mockActionCpuBig);
138     g_service->EnableMock(needMockMed, mockActionCpuMed);
139     g_service->EnableMock(needMockLit, mockActionCpuLit);
140     g_service->EnableMock(needMockGpu, mockActionGpu);
141 }
142 
SetUpTestCase()143 void ThermalMockActionTest::SetUpTestCase()
144 {
145     g_service = ThermalService::GetInstance();
146     g_service->InitSystemTestModules();
147     g_service->OnStart();
148     g_service->InitStateMachine();
149     g_service->InitActionManager();
150     EnableMock();
151 }
152 
TearDownTestCase()153 void ThermalMockActionTest::TearDownTestCase()
154 {
155     g_service->OnStop();
156     ThermalService::DestroyInstance();
157 }
158 
TearDown()159 void ThermalMockActionTest::TearDown()
160 {
161     InitNode();
162     g_service->SetScene("");
163     g_service->GetThermalInfo();
164     MockSocPerfAction::ClearLimit();
165     MockSocPerfAction::ClearBoost();
166 }
167 
168 namespace {
169 /**
170  * @tc.name: ThermalMockActionTest001
171  * @tc.desc: test cpu boost action
172  * @tc.type: FUNC
173  * @tc.cond: Set Battery temp
174  * @tc.result: level 1~3, socperf function execution three times
175  * @tc.require: issueI6JSQD
176  */
177 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest001, Function|MediumTest|Level2)
178 {
179     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest001 function start!");
180     ASSERT_NE(g_service, nullptr);
181     ThermalLevel level = ThermalLevel::COOL;
182     int32_t expectLevel = 1;
183     HdfThermalCallbackInfo event;
184     ThermalZoneInfo info1;
185     info1.type = "battery";
186     info1.temp = 40100;
187     event.info.push_back(info1);
188     g_service->HandleThermalCallbackEvent(event);
189     int32_t levelValue = static_cast<int32_t>(level);
190     g_service->GetThermalLevel(levelValue);
191     EXPECT_EQ(expectLevel, levelValue);
192     EXPECT_EQ(1, MockSocPerfAction::GetBoostRequestCounter());
193     event.info.clear();
194 
195     info1.temp = 46100;
196     event.info.push_back(info1);
197     g_service->HandleThermalCallbackEvent(event);
198     expectLevel = 3;
199     g_service->GetThermalLevel(levelValue);
200     EXPECT_EQ(expectLevel, levelValue);
201     EXPECT_EQ(2, MockSocPerfAction::GetBoostRequestCounter());
202     event.info.clear();
203 
204     info1.temp = 43100;
205     event.info.push_back(info1);
206     g_service->HandleThermalCallbackEvent(event);
207     expectLevel = 2;
208     g_service->GetThermalLevel(levelValue);
209     EXPECT_EQ(expectLevel, levelValue);
210     EXPECT_EQ(3, MockSocPerfAction::GetBoostRequestCounter());
211     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest001 function end!");
212 }
213 
214 /**
215  * @tc.name: ThermalMockActionTest002
216  * @tc.desc: test cpu boost action
217  * @tc.type: FUNC
218  * @tc.cond: Set Battery temp
219  * @tc.result: level 3, socperf function does not execute
220  * @tc.require: issueI6JSQD
221  */
222 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest002, Function|MediumTest|Level2)
223 {
224     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest002 function start!");
225     ASSERT_NE(g_service, nullptr);
226     HdfThermalCallbackInfo event;
227     ThermalZoneInfo info1;
228     info1.type = "battery";
229     info1.temp = 46100;
230     event.info.push_back(info1);
231     g_service->HandleThermalCallbackEvent(event);
232     ThermalLevel level = ThermalLevel::COOL;
233     int32_t expectLevel = 3;
234     int32_t levelValue = static_cast<int32_t>(level);
235     g_service->GetThermalLevel(levelValue);
236     EXPECT_EQ(expectLevel, levelValue);
237     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest002 function end!");
238 }
239 
240 /**
241  * @tc.name: ThermalMockActionTest003
242  * @tc.desc: test get cpu and gpu freq by setting temp
243  * @tc.type: FEATURE
244  * @tc.cond: Set BATTERY temp, state not satisfied
245  * @tc.result level 1
246  * @tc.require: issueI6UI5Q
247  */
248 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest003, Function|MediumTest|Level2)
249 {
250     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest003 function start!");
251     ASSERT_NE(g_service, nullptr);
252     HdfThermalCallbackInfo event;
253     ThermalZoneInfo info1;
254     info1.type = "battery";
255     info1.temp = 40100;
256     event.info.push_back(info1);
257     g_service->HandleThermalCallbackEvent(event);
258     ThermalLevel level = ThermalLevel::COOL;
259     int32_t expectLevel = 1;
260     int32_t levelValue = static_cast<int32_t>(level);
261     g_service->GetThermalLevel(levelValue);
262     MockSocPerfAction::LimitRequest(LIM_CPU_BIG_ID, 1992000);
263     EXPECT_EQ(expectLevel, levelValue);
264     EXPECT_EQ(1992000, MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID));
265     EXPECT_EQ(1991500, MockSocPerfAction::GetLimitValue(LIM_CPU_MED_ID));
266     EXPECT_EQ(1991200, MockSocPerfAction::GetLimitValue(LIM_CPU_LIT_ID));
267     int64_t gpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_GPU_ID);
268     if (PowerMgrClient::GetInstance().IsScreenOn()) {
269         EXPECT_TRUE(gpuLimitValue == 512000);
270     } else {
271         EXPECT_TRUE(gpuLimitValue == 524288);
272     }
273     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest003 function end!");
274 }
275 
276 /**
277  * @tc.name: ThermalMockActionTest004
278  * @tc.desc: test get cpu and gpu freq by setting temp
279  * @tc.type: FEATURE
280  * @tc.cond: Set BATTERY temp, state not satisfied
281  * @tc.result level 2
282  * @tc.require: issueI6UI5Q
283  */
284 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest004, Function|MediumTest|Level2)
285 {
286     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest004 function start!");
287     ASSERT_NE(g_service, nullptr);
288     HdfThermalCallbackInfo event;
289     ThermalZoneInfo info1;
290     info1.type = "battery";
291     info1.temp = 43100;
292     event.info.push_back(info1);
293     g_service->HandleThermalCallbackEvent(event);
294     ThermalLevel level = ThermalLevel::COOL;
295     int32_t expectLevel = 2;
296     int32_t levelValue = static_cast<int32_t>(level);
297     g_service->GetThermalLevel(levelValue);
298     EXPECT_EQ(expectLevel, levelValue);
299     EXPECT_EQ(1991000, MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID));
300     EXPECT_EQ(1990500, MockSocPerfAction::GetLimitValue(LIM_CPU_MED_ID));
301     EXPECT_EQ(1990200, MockSocPerfAction::GetLimitValue(LIM_CPU_LIT_ID));
302     int64_t gpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_GPU_ID);
303     if (PowerMgrClient::GetInstance().IsScreenOn()) {
304         EXPECT_TRUE(gpuLimitValue == 487424);
305     } else {
306         EXPECT_TRUE(gpuLimitValue == 499712);
307     }
308     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest004 function end!");
309 }
310 
311 /**
312  * @tc.name: ThermalMockActionTest005
313  * @tc.desc: test get cpu and gpu freq by setting temp
314  * @tc.type: FEATURE
315  * @tc.cond: Set BATTERY temp, state not satisfied
316  * @tc.result level 2
317  * @tc.require: issueI6UI5Q
318  */
319 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest005, Function|MediumTest|Level2)
320 {
321     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest005 function start!");
322     ASSERT_NE(g_service, nullptr);
323     HdfThermalCallbackInfo event;
324     ThermalZoneInfo info1;
325     info1.type = "battery";
326     info1.temp = 46100;
327     event.info.push_back(info1);
328     g_service->HandleThermalCallbackEvent(event);
329     ThermalLevel level = ThermalLevel::COOL;
330     int32_t expectLevel = 3;
331     int32_t levelValue = static_cast<int32_t>(level);
332     g_service->GetThermalLevel(levelValue);
333     EXPECT_EQ(expectLevel, levelValue);
334     EXPECT_EQ(1990000, MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID));
335     EXPECT_EQ(1989500, MockSocPerfAction::GetLimitValue(LIM_CPU_MED_ID));
336     EXPECT_EQ(1989200, MockSocPerfAction::GetLimitValue(LIM_CPU_LIT_ID));
337     int64_t gpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_GPU_ID);
338     if (PowerMgrClient::GetInstance().IsScreenOn()) {
339         EXPECT_TRUE(gpuLimitValue == 462848);
340     } else {
341         EXPECT_TRUE(gpuLimitValue == 475136);
342     }
343     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest005 function end!");
344 }
345 
346 /**
347  * @tc.name: ThermalMockActionTest006
348  * @tc.desc: test get cpu freq by setting temp
349  * @tc.type: FEATURE
350  * @tc.cond: Set BATTERY temp, scene = "cam"
351  * @tc.result level 1
352  * @tc.require: issueI6UI5Q
353  */
354 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest006, Function|MediumTest|Level2)
355 {
356     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest006 function start!");
357     auto chargerState =  ChargerStateCollection::GetInstance();
358     chargerState->SetCharge(true);
359     auto state = chargerState->GetCharge();
360     ASSERT_NE(g_service, nullptr);
361     HdfThermalCallbackInfo event;
362     ThermalZoneInfo info1;
363     info1.type = "battery";
364     info1.temp = 40100;
365     event.info.push_back(info1);
366     g_service->SetScene("cam");
367     g_service->HandleThermalCallbackEvent(event);
368     ThermalLevel level = ThermalLevel::COOL;
369     int32_t expectLevel = 1;
370     int32_t levelValue = static_cast<int32_t>(level);
371     g_service->GetThermalLevel(levelValue);
372     EXPECT_EQ(expectLevel, levelValue);
373 #ifdef BATTERY_MANAGER_ENABLE
374     int64_t cpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID);
375     if (state) {
376         EXPECT_TRUE(cpuLimitValue == 1991800);
377     } else {
378         EXPECT_TRUE(cpuLimitValue == 1991600);
379     }
380 #endif
381     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest006 function end!");
382 }
383 
384 /**
385  * @tc.name: ThermalMockActionTest007
386  * @tc.desc: test get cpu freq by setting temp
387  * @tc.type: FEATURE
388  * @tc.cond: Set BATTERY temp, scene = "cam"
389  * @tc.result level 2
390  * @tc.require: issueI6UI5Q
391  */
392 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest007, Function|MediumTest|Level2)
393 {
394     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest007 function start!");
395     ASSERT_NE(g_service, nullptr);
396     HdfThermalCallbackInfo event;
397     ThermalZoneInfo info1;
398     info1.type = "battery";
399     info1.temp = 43100;
400     event.info.push_back(info1);
401     g_service->SetScene("cam");
402     g_service->HandleThermalCallbackEvent(event);
403     ThermalLevel level = ThermalLevel::COOL;
404     int32_t expectLevel = 2;
405     int32_t levelValue = static_cast<int32_t>(level);
406     g_service->GetThermalLevel(levelValue);
407     EXPECT_EQ(expectLevel, levelValue);
408 #ifdef BATTERY_MANAGER_ENABLE
409     int64_t cpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID);
410     auto chargerState =  ChargerStateCollection::GetInstance();
411     chargerState->SetCharge(true);
412     auto state = chargerState->GetCharge();
413     if (state) {
414         EXPECT_TRUE(cpuLimitValue == 1990800);
415     } else {
416         EXPECT_TRUE(cpuLimitValue == 1990600);
417     }
418 #endif
419     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest007 function end!");
420 }
421 
422 /**
423  * @tc.name: ThermalMockActionTest008
424  * @tc.desc: test get cpu freq by setting temp
425  * @tc.type: FEATURE
426  * @tc.cond: Set BATTERY temp, scene = "cam"
427  * @tc.result level 3
428  * @tc.require: issueI6UI5Q
429  */
430 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest008, Function|MediumTest|Level2)
431 {
432     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest008 function start!");
433     ASSERT_NE(g_service, nullptr);
434     HdfThermalCallbackInfo event;
435     ThermalZoneInfo info1;
436     info1.type = "battery";
437     info1.temp = 46100;
438     event.info.push_back(info1);
439     g_service->SetScene("cam");
440     g_service->HandleThermalCallbackEvent(event);
441     ThermalLevel level = ThermalLevel::COOL;
442     int32_t expectLevel = 3;
443     int32_t levelValue = static_cast<int32_t>(level);
444     g_service->GetThermalLevel(levelValue);
445     EXPECT_EQ(expectLevel, levelValue);
446 #ifdef BATTERY_MANAGER_ENABLE
447     int64_t cpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID);
448     auto chargerState =  ChargerStateCollection::GetInstance();
449     chargerState->SetCharge(true);
450     auto state = chargerState->GetCharge();
451     if (state) {
452         EXPECT_TRUE(cpuLimitValue == 1989800);
453     } else {
454         EXPECT_TRUE(cpuLimitValue == 1989600);
455     }
456 #endif
457     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest008 function end!");
458 }
459 }
460