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 "hdi_interface_test.h"
17 
18 #include <fstream>
19 #include "v2_0/battery_interface_proxy.h"
20 #include "v2_0/types.h"
21 #include "battery_log.h"
22 
23 using namespace OHOS::HDI::Battery;
24 using namespace OHOS::HDI::Battery::V2_0;
25 using namespace testing::ext;
26 using namespace OHOS;
27 
28 namespace {
29 sptr<IBatteryInterface> g_batteryInterface = nullptr;
30 }
31 
SetUpTestCase(void)32 void HdiInterfaceTest::SetUpTestCase(void)
33 {
34     g_batteryInterface = IBatteryInterface::Get(true);
35     if (g_batteryInterface == nullptr) {
36         BATTERY_HILOGI(LABEL_TEST, "Failed to get g_batteryInterface");
37         return;
38     }
39 }
40 
TearDownTestCase(void)41 void HdiInterfaceTest::TearDownTestCase(void)
42 {
43 }
44 
SetUp(void)45 void HdiInterfaceTest::SetUp(void)
46 {
47 }
48 
TearDown(void)49 void HdiInterfaceTest::TearDown(void)
50 {
51 }
52 
CreateFile(std::string path,std::string content)53 std::string CreateFile(std::string path, std::string content)
54 {
55     std::ofstream stream(path.c_str());
56     if (!stream.is_open()) {
57         BATTERY_HILOGI(LABEL_TEST, "Cannot create file");
58         return nullptr;
59     }
60     stream << content.c_str() << std::endl;
61     stream.close();
62     return path;
63 }
64 
65 namespace {
66 /**
67  * @tc.name: HdiInterfaceTest001
68  * @tc.desc: Test limit charging current
69  * @tc.type: FUNC
70  */
71 HWTEST_F (HdiInterfaceTest, HdiInterfaceTest001, TestSize.Level1)
72 {
73     std::string currentPath = "/data/service/el0/battery/current_limit";
74     CreateFile(currentPath, "");
75     ChargingLimit scLimit;
76     scLimit.type = TYPE_CURRENT;
77     scLimit.protocol = "sc";
78     scLimit.value = 1000;
79     ChargingLimit buckLimit;
80     buckLimit.type = TYPE_CURRENT;
81     buckLimit.protocol = "buck";
82     buckLimit.value = 1100;
83     std::vector<ChargingLimit> chargeLimitList;
84     chargeLimitList.push_back(scLimit);
85     chargeLimitList.push_back(buckLimit);
86     int32_t result = g_batteryInterface->SetChargingLimit(chargeLimitList);
87     EXPECT_EQ(true, result == ERR_OK);
88 
89     std::string line;
90     std::string chargeLimitStr;
91     std::string writeChargeInfo = scLimit.protocol + " " + std::to_string(scLimit.value) + "\n" +
92         buckLimit.protocol + " " + std::to_string(buckLimit.value) + "\n";
93     std::ifstream fin(currentPath.c_str());
94     if (fin) {
95         while (getline(fin, line)) {
96             chargeLimitStr += line + "\n";
97         }
98     }
99     EXPECT_EQ(true, chargeLimitStr == writeChargeInfo);
100 }
101 
102 /**
103  * @tc.name: HdiInterfaceTest002
104  * @tc.desc: Test limit charging voltage
105  * @tc.type: FUNC
106  */
107 HWTEST_F (HdiInterfaceTest, HdiInterfaceTest002, TestSize.Level1)
108 {
109     std::string voltagePath = "/data/service/el0/battery/voltage_limit";
110     CreateFile(voltagePath, "");
111     ChargingLimit scLimit;
112     scLimit.type = TYPE_VOLTAGE;
113     scLimit.protocol = "sc";
114     scLimit.value = 2000;
115     ChargingLimit buckLimit;
116     buckLimit.type = TYPE_VOLTAGE;
117     buckLimit.protocol = "buck";
118     buckLimit.value = 3000;
119     std::vector<ChargingLimit> chargeLimitList;
120     chargeLimitList.push_back(scLimit);
121     chargeLimitList.push_back(buckLimit);
122     int32_t result = g_batteryInterface->SetChargingLimit(chargeLimitList);
123     EXPECT_EQ(true, result == ERR_OK);
124 
125     std::string line;
126     std::string voltageLimitStr;
127     std::string writeVoltageInfo = scLimit.protocol + " " + std::to_string(scLimit.value) + "\n" +
128         buckLimit.protocol + " " + std::to_string(buckLimit.value) + "\n";
129     std::ifstream fin(voltagePath.c_str());
130     if (fin) {
131         while (getline(fin, line)) {
132             voltageLimitStr += line + "\n";
133         }
134     }
135     EXPECT_EQ(true, voltageLimitStr == writeVoltageInfo);
136 }
137 }
138