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