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 "battery_config_test.h" 17 #include "battery_config.h" 18 #include "battery_log.h" 19 20 using namespace testing::ext; 21 22 namespace OHOS { 23 namespace HDI { 24 namespace Battery { 25 namespace V1_1 { 26 namespace { 27 constexpr const char* VENDOR_BATTERY_CONFIG_PATH = "/vendor/etc/battery/battery_config.json"; 28 auto& g_configTest = BatteryConfig::GetInstance(); 29 } 30 31 /** 32 * @tc.name: BatteryConfig001 33 * @tc.desc: Parse config 34 * @tc.type: FUNC 35 */ 36 HWTEST_F (BatteryConfigTest, BatteryConfig001, TestSize.Level1) 37 { 38 ASSERT_TRUE(g_configTest.ParseConfig(VENDOR_BATTERY_CONFIG_PATH)); 39 } 40 41 /** 42 * @tc.name: BatteryConfig002 43 * @tc.desc: Get battery light config 44 * @tc.type: FUNC 45 */ 46 HWTEST_F (BatteryConfigTest, BatteryConfig002, TestSize.Level1) 47 { 48 BATTERY_HILOGD(LABEL_TEST, "BatteryConfig002 begin"); 49 const std::vector<BatteryConfig::LightConf> lightConf = g_configTest.GetLightConf(); 50 ASSERT_TRUE(lightConf.size()); 51 52 uint32_t maxRgb = (255 << 16) | (255 << 8) | 255; 53 for (uint32_t i = 0; i < lightConf.size(); ++i) { 54 // The value ranges from 0 to 100 55 ASSERT_TRUE(lightConf[i].beginSoc >= 0 && lightConf[i].beginSoc <= 100); 56 ASSERT_TRUE(lightConf[i].endSoc >= 0 && lightConf[i].endSoc <= 100); 57 // The start range is smaller than the end range 58 ASSERT_TRUE(lightConf[i].beginSoc < lightConf[i].endSoc); 59 // The value ranges from 0 to maxRgb 60 ASSERT_TRUE(lightConf[i].rgb >= 0 && lightConf[i].rgb <= maxRgb); 61 } 62 BATTERY_HILOGD(LABEL_TEST, "BatteryConfig002 end"); 63 } 64 65 /** 66 * @tc.name: BatteryConfig004 67 * @tc.desc: Get config Int value 68 * @tc.type: FUNC 69 */ 70 HWTEST_F (BatteryConfigTest, BatteryConfig004, TestSize.Level1) 71 { 72 std::string key = "soc.warning"; 73 if (!g_configTest.IsExist(key)) { 74 BATTERY_HILOGD(LABEL_TEST, "BatteryConfig004 %{public}s does not exist", key.c_str()); 75 return; 76 } 77 int32_t invalid = -1; 78 int32_t warnCapacity = g_configTest.GetInt(key, invalid); 79 BATTERY_HILOGD(LABEL_TEST, "BatteryConfig004 warnCapacity=%{public}d", warnCapacity); 80 // The value ranges from 0 to 100 81 ASSERT_TRUE(warnCapacity >= 0 && warnCapacity <= 100); 82 } 83 84 /** 85 * @tc.name: BatteryConfig005 86 * @tc.desc: Get config Int value 87 * @tc.type: FUNC 88 */ 89 HWTEST_F (BatteryConfigTest, BatteryConfig005, TestSize.Level1) 90 { 91 std::string key = "temperature.high"; 92 if (!g_configTest.IsExist(key)) { 93 BATTERY_HILOGD(LABEL_TEST, "BatteryConfig005 %{public}s does not exist", key.c_str()); 94 return; 95 } 96 int32_t minTemp = -900; // (-90℃) 97 int32_t maxTemp = 900; // (90℃) 98 int32_t highTemperature = g_configTest.GetInt(key, maxTemp); 99 BATTERY_HILOGD(LABEL_TEST, "BatteryConfig005 highTemperature=%{public}d", highTemperature); 100 // The value ranges from -900 to 900 101 ASSERT_TRUE(highTemperature > minTemp && highTemperature < maxTemp); 102 } 103 104 /** 105 * @tc.name: BatteryConfig006 106 * @tc.desc: Get config Int value 107 * @tc.type: FUNC 108 */ 109 HWTEST_F (BatteryConfigTest, BatteryConfig006, TestSize.Level1) 110 { 111 std::string key = "temperature.low"; 112 if (!g_configTest.IsExist(key)) { 113 BATTERY_HILOGD(LABEL_TEST, "BatteryConfig006 %{public}s does not exist", key.c_str()); 114 return; 115 } 116 int32_t minTemp = -900; // (-90℃) 117 int32_t maxTemp = 900; // (90℃) 118 int32_t lowTemperature = g_configTest.GetInt(key, minTemp); 119 BATTERY_HILOGD(LABEL_TEST, "BatteryConfig006 lowTemperature=%{public}d", lowTemperature); 120 // The value ranges from -900 to 900 121 ASSERT_TRUE(lowTemperature < maxTemp && lowTemperature > minTemp); 122 } 123 124 /** 125 * @tc.name: BatteryConfig007 126 * @tc.desc: Get config Int value 127 * @tc.type: FUNC 128 */ 129 HWTEST_F (BatteryConfigTest, BatteryConfig007, TestSize.Level1) 130 { 131 std::string key = "soc.shutdown"; 132 if (!g_configTest.IsExist(key)) { 133 BATTERY_HILOGD(LABEL_TEST, "BatteryConfig007 %{public}s does not exist", key.c_str()); 134 return; 135 } 136 int32_t invalid = -1; 137 int32_t shtdwonCapacity = g_configTest.GetInt(key, invalid); 138 BATTERY_HILOGD(LABEL_TEST, "BatteryConfig007 shtdwonCapacity=%{public}d", shtdwonCapacity); 139 // The value ranges from 0 to 100 140 ASSERT_TRUE(shtdwonCapacity >= 0 && shtdwonCapacity <= 100); 141 } 142 143 /** 144 * @tc.name: BatteryConfig008 145 * @tc.desc: Get config Int value 146 * @tc.type: FUNC 147 */ 148 HWTEST_F (BatteryConfigTest, BatteryConfig008, TestSize.Level1) 149 { 150 std::string key = "soc.low"; 151 if (!g_configTest.IsExist(key)) { 152 BATTERY_HILOGD(LABEL_TEST, "BatteryConfig008 %{public}s does not exist", key.c_str()); 153 return; 154 } 155 int32_t invalid = -1; 156 int32_t low_battery_event = g_configTest.GetInt(key, invalid); 157 BATTERY_HILOGD(LABEL_TEST, "BatteryConfig008 low_battery_event=%{public}d", low_battery_event); 158 // The value ranges from 0 to 100 159 ASSERT_TRUE(low_battery_event >= 0 && low_battery_event <= 100); 160 } 161 162 /** 163 * @tc.name: BatteryConfig009 164 * @tc.desc: Get config String value 165 * @tc.type: FUNC 166 */ 167 HWTEST_F (BatteryConfigTest, BatteryConfig009, TestSize.Level1) 168 { 169 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig009 begin"); 170 171 std::string currentPath = g_configTest.GetString("charger.current_limit.path"); 172 ASSERT_TRUE(!currentPath.empty()); 173 std::string voltagePath = g_configTest.GetString("charger.voltage_limit.path"); 174 ASSERT_TRUE(!voltagePath.empty()); 175 176 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig009 end"); 177 } 178 179 /** 180 * @tc.name: BatteryConfig010 181 * @tc.desc: Get unknown configuration, return default value 182 * @tc.type: FUNC 183 */ 184 HWTEST_F (BatteryConfigTest, BatteryConfig010, TestSize.Level1) 185 { 186 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig010 begin"); 187 int32_t defValue = 100; 188 ASSERT_EQ(defValue, g_configTest.GetInt("XXXXXXXXX", defValue)); 189 ASSERT_EQ("123", g_configTest.GetString("XXXXXXXXX", "123")); 190 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig010 end"); 191 } 192 193 /** 194 * @tc.name: BatteryConfig011 195 * @tc.desc: Get a maximum nesting depth of 5 or more 196 * @tc.type: FUNC 197 */ 198 HWTEST_F (BatteryConfigTest, BatteryConfig011, TestSize.Level1) 199 { 200 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig011 begin"); 201 int32_t defValue = 200; 202 ASSERT_EQ(defValue, g_configTest.GetInt("X.X.X.X.X.X", defValue)); 203 ASSERT_EQ("", g_configTest.GetString("X.X.X.X.X.X")); 204 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig011 end"); 205 } 206 207 /** 208 * @tc.name: BatteryConfig012 209 * @tc.desc: Get empty configuration, return default value 210 * @tc.type: FUNC 211 */ 212 HWTEST_F (BatteryConfigTest, BatteryConfig012, TestSize.Level1) 213 { 214 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig012 begin"); 215 int32_t defValue = 300; 216 ASSERT_EQ(defValue, g_configTest.GetInt("", defValue)); 217 ASSERT_EQ("", g_configTest.GetString("")); 218 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig012 end"); 219 } 220 } // namespace V1_1 221 } // namespace Battery 222 } // namespace HDI 223 } // namespace OHOS 224