• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "batterystats_sys_test.h"
17 
18 #include <cmath>
19 #include <csignal>
20 #include <iostream>
21 #include <unistd.h>
22 
23 #include <bluetooth_def.h>
24 #include <call_manager_inner_type.h>
25 #include <display_power_info.h>
26 #include <hisysevent.h>
27 #include <if_system_ability_manager.h>
28 #include <ipc_skeleton.h>
29 #include <iservice_registry.h>
30 #include <running_lock_info.h>
31 #include <string_ex.h>
32 #include <system_ability_definition.h>
33 #include "wifi_msg.h"
34 
35 #include "battery_stats_client.h"
36 #include "battery_stats_parser.h"
37 #include "stats_common.h"
38 #include "stats_hisysevent.h"
39 
40 using namespace std;
41 using namespace testing::ext;
42 using namespace OHOS;
43 using namespace OHOS::HiviewDFX;
44 using namespace OHOS::PowerMgr;
45 using namespace OHOS::Telephony;
46 
47 namespace {
48 constexpr int64_t US_PER_HOUR = 3600000000;
49 constexpr int32_t US_PER_SECOND = 1000000;
50 constexpr int32_t POWER_CONSUMPTION_DURATION_US = 300000;
51 constexpr double DEVIATION_PERCENT_THRESHOLD = 0.05;
52 static std::vector<std::string> dumpArgs;
53 static std::shared_ptr<BatteryStatsParser> g_statsParser = nullptr;
54 }
55 
ParserAveragePowerFile()56 static void ParserAveragePowerFile()
57 {
58     if (g_statsParser == nullptr) {
59         g_statsParser = std::make_shared<BatteryStatsParser>();
60         if (!g_statsParser->Init()) {
61             GTEST_LOG_(INFO) << __func__ << ": Battery stats parser initialization failed";
62         }
63     }
64 }
65 
SetUpTestCase()66 void BatterystatsSysTest::SetUpTestCase()
67 {
68     ParserAveragePowerFile();
69     dumpArgs.push_back("-batterystats");
70     system("hidumper -s 3302 -a -u");
71 }
72 
TearDownTestCase()73 void BatterystatsSysTest::TearDownTestCase()
74 {
75     system("hidumper -s 3302 -a -r");
76 }
77 
SetUp()78 void BatterystatsSysTest::SetUp()
79 {
80     auto& statsClient = BatteryStatsClient::GetInstance();
81     statsClient.SetOnBattery(true);
82 }
83 
TearDown()84 void BatterystatsSysTest::TearDown()
85 {
86     auto& statsClient = BatteryStatsClient::GetInstance();
87     statsClient.SetOnBattery(false);
88 }
89 
90 /**
91  *
92  * @tc.name: BatteryStatsSysTest_001
93  * @tc.desc: test Wakelock consumption
94  * @tc.type: FUNC
95  */
96 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_001, TestSize.Level0)
97 {
98     auto& statsClient = BatteryStatsClient::GetInstance();
99     statsClient.Reset();
100 
101     int32_t uid = 10001;
102     int32_t pid = 3456;
103     int32_t stateLock = 1;
104     int32_t stateUnlock = 0;
105     double wakelockAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_CPU_AWAKE);
106     int32_t type = static_cast<int32_t>(RunningLockType::RUNNINGLOCK_SCREEN);
107     std::string name = " BatteryStatsSysTest_001";
108 
109     HiSysEventWrite(HiSysEvent::Domain::POWER, StatsHiSysEvent::POWER_RUNNINGLOCK,
110         HiSysEvent::EventType::STATISTIC, "PID", pid,
111         "UID", uid, "STATE", stateLock, "TYPE", type, "NAME", name);
112     usleep(POWER_CONSUMPTION_DURATION_US);
113     HiSysEventWrite(HiSysEvent::Domain::POWER, StatsHiSysEvent::POWER_RUNNINGLOCK,
114         HiSysEvent::EventType::STATISTIC, "PID", pid,
115         "UID", uid, "STATE", stateUnlock, "TYPE", type, "NAME", name);
116 
117     double expectedPowerMah = wakelockAverage * POWER_CONSUMPTION_DURATION_US / US_PER_HOUR;
118     double actualPowerMah = statsClient.GetAppStatsMah(uid);
119     double devPrecent = abs(expectedPowerMah - actualPowerMah) / expectedPowerMah;
120     long expectedTimeSec = round(POWER_CONSUMPTION_DURATION_US / US_PER_SECOND);
121     long actualTimeSec = statsClient.GetTotalTimeSecond(StatsUtils::STATS_TYPE_WAKELOCK_HOLD, uid);
122 
123     std::string expectedDebugInfo;
124     expectedDebugInfo.append("UID = ")
125         .append(ToString(uid))
126         .append(", PID = ")
127         .append(ToString(pid))
128         .append(", wakelock type = ")
129         .append(ToString(type))
130         .append(", wakelock name = ")
131         .append(name);
132 
133     std::string actualDebugInfo = statsClient.Dump(dumpArgs);
134 
135     auto index = actualDebugInfo.find(expectedDebugInfo);
136 
137     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPowerMah << " mAh";
138     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPowerMah << " mAh";
139     GTEST_LOG_(INFO) << __func__ << ": expected time = " << expectedTimeSec << " seconds";
140     GTEST_LOG_(INFO) << __func__ << ": actual time = " <<  actualTimeSec << " seconds";
141 
142     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
143     EXPECT_EQ(expectedTimeSec,  actualTimeSec);
144     EXPECT_TRUE(index != string::npos);
145 }
146 
147 /**
148  *
149  * @tc.name: BatteryStatsSysTest_002
150  * @tc.desc: test Screen consumption
151  * @tc.type: FUNC
152  */
153 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_002, TestSize.Level0)
154 {
155     auto& statsClient = BatteryStatsClient::GetInstance();
156     statsClient.Reset();
157 
158     int32_t stateOn = static_cast<int32_t>(DisplayPowerMgr::DisplayState::DISPLAY_ON);
159     int32_t stateOff = static_cast<int32_t>(DisplayPowerMgr::DisplayState::DISPLAY_OFF);
160     int32_t brightness = 150;
161     double screenOnAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_ON);
162     double screenBrightnessAverage = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_BRIGHTNESS);
163 
164     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
165         HiviewDFX::HiSysEvent::EventType::STATISTIC, "STATE", stateOn);
166     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BRIGHTNESS_NIT,
167         HiviewDFX::HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", brightness);
168     usleep(POWER_CONSUMPTION_DURATION_US);
169     HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::SCREEN_STATE,
170         HiviewDFX::HiSysEvent::EventType::STATISTIC, "STATE", stateOff);
171 
172     double average = screenBrightnessAverage * brightness + screenOnAverage;
173 
174     double expectedPowerMah = average * POWER_CONSUMPTION_DURATION_US / US_PER_HOUR;
175     double actualPowerMah = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN);
176     double devPrecent = abs(expectedPowerMah - actualPowerMah) / expectedPowerMah;
177     long expectedTimeSec = round(POWER_CONSUMPTION_DURATION_US / US_PER_SECOND);
178     long actualTimeSec = statsClient.GetTotalTimeSecond(StatsUtils::STATS_TYPE_SCREEN_ON);
179 
180     std::string expectedDebugInfo;
181     expectedDebugInfo.append("Additional debug info: ")
182         .append("Event name = ")
183         .append(StatsHiSysEvent::SCREEN_STATE);
184 
185     std::string actualDebugInfo = statsClient.Dump(dumpArgs);
186 
187     auto index = actualDebugInfo.find(expectedDebugInfo);
188 
189     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPowerMah << " mAh";
190     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPowerMah << " mAh";
191     GTEST_LOG_(INFO) << __func__ << ": expected time = " << expectedTimeSec << " seconds";
192     GTEST_LOG_(INFO) << __func__ << ": actual time = " <<  actualTimeSec << " seconds";
193 
194     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
195     EXPECT_EQ(expectedTimeSec,  actualTimeSec);
196     EXPECT_TRUE(index != string::npos);
197 }
198 
199 /**
200  *
201  * @tc.name: BatteryStatsSysTest_003
202  * @tc.desc: test Battery stats event
203  * @tc.type: FUNC
204  */
205 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_003, TestSize.Level0)
206 {
207     auto& statsClient = BatteryStatsClient::GetInstance();
208     statsClient.Reset();
209 
210     int32_t batteryLevel = 60;
211     int32_t batteryChargerType = 2;
212 
213     HiSysEventWrite(HiSysEvent::Domain::BATTERY, StatsHiSysEvent::BATTERY_CHANGED,
214         HiSysEvent::EventType::STATISTIC, "LEVEL", batteryLevel, "CHARGER", batteryChargerType);
215 
216     std::string expectedDebugInfo;
217     expectedDebugInfo.append("Battery level = ")
218         .append(ToString(batteryLevel))
219         .append(", Charger type = ")
220         .append(ToString(batteryChargerType));
221 
222     std::string actualDebugInfo = statsClient.Dump(dumpArgs);
223     auto index = actualDebugInfo.find(expectedDebugInfo);
224     EXPECT_TRUE(index != string::npos);
225 }
226 
227 /**
228  *
229  * @tc.name: BatteryStatsSysTest_004
230  * @tc.desc: test Thermal stats event
231  * @tc.type: FUNC
232  */
233 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_004, TestSize.Level0)
234 {
235     auto& statsClient = BatteryStatsClient::GetInstance();
236     statsClient.Reset();
237 
238     std::string partName = "Battery";
239     int32_t temperature = 40;
240 
241     HiSysEventWrite(HiSysEvent::Domain::THERMAL, StatsHiSysEvent::POWER_TEMPERATURE,
242         HiSysEvent::EventType::STATISTIC, "NAME", partName, "TEMPERATURE", temperature);
243 
244     std::string expectedDebugInfo;
245     expectedDebugInfo.append("Additional debug info: ")
246         .append("Event name = POWER_TEMPERATURE")
247         .append(" Name = ")
248         .append(partName);
249 
250     std::string actualDebugInfo = statsClient.Dump(dumpArgs);
251     auto index = actualDebugInfo.find(expectedDebugInfo);
252     EXPECT_TRUE(index != string::npos);
253 }
254 
255 /**
256  *
257  * @tc.name: BatteryStatsSysTest_005
258  * @tc.desc: test WorkScheduler stats event
259  * @tc.type: FUNC
260  */
261 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_005, TestSize.Level0)
262 {
263     auto& statsClient = BatteryStatsClient::GetInstance();
264     statsClient.Reset();
265 
266     int32_t pid = 3457;
267     int32_t uid = 10002;
268     int32_t type = 1;
269     int32_t interval = 30000;
270     int32_t state = 5;
271 
272     HiSysEventWrite(HiSysEvent::Domain::STATS, StatsHiSysEvent::POWER_WORKSCHEDULER,
273         HiSysEvent::EventType::STATISTIC, "PID", pid, "UID", uid, "TYPE", type,
274         "INTERVAL", interval, "STATE", state);
275 
276     std::string expectedDebugInfo;
277     expectedDebugInfo.append("UID = ")
278         .append(ToString(uid))
279         .append(", PID = ")
280         .append(ToString(pid))
281         .append(", work type = ")
282         .append(ToString(type))
283         .append(", work interval = ")
284         .append(ToString(interval))
285         .append(", work state = ")
286         .append(ToString(state));
287 
288     std::string actualDebugInfo = statsClient.Dump(dumpArgs);
289     auto index = actualDebugInfo.find(expectedDebugInfo);
290     EXPECT_TRUE(index != string::npos);
291 }
292 
293 /**
294  *
295  * @tc.name: BatteryStatsSysTest_006
296  * @tc.desc: test Dump function
297  * @tc.type: FUNC
298  */
299 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_006, TestSize.Level0)
300 {
301     auto& statsClient = BatteryStatsClient::GetInstance();
302     std::string result;
303     result.clear();
304     result = statsClient.Dump(dumpArgs);
305     EXPECT_TRUE(result != "");
306     statsClient.Reset();
307 }
308 
309 /**
310  *
311  * @tc.name: BatteryStatsSysTest_008
312  * @tc.desc: test Bluetooth consumption
313  * @tc.type: FUNC
314  */
315 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_008, TestSize.Level0)
316 {
317     auto& statsClient = BatteryStatsClient::GetInstance();
318     statsClient.Reset();
319 
320     double bluetoothBrOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_ON);
321     int32_t stateOn = static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_ON);
322     int32_t stateOff = static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_OFF);
323     int32_t uid = 10003;
324     int32_t pid = 3458;
325     HiSysEventWrite(HiSysEvent::Domain::BT_SERVICE, StatsHiSysEvent::BR_SWITCH_STATE, HiSysEvent::EventType::STATISTIC,
326         "PID", pid, "UID", uid, "STATE", stateOn);
327     usleep(POWER_CONSUMPTION_DURATION_US);
328     HiSysEventWrite(HiSysEvent::Domain::BT_SERVICE, StatsHiSysEvent::BR_SWITCH_STATE, HiSysEvent::EventType::STATISTIC,
329         "PID", pid, "UID", uid, "STATE", stateOff);
330 
331     double expectedPower = POWER_CONSUMPTION_DURATION_US * bluetoothBrOnAverageMa / US_PER_HOUR;
332     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
333     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
334     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
335     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
336     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
337 }
338 
339 /**
340  *
341  * @tc.name: BatteryStatsSysTest_009
342  * @tc.desc: test Wifi consumption
343  * @tc.type: FUNC
344  */
345 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_009, TestSize.Level0)
346 {
347     auto& statsClient = BatteryStatsClient::GetInstance();
348     statsClient.Reset();
349 
350     double wifiOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_WIFI_ON);
351     int32_t stateOn = static_cast<int32_t>(Wifi::ConnState::CONNECTED);
352     int32_t stateOff = static_cast<int32_t>(Wifi::ConnState::DISCONNECTED);
353     HiSysEventWrite(HiSysEvent::Domain::COMMUNICATION, StatsHiSysEvent::WIFI_CONNECTION,
354         HiSysEvent::EventType::STATISTIC, "TYPE", stateOn);
355     usleep(POWER_CONSUMPTION_DURATION_US);
356     HiSysEventWrite(HiSysEvent::Domain::COMMUNICATION, StatsHiSysEvent::WIFI_CONNECTION,
357         HiSysEvent::EventType::STATISTIC, "TYPE", stateOff);
358 
359     double expectedPower = POWER_CONSUMPTION_DURATION_US * wifiOnAverageMa / US_PER_HOUR;
360     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_WIFI);
361     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
362     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
363     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
364     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
365 }
366 
367 /**
368  *
369  * @tc.name: BatteryStatsSysTest_010
370  * @tc.desc: test Phone Call consumption
371  * @tc.type: FUNC
372  */
373 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_010, TestSize.Level0)
374 {
375     auto& statsClient = BatteryStatsClient::GetInstance();
376     statsClient.Reset();
377 
378     int32_t stateOn = static_cast<int32_t>(TelCallState::CALL_STATUS_ACTIVE);
379     int32_t stateOff = static_cast<int32_t>(TelCallState::CALL_STATUS_DISCONNECTED);
380     int16_t level = 0;
381     double phoneOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_RADIO_ON, level);
382 
383     HiSysEventWrite(HiSysEvent::Domain::TELEPHONY, StatsHiSysEvent::CALL_STATE,
384         HiSysEvent::EventType::BEHAVIOR, "STATE", stateOn);
385     usleep(POWER_CONSUMPTION_DURATION_US);
386     HiSysEventWrite(HiSysEvent::Domain::TELEPHONY, StatsHiSysEvent::CALL_STATE,
387         HiSysEvent::EventType::BEHAVIOR, "STATE", stateOff);
388 
389     double expectedPower = POWER_CONSUMPTION_DURATION_US * phoneOnAverageMa / US_PER_HOUR;
390     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_PHONE);
391     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
392     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
393     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
394     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
395 }
396 
397 /**
398  *
399  * @tc.name: BatteryStatsSysTest_011
400  * @tc.desc: test Idle consumption
401  * @tc.type: FUNC
402  */
403 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_011, TestSize.Level0)
404 {
405     auto& statsClient = BatteryStatsClient::GetInstance();
406     statsClient.Reset();
407 
408     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_IDLE);
409     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
410     EXPECT_TRUE(actualPower >= StatsUtils::DEFAULT_VALUE);
411 }
412 
413 /**
414  *
415  * @tc.name: BatteryStatsSysTest_012
416  * @tc.desc: test User consumption
417  * @tc.type: FUNC
418  */
419 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_012, TestSize.Level0)
420 {
421     auto& statsClient = BatteryStatsClient::GetInstance();
422     statsClient.Reset();
423 
424     int32_t uerId = 20003;
425 
426     double actualPower = statsClient.GetAppStatsMah(uerId);
427     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
428     EXPECT_TRUE(actualPower >= StatsUtils::DEFAULT_VALUE);
429 }
430 
431 /**
432  *
433  * @tc.name: BatteryStatsSysTest_013
434  * @tc.desc: test Audio consumption
435  * @tc.type: FUNC
436  */
437 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_013, TestSize.Level0)
438 {
439     auto& statsClient = BatteryStatsClient::GetInstance();
440     statsClient.Reset();
441 
442     double audioOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_AUDIO_ON);
443     int32_t uid = 10003;
444     int32_t pid = 3458;
445     int32_t stateRunning = 2;
446     int32_t stateStopped = 3;
447 
448     HiSysEventWrite(HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE,
449         HiSysEvent::EventType::BEHAVIOR, "PID", pid, "UID", uid, "STATE", stateRunning);
450     usleep(POWER_CONSUMPTION_DURATION_US);
451     HiSysEventWrite(HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE,
452         HiSysEvent::EventType::BEHAVIOR, "PID", pid, "UID", uid, "STATE", stateStopped);
453 
454     double expectedPower = POWER_CONSUMPTION_DURATION_US * audioOnAverageMa / US_PER_HOUR;
455     double actualPower = statsClient.GetAppStatsMah(uid);
456     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
457     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
458     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
459     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
460 }
461 
462 /**
463  *
464  * @tc.name: BatteryStatsSysTest_014
465  * @tc.desc: test GNSS consumption
466  * @tc.type: FUNC
467  */
468 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_014, TestSize.Level0)
469 {
470     auto& statsClient = BatteryStatsClient::GetInstance();
471     statsClient.Reset();
472 
473     double gnssOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_GNSS_ON);
474     int32_t uid = 10003;
475     int32_t pid = 3458;
476     std::string stateOn = "start";
477     std::string stateOff = "stop";
478 
479     HiSysEventWrite(HiSysEvent::Domain::LOCATION, StatsHiSysEvent::GNSS_STATE,
480         HiSysEvent::EventType::STATISTIC, "PID", pid, "UID", uid, "STATE", stateOn);
481     usleep(POWER_CONSUMPTION_DURATION_US);
482     HiSysEventWrite(HiSysEvent::Domain::LOCATION, StatsHiSysEvent::GNSS_STATE, HiSysEvent::EventType::STATISTIC, "PID",
483         pid, "UID", uid, "STATE", stateOff);
484 
485     double expectedPower = POWER_CONSUMPTION_DURATION_US * gnssOnAverageMa / US_PER_HOUR;
486     double actualPower = statsClient.GetAppStatsMah(uid);
487     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
488     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
489     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
490     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
491 }
492 
493 /**
494  *
495  * @tc.name: BatteryStatsSysTest_015
496  * @tc.desc: test Sensor consumption
497  * @tc.type: FUNC
498  */
499 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_015, TestSize.Level0)
500 {
501     auto& statsClient = BatteryStatsClient::GetInstance();
502     statsClient.Reset();
503 
504     double sensorGravityOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SENSOR_GRAVITY);
505     int32_t uid = 10003;
506     int32_t pid = 3458;
507     int32_t stateOn = 1;
508     int32_t stateOff = 0;
509 
510     HiSysEventWrite(HiSysEvent::Domain::STATS, StatsHiSysEvent::POWER_SENSOR_GRAVITY,
511         HiSysEvent::EventType::STATISTIC, "PID", pid, "UID", uid, "STATE", stateOn);
512     usleep(POWER_CONSUMPTION_DURATION_US);
513     HiSysEventWrite(HiSysEvent::Domain::STATS, StatsHiSysEvent::POWER_SENSOR_GRAVITY,
514         HiSysEvent::EventType::STATISTIC, "PID", pid, "UID", uid, "STATE", stateOff);
515 
516     double expectedPower = POWER_CONSUMPTION_DURATION_US * sensorGravityOnAverageMa / US_PER_HOUR;
517     double actualPower = statsClient.GetAppStatsMah(uid);
518     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
519     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
520     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
521     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
522 }
523 
524 /**
525  *
526  * @tc.name: BatteryStatsSysTest_016
527  * @tc.desc: test Camera consumption
528  * @tc.type: FUNC
529  */
530 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_016, TestSize.Level0)
531 {
532     auto& statsClient = BatteryStatsClient::GetInstance();
533     statsClient.Reset();
534 
535     double cameraOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_CAMERA_ON);
536     int32_t uid = 10003;
537     int32_t pid = 3458;
538     std::string cameraId = "Camera0";
539 
540     HiSysEventWrite(HiSysEvent::Domain::CAMERA, StatsHiSysEvent::CAMERA_CONNECT, HiSysEvent::EventType::STATISTIC,
541         "PID", pid, "UID", uid, "ID", cameraId);
542     usleep(POWER_CONSUMPTION_DURATION_US);
543     HiSysEventWrite(HiSysEvent::Domain::CAMERA, StatsHiSysEvent::CAMERA_DISCONNECT, HiSysEvent::EventType::STATISTIC,
544         "ID", cameraId);
545 
546     double expectedPower = POWER_CONSUMPTION_DURATION_US * cameraOnAverageMa / US_PER_HOUR;
547     double actualPower = statsClient.GetAppStatsMah(uid);
548     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
549     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
550     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
551     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
552 }
553 
554 /**
555  *
556  * @tc.name: BatteryStatsSysTest_017
557  * @tc.desc: test Flashlight consumption
558  * @tc.type: FUNC
559  */
560 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_017, TestSize.Level0)
561 {
562     auto& statsClient = BatteryStatsClient::GetInstance();
563     statsClient.Reset();
564 
565     double flashlightOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_FLASHLIGHT_ON);
566     int32_t uid = 10003;
567     int32_t pid = 3458;
568     int32_t stateOn = 1;
569     int32_t stateOff = 0;
570 
571     HiSysEventWrite(HiSysEvent::Domain::CAMERA, StatsHiSysEvent::TORCH_STATE, HiSysEvent::EventType::STATISTIC, "PID",
572         pid, "UID", uid, "STATE", stateOn);
573     usleep(POWER_CONSUMPTION_DURATION_US);
574     HiSysEventWrite(HiSysEvent::Domain::CAMERA, StatsHiSysEvent::TORCH_STATE, HiSysEvent::EventType::STATISTIC, "PID",
575         pid, "UID", uid, "STATE", stateOff);
576 
577     double expectedPower = POWER_CONSUMPTION_DURATION_US * flashlightOnAverageMa / US_PER_HOUR;
578     double actualPower = statsClient.GetAppStatsMah(uid);
579     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
580     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
581     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
582     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
583 }
584 
585 /**
586  *
587  * @tc.name: BatteryStatsSysTest_018
588  * @tc.desc: test Bluetooth and Wifi consumption
589  * @tc.type: FUNC
590  */
591 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_018, TestSize.Level0)
592 {
593     auto& statsClient = BatteryStatsClient::GetInstance();
594     statsClient.Reset();
595 
596     double bluetoothBleOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BLE_ON);
597     int32_t stateOn = static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_ON);
598     int32_t stateOff = static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_OFF);
599     int32_t uid = 10003;
600     int32_t pid = 3458;
601     HiSysEventWrite(HiSysEvent::Domain::BT_SERVICE, StatsHiSysEvent::BLE_SWITCH_STATE, HiSysEvent::EventType::STATISTIC,
602         "PID", pid, "UID", uid, "STATE", stateOn);
603     usleep(POWER_CONSUMPTION_DURATION_US);
604     HiSysEventWrite(HiSysEvent::Domain::BT_SERVICE, StatsHiSysEvent::BLE_SWITCH_STATE, HiSysEvent::EventType::STATISTIC,
605         "PID", pid, "UID", uid, "STATE", stateOff);
606 
607     double expectedPower = POWER_CONSUMPTION_DURATION_US * bluetoothBleOnAverageMa / US_PER_HOUR;
608     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
609     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
610     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
611     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
612     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
613 
614     double wifiOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_WIFI_ON);
615     stateOn = static_cast<int32_t>(Wifi::ConnState::CONNECTED);
616     stateOff = static_cast<int32_t>(Wifi::ConnState::DISCONNECTED);
617     HiSysEventWrite(HiSysEvent::Domain::COMMUNICATION, StatsHiSysEvent::WIFI_CONNECTION,
618         HiSysEvent::EventType::STATISTIC, "TYPE", stateOn);
619     usleep(POWER_CONSUMPTION_DURATION_US);
620     HiSysEventWrite(HiSysEvent::Domain::COMMUNICATION, StatsHiSysEvent::WIFI_CONNECTION,
621         HiSysEvent::EventType::STATISTIC, "TYPE", stateOff);
622 
623     expectedPower = POWER_CONSUMPTION_DURATION_US * wifiOnAverageMa / US_PER_HOUR;
624     actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_WIFI);
625     devPrecent = abs(expectedPower - actualPower) / expectedPower;
626     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
627     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
628     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
629 }
630 
631 /**
632  *
633  * @tc.name: BatteryStatsSysTest_019
634  * @tc.desc: test Flashlight and Camera consumption
635  * @tc.type: FUNC
636  */
637 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_019, TestSize.Level0)
638 {
639     auto& statsClient = BatteryStatsClient::GetInstance();
640     statsClient.Reset();
641 
642     double flashlightOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_FLASHLIGHT_ON);
643     int32_t uid = 10003;
644     int32_t pid = 3458;
645     int32_t stateOn = 1;
646     int32_t stateOff = 0;
647 
648     HiSysEventWrite(HiSysEvent::Domain::CAMERA, StatsHiSysEvent::TORCH_STATE, HiSysEvent::EventType::STATISTIC, "PID",
649         pid, "UID", uid, "STATE", stateOn);
650     usleep(POWER_CONSUMPTION_DURATION_US);
651     HiSysEventWrite(HiSysEvent::Domain::CAMERA, StatsHiSysEvent::TORCH_STATE, HiSysEvent::EventType::STATISTIC, "PID",
652         pid, "UID", uid, "STATE", stateOff);
653 
654     double expectedPower = POWER_CONSUMPTION_DURATION_US * flashlightOnAverageMa / US_PER_HOUR;
655     double actualPower = statsClient.GetAppStatsMah(uid);
656     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
657     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
658     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
659     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
660 
661     double cameraOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_CAMERA_ON);
662     uid = 10004;
663     pid = 3459;
664     std::string deviceId = "Camera0";
665 
666     HiSysEventWrite(HiSysEvent::Domain::CAMERA, StatsHiSysEvent::CAMERA_CONNECT, HiSysEvent::EventType::STATISTIC,
667         "PID", pid, "UID", uid, "ID", deviceId);
668     usleep(POWER_CONSUMPTION_DURATION_US);
669     HiSysEventWrite(HiSysEvent::Domain::CAMERA, StatsHiSysEvent::CAMERA_DISCONNECT, HiSysEvent::EventType::STATISTIC,
670         "ID", deviceId);
671 
672     expectedPower = POWER_CONSUMPTION_DURATION_US * cameraOnAverageMa / US_PER_HOUR;
673     actualPower = statsClient.GetAppStatsMah(uid);
674     devPrecent = abs(expectedPower - actualPower) / expectedPower;
675     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
676     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
677     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
678 }
679 
680 /**
681  *
682  * @tc.name: BatteryStatsSysTest_020
683  * @tc.desc: test Audio, Sensor and Gnss consumption
684  * @tc.type: FUNC
685  */
686 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_020, TestSize.Level0)
687 {
688     auto& statsClient = BatteryStatsClient::GetInstance();
689     statsClient.Reset();
690 
691     double audioOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_AUDIO_ON);
692     int32_t uid = 10003;
693     int32_t pid = 3458;
694     int32_t stateOn = 1;
695     int32_t stateOff = 0;
696     int32_t stateRunning = 2;
697     int32_t stateStopped = 3;
698     HiSysEventWrite(HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID",
699         pid, "UID", uid, "STATE", stateRunning);
700     usleep(POWER_CONSUMPTION_DURATION_US);
701     HiSysEventWrite(HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID",
702         pid, "UID", uid, "STATE", stateStopped);
703 
704     double expectedPower = POWER_CONSUMPTION_DURATION_US * audioOnAverageMa / US_PER_HOUR;
705     double actualPower = statsClient.GetAppStatsMah(uid);
706     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
707     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
708 
709     double gnssOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_GNSS_ON);
710     uid = 10004;
711     pid = 3459;
712     std::string gnssStateOn = "start";
713     std::string gnssStateOff = "stop";
714     HiSysEventWrite(HiSysEvent::Domain::LOCATION, StatsHiSysEvent::GNSS_STATE, HiSysEvent::EventType::STATISTIC, "PID",
715         pid, "UID", uid, "STATE", gnssStateOn);
716     usleep(POWER_CONSUMPTION_DURATION_US);
717     HiSysEventWrite(HiSysEvent::Domain::LOCATION, StatsHiSysEvent::GNSS_STATE, HiSysEvent::EventType::STATISTIC, "PID",
718         pid, "UID", uid, "STATE", gnssStateOff);
719 
720     expectedPower = POWER_CONSUMPTION_DURATION_US * gnssOnAverageMa / US_PER_HOUR;
721     actualPower = statsClient.GetAppStatsMah(uid);
722     devPrecent = abs(expectedPower - actualPower) / expectedPower;
723     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
724     double sensorGravityOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_SENSOR_GRAVITY);
725     uid = 10005;
726     pid = 3457;
727     HiSysEventWrite(HiSysEvent::Domain::STATS, StatsHiSysEvent::POWER_SENSOR_GRAVITY,
728         HiSysEvent::EventType::STATISTIC, "PID", pid, "UID", uid, "STATE", stateOn);
729     usleep(POWER_CONSUMPTION_DURATION_US);
730     HiSysEventWrite(HiSysEvent::Domain::STATS, StatsHiSysEvent::POWER_SENSOR_GRAVITY,
731         HiSysEvent::EventType::STATISTIC, "PID", pid, "UID", uid, "STATE", stateOff);
732 
733     expectedPower = POWER_CONSUMPTION_DURATION_US * sensorGravityOnAverageMa / US_PER_HOUR;
734     actualPower = statsClient.GetAppStatsMah(uid);
735     devPrecent = abs(expectedPower - actualPower) / expectedPower;
736     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
737 }
738 
739 /**
740  *
741  * @tc.name: BatteryStatsSysTest_021
742  * @tc.desc: test Phone data and Audio consumption
743  * @tc.type: FUNC
744  */
745 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_021, TestSize.Level0)
746 {
747     auto& statsClient = BatteryStatsClient::GetInstance();
748     statsClient.Reset();
749 
750     int32_t stateOn = 1;
751     int32_t stateOff = 0;
752     int16_t level = 0;
753     double phoneDataAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_RADIO_DATA, level);
754 
755     HiSysEventWrite(HiSysEvent::Domain::TELEPHONY, StatsHiSysEvent::DATA_CONNECTION_STATE,
756         HiSysEvent::EventType::BEHAVIOR, "STATE", stateOn);
757     usleep(POWER_CONSUMPTION_DURATION_US);
758     HiSysEventWrite(HiSysEvent::Domain::TELEPHONY, StatsHiSysEvent::DATA_CONNECTION_STATE,
759         HiSysEvent::EventType::BEHAVIOR, "STATE", stateOff);
760 
761     double expectedPower = POWER_CONSUMPTION_DURATION_US * phoneDataAverageMa / US_PER_HOUR;
762     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_PHONE);
763     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
764     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
765     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
766 
767     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
768 
769     double audioOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_AUDIO_ON);
770     int32_t uid = 10003;
771     int32_t pid = 3458;
772     int32_t stateRunning = 2;
773     int32_t stateStopped = 3;
774 
775     HiSysEventWrite(HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID",
776         pid, "UID", uid, "STATE", stateRunning);
777     usleep(POWER_CONSUMPTION_DURATION_US);
778     HiSysEventWrite(HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID",
779         pid, "UID", uid, "STATE", stateStopped);
780 
781     expectedPower = POWER_CONSUMPTION_DURATION_US * audioOnAverageMa / US_PER_HOUR;
782     actualPower = statsClient.GetAppStatsMah(uid);
783     devPrecent = abs(expectedPower - actualPower) / expectedPower;
784     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
785     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
786     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
787 }
788 
789 /**
790  *
791  * @tc.name: BatteryStatsSysTest_022
792  * @tc.desc: test Idle and consumption, Dump function
793  * @tc.type: FUNC
794  */
795 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_022, TestSize.Level0)
796 {
797     auto& statsClient = BatteryStatsClient::GetInstance();
798     statsClient.Reset();
799 
800     double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_IDLE);
801     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
802 
803     EXPECT_TRUE(actualPower >= StatsUtils::DEFAULT_VALUE);
804 
805     int32_t uerId = 20003;
806 
807     actualPower = statsClient.GetAppStatsMah(uerId);
808     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
809     EXPECT_TRUE(actualPower >= StatsUtils::DEFAULT_VALUE);
810 
811     std::string result;
812     result.clear();
813     result = statsClient.Dump(dumpArgs);
814     EXPECT_TRUE(result != "");
815 }
816 
817 /**
818  *
819  * @tc.name: BatteryStatsSysTest_023
820  * @tc.desc: test Camera and Camera Flashlight consumption
821  * @tc.type: FUNC
822  */
823 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_023, TestSize.Level0)
824 {
825     auto& statsClient = BatteryStatsClient::GetInstance();
826     statsClient.Reset();
827 
828     double cameraOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_CAMERA_ON);
829     double flashlightOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_FLASHLIGHT_ON);
830     int32_t uid = 10003;
831     int32_t pid = 3458;
832     std::string cameraId = "Camera0";
833 
834     HiSysEventWrite(HiSysEvent::Domain::CAMERA, StatsHiSysEvent::CAMERA_CONNECT, HiSysEvent::EventType::STATISTIC,
835         "PID", pid, "UID", uid, "ID", cameraId);
836     usleep(POWER_CONSUMPTION_DURATION_US);
837     HiSysEventWrite(HiSysEvent::Domain::CAMERA, StatsHiSysEvent::FLASHLIGHT_ON, HiSysEvent::EventType::STATISTIC);
838     usleep(POWER_CONSUMPTION_DURATION_US);
839     HiSysEventWrite(HiSysEvent::Domain::CAMERA, StatsHiSysEvent::FLASHLIGHT_OFF, HiSysEvent::EventType::STATISTIC);
840     usleep(POWER_CONSUMPTION_DURATION_US);
841     HiSysEventWrite(HiSysEvent::Domain::CAMERA, StatsHiSysEvent::CAMERA_DISCONNECT, HiSysEvent::EventType::STATISTIC,
842         "ID", cameraId);
843 
844     double expectedPower = (3 * POWER_CONSUMPTION_DURATION_US * cameraOnAverageMa / US_PER_HOUR) +
845         (POWER_CONSUMPTION_DURATION_US * flashlightOnAverageMa / US_PER_HOUR);
846     double actualPower = statsClient.GetAppStatsMah(uid);
847     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
848     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
849     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
850     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
851 }
852 
853 /**
854  *
855  * @tc.name: BatteryStatsSysTest_024
856  * @tc.desc: test GetLastError interface
857  * @tc.type: FUNC
858  */
859 HWTEST_F (BatterystatsSysTest,  BatteryStatsSysTest_024, TestSize.Level0)
860 {
861     auto& statsClient = BatteryStatsClient::GetInstance();
862     StatsError error = statsClient.GetLastError();
863     EXPECT_TRUE(error == StatsError::ERR_OK) << "last error is " << static_cast<int32_t>(error);
864 }
865