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_event_system_test.h"
17
18 #include "battery_info.h"
19 #include "battery_srv_client.h"
20 #include "battery_log.h"
21 #include "common_event_data.h"
22 #include "common_event_manager.h"
23 #include "common_event_subscribe_info.h"
24 #include "common_event_subscriber.h"
25 #include "common_event_support.h"
26 #include "power_common.h"
27 #include "securec.h"
28 #include "test_utils.h"
29 #include <condition_variable>
30 #include <datetime_ex.h>
31 #include <fcntl.h>
32 #include <gtest/gtest.h>
33 #include <if_system_ability_manager.h>
34 #include <iostream>
35 #include <ipc_skeleton.h>
36 #include <mutex>
37 #include <string_ex.h>
38 #include <unistd.h>
39
40 using namespace testing::ext;
41 using namespace std;
42 using namespace OHOS;
43 using namespace OHOS::AAFwk;
44 using namespace OHOS::EventFwk;
45 using namespace OHOS::PowerMgr;
46
47 namespace {
48 std::condition_variable g_cv;
49 std::mutex g_mtx;
50 std::string g_action = "";
51 int32_t g_capacity = -1;
52 int32_t g_chargeState = -1;
53 int32_t g_capacityLevel = -1;
54 constexpr int64_t TIME_OUT = 1;
55 bool g_isMock = false;
56 const int32_t RETRY_TIMES = 2;
57 const std::string MOCK_BATTERY_PATH = "/data/service/el0/battery/";
58 const std::string KEY_CAPACITY = BatteryInfo::COMMON_EVENT_KEY_CAPACITY;
59 const std::string KEY_CAPACITY_LEVEL = BatteryInfo::COMMON_EVENT_KEY_CAPACITY_LEVEL;
60 const std::string KEY_CHARGE_STATE = BatteryInfo::COMMON_EVENT_KEY_CHARGE_STATE;
61 const std::string KEY_PLUGGED_MAX_VOLTAGE = BatteryInfo::COMMON_EVENT_KEY_PLUGGED_MAX_VOLTAGE;
62 } // namespace
63
64 class CommonEventBatteryChangedTest : public CommonEventSubscriber {
65 public:
66 CommonEventBatteryChangedTest() = default;
67 explicit CommonEventBatteryChangedTest(const CommonEventSubscribeInfo& subscriberInfo);
~CommonEventBatteryChangedTest()68 virtual ~CommonEventBatteryChangedTest() {};
69 virtual void OnReceiveEvent(const CommonEventData& data);
70 static shared_ptr<CommonEventBatteryChangedTest> RegisterEvent();
71 };
72
CommonEventBatteryChangedTest(const CommonEventSubscribeInfo & subscriberInfo)73 CommonEventBatteryChangedTest::CommonEventBatteryChangedTest(const CommonEventSubscribeInfo& subscriberInfo)
74 : CommonEventSubscriber(subscriberInfo)
75 {
76 }
77
78 class CommonEventBatteryLowTest : public CommonEventSubscriber {
79 public:
80 CommonEventBatteryLowTest() = default;
81 explicit CommonEventBatteryLowTest(const CommonEventSubscribeInfo& subscriberInfo);
~CommonEventBatteryLowTest()82 virtual ~CommonEventBatteryLowTest() {};
83 virtual void OnReceiveEvent(const CommonEventData& data);
84 static shared_ptr<CommonEventBatteryLowTest> RegisterEvent();
85 };
86
CommonEventBatteryLowTest(const CommonEventSubscribeInfo & subscriberInfo)87 CommonEventBatteryLowTest::CommonEventBatteryLowTest(const CommonEventSubscribeInfo& subscriberInfo)
88 : CommonEventSubscriber(subscriberInfo)
89 {
90 }
91
92 class CommonEventBatteryOkayTest : public CommonEventSubscriber {
93 public:
94 CommonEventBatteryOkayTest() = default;
95 explicit CommonEventBatteryOkayTest(const CommonEventSubscribeInfo& subscriberInfo);
~CommonEventBatteryOkayTest()96 virtual ~CommonEventBatteryOkayTest() {};
97 virtual void OnReceiveEvent(const CommonEventData& data);
98 static shared_ptr<CommonEventBatteryOkayTest> RegisterEvent();
99 };
100
CommonEventBatteryOkayTest(const CommonEventSubscribeInfo & subscriberInfo)101 CommonEventBatteryOkayTest::CommonEventBatteryOkayTest(const CommonEventSubscribeInfo& subscriberInfo)
102 : CommonEventSubscriber(subscriberInfo)
103 {
104 }
105
106 class CommonEventBatteryChargingTest : public CommonEventSubscriber {
107 public:
108 CommonEventBatteryChargingTest() = default;
109 explicit CommonEventBatteryChargingTest(const CommonEventSubscribeInfo& subscriberInfo);
~CommonEventBatteryChargingTest()110 virtual ~CommonEventBatteryChargingTest() {};
111 virtual void OnReceiveEvent(const CommonEventData& data);
112 static shared_ptr<CommonEventBatteryChargingTest> RegisterEvent();
113 };
114
CommonEventBatteryChargingTest(const CommonEventSubscribeInfo & subscriberInfo)115 CommonEventBatteryChargingTest::CommonEventBatteryChargingTest(const CommonEventSubscribeInfo& subscriberInfo)
116 : CommonEventSubscriber(subscriberInfo)
117 {
118 }
119
120 class CommonEventBatteryDischargingTest : public CommonEventSubscriber {
121 public:
122 CommonEventBatteryDischargingTest() = default;
123 explicit CommonEventBatteryDischargingTest(const CommonEventSubscribeInfo& subscriberInfo);
~CommonEventBatteryDischargingTest()124 virtual ~CommonEventBatteryDischargingTest() {};
125 virtual void OnReceiveEvent(const CommonEventData& data);
126 static shared_ptr<CommonEventBatteryDischargingTest> RegisterEvent();
127 };
128
CommonEventBatteryDischargingTest(const CommonEventSubscribeInfo & subscriberInfo)129 CommonEventBatteryDischargingTest::CommonEventBatteryDischargingTest(const CommonEventSubscribeInfo& subscriberInfo)
130 : CommonEventSubscriber(subscriberInfo)
131 {
132 }
133
134 class CommonEventBatteryDisconnectTest : public CommonEventSubscriber {
135 public:
136 CommonEventBatteryDisconnectTest() = default;
137 explicit CommonEventBatteryDisconnectTest(const CommonEventSubscribeInfo& subscriberInfo);
~CommonEventBatteryDisconnectTest()138 virtual ~CommonEventBatteryDisconnectTest() {};
139 virtual void OnReceiveEvent(const CommonEventData& data);
140 static shared_ptr<CommonEventBatteryDisconnectTest> RegisterEvent();
141 };
142
CommonEventBatteryDisconnectTest(const CommonEventSubscribeInfo & subscriberInfo)143 CommonEventBatteryDisconnectTest::CommonEventBatteryDisconnectTest(const CommonEventSubscribeInfo& subscriberInfo)
144 : CommonEventSubscriber(subscriberInfo)
145 {
146 }
147
148 class CommonEventBatteryConnectTest : public CommonEventSubscriber {
149 public:
150 CommonEventBatteryConnectTest() = default;
151 explicit CommonEventBatteryConnectTest(const CommonEventSubscribeInfo& subscriberInfo);
~CommonEventBatteryConnectTest()152 virtual ~CommonEventBatteryConnectTest() {};
153 virtual void OnReceiveEvent(const CommonEventData& data);
154 static shared_ptr<CommonEventBatteryConnectTest> RegisterEvent();
155 };
156
CommonEventBatteryConnectTest(const CommonEventSubscribeInfo & subscriberInfo)157 CommonEventBatteryConnectTest::CommonEventBatteryConnectTest(const CommonEventSubscribeInfo& subscriberInfo)
158 : CommonEventSubscriber(subscriberInfo)
159 {
160 }
161
162 class CommonEventChargeTypeChangedTest : public CommonEventSubscriber {
163 public:
164 CommonEventChargeTypeChangedTest() = default;
165 explicit CommonEventChargeTypeChangedTest(const CommonEventSubscribeInfo& subscriberInfo);
~CommonEventChargeTypeChangedTest()166 virtual ~CommonEventChargeTypeChangedTest() {};
167 virtual void OnReceiveEvent(const CommonEventData& data);
168 static shared_ptr<CommonEventChargeTypeChangedTest> RegisterEvent();
169 };
170
CommonEventChargeTypeChangedTest(const CommonEventSubscribeInfo & subscriberInfo)171 CommonEventChargeTypeChangedTest::CommonEventChargeTypeChangedTest(const CommonEventSubscribeInfo& subscriberInfo)
172 : CommonEventSubscriber(subscriberInfo)
173 {
174 }
175
176 class CommonEventDumpCapacityTest : public CommonEventSubscriber {
177 public:
178 CommonEventDumpCapacityTest() = default;
179 explicit CommonEventDumpCapacityTest(const CommonEventSubscribeInfo& subscriberInfo);
~CommonEventDumpCapacityTest()180 virtual ~CommonEventDumpCapacityTest() {};
181 virtual void OnReceiveEvent(const CommonEventData& data);
182 static shared_ptr<CommonEventDumpCapacityTest> RegisterEvent();
183 };
184
CommonEventDumpCapacityTest(const CommonEventSubscribeInfo & subscriberInfo)185 CommonEventDumpCapacityTest::CommonEventDumpCapacityTest(const CommonEventSubscribeInfo& subscriberInfo)
186 : CommonEventSubscriber(subscriberInfo)
187 {
188 }
189
OnReceiveEvent(const CommonEventData & data)190 void CommonEventBatteryChangedTest::OnReceiveEvent(const CommonEventData& data)
191 {
192 g_action = data.GetWant().GetAction();
193 g_cv.notify_one();
194 }
195
OnReceiveEvent(const CommonEventData & data)196 void CommonEventBatteryLowTest::OnReceiveEvent(const CommonEventData& data)
197 {
198 g_action = data.GetWant().GetAction();
199 g_cv.notify_one();
200 }
201
OnReceiveEvent(const CommonEventData & data)202 void CommonEventBatteryOkayTest::OnReceiveEvent(const CommonEventData& data)
203 {
204 int defaultCapacity = -1;
205 int capacity = data.GetWant().GetIntParam(KEY_CAPACITY, defaultCapacity);
206 g_cv.notify_one();
207 EXPECT_EQ(capacity, static_cast<int32_t>(BatteryCapacityLevel::LEVEL_HIGH)) << "COMMON_EVENT_BATTERY_OKAY";
208 }
209
OnReceiveEvent(const CommonEventData & data)210 void CommonEventBatteryChargingTest::OnReceiveEvent(const CommonEventData& data)
211 {
212 int defaultChargeState = -1;
213 int chargeState = data.GetWant().GetIntParam(KEY_CHARGE_STATE, defaultChargeState);
214 g_cv.notify_one();
215 EXPECT_EQ(chargeState, static_cast<int32_t>(BatteryChargeState::CHARGE_STATE_ENABLE)) << "COMMON_EVENT_CHARGING";
216 }
217
OnReceiveEvent(const CommonEventData & data)218 void CommonEventBatteryDischargingTest::OnReceiveEvent(const CommonEventData& data)
219 {
220 int defaultChargeState = -1;
221 int chargeState = data.GetWant().GetIntParam(KEY_CHARGE_STATE, defaultChargeState);
222 g_cv.notify_one();
223 EXPECT_EQ(chargeState, static_cast<int32_t>(BatteryChargeState::CHARGE_STATE_NONE)) << "COMMON_EVENT_DISCHARGING";
224 }
225
OnReceiveEvent(const CommonEventData & data)226 void CommonEventBatteryDisconnectTest::OnReceiveEvent(const CommonEventData& data)
227 {
228 int defaultMaxVoltage = -1;
229 int maxVoltage = data.GetWant().GetIntParam(KEY_PLUGGED_MAX_VOLTAGE, defaultMaxVoltage);
230 g_cv.notify_one();
231 EXPECT_NE(maxVoltage, static_cast<int32_t>(BatteryPluggedType::PLUGGED_TYPE_NONE))
232 << "COMMON_EVENT_POWER_DISCONNECTED";
233 }
234
OnReceiveEvent(const CommonEventData & data)235 void CommonEventBatteryConnectTest::OnReceiveEvent(const CommonEventData& data)
236 {
237 int defaultMaxVoltage = -1;
238 int maxVoltage = data.GetWant().GetIntParam(KEY_PLUGGED_MAX_VOLTAGE, defaultMaxVoltage);
239 g_cv.notify_one();
240 EXPECT_NE(maxVoltage, static_cast<int32_t>(BatteryPluggedType::PLUGGED_TYPE_USB)) << "COMMON_EVENT_POWER_CONNECTED";
241 }
242
OnReceiveEvent(const CommonEventData & data)243 void CommonEventChargeTypeChangedTest::OnReceiveEvent(const CommonEventData& data)
244 {
245 g_action = data.GetWant().GetAction();
246 g_cv.notify_one();
247 }
248
OnReceiveEvent(const CommonEventData & data)249 void CommonEventDumpCapacityTest::OnReceiveEvent(const CommonEventData& data)
250 {
251 int defaultCapacity = -1;
252 int defaultCapacityLevel = -1;
253 int defaultChargeState = -1;
254 g_capacity = data.GetWant().GetIntParam(KEY_CAPACITY, defaultCapacity);
255 g_capacityLevel = data.GetWant().GetIntParam(KEY_CAPACITY_LEVEL, defaultCapacityLevel);
256 g_chargeState = data.GetWant().GetIntParam(KEY_CHARGE_STATE, defaultChargeState);
257 g_cv.notify_one();
258 }
259
RegisterEvent()260 shared_ptr<CommonEventBatteryChangedTest> CommonEventBatteryChangedTest::RegisterEvent()
261 {
262 bool succeed = false;
263 MatchingSkills matchingSkills;
264 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
265 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
266 auto subscriberPtr = std::make_shared<CommonEventBatteryChangedTest>(subscribeInfo);
267 for (int32_t tryTimes = 0; tryTimes < RETRY_TIMES; tryTimes++) {
268 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
269 }
270 if (!succeed) {
271 return nullptr;
272 }
273 return subscriberPtr;
274 }
275
RegisterEvent()276 shared_ptr<CommonEventBatteryLowTest> CommonEventBatteryLowTest::RegisterEvent()
277 {
278 bool succeed = false;
279 MatchingSkills matchingSkills;
280 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BATTERY_LOW);
281 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
282 auto subscriberPtr = std::make_shared<CommonEventBatteryLowTest>(subscribeInfo);
283 for (int32_t tryTimes = 0; tryTimes < RETRY_TIMES; tryTimes++) {
284 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
285 }
286 if (!succeed) {
287 return nullptr;
288 }
289 return subscriberPtr;
290 }
291
RegisterEvent()292 shared_ptr<CommonEventBatteryOkayTest> CommonEventBatteryOkayTest::RegisterEvent()
293 {
294 bool succeed = false;
295 MatchingSkills matchingSkills;
296 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BATTERY_OKAY);
297 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
298 auto subscriberPtr = std::make_shared<CommonEventBatteryOkayTest>(subscribeInfo);
299 for (int32_t tryTimes = 0; tryTimes < RETRY_TIMES; tryTimes++) {
300 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
301 }
302 if (!succeed) {
303 return nullptr;
304 }
305 return subscriberPtr;
306 }
307
RegisterEvent()308 shared_ptr<CommonEventBatteryChargingTest> CommonEventBatteryChargingTest::RegisterEvent()
309 {
310 bool succeed = false;
311 MatchingSkills matchingSkills;
312 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CHARGING);
313 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
314 auto subscriberPtr = std::make_shared<CommonEventBatteryChargingTest>(subscribeInfo);
315 for (int32_t tryTimes = 0; tryTimes < RETRY_TIMES; tryTimes++) {
316 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
317 }
318 if (!succeed) {
319 return nullptr;
320 }
321 return subscriberPtr;
322 }
323
RegisterEvent()324 shared_ptr<CommonEventBatteryDischargingTest> CommonEventBatteryDischargingTest::RegisterEvent()
325 {
326 bool succeed = false;
327 MatchingSkills matchingSkills;
328 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DISCHARGING);
329 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
330 auto subscriberPtr = std::make_shared<CommonEventBatteryDischargingTest>(subscribeInfo);
331 for (int32_t tryTimes = 0; tryTimes < RETRY_TIMES; tryTimes++) {
332 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
333 }
334 if (!succeed) {
335 return nullptr;
336 }
337 return subscriberPtr;
338 }
339
RegisterEvent()340 shared_ptr<CommonEventBatteryDisconnectTest> CommonEventBatteryDisconnectTest::RegisterEvent()
341 {
342 bool succeed = false;
343 MatchingSkills matchingSkills;
344 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
345 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
346 auto subscriberPtr = std::make_shared<CommonEventBatteryDisconnectTest>(subscribeInfo);
347 for (int32_t tryTimes = 0; tryTimes < RETRY_TIMES; tryTimes++) {
348 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
349 }
350 if (!succeed) {
351 return nullptr;
352 }
353 return subscriberPtr;
354 }
355
RegisterEvent()356 shared_ptr<CommonEventBatteryConnectTest> CommonEventBatteryConnectTest::RegisterEvent()
357 {
358 bool succeed = false;
359 MatchingSkills matchingSkills;
360 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
361 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
362 auto subscriberPtr = std::make_shared<CommonEventBatteryConnectTest>(subscribeInfo);
363 for (int32_t tryTimes = 0; tryTimes < RETRY_TIMES; tryTimes++) {
364 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
365 }
366 if (!succeed) {
367 return nullptr;
368 }
369 return subscriberPtr;
370 }
371
RegisterEvent()372 shared_ptr<CommonEventChargeTypeChangedTest> CommonEventChargeTypeChangedTest::RegisterEvent()
373 {
374 bool succeed = false;
375 MatchingSkills matchingSkills;
376 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CHARGE_TYPE_CHANGED);
377 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
378 auto subscriberPtr = std::make_shared<CommonEventChargeTypeChangedTest>(subscribeInfo);
379 for (int32_t tryTimes = 0; tryTimes < RETRY_TIMES; tryTimes++) {
380 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
381 }
382 if (!succeed) {
383 return nullptr;
384 }
385 return subscriberPtr;
386 }
387
RegisterEvent()388 shared_ptr<CommonEventDumpCapacityTest> CommonEventDumpCapacityTest::RegisterEvent()
389 {
390 bool succeed = false;
391 MatchingSkills matchingSkills;
392 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
393 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
394 auto subscriberPtr = std::make_shared<CommonEventDumpCapacityTest>(subscribeInfo);
395 for (int32_t tryTimes = 0; tryTimes < RETRY_TIMES; tryTimes++) {
396 succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
397 }
398 if (!succeed) {
399 return nullptr;
400 }
401 return subscriberPtr;
402 }
403
SetUpTestCase(void)404 void BatteryEventSystemTest::SetUpTestCase(void)
405 {
406 g_isMock = TestUtils::IsMock();
407 GTEST_LOG_(INFO) << " g_isMock: " << g_isMock;
408 }
409
TearDownTestCase(void)410 void BatteryEventSystemTest::TearDownTestCase(void)
411 {
412 g_isMock = false;
413 TestUtils::ResetOnline();
414 }
415
416 namespace {
417
418 /*
419 * @tc.number: BatteryEventSystemTest001
420 * @tc.name: BatteryEventSystemTest
421 * @tc.desc: Verify the receive the common event
422 */
423 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest001, TestSize.Level0)
424 {
425 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest001 function start!");
426 shared_ptr<CommonEventBatteryLowTest> subscriber = CommonEventBatteryLowTest::RegisterEvent();
427 if (g_isMock) {
428 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest001 g_isMock 1.");
429 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/capacity", "100");
430 system("hidumper -s 3302 -a -r");
431 sleep(1);
432 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/capacity", "5");
433 system("hidumper -s 3302 -a -r");
434 std::unique_lock<std::mutex> lck(g_mtx);
435 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
436 g_cv.notify_one();
437 }
438 EXPECT_EQ(CommonEventSupport::COMMON_EVENT_BATTERY_LOW, g_action);
439 }
440 CommonEventManager::UnSubscribeCommonEvent(subscriber);
441 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest001 function end!");
442 }
443
444 /*
445 * @tc.number: BatteryEventSystemTest002
446 * @tc.name: BatteryEventSystemTest
447 * @tc.desc: Verify the receive the common event
448 */
449 #ifdef ENABLE_TEST
450 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest002, TestSize.Level0)
451 {
452 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest002 function start!");
453 shared_ptr<CommonEventBatteryChangedTest> subscriber = CommonEventBatteryChangedTest::RegisterEvent();
454 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/capacity", "40");
455 system("hidumper -s 3302 -a -r");
456 std::unique_lock<std::mutex> lck(g_mtx);
457 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
458 g_cv.notify_one();
459 }
460 EXPECT_EQ(CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED, g_action);
461 CommonEventManager::UnSubscribeCommonEvent(subscriber);
462 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest002 function end!");
463 }
464 #endif
465
466 /*
467 * @tc.number: BatteryEventSystemTest003
468 * @tc.name: BatteryEventSystemTest
469 * @tc.desc: Verify the receive the common event
470 * @tc.require: issueI6KRS8
471 */
472 #ifndef BATTERY_USER_VERSION
473 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest003, TestSize.Level0)
474 {
475 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest003 function start!");
476 shared_ptr<CommonEventBatteryChargingTest> subscriber = CommonEventBatteryChargingTest::RegisterEvent();
477 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/type", "Charging");
478 system("hidumper -s 3302 -a -r");
479 std::unique_lock<std::mutex> lck(g_mtx);
480 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
481 g_cv.notify_one();
482 }
483 auto ret = CommonEventManager::UnSubscribeCommonEvent(subscriber);
484 EXPECT_TRUE(ret);
485 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest003 function end!");
486 }
487 #endif
488
489 /*
490 * @tc.number: BatteryEventSystemTest004
491 * @tc.name: BatteryEventSystemTest
492 * @tc.desc: Verify the receive the common event
493 * @tc.require: issueI6KRS8
494 */
495 #ifndef BATTERY_USER_VERSION
496 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest004, TestSize.Level0)
497 {
498 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest004 function start!");
499 shared_ptr<CommonEventBatteryDischargingTest> subscriber = CommonEventBatteryDischargingTest::RegisterEvent();
500 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/type", "DisCharging");
501 system("hidumper -s 3302 -a -r");
502 std::unique_lock<std::mutex> lck(g_mtx);
503 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
504 g_cv.notify_one();
505 }
506 auto ret = CommonEventManager::UnSubscribeCommonEvent(subscriber);
507 EXPECT_TRUE(ret);
508 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest004 function end!");
509 }
510 #endif
511
512 /*
513 * @tc.number: BatteryEventSystemTest005
514 * @tc.name: BatteryEventSystemTest
515 * @tc.desc: Verify the receive the common event
516 * @tc.require: issueI6KRS8
517 */
518 #ifndef BATTERY_USER_VERSION
519 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest005, TestSize.Level0)
520 {
521 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest005 function start!");
522 shared_ptr<CommonEventBatteryOkayTest> subscriber = CommonEventBatteryOkayTest::RegisterEvent();
523 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/capacity", "90");
524 system("hidumper -s 3302 -a -r");
525 std::unique_lock<std::mutex> lck(g_mtx);
526 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
527 g_cv.notify_one();
528 }
529 auto ret = CommonEventManager::UnSubscribeCommonEvent(subscriber);
530 EXPECT_TRUE(ret);
531 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest005 function end!");
532 }
533 #endif
534
535 /*
536 * @tc.number: BatteryEventSystemTest006
537 * @tc.name: BatteryEventSystemTest
538 * @tc.desc: Verify the receive the common event
539 * @tc.require: issueI6KRS8
540 */
541 #ifndef BATTERY_USER_VERSION
542 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest006, TestSize.Level0)
543 {
544 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest006 function start!");
545 shared_ptr<CommonEventBatteryDisconnectTest> subscriber = CommonEventBatteryDisconnectTest::RegisterEvent();
546 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/ohos_charger/type", "Disconnect");
547 system("hidumper -s 3302 -a -r");
548 std::unique_lock<std::mutex> lck(g_mtx);
549 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
550 g_cv.notify_one();
551 }
552 auto ret = CommonEventManager::UnSubscribeCommonEvent(subscriber);
553 EXPECT_TRUE(ret);
554 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest006 function end!");
555 }
556 #endif
557
558 /*
559 * @tc.number: BatteryEventSystemTest007
560 * @tc.name: BatteryEventSystemTest
561 * @tc.desc: Verify the receive the common event
562 * @tc.require: issueI6KRS8
563 */
564 #ifndef BATTERY_USER_VERSION
565 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest007, TestSize.Level0)
566 {
567 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest007 function start!");
568 shared_ptr<CommonEventBatteryConnectTest> subscriber = CommonEventBatteryConnectTest::RegisterEvent();
569 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/ohos_charger/type", "USB");
570 system("hidumper -s 3302 -a -r");
571 std::unique_lock<std::mutex> lck(g_mtx);
572 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
573 g_cv.notify_one();
574 }
575 auto ret = CommonEventManager::UnSubscribeCommonEvent(subscriber);
576 EXPECT_TRUE(ret);
577 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest007 function end!");
578 }
579 #endif
580
581 /*
582 * @tc.number: BatteryEventSystemTest008
583 * @tc.name: BatteryEventSystemTest
584 * @tc.desc: Verify the receive the common event
585 * @tc.require: issueI6KRS8
586 */
587 #ifndef BATTERY_USER_VERSION
588 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest008, TestSize.Level0)
589 {
590 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest008 function start!");
591 shared_ptr<CommonEventChargeTypeChangedTest> subscriber = CommonEventChargeTypeChangedTest::RegisterEvent();
592 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/charge_type", "1");
593 system("hidumper -s 3302 -a -r");
594 std::unique_lock<std::mutex> lck(g_mtx);
595 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
596 g_cv.notify_one();
597 }
598 auto ret = CommonEventManager::UnSubscribeCommonEvent(subscriber);
599 EXPECT_TRUE(ret);
600 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest008 function end!");
601 }
602 #endif
603
604 /*
605 * @tc.number: BatteryEventSystemTest009
606 * @tc.name: BatteryEventSystemTest
607 * @tc.desc: Test capacity and unplugged dump, verify the receive the common event
608 * @tc.require: issueI6Z8RB
609 */
610 #ifdef ENABLE_TEST
611 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest009, TestSize.Level0)
612 {
613 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest009 function start!");
614 shared_ptr<CommonEventDumpCapacityTest> subscriber = CommonEventDumpCapacityTest::RegisterEvent();
615 int32_t capacity = 2;
616 std::string baseCmdStr = "hidumper -s 3302 -a";
617 std::string cmdStr = baseCmdStr;
618 cmdStr.append(" \"--capacity ").append(ToString(capacity)).append("\"");
619 system(cmdStr.c_str());
620 std::unique_lock<std::mutex> lck(g_mtx);
621 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
622 g_cv.notify_one();
623 }
624 EXPECT_EQ(g_capacity, capacity);
625 EXPECT_EQ(capacity, BatterySrvClient::GetInstance().GetCapacity());
626 #ifndef PC_TEST
627 EXPECT_EQ(g_capacityLevel, static_cast<int32_t>(BatteryCapacityLevel::LEVEL_CRITICAL));
628 EXPECT_TRUE(BatteryCapacityLevel::LEVEL_CRITICAL == BatterySrvClient::GetInstance().GetCapacityLevel());
629 #endif
630
631 system("hidumper -s 3302 -a -u");
632 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
633 g_cv.notify_one();
634 }
635 EXPECT_EQ(g_chargeState, static_cast<int32_t>(BatteryChargeState::CHARGE_STATE_NONE));
636 EXPECT_TRUE(BatteryPluggedType::PLUGGED_TYPE_NONE == BatterySrvClient::GetInstance().GetPluggedType());
637 EXPECT_TRUE(BatteryChargeState::CHARGE_STATE_NONE == BatterySrvClient::GetInstance().GetChargingStatus());
638 EXPECT_EQ(g_capacity, capacity);
639 EXPECT_EQ(capacity, BatterySrvClient::GetInstance().GetCapacity());
640
641 capacity = 91;
642 cmdStr = baseCmdStr;
643 cmdStr.append(" \"--capacity ").append(ToString(capacity)).append("\"");
644 system(cmdStr.c_str());
645 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
646 g_cv.notify_one();
647 }
648 EXPECT_EQ(g_capacity, capacity);
649 EXPECT_EQ(capacity, BatterySrvClient::GetInstance().GetCapacity());
650 EXPECT_EQ(g_capacityLevel, static_cast<int32_t>(BatteryCapacityLevel::LEVEL_HIGH));
651 EXPECT_TRUE(BatteryCapacityLevel::LEVEL_HIGH == BatterySrvClient::GetInstance().GetCapacityLevel());
652 EXPECT_EQ(g_chargeState, static_cast<int32_t>(BatteryChargeState::CHARGE_STATE_NONE));
653 EXPECT_TRUE(BatteryPluggedType::PLUGGED_TYPE_NONE == BatterySrvClient::GetInstance().GetPluggedType());
654 EXPECT_TRUE(BatteryChargeState::CHARGE_STATE_NONE == BatterySrvClient::GetInstance().GetChargingStatus());
655
656 system("hidumper -s 3302 -a -r");
657 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
658 g_cv.notify_one();
659 }
660 EXPECT_EQ(g_capacity, BatterySrvClient::GetInstance().GetCapacity());
661 EXPECT_EQ(g_chargeState, static_cast<int32_t>(BatterySrvClient::GetInstance().GetChargingStatus()));
662 EXPECT_TRUE(CommonEventManager::UnSubscribeCommonEvent(subscriber));
663 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest009 function end!");
664 }
665 #endif
666 } // namespace
667