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