1 /*
2 * Copyright (c) 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 #include <cJSON.h>
16 #include <gtest/gtest.h>
17 #include "battery_stats_core.h"
18 #include "battery_stats_listener.h"
19 #include "battery_stats_service.h"
20 #include "stats_hisysevent.h"
21 #include "stats_log.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace OHOS::PowerMgr;
26 using namespace OHOS;
27 using namespace std;
28 class StatsServiceConfigParseTestTwo : public Test {
29 public:
SetUp()30 void SetUp() override
31 {
32 root_ = cJSON_CreateObject();
33 }
34
TearDown()35 void TearDown() override
36 {
37 cJSON_Delete(root_);
38 }
39
40 cJSON* root_;
41 };
cJSON_AddNumberToObject(cJSON * const object,const char * const name,const double number)42 CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number)
43 {
44 return nullptr;
45 }
46
cJSON_AddItemToArray(cJSON * array,cJSON * item)47 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item)
48 {
49 return false;
50 }
51
cJSON_AddItemToObject(cJSON * object,const char * string,cJSON * item)52 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
53 {
54 return false;
55 }
56
57 namespace {
58 constexpr size_t NUMBER_UID = 100;
59 HWTEST_F(StatsServiceConfigParseTestTwo, StatsServiceConfigParseTestTwo001, TestSize.Level0)
60 {
61 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestTwo001 function start!");
62 ASSERT_TRUE(root_);
63 auto statsService = BatteryStatsService::GetInstance();
64 EXPECT_TRUE(statsService != nullptr);
65 statsService->OnStart();
66 auto statsCore = statsService->GetBatteryStatsCore();
67 EXPECT_TRUE(statsCore != nullptr);
68 statsCore->SaveForHardware(root_);
69 cJSON* hardwareObj = cJSON_GetObjectItemCaseSensitive(root_, "Hardware");
70 EXPECT_TRUE(hardwareObj == nullptr);
71 hardwareObj = cJSON_CreateObject();
72 statsCore->SaveForHardwareInternal(hardwareObj);
73 cJSON* item = cJSON_GetObjectItemCaseSensitive(root_, "bluetooth_br_on");
74 EXPECT_TRUE(item == nullptr);
75 cJSON_Delete(hardwareObj);
76 statsService->OnStop();
77 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestTwo001 function end!");
78 }
79
80 HWTEST_F(StatsServiceConfigParseTestTwo, StatsServiceConfigParseTestTwo002, TestSize.Level0)
81 {
82 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestTwo002 function start!");
83 ASSERT_TRUE(root_);
84 auto statsService = BatteryStatsService::GetInstance();
85 EXPECT_TRUE(statsService != nullptr);
86 statsService->OnStart();
87 auto statsCore = statsService->GetBatteryStatsCore();
88 EXPECT_TRUE(statsCore != nullptr);
89 statsCore->SaveForSoftwareCommon(root_, NUMBER_UID);
90 cJSON* softwareObj = cJSON_GetObjectItemCaseSensitive(root_, "Software");
91 EXPECT_TRUE(softwareObj == nullptr);
92 cJSON* uidObj = cJSON_CreateObject();
93 statsCore->SaveForSoftwareCommonInternal(uidObj, NUMBER_UID);
94 cJSON* item = cJSON_GetObjectItemCaseSensitive(uidObj, "audio_on");
95 EXPECT_TRUE(item == nullptr);
96 cJSON_Delete(uidObj);
97 statsService->OnStop();
98 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestTwo002 function end!");
99 }
100
101 HWTEST_F(StatsServiceConfigParseTestTwo, StatsServiceConfigParseTestTwo003, TestSize.Level0)
102 {
103 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestTwo003 function start!");
104 ASSERT_TRUE(root_);
105 auto statsService = BatteryStatsService::GetInstance();
106 EXPECT_TRUE(statsService != nullptr);
107 statsService->OnStart();
108 auto statsCore = statsService->GetBatteryStatsCore();
109 EXPECT_TRUE(statsCore != nullptr);
110 statsCore->SaveForSoftwareConnectivity(root_, NUMBER_UID);
111 cJSON* softwareObj = cJSON_GetObjectItemCaseSensitive(root_, "Software");
112 EXPECT_TRUE(softwareObj == nullptr);
113 statsService->OnStop();
114 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestTwo003 function end!");
115 }
116
117 HWTEST_F(StatsServiceConfigParseTestTwo, StatsServiceConfigParseTestTwo004, TestSize.Level0)
118 {
119 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestTwo004 function start!");
120 ASSERT_TRUE(root_);
121 auto statsService = BatteryStatsService::GetInstance();
122 EXPECT_TRUE(statsService != nullptr);
123 statsService->OnStart();
124 auto statsCore = statsService->GetBatteryStatsCore();
125 EXPECT_TRUE(statsCore != nullptr);
126 statsCore->SaveForPower(root_);
127 cJSON* powerObj = cJSON_GetObjectItemCaseSensitive(root_, "Power");
128 EXPECT_TRUE(powerObj == nullptr);
129 statsService->OnStop();
130 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestTwo004 function end!");
131 }
132 } // namespace
133