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