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