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->Reset(); 74 sptrStatsProxy->SetOnBattery(true); 75 auto infoList = sptrStatsProxy->GetBatteryStats(); 76 EXPECT_TRUE(infoList.empty()); 77 int32_t uid = 1004; 78 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetAppStatsMah(uid)); 79 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetAppStatsPercent(uid)); 80 BatteryStatsInfo::ConsumptionType consumptionType = BatteryStatsInfo::CONSUMPTION_TYPE_APP; 81 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetPartStatsMah(consumptionType)); 82 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetPartStatsPercent(consumptionType)); 83 StatsUtils::StatsType statsType = StatsUtils::STATS_TYPE_PHONE_ACTIVE; 84 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetTotalTimeSecond(statsType, StatsUtils::INVALID_VALUE)); 85 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetTotalDataBytes(statsType, StatsUtils::INVALID_VALUE)); 86 std::vector<std::string> dumpArgs; 87 dumpArgs.push_back("-batterystats"); 88 EXPECT_EQ("remote error", sptrStatsProxy->ShellDump(dumpArgs, dumpArgs.size())); 89 STATS_HILOGI(LABEL_TEST, "StatsClientTestMockParcel_002 end"); 90 } 91 92 /** 93 * @tc.name: StatsClientTestMockParcel_003 94 * @tc.desc: test BatteryStatsProxy function(IRemoteObject is not null) 95 * @tc.type: FUNC 96 * @tc.require: issueI5X13X 97 */ 98 HWTEST_F (StatsClientTestMockParcel, StatsClientTestMockParcel_003, TestSize.Level0) 99 { 100 STATS_HILOGI(LABEL_TEST, "StatsClientTestMockParcel_003 start"); 101 sptr<IPCObjectStub> sptrRemoteObj = new IPCObjectStub(); 102 std::shared_ptr<BatteryStatsProxy> sptrStatsProxy = std::make_shared<BatteryStatsProxy>(sptrRemoteObj); 103 EXPECT_TRUE(sptrStatsProxy != nullptr); 104 sptrStatsProxy->Reset(); 105 sptrStatsProxy->SetOnBattery(true); 106 auto infoList = sptrStatsProxy->GetBatteryStats(); 107 EXPECT_TRUE(infoList.empty()); 108 int32_t uid = 1004; 109 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetAppStatsMah(uid)); 110 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetAppStatsPercent(uid)); 111 BatteryStatsInfo::ConsumptionType consumptionType = BatteryStatsInfo::CONSUMPTION_TYPE_APP; 112 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetPartStatsMah(consumptionType)); 113 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetPartStatsPercent(consumptionType)); 114 StatsUtils::StatsType statsType = StatsUtils::STATS_TYPE_PHONE_ACTIVE; 115 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetTotalTimeSecond(statsType, StatsUtils::INVALID_VALUE)); 116 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetTotalDataBytes(statsType, StatsUtils::INVALID_VALUE)); 117 std::vector<std::string> dumpArgs; 118 dumpArgs.push_back("-batterystats"); 119 EXPECT_EQ("remote error", sptrStatsProxy->ShellDump(dumpArgs, dumpArgs.size())); 120 STATS_HILOGI(LABEL_TEST, "StatsClientTestMockParcel_003 end"); 121 } 122 123 /** 124 * @tc.name: StatsClientTestMockParcel_004 125 * @tc.desc: test class BatteryStatsInfo function 126 * @tc.type: FUNC 127 * @tc.require: issueI5X13X 128 */ 129 HWTEST_F (StatsClientTestMockParcel, StatsClientTestMockParcel_004, TestSize.Level0) 130 { 131 STATS_HILOGI(LABEL_TEST, "StatsClientTestMockParcel_004 start"); 132 std::shared_ptr<BatteryStatsInfo> sptrStatsInfo = std::make_shared<BatteryStatsInfo>(); 133 EXPECT_NE(sptrStatsInfo, nullptr); 134 int32_t uid = 1004; 135 int32_t userId = 10005; 136 BatteryStatsInfo::ConsumptionType consumptionType = BatteryStatsInfo::CONSUMPTION_TYPE_APP; 137 double totalPowerMah = 1000.0; 138 sptrStatsInfo->SetUid(uid); 139 sptrStatsInfo->SetUserId(userId); 140 sptrStatsInfo->SetConsumptioType(consumptionType); 141 sptrStatsInfo->SetPower(totalPowerMah); 142 EXPECT_EQ(uid, sptrStatsInfo->GetUid()); 143 EXPECT_EQ(userId, sptrStatsInfo->GetUserId()); 144 EXPECT_EQ(consumptionType, sptrStatsInfo->GetConsumptionType()); 145 EXPECT_EQ(totalPowerMah, sptrStatsInfo->GetPower()); 146 147 Parcel infoParcel = {}; 148 EXPECT_FALSE(sptrStatsInfo->Marshalling(infoParcel)); 149 STATS_HILOGI(LABEL_TEST, "StatsClientTestMockParcel_004 end"); 150 } 151 } 152