1 /*
2  * Copyright (c) 2021-2024 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_action_test.h"
17 
18 #ifdef THERMAL_GTEST
19 #define private   public
20 #endif
21 
22 #include <memory>
23 #include <singleton.h>
24 #include "action_airplane.h"
25 #include "action_application_process.h"
26 #include "action_charger.h"
27 #include "action_cpu_big.h"
28 #include "action_cpu_med.h"
29 #include "action_cpu_lit.h"
30 #include "action_cpu_isolate.h"
31 #include "action_display.h"
32 #include "action_gpu.h"
33 #include "action_node.h"
34 #include "action_shutdown.h"
35 #include "action_thermal_level.h"
36 #include "action_popup.h"
37 #include "action_volume.h"
38 #include "action_voltage.h"
39 #include "file_operation.h"
40 #include "iremote_object.h"
41 #include "mock_thermal_remote_object.h"
42 #include "thermal_service.h"
43 #include "thermal_timer.h"
44 
45 using namespace testing::ext;
46 using namespace OHOS::PowerMgr;
47 using namespace OHOS;
48 using namespace std;
49 
50 namespace {
51 constexpr int32_t BUF_LEN = 32;
52 static sptr<ThermalService> g_service = nullptr;
53 std::shared_ptr<ActionApplicationProcess> g_actionApplicationProcess =
54     std::make_shared<ActionApplicationProcess>("process_ctrl");
55 std::shared_ptr<ActionAirplane> g_actionAirplane = std::make_shared<ActionAirplane>("airplane");
56 std::shared_ptr<ActionCharger> g_actionCharger = std::make_shared<ActionCharger>("current_sc");
57 std::shared_ptr<ActionCpuBig> g_actionCpuBig = std::make_shared<ActionCpuBig>("cpu_big");
58 std::shared_ptr<ActionCpuMed> g_actionCpuMed = std::make_shared<ActionCpuMed>("cpu_med");
59 std::shared_ptr<ActionCpuLit> g_actionCpuLit = std::make_shared<ActionCpuLit>("cpu_lit");
60 std::shared_ptr<ActionCpuIsolate> g_actionCpuIsolate = std::make_shared<ActionCpuIsolate>("isolate");
61 std::shared_ptr<ActionDisplay> g_actionDisplay = std::make_shared<ActionDisplay>("lcd");
62 std::shared_ptr<ActionGpu> g_actionGpu = std::make_shared<ActionGpu>("gpu");
63 std::shared_ptr<ActionPopup> g_actionPopup = std::make_shared<ActionPopup>("popup");
64 std::shared_ptr<ActionShutdown> g_actionShutdown = std::make_shared<ActionShutdown>("shut_down");
65 std::shared_ptr<ActionThermalLevel> g_actionThermalLevel = std::make_shared<ActionThermalLevel>("thermallevel");
66 std::shared_ptr<ActionVolume> g_actionVolume = std::make_shared<ActionVolume>("volume");
67 std::shared_ptr<ActionVoltage> g_actionVoltage = std::make_shared<ActionVoltage>("voltage");
68 std::shared_ptr<ActionNode> g_actionNode = std::make_shared<ActionNode>("action_node");
69 }
70 
SetUpTestCase()71 void ThermalActionTest::SetUpTestCase()
72 {
73     g_service = ThermalService::GetInstance();
74     g_service->InitSystemTestModules();
75     g_service->OnStart();
76 }
77 
TearDownTestCase()78 void ThermalActionTest::TearDownTestCase()
79 {
80     g_service->OnStop();
81 }
82 
SetUp()83 void ThermalActionTest::SetUp() {}
84 
TearDown()85 void ThermalActionTest::TearDown() {}
86 
87 namespace {
88 /**
89  * @tc.name: ThermalActionTest001
90  * @tc.desc: Action Volume Function Test
91  * @tc.type: FUNC
92  */
HWTEST_F(ThermalActionTest, ThermalActionTest001, TestSize.Level0)93 HWTEST_F(ThermalActionTest, ThermalActionTest001, TestSize.Level0)
94 {
95     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest001 start");
96     g_actionVolume->InitParams("volume");
97     g_actionVolume->SetEnableEvent(false);
98     g_actionVolume->AddActionValue("");
99     g_actionVolume->AddActionValue("1.0");
100     g_actionVolume->Execute();
101     g_actionVolume->SetStrict(true);
102     g_actionVolume->AddActionValue("2.0");
103     g_actionVolume->GetActionValue();
104     g_actionVolume->VolumeRequest(1.0);
105     int32_t ret = g_actionVolume->VolumeExecution(1.0);
106     EXPECT_TRUE(ret == ERR_OK);
107     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest001 end");
108 }
109 
110 /**
111  * @tc.name: ThermalActionTest002
112  * @tc.desc: Action Voltage Function Test
113  * @tc.type: FUNC
114  */
HWTEST_F(ThermalActionTest, ThermalActionTest002, TestSize.Level0)115 HWTEST_F(ThermalActionTest, ThermalActionTest002, TestSize.Level0)
116 {
117     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest002 start");
118     g_actionVoltage->AddActionValue("");
119     g_actionVoltage->AddActionValue("1.0");
120     g_actionVoltage->Execute();
121     g_actionVoltage->SetStrict(true);
122     g_actionVoltage->AddActionValue("2.0");
123     g_actionVoltage->GetActionValue();
124     g_actionVoltage->SetVoltage(123456);
125     g_actionVoltage->ExecuteVoltageLimit();
126     int32_t ret = g_actionVoltage->WriteMockNode(123456);
127     EXPECT_FALSE(ret == ERR_OK);
128     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest002 end");
129 }
130 
131 /**
132  * @tc.name: ThermalActionTest003
133  * @tc.desc: Action Thermal Level Function Test
134  * @tc.type: FUNC
135  */
HWTEST_F(ThermalActionTest, ThermalActionTest003, TestSize.Level0)136 HWTEST_F(ThermalActionTest, ThermalActionTest003, TestSize.Level0)
137 {
138     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest003 start");
139     g_actionThermalLevel->AddActionValue("");
140     g_actionThermalLevel->AddActionValue("1.0");
141     g_actionThermalLevel->Execute();
142     g_actionThermalLevel->GetThermalLevel();
143     g_actionThermalLevel->LevelRequest(1);
144     g_actionThermalLevel->LevelRequest(9);
145     g_actionThermalLevel->LevelRequest(-1);
146     g_actionThermalLevel->SetStrict(true);
147     g_actionThermalLevel->AddActionValue("2.0");
148     g_actionThermalLevel->GetActionValue();
149     g_actionThermalLevel->SubscribeThermalLevelCallback(nullptr);
150     g_actionThermalLevel->UnSubscribeThermalLevelCallback(nullptr);
151     std::shared_ptr<IRemoteObject::DeathRecipient> deathRecipient =
152         std::make_shared<ActionThermalLevel::ThermalLevelCallbackDeathRecipient>();
153     wptr<IRemoteObject> remoteObj = nullptr;
154     EXPECT_NE(deathRecipient, nullptr);
155     deathRecipient->OnRemoteDied(remoteObj);
156 
157     sptr<IRemoteObject> sptrRemoteObj = new MockThermalRemoteObject();
158     EXPECT_FALSE(sptrRemoteObj == nullptr);
159     deathRecipient->OnRemoteDied(sptrRemoteObj);
160 
161     g_actionThermalLevel->NotifyThermalLevelChanged(1);
162     bool ret = g_actionThermalLevel->
163         PublishLevelChangedEvents(ThermalCommonEventCode::CODE_THERMAL_LEVEL_CHANGED, 1);
164     EXPECT_TRUE(ret);
165     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest003 end");
166 }
167 
168 /**
169  * @tc.name: ThermalActionTest004
170  * @tc.desc: Action Shutdown Function Test
171  * @tc.type: FUNC
172  */
HWTEST_F(ThermalActionTest, ThermalActionTest004, TestSize.Level0)173 HWTEST_F(ThermalActionTest, ThermalActionTest004, TestSize.Level0)
174 {
175     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest004 start");
176     g_actionShutdown->AddActionValue("");
177     g_actionShutdown->AddActionValue("1.0");
178     g_actionShutdown->Execute();
179     g_actionShutdown->SetStrict(true);
180     g_actionShutdown->AddActionValue("2.0");
181     g_actionShutdown->GetActionValue();
182     int32_t ret = g_actionShutdown->ShutdownRequest(false);
183     EXPECT_TRUE(ret == ERR_OK);
184     g_actionShutdown->ShutdownExecution(false);
185     g_actionShutdown->ShutdownExecution(false);
186     ret = g_actionShutdown->DelayShutdown(false, 0, 0);
187     EXPECT_TRUE(ret == ERR_OK);
188     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest004 end");
189 }
190 
191 /**
192  * @tc.name: ThermalActionTest005
193  * @tc.desc: Action Display Function Test
194  * @tc.type: FUNC
195  * @tc.require: issueI6KRS8
196  */
HWTEST_F(ThermalActionTest, ThermalActionTest005, TestSize.Level0)197 HWTEST_F(ThermalActionTest, ThermalActionTest005, TestSize.Level0)
198 {
199     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest005 start");
200     g_actionDisplay->AddActionValue("");
201     g_actionDisplay->AddActionValue("1.0");
202     g_actionDisplay->Execute();
203     g_actionDisplay->SetStrict(true);
204     g_actionDisplay->AddActionValue("2.0");
205     g_actionDisplay->GetActionValue();
206     EXPECT_FALSE(g_actionDisplay->valueList_.empty());
207     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest005 end");
208 }
209 
210 /**
211  * @tc.name: ThermalActionTest006
212  * @tc.desc: Action Charger Function Test
213  * @tc.type: FUNC
214  */
HWTEST_F(ThermalActionTest, ThermalActionTest006, TestSize.Level0)215 HWTEST_F(ThermalActionTest, ThermalActionTest006, TestSize.Level0)
216 {
217     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest006 start");
218     g_actionCharger->AddActionValue("");
219     g_actionCharger->AddActionValue("1.0");
220     g_actionCharger->Execute();
221     g_actionCharger->ChargerRequest(0);
222     g_actionCharger->ExecuteCurrentLimit();
223     int32_t ret = g_actionCharger->WriteSimValue(0);
224     EXPECT_TRUE(ret);
225     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest006 end");
226 }
227 
228 /**
229  * @tc.name: ThermalActionTest007
230  * @tc.desc: Action Application Process Test
231  * @tc.type: FUNC
232  */
HWTEST_F(ThermalActionTest, ThermalActionTest007, TestSize.Level0)233 HWTEST_F(ThermalActionTest, ThermalActionTest007, TestSize.Level0)
234 {
235     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest007 start");
236     g_actionApplicationProcess->InitParams("");
237     g_actionApplicationProcess->AddActionValue("");
238     g_actionApplicationProcess->AddActionValue("1");
239     g_actionApplicationProcess->Execute();
240     g_actionApplicationProcess->KillApplicationAction("");
241     g_actionApplicationProcess->KillProcess(0);
242     g_actionApplicationProcess->GetAppProcessInfoByName("");
243     g_actionApplicationProcess->GetAllRunnningAppProcess();
244     g_actionApplicationProcess->KillBgAppProcess();
245     g_actionApplicationProcess->KillAllAppProcess();
246     g_actionApplicationProcess->ProcessAppActionRequest(0);
247     g_actionApplicationProcess->ProcessAppActionRequest(1);
248     g_actionApplicationProcess->ProcessAppActionRequest(2);
249     g_actionApplicationProcess->ProcessAppActionRequest(3);
250     g_actionApplicationProcess->ProcessAppActionExecution(0);
251     EXPECT_TRUE(g_actionApplicationProcess->valueList_.empty());
252     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest007 end");
253 }
254 
255 /**
256  * @tc.name: ThermalActionTest008
257  * @tc.desc: Action CPU Big Test
258  * @tc.type: FUNC
259  * @tc.require: issueI6KRS8
260  */
HWTEST_F(ThermalActionTest, ThermalActionTest008, TestSize.Level0)261 HWTEST_F(ThermalActionTest, ThermalActionTest008, TestSize.Level0)
262 {
263     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest008 start");
264     g_actionCpuBig->AddActionValue("");
265     g_actionCpuBig->AddActionValue("1.0");
266     g_actionCpuBig->Execute();
267     EXPECT_TRUE(g_actionCpuBig->valueList_.empty());
268     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest008 end");
269 }
270 
271 /**
272  * @tc.name: ThermalActionTest009
273  * @tc.desc: Action CPU Med Test
274  * @tc.type: FUNC
275  * @tc.require: issueI6KRS8
276  */
HWTEST_F(ThermalActionTest, ThermalActionTest009, TestSize.Level0)277 HWTEST_F(ThermalActionTest, ThermalActionTest009, TestSize.Level0)
278 {
279     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest009 start");
280     g_actionCpuMed->AddActionValue("");
281     g_actionCpuMed->AddActionValue("1.0");
282     g_actionCpuMed->Execute();
283     EXPECT_TRUE(g_actionCpuMed->valueList_.empty());
284     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest009 end");
285 }
286 
287 /**
288  * @tc.name: ThermalActionTest010
289  * @tc.desc: Action CPU Lit Test
290  * @tc.type: FUNC
291  * @tc.require: issueI6KRS8
292  */
HWTEST_F(ThermalActionTest, ThermalActionTest010, TestSize.Level0)293 HWTEST_F(ThermalActionTest, ThermalActionTest010, TestSize.Level0)
294 {
295     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest010 start");
296     g_actionCpuLit->AddActionValue("");
297     g_actionCpuLit->AddActionValue("1.0");
298     g_actionCpuLit->Execute();
299     EXPECT_TRUE(g_actionCpuLit->valueList_.empty());
300     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest010 end");
301 }
302 
303 /**
304  * @tc.name: ThermalActionTest011
305  * @tc.desc: Action GPU Test
306  * @tc.type: FUNC
307  * @tc.require: issueI6KRS8
308  */
HWTEST_F(ThermalActionTest, ThermalActionTest011, TestSize.Level0)309 HWTEST_F(ThermalActionTest, ThermalActionTest011, TestSize.Level0)
310 {
311     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest011 start");
312     g_actionGpu->AddActionValue("");
313     g_actionGpu->AddActionValue("1.0");
314     g_actionGpu->Execute();
315     EXPECT_TRUE(g_actionGpu->valueList_.empty());
316     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest011 end");
317 }
318 
319 /**
320  * @tc.name: ThermalActionTest012
321  * @tc.desc: Action Isolate CPU Test
322  * @tc.type: FUNC
323   */
HWTEST_F(ThermalActionTest, ThermalActionTest012, TestSize.Level0)324 HWTEST_F(ThermalActionTest, ThermalActionTest012, TestSize.Level0)
325 {
326     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest012 start");
327     g_actionCpuIsolate->AddActionValue("");
328     g_actionCpuIsolate->AddActionValue("1.0");
329     g_actionCpuIsolate->Execute();
330     g_actionCpuIsolate->SetStrict(true);
331     g_actionCpuIsolate->AddActionValue("2.0");
332     g_actionCpuIsolate->GetActionValue();
333     EXPECT_FALSE(g_actionCpuIsolate->valueList_.empty());
334     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest012 end");
335 }
336 
337 /**
338  * @tc.name: ThermalActionTest013
339  * @tc.desc: Action node Test
340  * @tc.type: FUNC
341   */
HWTEST_F(ThermalActionTest, ThermalActionTest013, TestSize.Level0)342 HWTEST_F(ThermalActionTest, ThermalActionTest013, TestSize.Level0)
343 {
344     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest013 start");
345     std::string input = "1";
346     g_actionNode->InitParams("/data/service/el0/thermal/config/lcd");
347     g_actionNode->AddActionValue(input);
348     g_actionNode->Execute();
349     char buf[BUF_LEN];
350     FileOperation::ReadFile("/data/service/el0/thermal/config/lcd", buf, BUF_LEN);
351     std::string ret = buf;
352     EXPECT_EQ(input, ret);
353     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest013 end");
354 }
355 
356 /**
357  * @tc.name: ThermalActionTest014
358  * @tc.desc: Action Airplane Function Test
359  * @tc.type: FUNC
360  */
HWTEST_F(ThermalActionTest, ThermalActionTest014, TestSize.Level0)361 HWTEST_F(ThermalActionTest, ThermalActionTest014, TestSize.Level0)
362 {
363     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest014 start");
364     g_actionAirplane->InitParams("airplane");
365     g_actionAirplane->AddActionValue("");
366     g_actionAirplane->AddActionValue("0");
367     g_actionAirplane->AddActionValue("air");
368     g_actionAirplane->Execute();
369     EXPECT_TRUE(g_actionAirplane->valueList_.empty());
370     std::string input = "1";
371     g_actionAirplane->AddActionValue(input);
372     g_actionCpuIsolate->SetStrict(true);
373     uint32_t value = g_actionAirplane->GetActionValue();
374     int32_t ret = g_actionAirplane->AirplaneRequest(value);
375     g_actionAirplane->AirplaneRequest(0);
376     g_actionAirplane->AirplaneRequest(1);
377     EXPECT_TRUE(ret == ERR_OK);
378     ret = g_actionAirplane->AirplaneExecution(value);
379     EXPECT_TRUE(ret == ERR_OK);
380     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest014 end");
381 }
382 
383 /**
384  * @tc.name: ThermalActionTest015
385  * @tc.desc: Action Timer Test
386  * @tc.type: FUNC
387  */
HWTEST_F(ThermalActionTest, ThermalActionTest015, TestSize.Level0)388 HWTEST_F(ThermalActionTest, ThermalActionTest015, TestSize.Level0)
389 {
390     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest015 start");
391     auto timerInfo = std::make_shared<ThermalTimerInfo>();
392     ASSERT_NE(timerInfo, nullptr);
393     OHOS::PowerMgr::ThermalTimerInfo::TimerInfoCallback callback;
394     timerInfo->SetCallbackInfo(callback);
395     timerInfo->OnTrigger();
396     auto timer = std::make_shared<ThermalTimer>();
397     ASSERT_NE(timer, nullptr);
398     uint64_t triggerTime = 1;
399     std::shared_ptr<ThermalTimerInfo> info;
400     uint64_t timerId = timer->CreateTimer(info);
401     EXPECT_EQ(timerId, 0);
402     timer->StartTimer(timerId, triggerTime);
403     timer->DestroyTimer(timerId);
404     timer->StopTimer(timerId);
405     auto timerUtils = std::make_shared<ThermalTimerUtils>();
406     ASSERT_NE(timerUtils, nullptr);
407     timerUtils->Stop();
408     int delay = 1;
409     OHOS::PowerMgr::ThermalTimerUtils::NotifyTask task;
410     timerUtils->Start(delay, task);
411     timerUtils->Start(delay, task);
412     timerUtils->Stop();
413     timerUtils->Stop();
414     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest015 end");
415 }
416 
417 /**
418  * @tc.name: ThermalActionTest016
419  * @tc.desc: Action Popup Test
420  * @tc.type: FUNC
421  */
HWTEST_F(ThermalActionTest, ThermalActionTest016, TestSize.Level0)422 HWTEST_F(ThermalActionTest, ThermalActionTest016, TestSize.Level0)
423 {
424     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest016 start");
425     g_actionPopup->AddActionValue("pop");
426     g_actionPopup->Execute();
427     EXPECT_TRUE(g_actionPopup->valueList_.empty());
428     THERMAL_HILOGD(LABEL_TEST, "ThermalActionTest016 end");
429 }
430 } // namespace
431