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_notify_test.h"
17
18 #include <string>
19
20 #include "battery_log.h"
21 #include "battery_notify.h"
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace PowerMgr {
26 BatteryInfo* g_batteryInfo;
27 std::shared_ptr<BatteryNotify> g_batteryNotify;
28
SetUpTestCase()29 void BatteryNotifyTest::SetUpTestCase()
30 {
31 g_batteryNotify = std::make_shared<BatteryNotify>();
32 }
33
SetUp()34 void BatteryNotifyTest::SetUp()
35 {
36 g_batteryInfo = new BatteryInfo();
37 const int32_t capacity = 100;
38 g_batteryInfo->SetCapacity(capacity);
39 const int32_t voltage = 1;
40 g_batteryInfo->SetVoltage(voltage);
41 const int32_t temperature = 25;
42 g_batteryInfo->SetTemperature(temperature);
43 const BatteryHealthState healthState = BatteryHealthState::HEALTH_STATE_UNKNOWN;
44 g_batteryInfo->SetHealthState(healthState);
45 const BatteryPluggedType pluggedType = BatteryPluggedType::PLUGGED_TYPE_NONE;
46 g_batteryInfo->SetPluggedType(pluggedType);
47 const int32_t plugMaxCur = 50;
48 g_batteryInfo->SetPluggedMaxCurrent(plugMaxCur);
49 const int32_t plugMaxVol = 50;
50 g_batteryInfo->SetPluggedMaxVoltage(plugMaxVol);
51 const BatteryChargeState chargeState = BatteryChargeState::CHARGE_STATE_NONE;
52 g_batteryInfo->SetChargeState(chargeState);
53 const int32_t chargeCounter = 1;
54 g_batteryInfo->SetChargeCounter(chargeCounter);
55 const int32_t totalEnergy = 50;
56 g_batteryInfo->SetTotalEnergy(totalEnergy);
57 const int32_t curAverage = 10;
58 g_batteryInfo->SetCurAverage(curAverage);
59 const int32_t remainEnergy = 30;
60 g_batteryInfo->SetRemainEnergy(remainEnergy);
61 g_batteryInfo->SetPresent(true);
62 const string tec = "H2";
63 g_batteryInfo->SetTechnology(tec);
64 const int32_t nowCur = 10;
65 g_batteryInfo->SetNowCurrent(nowCur);
66 }
67
TearDown()68 void BatteryNotifyTest::TearDown()
69 {
70 if (g_batteryInfo != nullptr) {
71 delete g_batteryInfo;
72 g_batteryInfo = nullptr;
73 }
74 }
75
76 /**
77 * @tc.name: BatteryNotify001
78 * @tc.desc: Test PublishEvents
79 * @tc.type: FUNC
80 * @tc.require: issueI5YZR1
81 */
82 HWTEST_F(BatteryNotifyTest, BatteryNotify001, TestSize.Level1)
83 {
84 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
85 EXPECT_EQ(ret, ERR_OK);
86 }
87
88 /**
89 * @tc.name: BatteryNotify002
90 * @tc.desc: Test PublishEvents
91 * @tc.type: FUNC
92 * @tc.require: issueI5YZR1
93 */
94 HWTEST_F(BatteryNotifyTest, BatteryNotify002, TestSize.Level1)
95 {
96 const BatteryHealthState healthState = BatteryHealthState::HEALTH_STATE_GOOD;
97 g_batteryInfo->SetHealthState(healthState);
98 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
99 EXPECT_EQ(ret, ERR_OK);
100 }
101
102 /**
103 * @tc.name: BatteryNotify003
104 * @tc.desc: Test PublishEvents
105 * @tc.type: FUNC
106 * @tc.require: issueI5YZR1
107 */
108 HWTEST_F(BatteryNotifyTest, BatteryNotify003, TestSize.Level1)
109 {
110 const BatteryHealthState healthState = BatteryHealthState::HEALTH_STATE_OVERHEAT;
111 g_batteryInfo->SetHealthState(healthState);
112 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
113 EXPECT_EQ(ret, ERR_OK);
114 }
115
116 /**
117 * @tc.name: BatteryNotify004
118 * @tc.desc: Test PublishEvents
119 * @tc.type: FUNC
120 * @tc.require: issueI5YZR1
121 */
122 HWTEST_F(BatteryNotifyTest, BatteryNotify004, TestSize.Level1)
123 {
124 const BatteryHealthState healthState = BatteryHealthState::HEALTH_STATE_OVERVOLTAGE;
125 g_batteryInfo->SetHealthState(healthState);
126 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
127 EXPECT_EQ(ret, ERR_OK);
128 }
129
130 /**
131 * @tc.name: BatteryNotify005
132 * @tc.desc: Test PublishEvents
133 * @tc.type: FUNC
134 * @tc.require: issueI5YZR1
135 */
136 HWTEST_F(BatteryNotifyTest, BatteryNotify005, TestSize.Level1)
137 {
138 const BatteryHealthState healthState = BatteryHealthState::HEALTH_STATE_COLD;
139 g_batteryInfo->SetHealthState(healthState);
140 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
141 EXPECT_EQ(ret, ERR_OK);
142 }
143
144 /**
145 * @tc.name: BatteryNotify006
146 * @tc.desc: Test PublishEvents
147 * @tc.type: FUNC
148 * @tc.require: issueI5YZR1
149 */
150 HWTEST_F(BatteryNotifyTest, BatteryNotify006, TestSize.Level1)
151 {
152 const BatteryHealthState healthState = BatteryHealthState::HEALTH_STATE_DEAD;
153 g_batteryInfo->SetHealthState(healthState);
154 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
155 EXPECT_EQ(ret, ERR_OK);
156 }
157
158 /**
159 * @tc.name: BatteryNotify007
160 * @tc.desc: Test PublishEvents
161 * @tc.type: FUNC
162 * @tc.require: issueI5YZR1
163 */
164 HWTEST_F(BatteryNotifyTest, BatteryNotify007, TestSize.Level1)
165 {
166 const BatteryHealthState healthState = BatteryHealthState::HEALTH_STATE_BUTT;
167 g_batteryInfo->SetHealthState(healthState);
168 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
169 EXPECT_EQ(ret, ERR_OK);
170 }
171
172 /**
173 * @tc.name: BatteryNotify008
174 * @tc.desc: Test PublishEvents
175 * @tc.type: FUNC
176 * @tc.require: issueI5YZR1
177 */
178 HWTEST_F(BatteryNotifyTest, BatteryNotify008, TestSize.Level1)
179 {
180 const BatteryPluggedType pluggedType = BatteryPluggedType::PLUGGED_TYPE_NONE;
181 g_batteryInfo->SetPluggedType(pluggedType);
182 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
183 EXPECT_EQ(ret, ERR_OK);
184 }
185
186 /**
187 * @tc.name: BatteryNotify009
188 * @tc.desc: Test PublishEvents
189 * @tc.type: FUNC
190 * @tc.require: issueI5YZR1
191 */
192 HWTEST_F(BatteryNotifyTest, BatteryNotify009, TestSize.Level1)
193 {
194 const BatteryPluggedType pluggedType = BatteryPluggedType::PLUGGED_TYPE_AC;
195 g_batteryInfo->SetPluggedType(pluggedType);
196 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
197 EXPECT_EQ(ret, ERR_OK);
198 }
199
200 /**
201 * @tc.name: BatteryNotify010
202 * @tc.desc: Test PublishEvents
203 * @tc.type: FUNC
204 * @tc.require: issueI5YZR1
205 */
206 HWTEST_F(BatteryNotifyTest, BatteryNotify010, TestSize.Level1)
207 {
208 const BatteryPluggedType pluggedType = BatteryPluggedType::PLUGGED_TYPE_USB;
209 g_batteryInfo->SetPluggedType(pluggedType);
210 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
211 EXPECT_EQ(ret, ERR_OK);
212 }
213
214 /**
215 * @tc.name: BatteryNotify011
216 * @tc.desc: Test PublishEvents
217 * @tc.type: FUNC
218 * @tc.require: issueI5YZR1
219 */
220 HWTEST_F(BatteryNotifyTest, BatteryNotify011, TestSize.Level1)
221 {
222 const BatteryPluggedType pluggedType = BatteryPluggedType::PLUGGED_TYPE_WIRELESS;
223 g_batteryInfo->SetPluggedType(pluggedType);
224 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
225 EXPECT_EQ(ret, ERR_OK);
226 }
227
228 /**
229 * @tc.name: BatteryNotify012
230 * @tc.desc: Test PublishEvents
231 * @tc.type: FUNC
232 * @tc.require: issueI5YZR1
233 */
234 HWTEST_F(BatteryNotifyTest, BatteryNotify012, TestSize.Level1)
235 {
236 const BatteryPluggedType pluggedType = BatteryPluggedType::PLUGGED_TYPE_BUTT;
237 g_batteryInfo->SetPluggedType(pluggedType);
238 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
239 EXPECT_EQ(ret, ERR_OK);
240 }
241
242 /**
243 * @tc.name: BatteryNotify013
244 * @tc.desc: Test PublishEvents
245 * @tc.type: FUNC
246 * @tc.require: issueI5YZR1
247 */
248 HWTEST_F(BatteryNotifyTest, BatteryNotify013, TestSize.Level1)
249 {
250 const BatteryChargeState chargeState = BatteryChargeState::CHARGE_STATE_ENABLE;
251 g_batteryInfo->SetChargeState(chargeState);
252 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
253 EXPECT_EQ(ret, ERR_OK);
254 }
255
256 /**
257 * @tc.name: BatteryNotify014
258 * @tc.desc: Test PublishEvents
259 * @tc.type: FUNC
260 * @tc.require: issueI5YZR1
261 */
262 HWTEST_F(BatteryNotifyTest, BatteryNotify014, TestSize.Level1)
263 {
264 const BatteryChargeState chargeState = BatteryChargeState::CHARGE_STATE_DISABLE;
265 g_batteryInfo->SetChargeState(chargeState);
266 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
267 EXPECT_EQ(ret, ERR_OK);
268 }
269
270 /**
271 * @tc.name: BatteryNotify015
272 * @tc.desc: Test PublishEvents
273 * @tc.type: FUNC
274 * @tc.require: issueI5YZR1
275 */
276 HWTEST_F(BatteryNotifyTest, BatteryNotify015, TestSize.Level1)
277 {
278 const BatteryChargeState chargeState = BatteryChargeState::CHARGE_STATE_FULL;
279 g_batteryInfo->SetChargeState(chargeState);
280 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
281 EXPECT_EQ(ret, ERR_OK);
282 }
283
284 /**
285 * @tc.name: BatteryNotify016
286 * @tc.desc: Test PublishEvents
287 * @tc.type: FUNC
288 * @tc.require: issueI5YZR1
289 */
290 HWTEST_F(BatteryNotifyTest, BatteryNotify016, TestSize.Level1)
291 {
292 const BatteryChargeState chargeState = BatteryChargeState::CHARGE_STATE_BUTT;
293 g_batteryInfo->SetChargeState(chargeState);
294 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
295 EXPECT_EQ(ret, ERR_OK);
296 }
297
298 /**
299 * @tc.name: BatteryNotify017
300 * @tc.desc: Test PublishEvents
301 * @tc.type: FUNC
302 * @tc.require: issueI5YZR1
303 */
304 HWTEST_F(BatteryNotifyTest, BatteryNotify017, TestSize.Level1)
305 {
306 const int32_t capacity = -100;
307 g_batteryInfo->SetCapacity(capacity);
308 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
309 EXPECT_EQ(ret, ERR_OK);
310 }
311
312 /**
313 * @tc.name: BatteryNotify018
314 * @tc.desc: Test PublishEvents
315 * @tc.type: FUNC
316 * @tc.require: issueI5YZR1
317 */
318 HWTEST_F(BatteryNotifyTest, BatteryNotify018, TestSize.Level1)
319 {
320 for (int i = 0; i < 2; i++) {
321 const BatteryChargeState chargeState = BatteryChargeState::CHARGE_STATE_ENABLE;
322 g_batteryInfo->SetChargeState(chargeState);
323 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
324 EXPECT_EQ(ret, ERR_OK);
325 }
326 }
327
328 /**
329 * @tc.name: BatteryNotify019
330 * @tc.desc: Test PublishEvents
331 * @tc.type: FUNC
332 * @tc.require: issueI5YZR1
333 */
334 HWTEST_F(BatteryNotifyTest, BatteryNotify019, TestSize.Level1)
335 {
336 const int32_t capacity = -100;
337 g_batteryInfo->SetCapacity(capacity);
338 for (int i = 0; i < 2; i++) {
339 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
340 EXPECT_EQ(ret, ERR_OK);
341 }
342 }
343
344 /**
345 * @tc.name: BatteryNotify020
346 * @tc.desc: Test PublishChargeTypeEvent
347 * @tc.type: FUNC
348 */
349 HWTEST_F(BatteryNotifyTest, BatteryNotify020, TestSize.Level1)
350 {
351 const ChargeType chargeType = ChargeType::WIRED_NORMAL;
352 g_batteryInfo->SetChargeType(chargeType);
353 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
354 EXPECT_EQ(ret, ERR_OK);
355 }
356
357 /**
358 * @tc.name: BatteryNotify021
359 * @tc.desc: Test PublishUEvent
360 * @tc.type: FUNC
361 */
362 HWTEST_F(BatteryNotifyTest, BatteryNotify021, TestSize.Level1)
363 {
364 const string uevent1 = "TEST_BATTERY_UNDER_VOLTAGE=3";
365 g_batteryInfo->SetUevent(uevent1);
366 EXPECT_EQ(g_batteryInfo->GetUevent(), uevent1);
367
368 const string uevent2 = "BATTERY_UNDER_VOLTAGE=3";
369 g_batteryInfo->SetUevent(uevent2);
370 auto ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
371 EXPECT_EQ(ret, ERR_OK);
372
373 const string uevent3 = "XXXBATTERY_UNDER_VOLTAGE=1";
374 g_batteryInfo->SetUevent(uevent3);
375 ret = g_batteryNotify->PublishEvents(*g_batteryInfo);
376 EXPECT_EQ(ret, ERR_OK);
377 }
378 } // namespace PowerMgr
379 } // namespace OHOS
380