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 */
HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest001, TestSize.Level0)423 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest001, TestSize.Level0)
424 {
425 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest001 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", "5");
430 system("hidumper -s 3302 -a -r");
431 std::unique_lock<std::mutex> lck(g_mtx);
432 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
433 g_cv.notify_one();
434 }
435 EXPECT_EQ(CommonEventSupport::COMMON_EVENT_BATTERY_LOW, g_action);
436 }
437 CommonEventManager::UnSubscribeCommonEvent(subscriber);
438 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest001 end.");
439 }
440
441 /*
442 * @tc.number: BatteryEventSystemTest002
443 * @tc.name: BatteryEventSystemTest
444 * @tc.desc: Verify the receive the common event
445 */
446 #ifndef BATTERY_USER_VERSION
HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest002, TestSize.Level0)447 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest002, TestSize.Level0)
448 {
449 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest002 start.");
450 shared_ptr<CommonEventBatteryChangedTest> subscriber = CommonEventBatteryChangedTest::RegisterEvent();
451 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/capacity", "40");
452 system("hidumper -s 3302 -a -r");
453 std::unique_lock<std::mutex> lck(g_mtx);
454 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
455 g_cv.notify_one();
456 }
457 EXPECT_EQ(CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED, g_action);
458 CommonEventManager::UnSubscribeCommonEvent(subscriber);
459 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest002 end.");
460 }
461 #endif
462
463 /*
464 * @tc.number: BatteryEventSystemTest003
465 * @tc.name: BatteryEventSystemTest
466 * @tc.desc: Verify the receive the common event
467 * @tc.require: issueI6KRS8
468 */
469 #ifndef BATTERY_USER_VERSION
HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest003, TestSize.Level0)470 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest003, TestSize.Level0)
471 {
472 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest003 start.");
473 shared_ptr<CommonEventBatteryChargingTest> subscriber = CommonEventBatteryChargingTest::RegisterEvent();
474 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/type", "Charging");
475 system("hidumper -s 3302 -a -r");
476 std::unique_lock<std::mutex> lck(g_mtx);
477 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
478 g_cv.notify_one();
479 }
480 auto ret = CommonEventManager::UnSubscribeCommonEvent(subscriber);
481 EXPECT_TRUE(ret);
482 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest003 end.");
483 }
484 #endif
485
486 /*
487 * @tc.number: BatteryEventSystemTest004
488 * @tc.name: BatteryEventSystemTest
489 * @tc.desc: Verify the receive the common event
490 * @tc.require: issueI6KRS8
491 */
492 #ifndef BATTERY_USER_VERSION
HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest004, TestSize.Level0)493 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest004, TestSize.Level0)
494 {
495 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest004 start.");
496 shared_ptr<CommonEventBatteryDischargingTest> subscriber = CommonEventBatteryDischargingTest::RegisterEvent();
497 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/type", "DisCharging");
498 system("hidumper -s 3302 -a -r");
499 std::unique_lock<std::mutex> lck(g_mtx);
500 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
501 g_cv.notify_one();
502 }
503 auto ret = CommonEventManager::UnSubscribeCommonEvent(subscriber);
504 EXPECT_TRUE(ret);
505 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest004 end.");
506 }
507 #endif
508
509 /*
510 * @tc.number: BatteryEventSystemTest005
511 * @tc.name: BatteryEventSystemTest
512 * @tc.desc: Verify the receive the common event
513 * @tc.require: issueI6KRS8
514 */
515 #ifndef BATTERY_USER_VERSION
HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest005, TestSize.Level0)516 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest005, TestSize.Level0)
517 {
518 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest005 start.");
519 shared_ptr<CommonEventBatteryOkayTest> subscriber = CommonEventBatteryOkayTest::RegisterEvent();
520 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/battery/capacity", "90");
521 system("hidumper -s 3302 -a -r");
522 std::unique_lock<std::mutex> lck(g_mtx);
523 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
524 g_cv.notify_one();
525 }
526 auto ret = CommonEventManager::UnSubscribeCommonEvent(subscriber);
527 EXPECT_TRUE(ret);
528 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest005 end.");
529 }
530 #endif
531
532 /*
533 * @tc.number: BatteryEventSystemTest006
534 * @tc.name: BatteryEventSystemTest
535 * @tc.desc: Verify the receive the common event
536 * @tc.require: issueI6KRS8
537 */
538 #ifndef BATTERY_USER_VERSION
HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest006, TestSize.Level0)539 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest006, TestSize.Level0)
540 {
541 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest006 start.");
542 shared_ptr<CommonEventBatteryDisconnectTest> subscriber = CommonEventBatteryDisconnectTest::RegisterEvent();
543 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/ohos_charger/type", "Disconnect");
544 system("hidumper -s 3302 -a -r");
545 std::unique_lock<std::mutex> lck(g_mtx);
546 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
547 g_cv.notify_one();
548 }
549 auto ret = CommonEventManager::UnSubscribeCommonEvent(subscriber);
550 EXPECT_TRUE(ret);
551 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest006 end.");
552 }
553 #endif
554
555 /*
556 * @tc.number: BatteryEventSystemTest007
557 * @tc.name: BatteryEventSystemTest
558 * @tc.desc: Verify the receive the common event
559 * @tc.require: issueI6KRS8
560 */
561 #ifndef BATTERY_USER_VERSION
HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest007, TestSize.Level0)562 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest007, TestSize.Level0)
563 {
564 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest007 start.");
565 shared_ptr<CommonEventBatteryConnectTest> subscriber = CommonEventBatteryConnectTest::RegisterEvent();
566 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/ohos_charger/type", "USB");
567 system("hidumper -s 3302 -a -r");
568 std::unique_lock<std::mutex> lck(g_mtx);
569 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
570 g_cv.notify_one();
571 }
572 auto ret = CommonEventManager::UnSubscribeCommonEvent(subscriber);
573 EXPECT_TRUE(ret);
574 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest007 end.");
575 }
576 #endif
577
578 /*
579 * @tc.number: BatteryEventSystemTest008
580 * @tc.name: BatteryEventSystemTest
581 * @tc.desc: Verify the receive the common event
582 * @tc.require: issueI6KRS8
583 */
584 #ifndef BATTERY_USER_VERSION
HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest008, TestSize.Level0)585 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest008, TestSize.Level0)
586 {
587 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest008 start.");
588 shared_ptr<CommonEventChargeTypeChangedTest> subscriber = CommonEventChargeTypeChangedTest::RegisterEvent();
589 TestUtils::WriteMock(MOCK_BATTERY_PATH + "/charge_type", "1");
590 system("hidumper -s 3302 -a -r");
591 std::unique_lock<std::mutex> lck(g_mtx);
592 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
593 g_cv.notify_one();
594 }
595 auto ret = CommonEventManager::UnSubscribeCommonEvent(subscriber);
596 EXPECT_TRUE(ret);
597 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest008 end.");
598 }
599 #endif
600
601 /*
602 * @tc.number: BatteryEventSystemTest009
603 * @tc.name: BatteryEventSystemTest
604 * @tc.desc: Test capacity and unplugged dump, verify the receive the common event
605 * @tc.require: issueI6Z8RB
606 */
607 #ifndef BATTERY_USER_VERSION
HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest009, TestSize.Level0)608 HWTEST_F(BatteryEventSystemTest, BatteryEventSystemTest009, TestSize.Level0)
609 {
610 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest009 start.");
611 shared_ptr<CommonEventDumpCapacityTest> subscriber = CommonEventDumpCapacityTest::RegisterEvent();
612 int32_t capacity = 2;
613 std::string baseCmdStr = "hidumper -s 3302 -a";
614 std::string cmdStr = baseCmdStr;
615 cmdStr.append(" \"--capacity ").append(ToString(capacity)).append("\"");
616 system(cmdStr.c_str());
617 std::unique_lock<std::mutex> lck(g_mtx);
618 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
619 g_cv.notify_one();
620 }
621 EXPECT_EQ(g_capacity, capacity);
622 EXPECT_EQ(capacity, BatterySrvClient::GetInstance().GetCapacity());
623 EXPECT_EQ(g_capacityLevel, static_cast<int32_t>(BatteryCapacityLevel::LEVEL_CRITICAL));
624 EXPECT_TRUE(BatteryCapacityLevel::LEVEL_CRITICAL == BatterySrvClient::GetInstance().GetCapacityLevel());
625
626 system("hidumper -s 3302 -a -u");
627 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
628 g_cv.notify_one();
629 }
630 EXPECT_EQ(g_chargeState, static_cast<int32_t>(BatteryChargeState::CHARGE_STATE_NONE));
631 EXPECT_TRUE(BatteryPluggedType::PLUGGED_TYPE_NONE == BatterySrvClient::GetInstance().GetPluggedType());
632 EXPECT_TRUE(BatteryChargeState::CHARGE_STATE_NONE == BatterySrvClient::GetInstance().GetChargingStatus());
633 EXPECT_EQ(g_capacity, capacity);
634 EXPECT_EQ(capacity, BatterySrvClient::GetInstance().GetCapacity());
635
636 capacity = 91;
637 cmdStr = baseCmdStr;
638 cmdStr.append(" \"--capacity ").append(ToString(capacity)).append("\"");
639 system(cmdStr.c_str());
640 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
641 g_cv.notify_one();
642 }
643 EXPECT_EQ(g_capacity, capacity);
644 EXPECT_EQ(capacity, BatterySrvClient::GetInstance().GetCapacity());
645 EXPECT_EQ(g_capacityLevel, static_cast<int32_t>(BatteryCapacityLevel::LEVEL_HIGH));
646 EXPECT_TRUE(BatteryCapacityLevel::LEVEL_HIGH == BatterySrvClient::GetInstance().GetCapacityLevel());
647 EXPECT_EQ(g_chargeState, static_cast<int32_t>(BatteryChargeState::CHARGE_STATE_NONE));
648 EXPECT_TRUE(BatteryPluggedType::PLUGGED_TYPE_NONE == BatterySrvClient::GetInstance().GetPluggedType());
649 EXPECT_TRUE(BatteryChargeState::CHARGE_STATE_NONE == BatterySrvClient::GetInstance().GetChargingStatus());
650
651 system("hidumper -s 3302 -a -r");
652 if (g_cv.wait_for(lck, std::chrono::seconds(TIME_OUT)) == std::cv_status::timeout) {
653 g_cv.notify_one();
654 }
655 EXPECT_EQ(g_capacity, BatterySrvClient::GetInstance().GetCapacity());
656 EXPECT_EQ(g_chargeState, static_cast<int32_t>(BatterySrvClient::GetInstance().GetChargingStatus()));
657 auto ret = CommonEventManager::UnSubscribeCommonEvent(subscriber);
658 EXPECT_TRUE(ret);
659 BATTERY_HILOGI(LABEL_TEST, "BatteryEventSystemTest009 end.");
660 }
661 #endif
662 } // namespace
663