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 "v1_1/battery_interface_proxy.h"
20 #include "v1_1/types.h"
21 #include "battery_log.h"
22
23 using namespace OHOS::HDI::Battery;
24 using namespace OHOS::HDI::Battery::V1_1;
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
53 namespace {
54 /**
55 * @tc.name: HdiInterfaceTest001
56 * @tc.desc: Test limit charging current
57 * @tc.type: FUNC
58 */
59 HWTEST_F (HdiInterfaceTest, HdiInterfaceTest001, TestSize.Level1)
60 {
61 ChargingLimit scLimit;
62 scLimit.type = TYPE_CURRENT;
63 scLimit.protocol = "sc";
64 scLimit.value = 1000;
65 ChargingLimit buckLimit;
66 buckLimit.type = TYPE_CURRENT;
67 buckLimit.protocol = "buck";
68 buckLimit.value = 1100;
69 std::vector<ChargingLimit> chargeLimitList;
70 chargeLimitList.push_back(scLimit);
71 chargeLimitList.push_back(buckLimit);
72 int32_t result = g_batteryInterface->SetChargingLimit(chargeLimitList);
73 EXPECT_EQ(true, result == ERR_OK);
74
75 std::string currentPath = "/data/service/el0/battery/current_limit";
76 std::string line;
77 std::string chargeLimitStr;
78 std::string writeChargeInfo = scLimit.protocol + " " + std::to_string(scLimit.value) + "\n" +
79 buckLimit.protocol + " " + std::to_string(buckLimit.value) + "\n";
80 std::ifstream fin(currentPath.c_str());
81 if (fin) {
82 while (getline(fin, line)) {
83 chargeLimitStr += line + "\n";
84 }
85 }
86 EXPECT_EQ(true, chargeLimitStr == writeChargeInfo);
87 }
88
89 /**
90 * @tc.name: HdiInterfaceTest002
91 * @tc.desc: Test limit charging voltage
92 * @tc.type: FUNC
93 */
94 HWTEST_F (HdiInterfaceTest, HdiInterfaceTest002, TestSize.Level1)
95 {
96 ChargingLimit scLimit;
97 scLimit.type = TYPE_VOLTAGE;
98 scLimit.protocol = "sc";
99 scLimit.value = 2000;
100 ChargingLimit buckLimit;
101 buckLimit.type = TYPE_VOLTAGE;
102 buckLimit.protocol = "buck";
103 buckLimit.value = 3000;
104 std::vector<ChargingLimit> chargeLimitList;
105 chargeLimitList.push_back(scLimit);
106 chargeLimitList.push_back(buckLimit);
107 int32_t result = g_batteryInterface->SetChargingLimit(chargeLimitList);
108 EXPECT_EQ(true, result == ERR_OK);
109
110 std::string voltagePath = "/data/service/el0/battery/voltage_limit";
111 std::string line;
112 std::string voltageLimitStr;
113 std::string writeVoltageInfo = scLimit.protocol + " " + std::to_string(scLimit.value) + "\n" +
114 buckLimit.protocol + " " + std::to_string(buckLimit.value) + "\n";
115 std::ifstream fin(voltagePath.c_str());
116 if (fin) {
117 while (getline(fin, line)) {
118 voltageLimitStr += line + "\n";
119 }
120 }
121 EXPECT_EQ(true, voltageLimitStr == writeVoltageInfo);
122 }
123 }
124