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