• 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_object.h"
17 #include "stats_log.h"
18 
19 #include "battery_stats_proxy.h"
20 #include "mock_stats_remote_object.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS::PowerMgr;
24 using namespace OHOS;
25 using namespace std;
26 
27 namespace {
28 /**
29  * @tc.name: StatsClientTestMockObject_001
30  * @tc.desc: test BatteryStatsProxy function(IRemoteObject is mock, "SendRequest" function return false)
31  * @tc.type: FUNC
32  * @tc.require: issueI5X13X
33  */
34 HWTEST_F (StatsClientTestMockObject, StatsClientTestMockObject_001, TestSize.Level0)
35 {
36     STATS_HILOGI(LABEL_TEST, "StatsClientTestMockObject_001 start");
37     sptr<MockStatsRemoteObject> sptrRemoteObj = new MockStatsRemoteObject();
38     EXPECT_TRUE(sptrRemoteObj != nullptr);
39     std::shared_ptr<BatteryStatsProxy> sptrStatsProxy = std::make_shared<BatteryStatsProxy>(sptrRemoteObj);
40     EXPECT_TRUE(sptrStatsProxy != nullptr);
41     sptrStatsProxy->Reset();
42     sptrStatsProxy->SetOnBattery(true);
43     auto infoList = sptrStatsProxy->GetBatteryStats();
44     EXPECT_TRUE(infoList.empty());
45     int32_t uid = 1004;
46     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetAppStatsMah(uid));
47     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetAppStatsPercent(uid));
48     BatteryStatsInfo::ConsumptionType consumptionType = BatteryStatsInfo::CONSUMPTION_TYPE_APP;
49     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetPartStatsMah(consumptionType));
50     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetPartStatsPercent(consumptionType));
51     StatsUtils::StatsType statsType = StatsUtils::STATS_TYPE_PHONE_ACTIVE;
52     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetTotalTimeSecond(statsType, StatsUtils::INVALID_VALUE));
53     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, sptrStatsProxy->GetTotalDataBytes(statsType, StatsUtils::INVALID_VALUE));
54     std::vector<std::string> dumpArgs;
55     dumpArgs.push_back("-batterystats");
56     EXPECT_EQ("remote error", sptrStatsProxy->ShellDump(dumpArgs, dumpArgs.size()));
57     STATS_HILOGI(LABEL_TEST, "StatsClientTestMockObject_001 end");
58 }
59 }
60