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_HILOGD(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_HILOGD(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_HILOGD(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_HILOGD(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_HILOGD(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_HILOGD(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_HILOGD(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_HILOGD(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_HILOGD(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_HILOGD(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_HILOGD(LABEL_TEST, "ThermalMockActionTest006: start");
351 ASSERT_NE(g_service, nullptr);
352 HdfThermalCallbackInfo event;
353 ThermalZoneInfo info1;
354 info1.type = "battery";
355 info1.temp = 40100;
356 event.info.push_back(info1);
357 g_service->SetScene("cam");
358 g_service->HandleThermalCallbackEvent(event);
359 ThermalLevel level = ThermalLevel::COOL;
360 int32_t expectLevel = 1;
361 g_service->GetThermalLevel(level);
362 EXPECT_EQ(expectLevel, static_cast<int32_t>(level));
363 #ifdef BATTERY_MANAGER_ENABLE
364 int64_t cpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID);
365 auto state = BatterySrvClient::GetInstance().GetChargingStatus();
366 if (state == BatteryChargeState::CHARGE_STATE_ENABLE) {
367 EXPECT_TRUE(cpuLimitValue == 1991800);
368 } else if (state == BatteryChargeState::CHARGE_STATE_NONE) {
369 EXPECT_TRUE(cpuLimitValue == 1991600);
370 }
371 #endif
372 THERMAL_HILOGD(LABEL_TEST, "ThermalMockActionTest006: end");
373 }
374
375 /**
376 * @tc.name: ThermalMockActionTest007
377 * @tc.desc: test get cpu freq by setting temp
378 * @tc.type: FEATURE
379 * @tc.cond: Set BATTERY temp, scene = "cam"
380 * @tc.result level 2
381 * @tc.require: issueI6UI5Q
382 */
383 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest007, Function|MediumTest|Level2)
384 {
385 THERMAL_HILOGD(LABEL_TEST, "ThermalMockActionTest007: start");
386 ASSERT_NE(g_service, nullptr);
387 HdfThermalCallbackInfo event;
388 ThermalZoneInfo info1;
389 info1.type = "battery";
390 info1.temp = 43100;
391 event.info.push_back(info1);
392 g_service->SetScene("cam");
393 g_service->HandleThermalCallbackEvent(event);
394 ThermalLevel level = ThermalLevel::COOL;
395 int32_t expectLevel = 2;
396 g_service->GetThermalLevel(level);
397 EXPECT_EQ(expectLevel, static_cast<int32_t>(level));
398 #ifdef BATTERY_MANAGER_ENABLE
399 int64_t cpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID);
400 auto state = BatterySrvClient::GetInstance().GetChargingStatus();
401 if (state == BatteryChargeState::CHARGE_STATE_ENABLE) {
402 EXPECT_TRUE(cpuLimitValue == 1990800);
403 } else if (state == BatteryChargeState::CHARGE_STATE_NONE) {
404 EXPECT_TRUE(cpuLimitValue == 1990600);
405 }
406 #endif
407 THERMAL_HILOGD(LABEL_TEST, "ThermalMockActionTest007: end");
408 }
409
410 /**
411 * @tc.name: ThermalMockActionTest008
412 * @tc.desc: test get cpu freq by setting temp
413 * @tc.type: FEATURE
414 * @tc.cond: Set BATTERY temp, scene = "cam"
415 * @tc.result level 3
416 * @tc.require: issueI6UI5Q
417 */
418 HWTEST_F (ThermalMockActionTest, ThermalMockActionTest008, Function|MediumTest|Level2)
419 {
420 THERMAL_HILOGD(LABEL_TEST, "ThermalMockActionTest008: start");
421 ASSERT_NE(g_service, nullptr);
422 HdfThermalCallbackInfo event;
423 ThermalZoneInfo info1;
424 info1.type = "battery";
425 info1.temp = 46100;
426 event.info.push_back(info1);
427 g_service->SetScene("cam");
428 g_service->HandleThermalCallbackEvent(event);
429 ThermalLevel level = ThermalLevel::COOL;
430 int32_t expectLevel = 3;
431 g_service->GetThermalLevel(level);
432 EXPECT_EQ(expectLevel, static_cast<int32_t>(level));
433 #ifdef BATTERY_MANAGER_ENABLE
434 int64_t cpuLimitValue = MockSocPerfAction::GetLimitValue(LIM_CPU_BIG_ID);
435 auto state = BatterySrvClient::GetInstance().GetChargingStatus();
436 if (state == BatteryChargeState::CHARGE_STATE_ENABLE) {
437 EXPECT_TRUE(cpuLimitValue == 1989800);
438 } else if (state == BatteryChargeState::CHARGE_STATE_NONE) {
439 EXPECT_TRUE(cpuLimitValue == 1989600);
440 }
441 #endif
442 THERMAL_HILOGD(LABEL_TEST, "ThermalMockActionTest008: end");
443 }
444 }
445