1 /*
2 * Copyright (c) 2021-2024 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 "stats_service_test.h"
17 #include "stats_log.h"
18
19 #include <if_system_ability_manager.h>
20 #include <iservice_registry.h>
21 #include <system_ability_definition.h>
22 #include "battery_stats_parser.h"
23 #define private public
24 #define protected public
25 #include "battery_stats_service.h"
26 #undef private
27 #undef protected
28 #include "config_policy_utils.h"
29 #include "gtest/gtest-message.h"
30 #include "gtest/gtest-test-part.h"
31 #include "gtest/gtest.h"
32 #include "gtest/hwext/gtest-ext.h"
33 #include "gtest/hwext/gtest-tag.h"
34 #include "iremote_object.h"
35 #include "ostream"
36 #include "refbase.h"
37
38 using namespace testing::ext;
39 using namespace OHOS::PowerMgr;
40 using namespace OHOS;
41
GetOneCfgFile(const char * pathSuffix,char * buf,unsigned int bufLength)42 char* GetOneCfgFile(const char *pathSuffix, char *buf, unsigned int bufLength)
43 {
44 return nullptr;
45 }
46
SetUpTestCase()47 void StatsServiceTest::SetUpTestCase()
48 {
49 GTEST_LOG_(INFO) << __func__;
50 }
51
TearDownTestCase()52 void StatsServiceTest::TearDownTestCase()
53 {
54 GTEST_LOG_(INFO) << __func__;
55 }
56
SetUp()57 void StatsServiceTest::SetUp()
58 {
59 GTEST_LOG_(INFO) << __func__;
60 }
61
TearDown()62 void StatsServiceTest::TearDown()
63 {
64 GTEST_LOG_(INFO) << __func__;
65 }
66
67 namespace {
68 /**
69 * @tc.name: StatsServiceTest_002
70 * @tc.desc: test OnStart
71 * @tc.type: FUNC
72 */
73 HWTEST_F (StatsServiceTest, StatsServiceTest_002, TestSize.Level0)
74 {
75 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_002 start");
76 auto statsService = BatteryStatsService::GetInstance();
77 EXPECT_TRUE(statsService != nullptr);
78
79 statsService->OnStart();
80 statsService->OnStart();
81 bool ret = statsService->IsServiceReady();
82 EXPECT_FALSE(ret);
83 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_002 end");
84 }
85
86 /**
87 * @tc.name: StatsServiceTest_003
88 * @tc.desc: test OnStop
89 * @tc.type: FUNC
90 */
91 HWTEST_F (StatsServiceTest, StatsServiceTest_003, TestSize.Level0)
92 {
93 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_003 start");
94 auto statsService = BatteryStatsService::GetInstance();
95 EXPECT_TRUE(statsService != nullptr);
96
97 statsService->OnAddSystemAbility(DFX_SYS_EVENT_SERVICE_ABILITY_ID, "");
98 statsService->OnAddSystemAbility(COMMON_EVENT_SERVICE_ID, "");
99 statsService->OnStart();
100 statsService->OnStop();
101 bool ret = statsService->IsServiceReady();
102 EXPECT_FALSE(ret);
103 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_003 end");
104 }
105
106 /**
107 * @tc.name: StatsServiceTest_004
108 * @tc.desc: test Dump
109 * @tc.type: FUNC
110 */
111 HWTEST_F (StatsServiceTest, StatsServiceTest_004, TestSize.Level0)
112 {
113 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_004 start");
114 auto statsService = BatteryStatsService::GetInstance();
115 EXPECT_TRUE(statsService != nullptr);
116
117 int32_t fd = 1;
118 std::vector<std::u16string> vec;
119 statsService->isBootCompleted_ = true;
120 StatsXCollieTestable statsXCollie("test_log", true);
121 int32_t ret = statsService->Dump(fd, vec);
122 EXPECT_EQ(ret, OHOS::ERR_OK);
123 statsXCollie.CallCancel();
124 statsXCollie.CallCancel();
125 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_004 end");
126 }
127
128 /**
129 * @tc.name: StatsParserTest_001
130 * @tc.desc: test Init
131 * @tc.type: FUNC
132 */
133 HWTEST_F (StatsServiceTest, StatsParserTest_001, TestSize.Level0)
134 {
135 STATS_HILOGI(LABEL_TEST, "StatsParserTest_001 start");
136 auto parser = std::make_shared<BatteryStatsParser>();
137 EXPECT_TRUE(parser != nullptr);
138
139 bool ret = parser->Init();
140 EXPECT_TRUE(ret);
141 STATS_HILOGI(LABEL_TEST, "StatsParserTest_001 end");
142 }
143
144 /**
145 * @tc.name: StatsParserTest_002
146 * @tc.desc: test DumpInfo
147 * @tc.type: FUNC
148 */
149 HWTEST_F (StatsServiceTest, StatsParserTest_002, TestSize.Level0)
150 {
151 STATS_HILOGI(LABEL_TEST, "StatsParserTest_002 start");
152 auto parser = std::make_shared<BatteryStatsParser>();
153 EXPECT_TRUE(parser != nullptr);
154
155 parser->Init();
156 std::string result = "";
157 parser->DumpInfo(result);
158 EXPECT_TRUE(result != "");
159 STATS_HILOGI(LABEL_TEST, "StatsParserTest_002 end");
160 }
161 }