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 usleep(POWER_CONSUMPTION_DURATION_US);
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 usleep(POWER_CONSUMPTION_DURATION_US);
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 usleep(POWER_CONSUMPTION_DURATION_US);
142 std::string expectedDebugInfo;
143 expectedDebugInfo.append("Additional debug info: ")
144 .append("Event name = ")
145 .append(StatsHiSysEvent::BACKLIGHT_DISCOUNT)
146 .append(" Ratio = ")
147 .append(ToString(ratio));
148
149 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
150 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
151 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
152 auto index = actualDebugInfo.find(expectedDebugInfo);
153 EXPECT_TRUE(index != string::npos);
154 }
155
156 /**
157 * @tc.name: StatsDumpTest_004
158 * @tc.desc: test Dump function(POWER_WORKSCHEDULER)
159 * @tc.type: FUNC
160 */
161 HWTEST_F (StatsDumpTest, StatsDumpTest_004, TestSize.Level0)
162 {
163 auto& statsClient = BatteryStatsClient::GetInstance();
164 statsClient.Reset();
165
166 int32_t pid = 3457;
167 int32_t uid = 10002;
168 int32_t type = 1;
169 int32_t interval = 30000;
170 int32_t state = 5;
171
172 HiSysEventWrite(HiSysEvent::Domain::STATS, StatsHiSysEvent::POWER_WORKSCHEDULER,
173 HiSysEvent::EventType::STATISTIC, "PID", pid, "UID", uid, "TYPE", type, "INTERVAL", interval, "STATE", state);
174 usleep(POWER_CONSUMPTION_DURATION_US);
175 std::string expectedDebugInfo;
176 expectedDebugInfo.append("UID = ")
177 .append(ToString(uid))
178 .append(", PID = ")
179 .append(ToString(pid))
180 .append(", work type = ")
181 .append(ToString(type))
182 .append(", work interval = ")
183 .append(ToString(interval))
184 .append(", work state = ")
185 .append(ToString(state));
186
187 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
188 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
189 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
190 auto index = actualDebugInfo.find(expectedDebugInfo);
191 EXPECT_TRUE(index != string::npos);
192 }
193
194 /**
195 * @tc.name: StatsDumpTest_005
196 * @tc.desc: test Dump function(POWER_TEMPERATURE)
197 * @tc.type: FUNC
198 */
199 HWTEST_F (StatsDumpTest, StatsDumpTest_005, TestSize.Level0)
200 {
201 auto& statsClient = BatteryStatsClient::GetInstance();
202 statsClient.Reset();
203
204 std::string partName = "Battery";
205 int32_t temperature = 40;
206
207 HiSysEventWrite(HiSysEvent::Domain::THERMAL, StatsHiSysEvent::POWER_TEMPERATURE,
208 HiSysEvent::EventType::STATISTIC, "NAME", partName, "TEMPERATURE", temperature);
209 usleep(POWER_CONSUMPTION_DURATION_US);
210 std::string expectedDebugInfo;
211 expectedDebugInfo.append("Additional debug info: ")
212 .append("Event name = POWER_TEMPERATURE")
213 .append(" Name = ")
214 .append(partName);
215
216 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
217 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
218 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
219 auto index = actualDebugInfo.find(expectedDebugInfo);
220 EXPECT_TRUE(index != string::npos);
221 }
222
223 /**
224 * @tc.name: StatsDumpTest_006
225 * @tc.desc: test Dump function(START_REMOTE_ABILITY)
226 * @tc.type: FUNC
227 */
228 HWTEST_F (StatsDumpTest, StatsDumpTest_006, TestSize.Level0)
229 {
230 auto& statsClient = BatteryStatsClient::GetInstance();
231 statsClient.Reset();
232
233 std::string callType = "DUBAI_TAG_DIST_SCHED_TO_REMOTE";
234 int32_t callUid = 10003;
235 int32_t callPid = 3458;
236 std::string targetBundle = "TargetBundleName";
237 std::string targetAbility = "TargetAbilityName";
238 int32_t callAppUid = 9568;
239 int32_t result = 1;
240
241 HiSysEventWrite(HiSysEvent::Domain::DISTRIBUTED_SCHEDULE,
242 StatsHiSysEvent::START_REMOTE_ABILITY, HiSysEvent::EventType::BEHAVIOR,
243 "CALLING_TYPE", callType, "CALLING_UID", callUid, "CALLING_PID", callPid, "TARGET_BUNDLE", targetBundle,
244 "TARGET_ABILITY", targetAbility, "CALLING_APP_UID", callAppUid, "RESULT", result);
245 usleep(POWER_CONSUMPTION_DURATION_US);
246 std::string expectedDebugInfo;
247 expectedDebugInfo.append("Additional debug info: ")
248 .append("Event name = START_REMOTE_ABILITY")
249 .append(" Calling Type = ")
250 .append(callType);
251
252 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
253 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
254 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
255 auto index = actualDebugInfo.find(expectedDebugInfo);
256 EXPECT_TRUE(index != string::npos);
257 }
258
259 /**
260 * @tc.name: StatsDumpTest_007
261 * @tc.desc: test Dump function(THERMAL_ACTION_TRIGGERED)
262 * @tc.type: FUNC
263 */
264 HWTEST_F (StatsDumpTest, StatsDumpTest_007, TestSize.Level0)
265 {
266 auto& statsClient = BatteryStatsClient::GetInstance();
267 statsClient.Reset();
268
269 std::string actionName = "thermallevel";
270 int32_t value = 3;
271 float ratio = 0.60;
272 int32_t beginPos = 0;
273 int32_t ratioLen = 4;
274
275 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::THERMAL, StatsHiSysEvent::THERMAL_ACTION_TRIGGERED,
276 HiviewDFX::HiSysEvent::EventType::STATISTIC, "ACTION", actionName, "VALUE", value, "RATIO", ratio);
277 usleep(POWER_CONSUMPTION_DURATION_US);
278 std::string expectedDebugInfo;
279 expectedDebugInfo.append("Additional debug info: ")
280 .append("Event name = ACTION_TRIGGERED")
281 .append(" Action name = ")
282 .append(actionName)
283 .append(" Value = ")
284 .append(ToString(value))
285 .append(" Ratio = ")
286 .append(std::to_string(ratio).substr(beginPos, ratioLen));
287
288 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
289 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
290 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
291 auto index = actualDebugInfo.find(expectedDebugInfo);
292 EXPECT_TRUE(index != string::npos);
293 }
294
295 /**
296 * @tc.name: StatsDumpTest_008
297 * @tc.desc: test Dump function(BACKLIGHT_DISCOUNT)
298 * @tc.type: FUNC
299 */
300 HWTEST_F (StatsDumpTest, StatsDumpTest_008, TestSize.Level0)
301 {
302 auto& statsClient = BatteryStatsClient::GetInstance();
303 statsClient.Reset();
304
305 int32_t type = 100;
306 int32_t level = 101;
307
308 HiSysEventWrite(HiSysEvent::Domain::DISPLAY, StatsHiSysEvent::AMBIENT_LIGHT,
309 HiSysEvent::EventType::STATISTIC, "TYPE", type, "LEVEL", level);
310 usleep(POWER_CONSUMPTION_DURATION_US);
311 std::string expectedDebugInfo;
312 expectedDebugInfo.append("Additional debug info: ")
313 .append("Event name = ")
314 .append(StatsHiSysEvent::AMBIENT_LIGHT)
315 .append(" Ambient type = ")
316 .append(ToString(type))
317 .append(" Ambient brightness = ")
318 .append(ToString(level));
319
320 std::string actualDebugInfo = statsClient.Dump(dumpArgs);
321 GTEST_LOG_(INFO) << __func__ << ": expected debug info: " << expectedDebugInfo;
322 GTEST_LOG_(INFO) << __func__ << ": actual debug info: " << actualDebugInfo;
323 auto index = actualDebugInfo.find(expectedDebugInfo);
324 EXPECT_TRUE(index != string::npos);
325 }
326 }