1 /*
2 * Copyright (c) 2022-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 "battery_config_test.h"
17
18 #ifdef GTEST
19 #define private public
20 #define protected public
21 #endif
22 #include "battery_config.h"
23 #include "battery_log.h"
24
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace PowerMgr {
29 namespace {
30 auto& g_configTest = BatteryConfig::GetInstance();
31 }
32
DestroyJsonValue(cJSON * & value)33 void BatteryConfigTest::DestroyJsonValue(cJSON*& value)
34 {
35 if (value) {
36 cJSON_Delete(value);
37 value = nullptr;
38 }
39 }
40
ParseJsonStr(const std::string & jsonStr,bool isAssignToConfig)41 bool BatteryConfigTest::ParseJsonStr(const std::string& jsonStr, bool isAssignToConfig)
42 {
43 cJSON* parseResult = cJSON_Parse(jsonStr.c_str());
44 if (!parseResult) {
45 return false;
46 }
47 if (isAssignToConfig) {
48 g_configTest.config_ = parseResult;
49 }
50 return true;
51 }
52
53 /**
54 * @tc.name: BatteryConfig001
55 * @tc.desc: Parse config, and configPath parameter is real path
56 * @tc.type: FUNC
57 */
58 HWTEST_F(BatteryConfigTest, BatteryConfig001, TestSize.Level1)
59 {
60 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig001 function start!");
61 EXPECT_TRUE(g_configTest.ParseConfig());
62 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig001 function end!");
63 }
64
65 /**
66 * @tc.name: BatteryConfig002
67 * @tc.desc: Get battery light config
68 * @tc.type: FUNC
69 */
70 HWTEST_F(BatteryConfigTest, BatteryConfig002, TestSize.Level1)
71 {
72 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig002 function start!");
73 const std::vector<BatteryConfig::LightConf> lightConf = g_configTest.GetLightConf();
74 EXPECT_TRUE(lightConf.size());
75
76 uint32_t maxRgb = (255 << 16) | (255 << 8) | 255;
77 for (uint32_t i = 0; i < lightConf.size(); ++i) {
78 // The value ranges from 0 to 100
79 EXPECT_TRUE(lightConf[i].beginSoc >= 0 && lightConf[i].beginSoc <= 100);
80 EXPECT_TRUE(lightConf[i].endSoc >= 0 && lightConf[i].endSoc <= 100);
81 // The start range is smaller than the end range
82 EXPECT_TRUE(lightConf[i].beginSoc < lightConf[i].endSoc);
83 // The value ranges from 0 to maxRgb
84 EXPECT_TRUE(lightConf[i].rgb >= 0 && lightConf[i].rgb <= maxRgb);
85 }
86 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig002 function end!");
87 }
88
89 /**
90 * @tc.name: BatteryConfig003
91 * @tc.desc: Get config Int value
92 * @tc.type: FUNC
93 */
94 HWTEST_F(BatteryConfigTest, BatteryConfig003, TestSize.Level1)
95 {
96 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig003 function start!");
97 std::string key = "soc.warning";
98 ASSERT_TRUE(g_configTest.IsExist(key));
99 int32_t invalid = -1;
100 int32_t warnCapacity = g_configTest.GetInt(key, invalid);
101 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig003 warnCapacity=%{public}d", warnCapacity);
102 // The value ranges from 0 to 100
103 EXPECT_TRUE(warnCapacity >= 0 && warnCapacity <= 100);
104 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig003 function end!");
105 }
106
107 /**
108 * @tc.name: BatteryConfig004
109 * @tc.desc: Get config Int value
110 * @tc.type: FUNC
111 */
112 HWTEST_F(BatteryConfigTest, BatteryConfig004, TestSize.Level1)
113 {
114 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig004 function start!");
115 std::string key = "temperature.high";
116 ASSERT_TRUE(g_configTest.IsExist(key));
117 int32_t minTemp = -900; // (-90℃)
118 int32_t maxTemp = 900; // (90℃)
119 int32_t highTemperature = g_configTest.GetInt(key, maxTemp);
120 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig004 highTemperature=%{public}d", highTemperature);
121 // The value ranges from -900 to 900
122 EXPECT_TRUE(highTemperature > minTemp && highTemperature < maxTemp);
123 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig004 function end!");
124 }
125
126 /**
127 * @tc.name: BatteryConfig005
128 * @tc.desc: Get config Int value
129 * @tc.type: FUNC
130 */
131 HWTEST_F(BatteryConfigTest, BatteryConfig005, TestSize.Level1)
132 {
133 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig005 function start!");
134 std::string key = "temperature.low";
135 ASSERT_TRUE(g_configTest.IsExist(key));
136 int32_t minTemp = -900; // (-90℃)
137 int32_t maxTemp = 900; // (90℃)
138 int32_t lowTemperature = g_configTest.GetInt(key, minTemp);
139 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig005 lowTemperature=%{public}d", lowTemperature);
140 // The value ranges from -900 to 900
141 EXPECT_TRUE(lowTemperature < maxTemp && lowTemperature > minTemp);
142 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig005 function end!");
143 }
144
145 /**
146 * @tc.name: BatteryConfig006
147 * @tc.desc: Get config Int value
148 * @tc.type: FUNC
149 */
150 HWTEST_F(BatteryConfigTest, BatteryConfig006, TestSize.Level1)
151 {
152 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig006 function start!");
153 std::string key = "soc.shutdown";
154 ASSERT_TRUE(g_configTest.IsExist(key));
155 int32_t invalid = -1;
156 int32_t shtdwonCapacity = g_configTest.GetInt(key, invalid);
157 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig006 shtdwonCapacity=%{public}d", shtdwonCapacity);
158 // The value ranges from 0 to 100
159 EXPECT_TRUE(shtdwonCapacity >= 0 && shtdwonCapacity <= 100);
160 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig006 function end!");
161 }
162
163 /**
164 * @tc.name: BatteryConfig007
165 * @tc.desc: Get config Int value
166 * @tc.type: FUNC
167 */
168 HWTEST_F(BatteryConfigTest, BatteryConfig007, TestSize.Level1)
169 {
170 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig007 function start!");
171 std::string key = "soc.low";
172 ASSERT_TRUE(g_configTest.IsExist(key));
173 int32_t invalid = -1;
174 int32_t low_battery_event = g_configTest.GetInt(key, invalid);
175 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig007 low_battery_event=%{public}d", low_battery_event);
176 // The value ranges from 0 to 100
177 EXPECT_TRUE(low_battery_event >= 0 && low_battery_event <= 100);
178 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig007 function end!");
179 }
180
181 /**
182 * @tc.name: BatteryConfig008
183 * @tc.desc: Get unknown configuration, return default value
184 * @tc.type: FUNC
185 */
186 HWTEST_F(BatteryConfigTest, BatteryConfig008, TestSize.Level1)
187 {
188 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig008 function start!");
189 int32_t defValue = 100;
190 EXPECT_EQ(defValue, g_configTest.GetInt("XXXXXXXXX", defValue));
191 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig008 function end!");
192 }
193
194 /**
195 * @tc.name: BatteryConfig009
196 * @tc.desc: Get a maximum nesting depth of 5 or more
197 * @tc.type: FUNC
198 */
199 HWTEST_F(BatteryConfigTest, BatteryConfig009, TestSize.Level1)
200 {
201 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig009 function start!");
202 int32_t defValue = 200;
203 EXPECT_EQ(defValue, g_configTest.GetInt("X.X.X.X.X.X", defValue));
204 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig009 function end!");
205 }
206
207 /**
208 * @tc.name: BatteryConfig010
209 * @tc.desc: Get empty configuration, return default value
210 * @tc.type: FUNC
211 */
212 HWTEST_F(BatteryConfigTest, BatteryConfig010, TestSize.Level1)
213 {
214 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig010 function start!");
215 int32_t defValue = 300;
216 EXPECT_EQ(defValue, g_configTest.GetInt("", defValue));
217 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig010 function end!");
218 }
219
220 /**
221 * @tc.name: BatteryConfig0011
222 * @tc.desc: Parse config, and configPath parameter is empty
223 * @tc.type: FUNC
224 * @tc.require: issueI5YZR1
225 */
226 HWTEST_F(BatteryConfigTest, BatteryConfig011, TestSize.Level1)
227 {
228 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0011 function start!");
229 EXPECT_TRUE(g_configTest.ParseConfig());
230 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0011 function end!");
231 }
232
233 /**
234 * @tc.name: BatteryConfig012
235 * @tc.desc: Get config Int value, paramter is real key
236 * @tc.type: FUNC
237 * @tc.require: issueI5YZR1
238 */
239 HWTEST_F(BatteryConfigTest, BatteryConfig012, TestSize.Level1)
240 {
241 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0012 function start!");
242 std::string key = "light.high.soc";
243 ASSERT_TRUE(g_configTest.IsExist(key));
244 int32_t defVal = 90;
245 int32_t highSoc = g_configTest.GetInt(key, defVal);
246 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig012 highSoc=%{public}d", highSoc);
247 // The value ranges from 0 to 100
248 EXPECT_TRUE(highSoc >= 0 && highSoc <= 100);
249 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0012 function end!");
250 }
251
252 /**
253 * @tc.name: BatteryConfig0013
254 * @tc.desc: Get config Int value, paramter is invalid key
255 * @tc.type: FUNC
256 * @tc.require: issueI5YZR1
257 */
258 HWTEST_F(BatteryConfigTest, BatteryConfig013, TestSize.Level1)
259 {
260 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig013 function start!");
261 std::string key = "invalid.key";
262 EXPECT_TRUE(!g_configTest.IsExist(key));
263 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig013 function end!");
264 }
265
266 /**
267 * @tc.name: BatteryConfig0014
268 * @tc.desc: test ParseLightConf
269 * @tc.type: FUNC
270 * @tc.require: issueIBWNLX
271 */
272 HWTEST_F(BatteryConfigTest, BatteryConfig0014, TestSize.Level1)
273 {
274 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0014 function start!");
275 std::string key = "low";
276 g_configTest.lightConf_.clear();
277 std::string jsonStr = R"({"light": {"low": {"soc": "soc", "rgb": []}}})";
278 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
279 g_configTest.ParseLightConf(key);
280 EXPECT_TRUE(g_configTest.lightConf_.size() == 0);
281 DestroyJsonValue(g_configTest.config_);
282 jsonStr = R"({"light": {"low": {"soc": [], "rgb": "rgb"}}})";
283 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
284 g_configTest.ParseLightConf(key);
285 EXPECT_TRUE(g_configTest.lightConf_.size() == 0);
286 DestroyJsonValue(g_configTest.config_);
287 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0014 function end!");
288 }
289
290 /**
291 * @tc.name: BatteryConfig0015
292 * @tc.desc: test ParseLightConf
293 * @tc.type: FUNC
294 * @tc.require: issueIBWNLX
295 */
296 HWTEST_F(BatteryConfigTest, BatteryConfig0015, TestSize.Level1)
297 {
298 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0015 function start!");
299 std::string key = "low";
300 g_configTest.lightConf_.clear();
301 std::string jsonStr = R"({"light": {"low": {"soc": [], "rgb": []}}})";
302 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
303 g_configTest.ParseLightConf(key);
304 EXPECT_TRUE(g_configTest.lightConf_.size() == 0);
305 DestroyJsonValue(g_configTest.config_);
306 jsonStr = R"({"light": {"low": {"soc": ["0", 10], "rgb": []}}})";
307 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
308 g_configTest.ParseLightConf(key);
309 EXPECT_TRUE(g_configTest.lightConf_.size() == 0);
310 DestroyJsonValue(g_configTest.config_);
311 jsonStr = R"({"light": {"low": {"soc": [0, "10"], "rgb": []}}})";
312 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
313 g_configTest.ParseLightConf(key);
314 EXPECT_TRUE(g_configTest.lightConf_.size() == 0);
315 DestroyJsonValue(g_configTest.config_);
316 jsonStr = R"({"light": {"low": {"soc": [0, 10], "rgb": []}}})";
317 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
318 g_configTest.ParseLightConf(key);
319 EXPECT_TRUE(g_configTest.lightConf_.size() == 0);
320 DestroyJsonValue(g_configTest.config_);
321 jsonStr = R"({"light": {"low": {"soc": [0, 10], "rgb": ["255", 0, 0]}}})";
322 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
323 g_configTest.ParseLightConf(key);
324 EXPECT_TRUE(g_configTest.lightConf_.size() == 0);
325 DestroyJsonValue(g_configTest.config_);
326 jsonStr = R"({"light": {"low": {"soc": [0, 10], "rgb": [255, "0", 0]}}})";
327 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
328 g_configTest.ParseLightConf(key);
329 EXPECT_TRUE(g_configTest.lightConf_.size() == 0);
330 DestroyJsonValue(g_configTest.config_);
331 jsonStr = R"({"light": {"low": {"soc": [0, 10], "rgb": [255, 0, "0"]}}})";
332 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
333 g_configTest.ParseLightConf(key);
334 EXPECT_TRUE(g_configTest.lightConf_.size() == 0);
335 DestroyJsonValue(g_configTest.config_);
336 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0015 function end!");
337 }
338
339 /**
340 * @tc.name: BatteryConfig0016
341 * @tc.desc: test ParseWirelessChargerConf
342 * @tc.type: FUNC
343 * @tc.require: issueIBWNLX
344 */
345 HWTEST_F(BatteryConfigTest, BatteryConfig0016, TestSize.Level1)
346 {
347 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0016 function start!");
348 std::string jsonStr = R"({"wirelesscharger": 0})";
349 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
350 g_configTest.ParseWirelessChargerConf();
351 EXPECT_FALSE(g_configTest.wirelessChargerEnable_);
352 DestroyJsonValue(g_configTest.config_);
353 jsonStr = R"({"wirelesscharger": null})";
354 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
355 g_configTest.ParseWirelessChargerConf();
356 EXPECT_FALSE(g_configTest.wirelessChargerEnable_);
357 DestroyJsonValue(g_configTest.config_);
358 jsonStr = R"({"wirelesscharger": "0"})";
359 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
360 g_configTest.ParseWirelessChargerConf();
361 EXPECT_FALSE(g_configTest.wirelessChargerEnable_);
362 DestroyJsonValue(g_configTest.config_);
363 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0016 function end!");
364 }
365
366 /**
367 * @tc.name: BatteryConfig0017
368 * @tc.desc: test ParseBootActionsConf
369 * @tc.type: FUNC
370 * @tc.require: issueIBWNLX
371 */
372 HWTEST_F(BatteryConfigTest, BatteryConfig0017, TestSize.Level1)
373 {
374 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0017 function start!");
375 g_configTest.commonEventConf_.clear();
376 g_configTest.ParseBootActionsConf();
377 EXPECT_TRUE(g_configTest.commonEventConf_.size() == 0);
378 std::string jsonStr = R"({"boot_actions": ""})";
379 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
380 g_configTest.ParseBootActionsConf();
381 EXPECT_TRUE(g_configTest.commonEventConf_.size() == 0);
382 DestroyJsonValue(g_configTest.config_);
383 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0017 function end!");
384 }
385
386 /**
387 * @tc.name: BatteryConfig0018
388 * @tc.desc: test ParseCommonEventConf part1
389 * @tc.type: FUNC
390 * @tc.require: issueIBWNLX
391 */
392 HWTEST_F(BatteryConfigTest, BatteryConfig0018, TestSize.Level1)
393 {
394 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0018 function start!");
395 g_configTest.ParseCommonEventConf(nullptr);
396 EXPECT_TRUE(g_configTest.commonEventConf_.size() == 0);
397 std::string jsonStr = R"({"sendcommonevent": ""})";
398 cJSON* parseResult = cJSON_Parse(jsonStr.c_str());
399 EXPECT_TRUE(parseResult);
400 g_configTest.ParseCommonEventConf(parseResult);
401 EXPECT_TRUE(g_configTest.commonEventConf_.size() == 0);
402 DestroyJsonValue(parseResult);
403 jsonStr = R"({"sendcommonevent": [{
404 "event_name": 123,
405 "scene_config": { "name" : "wireless", "not_equal" : "0" },
406 "uevent": "battery common event"
407 }]})";
408 parseResult = cJSON_Parse(jsonStr.c_str());
409 EXPECT_TRUE(parseResult);
410 g_configTest.ParseCommonEventConf(parseResult);
411 EXPECT_TRUE(g_configTest.commonEventConf_.size() == 0);
412 DestroyJsonValue(parseResult);
413 jsonStr = R"({"sendcommonevent": [{
414 "event_name": "usual.event.BATTERY_CHANGED",
415 "scene_config": { "name" : 123, "not_equal" : "0" },
416 "uevent": "battery common event"
417 }]})";
418 parseResult = cJSON_Parse(jsonStr.c_str());
419 EXPECT_TRUE(parseResult);
420 g_configTest.ParseCommonEventConf(parseResult);
421 EXPECT_TRUE(g_configTest.commonEventConf_.size() == 0);
422 DestroyJsonValue(parseResult);
423 jsonStr = R"({"sendcommonevent": [{
424 "event_name": "usual.event.BATTERY_CHANGED",
425 "scene_config": { "name" : "wireless", "not_equal" : "0" },
426 "uevent": 123
427 }]})";
428 parseResult = cJSON_Parse(jsonStr.c_str());
429 EXPECT_TRUE(parseResult);
430 g_configTest.ParseCommonEventConf(parseResult);
431 EXPECT_TRUE(g_configTest.commonEventConf_.size() == 0);
432 DestroyJsonValue(parseResult);
433 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0018 function end!");
434 }
435
436 /**
437 * @tc.name: BatteryConfig0019
438 * @tc.desc: test ParseCommonEventConf part2
439 * @tc.type: FUNC
440 * @tc.require: issueIBWNLX
441 */
442 HWTEST_F(BatteryConfigTest, BatteryConfig0019, TestSize.Level1)
443 {
444 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0019 function start!");
445 std::string jsonStr = R"({"sendcommonevent": [{
446 "event_name": "usual.event.BATTERY_CHANGED",
447 "scene_config": { "name" : "wireless", "equal" : "0" },
448 "uevent": "battery common event"
449 }]})";
450 cJSON* parseResult = cJSON_Parse(jsonStr.c_str());
451 EXPECT_TRUE(parseResult);
452 g_configTest.ParseCommonEventConf(parseResult);
453 EXPECT_TRUE(g_configTest.commonEventConf_.size() == 1);
454 DestroyJsonValue(parseResult);
455 jsonStr = R"({"sendcommonevent": [{
456 "event_name": "usual.event.BATTERY_CHANGED",
457 "scene_config": { "name" : "wireless", "equal" : null },
458 "uevent": "battery common event"
459 }]})";
460 parseResult = cJSON_Parse(jsonStr.c_str());
461 EXPECT_TRUE(parseResult);
462 g_configTest.ParseCommonEventConf(parseResult);
463 DestroyJsonValue(parseResult);
464 EXPECT_TRUE(g_configTest.commonEventConf_.size() == 0);
465 jsonStr = R"({"sendcommonevent": [{
466 "event_name": "usual.event.BATTERY_CHANGED",
467 "scene_config": { "name" : "wireless", "not_equal" : null },
468 "uevent": "battery common event"
469 }]})";
470 parseResult = cJSON_Parse(jsonStr.c_str());
471 EXPECT_TRUE(parseResult);
472 g_configTest.ParseCommonEventConf(parseResult);
473 EXPECT_TRUE(g_configTest.commonEventConf_.size() == 0);
474 DestroyJsonValue(parseResult);
475 jsonStr = R"({"sendcommonevent": [{
476 "event_name": "usual.event.BATTERY_CHANGED",
477 "scene_config": { "name" : "wireless", "equal" : 0 },
478 "uevent": "battery common event"
479 }]})";
480 parseResult = cJSON_Parse(jsonStr.c_str());
481 EXPECT_TRUE(parseResult);
482 g_configTest.ParseCommonEventConf(parseResult);
483 EXPECT_TRUE(g_configTest.commonEventConf_.size() == 0);
484 DestroyJsonValue(parseResult);
485 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0019 function end!");
486 }
487
488 /**
489 * @tc.name: BatteryConfig0020
490 * @tc.desc: test ParsePopupConf
491 * @tc.type: FUNC
492 * @tc.require: issueIBWNLX
493 */
494 HWTEST_F(BatteryConfigTest, BatteryConfig0020, TestSize.Level1)
495 {
496 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0020 function start!");
497 std::string jsonStr = R"({"popup": null})";
498 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
499 g_configTest.ParsePopupConf();
500 EXPECT_TRUE(g_configTest.popupConfig_.size() == 0);
501 DestroyJsonValue(g_configTest.config_);
502 jsonStr = R"({"popup": "popup"})";
503 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
504 g_configTest.ParsePopupConf();
505 EXPECT_TRUE(g_configTest.popupConfig_.size() == 0);
506 DestroyJsonValue(g_configTest.config_);
507 jsonStr = R"({"popup": {"XXX": null}})";
508 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
509 g_configTest.ParsePopupConf();
510 EXPECT_TRUE(g_configTest.popupConfig_.size() == 0);
511 DestroyJsonValue(g_configTest.config_);
512 jsonStr = R"({"popup": {"XXX": "XXX"}})";
513 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
514 g_configTest.ParsePopupConf();
515 EXPECT_TRUE(g_configTest.popupConfig_.size() == 0);
516 DestroyJsonValue(g_configTest.config_);
517 jsonStr = R"({"popup": {"XXX": [{"name": 123}]}})";
518 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
519 g_configTest.ParsePopupConf();
520 EXPECT_TRUE(g_configTest.popupConfig_.size() == 1);
521 DestroyJsonValue(g_configTest.config_);
522 jsonStr = R"({"popup": {"XXX": [{"name": "123", "action": "456"}]}})";
523 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
524 g_configTest.ParsePopupConf();
525 EXPECT_TRUE(g_configTest.popupConfig_.size() == 1);
526 DestroyJsonValue(g_configTest.config_);
527 jsonStr = R"({"popup": {"XXX": [{"name": "123", "action": 456}]}})";
528 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
529 g_configTest.ParsePopupConf();
530 EXPECT_TRUE(g_configTest.popupConfig_.size() == 1);
531 DestroyJsonValue(g_configTest.config_);
532 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0020 function end!");
533 }
534
535 /**
536 * @tc.name: BatteryConfig0021
537 * @tc.desc: test ParseNotificationConf part1
538 * @tc.type: FUNC
539 * @tc.require: issueIBWNLX
540 */
541 HWTEST_F(BatteryConfigTest, BatteryConfig0021, TestSize.Level1)
542 {
543 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0021 function start!");
544 std::string jsonStr = R"({"notification": null})";
545 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
546 g_configTest.ParseNotificationConf();
547 EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
548 DestroyJsonValue(g_configTest.config_);
549 jsonStr = R"({"notification": "notification"})";
550 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
551 g_configTest.ParseNotificationConf();
552 EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
553 DestroyJsonValue(g_configTest.config_);
554 jsonStr = R"({"notification": [{"name": 1}]})";
555 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
556 g_configTest.ParseNotificationConf();
557 EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
558 DestroyJsonValue(g_configTest.config_);
559 jsonStr = R"({"notification": [{"name": "1", "icon": 2}]})";
560 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
561 g_configTest.ParseNotificationConf();
562 EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
563 DestroyJsonValue(g_configTest.config_);
564 jsonStr = R"({"notification": [{"name": "1", "icon": "2", "title": 3}]})";
565 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
566 g_configTest.ParseNotificationConf();
567 EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
568 DestroyJsonValue(g_configTest.config_);
569 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0021 function end!");
570 }
571
572 /**
573 * @tc.name: BatteryConfig0022
574 * @tc.desc: test ParseNotificationConf part2
575 * @tc.type: FUNC
576 * @tc.require: issueIBWNLX
577 */
578 HWTEST_F(BatteryConfigTest, BatteryConfig0022, TestSize.Level1)
579 {
580 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0022 function start!");
581 std::string jsonStr = R"({"notification": [{"name": "1", "icon": "2", "title": "3", "text": 4}]})";
582 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
583 g_configTest.ParseNotificationConf();
584 EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
585 DestroyJsonValue(g_configTest.config_);
586 jsonStr = R"({"notification": [{"name": "1", "icon": "2", "title": "3", "text": "4", "button": 5}]})";
587 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
588 g_configTest.ParseNotificationConf();
589 DestroyJsonValue(g_configTest.config_);
590 jsonStr = R"({"notification": [{"name": "1", "icon": "2", "title": "3", "text": "4", "button": []}]})";
591 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
592 g_configTest.ParseNotificationConf();
593 EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
594 DestroyJsonValue(g_configTest.config_);
595 jsonStr = R"({"notification": [{"name": "1", "icon": "2", "title": "3", "text": "4", "button": [1, {}]}]})";
596 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
597 g_configTest.ParseNotificationConf();
598 EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
599 DestroyJsonValue(g_configTest.config_);
600 jsonStr = R"({"notification": [{"name": "1", "icon": "2", "title": "3", "text": "4", "button": [{}, 2]}]})";
601 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
602 g_configTest.ParseNotificationConf();
603 EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
604 DestroyJsonValue(g_configTest.config_);
605 jsonStr = R"({"notification": [{"name": "1", "icon": "2", "title": "3", "text": "4",
606 "button": [{"name": 1}, {}]}]})";
607 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
608 g_configTest.ParseNotificationConf();
609 EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
610 DestroyJsonValue(g_configTest.config_);
611 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0022 function end!");
612 }
613
614 /**
615 * @tc.name: BatteryConfig0023
616 * @tc.desc: test ParseNotificationConf part3
617 * @tc.type: FUNC
618 * @tc.require: issueIBWNLX
619 */
620 HWTEST_F(BatteryConfigTest, BatteryConfig0023, TestSize.Level1)
621 {
622 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0023 function start!");
623 std::string jsonStr = R"({"notification": [{"name": "1", "icon": "2", "title": "3", "text": "4",
624 "button": [{"name": "1", "action": 1}, {}]}]})";
625 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
626 g_configTest.ParseNotificationConf();
627 EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
628 DestroyJsonValue(g_configTest.config_);
629 jsonStr = R"({"notification": [{"name": "1", "icon": "2", "title": "3", "text": "4",
630 "button": [{"name": "1", "action": "1"}, {"name": 2}]}]})";
631 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
632 g_configTest.ParseNotificationConf();
633 EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
634 DestroyJsonValue(g_configTest.config_);
635 jsonStr = R"({"notification": [{"name": "1", "icon": "2", "title": "3", "text": "4",
636 "button": [{"name": "1", "action": "1"}, {"name": "2", "action": 2}]}]})";
637 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
638 g_configTest.ParseNotificationConf();
639 EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
640 DestroyJsonValue(g_configTest.config_);
641 jsonStr = R"({"notification": [{"name": "1", "icon": "2", "title": "3", "text": "4",
642 "button": [{"name": "1", "action": "1"}, {"name": "2", "action": "2"}]}]})";
643 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
644 g_configTest.ParseNotificationConf();
645 EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 1);
646 DestroyJsonValue(g_configTest.config_);
647 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0023 function end!");
648 }
649
650 /**
651 * @tc.name: BatteryConfig0024
652 * @tc.desc: test GetNotificationConf
653 * @tc.type: FUNC
654 * @tc.require: issueIBWNLX
655 */
656 HWTEST_F(BatteryConfigTest, BatteryConfig0024, TestSize.Level1)
657 {
658 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0024 function start!");
659 auto& ntfMap = g_configTest.GetNotificationConf();
660 EXPECT_TRUE(ntfMap.size() != 0);
661 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0024 function end!");
662 }
663
664 /**
665 * @tc.name: BatteryConfig0025
666 * @tc.desc: test GetValue
667 * @tc.type: FUNC
668 * @tc.require: issueIBWNLX
669 */
670 HWTEST_F(BatteryConfigTest, BatteryConfig0025, TestSize.Level1)
671 {
672 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0025 function start!");
673 std::string str = "";
674 cJSON* jsonValue = g_configTest.GetValue(str);
675 EXPECT_TRUE((!jsonValue || cJSON_IsNull(jsonValue)));
676 str = "light.low.rbg.a.b.c";
677 jsonValue = g_configTest.GetValue(str);
678 EXPECT_TRUE((!jsonValue || cJSON_IsNull(jsonValue)));
679 std::string jsonStr = R"({"light": {"low": {"soc": "soc"}}})";
680 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
681 str = "light.low.rbg";
682 jsonValue = g_configTest.GetValue(str);
683 EXPECT_FALSE((jsonValue && cJSON_IsNull(jsonValue)));
684 str = "light.low.soc";
685 jsonValue = g_configTest.GetValue(str);
686 EXPECT_TRUE((strcmp(cJSON_GetStringValue(jsonValue), "soc") == 0));
687 str = "light.low.soc.a";
688 jsonValue = g_configTest.GetValue(str);
689 EXPECT_TRUE((strcmp(cJSON_GetStringValue(jsonValue), "soc") == 0));
690 DestroyJsonValue(g_configTest.config_);
691 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0025 function end!");
692 }
693
694 /**
695 * @tc.name: BatteryConfig0026
696 * @tc.desc: test ParseBootActionsConf
697 * @tc.type: FUNC
698 */
699 HWTEST_F(BatteryConfigTest, BatteryConfig0026, TestSize.Level1)
700 {
701 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0026 function start!");
702 g_configTest.commonEventConf_.clear();
703 std::string jsonStr = R"({"boot_actions": {"sendcommonevent": ""}})";
704 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
705 g_configTest.ParseBootActionsConf();
706 EXPECT_TRUE(g_configTest.commonEventConf_.size() == 0);
707 DestroyJsonValue(g_configTest.config_);
708
709 jsonStr = R"({"boot_actions": null})";
710 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
711 g_configTest.ParseBootActionsConf();
712 EXPECT_TRUE(g_configTest.commonEventConf_.size() == 0);
713 DestroyJsonValue(g_configTest.config_);
714
715 jsonStr = R"({"boot_actions": {"sendcommonevent": null}})";
716 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
717 g_configTest.ParseBootActionsConf();
718 EXPECT_TRUE(g_configTest.commonEventConf_.size() == 0);
719 DestroyJsonValue(g_configTest.config_);
720 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0026 function end!");
721 }
722
723 /**
724 * @tc.name: BatteryConfig0027
725 * @tc.desc: test ParsePopupConf
726 * @tc.type: FUNC
727 */
728 HWTEST_F(BatteryConfigTest, BatteryConfig0027, TestSize.Level1)
729 {
730 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0027 function start!");
731 g_configTest.popupConfig_.clear();
732 std::string jsonStr = R"({"popup": {"XXX": [{"action": 456},{"name": "123"},{"name": 123, "action": 456}]}})";
733 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
734 g_configTest.ParsePopupConf();
735 EXPECT_TRUE(g_configTest.popupConfig_.size() == 1);
736 DestroyJsonValue(g_configTest.config_);
737
738 jsonStr = R"({"popup": {"XXX": [{"name": "123", "action": "456"}]}})";
739 ASSERT_TRUE(ParseJsonStr(jsonStr, true));
740 g_configTest.ParsePopupConf();
741 EXPECT_TRUE(g_configTest.popupConfig_.size() == 1);
742 DestroyJsonValue(g_configTest.config_);
743 BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0027 function end!");
744 }
745 } // namespace PowerMgr
746 } // namespace OHOS
747