• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include <string>
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 #include "battery_log.h"
22 #include "battery_service.h"
23 #include "test_utils.h"
24 
25 using namespace testing::ext;
26 using namespace OHOS::PowerMgr;
27 using namespace OHOS::EventFwk;
28 using namespace OHOS;
29 using namespace std;
30 
31 namespace {
32 bool g_isMock = false;
33 static sptr<BatteryService> g_service;
34 }
35 
SetUpTestCase(void)36 void BatteryServiceTest::SetUpTestCase(void)
37 {
38     g_service = DelayedSpSingleton<BatteryService>::GetInstance();
39     g_service->OnStart();
40     GTEST_LOG_(INFO) << "is mock: " << g_service->GetPresent() << " g_isMock: " << g_isMock;
41     if (!g_service->GetPresent()) {
42         g_isMock = true;
43         TestUtils::InitTest();
44         g_service->ChangePath("/data/local/tmp");
45     }
46 }
47 
TearDownTestCase(void)48 void BatteryServiceTest::TearDownTestCase(void)
49 {
50     g_isMock = false;
51     TestUtils::ResetOnline();
52 }
53 
SetUp(void)54 void BatteryServiceTest::SetUp(void)
55 {
56 }
57 
TearDown(void)58 void BatteryServiceTest::TearDown(void)
59 {
60 }
61 
62 /**
63  * @tc.name: BatteryService001
64  * @tc.desc: Test functions GetCapacity in BatteryService
65  * @tc.type: FUNC
66  */
67 static HWTEST_F (BatteryServiceTest, BatteryService001, TestSize.Level1)
68 {
69     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService001 start.");
70     int32_t capacity = -1;
71     if (g_isMock) {
72         TestUtils::WriteMock("/data/local/tmp/battery/capacity", "50");
73         capacity = g_service->GetCapacity();
74         ASSERT_TRUE(capacity == 50);
75     } else {
76         capacity = g_service->GetCapacity();
77         ASSERT_TRUE(capacity >= 0 && capacity <= 100);
78     }
79     BATTERY_HILOGI(LABEL_TEST, "BatteryServiceTest::capacity=%{public}d", capacity);
80     GTEST_LOG_(INFO) << "BatteryService::BatteryService001 executing, capacity=" << capacity;
81     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService001 end.");
82 }
83 
84 /**
85  * @tc.name: BatteryService002
86  * @tc.desc: Test functions GetChargingStatus in BatteryService
87  * @tc.type: FUNC
88  */
89 HWTEST_F (BatteryServiceTest, BatteryService002, TestSize.Level1)
90 {
91     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService002 start.");
92     OHOS::PowerMgr::BatteryChargeState chargeState = OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_BUTT;
93     if (g_isMock) {
94         TestUtils::WriteMock("/data/local/tmp/battery/status", "Charging");
95         chargeState = g_service->GetChargingStatus();
96         ASSERT_TRUE(chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE);
97     } else {
98         chargeState = g_service->GetChargingStatus();
99         ASSERT_TRUE(chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE ||
100             chargeState == OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_FULL);
101     }
102     BATTERY_HILOGI(LABEL_TEST, "BatteryServiceTest::chargeState=%{public}d", int(chargeState));
103     GTEST_LOG_(INFO) << "BatteryService::BatteryService002 executing, chargeState=" << int(chargeState);
104     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService002 end.");
105 }
106 
107 /**
108  * @tc.name: BatteryService003
109  * @tc.desc: Test functions GetHealthStatus in BatteryService
110  * @tc.type: FUNC
111  */
112 HWTEST_F (BatteryServiceTest, BatteryService003, TestSize.Level1)
113 {
114     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService003 start.");
115     OHOS::PowerMgr::BatteryHealthState healthState = OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_BUTT;
116     if (g_isMock) {
117         TestUtils::WriteMock("/data/local/tmp/battery/health", "Good");
118         healthState =  g_service->GetHealthStatus();
119         ASSERT_TRUE(healthState == OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_GOOD);
120     } else {
121         healthState =  g_service->GetHealthStatus();
122         ASSERT_TRUE(healthState == OHOS::PowerMgr::BatteryHealthState::HEALTH_STATE_GOOD);
123     }
124     GTEST_LOG_(INFO) << "BatteryService::BatteryService003 executing, healthState=" << int(healthState);
125     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService003 end.");
126 }
127 
128 /**
129  * @tc.name: BatteryService004
130  * @tc.desc: Test functions GetPresent in BatteryService
131  * @tc.type: FUNC
132  */
133 HWTEST_F (BatteryServiceTest, BatteryService004, TestSize.Level1)
134 {
135     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService004 start.");
136     bool present = false;
137     if (g_isMock) {
138         TestUtils::WriteMock("/data/local/tmp/battery/present", "0");
139         present = g_service->GetPresent();
140         ASSERT_FALSE(present);
141     } else {
142         present = g_service->GetPresent();
143         ASSERT_TRUE(present);
144     }
145     GTEST_LOG_(INFO) << "BatteryService::BatteryService004 executing, present=" << present;
146     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService004 end.");
147 }
148 
149 /**
150  * @tc.name: BatteryService005
151  * @tc.desc: Test functions GetVoltage in BatteryService
152  * @tc.type: FUNC
153  */
154 HWTEST_F (BatteryServiceTest, BatteryService005, TestSize.Level1)
155 {
156     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService005 start.");
157     int32_t voltage = -1;
158     if (g_isMock) {
159         TestUtils::WriteMock("/data/local/tmp/battery/voltage_avg", "4123456");
160         TestUtils::WriteMock("/data/local/tmp/battery/voltage_now", "4123456");
161         voltage = g_service->GetVoltage();
162         ASSERT_TRUE(voltage == 4123456);
163     } else {
164         voltage = g_service->GetVoltage();
165         ASSERT_TRUE(voltage > 0);
166     }
167     BATTERY_HILOGI(LABEL_TEST, "BatteryServiceTest::voltage=%{public}d", voltage);
168     GTEST_LOG_(INFO) << "BatteryService::BatteryService005 executing, voltage=" << voltage;
169     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService005 end.");
170 }
171 
172 /**
173  * @tc.name: BatteryService006
174  * @tc.desc: Test functions GetBatteryTemperature in BatteryService
175  * @tc.type: FUNC
176  */
177 HWTEST_F (BatteryServiceTest, BatteryService006, TestSize.Level1)
178 {
179     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService006 start.");
180     int32_t temperature = -1;
181     if (g_isMock) {
182         TestUtils::WriteMock("/data/local/tmp/battery/temp", "333");
183         temperature = g_service->GetBatteryTemperature();
184         ASSERT_TRUE(temperature == 333);
185     } else {
186         temperature = g_service->GetBatteryTemperature();
187         ASSERT_TRUE(temperature > 0);
188     }
189     BATTERY_HILOGI(LABEL_TEST, "BatteryServiceTest::voltage=%{public}d", temperature);
190     GTEST_LOG_(INFO) << "BatteryService::BatteryService006 executing, temperature=" << temperature;
191     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService006 end.");
192 }
193 
194 /**
195  * @tc.name: BatteryService007
196  * @tc.desc: Test functions GetTechnology in BatteryService
197  * @tc.type: FUNC
198  */
199 HWTEST_F (BatteryServiceTest, BatteryService007, TestSize.Level1)
200 {
201     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService007 start.");
202     std::string technology = "";
203     if (g_isMock) {
204         TestUtils::WriteMock("/data/local/tmp/ohos-fgu/technology", "Li");
205         technology = g_service->GetTechnology();
206         ASSERT_TRUE(technology == "Li");
207     } else {
208         technology = g_service->GetTechnology();
209         ASSERT_TRUE(technology == "Li-poly");
210     }
211     BATTERY_HILOGI(LABEL_TEST, "BatteryServiceTest::technology=%{public}s", technology.c_str());
212     GTEST_LOG_(INFO) << "BatteryService::BatteryService007 executing, technology=" << technology;
213     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService007 end.");
214 }
215 
216 /**
217  * @tc.name: BatteryService008
218  * @tc.desc: Test functions GetPluggedType in BatteryService
219  * @tc.type: FUNC
220  */
221 HWTEST_F (BatteryServiceTest, BatteryService008, TestSize.Level1)
222 {
223     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService008 start.");
224     OHOS::PowerMgr::BatteryPluggedType pluggedType = OHOS::PowerMgr::BatteryPluggedType::PLUGGED_TYPE_BUTT;
225     if (g_isMock) {
226         TestUtils::ResetOnline();
227         TestUtils::WriteMock("/data/local/tmp/Wireless/online", "1");
228         TestUtils::WriteMock("/data/local/tmp/Wireless/type", "Wireless");
229         pluggedType = g_service->GetPluggedType();
230         ASSERT_TRUE(pluggedType == OHOS::PowerMgr::BatteryPluggedType::PLUGGED_TYPE_WIRELESS);
231     } else {
232         pluggedType = g_service->GetPluggedType();
233         ASSERT_TRUE(pluggedType == OHOS::PowerMgr::BatteryPluggedType::PLUGGED_TYPE_USB);
234     }
235     BATTERY_HILOGI(LABEL_TEST, "BatteryServiceTest::pluggedType=%{public}d", int(pluggedType));
236     GTEST_LOG_(INFO) << "BatteryService::BatteryService008 executing, pluggedType=" << int(pluggedType);
237     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService008 end.");
238 }
239 
240 /**
241  * @tc.name: BatteryService009
242  * @tc.desc: Test functions OnStart in BatteryService
243  * @tc.type: FUNC
244  */
245 HWTEST_F (BatteryServiceTest, BatteryService009, TestSize.Level1)
246 {
247     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService009 start.");
248     g_service->OnStart();
249     bool ready = g_service->IsServiceReady();
250     BATTERY_HILOGI(LABEL_TEST, "BatteryServiceTest::ready=%{public}d", ready);
251     ASSERT_TRUE(ready);
252     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService009 end.");
253 }
254 
255 /**
256  * @tc.name: BatteryService010
257  * @tc.desc: Test functions OnStop in BatteryService
258  * @tc.type: FUNC
259  */
260 HWTEST_F (BatteryServiceTest, BatteryService010, TestSize.Level1)
261 {
262     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService010 start.");
263     g_service->OnStop();
264     bool ready = g_service->IsServiceReady();
265     BATTERY_HILOGI(LABEL_TEST, "BatteryServiceTest::ready=%{public}d", ready);
266     ASSERT_FALSE(ready);
267     BATTERY_HILOGD(LABEL_TEST, "BatteryService::BatteryService010 end.");
268 }
269