• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_client_test_mock_parcel.h"
17 #include "stats_log.h"
18 
19 #include <hisysevent.h>
20 
21 #include "ipc_object_stub.h"
22 
23 #include "battery_stats_client.h"
24 #include "battery_stats_proxy.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS::HiviewDFX;
28 using namespace OHOS::PowerMgr;
29 using namespace OHOS;
30 using namespace std;
31 
32 namespace {
33 /**
34  * @tc.name: StatsClientTestMockParcel_001
35  * @tc.desc: test BatteryStatsClient function
36  * @tc.type: FUNC
37  * @tc.require: issueI5X13X
38  */
39 HWTEST_F (StatsClientTestMockParcel, StatsClientTestMockParcel_001, TestSize.Level0)
40 {
41     STATS_HILOGI(LABEL_TEST, "StatsClientTestMockParcel_001 start");
42     auto& statsClient = BatteryStatsClient::GetInstance();
43     statsClient.Reset();
44     statsClient.SetOnBattery(true);
45     auto infoList = statsClient.GetBatteryStats();
46     EXPECT_TRUE(infoList.empty());
47     int32_t uid = 1004;
48     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, statsClient.GetAppStatsMah(uid));
49     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, statsClient.GetAppStatsPercent(uid));
50     BatteryStatsInfo::ConsumptionType consumptionType = BatteryStatsInfo::CONSUMPTION_TYPE_APP;
51     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, statsClient.GetPartStatsMah(consumptionType));
52     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, statsClient.GetPartStatsPercent(consumptionType));
53     StatsUtils::StatsType statsType = StatsUtils::STATS_TYPE_PHONE_ACTIVE;
54     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, statsClient.GetTotalTimeSecond(statsType));
55     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, statsClient.GetTotalDataBytes(statsType));
56     std::vector<std::string> dumpArgs;
57     dumpArgs.push_back("-batterystats");
58     EXPECT_EQ("can't connect service", statsClient.Dump(dumpArgs));
59     STATS_HILOGI(LABEL_TEST, "StatsClientTestMockParcel_001 end");
60 }
61 
62 /**
63  * @tc.name: StatsClientTestMockParcel_002
64  * @tc.desc: test BatteryStatsProxy function(IRemoteObject is null)
65  * @tc.type: FUNC
66  * @tc.require: issueI5X13X
67  */
68 HWTEST_F (StatsClientTestMockParcel, StatsClientTestMockParcel_002, TestSize.Level0)
69 {
70     STATS_HILOGI(LABEL_TEST, "StatsClientTestMockParcel_002 start");
71     std::shared_ptr<BatteryStatsProxy> sptrStatsProxy = std::make_shared<BatteryStatsProxy>(nullptr);
72     EXPECT_TRUE(sptrStatsProxy != nullptr);
73     sptrStatsProxy->ResetIpc();
74     sptrStatsProxy->SetOnBatteryIpc(true);
75     ParcelableBatteryStatsList parcelableEntityList;
76     int32_t tempError;
77     sptrStatsProxy->GetBatteryStatsIpc(parcelableEntityList, tempError);
78     auto infoList = parcelableEntityList.statsList_;
79     EXPECT_TRUE(infoList.empty());
80     int32_t uid = 1004;
81     double appStatsMah;
82     sptrStatsProxy->GetAppStatsMahIpc(uid, appStatsMah, tempError);
83     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, appStatsMah);
84     double appStatsPercent;
85     sptrStatsProxy->GetAppStatsPercentIpc(uid, appStatsPercent, tempError);
86     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, appStatsPercent);
87     BatteryStatsInfo::ConsumptionType consumptionType = BatteryStatsInfo::CONSUMPTION_TYPE_APP;
88     double partStatsMah;
89     sptrStatsProxy->GetPartStatsMahIpc(consumptionType, partStatsMah, tempError);
90     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, partStatsMah);
91     double partStatsPercent;
92     sptrStatsProxy->GetPartStatsPercentIpc(consumptionType, partStatsPercent, tempError);
93     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, partStatsPercent);
94     StatsUtils::StatsType statsType = StatsUtils::STATS_TYPE_PHONE_ACTIVE;
95     uint64_t totalTimeSecond;
96     sptrStatsProxy->GetTotalTimeSecondIpc(statsType, StatsUtils::INVALID_VALUE, totalTimeSecond);
97     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, totalTimeSecond);
98     uint64_t totalDataBytes;
99     sptrStatsProxy->GetTotalDataBytesIpc(statsType, StatsUtils::INVALID_VALUE, totalDataBytes);
100     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, totalDataBytes);
101     STATS_HILOGI(LABEL_TEST, "StatsClientTestMockParcel_002 end");
102 }
103 
104 /**
105  * @tc.name: StatsClientTestMockParcel_003
106  * @tc.desc: test BatteryStatsProxy function(IRemoteObject is not null)
107  * @tc.type: FUNC
108  * @tc.require: issueI5X13X
109  */
110 HWTEST_F (StatsClientTestMockParcel, StatsClientTestMockParcel_003, TestSize.Level0)
111 {
112     STATS_HILOGI(LABEL_TEST, "StatsClientTestMockParcel_003 start");
113     sptr<IPCObjectStub> sptrRemoteObj = new IPCObjectStub();
114     std::shared_ptr<BatteryStatsProxy> sptrStatsProxy = std::make_shared<BatteryStatsProxy>(sptrRemoteObj);
115     EXPECT_TRUE(sptrStatsProxy != nullptr);
116     sptrStatsProxy->ResetIpc();
117     sptrStatsProxy->SetOnBatteryIpc(true);
118     ParcelableBatteryStatsList parcelableEntityList;
119     int32_t tempError;
120     sptrStatsProxy->GetBatteryStatsIpc(parcelableEntityList, tempError);
121     auto infoList = parcelableEntityList.statsList_;
122     EXPECT_TRUE(infoList.empty());
123     int32_t uid = 1004;
124     double appStatsMah;
125     sptrStatsProxy->GetAppStatsMahIpc(uid, appStatsMah, tempError);
126     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, appStatsMah);
127     double appStatsPercent;
128     sptrStatsProxy->GetAppStatsPercentIpc(uid, appStatsPercent, tempError);
129     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, appStatsPercent);
130     BatteryStatsInfo::ConsumptionType consumptionType = BatteryStatsInfo::CONSUMPTION_TYPE_APP;
131     double partStatsMah;
132     sptrStatsProxy->GetPartStatsMahIpc(consumptionType, partStatsMah, tempError);
133     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, partStatsMah);
134     double partStatsPercent;
135     sptrStatsProxy->GetPartStatsPercentIpc(consumptionType, partStatsPercent, tempError);
136     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, partStatsPercent);
137     StatsUtils::StatsType statsType = StatsUtils::STATS_TYPE_PHONE_ACTIVE;
138     uint64_t totalTimeSecond;
139     sptrStatsProxy->GetTotalTimeSecondIpc(statsType, StatsUtils::INVALID_VALUE, totalTimeSecond);
140     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, totalTimeSecond);
141     uint64_t totalDataBytes;
142     sptrStatsProxy->GetTotalDataBytesIpc(statsType, StatsUtils::INVALID_VALUE, totalDataBytes);
143     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, totalDataBytes);
144     STATS_HILOGI(LABEL_TEST, "StatsClientTestMockParcel_003 end");
145 }
146 
147 /**
148  * @tc.name: StatsClientTestMockParcel_004
149  * @tc.desc: test class BatteryStatsInfo function
150  * @tc.type: FUNC
151  * @tc.require: issueI5X13X
152  */
153 HWTEST_F (StatsClientTestMockParcel, StatsClientTestMockParcel_004, TestSize.Level0)
154 {
155     STATS_HILOGI(LABEL_TEST, "StatsClientTestMockParcel_004 start");
156     std::shared_ptr<BatteryStatsInfo> sptrStatsInfo = std::make_shared<BatteryStatsInfo>();
157     EXPECT_NE(sptrStatsInfo, nullptr);
158     int32_t uid = 1004;
159     int32_t userId = 10005;
160     BatteryStatsInfo::ConsumptionType consumptionType = BatteryStatsInfo::CONSUMPTION_TYPE_APP;
161     double totalPowerMah = 1000.0;
162     sptrStatsInfo->SetUid(uid);
163     sptrStatsInfo->SetUserId(userId);
164     sptrStatsInfo->SetConsumptioType(consumptionType);
165     sptrStatsInfo->SetPower(totalPowerMah);
166     EXPECT_EQ(uid, sptrStatsInfo->GetUid());
167     EXPECT_EQ(userId, sptrStatsInfo->GetUserId());
168     EXPECT_EQ(consumptionType, sptrStatsInfo->GetConsumptionType());
169     EXPECT_EQ(totalPowerMah, sptrStatsInfo->GetPower());
170 
171     Parcel infoParcel = {};
172     EXPECT_FALSE(sptrStatsInfo->Marshalling(infoParcel));
173     STATS_HILOGI(LABEL_TEST, "StatsClientTestMockParcel_004 end");
174 }
175 }
176