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 g_statsServiceProxy->ResetIpc();
60 g_statsServiceProxy->SetOnBatteryIpc(true);
61 ParcelableBatteryStatsList parcelableEntityList;
62 int32_t tempError;
63 g_statsServiceProxy->GetBatteryStatsIpc(parcelableEntityList, tempError);
64 auto infoList = parcelableEntityList.statsList_;
65 EXPECT_TRUE(infoList.empty());
66 int32_t uid = 1004;
67 double appStatsMah;
68 g_statsServiceProxy->GetAppStatsMahIpc(uid, appStatsMah, tempError);
69 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, appStatsMah);
70 double appStatsPercent;
71 g_statsServiceProxy->GetAppStatsPercentIpc(uid, appStatsPercent, tempError);
72 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, appStatsPercent);
73 BatteryStatsInfo::ConsumptionType consumptionType = BatteryStatsInfo::CONSUMPTION_TYPE_APP;
74 double partStatsMah;
75 g_statsServiceProxy->GetPartStatsMahIpc(consumptionType, partStatsMah, tempError);
76 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, partStatsMah);
77 double partStatsPercent;
78 g_statsServiceProxy->GetPartStatsPercentIpc(consumptionType, partStatsPercent, tempError);
79 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, partStatsPercent);
80 StatsUtils::StatsType statsType = StatsUtils::STATS_TYPE_PHONE_ACTIVE;
81 uint64_t totalTimeSecond;
82 g_statsServiceProxy->GetTotalTimeSecondIpc(statsType, StatsUtils::INVALID_VALUE, totalTimeSecond);
83 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, totalTimeSecond);
84 uint64_t totalDataBytes;
85 g_statsServiceProxy->GetTotalDataBytesIpc(statsType, StatsUtils::INVALID_VALUE, totalDataBytes);
86 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, totalDataBytes);
87 STATS_HILOGI(LABEL_TEST, "StatsServiceTestMockParcel_001 end");
88 }
89 }