• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_alarm_test.h"
17 
18 #include <hisysevent.h>
19 
20 #include "battery_stats_client.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS::HiviewDFX;
24 using namespace OHOS::PowerMgr;
25 using namespace std;
26 
SetUpTestCase()27 void StatsAlarmTest::SetUpTestCase()
28 {
29     ParserAveragePowerFile();
30     system("hidumper -s 3302 -a -u");
31 }
32 
TearDownTestCase()33 void StatsAlarmTest::TearDownTestCase()
34 {
35     system("hidumper -s 3302 -a -r");
36 }
37 
SetUp()38 void StatsAlarmTest::SetUp()
39 {
40     auto& statsClient = BatteryStatsClient::GetInstance();
41     statsClient.SetOnBattery(true);
42 }
43 
TearDown()44 void StatsAlarmTest::TearDown()
45 {
46     auto& statsClient = BatteryStatsClient::GetInstance();
47     statsClient.SetOnBattery(false);
48 }
49 
50 namespace {
51 /**
52  * @tc.name: StatsAlarmTest_001
53  * @tc.desc: test Reset function(Alarm)
54  * @tc.type: FUNC
55  * @tc.require: issueI5OKAY
56  */
57 HWTEST_F (StatsAlarmTest, StatsAlarmTest_001, TestSize.Level0)
58 {
59     auto& statsClient = BatteryStatsClient::GetInstance();
60     statsClient.Reset();
61 
62     int32_t uid = 10003;
63     int32_t pid = 3458;
64     int16_t count = 2;
65 
66     for (int16_t i = 0; i < count; i++) {
67         HiSysEvent::Write("TIME", "MISC_TIME_STATISTIC_REPORT", HiSysEvent::EventType::STATISTIC, "CALLER_PID", pid,
68         "CALLER_UID", uid);
69         usleep(POWER_CONSUMPTION_TRIGGERED_US);
70     }
71 
72     double powerMahBefore = statsClient.GetAppStatsMah(uid);
73     statsClient.Reset();
74     double powerMahAfter = statsClient.GetAppStatsMah(uid);
75     GTEST_LOG_(INFO) << __func__ << ": before consumption = " << powerMahBefore << " mAh";
76     GTEST_LOG_(INFO) << __func__ << ": after consumption = " << powerMahAfter << " mAh";
77     EXPECT_TRUE(powerMahBefore > StatsUtils::DEFAULT_VALUE && powerMahAfter == StatsUtils::DEFAULT_VALUE);
78 }
79 
80 /**
81  * @tc.name: StatsAlarmTest_002
82  * @tc.desc: test GetPartStatsMah function(Alarm)
83  * @tc.type: FUNC
84  * @tc.require: issueI5OKAY
85  */
86 HWTEST_F (StatsAlarmTest, StatsAlarmTest_002, TestSize.Level0)
87 {
88     auto& statsClient = BatteryStatsClient::GetInstance();
89     statsClient.Reset();
90 
91     double alarmOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_ALARM_ON);
92     int32_t uid = 10003;
93     int32_t pid = 3458;
94     int16_t count = 2;
95 
96     for (int16_t i = 0; i < count; i++) {
97         HiSysEvent::Write("TIME", "MISC_TIME_STATISTIC_REPORT", HiSysEvent::EventType::STATISTIC, "CALLER_PID", pid,
98         "CALLER_UID", uid);
99         usleep(POWER_CONSUMPTION_TRIGGERED_US);
100     }
101 
102     double expectedPower = count * alarmOnAverageMa;
103     double actualPower = statsClient.GetAppStatsMah(uid);
104     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
105     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
106     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
107     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
108 }
109 
110 /**
111  * @tc.name: StatsAlarmTest_003
112  * @tc.desc: test GetAppStatsPercent function(Alarm)
113  * @tc.type: FUNC
114  * @tc.require: issueI5OKAY
115  */
116 HWTEST_F (StatsAlarmTest, StatsAlarmTest_003, TestSize.Level0)
117 {
118     auto& statsClient = BatteryStatsClient::GetInstance();
119     statsClient.Reset();
120 
121     int32_t uid = 10003;
122     int32_t pid = 3458;
123     int16_t count = 2;
124     double fullPercent = 1;
125     double zeroPercent = 0;
126 
127     for (int16_t i = 0; i < count; i++) {
128         HiSysEvent::Write("TIME", "MISC_TIME_STATISTIC_REPORT", HiSysEvent::EventType::STATISTIC, "CALLER_PID", pid,
129         "CALLER_UID", uid);
130     }
131 
132     double actualPercent = statsClient.GetAppStatsPercent(uid);
133     GTEST_LOG_(INFO) << __func__ << ": actual percent = " << actualPercent;
134     EXPECT_TRUE(actualPercent >= zeroPercent && actualPercent <= fullPercent);
135 }
136 
137 /**
138  * @tc.name: StatsAlarmTest_004
139  * @tc.desc: test SetOnBattery function(Alarm)
140  * @tc.type: FUNC
141  * @tc.require: issueI5OKAY
142  */
143 HWTEST_F (StatsAlarmTest, StatsAlarmTest_004, TestSize.Level0)
144 {
145     auto& statsClient = BatteryStatsClient::GetInstance();
146     statsClient.Reset();
147     statsClient.SetOnBattery(false);
148 
149     int32_t uid = 10003;
150     int32_t pid = 3458;
151     int16_t count = 2;
152 
153     for (int16_t i = 0; i < count; i++) {
154         HiSysEvent::Write("TIME", "MISC_TIME_STATISTIC_REPORT", HiSysEvent::EventType::STATISTIC, "CALLER_PID", pid,
155         "CALLER_UID", uid);
156         usleep(POWER_CONSUMPTION_TRIGGERED_US);
157     }
158 
159     double expectedPower = StatsUtils::DEFAULT_VALUE;
160     double actualPower = statsClient.GetAppStatsMah(uid);
161     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
162     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
163     EXPECT_EQ(expectedPower, actualPower);
164     statsClient.SetOnBattery(true);
165 }
166 }