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