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_audio_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 testing::ext;
29 using namespace OHOS::HiviewDFX;
30 using namespace OHOS::PowerMgr;
31 using namespace std;
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 StatsServiceAudioTest::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 StatsServiceAudioTest::TearDownTestCase()
54 {
55 g_statsService->listenerPtr_ = nullptr;
56 g_statsService->OnStop();
57 }
58
SetUp()59 void StatsServiceAudioTest::SetUp()
60 {
61 auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
62 statsService->SetOnBattery(true);
63 }
64
TearDown()65 void StatsServiceAudioTest::TearDown()
66 {
67 auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
68 statsService->SetOnBattery(false);
69 }
70
71 namespace {
72 /**
73 * @tc.name: StatsServiceAudioTest_001
74 * @tc.desc: test Reset function(Audio)
75 * @tc.type: FUNC
76 * @tc.require: issueI663DX
77 */
78 HWTEST_F (StatsServiceAudioTest, StatsServiceAudioTest_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 int32_t stateRunning = 2;
87 int32_t stateStopped = 3;
88
89 StatsWriteHiSysEvent(statsService,
90 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
91 "UID", uid, "STATE", stateRunning);
92 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
93 StatsWriteHiSysEvent(statsService,
94 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
95 "UID", uid, "STATE", stateStopped);
96
97 double powerMahBefore = g_statsServiceProxy->GetAppStatsMah(uid);
98 g_statsServiceProxy->Reset();
99 double powerMahAfter = g_statsServiceProxy->GetAppStatsMah(uid);
100 GTEST_LOG_(INFO) << __func__ << ": before consumption = " << powerMahBefore << " mAh";
101 GTEST_LOG_(INFO) << __func__ << ": after consumption = " << powerMahAfter << " mAh";
102 EXPECT_TRUE(powerMahBefore > StatsUtils::DEFAULT_VALUE && powerMahAfter == StatsUtils::DEFAULT_VALUE);
103 }
104
105 /**
106 * @tc.name: StatsServiceAudioTest_002
107 * @tc.desc: test GetAppStatsMah function(Audio)
108 * @tc.type: FUNC
109 * @tc.require: issueI663DX
110 */
111 HWTEST_F (StatsServiceAudioTest, StatsServiceAudioTest_002, TestSize.Level0)
112 {
113 ASSERT_NE(g_statsServiceProxy, nullptr);
114 auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
115 g_statsServiceProxy->Reset();
116
117 double audioOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_AUDIO_ON);
118 int32_t uid = 10003;
119 int32_t pid = 3458;
120 int32_t stateRunning = 2;
121 int32_t stateStopped = 3;
122
123 StatsWriteHiSysEvent(statsService,
124 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
125 "UID", uid, "STATE", stateRunning);
126 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
127 StatsWriteHiSysEvent(statsService,
128 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
129 "UID", uid, "STATE", stateStopped);
130
131 double expectedPower = SERVICE_POWER_CONSUMPTION_DURATION_US * audioOnAverageMa / US_PER_HOUR;
132 double actualPower = g_statsServiceProxy->GetAppStatsMah(uid);
133 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
134 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
135 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
136 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
137 }
138
139 /**
140 * @tc.name: StatsServiceAudioTest_003
141 * @tc.desc: test GetAppStatsPercent function(Audio)
142 * @tc.type: FUNC
143 * @tc.require: issueI663DX
144 */
145 HWTEST_F (StatsServiceAudioTest, StatsServiceAudioTest_003, TestSize.Level0)
146 {
147 ASSERT_NE(g_statsServiceProxy, nullptr);
148 auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
149 g_statsServiceProxy->Reset();
150
151 int32_t uid = 10003;
152 int32_t pid = 3458;
153 int32_t stateRunning = 2;
154 int32_t stateStopped = 3;
155 double fullPercent = 1;
156 double zeroPercent = 0;
157
158 StatsWriteHiSysEvent(statsService,
159 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
160 "UID", uid, "STATE", stateRunning);
161 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
162 StatsWriteHiSysEvent(statsService,
163 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
164 "UID", uid, "STATE", stateStopped);
165
166 double actualPercent = g_statsServiceProxy->GetAppStatsPercent(uid);
167 GTEST_LOG_(INFO) << __func__ << ": actual percent = " << actualPercent;
168 EXPECT_TRUE(actualPercent >= zeroPercent && actualPercent <= fullPercent);
169 }
170
171 /**
172 * @tc.name: StatsServiceAudioTest_004
173 * @tc.desc: test GetAppStatsMah function, Audio state composite test
174 * @tc.type: FUNC
175 * @tc.require: issueI663DX
176 */
177 HWTEST_F (StatsServiceAudioTest, StatsServiceAudioTest_004, TestSize.Level0)
178 {
179 ASSERT_NE(g_statsServiceProxy, nullptr);
180 auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
181 g_statsServiceProxy->Reset();
182
183 double audioOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_AUDIO_ON);
184 int32_t uid = 10003;
185 int32_t pid = 3458;
186 int32_t stateRunning = 2;
187 int32_t stateStopped = 3;
188 int32_t stateReleased = 4;
189 int32_t statePaused = 5;
190
191 StatsWriteHiSysEvent(statsService,
192 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
193 "UID", uid, "STATE", stateRunning);
194 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
195 StatsWriteHiSysEvent(statsService,
196 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
197 "UID", uid, "STATE", stateStopped);
198
199 StatsWriteHiSysEvent(statsService,
200 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
201 "UID", uid, "STATE", stateRunning);
202 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
203 StatsWriteHiSysEvent(statsService,
204 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
205 "UID", uid, "STATE", stateReleased);
206
207 StatsWriteHiSysEvent(statsService,
208 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
209 "UID", uid, "STATE", stateRunning);
210 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
211 StatsWriteHiSysEvent(statsService,
212 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
213 "UID", uid, "STATE", statePaused);
214
215 double expectedPower = 3 * SERVICE_POWER_CONSUMPTION_DURATION_US * audioOnAverageMa / US_PER_HOUR;
216 double actualPower = g_statsServiceProxy->GetAppStatsMah(uid);
217 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
218 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
219 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
220 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
221 }
222
223 /**
224 * @tc.name: StatsServiceAudioTest_005
225 * @tc.desc: test GetAppStatsMah function, Audio abnormal state test
226 * @tc.type: FUNC
227 * @tc.require: issueI663DX
228 */
229 HWTEST_F (StatsServiceAudioTest, StatsServiceAudioTest_005, TestSize.Level0)
230 {
231 ASSERT_NE(g_statsServiceProxy, nullptr);
232 auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
233 g_statsServiceProxy->Reset();
234
235 double audioOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_AUDIO_ON);
236 int32_t uid = 10003;
237 int32_t pid = 3458;
238 int32_t stateRunning = 2;
239 int32_t stateStopped = 3;
240 int32_t stateInvalid = -1;
241 int32_t stateAbnormal = 101;
242
243 StatsWriteHiSysEvent(statsService,
244 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
245 "UID", uid, "STATE", stateInvalid);
246 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
247 StatsWriteHiSysEvent(statsService,
248 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
249 "UID", uid, "STATE", stateRunning);
250 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
251 StatsWriteHiSysEvent(statsService,
252 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
253 "UID", uid, "STATE", stateAbnormal);
254 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
255 StatsWriteHiSysEvent(statsService,
256 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
257 "UID", uid, "STATE", stateStopped);
258
259 double expectedPower = 2 * SERVICE_POWER_CONSUMPTION_DURATION_US * audioOnAverageMa / US_PER_HOUR;
260 double actualPower = g_statsServiceProxy->GetAppStatsMah(uid);
261 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
262 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
263 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
264 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
265 }
266
267 /**
268 * @tc.name: StatsServiceAudioTest_006
269 * @tc.desc: test GetAppStatsMah(Audio) and GetAppStatsPercent(Sensor) function
270 * @tc.type: FUNC
271 * @tc.require: issueI663DX
272 */
273 HWTEST_F (StatsServiceAudioTest, StatsServiceAudioTest_006, TestSize.Level0)
274 {
275 ASSERT_NE(g_statsServiceProxy, nullptr);
276 auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
277 g_statsServiceProxy->Reset();
278
279 double audioOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_AUDIO_ON);
280 int32_t uid = 10003;
281 int32_t pid = 3458;
282 int32_t stateRunning = 2;
283 int32_t stateStopped = 3;
284
285 StatsWriteHiSysEvent(statsService,
286 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
287 "UID", uid, "STATE", stateRunning);
288 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
289 StatsWriteHiSysEvent(statsService,
290 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
291 "UID", uid, "STATE", stateStopped);
292
293 double expectedPower = SERVICE_POWER_CONSUMPTION_DURATION_US * audioOnAverageMa / US_PER_HOUR;
294 double actualPower = g_statsServiceProxy->GetAppStatsMah(uid);
295 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
296 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
297 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
298 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
299
300 uid = 10004;
301 pid = 3459;
302 int32_t stateOn = 1;
303 int32_t stateOff = 0;
304 double fullPercent = 1;
305 double zeroPercent = 0;
306
307 StatsWriteHiSysEvent(statsService,
308 HiSysEvent::Domain::STATS, StatsHiSysEvent::POWER_SENSOR_GRAVITY, HiSysEvent::EventType::STATISTIC, "PID",
309 pid, "UID", uid, "STATE", stateOn);
310 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
311 StatsWriteHiSysEvent(statsService,
312 HiSysEvent::Domain::STATS, StatsHiSysEvent::POWER_SENSOR_GRAVITY, HiSysEvent::EventType::STATISTIC, "PID",
313 pid, "UID", uid, "STATE", stateOff);
314
315 double actualPercent = g_statsServiceProxy->GetAppStatsPercent(uid);
316 GTEST_LOG_(INFO) << __func__ << ": actual percent = " << actualPercent;
317 EXPECT_TRUE(actualPercent >= zeroPercent && actualPercent <= fullPercent);
318 }
319
320 /**
321 * @tc.name: StatsServiceAudioTest_007
322 * @tc.desc: test audio entity GetPartStatsMah function(Audio)
323 * @tc.type: FUNC
324 * @tc.require: issueI663DX
325 */
326 HWTEST_F (StatsServiceAudioTest, StatsServiceAudioTest_007, TestSize.Level0)
327 {
328 ASSERT_NE(g_statsServiceProxy, nullptr);
329 auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
330 g_statsServiceProxy->Reset();
331
332 double audioOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_AUDIO_ON);
333 int32_t uid = 10003;
334 int32_t pid = 3458;
335 int32_t stateRunning = 2;
336 int32_t stateStopped = 3;
337
338 StatsWriteHiSysEvent(statsService,
339 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
340 "UID", uid, "STATE", stateRunning);
341 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
342 StatsWriteHiSysEvent(statsService,
343 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
344 "UID", uid, "STATE", stateStopped);
345
346 auto statsCore = statsService->GetBatteryStatsCore();
347 auto audioEntity = statsCore->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_AUDIO);
348 statsCore->ComputePower();
349
350 double expectedPower = SERVICE_POWER_CONSUMPTION_DURATION_US * audioOnAverageMa / US_PER_HOUR;
351 double actualPower = audioEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_AUDIO_ON, uid);
352 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
353 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
354 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
355 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
356
357 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, audioEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_AUDIO_ON));
358 EXPECT_EQ(StatsUtils::DEFAULT_VALUE, audioEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_INVALID, uid));
359 }
360
361 /**
362 * @tc.name: StatsServiceAudioTest_008
363 * @tc.desc: test send hisysevent with missing information(Audio)
364 * @tc.type: FUNC
365 * @tc.require: issueI663DX
366 */
367 HWTEST_F (StatsServiceAudioTest, StatsServiceAudioTest_008, TestSize.Level0)
368 {
369 ASSERT_NE(g_statsServiceProxy, nullptr);
370 auto statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
371 g_statsServiceProxy->Reset();
372
373 int32_t uid = 10003;
374 StatsWriteHiSysEvent(statsService,
375 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR);
376 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
377 StatsWriteHiSysEvent(statsService,
378 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR);
379
380 double expectedPower = StatsUtils::DEFAULT_VALUE;
381 double actualPower = g_statsServiceProxy->GetAppStatsMah(uid);
382 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
383 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
384 EXPECT_EQ(expectedPower, actualPower);
385 }
386 }