1 /*
2 * Copyright (c) 2022 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_bluetooth_test.h"
17
18 #include <bt_def.h>
19 #include <hisysevent.h>
20
21 #include "battery_stats_client.h"
22
23 using namespace testing::ext;
24 using namespace OHOS::HiviewDFX;
25 using namespace OHOS::PowerMgr;
26 using namespace OHOS;
27 using namespace std;
28
WriteBluetoothEvent(int32_t pid,int32_t uid,long time)29 static void WriteBluetoothEvent(int32_t pid, int32_t uid, long time)
30 {
31 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
32 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
33 int32_t stateScanOn = static_cast<int32_t>(bluetooth::DISCOVERY_STARTED);
34 int32_t stateScanOff = static_cast<int32_t>(bluetooth::DISCOVERY_STOPED);
35
36 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
37 "PID", pid, "UID", uid, "STATE", stateOn);
38 usleep(time * StatsTest::US_PER_MS);
39 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
40 "PID", pid, "UID", uid, "STATE", stateOff);
41
42 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_START", HiSysEvent::EventType::STATISTIC,
43 "PID", pid, "UID", uid);
44 usleep(time * StatsTest::US_PER_MS);
45 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_STOP", HiSysEvent::EventType::STATISTIC,
46 "PID", pid, "UID", uid);
47
48 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
49 "PID", pid, "UID", uid, "STATE", stateOn);
50 usleep(time * StatsTest::US_PER_MS);
51 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
52 "PID", pid, "UID", uid, "STATE", stateOff);
53
54 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
55 "PID", pid, "UID", uid, "STATE", stateScanOn);
56 usleep(time * StatsTest::US_PER_MS);
57 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
58 "PID", pid, "UID", uid, "STATE", stateScanOff);
59 }
60
SetUpTestCase()61 void StatsBluetoothTest::SetUpTestCase()
62 {
63 ParserAveragePowerFile();
64 system("hidumper -s 3302 -a -u");
65 }
66
TearDownTestCase()67 void StatsBluetoothTest::TearDownTestCase()
68 {
69 system("hidumper -s 3302 -a -r");
70 }
71
SetUp()72 void StatsBluetoothTest::SetUp()
73 {
74 auto& statsClient = BatteryStatsClient::GetInstance();
75 statsClient.SetOnBattery(true);
76 }
77
TearDown()78 void StatsBluetoothTest::TearDown()
79 {
80 auto& statsClient = BatteryStatsClient::GetInstance();
81 statsClient.SetOnBattery(false);
82 }
83
84 namespace {
85 /**
86 * @tc.name: StatsBluetoothTest_001
87 * @tc.desc: test Reset function(Bluetooth BR on)
88 * @tc.type: FUNC
89 * @tc.require: issueI5HWJK
90 */
91 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_001, TestSize.Level0)
92 {
93 auto& statsClient = BatteryStatsClient::GetInstance();
94 statsClient.Reset();
95
96 int32_t uid = 10003;
97 int32_t pid = 3458;
98 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
99 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
100 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
101 "PID", pid, "UID", uid, "STATE", stateOn);
102 usleep(POWER_CONSUMPTION_DURATION_US);
103 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
104 "PID", pid, "UID", uid, "STATE", stateOff);
105
106 double powerMahBefore = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
107 statsClient.Reset();
108 double powerMahAfter = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
109 GTEST_LOG_(INFO) << __func__ << ": before consumption = " << powerMahBefore << " mAh";
110 GTEST_LOG_(INFO) << __func__ << ": after consumption = " << powerMahAfter << " mAh";
111 EXPECT_TRUE(powerMahBefore > StatsUtils::DEFAULT_VALUE && powerMahAfter == StatsUtils::DEFAULT_VALUE);
112 }
113
114 /**
115 * @tc.name: StatsBluetoothTest_002
116 * @tc.desc: test GetPartStatsMah function(Bluetooth BR on)
117 * @tc.type: FUNC
118 * @tc.require: issueI5HWJK
119 */
120 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_002, TestSize.Level0)
121 {
122 auto& statsClient = BatteryStatsClient::GetInstance();
123 statsClient.Reset();
124
125 double bluetoothBrOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_ON);
126 int32_t uid = 10003;
127 int32_t pid = 3458;
128 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
129 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
130
131 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
132 "PID", pid, "UID", uid, "STATE", stateOn);
133 usleep(POWER_CONSUMPTION_DURATION_US);
134 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
135 "PID", pid, "UID", uid, "STATE", stateOff);
136
137 double expectedPower = POWER_CONSUMPTION_DURATION_US * bluetoothBrOnAverageMa / US_PER_HOUR;
138 double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
139 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
140 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
141 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
142 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
143 }
144
145 /**
146 * @tc.name: StatsBluetoothTest_003
147 * @tc.desc: test GetPartStatsPercent function(Bluetooth BR on)
148 * @tc.type: FUNC
149 * @tc.require: issueI5HWJK
150 */
151 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_003, TestSize.Level0)
152 {
153 auto& statsClient = BatteryStatsClient::GetInstance();
154 statsClient.Reset();
155
156 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
157 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
158 int32_t uid = 10003;
159 int32_t pid = 3458;
160 double fullPercent = 1;
161 double zeroPercent = 0;
162
163 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
164 "PID", pid, "UID", uid, "STATE", stateOn);
165 usleep(POWER_CONSUMPTION_DURATION_US);
166 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
167 "PID", pid, "UID", uid, "STATE", stateOff);
168
169 double actualPercent = statsClient.GetPartStatsPercent(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
170 GTEST_LOG_(INFO) << __func__ << ": actual percent = " << actualPercent;
171 EXPECT_TRUE(actualPercent >= zeroPercent && actualPercent <= fullPercent);
172 }
173
174 /**
175 * @tc.name: StatsBluetoothTest_004
176 * @tc.desc: test GetBatteryStats function(Bluetooth BR on)
177 * @tc.type: FUNC
178 * @tc.require: issueI5HWJK
179 */
180 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_004, TestSize.Level0)
181 {
182 auto& statsClient = BatteryStatsClient::GetInstance();
183 statsClient.Reset();
184
185 double bluetoothBrOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_ON);
186 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
187 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
188 int32_t uid = 10003;
189 int32_t pid = 3458;
190
191 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
192 "PID", pid, "UID", uid, "STATE", stateOn);
193 usleep(POWER_CONSUMPTION_DURATION_US);
194 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
195 "PID", pid, "UID", uid, "STATE", stateOff);
196
197 double expectedPower = POWER_CONSUMPTION_DURATION_US * bluetoothBrOnAverageMa / US_PER_HOUR;
198 double actualPower = StatsUtils::DEFAULT_VALUE;
199 auto list = statsClient.GetBatteryStats();
200 for (auto it : list) {
201 if ((*it).GetConsumptionType() == BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH) {
202 actualPower = (*it).GetPower();
203 }
204 }
205 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
206 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
207 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
208 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
209 }
210
211 /**
212 * @tc.name: StatsBluetoothTest_005
213 * @tc.desc: test BLUETOOTH_BR_SWITCH_STATE event are sent repeatedly, Bluetooth BR power consumption(Bluetooth BR on)
214 * @tc.type: FUNC
215 * @tc.require: issueI5HWJK
216 */
217 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_005, TestSize.Level0)
218 {
219 auto& statsClient = BatteryStatsClient::GetInstance();
220 statsClient.Reset();
221
222 double bluetoothBrOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_ON);
223 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
224 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
225 int32_t uid = 10003;
226 int32_t pid = 3458;
227
228 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
229 "PID", pid, "UID", uid, "STATE", stateOn);
230 usleep(POWER_CONSUMPTION_DURATION_US);
231 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
232 "PID", pid, "UID", uid, "STATE", stateOn);
233 usleep(POWER_CONSUMPTION_DURATION_US);
234 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
235 "PID", pid, "UID", uid, "STATE", stateOff);
236 usleep(POWER_CONSUMPTION_DURATION_US);
237 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
238 "PID", pid, "UID", uid, "STATE", stateOff);
239
240 double expectedPower = 2 * POWER_CONSUMPTION_DURATION_US * bluetoothBrOnAverageMa / US_PER_HOUR;
241 double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
242 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
243 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
244 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
245 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
246 }
247
248 /**
249 * @tc.name: StatsBluetoothTest_006
250 * @tc.desc: test GetAppStatsMah function, Bluetooth BR on state composite test
251 * @tc.type: FUNC
252 * @tc.require: issueI5HWJK
253 */
254 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_006, TestSize.Level0)
255 {
256 auto& statsClient = BatteryStatsClient::GetInstance();
257 statsClient.Reset();
258
259 double bluetoothBrOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_ON);
260 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
261 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
262 int32_t stateTurningOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURNING_ON);
263 int32_t stateTurningOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURNING_OFF);
264 int32_t uid = 10003;
265 int32_t pid = 3458;
266
267 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
268 "PID", pid, "UID", uid, "STATE", stateTurningOn);
269 usleep(POWER_CONSUMPTION_DURATION_US);
270 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
271 "PID", pid, "UID", uid, "STATE", stateOn);
272 usleep(POWER_CONSUMPTION_DURATION_US);
273 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
274 "PID", pid, "UID", uid, "STATE", stateTurningOff);
275 usleep(POWER_CONSUMPTION_DURATION_US);
276 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
277 "PID", pid, "UID", uid, "STATE", stateOff);
278
279 double expectedPower = 2 * POWER_CONSUMPTION_DURATION_US * bluetoothBrOnAverageMa / US_PER_HOUR;
280 double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
281 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
282 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
283 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
284 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
285 }
286
287 /**
288 * @tc.name: StatsBluetoothTest_007
289 * @tc.desc: test test GetAppStatsMah function, Bluetooth BR on abnormal state test(Bluetooth BR is off)
290 * @tc.type: FUNC
291 * @tc.require: issueI5HWJK
292 */
293 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_007, TestSize.Level0)
294 {
295 auto& statsClient = BatteryStatsClient::GetInstance();
296 statsClient.Reset();
297
298 int32_t uid = 10003;
299 int32_t pid = 3458;
300 int32_t stateOn = 10;
301 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
302
303 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
304 "PID", pid, "UID", uid, "STATE", stateOn);
305 usleep(POWER_CONSUMPTION_DURATION_US);
306 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
307 "PID", pid, "UID", uid, "STATE", stateOff);
308
309 double expectedPower = StatsUtils::DEFAULT_VALUE;
310 double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
311 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
312 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
313 EXPECT_EQ(expectedPower, actualPower);
314 }
315
316 /**
317 * @tc.name: StatsBluetoothTest_008
318 * @tc.desc: test test GetAppStatsMah function, Bluetooth BR on abnormal state test(Bluetooth BR is on)
319 * @tc.type: FUNC
320 * @tc.require: issueI5HWJK
321 */
322 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_008, TestSize.Level0)
323 {
324 auto& statsClient = BatteryStatsClient::GetInstance();
325 statsClient.Reset();
326
327 double bluetoothBrOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_ON);
328 int32_t uid = 10003;
329 int32_t pid = 3458;
330 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
331 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
332 int32_t stateInvaildOn = 5;
333 int32_t stateInvaildOff = -1;
334
335 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
336 "PID", pid, "UID", uid, "STATE", stateOn);
337 usleep(POWER_CONSUMPTION_DURATION_US);
338 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
339 "PID", pid, "UID", uid, "STATE", stateInvaildOn);
340 usleep(POWER_CONSUMPTION_DURATION_US);
341 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
342 "PID", pid, "UID", uid, "STATE", stateInvaildOff);
343 usleep(POWER_CONSUMPTION_DURATION_US);
344 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
345 "PID", pid, "UID", uid, "STATE", stateOff);
346
347 double expectedPower = 3 * POWER_CONSUMPTION_DURATION_US * bluetoothBrOnAverageMa / US_PER_HOUR;
348 double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
349 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
350 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
351 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
352 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
353 }
354
355 /**
356 * @tc.name: StatsBluetoothTest_009
357 * @tc.desc: test GetTotalTimeSecond function(Bluetooth BR on)
358 * @tc.type: FUNC
359 * @tc.require: issueI5HWJK
360 */
361 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_009, TestSize.Level0)
362 {
363 auto& statsClient = BatteryStatsClient::GetInstance();
364 statsClient.Reset();
365
366 int32_t uid = 10003;
367 int32_t pid = 3458;
368 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
369 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
370
371 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
372 "PID", pid, "UID", uid, "STATE", stateOn);
373 usleep(POWER_CONSUMPTION_DURATION_US);
374 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
375 "PID", pid, "UID", uid, "STATE", stateOff);
376
377 long expectedTime = round(POWER_CONSUMPTION_DURATION_US / US_PER_SECOND);
378 long actualTime = statsClient.GetTotalTimeSecond(StatsUtils::STATS_TYPE_BLUETOOTH_BR_ON);
379 GTEST_LOG_(INFO) << __func__ << ": expected time = " << expectedTime << " seconds";
380 GTEST_LOG_(INFO) << __func__ << ": actual time = " << actualTime << " seconds";
381 EXPECT_EQ(expectedTime, actualTime);
382 }
383
384 /**
385 * @tc.name: StatsBluetoothTest_010
386 * @tc.desc: test Reset function(Bluetooth BLE on)
387 * @tc.type: FUNC
388 * @tc.require: issueI5HWJK
389 */
390 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_010, TestSize.Level0)
391 {
392 auto& statsClient = BatteryStatsClient::GetInstance();
393 statsClient.Reset();
394
395 int32_t uid = 10003;
396 int32_t pid = 3458;
397 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
398 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
399 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
400 "PID", pid, "UID", uid, "STATE", stateOn);
401 usleep(POWER_CONSUMPTION_DURATION_US);
402 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
403 "PID", pid, "UID", uid, "STATE", stateOff);
404
405 double powerMahBefore = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
406 statsClient.Reset();
407 double powerMahAfter = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
408 GTEST_LOG_(INFO) << __func__ << ": before consumption = " << powerMahBefore << " mAh";
409 GTEST_LOG_(INFO) << __func__ << ": after consumption = " << powerMahAfter << " mAh";
410 EXPECT_TRUE(powerMahBefore > StatsUtils::DEFAULT_VALUE && powerMahAfter == StatsUtils::DEFAULT_VALUE);
411 }
412
413 /**
414 * @tc.name: StatsBluetoothTest_011
415 * @tc.desc: test GetPartStatsMah function(Bluetooth BLE on)
416 * @tc.type: FUNC
417 * @tc.require: issueI5HWJK
418 */
419 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_011, TestSize.Level0)
420 {
421 auto& statsClient = BatteryStatsClient::GetInstance();
422 statsClient.Reset();
423
424 double bluetoothBleOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BLE_ON);
425
426 int32_t uid = 10003;
427 int32_t pid = 3458;
428 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
429 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
430
431 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
432 "PID", pid, "UID", uid, "STATE", stateOn);
433 usleep(POWER_CONSUMPTION_DURATION_US);
434 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
435 "PID", pid, "UID", uid, "STATE", stateOff);
436
437 double expectedPower = POWER_CONSUMPTION_DURATION_US * bluetoothBleOnAverageMa / US_PER_HOUR;
438 double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
439 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
440 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
441 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
442 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
443 }
444
445 /**
446 * @tc.name: StatsBluetoothTest_012
447 * @tc.desc: test GetPartStatsPercent function(Bluetooth BLE on)
448 * @tc.type: FUNC
449 * @tc.require: issueI5HWJK
450 */
451 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_012, TestSize.Level0)
452 {
453 auto& statsClient = BatteryStatsClient::GetInstance();
454 statsClient.Reset();
455
456 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
457 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
458 int32_t uid = 10003;
459 int32_t pid = 3458;
460 double fullPercent = 1;
461 double zeroPercent = 0;
462
463 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
464 "PID", pid, "UID", uid, "STATE", stateOn);
465 usleep(POWER_CONSUMPTION_DURATION_US);
466 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
467 "PID", pid, "UID", uid, "STATE", stateOff);
468
469 double actualPercent = statsClient.GetPartStatsPercent(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
470 GTEST_LOG_(INFO) << __func__ << ": actual percent = " << actualPercent;
471 EXPECT_TRUE(actualPercent >= zeroPercent && actualPercent <= fullPercent);
472 }
473
474 /**
475 * @tc.name: StatsBluetoothTest_013
476 * @tc.desc: test GetBatteryStats function(Bluetooth BLE on)
477 * @tc.type: FUNC
478 * @tc.require: issueI5HWJK
479 */
480 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_013, TestSize.Level0)
481 {
482 auto& statsClient = BatteryStatsClient::GetInstance();
483 statsClient.Reset();
484
485 double bluetoothBleOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BLE_ON);
486 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
487 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
488 int32_t uid = 10003;
489 int32_t pid = 3458;
490
491 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
492 "PID", pid, "UID", uid, "STATE", stateOn);
493 usleep(POWER_CONSUMPTION_DURATION_US);
494 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
495 "PID", pid, "UID", uid, "STATE", stateOff);
496
497 double expectedPower = POWER_CONSUMPTION_DURATION_US * bluetoothBleOnAverageMa / US_PER_HOUR;
498 double actualPower = StatsUtils::DEFAULT_VALUE;
499 auto list = statsClient.GetBatteryStats();
500 for (auto it : list) {
501 if ((*it).GetConsumptionType() == BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH) {
502 actualPower = (*it).GetPower();
503 }
504 }
505 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
506 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
507 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
508 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
509 }
510
511 /**
512 * @tc.name: StatsBluetoothTest_014
513 * @tc.desc: test BLUETOOTH_BLE_STATE event are sent repeatedly, Bluetooth BLE power consumption(Bluetooth BLE on)
514 * @tc.type: FUNC
515 * @tc.require: issueI5HWJK
516 */
517 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_014, TestSize.Level0)
518 {
519 auto& statsClient = BatteryStatsClient::GetInstance();
520 statsClient.Reset();
521
522 double bluetoothBleOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BLE_ON);
523 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
524 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
525 int32_t uid = 10003;
526 int32_t pid = 3458;
527
528 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
529 "PID", pid, "UID", uid, "STATE", stateOn);
530 usleep(POWER_CONSUMPTION_DURATION_US);
531 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
532 "PID", pid, "UID", uid, "STATE", stateOn);
533 usleep(POWER_CONSUMPTION_DURATION_US);
534 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
535 "PID", pid, "UID", uid, "STATE", stateOff);
536 usleep(POWER_CONSUMPTION_DURATION_US);
537 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
538 "PID", pid, "UID", uid, "STATE", stateOff);
539
540 double expectedPower = 2 * POWER_CONSUMPTION_DURATION_US * bluetoothBleOnAverageMa / US_PER_HOUR;
541 double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
542 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
543 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
544 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
545 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
546 }
547
548 /**
549 * @tc.name: StatsBluetoothTest_015
550 * @tc.desc: test GetAppStatsMah function, Bluetooth BLE on state composite test
551 * @tc.type: FUNC
552 * @tc.require: issueI5HWJK
553 */
554 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_015, TestSize.Level0)
555 {
556 auto& statsClient = BatteryStatsClient::GetInstance();
557 statsClient.Reset();
558
559 double bluetoothBleOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BLE_ON);
560 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
561 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
562 int32_t stateTurningOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURNING_ON);
563 int32_t stateTurningOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURNING_OFF);
564 int32_t uid = 10003;
565 int32_t pid = 3458;
566
567 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
568 "PID", pid, "UID", uid, "STATE", stateTurningOn);
569 usleep(POWER_CONSUMPTION_DURATION_US);
570 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
571 "PID", pid, "UID", uid, "STATE", stateOn);
572 usleep(POWER_CONSUMPTION_DURATION_US);
573 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
574 "PID", pid, "UID", uid, "STATE", stateTurningOff);
575 usleep(POWER_CONSUMPTION_DURATION_US);
576 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
577 "PID", pid, "UID", uid, "STATE", stateOff);
578
579 double expectedPower = 2 * POWER_CONSUMPTION_DURATION_US * bluetoothBleOnAverageMa / US_PER_HOUR;
580 double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
581 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
582 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
583 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
584 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
585 }
586
587 /**
588 * @tc.name: StatsBluetoothTest_016
589 * @tc.desc: test test GetAppStatsMah function, Bluetooth BLE on abnormal state test(Bluetooth BLE is off)
590 * @tc.type: FUNC
591 * @tc.require: issueI5HWJK
592 */
593 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_016, TestSize.Level0)
594 {
595 auto& statsClient = BatteryStatsClient::GetInstance();
596 statsClient.Reset();
597
598 int32_t uid = 10003;
599 int32_t pid = 3458;
600 int32_t stateOn = 10;
601 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
602
603 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
604 "PID", pid, "UID", uid, "STATE", stateOn);
605 usleep(POWER_CONSUMPTION_DURATION_US);
606 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
607 "PID", pid, "UID", uid, "STATE", stateOff);
608
609 double expectedPower = StatsUtils::DEFAULT_VALUE;
610 double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
611 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
612 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
613 EXPECT_EQ(expectedPower, actualPower);
614 }
615
616 /**
617 * @tc.name: StatsBluetoothTest_017
618 * @tc.desc: test test GetAppStatsMah function, Bluetooth BLE on abnormal state test(Bluetooth BLE is on)
619 * @tc.type: FUNC
620 * @tc.require: issueI5HWJK
621 */
622 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_017, TestSize.Level0)
623 {
624 auto& statsClient = BatteryStatsClient::GetInstance();
625 statsClient.Reset();
626
627 double bluetoothBleOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BLE_ON);
628 int32_t uid = 10003;
629 int32_t pid = 3458;
630 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
631 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
632 int32_t stateInvaildOn = 5;
633 int32_t stateInvaildOff = -1;
634
635 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
636 "PID", pid, "UID", uid, "STATE", stateOn);
637 usleep(POWER_CONSUMPTION_DURATION_US);
638 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
639 "PID", pid, "UID", uid, "STATE", stateInvaildOn);
640 usleep(POWER_CONSUMPTION_DURATION_US);
641 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
642 "PID", pid, "UID", uid, "STATE", stateInvaildOff);
643 usleep(POWER_CONSUMPTION_DURATION_US);
644 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
645 "PID", pid, "UID", uid, "STATE", stateOff);
646
647 double expectedPower = 3 * POWER_CONSUMPTION_DURATION_US * bluetoothBleOnAverageMa / US_PER_HOUR;
648 double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
649 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
650 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
651 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
652 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
653 }
654
655 /**
656 * @tc.name: StatsBluetoothTest_018
657 * @tc.desc: test GetTotalTimeSecond function(Bluetooth BLE on)
658 * @tc.type: FUNC
659 * @tc.require: issueI5HWJK
660 */
661 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_018, TestSize.Level0)
662 {
663 auto& statsClient = BatteryStatsClient::GetInstance();
664 statsClient.Reset();
665
666 int32_t uid = 10003;
667 int32_t pid = 3458;
668 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
669 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
670
671 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
672 "PID", pid, "UID", uid, "STATE", stateOn);
673 usleep(POWER_CONSUMPTION_DURATION_US);
674 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_STATE", HiSysEvent::EventType::STATISTIC,
675 "PID", pid, "UID", uid, "STATE", stateOff);
676
677 long expectedTime = round(POWER_CONSUMPTION_DURATION_US / US_PER_SECOND);
678 long actualTime = statsClient.GetTotalTimeSecond(StatsUtils::STATS_TYPE_BLUETOOTH_BLE_ON);
679 GTEST_LOG_(INFO) << __func__ << ": expected time = " << expectedTime << " seconds";
680 GTEST_LOG_(INFO) << __func__ << ": actual time = " << actualTime << " seconds";
681 EXPECT_EQ(expectedTime, actualTime);
682 }
683
684 /**
685 * @tc.name: StatsBluetoothTest_019
686 * @tc.desc: test Reset function(Bluetooth BR scan)
687 * @tc.type: FUNC
688 * @tc.require: issueI5HWJK
689 */
690 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_019, TestSize.Level0)
691 {
692 auto& statsClient = BatteryStatsClient::GetInstance();
693 statsClient.Reset();
694
695 int32_t uid = 10003;
696 int32_t pid = 3458;
697 int32_t stateOn = static_cast<int32_t>(bluetooth::DISCOVERY_STARTED);
698 int32_t stateOff = static_cast<int32_t>(bluetooth::DISCOVERY_STOPED);
699
700 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
701 "PID", pid, "UID", uid, "STATE", stateOn);
702 usleep(POWER_CONSUMPTION_DURATION_US);
703 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
704 "PID", pid, "UID", uid, "STATE", stateOff);
705
706 double powerMahBefore = statsClient.GetAppStatsMah(uid);
707 statsClient.Reset();
708 double powerMahAfter = statsClient.GetAppStatsMah(uid);
709 GTEST_LOG_(INFO) << __func__ << ": before consumption = " << powerMahBefore << " mAh";
710 GTEST_LOG_(INFO) << __func__ << ": after consumption = " << powerMahAfter << " mAh";
711 EXPECT_TRUE(powerMahBefore > StatsUtils::DEFAULT_VALUE && powerMahAfter == StatsUtils::DEFAULT_VALUE);
712 }
713
714 /**
715 * @tc.name: StatsBluetoothTest_020
716 * @tc.desc: test GetAppStatsMah function(Bluetooth BR scan)
717 * @tc.type: FUNC
718 * @tc.require: issueI5HWJK
719 */
720 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_020, TestSize.Level0)
721 {
722 auto& statsClient = BatteryStatsClient::GetInstance();
723 statsClient.Reset();
724
725 double bluetoothBrScanAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_SCAN);
726 int32_t uid = 10003;
727 int32_t pid = 3458;
728 int32_t stateOn = static_cast<int32_t>(bluetooth::DISCOVERY_STARTED);
729 int32_t stateOff = static_cast<int32_t>(bluetooth::DISCOVERY_STOPED);
730
731 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
732 "PID", pid, "UID", uid, "STATE", stateOn);
733 usleep(POWER_CONSUMPTION_DURATION_US);
734 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
735 "PID", pid, "UID", uid, "STATE", stateOff);
736
737 double expectedPower = POWER_CONSUMPTION_DURATION_US * bluetoothBrScanAverageMa / US_PER_HOUR;
738 double actualPower = statsClient.GetAppStatsMah(uid);
739 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
740 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
741 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
742 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
743 }
744
745 /**
746 * @tc.name: StatsBluetoothTest_021
747 * @tc.desc: test GetAppStatsPercent function(Bluetooth BR scan)
748 * @tc.type: FUNC
749 * @tc.require: issueI5HWJK
750 */
751 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_021, TestSize.Level0)
752 {
753 auto& statsClient = BatteryStatsClient::GetInstance();
754 statsClient.Reset();
755
756 int32_t uid = 10003;
757 int32_t pid = 3458;
758 int32_t stateOn = static_cast<int32_t>(bluetooth::DISCOVERY_STARTED);
759 int32_t stateOff = static_cast<int32_t>(bluetooth::DISCOVERY_STOPED);
760 double fullPercent = 1;
761 double zeroPercent = 0;
762
763 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
764 "PID", pid, "UID", uid, "STATE", stateOn);
765 usleep(POWER_CONSUMPTION_DURATION_US);
766 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
767 "PID", pid, "UID", uid, "STATE", stateOff);
768
769 double actualPercent = statsClient.GetAppStatsPercent(uid);
770 GTEST_LOG_(INFO) << __func__ << ": actual percent = " << actualPercent;
771 EXPECT_TRUE(actualPercent >= zeroPercent && actualPercent <= fullPercent);
772 }
773
774 /**
775 * @tc.name: StatsBluetoothTest_022
776 * @tc.desc: test BLUETOOTH_DISCOVERY_STATE event are sent repeatedly, BR scan power consumption(Bluetooth BR scan)
777 * @tc.type: FUNC
778 * @tc.require: issueI5HWJK
779 */
780 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_022, TestSize.Level0)
781 {
782 auto& statsClient = BatteryStatsClient::GetInstance();
783 statsClient.Reset();
784
785 double bluetoothBrScanAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_SCAN);
786 int32_t stateOn = static_cast<int32_t>(bluetooth::DISCOVERY_STARTED);
787 int32_t stateOff = static_cast<int32_t>(bluetooth::DISCOVERY_STOPED);
788 int32_t uid = 10003;
789 int32_t pid = 3458;
790
791 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
792 "PID", pid, "UID", uid, "STATE", stateOn);
793 usleep(POWER_CONSUMPTION_DURATION_US);
794 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
795 "PID", pid, "UID", uid, "STATE", stateOn);
796 usleep(POWER_CONSUMPTION_DURATION_US);
797 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
798 "PID", pid, "UID", uid, "STATE", stateOff);
799 usleep(POWER_CONSUMPTION_DURATION_US);
800 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
801 "PID", pid, "UID", uid, "STATE", stateOff);
802
803 double expectedPower = 2 * POWER_CONSUMPTION_DURATION_US * bluetoothBrScanAverageMa / US_PER_HOUR;
804 double actualPower = statsClient.GetAppStatsMah(uid);
805 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
806 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
807 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
808 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
809 }
810
811 /**
812 * @tc.name: StatsBluetoothTest_023
813 * @tc.desc: test GetAppStatsMah function, Bluetooth BR scan state composite test
814 * @tc.type: FUNC
815 * @tc.require: issueI5HWJK
816 */
817 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_023, TestSize.Level0)
818 {
819 auto& statsClient = BatteryStatsClient::GetInstance();
820 statsClient.Reset();
821
822 double bluetoothBrScanAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_SCAN);
823 int32_t stateOn = static_cast<int32_t>(bluetooth::DISCOVERY_STARTED);
824 int32_t stateOff = static_cast<int32_t>(bluetooth::DISCOVERY_STOPED);
825 int32_t stateScan = static_cast<int32_t>(bluetooth::DISCOVERYING);
826 int32_t uid = 10003;
827 int32_t pid = 3458;
828
829 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
830 "PID", pid, "UID", uid, "STATE", stateScan);
831 usleep(POWER_CONSUMPTION_DURATION_US);
832 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
833 "PID", pid, "UID", uid, "STATE", stateOn);
834 usleep(POWER_CONSUMPTION_DURATION_US);
835 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
836 "PID", pid, "UID", uid, "STATE", stateScan);
837 usleep(POWER_CONSUMPTION_DURATION_US);
838 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
839 "PID", pid, "UID", uid, "STATE", stateOff);
840
841 double expectedPower = 2 * POWER_CONSUMPTION_DURATION_US * bluetoothBrScanAverageMa / US_PER_HOUR;
842 double actualPower = statsClient.GetAppStatsMah(uid);
843 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
844 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
845 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
846 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
847 }
848
849 /**
850 * @tc.name: StatsBluetoothTest_024
851 * @tc.desc: test test GetAppStatsMah function, Bluetooth BR scan abnormal state test(Bluetooth BR scan is off)
852 * @tc.type: FUNC
853 * @tc.require: issueI5HWJK
854 */
855 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_024, TestSize.Level0)
856 {
857 auto& statsClient = BatteryStatsClient::GetInstance();
858 statsClient.Reset();
859
860 int32_t uid = 10003;
861 int32_t pid = 3458;
862 int32_t stateOn = 10;
863 int32_t stateOff = static_cast<int32_t>(bluetooth::DISCOVERY_STOPED);
864
865 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
866 "PID", pid, "UID", uid, "STATE", stateOn);
867 usleep(POWER_CONSUMPTION_DURATION_US);
868 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
869 "PID", pid, "UID", uid, "STATE", stateOff);
870
871 double expectedPower = StatsUtils::DEFAULT_VALUE;
872 double actualPower = statsClient.GetAppStatsMah(uid);
873 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
874 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
875 EXPECT_EQ(expectedPower, actualPower);
876 }
877
878 /**
879 * @tc.name: StatsBluetoothTest_025
880 * @tc.desc: test test GetAppStatsMah function, Bluetooth BR scan abnormal state test(Bluetooth BR scan is on)
881 * @tc.type: FUNC
882 * @tc.require: issueI5HWJK
883 */
884 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_025, TestSize.Level0)
885 {
886 auto& statsClient = BatteryStatsClient::GetInstance();
887 statsClient.Reset();
888
889 double bluetoothBleOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_SCAN);
890 int32_t uid = 10003;
891 int32_t pid = 3458;
892 int32_t stateOn = static_cast<int32_t>(bluetooth::DISCOVERY_STARTED);
893 int32_t stateOff = static_cast<int32_t>(bluetooth::DISCOVERY_STOPED);
894 int32_t stateInvaildOn = 5;
895 int32_t stateInvaildOff = -1;
896
897 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
898 "PID", pid, "UID", uid, "STATE", stateOn);
899 usleep(POWER_CONSUMPTION_DURATION_US);
900 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
901 "PID", pid, "UID", uid, "STATE", stateInvaildOn);
902 usleep(POWER_CONSUMPTION_DURATION_US);
903 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
904 "PID", pid, "UID", uid, "STATE", stateInvaildOff);
905 usleep(POWER_CONSUMPTION_DURATION_US);
906 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
907 "PID", pid, "UID", uid, "STATE", stateOff);
908
909 double expectedPower = 3 * POWER_CONSUMPTION_DURATION_US * bluetoothBleOnAverageMa / US_PER_HOUR;
910 double actualPower = statsClient.GetAppStatsMah(uid);
911 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
912 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
913 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
914 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
915 }
916
917 /**
918 * @tc.name: StatsBluetoothTest_026
919 * @tc.desc: test GetTotalTimeSecond function(Bluetooth BR scan)
920 * @tc.type: FUNC
921 * @tc.require: issueI5HWJK
922 */
923 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_026, TestSize.Level0)
924 {
925 auto& statsClient = BatteryStatsClient::GetInstance();
926 statsClient.Reset();
927
928 int32_t uid = 10003;
929 int32_t pid = 3458;
930 int32_t stateOn = static_cast<int32_t>(bluetooth::DISCOVERY_STARTED);
931 int32_t stateOff = static_cast<int32_t>(bluetooth::DISCOVERY_STOPED);
932
933 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
934 "PID", pid, "UID", uid, "STATE", stateOn);
935 usleep(POWER_CONSUMPTION_DURATION_US);
936 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
937 "PID", pid, "UID", uid, "STATE", stateOff);
938
939 long expectedTime = round(POWER_CONSUMPTION_DURATION_US / US_PER_SECOND);
940 long actualTime = statsClient.GetTotalTimeSecond(StatsUtils::STATS_TYPE_BLUETOOTH_BR_SCAN, uid);
941 GTEST_LOG_(INFO) << __func__ << ": expected time = " << expectedTime << " seconds";
942 GTEST_LOG_(INFO) << __func__ << ": actual time = " << actualTime << " seconds";
943 EXPECT_EQ(expectedTime, actualTime);
944 }
945
946 /**
947 * @tc.name: StatsBluetoothTest_027
948 * @tc.desc: test GetAppStatsMah function with different UID(Bluetooth BR scan)
949 * @tc.type: FUNC
950 * @tc.require: issueI5HWJK
951 */
952 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_027, TestSize.Level0)
953 {
954 auto& statsClient = BatteryStatsClient::GetInstance();
955 statsClient.Reset();
956
957 double bluetoothBrScanAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_SCAN);
958 int32_t uidOne = 10003;
959 int32_t pidOne = 3458;
960 int32_t uidTwo = 10004;
961 int32_t pidTwo = 3459;
962 int32_t stateOn = static_cast<int32_t>(bluetooth::DISCOVERY_STARTED);
963 int32_t stateOff = static_cast<int32_t>(bluetooth::DISCOVERY_STOPED);
964
965 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
966 "PID", pidOne, "UID", uidOne, "STATE", stateOn);
967 usleep(POWER_CONSUMPTION_DURATION_US);
968 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
969 "PID", pidTwo, "UID", uidTwo, "STATE", stateOn);
970 usleep(POWER_CONSUMPTION_DURATION_US);
971 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
972 "PID", pidTwo, "UID", uidTwo, "STATE", stateOff);
973 usleep(POWER_CONSUMPTION_DURATION_US);
974 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_DISCOVERY_STATE", HiSysEvent::EventType::STATISTIC,
975 "PID", pidOne, "UID", uidOne, "STATE", stateOff);
976
977 double expectedPowerOne = 3 * POWER_CONSUMPTION_DURATION_US * bluetoothBrScanAverageMa / US_PER_HOUR;
978 double actualPowerOne = statsClient.GetAppStatsMah(uidOne);
979 double devPrecentOne = abs(expectedPowerOne - actualPowerOne) / expectedPowerOne;
980 GTEST_LOG_(INFO) << __func__ << ": expected consumption One = " << expectedPowerOne << " mAh";
981 GTEST_LOG_(INFO) << __func__ << ": actual consumption One = " << actualPowerOne << " mAh";
982 EXPECT_LE(devPrecentOne, DEVIATION_PERCENT_THRESHOLD);
983
984 double expectedPowerTwo = POWER_CONSUMPTION_DURATION_US * bluetoothBrScanAverageMa / US_PER_HOUR;
985 double actualPowerTwo = statsClient.GetAppStatsMah(uidTwo);
986 double devPrecentTwo = abs(expectedPowerTwo - actualPowerTwo) / expectedPowerTwo;
987 GTEST_LOG_(INFO) << __func__ << ": expected consumption Two = " << expectedPowerTwo << " mAh";
988 GTEST_LOG_(INFO) << __func__ << ": actual consumption Two = " << actualPowerTwo << " mAh";
989 EXPECT_LE(devPrecentTwo, DEVIATION_PERCENT_THRESHOLD);
990 }
991
992 /**
993 * @tc.name: StatsBluetoothTest_028
994 * @tc.desc: test Reset function(Bluetooth BLE scan)
995 * @tc.type: FUNC
996 * @tc.require: issueI5HWJK
997 */
998 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_028, TestSize.Level0)
999 {
1000 auto& statsClient = BatteryStatsClient::GetInstance();
1001 statsClient.Reset();
1002
1003 int32_t uid = 10003;
1004 int32_t pid = 3458;
1005
1006 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_START", HiSysEvent::EventType::STATISTIC,
1007 "PID", pid, "UID", uid);
1008 usleep(POWER_CONSUMPTION_DURATION_US);
1009 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_STOP", HiSysEvent::EventType::STATISTIC,
1010 "PID", pid, "UID", uid);
1011
1012 double powerMahBefore = statsClient.GetAppStatsMah(uid);
1013 statsClient.Reset();
1014 double powerMahAfter = statsClient.GetAppStatsMah(uid);
1015 GTEST_LOG_(INFO) << __func__ << ": before consumption = " << powerMahBefore << " mAh";
1016 GTEST_LOG_(INFO) << __func__ << ": after consumption = " << powerMahAfter << " mAh";
1017 EXPECT_TRUE(powerMahBefore > StatsUtils::DEFAULT_VALUE && powerMahAfter == StatsUtils::DEFAULT_VALUE);
1018 }
1019
1020 /**
1021 * @tc.name: StatsBluetoothTest_029
1022 * @tc.desc: test GetAppStatsMah function(Bluetooth BLE scan)
1023 * @tc.type: FUNC
1024 * @tc.require: issueI5HWJK
1025 */
1026 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_029, TestSize.Level0)
1027 {
1028 auto& statsClient = BatteryStatsClient::GetInstance();
1029 statsClient.Reset();
1030
1031 double bluetoothBleScanAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BLE_SCAN);
1032 int32_t uid = 10003;
1033 int32_t pid = 3458;
1034
1035 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_START", HiSysEvent::EventType::STATISTIC,
1036 "PID", pid, "UID", uid);
1037 usleep(POWER_CONSUMPTION_DURATION_US);
1038 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_STOP", HiSysEvent::EventType::STATISTIC,
1039 "PID", pid, "UID", uid);
1040
1041 double expectedPower = POWER_CONSUMPTION_DURATION_US * bluetoothBleScanAverageMa / US_PER_HOUR;
1042 double actualPower = statsClient.GetAppStatsMah(uid);
1043 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
1044 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
1045 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
1046 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
1047 }
1048
1049 /**
1050 * @tc.name: StatsBluetoothTest_030
1051 * @tc.desc: test GetAppStatsPercent function(Bluetooth BLE scan)
1052 * @tc.type: FUNC
1053 * @tc.require: issueI5HWJK
1054 */
1055 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_030, TestSize.Level0)
1056 {
1057 auto& statsClient = BatteryStatsClient::GetInstance();
1058 statsClient.Reset();
1059
1060 int32_t uid = 10003;
1061 int32_t pid = 3458;
1062 double fullPercent = 1;
1063 double zeroPercent = 0;
1064
1065 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_START", HiSysEvent::EventType::STATISTIC,
1066 "PID", pid, "UID", uid);
1067 usleep(POWER_CONSUMPTION_DURATION_US);
1068 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_STOP", HiSysEvent::EventType::STATISTIC,
1069 "PID", pid, "UID", uid);
1070
1071 double actualPercent = statsClient.GetAppStatsPercent(uid);
1072 GTEST_LOG_(INFO) << __func__ << ": actual percent = " << actualPercent;
1073 EXPECT_TRUE(actualPercent >= zeroPercent && actualPercent <= fullPercent);
1074 }
1075
1076 /**
1077 * @tc.name: StatsBluetoothTest_031
1078 * @tc.desc: test Bluetooth BLE scan event are sent repeatedly, Bluetooth BLE power consumption(Bluetooth BLE scan)
1079 * @tc.type: FUNC
1080 * @tc.require: issueI5HWJK
1081 */
1082 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_031, TestSize.Level0)
1083 {
1084 auto& statsClient = BatteryStatsClient::GetInstance();
1085 statsClient.Reset();
1086
1087 double bluetoothBleScanAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BLE_SCAN);
1088 int32_t uid = 10003;
1089 int32_t pid = 3458;
1090
1091 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_START", HiSysEvent::EventType::STATISTIC,
1092 "PID", pid, "UID", uid);
1093 usleep(POWER_CONSUMPTION_DURATION_US);
1094 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_START", HiSysEvent::EventType::STATISTIC,
1095 "PID", pid, "UID", uid);
1096 usleep(POWER_CONSUMPTION_DURATION_US);
1097 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_STOP", HiSysEvent::EventType::STATISTIC,
1098 "PID", pid, "UID", uid);
1099 usleep(POWER_CONSUMPTION_DURATION_US);
1100 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_STOP", HiSysEvent::EventType::STATISTIC,
1101 "PID", pid, "UID", uid);
1102
1103 double expectedPower = 2 * POWER_CONSUMPTION_DURATION_US * bluetoothBleScanAverageMa / US_PER_HOUR;
1104 double actualPower = statsClient.GetAppStatsMah(uid);
1105 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
1106 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
1107 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
1108 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
1109 }
1110
1111 /**
1112 * @tc.name: StatsBluetoothTest_032
1113 * @tc.desc: test GetTotalTimeSecond function(Bluetooth BLE scan)
1114 * @tc.type: FUNC
1115 * @tc.require: issueI5HWJK
1116 */
1117 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_032, TestSize.Level0)
1118 {
1119 auto& statsClient = BatteryStatsClient::GetInstance();
1120 statsClient.Reset();
1121
1122 int32_t uid = 10003;
1123 int32_t pid = 3458;
1124
1125 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_START", HiSysEvent::EventType::STATISTIC,
1126 "PID", pid, "UID", uid);
1127 usleep(POWER_CONSUMPTION_DURATION_US);
1128 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_STOP", HiSysEvent::EventType::STATISTIC,
1129 "PID", pid, "UID", uid);
1130
1131 long expectedTime = round(POWER_CONSUMPTION_DURATION_US / US_PER_SECOND);
1132 long actualTime = statsClient.GetTotalTimeSecond(StatsUtils::STATS_TYPE_BLUETOOTH_BLE_SCAN, uid);
1133 GTEST_LOG_(INFO) << __func__ << ": expected time = " << expectedTime << " seconds";
1134 GTEST_LOG_(INFO) << __func__ << ": actual time = " << actualTime << " seconds";
1135 EXPECT_EQ(expectedTime, actualTime);
1136 }
1137
1138 /**
1139 * @tc.name: StatsBluetoothTest_033
1140 * @tc.desc: test GetAppStatsMah function with different UID(Bluetooth BLE scan)
1141 * @tc.type: FUNC
1142 * @tc.require: issueI5HWJK
1143 */
1144 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_033, TestSize.Level0)
1145 {
1146 auto& statsClient = BatteryStatsClient::GetInstance();
1147 statsClient.Reset();
1148
1149 double bluetoothBleScanAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BLE_SCAN);
1150 int32_t uidOne = 10003;
1151 int32_t pidOne = 3458;
1152 int32_t uidTwo = 10004;
1153 int32_t pidTwo = 3459;
1154
1155 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_START", HiSysEvent::EventType::STATISTIC,
1156 "PID", pidOne, "UID", uidOne);
1157 usleep(POWER_CONSUMPTION_DURATION_US);
1158 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_START", HiSysEvent::EventType::STATISTIC,
1159 "PID", pidTwo, "UID", uidTwo);
1160 usleep(POWER_CONSUMPTION_DURATION_US);
1161 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_STOP", HiSysEvent::EventType::STATISTIC,
1162 "PID", pidTwo, "UID", uidTwo);
1163 usleep(POWER_CONSUMPTION_DURATION_US);
1164 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BLE_SCAN_STOP", HiSysEvent::EventType::STATISTIC,
1165 "PID", pidOne, "UID", uidOne);
1166
1167 double expectedPowerOne = 3 * POWER_CONSUMPTION_DURATION_US * bluetoothBleScanAverageMa / US_PER_HOUR;
1168 double actualPowerOne = statsClient.GetAppStatsMah(uidOne);
1169 double devPrecentOne = abs(expectedPowerOne - actualPowerOne) / expectedPowerOne;
1170 GTEST_LOG_(INFO) << __func__ << ": expected consumption One = " << expectedPowerOne << " mAh";
1171 GTEST_LOG_(INFO) << __func__ << ": actual consumption One = " << actualPowerOne << " mAh";
1172 EXPECT_LE(devPrecentOne, DEVIATION_PERCENT_THRESHOLD);
1173
1174 double expectedPowerTwo = POWER_CONSUMPTION_DURATION_US * bluetoothBleScanAverageMa / US_PER_HOUR;
1175 double actualPowerTwo = statsClient.GetAppStatsMah(uidTwo);
1176 double devPrecentTwo = abs(expectedPowerTwo - actualPowerTwo) / expectedPowerTwo;
1177 GTEST_LOG_(INFO) << __func__ << ": expected consumption Two = " << expectedPowerTwo << " mAh";
1178 GTEST_LOG_(INFO) << __func__ << ": actual consumption Two = " << actualPowerTwo << " mAh";
1179 EXPECT_LE(devPrecentTwo, DEVIATION_PERCENT_THRESHOLD);
1180 }
1181
1182 /**
1183 * @tc.name: StatsBluetoothTest_034
1184 * @tc.desc: test GetPartStatsMah function(Bluetooth BR on & BLE on & BR scan & BLE scan)
1185 * @tc.type: FUNC
1186 * @tc.require: issueI5HWJK
1187 */
1188 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_034, TestSize.Level0)
1189 {
1190 auto& statsClient = BatteryStatsClient::GetInstance();
1191 statsClient.Reset();
1192
1193 double bluetoothBrOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_ON);
1194 double bluetoothBleOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BLE_ON);
1195 double bluetoothBrScanAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_SCAN);
1196 double bluetoothBleScanAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BLE_SCAN);
1197 long testTimeMs = 200;
1198 int32_t uid = 10003;
1199 int32_t pid = 3458;
1200
1201 WriteBluetoothEvent(pid, uid, testTimeMs);
1202
1203 double expectedPartPower = testTimeMs * (bluetoothBrOnAverageMa + bluetoothBleOnAverageMa) / MS_PER_HOUR;
1204 double actualPartPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
1205 double devPrecentPart = abs(expectedPartPower - actualPartPower) / expectedPartPower;
1206 GTEST_LOG_(INFO) << __func__ << ": expected part consumption = " << expectedPartPower << " mAh";
1207 GTEST_LOG_(INFO) << __func__ << ": actual part consumption = " << actualPartPower << " mAh";
1208 EXPECT_LE(devPrecentPart, DEVIATION_PERCENT_THRESHOLD);
1209
1210 double expectedSoftPower = testTimeMs * (bluetoothBrScanAverageMa + bluetoothBleScanAverageMa) / MS_PER_HOUR;
1211 double actualSoftPower = statsClient.GetAppStatsMah(uid);
1212 double devPrecentSoft = abs(expectedSoftPower - actualSoftPower) / expectedSoftPower;
1213 GTEST_LOG_(INFO) << __func__ << ": expected soft consumption = " << expectedSoftPower << " mAh";
1214 GTEST_LOG_(INFO) << __func__ << ": actual soft consumption = " << actualSoftPower << " mAh";
1215 EXPECT_LE(devPrecentSoft, DEVIATION_PERCENT_THRESHOLD);
1216 }
1217
1218 /**
1219 * @tc.name: StatsBluetoothTest_035
1220 * @tc.desc: test SetOnBattery function(Bluetooth BR on & BLE on & BR scan & BLE scan)
1221 * @tc.type: FUNC
1222 * @tc.require: issueI5HWJK
1223 */
1224 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_035, TestSize.Level0)
1225 {
1226 auto& statsClient = BatteryStatsClient::GetInstance();
1227 statsClient.Reset();
1228 statsClient.SetOnBattery(false);
1229
1230 long testTimeMs = 200;
1231 int32_t uid = 10003;
1232 int32_t pid = 3458;
1233
1234 WriteBluetoothEvent(pid, uid, testTimeMs);
1235
1236 double expectedPower = StatsUtils::DEFAULT_VALUE;
1237 double actualPartPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
1238 GTEST_LOG_(INFO) << __func__ << ": expected part consumption = " << expectedPower << " mAh";
1239 GTEST_LOG_(INFO) << __func__ << ": actual part consumption = " << actualPartPower << " mAh";
1240 EXPECT_EQ(expectedPower, actualPartPower);
1241
1242 double actualSoftPower = statsClient.GetAppStatsMah(uid);
1243 GTEST_LOG_(INFO) << __func__ << ": expected soft consumption = " << expectedPower << " mAh";
1244 GTEST_LOG_(INFO) << __func__ << ": actual soft consumption = " << actualSoftPower << " mAh";
1245 EXPECT_EQ(expectedPower, actualSoftPower);
1246 statsClient.SetOnBattery(true);
1247 }
1248
1249 /**
1250 * @tc.name: StatsBluetoothTest_036
1251 * @tc.desc: test GetPartStatsMah function with battery changed(Bluetooth BR on)
1252 * @tc.type: FUNC
1253 * @tc.require: issueI5HWJK
1254 */
1255 HWTEST_F (StatsBluetoothTest, StatsBluetoothTest_036, TestSize.Level0)
1256 {
1257 auto& statsClient = BatteryStatsClient::GetInstance();
1258 statsClient.Reset();
1259
1260 double bluetoothBrOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_ON);
1261 int32_t uid = 10003;
1262 int32_t pid = 3458;
1263 int32_t stateOn = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_ON);
1264 int32_t stateOff = static_cast<int32_t>(bluetooth::BTStateID::STATE_TURN_OFF);
1265
1266 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
1267 "PID", pid, "UID", uid, "STATE", stateOn);
1268 usleep(POWER_CONSUMPTION_DURATION_US);
1269 statsClient.SetOnBattery(false);
1270 usleep(POWER_CONSUMPTION_DURATION_US);
1271 statsClient.SetOnBattery(true);
1272 usleep(POWER_CONSUMPTION_DURATION_US);
1273 HiSysEvent::Write("BLUETOOTH", "BLUETOOTH_BR_SWITCH_STATE", HiSysEvent::EventType::STATISTIC,
1274 "PID", pid, "UID", uid, "STATE", stateOff);
1275
1276 double expectedPower = 2 * POWER_CONSUMPTION_DURATION_US * bluetoothBrOnAverageMa / US_PER_HOUR;
1277 double actualPower = statsClient.GetPartStatsMah(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
1278 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
1279 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
1280 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
1281 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
1282 }
1283 }