1 /*
2 * Copyright (c) 2022 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 "battery_service_test.h"
17
18 #include <memory>
19 #include <string>
20 #include <fcntl.h>
21
22 #include "battery_info.h"
23 #include "battery_log.h"
24 #include "battery_service.h"
25 #include "common_event_data.h"
26 #include "common_event_subscriber.h"
27 #include "common_event_support.h"
28 #include "securec.h"
29 #include "test_utils.h"
30
31 using namespace testing::ext;
32 using namespace OHOS::PowerMgr;
33 using namespace OHOS::EventFwk;
34 using namespace OHOS::HDI::Battery;
35 using namespace OHOS;
36 using namespace std;
37
38 namespace {
39 sptr<BatteryService> g_service;
40 bool g_isMock = TestUtils::IsMock();
41 const std::string MOCK_BATTERY_PATH = "/data/service/el0/battery/";
42 }
43
SetUpTestCase()44 void BatteryServiceTest::SetUpTestCase()
45 {
46 g_service = DelayedSpSingleton<BatteryService>::GetInstance();
47 g_service->OnStart();
48 }
49
50 /**
51 * @tc.name: BatteryService001
52 * @tc.desc: Test functions RegisterBatteryHdiCallback
53 * @tc.type: FUNC
54 * @tc.require: issueI5YZR1
55 */
56 static HWTEST_F(BatteryServiceTest, BatteryService001, TestSize.Level1)
57 {
58 BATTERY_HILOGD(LABEL_TEST, "BatteryService001 start.");
59 EXPECT_TRUE(g_service->RegisterBatteryHdiCallback());
60 BATTERY_HILOGD(LABEL_TEST, "BatteryService001 end.");
61 }
62
63 /**
64 * @tc.name: BatteryService003
65 * @tc.desc: Test functions DestoryBatteryDialog
66 * @tc.type: FUNC
67 * @tc.require: issueI5YZR1
68 */
69 static HWTEST_F(BatteryServiceTest, BatteryService003, TestSize.Level1)
70 {
71 BATTERY_HILOGD(LABEL_TEST, "BatteryService003 start.");
72 EXPECT_TRUE(g_service->DestoryBatteryDialog());
73 BATTERY_HILOGD(LABEL_TEST, "BatteryService003 end.");
74 }
75
76 /**
77 * @tc.name: BatteryService004
78 * @tc.desc: Test functions GetVoltage
79 * @tc.type: FUNC
80 * @tc.require: issueI5YZR1
81 */
82 static HWTEST_F(BatteryServiceTest, BatteryService004, TestSize.Level1)
83 {
84 BATTERY_HILOGD(LABEL_TEST, "BatteryService004 start.");
85 if (g_isMock) {
86 auto tempVoltage = g_service->GetVoltage();
87 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/voltage_now", "4654321");
88 auto voltage = g_service->GetVoltage();
89 ASSERT_TRUE(voltage == 4654321);
90 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/voltage_now", std::to_string(tempVoltage));
91 } else {
92 auto voltage = g_service->GetVoltage();
93 BATTERY_HILOGI(LABEL_TEST, "BatteryService004::voltage=%{public}d", voltage);
94 GTEST_LOG_(INFO) << "BatteryService004 executing, voltage=" << voltage;
95 ASSERT_TRUE(voltage >= 0);
96 }
97
98 BATTERY_HILOGD(LABEL_TEST, "BatteryService004 end.");
99 }
100
101 /**
102 * @tc.name: BatteryService005
103 * @tc.desc: Test functions GetPresent
104 * @tc.type: FUNC
105 * @tc.require: issueI5YZR1
106 */
107 static HWTEST_F(BatteryServiceTest, BatteryService005, TestSize.Level1)
108 {
109 BATTERY_HILOGD(LABEL_TEST, "BatteryService005 start.");
110 if (g_isMock) {
111 auto tempPresent = g_service->GetPresent();
112 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/present", "0");
113 auto present = g_service->GetPresent();
114 BATTERY_HILOGI(LABEL_TEST, "BatteryService005::present=%{public}d", present);
115 GTEST_LOG_(INFO) << "BatteryService005 executing, present=" << present;
116 ASSERT_FALSE(present);
117 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/present", std::to_string(tempPresent));
118 } else {
119 auto present = g_service->GetPresent();
120 BATTERY_HILOGI(LABEL_TEST, "BatteryService005::present=%{public}d", present);
121 GTEST_LOG_(INFO) << "BatteryService005 executing, present=" << present;
122 ASSERT_TRUE(present);
123 }
124 BATTERY_HILOGD(LABEL_TEST, "BatteryService005 end.");
125 }
126
127 /**
128 * @tc.name: BatteryService006
129 * @tc.desc: Test functions GetBatteryTemperature
130 * @tc.type: FUNC
131 * @tc.require: issueI5YZR1
132 */
133 static HWTEST_F(BatteryServiceTest, BatteryService006, TestSize.Level1)
134 {
135 BATTERY_HILOGD(LABEL_TEST, "BatteryService006 start.");
136 if (g_isMock) {
137 auto tempTempPresent = g_service->GetBatteryTemperature();
138 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/temp", "222");
139 auto temperature = g_service->GetBatteryTemperature();
140 BATTERY_HILOGI(LABEL_TEST, "BatteryService006::temperature=%{public}d", temperature);
141 GTEST_LOG_(INFO) << "BatteryService006 executing, temperature=" << temperature;
142 ASSERT_TRUE(temperature == 222);
143 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/temp", std::to_string(tempTempPresent));
144 } else {
145 auto temperature = g_service->GetBatteryTemperature();
146 BATTERY_HILOGI(LABEL_TEST, "BatteryService006::temperature=%{public}d", temperature);
147 GTEST_LOG_(INFO) << "BatteryService006 executing, temperature=" << temperature;
148 ASSERT_TRUE(temperature >= 0 && temperature <= 600);
149 }
150 BATTERY_HILOGD(LABEL_TEST, "BatteryService006 end.");
151 }
152
153 /**
154 * @tc.name: BatteryService012
155 * @tc.desc: Test functions RegisterHdiStatusListener
156 * @tc.type: FUNC
157 * @tc.require: issueI5YZR1
158 */
159 static HWTEST_F(BatteryServiceTest, BatteryService007, TestSize.Level1)
160 {
161 BATTERY_HILOGD(LABEL_TEST, "BatteryService007 start.");
162 EXPECT_TRUE(g_service->RegisterHdiStatusListener());
163 BATTERY_HILOGD(LABEL_TEST, "BatteryService007 end.");
164 }
165
166 /**
167 * @tc.name: BatteryService008
168 * @tc.desc: Test functions OnStart and OnStop and OnAddSystemAbility
169 * @tc.type: FUNC
170 * @tc.require: issueI5YZR1
171 */
172 static HWTEST_F(BatteryServiceTest, BatteryService008, TestSize.Level1)
173 {
174 BATTERY_HILOGD(LABEL_TEST, "BatteryService008 start.");
175 g_service->OnStop();
176 g_service->OnStop();
177 g_service->OnStart();
178 g_service->OnStart();
179 int32_t systemAbilityId = 3602; // MISCDEVICE_SERVICE_ABILITY_ID
180 g_service->OnAddSystemAbility(systemAbilityId, std::string("BatteryService008"));
181 BATTERY_HILOGD(LABEL_TEST, "BatteryService008 end.");
182 }
183
184 /**
185 * @tc.name: BatteryService009
186 * @tc.desc: Test functions MockUnplugged, with parameter is false
187 * @tc.type: FUNC
188 * @tc.require: issueI5YZR1
189 */
190 static HWTEST_F(BatteryServiceTest, BatteryService009, TestSize.Level1)
191 {
192 BATTERY_HILOGD(LABEL_TEST, "BatteryService009 start.");
193 g_service->MockUnplugged(false);
194 EXPECT_EQ(g_service->IsMockUnplugged(), false);
195 BATTERY_HILOGD(LABEL_TEST, "BatteryService009 end.");
196 }
197
198 /**
199 * @tc.name: BatteryService010
200 * @tc.desc: Test functions GetRemainEnergy
201 * @tc.type: FUNC
202 * @tc.require: issueI5YZR1
203 */
204 static HWTEST_F(BatteryServiceTest, BatteryService010, TestSize.Level1)
205 {
206 BATTERY_HILOGD(LABEL_TEST, "BatteryService010 start.");
207 EXPECT_NE(g_service->GetRemainEnergy(), ERR_NO_INIT);
208 BATTERY_HILOGD(LABEL_TEST, "BatteryService010 end.");
209 }
210
211 /**
212 * @tc.name: BatteryService011
213 * @tc.desc: Test functions GetNowCurrent
214 * @tc.type: FUNC
215 * @tc.require: issueI5YZR1
216 */
217 static HWTEST_F(BatteryServiceTest, BatteryService011, TestSize.Level1)
218 {
219 BATTERY_HILOGD(LABEL_TEST, "BatteryService011 start.");
220 EXPECT_NE(g_service->GetNowCurrent(), ERR_NO_INIT);
221 BATTERY_HILOGD(LABEL_TEST, "BatteryService011 end.");
222 }
223
224 /**
225 * @tc.name: BatteryService012
226 * @tc.desc: Test functions GetTechnology
227 * @tc.type: FUNC
228 * @tc.require: issueI5YZR1
229 */
230 static HWTEST_F(BatteryServiceTest, BatteryService012, TestSize.Level1)
231 {
232 BATTERY_HILOGD(LABEL_TEST, "BatteryService012 start.");
233 EXPECT_FALSE(g_service->GetTechnology().empty());
234 BATTERY_HILOGD(LABEL_TEST, "BatteryService012 end.");
235 }
236
237 /**
238 * @tc.name: BatteryService013
239 * @tc.desc: Test functions GetPluggedType
240 * @tc.type: FUNC
241 * @tc.require: issueI5YZR1
242 */
243 static HWTEST_F(BatteryServiceTest, BatteryService013, TestSize.Level1)
244 {
245 BATTERY_HILOGD(LABEL_TEST, "BatteryService013 start.");
246 BatteryPluggedType pluggedType = g_service->GetPluggedType();
247 EXPECT_TRUE(
248 pluggedType >= BatteryPluggedType::PLUGGED_TYPE_NONE && pluggedType <= BatteryPluggedType::PLUGGED_TYPE_BUTT);
249 BATTERY_HILOGD(LABEL_TEST, "BatteryService013 end.");
250 }
251
252 /**
253 * @tc.name: BatteryService014
254 * @tc.desc: Test functions GetCurrentAverage
255 * @tc.type: FUNC
256 * @tc.require: issueI5YZR1
257 */
258 static HWTEST_F(BatteryServiceTest, BatteryService014, TestSize.Level1)
259 {
260 BATTERY_HILOGD(LABEL_TEST, "BatteryService014 start.");
261 EXPECT_NE(g_service->GetCurrentAverage(), ERR_NO_INIT);
262 BATTERY_HILOGD(LABEL_TEST, "BatteryService014 end.");
263 }
264
265 /**
266 * @tc.name: BatteryService015
267 * @tc.desc: Test functions GetHealthStatus
268 * @tc.type: FUNC
269 * @tc.require: issueI5YZR1
270 */
271 static HWTEST_F(BatteryServiceTest, BatteryService015, TestSize.Level1)
272 {
273 BATTERY_HILOGD(LABEL_TEST, "BatteryService015 start.");
274 BatteryHealthState healthState = g_service->GetHealthStatus();
275 EXPECT_TRUE(healthState >= BatteryHealthState::HEALTH_STATE_UNKNOWN &&
276 healthState <= BatteryHealthState::HEALTH_STATE_BUTT);
277 BATTERY_HILOGD(LABEL_TEST, "BatteryService015 end.");
278 }
279
280 /**
281 * @tc.name: BatteryService016
282 * @tc.desc: Test functions GetChargingStatus
283 * @tc.type: FUNC
284 * @tc.require: issueI5YZR1
285 */
286 static HWTEST_F(BatteryServiceTest, BatteryService016, TestSize.Level1)
287 {
288 BATTERY_HILOGD(LABEL_TEST, "BatteryService016 start.");
289 BatteryChargeState chargeState = g_service->GetChargingStatus();
290 EXPECT_TRUE(
291 chargeState >= BatteryChargeState::CHARGE_STATE_NONE && chargeState <= BatteryChargeState::CHARGE_STATE_BUTT);
292 BATTERY_HILOGD(LABEL_TEST, "BatteryService016 end.");
293 }
294
295 /**
296 * @tc.name: BatteryService017
297 * @tc.desc: Test functions GetTotalEnergy
298 * @tc.type: FUNC
299 * @tc.require: issueI5YZR1
300 */
301 static HWTEST_F(BatteryServiceTest, BatteryService017, TestSize.Level1)
302 {
303 BATTERY_HILOGD(LABEL_TEST, "BatteryService017 start.");
304 EXPECT_NE(g_service->GetTotalEnergy(), ERR_NO_INIT);
305 BATTERY_HILOGD(LABEL_TEST, "BatteryService017 end.");
306 }
307