• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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_sttest.h"
17 
18 #include <csignal>
19 #include <iostream>
20 #include <fstream>
21 #include <memory>
22 #include <mutex>
23 #include <streambuf>
24 #include <cstring>
25 #include <thread>
26 #include <vector>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <sys/types.h>
30 #include <dirent.h>
31 
32 #include "battery_log.h"
33 #include "battery_srv_client.h"
34 
35 using namespace testing::ext;
36 using namespace OHOS;
37 using namespace OHOS::PowerMgr;
38 using namespace std;
39 
40 namespace {
41 const std::string MOCK_BATTERY_PATH = "/data/service/el0/battery/";
42 bool g_isMock = false;
43 }
44 
SetUpTestCase(void)45 void BatterySttest::SetUpTestCase(void)
46 {
47     g_isMock = TestUtils::IsMock();
48     GTEST_LOG_(INFO) << " g_isMock: " << g_isMock;
49 }
50 
TearDownTestCase(void)51 void BatterySttest::TearDownTestCase(void)
52 {
53     g_isMock = false;
54     TestUtils::ResetOnline();
55 }
56 
57 namespace {
58 /**
59  * @tc.name: BatteryST001
60  * @tc.desc: Test IBatterySrv interface GetCapacity
61  * @tc.type: FUNC
62  * @tc.require: issueI6KRS8
63  */
64 HWTEST_F (BatterySttest, BatteryST001, TestSize.Level1)
65 {
66     BATTERY_HILOGD(LABEL_TEST, "BatteryST001 start.");
67     auto& BatterySrvClient = BatterySrvClient::GetInstance();
68     if (g_isMock) {
69         auto tempCapacity = BatterySrvClient.GetCapacity();
70         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/capacity", "44");
71         auto capacity = BatterySrvClient.GetCapacity();
72         BATTERY_HILOGD(LABEL_TEST, "BatteryST001::capacity=%{public}d.", capacity);
73         GTEST_LOG_(INFO) << "BatteryST001 executing, capacity=" << capacity;
74         EXPECT_TRUE(capacity == 44);
75 
76         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/capacity", std::to_string(tempCapacity));
77         GTEST_LOG_(INFO) << "BatteryST001 executing, capacity=" << tempCapacity;
78     } else {
79         auto capacity = BatterySrvClient.GetCapacity();
80         BATTERY_HILOGD(LABEL_TEST, "BatteryST001::capacity=%{public}d", capacity);
81         GTEST_LOG_(INFO) << "BatteryST001 executing, capacity=" << capacity;
82         EXPECT_TRUE(capacity <= 100 && capacity >= 0);
83     }
84     BATTERY_HILOGD(LABEL_TEST, "BatteryST001 end.");
85 }
86 
87 /**
88  * @tc.name: BatteryST002
89  * @tc.desc: Test IBatterySrv interface GetChargingStatus when chargestate value is none
90  * @tc.type: FUNC
91  * @tc.require: issueI6KRS8
92  */
93 HWTEST_F (BatterySttest, BatteryST002, TestSize.Level1)
94 {
95     BATTERY_HILOGI(LABEL_TEST, "BatteryST002 start.");
96     auto& BatterySrvClient = BatterySrvClient::GetInstance();
97     if (g_isMock) {
98         auto tempChargeState = BatterySrvClient.GetChargingStatus();
99         std::string chargeStateArr[] = {"Discharging", "Charging", "Not charging", "Full", "Unknown"};
100 
101         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/status", "Discharging");
102         auto chargeState = BatterySrvClient.GetChargingStatus();
103         BATTERY_HILOGD(LABEL_TEST, "BatteryST002::capacity=%{public}d.", chargeState);
104         GTEST_LOG_(INFO) << "BatteryST002 executing, chargeState=" << static_cast<int32_t>(chargeState);
105 
106         EXPECT_TRUE(chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_NONE);
107         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/status",
108                 chargeStateArr[static_cast<int32_t>(tempChargeState)]);
109         GTEST_LOG_(INFO) << "BatteryST002 executing, chargeState=" << static_cast<int32_t>(tempChargeState);
110     } else {
111         auto chargeState = BatterySrvClient.GetChargingStatus();
112         BATTERY_HILOGD(LABEL_TEST, "BatteryST002::chargeState=%{public}d",
113             static_cast<int32_t>(chargeState));
114         GTEST_LOG_(INFO) << "BatteryST002 executing, chargeState="
115             << static_cast<int32_t>(chargeState);
116         EXPECT_TRUE(chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE ||
117             chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_FULL);
118     }
119     BATTERY_HILOGD(LABEL_TEST, "BatteryST002 end.");
120 }
121 
122 /**
123  * @tc.name: BatteryST003
124  * @tc.desc: Test IBatterySrv interface GetChargingStatus when chargestate value is enable
125  * @tc.type: FUNC
126  * @tc.require: issueI6KRS8
127  */
128 HWTEST_F (BatterySttest, BatteryST003, TestSize.Level1)
129 {
130     BATTERY_HILOGD(LABEL_TEST, "BatteryST003 start.");
131     auto& BatterySrvClient = BatterySrvClient::GetInstance();
132     if (g_isMock) {
133         auto tempChargeState = BatterySrvClient.GetChargingStatus();
134         std::string chargeStateArr[] = {"Discharging", "Charging", "Not charging", "Full", "Unknown"};
135 
136         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/status", "Charging");
137         auto chargeState = BatterySrvClient.GetChargingStatus();
138         GTEST_LOG_(INFO) << "BatteryST003 executing, chargeState=" << static_cast<int32_t>(chargeState);
139 
140         EXPECT_TRUE(chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE);
141         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/status",
142                 chargeStateArr[static_cast<int32_t>(tempChargeState)]);
143         GTEST_LOG_(INFO) << "BatteryST003 executing, chargeState=" << static_cast<int32_t>(tempChargeState);
144     } else {
145         auto chargeState = BatterySrvClient.GetChargingStatus();
146         BATTERY_HILOGI(LABEL_TEST, "BatteryST003::chargeState=%{public}d",
147             static_cast<int32_t>(chargeState));
148         GTEST_LOG_(INFO) << "BatteryST003 executing, chargeState="
149             << static_cast<int32_t>(chargeState);
150         EXPECT_TRUE(chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE ||
151             chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_FULL);
152     }
153     BATTERY_HILOGD(LABEL_TEST, "BatteryST003 end.");
154 }
155 
156 /**
157  * @tc.name: BatteryST004
158  * @tc.desc: Test IBatterySrv interface GetChargingStatus when chargestate value is full
159  * @tc.type: FUNC
160  * @tc.require: issueI6KRS8
161  */
162 HWTEST_F (BatterySttest, BatteryST004, TestSize.Level1)
163 {
164     BATTERY_HILOGD(LABEL_TEST, "enter. BatteryST004 start.");
165     auto& BatterySrvClient = BatterySrvClient::GetInstance();
166     if (g_isMock) {
167         auto tempChargeState = BatterySrvClient.GetChargingStatus();
168         std::string chargeStateArr[] = {"Discharging", "Charging", "Not charging", "Full", "Unknown"};
169 
170         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/status", "Full");
171         auto chargeState = BatterySrvClient.GetChargingStatus();
172         GTEST_LOG_(INFO) << "BatteryST004 executing, chargeState=" << static_cast<int32_t>(chargeState);
173 
174         EXPECT_TRUE(chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_FULL);
175         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/status",
176                 chargeStateArr[static_cast<int32_t>(tempChargeState)]);
177         GTEST_LOG_(INFO) << "BatteryST004 executing, chargeState=" << static_cast<int32_t>(tempChargeState);
178     } else {
179         auto chargeState = BatterySrvClient.GetChargingStatus();
180         BATTERY_HILOGD(LABEL_TEST, "BatteryST004::chargeState=%{public}d",
181             static_cast<int32_t>(chargeState));
182         GTEST_LOG_(INFO) << "BatteryST004 executing, chargeState="
183             << static_cast<int32_t>(chargeState);
184         EXPECT_TRUE(chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE ||
185             chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_FULL);
186     }
187     BATTERY_HILOGD(LABEL_TEST, "BatteryST004 end.");
188 }
189 
190 /**
191  * @tc.name: BatteryST005
192  * @tc.desc: Test IBatterySrv interface GetChargingStatus when chargestate value is disable
193  * @tc.type: FUNC
194  * @tc.require: issueI6KRS8
195  */
196 HWTEST_F (BatterySttest, BatteryST005, TestSize.Level1)
197 {
198     BATTERY_HILOGD(LABEL_TEST, "BatteryST005 start.");
199     auto& BatterySrvClient = BatterySrvClient::GetInstance();
200     if (g_isMock) {
201         auto tempChargeState = BatterySrvClient.GetChargingStatus();
202         std::string chargeStateArr[] = {"Discharging", "Charging", "Not charging", "Full", "Unknown"};
203 
204         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/status", "Not charging");
205         auto chargeState = BatterySrvClient.GetChargingStatus();
206         GTEST_LOG_(INFO) << "BatteryST005 executing, chargeState=" << static_cast<int32_t>(chargeState);
207 
208         EXPECT_TRUE(chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_DISABLE);
209         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/status",
210                 chargeStateArr[static_cast<int32_t>(tempChargeState)]);
211         GTEST_LOG_(INFO) << "BatteryST005 executing, chargeState=" << static_cast<int32_t>(tempChargeState);
212     } else {
213         auto chargeState = BatterySrvClient.GetChargingStatus();
214         BATTERY_HILOGD(LABEL_TEST, "BatteryST005::chargeState=%{public}d",
215             static_cast<int32_t>(chargeState));
216         GTEST_LOG_(INFO) << "BatteryST005 executing, chargeState="
217             << static_cast<int32_t>(chargeState);
218         EXPECT_TRUE(chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE ||
219             chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_FULL);
220     }
221     BATTERY_HILOGD(LABEL_TEST, "BatteryST005 end.");
222 }
223 
224 /**
225  * @tc.name: BatteryST006
226  * @tc.desc: Test IBatterySrv interface GetChargingStatus when chargestate value is butt
227  * @tc.type: FUNC
228  * @tc.require: issueI6KRS8
229  */
230 HWTEST_F (BatterySttest, BatteryST006, TestSize.Level1)
231 {
232     BATTERY_HILOGD(LABEL_TEST, "BatteryST006 start.");
233     auto& BatterySrvClient = BatterySrvClient::GetInstance();
234     if (g_isMock) {
235         auto tempChargeState = BatterySrvClient.GetChargingStatus();
236         std::string chargeStateArr[] = {"Discharging", "Charging", "Not charging", "Full", "Unknown"};
237 
238         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/status", "Unknown");
239         auto chargeState = BatterySrvClient.GetChargingStatus();
240         GTEST_LOG_(INFO) << "BatteryST006 executing, chargeState=" << static_cast<int32_t>(chargeState);
241 
242         EXPECT_TRUE(chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_BUTT);
243         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/status",
244                 chargeStateArr[static_cast<int32_t>(tempChargeState)]);
245         GTEST_LOG_(INFO) << "BatteryST006 executing, chargeState=" << static_cast<int32_t>(tempChargeState);
246     } else {
247         auto chargeState = BatterySrvClient.GetChargingStatus();
248         BATTERY_HILOGD(LABEL_TEST, "BatteryST006::chargeState=%{public}d",
249             static_cast<int32_t>(chargeState));
250         GTEST_LOG_(INFO) << "BatteryST006 executing, chargeState="
251             << static_cast<int32_t>(chargeState);
252         EXPECT_TRUE(chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE ||
253             chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_FULL);
254     }
255     BATTERY_HILOGD(LABEL_TEST, "BatteryST006 end.");
256 }
257 
258 /**
259  * @tc.name: BatteryST007
260  * @tc.desc: Test IBatterySrv interface GetHealthStatus when healthstatus value is HEALTH_STATE_GOOD
261  * @tc.type: FUNC
262  * @tc.require: issueI6KRS8
263  */
264 HWTEST_F (BatterySttest, BatteryST007, TestSize.Level1)
265 {
266     BATTERY_HILOGD(LABEL_TEST, "BatteryST007 start.");
267     auto& BatterySrvClient = BatterySrvClient::GetInstance();
268     if (g_isMock) {
269         auto tempHealthState = BatterySrvClient.GetHealthStatus();
270         std::string healthStateArr[] = {"Unknown", "Good", "Hot", "Over voltage", "Cold", "Dead"};
271 
272         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/health", "Good");
273         auto healthState = BatterySrvClient.GetHealthStatus();
274         GTEST_LOG_(INFO) << "BatteryST007 executing, healthState=" << static_cast<int32_t>(healthState);
275 
276         EXPECT_TRUE(healthState == OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_GOOD);
277         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/health",
278                 healthStateArr[static_cast<int32_t>(tempHealthState)]);
279         GTEST_LOG_(INFO) << "BatteryST007 executing, healthState=" << static_cast<int32_t>(tempHealthState);
280     } else {
281         auto healthState = BatterySrvClient.GetHealthStatus();
282         BATTERY_HILOGD(LABEL_TEST, "BatteryST007::healthState=%{public}d",
283             static_cast<int32_t>(healthState));
284         GTEST_LOG_(INFO) << "BatteryST007 executing, healthState="
285             << static_cast<int32_t>(healthState);
286         EXPECT_TRUE(healthState == OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_GOOD);
287     }
288     BATTERY_HILOGD(LABEL_TEST, "BatteryST007 end.");
289 }
290 
291 /**
292  * @tc.name: BatteryST008
293  * @tc.desc: Test IBatterySrv interface GetHealthStatus when healthstatus value is HEALTH_STATE_COLD
294  * @tc.type: FUNC
295  * @tc.require: issueI6KRS8
296  */
297 HWTEST_F (BatterySttest, BatteryST008, TestSize.Level1)
298 {
299     BATTERY_HILOGD(LABEL_TEST, "BatteryST008 start.");
300     auto& BatterySrvClient = BatterySrvClient::GetInstance();
301     if (g_isMock) {
302         auto tempHealthState = BatterySrvClient.GetHealthStatus();
303         std::string healthStateArr[] = {"Unknown", "Good", "Hot", "Over voltage", "Cold", "Dead"};
304 
305         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/health", "Cold");
306         auto healthState = BatterySrvClient.GetHealthStatus();
307         GTEST_LOG_(INFO) << "BatteryST008 executing, healthState=" << static_cast<int32_t>(healthState);
308 
309         EXPECT_TRUE(healthState == OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_COLD);
310         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/health",
311                 healthStateArr[static_cast<int32_t>(tempHealthState)]);
312         GTEST_LOG_(INFO) << "BatteryST008 executing, healthState=" << static_cast<int32_t>(tempHealthState);
313     } else {
314         auto healthState = BatterySrvClient.GetHealthStatus();
315         BATTERY_HILOGD(LABEL_TEST, "BatteryST008::healthState=%{public}d",
316             static_cast<int32_t>(healthState));
317         GTEST_LOG_(INFO) << "BatteryST008 executing, healthState="
318             << static_cast<int32_t>(healthState);
319         EXPECT_TRUE(healthState == OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_GOOD);
320     }
321     BATTERY_HILOGD(LABEL_TEST, "BatteryST008 end.");
322 }
323 
324 /**
325  * @tc.name: BatteryST009
326  * @tc.desc: Test IBatterySrv interface GetHealthStatus when healthstatus value is HEALTH_STATE_OVERHEAT
327  * @tc.type: FUNC
328  * @tc.require: issueI6KRS8
329  */
330 HWTEST_F (BatterySttest, BatteryST009, TestSize.Level1)
331 {
332     BATTERY_HILOGD(LABEL_TEST, "BatteryST009 start.");
333     auto& BatterySrvClient = BatterySrvClient::GetInstance();
334     if (g_isMock) {
335         auto tempHealthState = BatterySrvClient.GetHealthStatus();
336         std::string healthStateArr[] = {"Unknown", "Good", "Hot", "Over voltage", "Cold", "Dead"};
337 
338         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/health", "Hot");
339         auto healthState = BatterySrvClient.GetHealthStatus();
340         GTEST_LOG_(INFO) << "BatteryST009 executing, healthState=" << static_cast<int32_t>(healthState);
341 
342         EXPECT_TRUE(healthState == OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_OVERHEAT);
343         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/health",
344                 healthStateArr[static_cast<int32_t>(tempHealthState)]);
345         GTEST_LOG_(INFO) << "BatteryST009 executing, healthState=" << static_cast<int32_t>(tempHealthState);
346     } else {
347         auto healthState = BatterySrvClient.GetHealthStatus();
348         BATTERY_HILOGD(LABEL_TEST, "BatteryST009::healthState=%{public}d",
349             static_cast<int32_t>(healthState));
350         GTEST_LOG_(INFO) << "BatteryST009 executing, healthState="
351             << static_cast<int32_t>(healthState);
352         EXPECT_TRUE(healthState == OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_GOOD);
353     }
354     BATTERY_HILOGD(LABEL_TEST, "BatteryST009 end.");
355 }
356 
357 /**
358  * @tc.name: BatteryST010
359  * @tc.desc: Test IBatterySrv interface GetHealthStatus when healthstatus value is HEALTH_STATE_OVERVOLTAGE
360  * @tc.type: FUNC
361  * @tc.require: issueI6KRS8
362  */
363 HWTEST_F (BatterySttest, BatteryST010, TestSize.Level1)
364 {
365     BATTERY_HILOGD(LABEL_TEST, "BatteryST010 start.");
366     auto& BatterySrvClient = BatterySrvClient::GetInstance();
367     if (g_isMock) {
368         auto tempHealthState = BatterySrvClient.GetHealthStatus();
369         std::string healthStateArr[] = {"Unknown", "Good", "Hot", "Over voltage", "Cold", "Dead"};
370 
371         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/health", "Over voltage");
372         auto healthState = BatterySrvClient.GetHealthStatus();
373         GTEST_LOG_(INFO) << "BatteryST010 executing, healthState=" << static_cast<int32_t>(healthState);
374 
375         EXPECT_TRUE(healthState == OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_OVERVOLTAGE);
376         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/health",
377                 healthStateArr[static_cast<int32_t>(tempHealthState)]);
378         GTEST_LOG_(INFO) << "BatteryST010 executing, healthState=" << static_cast<int32_t>(tempHealthState);
379     } else {
380         auto healthState = BatterySrvClient.GetHealthStatus();
381         BATTERY_HILOGD(LABEL_TEST, "BatteryST010::healthState=%{public}d",
382             static_cast<int32_t>(healthState));
383         GTEST_LOG_(INFO) << "BatteryST010 executing, healthState="
384             << static_cast<int32_t>(healthState);
385         EXPECT_TRUE(healthState == OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_GOOD);
386     }
387     BATTERY_HILOGD(LABEL_TEST, "BatteryST010 end.");
388 }
389 
390 /**
391  * @tc.name: BatteryST011
392  * @tc.desc: Test IBatterySrv interface GetHealthStatus when healthstatus value is HEALTH_STATE_DEAD
393  * @tc.type: FUNC
394  * @tc.require: issueI6KRS8
395  */
396 HWTEST_F (BatterySttest, BatteryST011, TestSize.Level1)
397 {
398     BATTERY_HILOGD(LABEL_TEST, "BatteryST011 start.");
399     auto& BatterySrvClient = BatterySrvClient::GetInstance();
400     if (g_isMock) {
401         auto tempHealthState = BatterySrvClient.GetHealthStatus();
402         std::string healthStateArr[] = {"Unknown", "Good", "Hot", "Over voltage", "Cold", "Dead"};
403 
404         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/health", "Dead");
405         auto healthState = BatterySrvClient.GetHealthStatus();
406         GTEST_LOG_(INFO) << "BatteryST011 executing, healthState=" << static_cast<int32_t>(healthState);
407 
408         EXPECT_TRUE(healthState == OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_DEAD);
409         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/health",
410                 healthStateArr[static_cast<int32_t>(tempHealthState)]);
411         GTEST_LOG_(INFO) << "BatteryST011 executing, healthState=" << static_cast<int32_t>(tempHealthState);
412     } else {
413         auto healthState = BatterySrvClient.GetHealthStatus();
414         BATTERY_HILOGD(LABEL_TEST, "BatteryST011::healthState=%{public}d",
415             static_cast<int32_t>(healthState));
416         GTEST_LOG_(INFO) << "BatteryST011 executing, healthState="
417             << static_cast<int32_t>(healthState);
418         EXPECT_TRUE(healthState == OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_GOOD);
419     }
420     BATTERY_HILOGD(LABEL_TEST, "BatteryST011 end.");
421 }
422 
423 /**
424  * @tc.name: BatteryST012
425  * @tc.desc: Test IBatterySrv interface GetHealthStatus when healthstatus value is HEALTH_STATE_UNKNOWN
426  * @tc.type: FUNC
427  * @tc.require: issueI6KRS8
428  */
429 HWTEST_F (BatterySttest, BatteryST012, TestSize.Level1)
430 {
431     BATTERY_HILOGD(LABEL_TEST, "BatteryST012 start.");
432     auto& BatterySrvClient = BatterySrvClient::GetInstance();
433     if (g_isMock) {
434         auto tempHealthState = BatterySrvClient.GetHealthStatus();
435         std::string healthStateArr[] = {"Unknown", "Good", "Hot", "Over voltage", "Cold", "Dead"};
436 
437         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/health", "Unknown");
438         auto healthState = BatterySrvClient.GetHealthStatus();
439         GTEST_LOG_(INFO) << "BatteryST012 executing, healthState=" << static_cast<int32_t>(healthState);
440 
441         EXPECT_TRUE(healthState == OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_UNKNOWN);
442         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/health",
443                 healthStateArr[static_cast<int32_t>(tempHealthState)]);
444         GTEST_LOG_(INFO) << "BatteryST012 executing, healthState=" << static_cast<int32_t>(tempHealthState);
445     } else {
446         auto healthState = BatterySrvClient.GetHealthStatus();
447         BATTERY_HILOGD(LABEL_TEST, "BatteryST012::healthState=%{public}d",
448             static_cast<int32_t>(healthState));
449         GTEST_LOG_(INFO) << "BatteryST012 executing, healthState="
450             << static_cast<int32_t>(healthState);
451         EXPECT_TRUE(healthState == OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_GOOD);
452     }
453     BATTERY_HILOGD(LABEL_TEST, "BatteryST012 end.");
454 }
455 
456 /**
457  * @tc.name: BatteryST013
458  * @tc.desc: Test IBatterySrv interface GetPresent when present is true
459  * @tc.type: FUNC
460  * @tc.require: issueI6KRS8
461  */
462 HWTEST_F (BatterySttest, BatteryST013, TestSize.Level1)
463 {
464     BATTERY_HILOGD(LABEL_TEST, "BatteryST013 start.");
465     auto& BatterySrvClient = BatterySrvClient::GetInstance();
466     if (g_isMock) {
467         auto tempPresent = BatterySrvClient.GetPresent();
468 
469         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/present", "1");
470         auto present = BatterySrvClient.GetPresent();
471         GTEST_LOG_(INFO) << "BatteryST013 executing, present=" << present;
472 
473         EXPECT_TRUE(present);
474         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/present", std::to_string(tempPresent));
475         GTEST_LOG_(INFO) << "BatteryST013 executing, tempPresent=" << static_cast<int32_t>(tempPresent);
476     } else {
477         auto present = BatterySrvClient.GetPresent();
478         BATTERY_HILOGD(LABEL_TEST, "BatteryST013::present=%{public}d", present);
479         GTEST_LOG_(INFO) << "BatteryST013 executing, present=" << present;
480         EXPECT_TRUE(present);
481     }
482     BATTERY_HILOGD(LABEL_TEST, "BatteryST013 end.");
483 }
484 
485 /**
486  * @tc.name: BatteryST014
487  * @tc.desc: Test IBatterySrv interface GetPresent when present is false
488  * @tc.type: FUNC
489  * @tc.require: issueI6KRS8
490  */
491 HWTEST_F (BatterySttest, BatteryST014, TestSize.Level1)
492 {
493     BATTERY_HILOGD(LABEL_TEST, "BatteryST014 start.");
494     auto& BatterySrvClient = BatterySrvClient::GetInstance();
495     if (g_isMock) {
496         auto tempPresent = BatterySrvClient.GetPresent();
497 
498         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/present", "0");
499         auto present = BatterySrvClient.GetPresent();
500         GTEST_LOG_(INFO) << "BatteryST014 executing, present=" << present;
501 
502         EXPECT_FALSE(present);
503         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/present", std::to_string(tempPresent));
504         GTEST_LOG_(INFO) << "BatteryST014 executing, tempPresent=" << static_cast<int32_t>(tempPresent);
505     } else {
506         auto present = BatterySrvClient.GetPresent();
507         BATTERY_HILOGD(LABEL_TEST, "BatteryST014::present=%{public}d", present);
508         GTEST_LOG_(INFO) << "BatteryST014 executing, present=" << present;
509         EXPECT_TRUE(present);
510     }
511     BATTERY_HILOGD(LABEL_TEST, "BatteryST014 end.");
512 }
513 
514 /**
515  * @tc.name: BatteryST015
516  * @tc.desc: Test IBatterySrv interface GetVoltage
517  * @tc.type: FUNC
518  * @tc.require: issueI6KRS8
519  */
520 HWTEST_F (BatterySttest, BatteryST015, TestSize.Level1)
521 {
522     BATTERY_HILOGD(LABEL_TEST, "BatteryST015 start.");
523     auto& BatterySrvClient = BatterySrvClient::GetInstance();
524     if (g_isMock) {
525         auto tempVoltagenow = BatterySrvClient.GetVoltage();
526 
527         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/voltage_now", "4654321");
528         auto voltage = BatterySrvClient.GetVoltage();
529         GTEST_LOG_(INFO) << "BatteryST015 executing, voltage=" << voltage;
530 
531         EXPECT_TRUE(voltage == 4654321);
532         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/voltage_now", std::to_string(tempVoltagenow));
533         GTEST_LOG_(INFO) << "BatteryST015 executing, tempPresent=" << static_cast<int32_t>(tempVoltagenow);
534     } else {
535         auto voltage = BatterySrvClient.GetVoltage();
536         BATTERY_HILOGD(LABEL_TEST, "BatteryST015::voltage=%{public}d", voltage);
537         GTEST_LOG_(INFO) << "BatteryST015 executing, voltage=" << voltage;
538         EXPECT_TRUE(voltage >= 0);
539     }
540     BATTERY_HILOGD(LABEL_TEST, "BatteryST015 end.");
541 }
542 
543 /**
544  * @tc.name: BatteryST016
545  * @tc.desc: Test IBatterySrv interface GetTemperature
546  * @tc.type: FUNC
547  * @tc.require: issueI6KRS8
548  */
549 HWTEST_F (BatterySttest, BatteryST016, TestSize.Level1)
550 {
551     BATTERY_HILOGD(LABEL_TEST, "BatteryST016 start.");
552     auto& BatterySrvClient = BatterySrvClient::GetInstance();
553     if (g_isMock) {
554         auto tempTempPresent = BatterySrvClient.GetBatteryTemperature();
555 
556         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/temp", "234");
557         auto temperature = BatterySrvClient.GetBatteryTemperature();
558         GTEST_LOG_(INFO) << "BatteryST016 executing, temperature=" << temperature;
559 
560         EXPECT_TRUE(temperature == 234);
561         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/temp", std::to_string(tempTempPresent));
562         GTEST_LOG_(INFO) << "BatteryST016 executing, temperature=" << static_cast<int32_t>(tempTempPresent);
563     } else {
564         auto temperature = BatterySrvClient.GetBatteryTemperature();
565         BATTERY_HILOGD(LABEL_TEST, "BatteryST016::temperature=%{public}d", temperature);
566         GTEST_LOG_(INFO) << "BatteryST016 executing, temperature=" << temperature;
567         EXPECT_TRUE(temperature >= 0 && temperature <= 600);
568     }
569     BATTERY_HILOGD(LABEL_TEST, "BatteryST016 end.");
570 }
571 
572 /**
573  * @tc.name: BatteryST017
574  * @tc.desc: Test IBatterySrv interface GetTechnology
575  * @tc.type: FUNC
576  * @tc.require: issueI6KRS8
577  */
578 HWTEST_F (BatterySttest, BatteryST017, TestSize.Level1)
579 {
580     BATTERY_HILOGD(LABEL_TEST, "BatteryST017 start.");
581     auto& BatterySrvClient = BatterySrvClient::GetInstance();
582     if (g_isMock) {
583         std::string tempTechnology = BatterySrvClient.GetTechnology();
584 
585         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/ohos-fgu/technology", "H2");
586         auto technology = BatterySrvClient.GetTechnology();
587         GTEST_LOG_(INFO) << "BatteryST017 executing, technology=" << technology;
588 
589         EXPECT_TRUE(technology == "H2");
590         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/ohos-fgu/technology", tempTechnology);
591         GTEST_LOG_(INFO) << "BatteryST017 executing, technology=" << tempTechnology;
592     } else {
593         auto technology = BatterySrvClient.GetTechnology();
594         BATTERY_HILOGD(LABEL_TEST, "BatteryST017::technology=%{public}s", technology.c_str());
595         GTEST_LOG_(INFO) << "BatteryST017 executing, technology=" << technology;
596         EXPECT_TRUE(technology == "Li-poly");
597     }
598     BATTERY_HILOGD(LABEL_TEST, "BatteryST058 end.");
599 }
600 
601 /**
602  * @tc.name: BatteryST018
603  * @tc.desc: Test IBatterySrv interface GetPluggedType when pluggedType value is PLUGGED_TYPE_AC
604  * @tc.type: FUNC
605  * @tc.require: issueI6KRS8
606  */
607 HWTEST_F (BatterySttest, BatteryST018, TestSize.Level1)
608 {
609     BATTERY_HILOGD(LABEL_TEST, "BatteryST018 start.");
610     auto& BatterySrvClient = BatterySrvClient::GetInstance();
611     if (g_isMock) {
612         TestUtils::ResetOnline();
613         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/ohos_charger/online", "1");
614         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/ohos_charger/type", "Mains");
615 
616         auto pluggedType = BatterySrvClient.GetPluggedType();
617         GTEST_LOG_(INFO) << "BatteryST018 executing, pluggedType=" << static_cast<int32_t>(pluggedType);
618 
619         EXPECT_TRUE(pluggedType == OHOS::PowerMgr::BatteryPluggedType::PLUGGED_TYPE_AC);
620     } else {
621         auto pluggedType = BatterySrvClient.GetPluggedType();
622         BATTERY_HILOGI(LABEL_TEST, "BatteryST018::pluggedType=%{public}d",
623             static_cast<int32_t>(pluggedType));
624         GTEST_LOG_(INFO) << "BatteryST018 executing, pluggedType="
625             << static_cast<int32_t>(pluggedType);
626         EXPECT_TRUE(pluggedType == OHOS::PowerMgr::BatteryPluggedType::PLUGGED_TYPE_USB);
627     }
628     BATTERY_HILOGD(LABEL_TEST, "BatteryST018 end.");
629 }
630 
631 /**
632  * @tc.name: BatteryST019
633  * @tc.desc: Test IBatterySrv interface GetPluggedType when pluggedType value is PLUGGED_TYPE_USB
634  * @tc.type: FUNC
635  * @tc.require: issueI6KRS8
636  */
637 HWTEST_F (BatterySttest, BatteryST019, TestSize.Level1)
638 {
639     BATTERY_HILOGD(LABEL_TEST, "BatteryST019 start.");
640     auto& BatterySrvClient = BatterySrvClient::GetInstance();
641     if (g_isMock) {
642         TestUtils::ResetOnline();
643         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/ohos_charger/online", "1");
644         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/ohos_charger/type", "USB");
645 
646         auto pluggedType = BatterySrvClient.GetPluggedType();
647         GTEST_LOG_(INFO) << "BatteryST019 executing, pluggedType=" << static_cast<int32_t>(pluggedType);
648 
649         EXPECT_TRUE(pluggedType == OHOS::PowerMgr::BatteryPluggedType::PLUGGED_TYPE_USB);
650     } else {
651         auto pluggedType = BatterySrvClient.GetPluggedType();
652         BATTERY_HILOGI(LABEL_TEST, "BatteryST019::pluggedType=%{public}d",
653             static_cast<int32_t>(pluggedType));
654         GTEST_LOG_(INFO) << "BatteryST019 executing, pluggedType="
655             << static_cast<int32_t>(pluggedType);
656         EXPECT_TRUE(pluggedType == OHOS::PowerMgr::BatteryPluggedType::PLUGGED_TYPE_USB);
657     }
658     BATTERY_HILOGD(LABEL_TEST, "BatteryST019 end.");
659 }
660 
661 /**
662  * @tc.name: BatteryST020
663  * @tc.desc: Test IBatterySrv interface GetPluggedType when pluggedType value is PLUGGED_TYPE_WIRELESS
664  * @tc.type: FUNC
665  * @tc.require: issueI6KRS8
666  */
667 HWTEST_F (BatterySttest, BatteryST020, TestSize.Level1)
668 {
669     BATTERY_HILOGD(LABEL_TEST, "BatteryST020 start.");
670     auto& BatterySrvClient = BatterySrvClient::GetInstance();
671     if (g_isMock) {
672         TestUtils::ResetOnline();
673         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/ohos_charger/online", "1");
674         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/ohos_charger/type", "Wireless");
675 
676         auto pluggedType = BatterySrvClient.GetPluggedType();
677         GTEST_LOG_(INFO) << "BatteryST020 executing, pluggedType=" << static_cast<int32_t>(pluggedType);
678 
679         EXPECT_TRUE(pluggedType == OHOS::PowerMgr::BatteryPluggedType::PLUGGED_TYPE_WIRELESS);
680     } else {
681         auto pluggedType = BatterySrvClient.GetPluggedType();
682         BATTERY_HILOGI(LABEL_TEST, "BatteryST020::pluggedType=%{public}d",
683             static_cast<int32_t>(pluggedType));
684         GTEST_LOG_(INFO) << "BatteryST020 executing, pluggedType="
685             << static_cast<int32_t>(pluggedType);
686         EXPECT_TRUE(pluggedType == OHOS::PowerMgr::BatteryPluggedType::PLUGGED_TYPE_USB);
687     }
688     BATTERY_HILOGD(LABEL_TEST, "BatteryST020 end.");
689 }
690 
691 /**
692  * @tc.name: BatteryST021
693  * @tc.desc: Test IBatterySrv interface GetPluggedType when pluggedType value is PLUGGED_TYPE_BUTT
694  *           ParsePluggedType return HDF_ERR_NOT_SUPPORT, g_service->GetPluggedType FAIL
695  * @tc.type: FUNC
696  * @tc.require: issueI6KRS8
697  */
698 HWTEST_F (BatterySttest, BatteryST021, TestSize.Level1)
699 {
700     BATTERY_HILOGD(LABEL_TEST, "BatteryST021 start.");
701     auto& BatterySrvClient = BatterySrvClient::GetInstance();
702     if (g_isMock) {
703         TestUtils::ResetOnline();
704         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/ohos_charger/online", "1");
705         TestUtils::WriteMock(MOCK_BATTERY_PATH + "/ohos_charger/type", "Unknown");
706 
707         auto pluggedType = BatterySrvClient.GetPluggedType();
708         GTEST_LOG_(INFO) << "BatteryST021 executing, pluggedType=" << static_cast<int32_t>(pluggedType);
709 
710         EXPECT_TRUE(pluggedType == OHOS::PowerMgr::BatteryPluggedType::PLUGGED_TYPE_NONE);
711     } else {
712         auto pluggedType = BatterySrvClient.GetPluggedType();
713         BATTERY_HILOGI(LABEL_TEST, "BatteryST021::pluggedType=%{public}d",
714             static_cast<int32_t>(pluggedType));
715         GTEST_LOG_(INFO) << "BatteryST021 executing, pluggedType="
716             << static_cast<int32_t>(pluggedType);
717         EXPECT_TRUE(pluggedType == OHOS::PowerMgr::BatteryPluggedType::PLUGGED_TYPE_USB);
718     }
719     BATTERY_HILOGD(LABEL_TEST, "BatteryST021 end.");
720 }
721 }
722