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