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