• 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 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     g_service->GetThermalLevel(level);
190     EXPECT_EQ(expectLevel, static_cast<int32_t>(level));
191     EXPECT_EQ(1, MockSocPerfAction::GetBoostRequestCounter());
192     event.info.clear();
193 
194     info1.temp = 46100;
195     event.info.push_back(info1);
196     g_service->HandleThermalCallbackEvent(event);
197     expectLevel = 3;
198     g_service->GetThermalLevel(level);
199     EXPECT_EQ(expectLevel, static_cast<int32_t>(level));
200     EXPECT_EQ(2, MockSocPerfAction::GetBoostRequestCounter());
201     event.info.clear();
202 
203     info1.temp = 43100;
204     event.info.push_back(info1);
205     g_service->HandleThermalCallbackEvent(event);
206     expectLevel = 2;
207     g_service->GetThermalLevel(level);
208     EXPECT_EQ(expectLevel, static_cast<int32_t>(level));
209     EXPECT_EQ(3, MockSocPerfAction::GetBoostRequestCounter());
210     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest001 end");
211 }
212 
213 /**
214  * @tc.name: ThermalMockActionTest002
215  * @tc.desc: test cpu boost action
216  * @tc.type: FUNC
217  * @tc.cond: Set Battery temp
218  * @tc.result: level 3, socperf function does not execute
219  * @tc.require: issueI6JSQD
220  */
221 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest002, Function|MediumTest|Level2)
222 {
223     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest002 start");
224     ASSERT_NE(g_service, nullptr);
225     HdfThermalCallbackInfo event;
226     ThermalZoneInfo info1;
227     info1.type = "battery";
228     info1.temp = 46100;
229     event.info.push_back(info1);
230     g_service->HandleThermalCallbackEvent(event);
231     ThermalLevel level = ThermalLevel::COOL;
232     int32_t expectLevel = 3;
233     g_service->GetThermalLevel(level);
234     EXPECT_EQ(expectLevel, static_cast<int32_t>(level));
235     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest002 end");
236 }
237 
238 /**
239  * @tc.name: ThermalMockActionTest003
240  * @tc.desc: test get cpu and gpu freq by setting temp
241  * @tc.type: FEATURE
242  * @tc.cond: Set BATTERY temp, state not satisfied
243  * @tc.result level 1
244  * @tc.require: issueI6UI5Q
245  */
246 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest003, Function|MediumTest|Level2)
247 {
248     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest003: start");
249     ASSERT_NE(g_service, nullptr);
250     HdfThermalCallbackInfo event;
251     ThermalZoneInfo info1;
252     info1.type = "battery";
253     info1.temp = 40100;
254     event.info.push_back(info1);
255     g_service->HandleThermalCallbackEvent(event);
256     ThermalLevel level = ThermalLevel::COOL;
257     int32_t expectLevel = 1;
258     g_service->GetThermalLevel(level);
259     EXPECT_EQ(expectLevel, static_cast<int32_t>(level));
260     EXPECT_EQ(1992000, MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID));
261     EXPECT_EQ(1991500, MockSocPerfAction::GetLimitValue(LIM_CPU_MED_ID));
262     EXPECT_EQ(1991200, MockSocPerfAction::GetLimitValue(LIM_CPU_LIT_ID));
263     int64_t gpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_GPU_ID);
264     if (PowerMgrClient::GetInstance().IsScreenOn()) {
265         EXPECT_TRUE(gpuLimitValue == 512000);
266     } else {
267         EXPECT_TRUE(gpuLimitValue == 524288);
268     }
269     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest003: end");
270 }
271 
272 /**
273  * @tc.name: ThermalMockActionTest004
274  * @tc.desc: test get cpu and gpu freq by setting temp
275  * @tc.type: FEATURE
276  * @tc.cond: Set BATTERY temp, state not satisfied
277  * @tc.result level 2
278  * @tc.require: issueI6UI5Q
279  */
280 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest004, Function|MediumTest|Level2)
281 {
282     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest004: start");
283     ASSERT_NE(g_service, nullptr);
284     HdfThermalCallbackInfo event;
285     ThermalZoneInfo info1;
286     info1.type = "battery";
287     info1.temp = 43100;
288     event.info.push_back(info1);
289     g_service->HandleThermalCallbackEvent(event);
290     ThermalLevel level = ThermalLevel::COOL;
291     int32_t expectLevel = 2;
292     g_service->GetThermalLevel(level);
293     EXPECT_EQ(expectLevel, static_cast<int32_t>(level));
294     EXPECT_EQ(1991000, MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID));
295     EXPECT_EQ(1990500, MockSocPerfAction::GetLimitValue(LIM_CPU_MED_ID));
296     EXPECT_EQ(1990200, MockSocPerfAction::GetLimitValue(LIM_CPU_LIT_ID));
297     int64_t gpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_GPU_ID);
298     if (PowerMgrClient::GetInstance().IsScreenOn()) {
299         EXPECT_TRUE(gpuLimitValue == 487424);
300     } else {
301         EXPECT_TRUE(gpuLimitValue == 499712);
302     }
303     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest004: end");
304 }
305 
306 /**
307  * @tc.name: ThermalMockActionTest005
308  * @tc.desc: test get cpu and gpu freq by setting temp
309  * @tc.type: FEATURE
310  * @tc.cond: Set BATTERY temp, state not satisfied
311  * @tc.result level 2
312  * @tc.require: issueI6UI5Q
313  */
314 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest005, Function|MediumTest|Level2)
315 {
316     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest005: start");
317     ASSERT_NE(g_service, nullptr);
318     HdfThermalCallbackInfo event;
319     ThermalZoneInfo info1;
320     info1.type = "battery";
321     info1.temp = 46100;
322     event.info.push_back(info1);
323     g_service->HandleThermalCallbackEvent(event);
324     ThermalLevel level = ThermalLevel::COOL;
325     int32_t expectLevel = 3;
326     g_service->GetThermalLevel(level);
327     EXPECT_EQ(expectLevel, static_cast<int32_t>(level));
328     EXPECT_EQ(1990000, MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID));
329     EXPECT_EQ(1989500, MockSocPerfAction::GetLimitValue(LIM_CPU_MED_ID));
330     EXPECT_EQ(1989200, MockSocPerfAction::GetLimitValue(LIM_CPU_LIT_ID));
331     int64_t gpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_GPU_ID);
332     if (PowerMgrClient::GetInstance().IsScreenOn()) {
333         EXPECT_TRUE(gpuLimitValue == 462848);
334     } else {
335         EXPECT_TRUE(gpuLimitValue == 475136);
336     }
337     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest005: end");
338 }
339 
340 /**
341  * @tc.name: ThermalMockActionTest006
342  * @tc.desc: test get cpu freq by setting temp
343  * @tc.type: FEATURE
344  * @tc.cond: Set BATTERY temp, scene = "cam"
345  * @tc.result level 1
346  * @tc.require: issueI6UI5Q
347  */
348 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest006, Function|MediumTest|Level2)
349 {
350     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest006: start");
351     auto chargerState =  ChargerStateCollection::GetInstance();
352     chargerState->SetCharge(true);
353     auto state = chargerState->GetCharge();
354     ASSERT_NE(g_service, nullptr);
355     HdfThermalCallbackInfo event;
356     ThermalZoneInfo info1;
357     info1.type = "battery";
358     info1.temp = 40100;
359     event.info.push_back(info1);
360     g_service->SetScene("cam");
361     g_service->HandleThermalCallbackEvent(event);
362     ThermalLevel level = ThermalLevel::COOL;
363     int32_t expectLevel = 1;
364     g_service->GetThermalLevel(level);
365     EXPECT_EQ(expectLevel, static_cast<int32_t>(level));
366 #ifdef BATTERY_MANAGER_ENABLE
367     int64_t cpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID);
368     if (state) {
369         EXPECT_TRUE(cpuLimitValue == 1991800);
370     } else {
371         EXPECT_TRUE(cpuLimitValue == 1991600);
372     }
373 #endif
374     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest006: end");
375 }
376 
377 /**
378  * @tc.name: ThermalMockActionTest007
379  * @tc.desc: test get cpu freq by setting temp
380  * @tc.type: FEATURE
381  * @tc.cond: Set BATTERY temp, scene = "cam"
382  * @tc.result level 2
383  * @tc.require: issueI6UI5Q
384  */
385 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest007, Function|MediumTest|Level2)
386 {
387     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest007: start");
388     ASSERT_NE(g_service, nullptr);
389     HdfThermalCallbackInfo event;
390     ThermalZoneInfo info1;
391     info1.type = "battery";
392     info1.temp = 43100;
393     event.info.push_back(info1);
394     g_service->SetScene("cam");
395     g_service->HandleThermalCallbackEvent(event);
396     ThermalLevel level = ThermalLevel::COOL;
397     int32_t expectLevel = 2;
398     g_service->GetThermalLevel(level);
399     EXPECT_EQ(expectLevel, static_cast<int32_t>(level));
400 #ifdef BATTERY_MANAGER_ENABLE
401     int64_t cpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID);
402     auto chargerState =  ChargerStateCollection::GetInstance();
403     chargerState->SetCharge(true);
404     auto state = chargerState->GetCharge();
405     if (state) {
406         EXPECT_TRUE(cpuLimitValue == 1990800);
407     } else {
408         EXPECT_TRUE(cpuLimitValue == 1990600);
409     }
410 #endif
411     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest007: end");
412 }
413 
414 /**
415  * @tc.name: ThermalMockActionTest008
416  * @tc.desc: test get cpu freq by setting temp
417  * @tc.type: FEATURE
418  * @tc.cond: Set BATTERY temp, scene = "cam"
419  * @tc.result level 3
420  * @tc.require: issueI6UI5Q
421  */
422 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest008, Function|MediumTest|Level2)
423 {
424     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest008: start");
425     ASSERT_NE(g_service, nullptr);
426     HdfThermalCallbackInfo event;
427     ThermalZoneInfo info1;
428     info1.type = "battery";
429     info1.temp = 46100;
430     event.info.push_back(info1);
431     g_service->SetScene("cam");
432     g_service->HandleThermalCallbackEvent(event);
433     ThermalLevel level = ThermalLevel::COOL;
434     int32_t expectLevel = 3;
435     g_service->GetThermalLevel(level);
436     EXPECT_EQ(expectLevel, static_cast<int32_t>(level));
437 #ifdef BATTERY_MANAGER_ENABLE
438     int64_t cpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID);
439     auto chargerState =  ChargerStateCollection::GetInstance();
440     chargerState->SetCharge(true);
441     auto state = chargerState->GetCharge();
442     if (state) {
443         EXPECT_TRUE(cpuLimitValue == 1989800);
444     } else {
445         EXPECT_TRUE(cpuLimitValue == 1989600);
446     }
447 #endif
448     THERMAL_HILOGI(LABEL_TEST, "ThermalMockActionTest008: end");
449 }
450 }
451