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_test_mock_parcel.h"
17 #include "stats_log.h"
18
19 #include "battery_stats_service.h"
20 #include "stats_service_test_proxy.h"
21
22 using namespace OHOS;
23 using namespace OHOS::PowerMgr;
24 using namespace std;
25 using namespace testing::ext;
26
27 namespace {
28 static sptr<BatteryStatsService> g_statsService = nullptr;
29 static std::shared_ptr<StatsServiceTestProxy> g_statsServiceProxy = nullptr;
30 } // namespace
31
SetUpTestCase()32 void StatsServiceTestMockParcel::SetUpTestCase()
33 {
34 ParserAveragePowerFile();
35 g_statsService = BatteryStatsService::GetInstance();
36 g_statsService->OnStart();
37
38 if (g_statsServiceProxy == nullptr) {
39 g_statsServiceProxy = std::make_shared<StatsServiceTestProxy>(g_statsService);
40 }
41 }
42
TearDownTestCase()43 void StatsServiceTestMockParcel::TearDownTestCase()
44 {
45 g_statsService->OnStop();
46 }
47
48 namespace {
49 /**
50 * @tc.name: StatsServiceTestMockParcel_001
51 * @tc.desc: test BatteryStatsStub by mock parcel
52 * @tc.type: FUNC
53 * @tc.require: issueI663DX
54 */
55 HWTEST_F (StatsServiceTestMockParcel, StatsServiceTestMockParcel_001, TestSize.Level0)
56 {
57 STATS_HILOGI(LABEL_TEST, "StatsServiceTestMockParcel_001 start");
58 ASSERT_NE(g_statsServiceProxy, nullptr);
59 EXPECT_TRUE(g_statsServiceProxy->Reset());
60 EXPECT_TRUE(g_statsServiceProxy->SetOnBattery(true));
61 auto infoList = g_statsServiceProxy->GetBatteryStats();
62 EXPECT_TRUE(infoList.empty());
63 int32_t uid = 1004;
64 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, g_statsServiceProxy->GetAppStatsMah(uid));
65 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, g_statsServiceProxy->GetAppStatsPercent(uid));
66 BatteryStatsInfo::ConsumptionType consumptionType = BatteryStatsInfo::CONSUMPTION_TYPE_APP;
67 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, g_statsServiceProxy->GetPartStatsMah(consumptionType));
68 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, g_statsServiceProxy->GetPartStatsPercent(consumptionType));
69 StatsUtils::StatsType statsType = StatsUtils::STATS_TYPE_PHONE_ACTIVE;
70 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, g_statsServiceProxy->GetTotalTimeSecond(statsType, StatsUtils::INVALID_VALUE));
71 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, g_statsServiceProxy->GetTotalDataBytes(statsType, StatsUtils::INVALID_VALUE));
72 std::vector<std::string> dumpArgs;
73 dumpArgs.push_back("-batterystats");
74 EXPECT_EQ("remote error", g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size()));
75 STATS_HILOGI(LABEL_TEST, "StatsServiceTestMockParcel_001 end");
76 }
77 }