• 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_service_alarm_test.h"
17 
18 #include <hisysevent.h>
19 
20 #include "battery_stats_listener.h"
21 #include "battery_stats_service.h"
22 #include "hisysevent_operation.h"
23 #include "stats_hisysevent.h"
24 #include "stats_service_test_proxy.h"
25 #include "stats_service_write_event.h"
26 
27 using namespace OHOS;
28 using namespace OHOS::HiviewDFX;
29 using namespace OHOS::PowerMgr;
30 using namespace std;
31 using namespace testing::ext;
32 
33 namespace {
34 static sptr<BatteryStatsService> g_statsService = nullptr;
35 static std::shared_ptr<StatsServiceTestProxy> g_statsServiceProxy = nullptr;
36 } // namespace
37 
SetUpTestCase()38 void StatsServiceAlarmTest::SetUpTestCase()
39 {
40     ParserAveragePowerFile();
41     g_statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
42     g_statsService->OnStart();
43 
44     if (g_statsService->listenerPtr_ == nullptr) {
45         g_statsService->listenerPtr_ = std::make_shared<BatteryStatsListener>();
46     }
47 
48     if (g_statsServiceProxy == nullptr) {
49         g_statsServiceProxy = std::make_shared<StatsServiceTestProxy>(g_statsService);
50     }
51 }
52 
TearDownTestCase()53 void StatsServiceAlarmTest::TearDownTestCase()
54 {
55     g_statsService->listenerPtr_ = nullptr;
56     g_statsService->OnStop();
57 }
58 
SetUp()59 void StatsServiceAlarmTest::SetUp()
60 {
61     auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
62     statsService->SetOnBattery(true);
63 }
64 
TearDown()65 void StatsServiceAlarmTest::TearDown()
66 {
67     auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
68     statsService->SetOnBattery(false);
69 }
70 
71 namespace {
72 /**
73  * @tc.name: StatsServiceAlarmTest_001
74  * @tc.desc: test Reset function(Alarm)
75  * @tc.type: FUNC
76  * @tc.require: issueI663DX
77  */
78 HWTEST_F (StatsServiceAlarmTest, StatsServiceAlarmTest_001, TestSize.Level0)
79 {
80     ASSERT_NE(g_statsServiceProxy, nullptr);
81     auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
82     g_statsServiceProxy->Reset();
83 
84     int32_t uid = 10003;
85     int32_t pid = 3458;
86     int16_t count = 10;
87 
88     for (int16_t i = 0; i < count; i++) {
89         StatsWriteHiSysEvent(statsService,
90             HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
91             "CALLER_PID", pid, "CALLER_UID", uid);
92     }
93 
94     double powerMahBefore = g_statsServiceProxy->GetAppStatsMah(uid);
95     g_statsServiceProxy->Reset();
96     double powerMahAfter = g_statsServiceProxy->GetAppStatsMah(uid);
97     GTEST_LOG_(INFO) << __func__ << ": before consumption = " << powerMahBefore << " mAh";
98     GTEST_LOG_(INFO) << __func__ << ": after consumption = " << powerMahAfter << " mAh";
99     EXPECT_TRUE(powerMahBefore > StatsUtils::DEFAULT_VALUE && powerMahAfter == StatsUtils::DEFAULT_VALUE);
100 }
101 
102 /**
103  * @tc.name: StatsServiceAlarmTest_002
104  * @tc.desc: test GetPartStatsMah function(Alarm)
105  * @tc.type: FUNC
106  * @tc.require: issueI663DX
107  */
108 HWTEST_F (StatsServiceAlarmTest, StatsServiceAlarmTest_002, TestSize.Level0)
109 {
110     ASSERT_NE(g_statsServiceProxy, nullptr);
111     auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
112     g_statsServiceProxy->Reset();
113 
114     double alarmOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_ALARM_ON);
115     int32_t uid = 10003;
116     int32_t pid = 3458;
117     int16_t count = 10;
118 
119     for (int16_t i = 0; i < count; i++) {
120         StatsWriteHiSysEvent(statsService,
121             HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
122             "CALLER_PID", pid, "CALLER_UID", uid);
123     }
124 
125     double expectedPower = count * alarmOnAverageMa;
126     double actualPower = g_statsServiceProxy->GetAppStatsMah(uid);
127     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
128     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
129     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
130     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
131 }
132 
133 /**
134  * @tc.name: StatsServiceAlarmTest_003
135  * @tc.desc: test GetAppStatsPercent function(Alarm)
136  * @tc.type: FUNC
137  * @tc.require: issueI663DX
138  */
139 HWTEST_F (StatsServiceAlarmTest, StatsServiceAlarmTest_003, TestSize.Level0)
140 {
141     ASSERT_NE(g_statsServiceProxy, nullptr);
142     auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
143     g_statsServiceProxy->Reset();
144 
145     int32_t uid = 10003;
146     int32_t pid = 3458;
147     int16_t count = 10;
148     double fullPercent = 1;
149     double zeroPercent = 0;
150 
151     for (int16_t i = 0; i < count; i++) {
152         StatsWriteHiSysEvent(statsService,
153             HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
154             "CALLER_PID", pid, "CALLER_UID", uid);
155     }
156 
157     double actualPercent = g_statsServiceProxy->GetAppStatsPercent(uid);
158     GTEST_LOG_(INFO) << __func__ << ": actual percent = " << actualPercent;
159     EXPECT_TRUE(actualPercent >= zeroPercent && actualPercent <= fullPercent);
160 }
161 
162 /**
163  * @tc.name: StatsServiceAlarmTest_004
164  * @tc.desc: test SetOnBattery function(Alarm)
165  * @tc.type: FUNC
166  * @tc.require: issueI663DX
167  */
168 HWTEST_F (StatsServiceAlarmTest, StatsServiceAlarmTest_004, TestSize.Level0)
169 {
170     ASSERT_NE(g_statsServiceProxy, nullptr);
171     auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
172     g_statsServiceProxy->Reset();
173     g_statsServiceProxy->SetOnBattery(false);
174 
175     int32_t uid = 10003;
176     int32_t pid = 3458;
177     int16_t count = 10;
178 
179     for (int16_t i = 0; i < count; i++) {
180         StatsWriteHiSysEvent(statsService,
181             HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
182             "CALLER_PID", pid, "CALLER_UID", uid);
183     }
184 
185     double expectedPower = StatsUtils::DEFAULT_VALUE;
186     double actualPower = g_statsServiceProxy->GetAppStatsMah(uid);
187     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
188     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
189     EXPECT_EQ(expectedPower, actualPower);
190     g_statsServiceProxy->SetOnBattery(true);
191 }
192 
193 /**
194  * @tc.name: StatsServiceAlarmTest_005
195  * @tc.desc: test alarm entity GetPartStatsMah function(Alarm)
196  * @tc.type: FUNC
197  * @tc.require: issueI663DX
198  */
199 HWTEST_F (StatsServiceAlarmTest, StatsServiceAlarmTest_005, TestSize.Level0)
200 {
201     ASSERT_NE(g_statsServiceProxy, nullptr);
202     auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
203     g_statsServiceProxy->Reset();
204 
205     double alarmOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_ALARM_ON);
206     int32_t uid = 10003;
207     int32_t pid = 3458;
208     int16_t count = 10;
209 
210     for (int16_t i = 0; i < count; i++) {
211         StatsWriteHiSysEvent(statsService,
212             HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
213             "CALLER_PID", pid, "CALLER_UID", uid);
214     }
215 
216     auto statsCore = statsService->GetBatteryStatsCore();
217     auto alarmEntity = statsCore->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_ALARM);
218     statsCore->ComputePower();
219 
220     double expectedPower = count * alarmOnAverageMa;
221     double actualPower = alarmEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_ALARM, uid);
222     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
223     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
224     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
225     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
226 
227     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, alarmEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_ALARM));
228     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, alarmEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_INVALID, uid));
229 }
230 
231 /**
232  * @tc.name: StatsServiceAlarmTest_006
233  * @tc.desc: test uid entity GetPartStatsMah function(Alarm)
234  * @tc.type: FUNC
235  * @tc.require: issueI663DX
236  */
237 HWTEST_F (StatsServiceAlarmTest, StatsServiceAlarmTest_006, TestSize.Level0)
238 {
239     ASSERT_NE(g_statsServiceProxy, nullptr);
240     auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
241     g_statsServiceProxy->Reset();
242 
243     double alarmOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_ALARM_ON);
244     int32_t uid = 10003;
245     int32_t pid = 3458;
246     int16_t count = 10;
247 
248     for (int16_t i = 0; i < count; i++) {
249         StatsWriteHiSysEvent(statsService,
250             HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
251             "CALLER_PID", pid, "CALLER_UID", uid);
252     }
253 
254     auto statsCore = statsService->GetBatteryStatsCore();
255     auto uidEntity = statsCore->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_APP);
256     statsCore->ComputePower();
257 
258     double expectedPower = count * alarmOnAverageMa;
259     double actualPower = uidEntity->GetEntityPowerMah(uid);
260     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
261     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
262     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
263     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
264 }
265 
266 /**
267  * @tc.name: StatsServiceAlarmTest_007
268  * @tc.desc: test send hisysevent with missing information(Alarm)
269  * @tc.type: FUNC
270  * @tc.require: issueI663DX
271  */
272 HWTEST_F (StatsServiceAlarmTest, StatsServiceAlarmTest_007, TestSize.Level0)
273 {
274     ASSERT_NE(g_statsServiceProxy, nullptr);
275     auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
276     g_statsServiceProxy->Reset();
277 
278     int32_t uid = 10003;
279     int16_t count = 10;
280 
281     for (int16_t i = 0; i < count; i++) {
282         StatsWriteHiSysEvent(statsService,
283             HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC);
284     }
285 
286     double expectedPower = StatsUtils::DEFAULT_VALUE;
287     double actualPower = g_statsServiceProxy->GetAppStatsMah(uid);
288     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
289     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
290     EXPECT_EQ(expectedPower, actualPower);
291 }
292 }