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 "battery_dump_test.h"
17 #include <memory>
18
19 #define private public
20 #define protected public
21 #include "battery_dump.h"
22 #include "battery_service.h"
23 #undef private
24 #undef protected
25 #include "power_common.h"
26
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace PowerMgr {
31 namespace {
32 sptr<BatteryService> g_service;
33 }
SetUpTestCase()34 void BatteryDumpTest::SetUpTestCase()
35 {
36 g_service = DelayedSpSingleton<BatteryService>::GetInstance();
37 g_service->isBootCompleted_ = true;
38 }
39
40 /**
41 * @tc.name: BatteryDump001
42 * @tc.desc: Dump parameter is -i, Get battery information
43 * @tc.type: FUNC
44 * @tc.require: issueI5YZR1
45 */
46 HWTEST_F(BatteryDumpTest, BatteryDump001, TestSize.Level1)
47 {
48 int32_t fd = 1;
49 std::vector<std::u16string> args;
50 std::u16string arg = u"-i";
51 args.push_back(arg);
52 EXPECT_EQ(g_service->Dump(fd, args), ERR_OK);
53 }
54
55 /**
56 * @tc.name: BatteryDump002
57 * @tc.desc: Dump parameter is -u, MockUnplugged
58 * @tc.type: FUNC
59 * @tc.require: issueI5YZR1
60 */
61 HWTEST_F(BatteryDumpTest, BatteryDump002, TestSize.Level1)
62 {
63 int32_t fd = 1;
64 std::vector<std::u16string> args;
65 std::u16string arg = u"-u";
66 args.push_back(arg);
67 EXPECT_EQ(g_service->Dump(fd, args), ERR_OK);
68 }
69
70 /**
71 * @tc.name: BatteryDump003
72 * @tc.desc: Dump parameter is -r, Reset
73 * @tc.type: FUNC
74 * @tc.require: issueI5YZR1
75 */
76 HWTEST_F(BatteryDumpTest, BatteryDump003, TestSize.Level1)
77 {
78 int32_t fd = 1;
79 std::vector<std::u16string> args;
80 std::u16string arg = u"-r";
81 args.push_back(arg);
82 EXPECT_EQ(g_service->Dump(fd, args), ERR_OK);
83 }
84
85 /*
86 * @tc.name: BatteryDump005
87 * @tc.desc: Test functions Dump
88 * @tc.type: FUNC
89 * @tc.require: issueI5YZR1
90 */
91 static HWTEST_F(BatteryDumpTest, BatteryDump005, TestSize.Level1)
92 {
93 int32_t fd = 1;
94 std::vector<std::u16string> args;
95 std::u16string arg = u"-l";
96 args.push_back(arg);
97 EXPECT_EQ(g_service->Dump(fd, args), ERR_NO_INIT);
98 }
99
100 /**
101 * @tc.name: BatteryDump006
102 * @tc.desc: Test functions Dump
103 * @tc.type: FUNC
104 * @tc.require: issueI5YZR1
105 */
106 static HWTEST_F(BatteryDumpTest, BatteryDump006, TestSize.Level1)
107 {
108 int32_t fd = 1;
109 std::vector<std::u16string> args;
110 std::u16string arg = u"-ls";
111 args.push_back(arg);
112 EXPECT_EQ(g_service->Dump(fd, args), ERR_NO_INIT);
113 }
114
115 /**
116 * @tc.name: BatteryDump007
117 * @tc.desc: Test functions Dump
118 * @tc.type: FUNC
119 * @tc.require: issueI5YZR1
120 */
121 static HWTEST_F(BatteryDumpTest, BatteryDump007, TestSize.Level1)
122 {
123 int32_t fd = 1;
124 std::vector<std::u16string> args;
125 EXPECT_EQ(g_service->Dump(fd, args), ERR_OK);
126 }
127
128 /**
129 * @tc.name: BatteryDump008
130 * @tc.desc: Dump parameter is empty, Get battery information
131 * @tc.type: FUNC
132 * @tc.require: issueI5YZR1
133 */
134 HWTEST_F(BatteryDumpTest, BatteryDump008, TestSize.Level1)
135 {
136 BatteryDump& batteryDump = BatteryDump::GetInstance();
137 int32_t fd = 1;
138 std::vector<std::u16string> args;
139 EXPECT_FALSE(batteryDump.GetBatteryInfo(fd, g_service, args));
140 }
141
142 /**
143 * @tc.name: BatteryDump009
144 * @tc.desc: Dump parameter is empty, MockUnplugged
145 * @tc.type: FUNC
146 * @tc.require: issueI5YZR1
147 */
148 HWTEST_F(BatteryDumpTest, BatteryDump009, TestSize.Level1)
149 {
150 BatteryDump& batteryDump = BatteryDump::GetInstance();
151 int32_t fd = 1;
152 std::vector<std::u16string> args;
153 EXPECT_FALSE(batteryDump.MockUnplugged(fd, g_service, args));
154 }
155
156 /**
157 * @tc.name: BatteryDump010
158 * @tc.desc: Dump parameter is empty, Reset
159 * @tc.type: FUNC
160 * @tc.require: issueI5YZR1
161 */
162 HWTEST_F(BatteryDumpTest, BatteryDump010, TestSize.Level1)
163 {
164 BatteryDump& batteryDump = BatteryDump::GetInstance();
165 int32_t fd = 1;
166 std::vector<std::u16string> args;
167 EXPECT_FALSE(batteryDump.Reset(fd, g_service, args));
168 }
169
170 /**
171 * @tc.name: BatteryDump011
172 * @tc.desc: Test functions Dump, capacity cmd normal
173 * @tc.type: FUNC
174 * @tc.require: issueI6Z8RB
175 */
176 static HWTEST_F(BatteryDumpTest, BatteryDump011, TestSize.Level1)
177 {
178 int32_t fd = 1;
179 std::vector<std::u16string> args;
180 std::u16string argParam = u"--capacity";
181 std::u16string argCapacity = u"20";
182 args.push_back(argParam);
183 args.push_back(argCapacity);
184 EXPECT_EQ(g_service->Dump(fd, args), ERR_OK);
185 }
186
187 /**
188 * @tc.name: BatteryDump012
189 * @tc.desc: Test functions Dump, capacity cmd invalid
190 * @tc.type: FUNC
191 * @tc.require: issueI6Z8RB
192 */
193 static HWTEST_F(BatteryDumpTest, BatteryDump012, TestSize.Level1)
194 {
195 int32_t fd = 1;
196 std::vector<std::u16string> args;
197 std::u16string arg = u"--capacity";
198 args.push_back(arg);
199 EXPECT_EQ(g_service->Dump(fd, args), ERR_NO_INIT);
200 }
201
202 /**
203 * @tc.name: BatteryDump013
204 * @tc.desc: Test functions Dump, capacity out of range
205 * @tc.type: FUNC
206 * @tc.require: issueI6Z8RB
207 */
208 static HWTEST_F(BatteryDumpTest, BatteryDump013, TestSize.Level1)
209 {
210 int32_t fd = 1;
211 std::vector<std::u16string> args;
212 std::u16string arg = u"--capacity 200";
213 args.push_back(arg);
214 EXPECT_EQ(g_service->Dump(fd, args), ERR_NO_INIT);
215 }
216
217 /**
218 * @tc.name: BatteryDump014
219 * @tc.desc: Dump parameter is empty, Capacity
220 * @tc.type: FUNC
221 * @tc.require: issueI5YZR1
222 */
223 HWTEST_F(BatteryDumpTest, BatteryDump014, TestSize.Level1)
224 {
225 BatteryDump& batteryDump = BatteryDump::GetInstance();
226 int32_t fd = 1;
227 std::vector<std::u16string> args;
228 EXPECT_FALSE(batteryDump.MockCapacity(fd, g_service, args));
229 }
230 } // namespace PowerMgr
231 } // namespace OHOS
232