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_dump_test.h"
17 #include "stats_log.h"
18
19 #include <display_power_info.h>
20 #include <hisysevent.h>
21 #include <running_lock_info.h>
22 #include <string_ex.h>
23
24 #include "battery_stats_client.h"
25 #include "stats_hisysevent.h"
26
27 using namespace testing::ext;
28 using namespace OHOS::HiviewDFX;
29 using namespace OHOS::PowerMgr;
30 using namespace OHOS;
31 using namespace std;
32
33 static std::vector<std::string> dumpArgs;
34
SetUpTestCase()35 void StatsDumpTest::SetUpTestCase()
36 {
37 dumpArgs.push_back("-batterystats");
38 system("hidumper -s 3302 -a -u");
39 }
40
TearDownTestCase()41 void StatsDumpTest::TearDownTestCase()
42 {
43 system("hidumper -s 3302 -a -r");
44 }
45
SetUp()46 void StatsDumpTest::SetUp()
47 {
48 auto& statsClient = BatteryStatsClient::GetInstance();
49 statsClient.SetOnBattery(true);
50 }
51
TearDown()52 void StatsDumpTest::TearDown()
53 {
54 auto& statsClient = BatteryStatsClient::GetInstance();
55 statsClient.SetOnBattery(false);
56 }
57
58 namespace {
59 /**
60 * @tc.name: StatsDumpTest_001
61 * @tc.desc: test Dump function(BATTERY_CHANGED)
62 * @tc.type: FUNC
63 */
HWTEST_F(StatsDumpTest, StatsDumpTest_001, TestSize.Level0)64 HWTEST_F (StatsDumpTest, StatsDumpTest_001, TestSize.Level0)
65 {
66 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_001 start");
67 auto& statsClient = BatteryStatsClient::GetInstance();
68 statsClient.Reset();
69
70 int32_t batteryLevel = 60;
71 int32_t batteryChargerType = 2;
72
73 HiSysEventWrite(HiSysEvent::Domain::BATTERY, StatsHiSysEvent::BATTERY_CHANGED,
74 HiSysEvent::EventType::STATISTIC, "LEVEL", batteryLevel, "CHARGER", batteryChargerType);
75 usleep(POWER_CONSUMPTION_DURATION_US);
76 std::string expectedDebugInfo;
77 expectedDebugInfo.append("Battery level = ")
78 .append(ToString(batteryLevel))
79 .append(", Charger type = ")
80 .append(ToString(batteryChargerType));
81
82 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
83 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
84 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
85 auto index = actualDebugInfo.find(expectedDebugInfo);
86 EXPECT_TRUE(index != string::npos);
87 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_001 end");
88 }
89
90 /**
91 * @tc.name: StatsDumpTest_002
92 * @tc.desc: test Dump function(POWER_RUNNINGLOCK)
93 * @tc.type: FUNC
94 */
HWTEST_F(StatsDumpTest, StatsDumpTest_002, TestSize.Level0)95 HWTEST_F (StatsDumpTest, StatsDumpTest_002, TestSize.Level0)
96 {
97 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_002 start");
98 auto& statsClient = BatteryStatsClient::GetInstance();
99 statsClient.Reset();
100
101 int32_t uid = 10001;
102 int32_t pid = 3456;
103 int32_t stateLock = 1;
104 int32_t stateUnlock = 0;
105 int32_t type = static_cast<int32_t>(RunningLockType::RUNNINGLOCK_SCREEN);
106 std::string name = " StatsDumpTest_002";
107
108 HiSysEventWrite(HiSysEvent::Domain::POWER, StatsHiSysEvent::POWER_RUNNINGLOCK,
109 HiSysEvent::EventType::STATISTIC, "PID", pid, "UID", uid, "STATE", stateLock, "TYPE", type, "NAME", name);
110 usleep(US_PER_MS);
111 HiSysEventWrite(HiSysEvent::Domain::POWER, StatsHiSysEvent::POWER_RUNNINGLOCK,
112 HiSysEvent::EventType::STATISTIC, "PID", pid, "UID", uid, "STATE", stateUnlock, "TYPE", type, "NAME", name);
113 usleep(POWER_CONSUMPTION_DURATION_US);
114 std::string expectedDebugInfo;
115 expectedDebugInfo.append("UID = ")
116 .append(ToString(uid))
117 .append(", PID = ")
118 .append(ToString(pid))
119 .append(", wakelock type = ")
120 .append(ToString(type))
121 .append(", wakelock name = ")
122 .append(name);
123
124 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
125 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
126 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
127 auto index = actualDebugInfo.find(expectedDebugInfo);
128 EXPECT_TRUE(index != string::npos);
129 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_002 end");
130 }
131
132 /**
133 * @tc.name: StatsDumpTest_003
134 * @tc.desc: test Dump function(BACKLIGHT_DISCOUNT)
135 * @tc.type: FUNC
136 */
HWTEST_F(StatsDumpTest, StatsDumpTest_003, TestSize.Level0)137 HWTEST_F (StatsDumpTest, StatsDumpTest_003, TestSize.Level0)
138 {
139 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_003 start");
140 auto& statsClient = BatteryStatsClient::GetInstance();
141 statsClient.Reset();
142
143 int32_t ratio = 100;
144
145 HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BACKLIGHT_DISCOUNT,
146 HiSysEvent::EventType::STATISTIC, "RATIO", ratio);
147 usleep(POWER_CONSUMPTION_DURATION_US);
148 std::string expectedDebugInfo;
149 expectedDebugInfo.append("Additional debug info: ")
150 .append("Event name = ")
151 .append(StatsHiSysEvent::BACKLIGHT_DISCOUNT)
152 .append(" Ratio = ")
153 .append(ToString(ratio));
154
155 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
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 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_003 end");
161 }
162
163 /**
164 * @tc.name: StatsDumpTest_004
165 * @tc.desc: test Dump function(POWER_WORKSCHEDULER)
166 * @tc.type: FUNC
167 */
HWTEST_F(StatsDumpTest, StatsDumpTest_004, TestSize.Level0)168 HWTEST_F (StatsDumpTest, StatsDumpTest_004, TestSize.Level0)
169 {
170 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_004 start");
171 auto& statsClient = BatteryStatsClient::GetInstance();
172 statsClient.Reset();
173
174 int32_t pid = 3457;
175 int32_t uid = 10002;
176 int32_t type = 1;
177 int32_t interval = 30000;
178 int32_t state = 5;
179
180 HiSysEventWrite(HiSysEvent::Domain::STATS, StatsHiSysEvent::POWER_WORKSCHEDULER,
181 HiSysEvent::EventType::STATISTIC, "PID", pid, "UID", uid, "TYPE", type, "INTERVAL", interval, "STATE", state);
182 usleep(POWER_CONSUMPTION_DURATION_US);
183 std::string expectedDebugInfo;
184 expectedDebugInfo.append("UID = ")
185 .append(ToString(uid))
186 .append(", PID = ")
187 .append(ToString(pid))
188 .append(", work type = ")
189 .append(ToString(type))
190 .append(", work interval = ")
191 .append(ToString(interval))
192 .append(", work state = ")
193 .append(ToString(state));
194
195 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
196 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
197 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
198 auto index = actualDebugInfo.find(expectedDebugInfo);
199 EXPECT_TRUE(index != string::npos);
200 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_004 end");
201 }
202
203 /**
204 * @tc.name: StatsDumpTest_005
205 * @tc.desc: test Dump function(POWER_TEMPERATURE)
206 * @tc.type: FUNC
207 */
HWTEST_F(StatsDumpTest, StatsDumpTest_005, TestSize.Level0)208 HWTEST_F (StatsDumpTest, StatsDumpTest_005, TestSize.Level0)
209 {
210 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_005 start");
211 auto& statsClient = BatteryStatsClient::GetInstance();
212 statsClient.Reset();
213
214 std::string partName = "Battery";
215 int32_t temperature = 40;
216
217 HiSysEventWrite(HiSysEvent::Domain::THERMAL, StatsHiSysEvent::POWER_TEMPERATURE,
218 HiSysEvent::EventType::STATISTIC, "NAME", partName, "TEMPERATURE", temperature);
219 usleep(POWER_CONSUMPTION_DURATION_US);
220 std::string expectedDebugInfo;
221 expectedDebugInfo.append("Additional debug info: ")
222 .append("Event name = POWER_TEMPERATURE")
223 .append(" Name = ")
224 .append(partName);
225
226 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
227 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
228 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
229 auto index = actualDebugInfo.find(expectedDebugInfo);
230 EXPECT_TRUE(index != string::npos);
231 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_005 end");
232 }
233
234 /**
235 * @tc.name: StatsDumpTest_006
236 * @tc.desc: test Dump function(START_REMOTE_ABILITY)
237 * @tc.type: FUNC
238 */
HWTEST_F(StatsDumpTest, StatsDumpTest_006, TestSize.Level0)239 HWTEST_F (StatsDumpTest, StatsDumpTest_006, TestSize.Level0)
240 {
241 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_006 start");
242 auto& statsClient = BatteryStatsClient::GetInstance();
243 statsClient.Reset();
244
245 std::string callType = "DUBAI_TAG_DIST_SCHED_TO_REMOTE";
246 int32_t callUid = 10003;
247 int32_t callPid = 3458;
248 std::string targetBundle = "TargetBundleName";
249 std::string targetAbility = "TargetAbilityName";
250 int32_t callAppUid = 9568;
251 int32_t result = 1;
252
253 HiSysEventWrite(HiSysEvent::Domain::DISTRIBUTED_SCHEDULE,
254 StatsHiSysEvent::START_REMOTE_ABILITY, HiSysEvent::EventType::BEHAVIOR,
255 "CALLING_TYPE", callType, "CALLING_UID", callUid, "CALLING_PID", callPid, "TARGET_BUNDLE", targetBundle,
256 "TARGET_ABILITY", targetAbility, "CALLING_APP_UID", callAppUid, "RESULT", result);
257 usleep(POWER_CONSUMPTION_DURATION_US);
258 std::string expectedDebugInfo;
259 expectedDebugInfo.append("Additional debug info: ")
260 .append("Event name = START_REMOTE_ABILITY")
261 .append(" Calling Type = ")
262 .append(callType);
263
264 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
265 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
266 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
267 auto index = actualDebugInfo.find(expectedDebugInfo);
268 EXPECT_TRUE(index != string::npos);
269 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_006 end");
270 }
271
272 /**
273 * @tc.name: StatsDumpTest_007
274 * @tc.desc: test Dump function(THERMAL_ACTION_TRIGGERED)
275 * @tc.type: FUNC
276 */
HWTEST_F(StatsDumpTest, StatsDumpTest_007, TestSize.Level0)277 HWTEST_F (StatsDumpTest, StatsDumpTest_007, TestSize.Level0)
278 {
279 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_007 start");
280 auto& statsClient = BatteryStatsClient::GetInstance();
281 statsClient.Reset();
282
283 std::string actionName = "thermallevel";
284 int32_t value = 3;
285 float ratio = 0.60;
286 int32_t beginPos = 0;
287 int32_t ratioLen = 4;
288
289 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::THERMAL, StatsHiSysEvent::THERMAL_ACTION_TRIGGERED,
290 HiviewDFX::HiSysEvent::EventType::STATISTIC, "ACTION", actionName, "VALUE", value, "RATIO", ratio);
291 usleep(POWER_CONSUMPTION_DURATION_US);
292 std::string expectedDebugInfo;
293 expectedDebugInfo.append("Additional debug info: ")
294 .append("Event name = ACTION_TRIGGERED")
295 .append(" Action name = ")
296 .append(actionName)
297 .append(" Value = ")
298 .append(ToString(value))
299 .append(" Ratio = ")
300 .append(std::to_string(ratio).substr(beginPos, ratioLen));
301
302 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
303 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
304 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
305 auto index = actualDebugInfo.find(expectedDebugInfo);
306 EXPECT_TRUE(index != string::npos);
307 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_007 end");
308 }
309
310 /**
311 * @tc.name: StatsDumpTest_008
312 * @tc.desc: test Dump function(BACKLIGHT_DISCOUNT)
313 * @tc.type: FUNC
314 */
HWTEST_F(StatsDumpTest, StatsDumpTest_008, TestSize.Level0)315 HWTEST_F (StatsDumpTest, StatsDumpTest_008, TestSize.Level0)
316 {
317 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_008 start");
318 auto& statsClient = BatteryStatsClient::GetInstance();
319 statsClient.Reset();
320
321 int32_t type = 100;
322 int32_t level = 101;
323
324 HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::AMBIENT_LIGHT,
325 HiSysEvent::EventType::STATISTIC, "TYPE", type, "LEVEL", level);
326 usleep(POWER_CONSUMPTION_DURATION_US);
327 std::string expectedDebugInfo;
328 expectedDebugInfo.append("Additional debug info: ")
329 .append("Event name = ")
330 .append(StatsHiSysEvent::AMBIENT_LIGHT)
331 .append(" Ambient type = ")
332 .append(ToString(type))
333 .append(" Ambient brightness = ")
334 .append(ToString(level));
335
336 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
337 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
338 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
339 auto index = actualDebugInfo.find(expectedDebugInfo);
340 EXPECT_TRUE(index != string::npos);
341 STATS_HILOGI(LABEL_TEST, "StatsDumpTest_008 end");
342 }
343 }