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_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 #include "stats_hisysevent.h"
25
26 using namespace testing::ext;
27 using namespace OHOS::HiviewDFX;
28 using namespace OHOS::PowerMgr;
29 using namespace OHOS;
30 using namespace std;
31
32 static std::vector<std::string> dumpArgs;
33
SetUpTestCase()34 void StatsDumpTest::SetUpTestCase()
35 {
36 dumpArgs.push_back("-batterystats");
37 system("hidumper -s 3302 -a -u");
38 }
39
TearDownTestCase()40 void StatsDumpTest::TearDownTestCase()
41 {
42 system("hidumper -s 3302 -a -r");
43 }
44
SetUp()45 void StatsDumpTest::SetUp()
46 {
47 auto& statsClient = BatteryStatsClient::GetInstance();
48 statsClient.SetOnBattery(true);
49 }
50
TearDown()51 void StatsDumpTest::TearDown()
52 {
53 auto& statsClient = BatteryStatsClient::GetInstance();
54 statsClient.SetOnBattery(false);
55 }
56
57 namespace {
58 /**
59 * @tc.name: StatsDumpTest_001
60 * @tc.desc: test Dump function(BATTERY_CHANGED)
61 * @tc.type: FUNC
62 */
63 HWTEST_F (StatsDumpTest, StatsDumpTest_001, TestSize.Level0)
64 {
65 auto& statsClient = BatteryStatsClient::GetInstance();
66 statsClient.Reset();
67
68 int32_t batteryLevel = 60;
69 int32_t batteryChargerType = 2;
70
71 HiSysEventWrite(HiSysEvent::Domain::BATTERY, StatsHiSysEvent::BATTERY_CHANGED,
72 HiSysEvent::EventType::STATISTIC, "LEVEL", batteryLevel, "CHARGER", batteryChargerType);
73
74 std::string expectedDebugInfo;
75 expectedDebugInfo.append("Battery level = ")
76 .append(ToString(batteryLevel))
77 .append(", Charger type = ")
78 .append(ToString(batteryChargerType));
79
80 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
81 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
82 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
83 auto index = actualDebugInfo.find(expectedDebugInfo);
84 EXPECT_TRUE(index != string::npos);
85 }
86
87 /**
88 * @tc.name: StatsDumpTest_002
89 * @tc.desc: test Dump function(POWER_RUNNINGLOCK)
90 * @tc.type: FUNC
91 */
92 HWTEST_F (StatsDumpTest, StatsDumpTest_002, TestSize.Level0)
93 {
94 auto& statsClient = BatteryStatsClient::GetInstance();
95 statsClient.Reset();
96
97 int32_t uid = 10001;
98 int32_t pid = 3456;
99 int32_t stateLock = 1;
100 int32_t stateUnlock = 0;
101 int32_t type = static_cast<int32_t>(RunningLockType::RUNNINGLOCK_SCREEN);
102 std::string name = " StatsDumpTest_002";
103
104 HiSysEventWrite(HiSysEvent::Domain::POWER, StatsHiSysEvent::POWER_RUNNINGLOCK,
105 HiSysEvent::EventType::STATISTIC, "PID", pid, "UID", uid, "STATE", stateLock, "TYPE", type, "NAME", name);
106 usleep(US_PER_MS);
107 HiSysEventWrite(HiSysEvent::Domain::POWER, StatsHiSysEvent::POWER_RUNNINGLOCK,
108 HiSysEvent::EventType::STATISTIC, "PID", pid, "UID", uid, "STATE", stateUnlock, "TYPE", type, "NAME", name);
109
110 std::string expectedDebugInfo;
111 expectedDebugInfo.append("UID = ")
112 .append(ToString(uid))
113 .append(", PID = ")
114 .append(ToString(pid))
115 .append(", wakelock type = ")
116 .append(ToString(type))
117 .append(", wakelock name = ")
118 .append(name);
119
120 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
121 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
122 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
123 auto index = actualDebugInfo.find(expectedDebugInfo);
124 EXPECT_TRUE(index != string::npos);
125 }
126
127 /**
128 * @tc.name: StatsDumpTest_003
129 * @tc.desc: test Dump function(BACKLIGHT_DISCOUNT)
130 * @tc.type: FUNC
131 */
132 HWTEST_F (StatsDumpTest, StatsDumpTest_003, TestSize.Level0)
133 {
134 auto& statsClient = BatteryStatsClient::GetInstance();
135 statsClient.Reset();
136
137 int32_t ratio = 100;
138
139 HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::BACKLIGHT_DISCOUNT,
140 HiSysEvent::EventType::STATISTIC, "RATIO", ratio);
141 std::string expectedDebugInfo;
142 expectedDebugInfo.append("Additional debug info: ")
143 .append("Event name = ")
144 .append(StatsHiSysEvent::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 HiSysEventWrite(HiSysEvent::Domain::STATS, StatsHiSysEvent::POWER_WORKSCHEDULER,
172 HiSysEvent::EventType::STATISTIC, "PID", pid, "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 HiSysEventWrite(HiSysEvent::Domain::THERMAL, StatsHiSysEvent::POWER_TEMPERATURE,
207 HiSysEvent::EventType::STATISTIC, "NAME", 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 HiSysEventWrite(HiSysEvent::Domain::DISTRIBUTED_SCHEDULE,
241 StatsHiSysEvent::START_REMOTE_ABILITY, HiSysEvent::EventType::BEHAVIOR,
242 "CALLING_TYPE", callType, "CALLING_UID", callUid, "CALLING_PID", callPid, "TARGET_BUNDLE", targetBundle,
243 "TARGET_ABILITY", targetAbility, "CALLING_APP_UID", callAppUid, "RESULT", result);
244
245 std::string expectedDebugInfo;
246 expectedDebugInfo.append("Additional debug info: ")
247 .append("Event name = START_REMOTE_ABILITY")
248 .append(" Calling Type = ")
249 .append(callType);
250
251 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
252 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
253 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
254 auto index = actualDebugInfo.find(expectedDebugInfo);
255 EXPECT_TRUE(index != string::npos);
256 }
257
258 /**
259 * @tc.name: StatsDumpTest_007
260 * @tc.desc: test Dump function(THERMAL_ACTION_TRIGGERED)
261 * @tc.type: FUNC
262 */
263 HWTEST_F (StatsDumpTest, StatsDumpTest_007, TestSize.Level0)
264 {
265 auto& statsClient = BatteryStatsClient::GetInstance();
266 statsClient.Reset();
267
268 std::string actionName = "thermallevel";
269 int32_t value = 3;
270 float ratio = 0.60;
271 int32_t beginPos = 0;
272 int32_t ratioLen = 4;
273
274 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::THERMAL, StatsHiSysEvent::THERMAL_ACTION_TRIGGERED,
275 HiviewDFX::HiSysEvent::EventType::STATISTIC, "ACTION", actionName, "VALUE", value, "RATIO", ratio);
276
277 std::string expectedDebugInfo;
278 expectedDebugInfo.append("Additional debug info: ")
279 .append("Event name = ACTION_TRIGGERED")
280 .append(" Action name = ")
281 .append(actionName)
282 .append(" Value = ")
283 .append(ToString(value))
284 .append(" Ratio = ")
285 .append(std::to_string(ratio).substr(beginPos, ratioLen));
286
287 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
288 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
289 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
290 auto index = actualDebugInfo.find(expectedDebugInfo);
291 EXPECT_TRUE(index != string::npos);
292 }
293
294 /**
295 * @tc.name: StatsDumpTest_008
296 * @tc.desc: test Dump function(BACKLIGHT_DISCOUNT)
297 * @tc.type: FUNC
298 */
299 HWTEST_F (StatsDumpTest, StatsDumpTest_008, TestSize.Level0)
300 {
301 auto& statsClient = BatteryStatsClient::GetInstance();
302 statsClient.Reset();
303
304 int32_t type = 100;
305 int32_t level = 101;
306
307 HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::AMBIENT_LIGHT,
308 HiSysEvent::EventType::STATISTIC, "TYPE", type, "LEVEL", level);
309
310 std::string expectedDebugInfo;
311 expectedDebugInfo.append("Additional debug info: ")
312 .append("Event name = ")
313 .append(StatsHiSysEvent::AMBIENT_LIGHT)
314 .append(" Ambient type = ")
315 .append(ToString(type))
316 .append(" Ambient brightness = ")
317 .append(ToString(level));
318
319 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
320 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
321 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
322 auto index = actualDebugInfo.find(expectedDebugInfo);
323 EXPECT_TRUE(index != string::npos);
324 }
325 }