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