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_001
70 * @tc.desc: test BatteryStatsService service ready.
71 * @tc.type: FUNC
72 */
HWTEST_F(StatsServiceTest, StatsServiceTest_001, TestSize.Level0)73 HWTEST_F (StatsServiceTest, StatsServiceTest_001, TestSize.Level0)
74 {
75 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_001 start");
76 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
77 ASSERT_TRUE(sam != nullptr) << "StatsServiceTest_001 fail to get GetSystemAbilityManager";
78 sptr<IRemoteObject> remoteObject_ = sam->CheckSystemAbility(POWER_MANAGER_BATT_STATS_SERVICE_ID);
79 ASSERT_TRUE(remoteObject_ != nullptr) << "GetSystemAbility failed.";
80 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_001 end");
81 }
82
83 /**
84 * @tc.name: StatsServiceTest_002
85 * @tc.desc: test OnStart
86 * @tc.type: FUNC
87 */
HWTEST_F(StatsServiceTest, StatsServiceTest_002, TestSize.Level0)88 HWTEST_F (StatsServiceTest, StatsServiceTest_002, TestSize.Level0)
89 {
90 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_002 start");
91 auto statsService = BatteryStatsService::GetInstance();
92 EXPECT_TRUE(statsService != nullptr);
93
94 statsService->OnStart();
95 statsService->OnStart();
96 bool ret = statsService->IsServiceReady();
97 EXPECT_FALSE(ret);
98 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_002 end");
99 }
100
101 /**
102 * @tc.name: StatsServiceTest_003
103 * @tc.desc: test OnStop
104 * @tc.type: FUNC
105 */
HWTEST_F(StatsServiceTest, StatsServiceTest_003, TestSize.Level0)106 HWTEST_F (StatsServiceTest, StatsServiceTest_003, TestSize.Level0)
107 {
108 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_003 start");
109 auto statsService = BatteryStatsService::GetInstance();
110 EXPECT_TRUE(statsService != nullptr);
111
112 statsService->OnAddSystemAbility(DFX_SYS_EVENT_SERVICE_ABILITY_ID, "");
113 statsService->OnAddSystemAbility(COMMON_EVENT_SERVICE_ID, "");
114 statsService->OnStart();
115 statsService->OnStop();
116 bool ret = statsService->IsServiceReady();
117 EXPECT_FALSE(ret);
118 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_003 end");
119 }
120
121 /**
122 * @tc.name: StatsServiceTest_004
123 * @tc.desc: test Dump
124 * @tc.type: FUNC
125 */
HWTEST_F(StatsServiceTest, StatsServiceTest_004, TestSize.Level0)126 HWTEST_F (StatsServiceTest, StatsServiceTest_004, TestSize.Level0)
127 {
128 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_004 start");
129 auto statsService = BatteryStatsService::GetInstance();
130 EXPECT_TRUE(statsService != nullptr);
131
132 int32_t fd = 1;
133 std::vector<std::u16string> vec;
134 statsService->isBootCompleted_ = true;
135 int32_t ret = statsService->Dump(fd, vec);
136 EXPECT_EQ(ret, OHOS::ERR_OK);
137 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_004 end");
138 }
139
140 /**
141 * @tc.name: StatsParserTest_001
142 * @tc.desc: test Init
143 * @tc.type: FUNC
144 */
HWTEST_F(StatsServiceTest, StatsParserTest_001, TestSize.Level0)145 HWTEST_F (StatsServiceTest, StatsParserTest_001, TestSize.Level0)
146 {
147 STATS_HILOGI(LABEL_TEST, "StatsParserTest_001 start");
148 auto parser = std::make_shared<BatteryStatsParser>();
149 EXPECT_TRUE(parser != nullptr);
150
151 bool ret = parser->Init();
152 EXPECT_TRUE(ret);
153 STATS_HILOGI(LABEL_TEST, "StatsParserTest_001 end");
154 }
155
156 /**
157 * @tc.name: StatsParserTest_002
158 * @tc.desc: test DumpInfo
159 * @tc.type: FUNC
160 */
HWTEST_F(StatsServiceTest, StatsParserTest_002, TestSize.Level0)161 HWTEST_F (StatsServiceTest, StatsParserTest_002, TestSize.Level0)
162 {
163 STATS_HILOGI(LABEL_TEST, "StatsParserTest_002 start");
164 auto parser = std::make_shared<BatteryStatsParser>();
165 EXPECT_TRUE(parser != nullptr);
166
167 parser->Init();
168 std::string result = "";
169 parser->DumpInfo(result);
170 EXPECT_TRUE(result != "");
171 STATS_HILOGI(LABEL_TEST, "StatsParserTest_002 end");
172 }
173 }