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