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_alarm_test.h"
17 #include "stats_log.h"
18 
19 #include <hisysevent.h>
20 
21 #include "battery_stats_listener.h"
22 #include "battery_stats_service.h"
23 #include "hisysevent_operation.h"
24 #include "stats_hisysevent.h"
25 #include "stats_service_test_proxy.h"
26 #include "stats_service_write_event.h"
27 
28 using namespace OHOS;
29 using namespace OHOS::HiviewDFX;
30 using namespace OHOS::PowerMgr;
31 using namespace std;
32 using namespace testing::ext;
33 
34 namespace {
35 static sptr<BatteryStatsService> g_statsService = nullptr;
36 static std::shared_ptr<StatsServiceTestProxy> g_statsServiceProxy = nullptr;
37 } // namespace
38 
SetUpTestCase()39 void StatsServiceAlarmTest::SetUpTestCase()
40 {
41     ParserAveragePowerFile();
42     g_statsService = BatteryStatsService::GetInstance();
43     g_statsService->OnStart();
44 
45     if (g_statsService->listenerPtr_ == nullptr) {
46         g_statsService->listenerPtr_ = std::make_shared<BatteryStatsListener>();
47     }
48 
49     if (g_statsServiceProxy == nullptr) {
50         g_statsServiceProxy = std::make_shared<StatsServiceTestProxy>(g_statsService);
51     }
52 }
53 
TearDownTestCase()54 void StatsServiceAlarmTest::TearDownTestCase()
55 {
56     g_statsService->listenerPtr_ = nullptr;
57     g_statsService->OnStop();
58 }
59 
SetUp()60 void StatsServiceAlarmTest::SetUp()
61 {
62     auto statsService = BatteryStatsService::GetInstance();
63     statsService->SetOnBattery(true);
64 }
65 
TearDown()66 void StatsServiceAlarmTest::TearDown()
67 {
68     auto statsService = BatteryStatsService::GetInstance();
69     statsService->SetOnBattery(false);
70 }
71 
72 namespace {
73 /**
74  * @tc.name: StatsServiceAlarmTest_001
75  * @tc.desc: test Reset function(Alarm)
76  * @tc.type: FUNC
77  * @tc.require: issueI663DX
78  */
HWTEST_F(StatsServiceAlarmTest, StatsServiceAlarmTest_001, TestSize.Level0)79 HWTEST_F (StatsServiceAlarmTest, StatsServiceAlarmTest_001, TestSize.Level0)
80 {
81     STATS_HILOGI(LABEL_TEST, "StatsServiceAlarmTest_001 start");
82     ASSERT_NE(g_statsServiceProxy, nullptr);
83     auto statsService = BatteryStatsService::GetInstance();
84     g_statsServiceProxy->Reset();
85 
86     int32_t uid = 10003;
87     int32_t pid = 3458;
88     int16_t count = 10;
89 
90     for (int16_t i = 0; i < count; i++) {
91         StatsWriteHiSysEvent(statsService,
92             HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
93             "CALLER_PID", pid, "CALLER_UID", uid);
94     }
95 
96     double powerMahBefore = g_statsServiceProxy->GetAppStatsMah(uid);
97     g_statsServiceProxy->Reset();
98     double powerMahAfter = g_statsServiceProxy->GetAppStatsMah(uid);
99     GTEST_LOG_(INFO) << __func__ << ": before consumption = " << powerMahBefore << " mAh";
100     GTEST_LOG_(INFO) << __func__ << ": after consumption = " << powerMahAfter << " mAh";
101     EXPECT_TRUE(powerMahBefore >= StatsUtils::DEFAULT_VALUE && powerMahAfter == StatsUtils::DEFAULT_VALUE);
102     STATS_HILOGI(LABEL_TEST, "StatsServiceAlarmTest_001 end");
103 }
104 
105 /**
106  * @tc.name: StatsServiceAlarmTest_002
107  * @tc.desc: test GetPartStatsMah function(Alarm)
108  * @tc.type: FUNC
109  * @tc.require: issueI663DX
110  */
HWTEST_F(StatsServiceAlarmTest, StatsServiceAlarmTest_002, TestSize.Level0)111 HWTEST_F (StatsServiceAlarmTest, StatsServiceAlarmTest_002, TestSize.Level0)
112 {
113     STATS_HILOGI(LABEL_TEST, "StatsServiceAlarmTest_002 start");
114     ASSERT_NE(g_statsServiceProxy, nullptr);
115     auto statsService = BatteryStatsService::GetInstance();
116     g_statsServiceProxy->Reset();
117 
118     double alarmOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_ALARM_ON);
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 expectedPower = count * alarmOnAverageMa;
130     double actualPower = g_statsServiceProxy->GetAppStatsMah(uid);
131     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
132     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
133     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
134     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
135     STATS_HILOGI(LABEL_TEST, "StatsServiceAlarmTest_002 end");
136 }
137 
138 /**
139  * @tc.name: StatsServiceAlarmTest_003
140  * @tc.desc: test GetAppStatsPercent function(Alarm)
141  * @tc.type: FUNC
142  * @tc.require: issueI663DX
143  */
HWTEST_F(StatsServiceAlarmTest, StatsServiceAlarmTest_003, TestSize.Level0)144 HWTEST_F (StatsServiceAlarmTest, StatsServiceAlarmTest_003, TestSize.Level0)
145 {
146     STATS_HILOGI(LABEL_TEST, "StatsServiceAlarmTest_003 start");
147     ASSERT_NE(g_statsServiceProxy, nullptr);
148     auto statsService = BatteryStatsService::GetInstance();
149     g_statsServiceProxy->Reset();
150 
151     int32_t uid = 10003;
152     int32_t pid = 3458;
153     int16_t count = 10;
154     double fullPercent = 1;
155     double zeroPercent = 0;
156 
157     for (int16_t i = 0; i < count; i++) {
158         StatsWriteHiSysEvent(statsService,
159             HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
160             "CALLER_PID", pid, "CALLER_UID", uid);
161     }
162 
163     double actualPercent = g_statsServiceProxy->GetAppStatsPercent(uid);
164     GTEST_LOG_(INFO) << __func__ << ": actual percent = " << actualPercent;
165     EXPECT_TRUE(actualPercent >= zeroPercent && actualPercent <= fullPercent);
166     STATS_HILOGI(LABEL_TEST, "StatsServiceAlarmTest_003 end");
167 }
168 
169 /**
170  * @tc.name: StatsServiceAlarmTest_004
171  * @tc.desc: test SetOnBattery function(Alarm)
172  * @tc.type: FUNC
173  * @tc.require: issueI663DX
174  */
HWTEST_F(StatsServiceAlarmTest, StatsServiceAlarmTest_004, TestSize.Level0)175 HWTEST_F (StatsServiceAlarmTest, StatsServiceAlarmTest_004, TestSize.Level0)
176 {
177     STATS_HILOGI(LABEL_TEST, "StatsServiceAlarmTest_004 start");
178     ASSERT_NE(g_statsServiceProxy, nullptr);
179     auto statsService = BatteryStatsService::GetInstance();
180     g_statsServiceProxy->Reset();
181     g_statsServiceProxy->SetOnBattery(false);
182 
183     int32_t uid = 10003;
184     int32_t pid = 3458;
185     int16_t count = 10;
186 
187     for (int16_t i = 0; i < count; i++) {
188         StatsWriteHiSysEvent(statsService,
189             HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
190             "CALLER_PID", pid, "CALLER_UID", uid);
191     }
192 
193     double expectedPower = StatsUtils::DEFAULT_VALUE;
194     double actualPower = g_statsServiceProxy->GetAppStatsMah(uid);
195     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
196     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
197     EXPECT_EQ(expectedPower, actualPower);
198     g_statsServiceProxy->SetOnBattery(true);
199     STATS_HILOGI(LABEL_TEST, "StatsServiceAlarmTest_004 end");
200 }
201 
202 /**
203  * @tc.name: StatsServiceAlarmTest_005
204  * @tc.desc: test alarm entity GetPartStatsMah function(Alarm)
205  * @tc.type: FUNC
206  * @tc.require: issueI663DX
207  */
HWTEST_F(StatsServiceAlarmTest, StatsServiceAlarmTest_005, TestSize.Level0)208 HWTEST_F (StatsServiceAlarmTest, StatsServiceAlarmTest_005, TestSize.Level0)
209 {
210     STATS_HILOGI(LABEL_TEST, "StatsServiceAlarmTest_005 start");
211     ASSERT_NE(g_statsServiceProxy, nullptr);
212     auto statsService = BatteryStatsService::GetInstance();
213     g_statsServiceProxy->Reset();
214 
215     double alarmOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_ALARM_ON);
216     int32_t uid = 10003;
217     int32_t pid = 3458;
218     int16_t count = 10;
219 
220     for (int16_t i = 0; i < count; i++) {
221         StatsWriteHiSysEvent(statsService,
222             HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
223             "CALLER_PID", pid, "CALLER_UID", uid);
224     }
225 
226     auto statsCore = statsService->GetBatteryStatsCore();
227     auto alarmEntity = statsCore->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_ALARM);
228     statsCore->ComputePower();
229 
230     double expectedPower = count * alarmOnAverageMa;
231     double actualPower = alarmEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_ALARM, uid);
232     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
233     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
234     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
235     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
236 
237     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, alarmEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_ALARM));
238     EXPECT_EQ(StatsUtils::DEFAULT_VALUE, alarmEntity->GetStatsPowerMah(StatsUtils::STATS_TYPE_INVALID, uid));
239     STATS_HILOGI(LABEL_TEST, "StatsServiceAlarmTest_005 end");
240 }
241 
242 /**
243  * @tc.name: StatsServiceAlarmTest_006
244  * @tc.desc: test uid entity GetPartStatsMah function(Alarm)
245  * @tc.type: FUNC
246  * @tc.require: issueI663DX
247  */
HWTEST_F(StatsServiceAlarmTest, StatsServiceAlarmTest_006, TestSize.Level0)248 HWTEST_F (StatsServiceAlarmTest, StatsServiceAlarmTest_006, TestSize.Level0)
249 {
250     STATS_HILOGI(LABEL_TEST, "StatsServiceAlarmTest_006 start");
251     ASSERT_NE(g_statsServiceProxy, nullptr);
252     auto statsService = BatteryStatsService::GetInstance();
253     g_statsServiceProxy->Reset();
254 
255     double alarmOnAverageMa = g_statsParser->GetAveragePowerMa(StatsUtils::CURRENT_ALARM_ON);
256     int32_t uid = 10003;
257     int32_t pid = 3458;
258     int16_t count = 10;
259 
260     for (int16_t i = 0; i < count; i++) {
261         StatsWriteHiSysEvent(statsService,
262             HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC,
263             "CALLER_PID", pid, "CALLER_UID", uid);
264     }
265 
266     auto statsCore = statsService->GetBatteryStatsCore();
267     auto uidEntity = statsCore->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_APP);
268     statsCore->ComputePower();
269 
270     double expectedPower = count * alarmOnAverageMa;
271     double actualPower = uidEntity->GetEntityPowerMah(uid);
272     double devPrecent = abs(expectedPower - actualPower) / expectedPower;
273     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
274     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
275     EXPECT_LE(devPrecent, DEVIATION_PERCENT_THRESHOLD);
276     STATS_HILOGI(LABEL_TEST, "StatsServiceAlarmTest_006 end");
277 }
278 
279 /**
280  * @tc.name: StatsServiceAlarmTest_007
281  * @tc.desc: test send hisysevent with missing information(Alarm)
282  * @tc.type: FUNC
283  * @tc.require: issueI663DX
284  */
HWTEST_F(StatsServiceAlarmTest, StatsServiceAlarmTest_007, TestSize.Level0)285 HWTEST_F (StatsServiceAlarmTest, StatsServiceAlarmTest_007, TestSize.Level0)
286 {
287     STATS_HILOGI(LABEL_TEST, "StatsServiceAlarmTest_007 start");
288     ASSERT_NE(g_statsServiceProxy, nullptr);
289     auto statsService = BatteryStatsService::GetInstance();
290     g_statsServiceProxy->Reset();
291 
292     int32_t uid = 10003;
293     int16_t count = 10;
294 
295     for (int16_t i = 0; i < count; i++) {
296         StatsWriteHiSysEvent(statsService,
297             HiSysEvent::Domain::TIME, StatsHiSysEvent::MISC_TIME_STATISTIC_REPORT, HiSysEvent::EventType::STATISTIC);
298     }
299 
300     double expectedPower = StatsUtils::DEFAULT_VALUE;
301     double actualPower = g_statsServiceProxy->GetAppStatsMah(uid);
302     GTEST_LOG_(INFO) << __func__ << ": expected consumption = " << expectedPower << " mAh";
303     GTEST_LOG_(INFO) << __func__ << ": actual consumption = " << actualPower << " mAh";
304     EXPECT_EQ(expectedPower, actualPower);
305     STATS_HILOGI(LABEL_TEST, "StatsServiceAlarmTest_007 end");
306 }
307 }