• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_battery_config_test.h"
17 
18 #include "battery_config.h"
19 
20 using namespace OHOS::HDI::Battery;
21 using namespace OHOS::HDI::Battery::V2_0;
22 using namespace testing::ext;
23 using namespace OHOS;
24 
25 namespace {
26 auto& g_configTest = BatteryConfig::GetInstance();
27 }
28 
SetUpTestCase(void)29 void HdiBatteryConfigTest::SetUpTestCase(void)
30 {
31 }
32 
TearDownTestCase(void)33 void HdiBatteryConfigTest::TearDownTestCase(void)
34 {
35 }
36 
DestroyJsonValue(cJSON * & value)37 void HdiBatteryConfigTest::DestroyJsonValue(cJSON*& value)
38 {
39     if (value) {
40         cJSON_Delete(value);
41         value = nullptr;
42     }
43 }
44 
45 namespace {
46 /**
47  * @tc.name: HdiBatteryConfigTest001
48  * @tc.desc: Test ParseChargerConfig
49  * @tc.type: FUNC
50  */
51 HWTEST_F(HdiBatteryConfigTest, HdiBatteryConfigTest001, TestSize.Level0)
52 {
53     std::string jsonStr = R"({"current_limit": {"path": "/test/current_limit"}})";
54     cJSON* parseResult = cJSON_Parse(jsonStr.c_str());
55     ASSERT_TRUE(parseResult);
56     g_configTest.ParseChargerConfig(parseResult);
57     EXPECT_TRUE(g_configTest.chargerConfig_.currentPath == "/test/current_limit");
58     DestroyJsonValue(parseResult);
59 
60     jsonStr = R"({"voltage_limit": {"path": "/test/voltage_limit"}})";
61     parseResult = cJSON_Parse(jsonStr.c_str());
62     ASSERT_TRUE(parseResult);
63     g_configTest.ParseChargerConfig(parseResult);
64     EXPECT_TRUE(g_configTest.chargerConfig_.voltagePath == "/test/voltage_limit");
65     DestroyJsonValue(parseResult);
66 
67     jsonStr = R"({"type": {"path": "/test/type"}})";
68     parseResult = cJSON_Parse(jsonStr.c_str());
69     ASSERT_TRUE(parseResult);
70     g_configTest.ParseChargerConfig(parseResult);
71     EXPECT_TRUE(g_configTest.chargerConfig_.chargeTypePath == "/test/type");
72     DestroyJsonValue(parseResult);
73 }
74 }
75