1 /*
2 * Copyright (c) 2022-2025 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 "battery_dump_test.h"
17 #include <string>
18 #include <vector>
19
20 #ifdef GTEST
21 #define private public
22 #define protected public
23 #endif
24 #include "battery_dump.h"
25 #include "battery_service.h"
26 #include "battery_log.h"
27
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace PowerMgr {
32 namespace {
33 sptr<BatteryService> g_service;
34 }
35
SetUpTestCase()36 void BatteryDumpTest::SetUpTestCase()
37 {
38 g_service = DelayedSpSingleton<BatteryService>::GetInstance();
39 g_service->isBootCompleted_ = true;
40 }
41
42 /**
43 * @tc.name: BatteryDump001
44 * @tc.desc: Dump parameter is -i, Get battery information
45 * @tc.type: FUNC
46 * @tc.require: issueI5YZR1
47 */
48 HWTEST_F(BatteryDumpTest, BatteryDump001, TestSize.Level1)
49 {
50 BATTERY_HILOGI(LABEL_TEST, "BatteryDump001 function start!");
51 int32_t fd = 1;
52 std::vector<std::u16string> args;
53 std::u16string arg = u"-i";
54 args.push_back(arg);
55 EXPECT_EQ(g_service->Dump(fd, args), ERR_OK);
56 BATTERY_HILOGI(LABEL_TEST, "BatteryDump001 function end!");
57 }
58
59 /**
60 * @tc.name: BatteryDump002
61 * @tc.desc: Dump parameter is -u, MockUnplugged
62 * @tc.type: FUNC
63 * @tc.require: issueI5YZR1
64 */
65 HWTEST_F(BatteryDumpTest, BatteryDump002, TestSize.Level1)
66 {
67 BATTERY_HILOGI(LABEL_TEST, "BatteryDump002 function start!");
68 int32_t fd = 1;
69 std::vector<std::u16string> args;
70 std::u16string arg = u"-u";
71 args.push_back(arg);
72 EXPECT_EQ(g_service->Dump(fd, args), ERR_OK);
73 BATTERY_HILOGI(LABEL_TEST, "BatteryDump002 function end!");
74 }
75
76 /**
77 * @tc.name: BatteryDump003
78 * @tc.desc: Dump parameter is -r, Reset
79 * @tc.type: FUNC
80 * @tc.require: issueI5YZR1
81 */
82 HWTEST_F(BatteryDumpTest, BatteryDump003, TestSize.Level1)
83 {
84 BATTERY_HILOGI(LABEL_TEST, "BatteryDump003 function start!");
85 int32_t fd = 1;
86 std::vector<std::u16string> args;
87 std::u16string arg = u"-r";
88 args.push_back(arg);
89 EXPECT_EQ(g_service->Dump(fd, args), ERR_OK);
90 BATTERY_HILOGI(LABEL_TEST, "BatteryDump003 function end!");
91 }
92
93 /*
94 * @tc.name: BatteryDump004
95 * @tc.desc: Test functions Dump
96 * @tc.type: FUNC
97 * @tc.require: issueI5YZR1
98 */
99 static HWTEST_F(BatteryDumpTest, BatteryDump004, TestSize.Level1)
100 {
101 BATTERY_HILOGI(LABEL_TEST, "BatteryDump004 function start!");
102 int32_t fd = 1;
103 std::vector<std::u16string> args;
104 std::u16string arg = u"-l";
105 args.push_back(arg);
106 EXPECT_EQ(g_service->Dump(fd, args), ERR_NO_INIT);
107 BATTERY_HILOGI(LABEL_TEST, "BatteryDump004 function end!");
108 }
109
110 /**
111 * @tc.name: BatteryDump005
112 * @tc.desc: Test functions Dump
113 * @tc.type: FUNC
114 * @tc.require: issueI5YZR1
115 */
116 static HWTEST_F(BatteryDumpTest, BatteryDump005, TestSize.Level1)
117 {
118 BATTERY_HILOGI(LABEL_TEST, "BatteryDump005 function start!");
119 int32_t fd = 1;
120 std::vector<std::u16string> args;
121 std::u16string arg = u"-ls";
122 args.push_back(arg);
123 EXPECT_EQ(g_service->Dump(fd, args), ERR_NO_INIT);
124 BATTERY_HILOGI(LABEL_TEST, "BatteryDump005 function end!");
125 }
126
127 /**
128 * @tc.name: BatteryDump006
129 * @tc.desc: Test functions Dump
130 * @tc.type: FUNC
131 * @tc.require: issueI5YZR1
132 */
133 static HWTEST_F(BatteryDumpTest, BatteryDump006, TestSize.Level1)
134 {
135 BATTERY_HILOGI(LABEL_TEST, "BatteryDump006 function start!");
136 int32_t fd = 1;
137 std::vector<std::u16string> args;
138 EXPECT_EQ(g_service->Dump(fd, args), ERR_OK);
139 BATTERY_HILOGI(LABEL_TEST, "BatteryDump006 function end!");
140 }
141
142 /**
143 * @tc.name: BatteryDump007
144 * @tc.desc: Dump parameter is empty, Get battery information
145 * @tc.type: FUNC
146 * @tc.require: issueI5YZR1
147 */
148 HWTEST_F(BatteryDumpTest, BatteryDump007, TestSize.Level1)
149 {
150 BATTERY_HILOGI(LABEL_TEST, "BatteryDump007 function start!");
151 BatteryDump& batteryDump = BatteryDump::GetInstance();
152 int32_t fd = 1;
153 std::vector<std::u16string> args;
154 EXPECT_FALSE(batteryDump.GetBatteryInfo(fd, g_service, args));
155 BATTERY_HILOGI(LABEL_TEST, "BatteryDump007 function end!");
156 }
157
158 /**
159 * @tc.name: BatteryDump008
160 * @tc.desc: Dump parameter is empty, MockUnplugged
161 * @tc.type: FUNC
162 * @tc.require: issueI5YZR1
163 */
164 HWTEST_F(BatteryDumpTest, BatteryDump008, TestSize.Level1)
165 {
166 BATTERY_HILOGI(LABEL_TEST, "BatteryDump008 function start!");
167 BatteryDump& batteryDump = BatteryDump::GetInstance();
168 int32_t fd = 1;
169 std::vector<std::u16string> args;
170 EXPECT_FALSE(batteryDump.MockUnplugged(fd, g_service, args));
171 BATTERY_HILOGI(LABEL_TEST, "BatteryDump008 function end!");
172 }
173
174 /**
175 * @tc.name: BatteryDump009
176 * @tc.desc: Dump parameter is empty, Reset
177 * @tc.type: FUNC
178 * @tc.require: issueI5YZR1
179 */
180 HWTEST_F(BatteryDumpTest, BatteryDump009, TestSize.Level1)
181 {
182 BATTERY_HILOGI(LABEL_TEST, "BatteryDump009 function start!");
183 BatteryDump& batteryDump = BatteryDump::GetInstance();
184 int32_t fd = 1;
185 std::vector<std::u16string> args;
186 EXPECT_FALSE(batteryDump.Reset(fd, g_service, args));
187 BATTERY_HILOGI(LABEL_TEST, "BatteryDump009 function end!");
188 }
189
190 /**
191 * @tc.name: BatteryDump010
192 * @tc.desc: Test functions Dump, capacity cmd normal
193 * @tc.type: FUNC
194 * @tc.require: issueI6Z8RB
195 */
196 static HWTEST_F(BatteryDumpTest, BatteryDump010, TestSize.Level1)
197 {
198 BATTERY_HILOGI(LABEL_TEST, "BatteryDump010 function start!");
199 int32_t fd = 1;
200 std::vector<std::u16string> args;
201 std::u16string argParam = u"--capacity";
202 std::u16string argCapacity = u"20";
203 args.push_back(argParam);
204 args.push_back(argCapacity);
205 EXPECT_EQ(g_service->Dump(fd, args), ERR_OK);
206 BATTERY_HILOGI(LABEL_TEST, "BatteryDump010 function end!");
207 }
208
209 /**
210 * @tc.name: BatteryDump011
211 * @tc.desc: Test functions Dump, capacity cmd invalid
212 * @tc.type: FUNC
213 * @tc.require: issueI6Z8RB
214 */
215 static HWTEST_F(BatteryDumpTest, BatteryDump011, TestSize.Level1)
216 {
217 BATTERY_HILOGI(LABEL_TEST, "BatteryDump011 function start!");
218 int32_t fd = 1;
219 std::vector<std::u16string> args;
220 std::u16string arg = u"--capacity";
221 args.push_back(arg);
222 EXPECT_EQ(g_service->Dump(fd, args), ERR_NO_INIT);
223 BATTERY_HILOGI(LABEL_TEST, "BatteryDump011 function end!");
224 }
225
226 /**
227 * @tc.name: BatteryDump012
228 * @tc.desc: Test functions Dump, capacity out of range
229 * @tc.type: FUNC
230 * @tc.require: issueI6Z8RB
231 */
232 static HWTEST_F(BatteryDumpTest, BatteryDump012, TestSize.Level1)
233 {
234 BATTERY_HILOGI(LABEL_TEST, "BatteryDump012 function start!");
235 int32_t fd = 1;
236 std::vector<std::u16string> args;
237 std::u16string arg = u"--capacity 200";
238 args.push_back(arg);
239 EXPECT_EQ(g_service->Dump(fd, args), ERR_NO_INIT);
240 BATTERY_HILOGI(LABEL_TEST, "BatteryDump012 function end!");
241 }
242
243 /**
244 * @tc.name: BatteryDump013
245 * @tc.desc: Dump parameter is empty, Capacity
246 * @tc.type: FUNC
247 * @tc.require: issueI5YZR1
248 */
249 HWTEST_F(BatteryDumpTest, BatteryDump013, TestSize.Level1)
250 {
251 BATTERY_HILOGI(LABEL_TEST, "BatteryDump013 function start!");
252 BatteryDump& batteryDump = BatteryDump::GetInstance();
253 int32_t fd = 1;
254 std::vector<std::u16string> args;
255 EXPECT_FALSE(batteryDump.MockCapacity(fd, g_service, args));
256 BATTERY_HILOGI(LABEL_TEST, "BatteryDump013 function end!");
257 }
258
259 /**
260 * @tc.name: BatteryDump014
261 * @tc.desc: Dump parameter is empty, Uevent
262 * @tc.type: FUNC
263 */
264 HWTEST_F(BatteryDumpTest, BatteryDump014, TestSize.Level1)
265 {
266 BATTERY_HILOGI(LABEL_TEST, "BatteryDump014 function start!");
267 BatteryDump& batteryDump = BatteryDump::GetInstance();
268 int32_t fd = 1;
269 std::vector<std::u16string> args;
270 EXPECT_FALSE(batteryDump.MockUevent(fd, g_service, args));
271 BATTERY_HILOGI(LABEL_TEST, "BatteryDump014 function end!");
272 }
273
274 /**
275 * @tc.name: BatteryDump015
276 * @tc.desc: Test functions Dump, Uevent cmd normal
277 * @tc.type: FUNC
278 */
279 static HWTEST_F(BatteryDumpTest, BatteryDump015, TestSize.Level1)
280 {
281 BATTERY_HILOGI(LABEL_TEST, "BatteryDump015 function start!");
282 int32_t fd = 1;
283 std::vector<std::u16string> args;
284 std::u16string argParam = u"--uevent";
285 std::u16string argCapacity = u"BATTERY_UNDER_VOLTAGE=3$sendcommonevent";
286 args.push_back(argParam);
287 args.push_back(argCapacity);
288 EXPECT_EQ(g_service->Dump(fd, args), ERR_OK);
289 BATTERY_HILOGI(LABEL_TEST, "BatteryDump015 function end!");
290 }
291
292 /**
293 * @tc.name: BatteryDump016
294 * @tc.desc: Test functions Dump, Uevent cmd invalid
295 * @tc.type: FUNC
296 */
297 static HWTEST_F(BatteryDumpTest, BatteryDump016, TestSize.Level1)
298 {
299 BATTERY_HILOGI(LABEL_TEST, "BatteryDump016 function start!");
300 int32_t fd = 1;
301 std::vector<std::u16string> args;
302 std::u16string arg = u"--uevent";
303 args.push_back(arg);
304 EXPECT_EQ(g_service->Dump(fd, args), ERR_NO_INIT);
305 BATTERY_HILOGI(LABEL_TEST, "BatteryDump016 function end!");
306 }
307 } // namespace PowerMgr
308 } // namespace OHOS
309