• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <algorithm>
17 #include <cmath>
18 #include <cstdio>
19 #include <gtest/gtest.h>
20 #include <fcntl.h>
21 #include <functional>
22 #include <securec.h>
23 #include <unistd.h>
24 
25 #include <fstream>
26 #include "v1_1/battery_interface_proxy.h"
27 #include "v1_1/ibattery_callback.h"
28 #include "v1_1/types.h"
29 
30 using namespace OHOS::HDI::Battery;
31 using namespace OHOS::HDI::Battery::V1_1;
32 using namespace testing::ext;
33 using namespace OHOS;
34 
35 namespace {
36 class BatteryCallback : public HDI::Battery::V1_1::IBatteryCallback {
37 public:
BatteryCallback()38     BatteryCallback() {};
~BatteryCallback()39     ~BatteryCallback() override {};
Update(const HDI::Battery::V1_1::BatteryInfo & event)40     int32_t Update(const HDI::Battery::V1_1::BatteryInfo& event) override
41     {
42         (void)event;
43         return 0;
44     };
45 };
46 
47 sptr<IBatteryInterface> g_batteryInterface = nullptr;
48 sptr<IBatteryCallback> g_callback = new BatteryCallback();
49 }
50 
51 class HdfBatteryHdiTest : public testing::Test {
52 public:
53     static void SetUpTestCase();
54     static void TearDownTestCase();
55     void SetUp();
56     void TearDown();
57     static int32_t ReadFile(const char *path, char *buf, size_t size);
58     static int32_t ConvertInt(const std::string &value);
59 };
60 
SetUpTestCase(void)61 void HdfBatteryHdiTest::SetUpTestCase(void)
62 {
63     g_batteryInterface = IBatteryInterface::Get(true);
64 }
65 
TearDownTestCase(void)66 void HdfBatteryHdiTest::TearDownTestCase(void)
67 {
68 }
69 
SetUp(void)70 void HdfBatteryHdiTest::SetUp(void)
71 {
72 }
73 
TearDown(void)74 void HdfBatteryHdiTest::TearDown(void)
75 {
76 }
77 
78 namespace {
79 /**
80   * @tc.name: HdfBatteryHdiTest001
81   * @tc.desc: Get a client and check whether the client is empty.
82   * @tc.type: FUNC
83   */
84 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest001, TestSize.Level1)
85 {
86     EXPECT_TRUE(nullptr != g_batteryInterface);
87 }
88 
89 /**
90   * @tc.name: HdfBatteryHdiTest002
91   * @tc.desc: Register
92   * @tc.type: FUNC
93   */
94 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest002, TestSize.Level1)
95 {
96     if (false) {
97         printf("HdfBatteryHdiTest002: start.");
98         int32_t ret = g_batteryInterface->Register(g_callback);
99         EXPECT_EQ(0, ret) << "HdfBatteryHdiTest002 failed";
100 
101         printf("HdfBatteryHdiTest002: return.");
102     }
103 }
104 
105 /**
106   * @tc.name: HdfBatteryHdiTest003
107   * @tc.desc: UnRegister
108   * @tc.type: FUNC
109   */
110 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest003, TestSize.Level1)
111 {
112     if (false) {
113         printf("HdfBatteryHdiTest003: start.");
114         int32_t ret = g_batteryInterface->UnRegister();
115         EXPECT_EQ(0, ret) << "HdfBatteryHdiTest002 failed";
116 
117         printf("HdfBatteryHdiTest003: return.");
118     }
119 }
120 
121 /**
122   * @tc.name: HdfBatteryHdiTest004
123   * @tc.desc: ChangePath
124   * @tc.type: FUNC
125   */
126 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest004, TestSize.Level1)
127 {
128     printf("HdfBatteryHdiTest004: start.");
129     std::string path;
130     EXPECT_TRUE(HDF_SUCCESS == g_batteryInterface->ChangePath(path));
131 
132     printf("HdfBatteryHdiTest004: return.");
133 }
134 
135 /**
136   * @tc.name: HdfBatteryHdiTest005
137   * @tc.desc: GetCapacity
138   * @tc.type: FUNC
139   */
140 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest005, TestSize.Level1)
141 {
142     printf("HdfBatteryHdiTest005: start.");
143     int capacity;
144     g_batteryInterface->GetCapacity(capacity);
145     EXPECT_TRUE(capacity <= 100 && capacity >= 0);
146 
147     printf("HdfBatteryHdiTest005: return.");
148 }
149 
150 /**
151   * @tc.name: HdfBatteryHdiTest006
152   * @tc.desc: GetVoltage
153   * @tc.type: FUNC
154   */
155 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest006, TestSize.Level1)
156 {
157     printf("HdfBatteryHdiTest006: start.");
158     int voltage;
159     g_batteryInterface->GetVoltage(voltage);
160     EXPECT_TRUE(voltage >= 0);
161 
162     printf("HdfBatteryHdiTest006: return.");
163 }
164 
165 /**
166   * @tc.name: HdfBatteryHdiTest007
167   * @tc.desc: GetTemperature
168   * @tc.type: FUNC
169   */
170 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest007, TestSize.Level1)
171 {
172     printf("HdfBatteryHdiTest007: start.");
173     int temperature;
174     g_batteryInterface->GetTemperature(temperature);
175     EXPECT_TRUE(temperature >= 0 && temperature <= 600);
176 
177     printf("HdfBatteryHdiTest007: return.");
178 }
179 
180 /**
181   * @tc.name: HdfBatteryHdiTest008
182   * @tc.desc: GetHealthState
183   * @tc.type: FUNC
184   */
185 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest008, TestSize.Level1)
186 {
187     printf("HdfBatteryHdiTest007: start.");
188     OHOS::HDI::Battery::V1_1::BatteryHealthState healthState = OHOS::HDI::Battery::V1_1::BatteryHealthState(0);
189     g_batteryInterface->GetHealthState(healthState);
190     EXPECT_TRUE(healthState >= 0 && healthState <= 6);
191 
192     printf("HdfBatteryHdiTest007: return.");
193 }
194 
195 /**
196   * @tc.name: HdfBatteryHdiTest009
197   * @tc.desc: GetPluggedType
198   * @tc.type: FUNC
199   */
200 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest009, TestSize.Level1)
201 {
202     printf("HdfBatteryHdiTest009: start.");
203     OHOS::HDI::Battery::V1_1::BatteryPluggedType pluggedType = OHOS::HDI::Battery::V1_1::BatteryPluggedType(0);
204     g_batteryInterface->GetPluggedType(pluggedType);
205     EXPECT_TRUE(pluggedType >= OHOS::HDI::Battery::V1_1::BatteryPluggedType::PLUGGED_TYPE_NONE &&
206         pluggedType <= OHOS::HDI::Battery::V1_1::BatteryPluggedType::PLUGGED_TYPE_BUTT);
207 
208     printf("HdfBatteryHdiTest009: return.");
209 }
210 
211 /**
212   * @tc.name: HdfBatteryHdiTest010
213   * @tc.desc: GetChargeState
214   * @tc.type: FUNC
215   */
216 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest010, TestSize.Level1)
217 {
218     printf("HdfBatteryHdiTest010: start.");
219     OHOS::HDI::Battery::V1_1::BatteryChargeState chargeState = OHOS::HDI::Battery::V1_1::BatteryChargeState(0);
220     g_batteryInterface->GetChargeState(chargeState);
221     EXPECT_TRUE(chargeState >= 0 && chargeState <= 4);
222 
223     printf("HdfBatteryHdiTest010: return.");
224 }
225 
226 /**
227   * @tc.name: HdfBatteryHdiTest011
228   * @tc.desc: GetPresent
229   * @tc.type: FUNC
230   */
231 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest011, TestSize.Level1)
232 {
233     printf("HdfBatteryHdiTest011: start.");
234     bool present = false;
235     EXPECT_TRUE(HDF_SUCCESS == g_batteryInterface->GetPresent(present));
236     printf("HdfBatteryHdiTest011: return.");
237 }
238 
239 /**
240   * @tc.name: HdfBatteryHdiTest012
241   * @tc.desc: GetTechnology
242   * @tc.type: FUNC
243   */
244 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest012, TestSize.Level1)
245 {
246     printf("HdfBatteryHdiTest012: start.");
247     std::string technology;
248     g_batteryInterface->GetTechnology(technology);
249     EXPECT_FALSE(technology.empty());
250 
251     printf("HdfBatteryHdiTest012: return.");
252 }
253 
254 /**
255   * @tc.name: HdfBatteryHdiTest013
256   * @tc.desc: GetTotalEnergy
257   * @tc.type: FUNC
258   */
259 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest013, TestSize.Level1)
260 {
261     printf("HdfBatteryHdiTest013: start.");
262     int32_t totalEnergy = -1;
263     g_batteryInterface->GetTotalEnergy(totalEnergy);
264     EXPECT_TRUE(totalEnergy >= 0);
265 
266     printf("HdfBatteryHdiTest013: return.");
267 }
268 
269 /**
270   * @tc.name: HdfBatteryHdiTest014
271   * @tc.desc: GetCurrentAverage
272   * @tc.type: FUNC
273   */
274 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest014, TestSize.Level1)
275 {
276     printf("HdfBatteryHdiTest014: start.");
277     int curAverage = -1;
278     g_batteryInterface->GetCurrentAverage(curAverage);
279     EXPECT_TRUE(curAverage != 0);
280 
281     printf("HdfBatteryHdiTest014: return.");
282 }
283 
284 /**
285   * @tc.name: HdfBatteryHdiTest015
286   * @tc.desc: GetCurrentNow
287   * @tc.type: FUNC
288   */
289 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest015, TestSize.Level1)
290 {
291     printf("HdfBatteryHdiTest015: start.");
292     int32_t nowCurr = -1;
293     g_batteryInterface->GetCurrentNow(nowCurr);
294     EXPECT_TRUE(nowCurr != -1);
295 
296     printf("HdfBatteryHdiTest015: return.");
297 }
298 
299 /**
300   * @tc.name: HdfBatteryHdiTest016
301   * @tc.desc: GetRemainEnergy
302   * @tc.type: FUNC
303   */
304 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest016, TestSize.Level1)
305 {
306     printf("HdfBatteryHdiTest016: start.");
307     int32_t remainEnergy = -1;
308     g_batteryInterface->GetRemainEnergy(remainEnergy);
309     EXPECT_TRUE(remainEnergy >= 0);
310 
311     printf("HdfBatteryHdiTest016: return.");
312 }
313 
314 /**
315   * @tc.name: HdfBatteryHdiTest017
316   * @tc.desc: GetBatteryInfo
317   * @tc.type: FUNC
318   */
319 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest017, TestSize.Level1)
320 {
321     printf("HdfBatteryHdiTest017: start.");
322     OHOS::HDI::Battery::V1_1::BatteryInfo event = {
323         .capacity = -1,
324     };
325     g_batteryInterface->GetBatteryInfo(event);
326     EXPECT_TRUE(-1 != event.capacity);
327 
328     printf("HdfBatteryHdiTest017: return.");
329 }
330 
331 /**
332  * @tc.name: HdfBatteryHdiTest018
333  * @tc.desc: Test limit charging current
334  * @tc.type: FUNC
335  */
336 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest018, TestSize.Level1)
337 {
338     ChargingLimit scLimit;
339     scLimit.type = TYPE_CURRENT;
340     scLimit.protocol = "sc";
341     scLimit.value = 1000;
342     ChargingLimit buckLimit;
343     buckLimit.type = TYPE_CURRENT;
344     buckLimit.protocol = "buck";
345     buckLimit.value = 1100;
346     std::vector<ChargingLimit> chargeLimitList;
347     chargeLimitList.push_back(scLimit);
348     chargeLimitList.push_back(buckLimit);
349     int32_t result = g_batteryInterface->SetChargingLimit(chargeLimitList);
350     EXPECT_EQ(true, result == ERR_OK);
351 
352     std::string currentPath = "/data/service/el0/battery/current_limit";
353     std::string line;
354     std::string chargeLimitStr;
355     std::string writeChargeInfo = scLimit.protocol + " " + std::to_string(scLimit.value) + "\n" +
356         buckLimit.protocol + " " + std::to_string(buckLimit.value) + "\n";
357     std::ifstream fin(currentPath.c_str());
358     if (fin) {
359         while (getline(fin, line)) {
360             chargeLimitStr += line + "\n";
361         }
362     }
363     EXPECT_EQ(true, chargeLimitStr == writeChargeInfo);
364 }
365 
366 /**
367  * @tc.name: HdfBatteryHdiTest019
368  * @tc.desc: Test limit charging voltage
369  * @tc.type: FUNC
370  */
371 HWTEST_F(HdfBatteryHdiTest, HdfBatteryHdiTest019, TestSize.Level1)
372 {
373     ChargingLimit scLimit;
374     scLimit.type = TYPE_VOLTAGE;
375     scLimit.protocol = "sc";
376     scLimit.value = 2000;
377     ChargingLimit buckLimit;
378     buckLimit.type = TYPE_VOLTAGE;
379     buckLimit.protocol = "buck";
380     buckLimit.value = 3000;
381     std::vector<ChargingLimit> chargeLimitList;
382     chargeLimitList.push_back(scLimit);
383     chargeLimitList.push_back(buckLimit);
384     int32_t result = g_batteryInterface->SetChargingLimit(chargeLimitList);
385     EXPECT_EQ(true, result == ERR_OK);
386 
387     std::string voltagePath = "/data/service/el0/battery/voltage_limit";
388     std::string line;
389     std::string voltageLimitStr;
390     std::string writeVoltageInfo = scLimit.protocol + " " + std::to_string(scLimit.value) + "\n" +
391         buckLimit.protocol + " " + std::to_string(buckLimit.value) + "\n";
392     std::ifstream fin(voltagePath.c_str());
393     if (fin) {
394         while (getline(fin, line)) {
395             voltageLimitStr += line + "\n";
396         }
397     }
398     EXPECT_EQ(true, voltageLimitStr == writeVoltageInfo);
399 }
400 }
401