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