1 /*
2 * Copyright (c) 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_death_test.h"
17 #include "stats_log.h"
18
19 #include "battery_stats_client.h"
20 #include "battery_stats_parser.h"
21 #include "battery_stats_service.h"
22 #include "config_policy_utils.h"
23
24 using namespace testing::ext;
25 using namespace OHOS::PowerMgr;
26 using namespace OHOS;
27 using namespace std;
28
GetOneCfgFile(const char *pathSuffix, char *buf, unsigned int bufLength)29 char* GetOneCfgFile(const char *pathSuffix, char *buf, unsigned int bufLength)
30 {
31 std::string path = " ";
32 char* ret = (char*)path.c_str();
33 return ret;
34 }
35
36 namespace {
37 /**
38 * @tc.name: StatsServiceDeathTest_001
39 * @tc.desc: test OnRemoteDied function(Input remoteObj is nullptr, function don't reset proxy)
40 * @tc.type: FUNC
41 * @tc.require: issueI63PI8
42 */
HWTEST_F(StatsServiceDeathTest, StatsServiceDeathTest_001, TestSize.Level0)43 HWTEST_F (StatsServiceDeathTest, StatsServiceDeathTest_001, TestSize.Level0)
44 {
45 STATS_HILOGI(LABEL_TEST, "StatsServiceDeathTest_001 start");
46 auto& statsClient = BatteryStatsClient::GetInstance();
47 EXPECT_EQ(statsClient.Connect(), ERR_OK);
48
49 std::shared_ptr<IRemoteObject::DeathRecipient> deathRecipient =
50 std::make_shared<BatteryStatsClient::BatteryStatsDeathRecipient>();
51 wptr<IRemoteObject> remoteObj = nullptr;
52 EXPECT_NE(deathRecipient, nullptr);
53 deathRecipient->OnRemoteDied(remoteObj);
54 EXPECT_NE(statsClient.proxy_, nullptr);
55 STATS_HILOGI(LABEL_TEST, "StatsServiceDeathTest_001 end");
56 }
57
58 /**
59 * @tc.name: StatsParserTest_001
60 * @tc.desc: test Init
61 * @tc.type: FUNC
62 */
HWTEST_F(StatsServiceDeathTest, StatsParserTest_001, TestSize.Level0)63 HWTEST_F (StatsServiceDeathTest, StatsParserTest_001, TestSize.Level0)
64 {
65 STATS_HILOGI(LABEL_TEST, "StatsParserTest_001 start");
66 auto parser = std::make_shared<BatteryStatsParser>();
67 EXPECT_TRUE(parser != nullptr);
68
69 bool ret = parser->Init();
70 EXPECT_FALSE(ret);
71 STATS_HILOGI(LABEL_TEST, "StatsParserTest_001 end");
72 }
73
74 /**
75 * @tc.name: StatsServiceTest_001
76 * @tc.desc: test OnStart
77 * @tc.type: FUNC
78 */
HWTEST_F(StatsServiceDeathTest, StatsServiceTest_001, TestSize.Level0)79 HWTEST_F (StatsServiceDeathTest, StatsServiceTest_001, TestSize.Level0)
80 {
81 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_001 start");
82 auto statsService = BatteryStatsService::GetInstance();
83 EXPECT_TRUE(statsService != nullptr);
84 statsService->OnStart();
85 bool ret = statsService->IsServiceReady();
86 EXPECT_FALSE(ret);
87 STATS_HILOGI(LABEL_TEST, "StatsServiceTest_001 end");
88 }
89 }
90