1 /*
2 * Copyright (c) 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 #ifdef GTEST
17 #define private public
18 #define protected public
19 #endif
20 #include <iostream>
21 #include <string>
22
23 #include "battery_notify.h"
24 #include "battery_log.h"
25 #include "test_utils.h"
26 #include <gtest/gtest.h>
27 #include <gmock/gmock.h>
28 #include "mock_common_event_manager.h"
29 #include "mock_sa_manager.h"
30 #include "mock_remote_object.h"
31 #include "iremote_object.h"
32
33 using namespace testing::ext;
34 using namespace OHOS::PowerMgr;
35 using namespace OHOS;
36 using namespace std;
37
38 namespace {
39 shared_ptr<BatteryNotify> g_batteryNotify = nullptr;
40 const int32_t COMMON_EVENT_SERVICE_ID = 3299;
41 bool g_commonEventInitSuccess = false;
42 } // namespace
43
44 namespace OHOS {
45 namespace PowerMgr {
IsCommonEventServiceAbilityExist() const46 bool BatteryNotify::IsCommonEventServiceAbilityExist() const
47 {
48 sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
49 if (!sysMgr) {
50 BATTERY_HILOGE(COMP_SVC,
51 "IsCommonEventServiceAbilityExist Get ISystemAbilityManager failed, no SystemAbilityManager");
52 return false;
53 }
54 sptr<IRemoteObject> remote = sysMgr->CheckSystemAbility(COMMON_EVENT_SERVICE_ID);
55 if (!remote) {
56 BATTERY_HILOGE(COMP_SVC, "No CesServiceAbility");
57 return false;
58 }
59
60 if (!g_commonEventInitSuccess) {
61 BATTERY_HILOGI(COMP_SVC, "common event service ability init success");
62 g_commonEventInitSuccess = true;
63 }
64
65 return true;
66 }
67 class BatteryEventTestPart2 : public testing::Test {
68 public:
69 static void SetUpTestCase();
70 static void TearDownTestCase();
71 MockSystemAbilityManager mockSam;
72 SystemAbilityManagerClient& mockClient = SystemAbilityManagerClient::GetInstance();
73 };
74 } // namespace PowerMgr
75 } // namespace OHOS
76
SetUpTestCase()77 void BatteryEventTestPart2::SetUpTestCase()
78 {
79 if (g_batteryNotify == nullptr) {
80 g_batteryNotify = make_shared<BatteryNotify>();
81 }
82 }
83
TearDownTestCase()84 void BatteryEventTestPart2::TearDownTestCase()
85 {
86 if (g_batteryNotify != nullptr) {
87 g_batteryNotify.reset();
88 g_batteryNotify = nullptr;
89 }
90 }
91
92 /**
93 * @tc.name: BatteryEventTestPart2001
94 * @tc.desc: test PublishLowEvent function
95 * @tc.type: FUNC
96 */
97 HWTEST_F(BatteryEventTestPart2, BatteryEventTestPart2001, TestSize.Level1)
98 {
99 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2001 start.");
100 BatteryInfo info;
101 info.SetCapacity(50);
102 MockBatteryCommonEventManager::SetBoolReturnValue(false);
103 bool ret = g_batteryNotify->PublishLowEvent(info);
104 EXPECT_TRUE(ret);
105 info.SetCapacity(8);
106 MockBatteryCommonEventManager::SetBoolReturnValue(false);
107 ret = g_batteryNotify->PublishLowEvent(info);
108 EXPECT_FALSE(ret);
109 info.SetCapacity(7);
110 MockBatteryCommonEventManager::SetBoolReturnValue(false);
111 ret = g_batteryNotify->PublishLowEvent(info);
112 EXPECT_TRUE(ret);
113 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2001 end.");
114 }
115
116 /**
117 * @tc.name: BatteryEventTestPart2002
118 * @tc.desc: test PublishPowerConnectedEvent function
119 * @tc.type: FUNC
120 */
121 HWTEST_F(BatteryEventTestPart2, BatteryEventTestPart2002, TestSize.Level1)
122 {
123 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2002 start.");
124 BatteryInfo info;
125 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_USB);
126 MockBatteryCommonEventManager::SetBoolReturnValue(true);
127 bool ret = g_batteryNotify->PublishPowerConnectedEvent(info);
128 EXPECT_TRUE(ret);
129 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_USB);
130 MockBatteryCommonEventManager::SetBoolReturnValue(false);
131 ret = g_batteryNotify->PublishPowerConnectedEvent(info);
132 EXPECT_TRUE(ret);
133 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_NONE);
134 MockBatteryCommonEventManager::SetBoolReturnValue(false);
135 ret = g_batteryNotify->PublishPowerConnectedEvent(info);
136 EXPECT_TRUE(ret);
137 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_USB);
138 MockBatteryCommonEventManager::SetBoolReturnValue(false);
139 ret = g_batteryNotify->PublishPowerConnectedEvent(info);
140 EXPECT_FALSE(ret);
141 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_BUTT);
142 MockBatteryCommonEventManager::SetBoolReturnValue(false);
143 ret = g_batteryNotify->PublishPowerConnectedEvent(info);
144 EXPECT_TRUE(ret);
145 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_USB);
146 MockBatteryCommonEventManager::SetBoolReturnValue(true);
147 ret = g_batteryNotify->PublishPowerConnectedEvent(info);
148 EXPECT_TRUE(ret);
149 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2002 end.");
150 }
151
152 /**
153 * @tc.name: BatteryEventTestPart2003
154 * @tc.desc: test PublishPowerDisconnectedEvent function
155 * @tc.type: FUNC
156 */
157 HWTEST_F(BatteryEventTestPart2, BatteryEventTestPart2003, TestSize.Level1)
158 {
159 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2003 start.");
160 BatteryInfo info;
161 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_NONE);
162 MockBatteryCommonEventManager::SetBoolReturnValue(true);
163 bool ret = g_batteryNotify->PublishPowerDisconnectedEvent(info);
164 EXPECT_TRUE(ret);
165 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_NONE);
166 MockBatteryCommonEventManager::SetBoolReturnValue(false);
167 ret = g_batteryNotify->PublishPowerDisconnectedEvent(info);
168 EXPECT_TRUE(ret);
169 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_USB);
170 MockBatteryCommonEventManager::SetBoolReturnValue(false);
171 ret = g_batteryNotify->PublishPowerDisconnectedEvent(info);
172 EXPECT_TRUE(ret);
173 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_NONE);
174 MockBatteryCommonEventManager::SetBoolReturnValue(false);
175 ret = g_batteryNotify->PublishPowerDisconnectedEvent(info);
176 EXPECT_FALSE(ret);
177 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_USB);
178 MockBatteryCommonEventManager::SetBoolReturnValue(false);
179 ret = g_batteryNotify->PublishPowerDisconnectedEvent(info);
180 EXPECT_TRUE(ret);
181 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_BUTT);
182 MockBatteryCommonEventManager::SetBoolReturnValue(true);
183 ret = g_batteryNotify->PublishPowerDisconnectedEvent(info);
184 EXPECT_TRUE(ret);
185 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2003 end.");
186 }
187
188 /**
189 * @tc.name: BatteryEventTestPart2004
190 * @tc.desc: test PublishEvents function
191 * @tc.type: FUNC
192 */
193 HWTEST_F(BatteryEventTestPart2, BatteryEventTestPart2004, TestSize.Level1)
194 {
195 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2004 start.");
196 BatteryInfo info;
197 info.SetUevent("InitBatteryInfo");
198 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_NONE);
199 EXPECT_CALL(mockClient, GetSystemAbilityManager())
200 .WillRepeatedly(testing::Return(nullptr));
201 int32_t ret = g_batteryNotify->PublishEvents(info);
202 EXPECT_TRUE(ret == ERR_NO_INIT);
203
204 sptr<IRemoteObject> mockRemote = new MockRemoteObject(u"BatteryEventTestPart2004");
205 EXPECT_CALL(mockClient, GetSystemAbilityManager())
206 .WillRepeatedly(testing::Return(&mockSam));
207 EXPECT_CALL(mockSam, CheckSystemAbility(COMMON_EVENT_SERVICE_ID))
208 .WillOnce(testing::Return(mockRemote));
209 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_BUTT);
210 ret = g_batteryNotify->PublishEvents(info);
211 EXPECT_TRUE(ret == ERR_OK);
212
213 EXPECT_CALL(mockSam, CheckSystemAbility(COMMON_EVENT_SERVICE_ID))
214 .WillOnce(testing::Return(mockRemote));
215 info.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_AC);
216 ret = g_batteryNotify->PublishEvents(info);
217 EXPECT_TRUE(ret == ERR_OK);
218 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2004 end.");
219 }
220
221 /**
222 * @tc.name: BatteryEventTestPart2005
223 * @tc.desc: test IsCommonEventServiceAbilityExist function
224 * @tc.type: FUNC
225 */
226 HWTEST_F(BatteryEventTestPart2, BatteryEventTestPart2005, TestSize.Level1)
227 {
228 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2005 start.");
229 EXPECT_CALL(mockClient, GetSystemAbilityManager())
230 .WillRepeatedly(testing::Return(nullptr));
231 bool result = g_batteryNotify->IsCommonEventServiceAbilityExist();
232 EXPECT_FALSE(result);
233
234 EXPECT_CALL(mockClient, GetSystemAbilityManager())
235 .WillRepeatedly(testing::Return(&mockSam));
236 EXPECT_CALL(mockSam, CheckSystemAbility(COMMON_EVENT_SERVICE_ID))
237 .WillOnce(testing::Return(nullptr));
238 result = g_batteryNotify->IsCommonEventServiceAbilityExist();
239 EXPECT_FALSE(result);
240
241 sptr<IRemoteObject> mockRemote = new MockRemoteObject(u"BatteryEventTestPart2005");
242 EXPECT_CALL(mockClient, GetSystemAbilityManager())
243 .WillOnce(testing::Return(&mockSam));
244 EXPECT_CALL(mockSam, CheckSystemAbility(COMMON_EVENT_SERVICE_ID))
245 .WillOnce(testing::Return(mockRemote));
246 result = g_batteryNotify->IsCommonEventServiceAbilityExist();
247 EXPECT_TRUE(result);
248 testing::Mock::AllowLeak(&mockClient);
249 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2005 end.");
250 }
251
252 /**
253 * @tc.name: BatteryEventTestPart2006
254 * @tc.desc: test PublishChangedEvent function
255 * @tc.type: FUNC
256 */
257 HWTEST_F(BatteryEventTestPart2, BatteryEventTestPart2006, TestSize.Level1)
258 {
259 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2006 start.");
260 BatteryInfo info;
261 MockBatteryCommonEventManager::SetBoolReturnValue(false);
262 bool ret = g_batteryNotify->PublishChangedEvent(info);
263 EXPECT_FALSE(ret);
264 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2006 end.");
265 }
266
267 /**
268 * @tc.name: BatteryEventTestPart2007
269 * @tc.desc: test PublishChangedEventInner function
270 * @tc.type: FUNC
271 */
272 HWTEST_F(BatteryEventTestPart2, BatteryEventTestPart2007, TestSize.Level1)
273 {
274 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2007 start.");
275 BatteryInfo info;
276 MockBatteryCommonEventManager::SetBoolReturnValue(false);
277 bool ret = g_batteryNotify->PublishChangedEventInner(info);
278 EXPECT_FALSE(ret);
279 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2007 end.");
280 }
281
282 /**
283 * @tc.name: BatteryEventTestPart2008
284 * @tc.desc: test PublishOkayEvent function
285 * @tc.type: FUNC
286 */
287 HWTEST_F(BatteryEventTestPart2, BatteryEventTestPart2008, TestSize.Level1)
288 {
289 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2008 start.");
290 BatteryInfo info;
291 info.SetCapacity(7);
292 MockBatteryCommonEventManager::SetBoolReturnValue(false);
293 bool ret = g_batteryNotify->PublishOkayEvent(info);
294 EXPECT_TRUE(ret);
295 info.SetCapacity(50);
296 MockBatteryCommonEventManager::SetBoolReturnValue(false);
297 ret = g_batteryNotify->PublishOkayEvent(info);
298 EXPECT_FALSE(ret);
299 info.SetCapacity(51);
300 MockBatteryCommonEventManager::SetBoolReturnValue(false);
301 ret = g_batteryNotify->PublishOkayEvent(info);
302 EXPECT_TRUE(ret);
303 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2008 end.");
304 }
305
306 /**
307 * @tc.name: BatteryEventTestPart2009
308 * @tc.desc: test PublishChargingEvent function
309 * @tc.type: FUNC
310 */
311 HWTEST_F(BatteryEventTestPart2, BatteryEventTestPart2009, TestSize.Level1)
312 {
313 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2009 start.");
314 BatteryInfo info;
315 info.SetChargeState(BatteryChargeState::CHARGE_STATE_NONE);
316 MockBatteryCommonEventManager::SetBoolReturnValue(false);
317 bool ret = g_batteryNotify->PublishChargingEvent(info);
318 EXPECT_TRUE(ret);
319 info.SetChargeState(BatteryChargeState::CHARGE_STATE_ENABLE);
320 MockBatteryCommonEventManager::SetBoolReturnValue(false);
321 ret = g_batteryNotify->PublishChargingEvent(info);
322 EXPECT_FALSE(ret);
323 info.SetChargeState(BatteryChargeState::CHARGE_STATE_FULL);
324 MockBatteryCommonEventManager::SetBoolReturnValue(false);
325 ret = g_batteryNotify->PublishChargingEvent(info);
326 EXPECT_TRUE(ret);
327 info.SetChargeState(BatteryChargeState::CHARGE_STATE_NONE);
328 MockBatteryCommonEventManager::SetBoolReturnValue(true);
329 ret = g_batteryNotify->PublishChargingEvent(info);
330 EXPECT_TRUE(ret);
331 info.SetChargeState(BatteryChargeState::CHARGE_STATE_ENABLE);
332 MockBatteryCommonEventManager::SetBoolReturnValue(true);
333 ret = g_batteryNotify->PublishChargingEvent(info);
334 EXPECT_TRUE(ret);
335 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2009 end.");
336 }
337
338 /**
339 * @tc.name: BatteryEventTestPart2010
340 * @tc.desc: test PublishDischargingEvent function
341 * @tc.type: FUNC
342 */
343 HWTEST_F(BatteryEventTestPart2, BatteryEventTestPart2010, TestSize.Level1)
344 {
345 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2010 start.");
346 BatteryInfo info;
347 info.SetChargeState(BatteryChargeState::CHARGE_STATE_ENABLE);
348 MockBatteryCommonEventManager::SetBoolReturnValue(false);
349 bool ret = g_batteryNotify->PublishDischargingEvent(info);
350 EXPECT_TRUE(ret);
351 info.SetChargeState(BatteryChargeState::CHARGE_STATE_NONE);
352 MockBatteryCommonEventManager::SetBoolReturnValue(false);
353 ret = g_batteryNotify->PublishDischargingEvent(info);
354 EXPECT_FALSE(ret);
355 info.SetChargeState(BatteryChargeState::CHARGE_STATE_DISABLE);
356 MockBatteryCommonEventManager::SetBoolReturnValue(false);
357 ret = g_batteryNotify->PublishDischargingEvent(info);
358 EXPECT_TRUE(ret);
359 info.SetChargeState(BatteryChargeState::CHARGE_STATE_FULL);
360 MockBatteryCommonEventManager::SetBoolReturnValue(true);
361 ret = g_batteryNotify->PublishDischargingEvent(info);
362 EXPECT_TRUE(ret);
363 info.SetChargeState(BatteryChargeState::CHARGE_STATE_NONE);
364 MockBatteryCommonEventManager::SetBoolReturnValue(true);
365 ret = g_batteryNotify->PublishDischargingEvent(info);
366 EXPECT_TRUE(ret);
367 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2010 end.");
368 }
369
370 /**
371 * @tc.name: BatteryEventTestPart2011
372 * @tc.desc: test PublishCustomEvent function
373 * @tc.type: FUNC
374 */
375 HWTEST_F(BatteryEventTestPart2, BatteryEventTestPart2011, TestSize.Level1)
376 {
377 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2011 start.");
378 BatteryInfo info;
379 info.SetChargeState(BatteryChargeState::CHARGE_STATE_ENABLE);
380 MockBatteryCommonEventManager::SetBoolReturnValue(false);
381 bool ret = g_batteryNotify->PublishCustomEvent(info, "usual.event.BatteryEventTestPart2011");
382 EXPECT_FALSE(ret);
383 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2011 end.");
384 }
385
386 /**
387 * @tc.name: BatteryEventTestPart2012
388 * @tc.desc: test PublishChargeTypeChangedEvent function
389 * @tc.type: FUNC
390 */
391 HWTEST_F(BatteryEventTestPart2, BatteryEventTestPart2012, TestSize.Level1)
392 {
393 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2012 start.");
394 BatteryInfo info;
395 info.SetChargeType(ChargeType::WIRED_NORMAL);
396 g_batteryNotify->batteryInfoChargeType_ = ChargeType::NONE;
397 MockBatteryCommonEventManager::SetBoolReturnValue(false);
398 bool ret = g_batteryNotify->PublishChargeTypeChangedEvent(info);
399 EXPECT_FALSE(ret);
400 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTestPart2012 end.");
401 }