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 StatsServiceConfigParseTestThree : 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
52 namespace {
53 constexpr size_t NUMBER_UID = 100;
54 HWTEST_F(StatsServiceConfigParseTestThree, StatsServiceConfigParseTestThree001, TestSize.Level0)
55 {
56 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestThree001 function start!");
57 ASSERT_TRUE(root_);
58 auto statsService = BatteryStatsService::GetInstance();
59 EXPECT_TRUE(statsService != nullptr);
60 statsService->OnStart();
61 auto statsCore = statsService->GetBatteryStatsCore();
62 EXPECT_TRUE(statsCore != nullptr);
63 statsCore->SaveForHardware(root_);
64 cJSON* hardwareObj = cJSON_GetObjectItemCaseSensitive(root_, "Hardware");
65 EXPECT_TRUE(hardwareObj != nullptr);
66 statsCore->SaveForHardwareInternal(hardwareObj);
67 cJSON* item = cJSON_GetObjectItemCaseSensitive(root_, "bluetooth_br_on");
68 EXPECT_TRUE(item == nullptr);
69 statsService->OnStop();
70 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestThree001 function end!");
71 }
72
73 HWTEST_F(StatsServiceConfigParseTestThree, StatsServiceConfigParseTestThree002, TestSize.Level0)
74 {
75 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestThree002 function start!");
76 ASSERT_TRUE(root_);
77 auto statsService = BatteryStatsService::GetInstance();
78 EXPECT_TRUE(statsService != nullptr);
79 statsService->OnStart();
80 auto statsCore = statsService->GetBatteryStatsCore();
81 EXPECT_TRUE(statsCore != nullptr);
82 statsCore->SaveForSoftwareCommon(root_, NUMBER_UID);
83 cJSON* softwareObj = cJSON_GetObjectItemCaseSensitive(root_, "Software");
84 EXPECT_TRUE(softwareObj != nullptr);
85 cJSON* uidObj = cJSON_CreateObject();
86 statsCore->SaveForSoftwareCommonInternal(uidObj, NUMBER_UID);
87 cJSON* item = cJSON_GetObjectItemCaseSensitive(uidObj, "audio_on");
88 EXPECT_TRUE(item == nullptr);
89 cJSON_Delete(uidObj);
90 statsService->OnStop();
91 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestThree002 function end!");
92 }
93
94 HWTEST_F(StatsServiceConfigParseTestThree, StatsServiceConfigParseTestThree003, TestSize.Level0)
95 {
96 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestThree003 function start!");
97 ASSERT_TRUE(root_);
98 auto statsService = BatteryStatsService::GetInstance();
99 EXPECT_TRUE(statsService != nullptr);
100 statsService->OnStart();
101 auto statsCore = statsService->GetBatteryStatsCore();
102 EXPECT_TRUE(statsCore != nullptr);
103 statsCore->SaveForSoftwareConnectivity(root_, NUMBER_UID);
104 cJSON* softwareObj = cJSON_GetObjectItemCaseSensitive(root_, "Software");
105 EXPECT_TRUE(softwareObj != nullptr);
106 statsService->OnStop();
107 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestThree003 function end!");
108 }
109
110 HWTEST_F(StatsServiceConfigParseTestThree, StatsServiceConfigParseTestThree004, TestSize.Level0)
111 {
112 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestThree004 function start!");
113 ASSERT_TRUE(root_);
114 auto statsService = BatteryStatsService::GetInstance();
115 EXPECT_TRUE(statsService != nullptr);
116 statsService->OnStart();
117 auto statsCore = statsService->GetBatteryStatsCore();
118 EXPECT_TRUE(statsCore != nullptr);
119 statsCore->SaveForPower(root_);
120 cJSON* powerObj = cJSON_GetObjectItemCaseSensitive(root_, "Power");
121 EXPECT_TRUE(powerObj != nullptr);
122 statsService->OnStop();
123 STATS_HILOGI(LABEL_TEST, "StatsServiceConfigParseTestThree004 function end!");
124 }
125 } // namespace
126