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
35 using namespace OHOS::PowerMgr;
36 using namespace OHOS;
37 using namespace testing::ext;
38 using namespace std;
39
40 namespace {
41 sptr<ThermalService> g_service = nullptr;
42 constexpr const char* VENDOR_CONFIG = "/vendor/etc/thermal_config/thermal_service_config.xml";
43 constexpr const char* SYSTEM_CONFIG = "/system/etc/thermal_config/thermal_service_config.xml";
44 } // namespace
45
GetOneCfgFile(const char * pathSuffix,char * buf,unsigned int bufLength)46 char* GetOneCfgFile(const char *pathSuffix, char *buf, unsigned int bufLength)
47 {
48 THERMAL_HILOGD(LABEL_TEST, "mock GetOneCfgFile.");
49 return nullptr;
50 }
51
SetUpTestCase()52 void ThermalServiceTest::SetUpTestCase()
53 {
54 g_service = ThermalService::GetInstance();
55 g_service->InitSystemTestModules();
56 g_service->OnStart();
57 }
58
59 namespace {
60 /**
61 * @tc.name: ThermalServiceTest000
62 * @tc.desc: test OnAddSystemAbility
63 * @tc.type: FUNC
64 * @tc.require: issueI6KRS8
65 */
66 HWTEST_F(ThermalServiceTest, ThermalServiceTest000, TestSize.Level0)
67 {
68 THERMAL_HILOGD(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_HILOGD(LABEL_TEST, "ThermalServiceTest000 end.");
74 }
75
76 /**
77 * @tc.name: ThermalServiceTest001
78 * @tc.desc: test OnStart and OnStop
79 * @tc.type: FUNC
80 */
81 HWTEST_F(ThermalServiceTest, ThermalServiceTest001, TestSize.Level0)
82 {
83 THERMAL_HILOGD(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_HILOGD(LABEL_TEST, "ThermalServiceTest001 end.");
102 }
103
104 /**
105 * @tc.name: ThermalServiceTest002
106 * @tc.desc: test Init
107 * @tc.type: FUNC
108 */
109 HWTEST_F(ThermalServiceTest, ThermalServiceTest002, TestSize.Level0)
110 {
111 THERMAL_HILOGD(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_HILOGD(LABEL_TEST, "ThermalServiceTest002 end.");
119 }
120
121 /**
122 * @tc.name: ThermalServiceTest003
123 * @tc.desc: test InitConfigFile
124 * @tc.type: FUNC
125 */
126 HWTEST_F(ThermalServiceTest, ThermalServiceTest003, TestSize.Level0)
127 {
128 THERMAL_HILOGD(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_HILOGD(LABEL_TEST, "ThermalServiceTest003 end.");
139 }
140
141 /**
142 * @tc.name: ThermalServiceTest004
143 * @tc.desc: test Service Dump
144 * @tc.type: FUNC
145 */
146 HWTEST_F(ThermalServiceTest, ThermalServiceTest004, TestSize.Level0)
147 {
148 THERMAL_HILOGD(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_HILOGD(LABEL_TEST, "ThermalServiceTest004 end.");
160 }
161 } // namespace
162