1 /*
2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "stats_service_subscriber_test.h"
17 #include "stats_log.h"
18 #ifdef HAS_BATTERYSTATS_CALL_MANAGER_PART
19 #include <call_manager_inner_type.h>
20 #endif
21 #include <hisysevent.h>
22
23 #include "battery_stats_listener.h"
24 #include "battery_stats_subscriber.h"
25 #include "common_event_data.h"
26 #include "common_event_manager.h"
27 #include "common_event_support.h"
28 #include "hisysevent_operation.h"
29 #include "stats_hisysevent.h"
30 #include "stats_service_test_proxy.h"
31 #include "stats_service_write_event.h"
32
33 using namespace OHOS;
34 using namespace OHOS::AAFwk;
35 using namespace OHOS::EventFwk;
36 using namespace OHOS::HiviewDFX;
37 using namespace OHOS::PowerMgr;
38 #ifdef HAS_BATTERYSTATS_CALL_MANAGER_PART
39 using namespace OHOS::Telephony;
40 #endif
41 using namespace std;
42 using namespace testing::ext;
43
44 namespace {
45 static sptr<BatteryStatsService> g_statsService = nullptr;
46 static std::shared_ptr<StatsServiceTestProxy> g_statsServiceProxy = nullptr;
47 const int32_t BATTERY_LEVEL_FULL = 100;
48 } // namespace
49
SetUpTestCase()50 void StatsServiceSubscriberTest::SetUpTestCase()
51 {
52 ParserAveragePowerFile();
53 g_statsService = BatteryStatsService::GetInstance();
54 g_statsService->OnStart();
55
56 if (g_statsService->listenerPtr_ == nullptr) {
57 g_statsService->listenerPtr_ = std::make_shared<BatteryStatsListener>();
58 }
59
60 if (g_statsService->subscriberPtr_ == nullptr) {
61 OHOS::EventFwk::MatchingSkills matchingSkills {};
62 OHOS::EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
63 g_statsService->subscriberPtr_ = std::make_shared<BatteryStatsSubscriber>(subscribeInfo);
64 }
65
66 if (g_statsServiceProxy == nullptr) {
67 g_statsServiceProxy = std::make_shared<StatsServiceTestProxy>(g_statsService);
68 }
69 }
70
TearDownTestCase()71 void StatsServiceSubscriberTest::TearDownTestCase()
72 {
73 g_statsService->listenerPtr_ = nullptr;
74 g_statsService->subscriberPtr_ = nullptr;
75 g_statsService->OnStop();
76 }
77
SetUp()78 void StatsServiceSubscriberTest::SetUp()
79 {
80 auto statsService = BatteryStatsService::GetInstance();
81 statsService->SetOnBattery(true);
82 }
83
TearDown()84 void StatsServiceSubscriberTest::TearDown()
85 {
86 auto statsService = BatteryStatsService::GetInstance();
87 statsService->SetOnBattery(false);
88 }
89
PublishChangedEvent(const sptr<BatteryStatsService>& service, const std::string& action)90 void StatsServiceSubscriberTest::PublishChangedEvent(const sptr<BatteryStatsService>& service,
91 const std::string& action)
92 {
93 Want want;
94 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CAPACITY, capacity_);
95 want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_TYPE, static_cast<int>(pluggedType_));
96 want.SetAction(action);
97 CommonEventData data;
98 data.SetWant(want);
99 if (service->subscriberPtr_ != nullptr) {
100 g_statsService->subscriberPtr_->OnReceiveEvent(data);
101 }
102 }
103
104 namespace {
105 /**
106 * @tc.name: StatsServiceSubscriberTest_001
107 * @tc.desc: test COMMON_EVENT_BATTERY_CHANGED with capacity is BATTERY_LEVEL_FULL(Alarm)
108 * @tc.type: FUNC
109 * @tc.require: issueI663DX
110 */
HWTEST_F(StatsServiceSubscriberTest, StatsServiceSubscriberTest_001, TestSize.Level0)111 HWTEST_F (StatsServiceSubscriberTest, StatsServiceSubscriberTest_001, TestSize.Level0)
112 {
113 STATS_HILOGI(LABEL_TEST, "StatsServiceSubscriberTest_001 start");
114 ASSERT_NE(g_statsServiceProxy, nullptr);
115 auto statsService = BatteryStatsService::GetInstance();
116 g_statsServiceProxy->Reset();
117 BatteryInfoReset();
118
119 int32_t uid = 10003;
120 int32_t pid = 3458;
121 int16_t count = 10;
122
123 for (int16_t i = 0; i < count; i++) {
124 StatsWriteHiSysEvent(statsService,
125 HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
126 "CALLER_PID", pid, "CALLER_UID", uid);
127 }
128
129 double powerMahBefore = g_statsServiceProxy->GetAppStatsMah(uid);
130 SetCapacity(BATTERY_LEVEL_FULL);
131 PublishChangedEvent(statsService, CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
132 double powerMahAfter = g_statsServiceProxy->GetAppStatsMah(uid);
133 GTEST_LOG_(INFO) << __func__ << ": before consumption = " << powerMahBefore << " mAh";
134 GTEST_LOG_(INFO) << __func__ << ": after consumption = " << powerMahAfter << " mAh";
135 EXPECT_TRUE(powerMahBefore >= StatsUtils::DEFAULT_VALUE && powerMahAfter == StatsUtils::DEFAULT_VALUE);
136 STATS_HILOGI(LABEL_TEST, "StatsServiceSubscriberTest_001 end");
137 }
138
139 /**
140 * @tc.name: StatsServiceSubscriberTest_002
141 * @tc.desc: test COMMON_EVENT_BATTERY_CHANGED with BatteryPluggedType(Alarm)
142 * @tc.type: FUNC
143 * @tc.require: issueI663DX
144 */
HWTEST_F(StatsServiceSubscriberTest, StatsServiceSubscriberTest_002, TestSize.Level0)145 HWTEST_F (StatsServiceSubscriberTest, StatsServiceSubscriberTest_002, TestSize.Level0)
146 {
147 STATS_HILOGI(LABEL_TEST, "StatsServiceSubscriberTest_002 start");
148 ASSERT_NE(g_statsServiceProxy, nullptr);
149 auto statsService = BatteryStatsService::GetInstance();
150 g_statsServiceProxy->Reset();
151 g_statsServiceProxy->SetOnBattery(false);
152 BatteryInfoReset();
153
154 double alarmOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_ALARM_ON);
155 int32_t uid = 10003;
156 int32_t pid = 3458;
157
158 SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_NONE);
159 PublishChangedEvent(statsService, CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
160 StatsWriteHiSysEvent(statsService,
161 HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
162 "CALLER_PID", pid, "CALLER_UID", uid);
163
164 SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_USB);
165 PublishChangedEvent(statsService, CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
166 StatsWriteHiSysEvent(statsService,
167 HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
168 "CALLER_PID", pid, "CALLER_UID", uid);
169
170 SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_BUTT);
171 PublishChangedEvent(statsService, CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
172 StatsWriteHiSysEvent(statsService,
173 HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
174 "CALLER_PID", pid, "CALLER_UID", uid);
175
176 double expectedPower = 2 * alarmOnAverageMa;
177 double actualPower = g_statsServiceProxy->GetAppStatsMah(uid);
178 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
179 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
180 EXPECT_EQ(expectedPower, actualPower);
181 g_statsServiceProxy->SetOnBattery(true);
182 STATS_HILOGI(LABEL_TEST, "StatsServiceSubscriberTest_002 end");
183 }
184
185 /**
186 * @tc.name: StatsServiceSubscriberTest_003
187 * @tc.desc: test COMMON_EVENT_BATTERY_CHANGED with capacity is BATTERY_LEVEL_FULL(Audio)
188 * @tc.type: FUNC
189 * @tc.require: issueI663DX
190 */
HWTEST_F(StatsServiceSubscriberTest, StatsServiceSubscriberTest_003, TestSize.Level0)191 HWTEST_F (StatsServiceSubscriberTest, StatsServiceSubscriberTest_003, TestSize.Level0)
192 {
193 STATS_HILOGI(LABEL_TEST, "StatsServiceSubscriberTest_003 start");
194 ASSERT_NE(g_statsServiceProxy, nullptr);
195 auto statsService = BatteryStatsService::GetInstance();
196 g_statsServiceProxy->Reset();
197 BatteryInfoReset();
198
199 int32_t uid = 10003;
200 int32_t pid = 3458;
201 int32_t stateRunning = 2;
202 int32_t stateStopped = 3;
203
204 StatsWriteHiSysEvent(statsService,
205 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
206 "UID", uid, "STATE", stateRunning);
207 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
208 StatsWriteHiSysEvent(statsService,
209 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
210 "UID", uid, "STATE", stateStopped);
211
212 double powerMahBefore = g_statsServiceProxy->GetAppStatsMah(uid);
213 SetCapacity(BATTERY_LEVEL_FULL);
214 PublishChangedEvent(statsService, CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
215 double powerMahAfter = g_statsServiceProxy->GetAppStatsMah(uid);
216 GTEST_LOG_(INFO) << __func__ << ": before consumption = " << powerMahBefore << " mAh";
217 GTEST_LOG_(INFO) << __func__ << ": after consumption = " << powerMahAfter << " mAh";
218 EXPECT_TRUE(powerMahBefore >= StatsUtils::DEFAULT_VALUE && powerMahAfter == StatsUtils::DEFAULT_VALUE);
219 STATS_HILOGI(LABEL_TEST, "StatsServiceSubscriberTest_003 end");
220 }
221
222 /**
223 * @tc.name: StatsServiceSubscriberTest_004
224 * @tc.desc: test COMMON_EVENT_BATTERY_CHANGED with BatteryPluggedType(Audio)
225 * @tc.type: FUNC
226 * @tc.require: issueI663DX
227 */
HWTEST_F(StatsServiceSubscriberTest, StatsServiceSubscriberTest_004, TestSize.Level0)228 HWTEST_F (StatsServiceSubscriberTest, StatsServiceSubscriberTest_004, TestSize.Level0)
229 {
230 STATS_HILOGI(LABEL_TEST, "StatsServiceSubscriberTest_004 start");
231 ASSERT_NE(g_statsServiceProxy, nullptr);
232 auto statsService = BatteryStatsService::GetInstance();
233 g_statsServiceProxy->Reset();
234 g_statsServiceProxy->SetOnBattery(false);
235 BatteryInfoReset();
236
237 double audioOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_AUDIO_ON);
238 int32_t uid = 10003;
239 int32_t pid = 3458;
240 int32_t stateRunning = 2;
241 int32_t stateStopped = 3;
242
243 SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_NONE);
244 PublishChangedEvent(statsService, CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
245 StatsWriteHiSysEvent(statsService,
246 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
247 "UID", uid, "STATE", stateRunning);
248 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
249 StatsWriteHiSysEvent(statsService,
250 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
251 "UID", uid, "STATE", stateStopped);
252
253 SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_AC);
254 PublishChangedEvent(statsService, CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
255 StatsWriteHiSysEvent(statsService,
256 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
257 "UID", uid, "STATE", stateRunning);
258 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
259 StatsWriteHiSysEvent(statsService,
260 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
261 "UID", uid, "STATE", stateStopped);
262
263 SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_BUTT);
264 PublishChangedEvent(statsService, CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
265 StatsWriteHiSysEvent(statsService,
266 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
267 "UID", uid, "STATE", stateRunning);
268 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
269 StatsWriteHiSysEvent(statsService,
270 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
271 "UID", uid, "STATE", stateStopped);
272
273 double expectedPower = 2 * SERVICE_POWER_CONSUMPTION_DURATION_US * audioOnAverageMa / US_PER_HOUR;
274 double actualPower = g_statsServiceProxy->GetAppStatsMah(uid);
275 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
276 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
277 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
278 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
279 STATS_HILOGI(LABEL_TEST, "StatsServiceSubscriberTest_004 end");
280 }
281
282 /**
283 * @tc.name: StatsServiceSubscriberTest_005
284 * @tc.desc: test COMMON_EVENT_BATTERY_LOW is no use(Audio)
285 * @tc.type: FUNC
286 * @tc.require: issueI663DX
287 */
HWTEST_F(StatsServiceSubscriberTest, StatsServiceSubscriberTest_005, TestSize.Level0)288 HWTEST_F (StatsServiceSubscriberTest, StatsServiceSubscriberTest_005, TestSize.Level0)
289 {
290 STATS_HILOGI(LABEL_TEST, "StatsServiceSubscriberTest_005 start");
291 ASSERT_NE(g_statsServiceProxy, nullptr);
292 auto statsService = BatteryStatsService::GetInstance();
293 g_statsServiceProxy->Reset();
294 BatteryInfoReset();
295
296 double audioOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_AUDIO_ON);
297 int32_t uid = 10003;
298 int32_t pid = 3458;
299 int32_t stateRunning = 2;
300 int32_t stateStopped = 3;
301
302 StatsWriteHiSysEvent(statsService,
303 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
304 "UID", uid, "STATE", stateRunning);
305 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
306 StatsWriteHiSysEvent(statsService,
307 HiSysEvent::Domain::AUDIO, StatsHiSysEvent::STREAM_CHANGE, HiSysEvent::EventType::BEHAVIOR, "PID", pid,
308 "UID", uid, "STATE", stateStopped);
309
310 SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_AC);
311 PublishChangedEvent(statsService, CommonEventSupport::COMMON_EVENT_BATTERY_LOW);
312 double expectedPower = SERVICE_POWER_CONSUMPTION_DURATION_US * audioOnAverageMa / US_PER_HOUR;
313 double actualPower = g_statsServiceProxy->GetAppStatsMah(uid);
314 double devPrecent = abs(expectedPower - actualPower) / expectedPower;
315 GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
316 GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
317 EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
318 STATS_HILOGI(LABEL_TEST, "StatsServiceSubscriberTest_005 end");
319 }
320
321 /**
322 * @tc.name: StatsServiceSubscriberTest_006
323 * @tc.desc: test COMMON_EVENT_SHUTDOWN(Phone)
324 * @tc.type: FUNC
325 * @tc.require: issueI663DX
326 */
HWTEST_F(StatsServiceSubscriberTest, StatsServiceSubscriberTest_006, TestSize.Level0)327 HWTEST_F (StatsServiceSubscriberTest, StatsServiceSubscriberTest_006, TestSize.Level0)
328 {
329 STATS_HILOGI(LABEL_TEST, "StatsServiceSubscriberTest_006 start");
330 ASSERT_NE(g_statsServiceProxy, nullptr);
331 auto statsService = BatteryStatsService::GetInstance();
332 g_statsServiceProxy->Reset();
333
334 int32_t stateOn = static_cast<int32_t>(TelCallState::CALL_STATUS_ACTIVE);
335 int32_t stateOff = static_cast<int32_t>(TelCallState::CALL_STATUS_DISCONNECTED);
336 int16_t level = 0;
337 double phoneOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_RADIO_ON, level);
338
339 StatsWriteHiSysEvent(statsService,
340 HiSysEvent::Domain::TELEPHONY, StatsHiSysEvent::CALL_STATE, HiSysEvent::EventType::BEHAVIOR, "STATE", stateOn);
341 usleep(SERVICE_POWER_CONSUMPTION_DURATION_US);
342 StatsWriteHiSysEvent(statsService,
343 HiSysEvent::Domain::TELEPHONY, StatsHiSysEvent::CALL_STATE, HiSysEvent::EventType::BEHAVIOR, "STATE", stateOff);
344
345 PublishChangedEvent(statsService, CommonEventSupport::COMMON_EVENT_SHUTDOWN);
346 g_statsServiceProxy->Reset();
347
348 auto statsCore = statsService->GetBatteryStatsCore();
349 statsCore->Init();
350 BatteryStatsInfoList statsInfoList {};
351 statsInfoList = statsCore->GetBatteryStats();
352
353 double expectedPower = SERVICE_POWER_CONSUMPTION_DURATION_US * phoneOnAverageMa / US_PER_HOUR;
354 double actualPower = StatsUtils::DEFAULT_VALUE;
355 for (auto it : statsInfoList) {
356 if ((*it).GetConsumptionType() == BatteryStatsInfo::CONSUMPTION_TYPE_PHONE) {
357 actualPower = (*it).GetPower();
358 }
359 }
360
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 STATS_HILOGI(LABEL_TEST, "StatsServiceSubscriberTest_006 end");
366 }
367 }