1 /*
2 * Copyright (c) 2022-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_service_test.h"
17 #include "thermal_xcollie.h"
18
19 #ifdef THERMAL_GTEST
20 #define private public
21 #define protected public
22 #define final
23 #endif
24
25 #include <map>
26 #include <string>
27 #include <vector>
28
29 #include "system_ability_definition.h"
30
31 #include "config_policy_utils.h"
32 #include "power_mgr_client.h"
33 #include "thermal_log.h"
34 #include "thermal_service.h"
35
36 using namespace OHOS::PowerMgr;
37 using namespace OHOS;
38 using namespace testing::ext;
39 using namespace std;
40
41 namespace {
42 sptr<ThermalService> g_service = nullptr;
43 constexpr const char* VENDOR_CONFIG = "/vendor/etc/thermal_config/thermal_service_config.xml";
44 constexpr const char* SYSTEM_CONFIG = "/system/etc/thermal_config/thermal_service_config.xml";
45 } // namespace
46
GetOneCfgFile(const char * pathSuffix,char * buf,unsigned int bufLength)47 char* GetOneCfgFile(const char *pathSuffix, char *buf, unsigned int bufLength)
48 {
49 THERMAL_HILOGD(LABEL_TEST, "mock GetOneCfgFile.");
50 return nullptr;
51 }
52
SetUpTestCase()53 void ThermalServiceTest::SetUpTestCase()
54 {
55 g_service = ThermalService::GetInstance();
56 g_service->InitSystemTestModules();
57 g_service->OnStart();
58 }
59
60 namespace {
61 /**
62 * @tc.name: ThermalServiceTest000
63 * @tc.desc: test OnAddSystemAbility
64 * @tc.type: FUNC
65 * @tc.require: issueI6KRS8
66 */
67 HWTEST_F(ThermalServiceTest, ThermalServiceTest000, TestSize.Level0)
68 {
69 THERMAL_HILOGI(LABEL_TEST, "ThermalServiceTest000 function start!");
70 std::string deviceId = "";
71 EXPECT_FALSE(g_service == nullptr);
72 g_service->OnAddSystemAbility(COMMON_EVENT_SERVICE_ID, deviceId);
73 g_service->OnAddSystemAbility(POWER_MANAGER_THERMAL_SERVICE_ID, deviceId);
74 THERMAL_HILOGI(LABEL_TEST, "ThermalServiceTest000 function end!");
75 }
76
77 /**
78 * @tc.name: ThermalServiceTest001
79 * @tc.desc: test OnStart and OnStop
80 * @tc.type: FUNC
81 */
82 HWTEST_F(ThermalServiceTest, ThermalServiceTest001, TestSize.Level0)
83 {
84 THERMAL_HILOGI(LABEL_TEST, "ThermalServiceTest001 function start!");
85 g_service->ready_ = true;
86 g_service->InitSystemTestModules();
87 g_service->OnStart();
88
89 g_service->ready_ = false;
90 g_service->OnStop();
91
92 g_service->ready_ = true;
93 g_service->RegisterHdiStatusListener();
94 g_service->GetThermalInfo();
95 g_service->OnStop();
96 EXPECT_FALSE(g_service->ready_);
97
98 g_service->ready_ = true;
99 g_service->OnStop();
100 EXPECT_FALSE(g_service->ready_);
101
102 THERMAL_HILOGI(LABEL_TEST, "ThermalServiceTest001 function end!");
103 }
104
105 /**
106 * @tc.name: ThermalServiceTest002
107 * @tc.desc: test Init
108 * @tc.type: FUNC
109 */
110 HWTEST_F(ThermalServiceTest, ThermalServiceTest002, TestSize.Level0)
111 {
112 THERMAL_HILOGI(LABEL_TEST, "ThermalServiceTest002 function start!");
113 EXPECT_TRUE(g_service->Init());
114 EXPECT_TRUE(g_service->CreateConfigModule());
115 EXPECT_TRUE(g_service->Init());
116 EXPECT_TRUE(g_service->CreateConfigModule());
117 EXPECT_TRUE(g_service->InitStateMachine());
118 g_service->InitSystemTestModules();
119 THERMAL_HILOGI(LABEL_TEST, "ThermalServiceTest002 function end!");
120 }
121
122 /**
123 * @tc.name: ThermalServiceTest003
124 * @tc.desc: test InitConfigFile
125 * @tc.type: FUNC
126 */
127 HWTEST_F(ThermalServiceTest, ThermalServiceTest003, TestSize.Level0)
128 {
129 THERMAL_HILOGI(LABEL_TEST, "ThermalServiceTest003 function start!");
130
131 std::string VENDOR_CONFIG_BACKUP = "/vendor/etc/thermal_config/thermal_service_config_backup.xml";
132 std::string SYSTEM_CONFIG_BACKUP = "/system/etc/thermal_config/thermal_service_config_backup.xml";
133
134 int32_t ret = rename(VENDOR_CONFIG_BACKUP.c_str(), VENDOR_CONFIG);
135 EXPECT_NE(ret, ERR_OK);
136 ret = rename(SYSTEM_CONFIG_BACKUP.c_str(), SYSTEM_CONFIG);
137 EXPECT_NE(ret, ERR_OK);
138
139 THERMAL_HILOGI(LABEL_TEST, "ThermalServiceTest003 function end!");
140 }
141
142 /**
143 * @tc.name: ThermalServiceTest004
144 * @tc.desc: test Service Dump
145 * @tc.type: FUNC
146 */
147 HWTEST_F(ThermalServiceTest, ThermalServiceTest004, TestSize.Level0)
148 {
149 THERMAL_HILOGI(LABEL_TEST, "ThermalServiceTest004 function start!");
150 ThermalXCollieTestable thermalXCollie("test_log", true);
151 int fd = 0;
152 std::vector<std::u16string> args;
153 args.push_back(u"-h");
154 g_service->isBootCompleted_ = true;
155 EXPECT_EQ(ERR_OK, g_service->Dump(fd, args));
156
157 fd = -1;
158 EXPECT_EQ(ERR_OK, g_service->Dump(fd, args));
159
160 thermalXCollie.CallCancel();
161 thermalXCollie.CallCancel();
162 THERMAL_HILOGI(LABEL_TEST, "ThermalServiceTest004 function end!");
163 }
164 } // namespace