1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "stats_service_dump_test.h"
17 
18 #include <display_power_info.h>
19 #include <hisysevent.h>
20 #include <running_lock_info.h>
21 #include <string_ex.h>
22 
23 #include "battery_stats_listener.h"
24 #include "battery_stats_service.h"
25 #include "hisysevent_operation.h"
26 #include "stats_hisysevent.h"
27 #include "stats_service_test_proxy.h"
28 #include "stats_service_write_event.h"
29 
30 using namespace testing::ext;
31 using namespace OHOS::HiviewDFX;
32 using namespace OHOS::PowerMgr;
33 using namespace OHOS;
34 using namespace std;
35 
36 namespace {
37 static sptr<BatteryStatsService> g_statsService = nullptr;
38 static std::shared_ptr<StatsServiceTestProxy> g_statsServiceProxy = nullptr;
39 static std::vector<std::string> dumpArgs;
40 } // namespace
41 
SetUpTestCase()42 void StatsServiceDumpTest::SetUpTestCase()
43 {
44     ParserAveragePowerFile();
45     g_statsService = BatteryStatsService::GetInstance();
46     g_statsService->OnStart();
47     g_statsService->isBootCompleted_ = true;
48 
49     if (g_statsService->listenerPtr_ == nullptr) {
50         g_statsService->listenerPtr_ = std::make_shared<BatteryStatsListener>();
51     }
52 
53     if (g_statsServiceProxy == nullptr) {
54         g_statsServiceProxy = std::make_shared<StatsServiceTestProxy>(g_statsService);
55     }
56     dumpArgs.push_back("-batterystats");
57 }
58 
TearDownTestCase()59 void StatsServiceDumpTest::TearDownTestCase()
60 {
61     g_statsService->listenerPtr_ = nullptr;
62     g_statsService->OnStop();
63 }
64 
SetUp()65 void StatsServiceDumpTest::SetUp()
66 {
67     auto statsService = BatteryStatsService::GetInstance();
68     statsService->SetOnBattery(true);
69 }
70 
TearDown()71 void StatsServiceDumpTest::TearDown()
72 {
73     auto statsService = BatteryStatsService::GetInstance();
74     statsService->SetOnBattery(false);
75 }
76 
77 namespace {
78 /**
79  * @tc.name: StatsServiceDumpTest_001
80  * @tc.desc: test Dump function(BATTERY_CHANGED)
81  * @tc.type: FUNC
82  * @tc.require: issueI663DX
83  */
84 HWTEST_F (StatsServiceDumpTest, StatsServiceDumpTest_001, TestSize.Level0)
85 {
86     ASSERT_NE(g_statsServiceProxy, nullptr);
87     auto statsService = BatteryStatsService::GetInstance();
88     g_statsServiceProxy->Reset();
89 
90     int32_t batteryLevel = 60;
91     int32_t batteryChargerType = 2;
92 
93     StatsWriteHiSysEvent(statsService,
94         HiSysEvent::Domain::BATTERY, StatsHiSysEvent::BATTERY_CHANGED, HiSysEvent::EventType::STATISTIC, "LEVEL",
95         batteryLevel, "CHARGER", batteryChargerType);
96 
97     std::string expectedDebugInfo;
98     expectedDebugInfo.append("Battery level = ")
99         .append(ToString(batteryLevel))
100         .append(", Charger type = ")
101         .append(ToString(batteryChargerType));
102 
103     std::string actualDebugInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
104     GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
105     GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
106     auto index = actualDebugInfo.find(expectedDebugInfo);
107     EXPECT_TRUE(index != string::npos);
108 
109     StatsWriteHiSysEvent(statsService,
110         HiSysEvent::Domain::BATTERY, StatsHiSysEvent::BATTERY_CHANGED, HiSysEvent::EventType::STATISTIC);
111     std::string expectedMissInfo;
112     expectedMissInfo.append("Battery level = ").append(ToString(-1));
113     std::string actualMissInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
114     auto missIndex = actualMissInfo.find(expectedMissInfo);
115     EXPECT_TRUE(missIndex != string::npos);
116 }
117 
118 /**
119  * @tc.name: StatsServiceDumpTest_002
120  * @tc.desc: test Dump function(POWER_RUNNINGLOCK)
121  * @tc.type: FUNC
122  * @tc.require: issueI663DX
123  */
124 HWTEST_F (StatsServiceDumpTest, StatsServiceDumpTest_002, TestSize.Level0)
125 {
126     ASSERT_NE(g_statsServiceProxy, nullptr);
127     auto statsService = BatteryStatsService::GetInstance();
128     g_statsServiceProxy->Reset();
129 
130     int32_t uid = 10001;
131     int32_t pid = 3456;
132     int32_t stateLock = 1;
133     int32_t stateUnlock = 0;
134     int32_t type = static_cast<int32_t>(RunningLockType::RUNNINGLOCK_SCREEN);
135     std::string name = " StatsServiceDumpTest_002";
136 
137     StatsWriteHiSysEvent(statsService,
138         HiSysEvent::Domain::POWER, StatsHiSysEvent::POWER_RUNNINGLOCK, HiSysEvent::EventType::STATISTIC, "PID", pid,
139         "UID", uid, "STATE", stateLock, "TYPE", type, "NAME", name);
140     usleep(US_PER_MS);
141     StatsWriteHiSysEvent(statsService,
142         HiSysEvent::Domain::POWER, StatsHiSysEvent::POWER_RUNNINGLOCK, HiSysEvent::EventType::STATISTIC, "PID", pid,
143         "UID", uid, "STATE", stateUnlock, "TYPE", type, "NAME", name);
144 
145     std::string expectedDebugInfo;
146     expectedDebugInfo.append("UID = ")
147         .append(ToString(uid))
148         .append(", PID = ")
149         .append(ToString(pid))
150         .append(", wakelock type = ")
151         .append(ToString(type))
152         .append(", wakelock name = ")
153         .append(name);
154 
155     std::string actualDebugInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
156     GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
157     GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
158     auto index = actualDebugInfo.find(expectedDebugInfo);
159     EXPECT_TRUE(index != string::npos);
160 
161     StatsWriteHiSysEvent(statsService,
162         HiSysEvent::Domain::POWER, StatsHiSysEvent::POWER_RUNNINGLOCK, HiSysEvent::EventType::STATISTIC);
163     std::string expectedMissInfo;
164     expectedMissInfo.append(", wakelock name = ").append("");
165     std::string actualMissInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
166     auto missIndex = actualMissInfo.find(expectedMissInfo);
167     EXPECT_TRUE(missIndex != string::npos);
168 }
169 
170 /**
171  * @tc.name: StatsServiceDumpTest_003
172  * @tc.desc: test Dump function(BACKLIGHT_DISCOUNT)
173  * @tc.type: FUNC
174  * @tc.require: issueI663DX
175  */
176 HWTEST_F (StatsServiceDumpTest, StatsServiceDumpTest_003, TestSize.Level0)
177 {
178     ASSERT_NE(g_statsServiceProxy, nullptr);
179     auto statsService = BatteryStatsService::GetInstance();
180     g_statsServiceProxy->Reset();
181 
182     int32_t ratio = 100;
183 
184     StatsWriteHiSysEvent(statsService,
185         HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BACKLIGHT_DISCOUNT,
186         HiSysEvent::EventType::STATISTIC, "RATIO", ratio);
187     std::string expectedDebugInfo;
188     expectedDebugInfo.append("Additional debug info: ")
189         .append("Event name = ")
190         .append(StatsHiSysEvent::BACKLIGHT_DISCOUNT)
191         .append(" Ratio = ")
192         .append(ToString(ratio));
193 
194     std::string actualDebugInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
195     GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
196     GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
197     auto index = actualDebugInfo.find(expectedDebugInfo);
198     EXPECT_TRUE(index != string::npos);
199 
200     StatsWriteHiSysEvent(statsService,
201         HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BACKLIGHT_DISCOUNT, HiSysEvent::EventType::STATISTIC);
202     std::string expectedMissInfo;
203     expectedMissInfo.append(" Ratio = ").append("");
204     std::string actualMissInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
205     auto missIndex = actualMissInfo.find(expectedMissInfo);
206     EXPECT_TRUE(missIndex != string::npos);
207 }
208 
209 /**
210  * @tc.name: StatsServiceDumpTest_004
211  * @tc.desc: test Dump function(POWER_WORKSCHEDULER)
212  * @tc.type: FUNC
213  * @tc.require: issueI663DX
214  */
215 HWTEST_F (StatsServiceDumpTest, StatsServiceDumpTest_004, TestSize.Level0)
216 {
217     ASSERT_NE(g_statsServiceProxy, nullptr);
218     auto statsService = BatteryStatsService::GetInstance();
219     g_statsServiceProxy->Reset();
220 
221     int32_t pid = 3457;
222     int32_t uid = 10002;
223     int32_t type = 1;
224     int32_t interval = 30000;
225     int32_t state = 5;
226 
227     StatsWriteHiSysEvent(statsService,
228         HiSysEvent::Domain::STATS, StatsHiSysEvent::POWER_WORKSCHEDULER,
229         HiSysEvent::EventType::STATISTIC, "PID", pid,
230         "UID", uid, "TYPE", type, "INTERVAL", interval, "STATE", state);
231 
232     std::string expectedDebugInfo;
233     expectedDebugInfo.append("UID = ")
234         .append(ToString(uid))
235         .append(", PID = ")
236         .append(ToString(pid))
237         .append(", work type = ")
238         .append(ToString(type))
239         .append(", work interval = ")
240         .append(ToString(interval))
241         .append(", work state = ")
242         .append(ToString(state));
243 
244     std::string actualDebugInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
245     GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
246     GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
247     auto index = actualDebugInfo.find(expectedDebugInfo);
248     EXPECT_TRUE(index != string::npos);
249 
250     StatsWriteHiSysEvent(statsService,
251         HiSysEvent::Domain::STATS, StatsHiSysEvent::POWER_WORKSCHEDULER, HiSysEvent::EventType::STATISTIC);
252     std::string expectedMissInfo;
253     expectedMissInfo.append(", work interval = ").append("");
254     std::string actualMissInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
255     auto missIndex = actualMissInfo.find(expectedMissInfo);
256     EXPECT_TRUE(missIndex != string::npos);
257 }
258 
259 /**
260  * @tc.name: StatsServiceDumpTest_005
261  * @tc.desc: test Dump function(POWER_TEMPERATURE)
262  * @tc.type: FUNC
263  * @tc.require: issueI663DX
264  */
265 HWTEST_F (StatsServiceDumpTest, StatsServiceDumpTest_005, TestSize.Level0)
266 {
267     ASSERT_NE(g_statsServiceProxy, nullptr);
268     auto statsService = BatteryStatsService::GetInstance();
269     g_statsServiceProxy->Reset();
270 
271     std::string partName = "Battery";
272     int32_t temperature = 40;
273 
274     StatsWriteHiSysEvent(statsService,
275         HiSysEvent::Domain::THERMAL, StatsHiSysEvent::POWER_TEMPERATURE, HiSysEvent::EventType::STATISTIC, "NAME",
276         partName, "TEMPERATURE", temperature);
277 
278     std::string expectedDebugInfo;
279     expectedDebugInfo.append("Additional debug info: ")
280         .append("Event name = POWER_TEMPERATURE")
281         .append(" Name = ")
282         .append(partName);
283 
284     std::string actualDebugInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
285     GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
286     GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
287     auto index = actualDebugInfo.find(expectedDebugInfo);
288     EXPECT_TRUE(index != string::npos);
289 
290     StatsWriteHiSysEvent(statsService,
291         HiSysEvent::Domain::THERMAL, StatsHiSysEvent::POWER_TEMPERATURE, HiSysEvent::EventType::STATISTIC);
292     std::string expectedMissInfo;
293     expectedMissInfo.append("Additional debug info: ")
294         .append("Event name = POWER_TEMPERATURE")
295         .append(" Name = ")
296         .append("");
297     std::string actualMissInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
298     auto missIndex = actualMissInfo.find(expectedMissInfo);
299     EXPECT_TRUE(missIndex != string::npos);
300 }
301 
302 /**
303  * @tc.name: StatsServiceDumpTest_006
304  * @tc.desc: test Dump function(START_REMOTE_ABILITY)
305  * @tc.type: FUNC
306  * @tc.require: issueI663DX
307  */
308 HWTEST_F (StatsServiceDumpTest, StatsServiceDumpTest_006, TestSize.Level0)
309 {
310     ASSERT_NE(g_statsServiceProxy, nullptr);
311     auto statsService = BatteryStatsService::GetInstance();
312     g_statsServiceProxy->Reset();
313 
314     std::string callType = "DUBAI_TAG_DIST_SCHED_TO_REMOTE";
315     int32_t callUid = 10003;
316     int32_t callPid = 3458;
317     std::string targetBundle = "TargetBundleName";
318     std::string targetAbility = "TargetAbilityName";
319     int32_t callAppUid = 9568;
320     int32_t result = 1;
321 
322     StatsWriteHiSysEvent(statsService,
323         HiSysEvent::Domain::DISTRIBUTED_SCHEDULE,
324         StatsHiSysEvent::START_REMOTE_ABILITY, HiSysEvent::EventType::BEHAVIOR,
325         "CALLING_TYPE", callType, "CALLING_UID", callUid, "CALLING_PID", callPid, "TARGET_BUNDLE", targetBundle,
326         "TARGET_ABILITY", targetAbility, "CALLING_APP_UID", callAppUid, "RESULT", result);
327 
328     std::string expectedDebugInfo;
329     expectedDebugInfo.append("Additional debug info: ")
330         .append("Event name = START_REMOTE_ABILITY")
331         .append(" Calling Type = ")
332         .append(callType);
333 
334     std::string actualDebugInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
335     GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
336     GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
337     auto index = actualDebugInfo.find(expectedDebugInfo);
338     EXPECT_TRUE(index != string::npos);
339 
340     StatsWriteHiSysEvent(statsService,
341         HiSysEvent::Domain::DISTRIBUTED_SCHEDULE,
342         StatsHiSysEvent::START_REMOTE_ABILITY, HiSysEvent::EventType::BEHAVIOR);
343     std::string expectedMissInfo;
344     expectedMissInfo.append("Additional debug info: ")
345         .append("Event name = START_REMOTE_ABILITY")
346         .append(" Calling Type = ")
347         .append("");
348     std::string actualMissInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
349     auto missIndex = actualMissInfo.find(expectedMissInfo);
350     EXPECT_TRUE(missIndex != string::npos);
351 }
352 
353 /**
354  * @tc.name: StatsServiceDumpTest_007
355  * @tc.desc: test Dump function(THERMAL_ACTION_TRIGGERED)
356  * @tc.type: FUNC
357  * @tc.require: issueI663DX
358  */
359 HWTEST_F (StatsServiceDumpTest, StatsServiceDumpTest_007, TestSize.Level0)
360 {
361     ASSERT_NE(g_statsServiceProxy, nullptr);
362     auto statsService = BatteryStatsService::GetInstance();
363     g_statsServiceProxy->Reset();
364 
365     std::string actionName = "thermallevel";
366     int32_t value = 3;
367     float ratio = 0.60;
368     int32_t beginPos = 0;
369     int32_t ratioLen = 4;
370 
371     StatsWriteHiSysEvent(statsService,
372         HiviewDFX::HiSysEvent::Domain::THERMAL, StatsHiSysEvent::THERMAL_ACTION_TRIGGERED,
373         HiviewDFX::HiSysEvent::EventType::STATISTIC, "ACTION", actionName, "VALUE", value, "RATIO", ratio);
374 
375     std::string expectedDebugInfo;
376     expectedDebugInfo.append("Additional debug info: ")
377         .append("Event name = ACTION_TRIGGERED")
378         .append(" Action name = ")
379         .append(actionName)
380         .append(" Value = ")
381         .append(ToString(value))
382         .append(" Ratio = ")
383         .append(std::to_string(ratio).substr(beginPos, ratioLen));
384 
385     std::string actualDebugInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
386     GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
387     GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
388     auto index = actualDebugInfo.find(expectedDebugInfo);
389     EXPECT_TRUE(index != string::npos);
390 
391     StatsWriteHiSysEvent(statsService,
392         HiSysEvent::Domain::THERMAL, StatsHiSysEvent::THERMAL_ACTION_TRIGGERED, HiSysEvent::EventType::STATISTIC);
393     std::string expectedMissInfo;
394     expectedMissInfo.append("Additional debug info: ")
395         .append("Event name = ACTION_TRIGGERED")
396         .append(" Action name = ")
397         .append("");
398     std::string actualMissInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
399     auto missIndex = actualMissInfo.find(expectedMissInfo);
400     EXPECT_TRUE(missIndex != string::npos);
401 }
402 
403 /**
404  * @tc.name: StatsServiceDumpTest_008
405  * @tc.desc: test Dump function(AMBIENT_LIGHT)
406  * @tc.type: FUNC
407  * @tc.require: issueI663DX
408  */
409 HWTEST_F (StatsServiceDumpTest, StatsServiceDumpTest_008, TestSize.Level0)
410 {
411     ASSERT_NE(g_statsServiceProxy, nullptr);
412     auto statsService = BatteryStatsService::GetInstance();
413     g_statsServiceProxy->Reset();
414 
415     int32_t type = 100;
416     int32_t level = 101;
417 
418     StatsWriteHiSysEvent(statsService,
419         HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::AMBIENT_LIGHT,
420         HiSysEvent::EventType::STATISTIC, "TYPE", type, "LEVEL", level);
421 
422     std::string expectedDebugInfo;
423     expectedDebugInfo.append("Additional debug info: ")
424         .append("Event name = ")
425         .append(StatsHiSysEvent::AMBIENT_LIGHT)
426         .append(" Ambient type = ")
427         .append(ToString(type))
428         .append(" Ambient brightness = ")
429         .append(ToString(level));
430 
431     std::string actualDebugInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
432     GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
433     GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
434     auto index = actualDebugInfo.find(expectedDebugInfo);
435     EXPECT_TRUE(index != string::npos);
436 
437     StatsWriteHiSysEvent(statsService,
438         HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::AMBIENT_LIGHT, HiSysEvent::EventType::STATISTIC);
439     std::string expectedMissInfo;
440     expectedMissInfo.append("Additional debug info: ")
441         .append("Event name = ")
442         .append(StatsHiSysEvent::AMBIENT_LIGHT)
443         .append(" Ambient type = ")
444         .append("");
445     std::string actualMissInfo = g_statsServiceProxy->ShellDump(dumpArgs, dumpArgs.size());
446     auto missIndex = actualMissInfo.find(expectedMissInfo);
447     EXPECT_TRUE(missIndex != string::npos);
448 }
449 
450 /**
451  * @tc.name: StatsServiceDumpTest_009
452  * @tc.desc: test Dump function(args is none or help)
453  * @tc.type: FUNC
454  * @tc.require: issueI663DX
455  */
456 HWTEST_F (StatsServiceDumpTest, StatsServiceDumpTest_009, TestSize.Level0)
457 {
458     ASSERT_NE(g_statsServiceProxy, nullptr);
459     auto statsService = BatteryStatsService::GetInstance();
460     g_statsServiceProxy->Reset();
461 
462     std::vector<std::string> dumpArgsNone {};
463     std::vector<std::string> dumpArgsHelp {};
464     dumpArgsHelp.push_back("-h");
465 
466     std::string expectedDebugInfo;
467     expectedDebugInfo.append("usage: statistics <command> [<options>]\n");
468 
469     std::string noneDebugInfo = g_statsServiceProxy->ShellDump(dumpArgsNone, dumpArgsNone.size());
470     auto noneIndex = noneDebugInfo.find(expectedDebugInfo);
471     EXPECT_TRUE(noneIndex != string::npos);
472 
473     std::string helpDebugInfo = g_statsServiceProxy->ShellDump(dumpArgsHelp, dumpArgsHelp.size());
474     auto helpIndex = helpDebugInfo.find(expectedDebugInfo);
475     EXPECT_TRUE(helpIndex != string::npos);
476 }
477 }