1 /*
2  * Copyright (c) 2022-2024 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_test.h"
17 #ifdef GTEST
18 #define private   public
19 #define protected public
20 #endif
21 
22 #include <iostream>
23 #include <string>
24 
25 #include "common_event_data.h"
26 #include "common_event_manager.h"
27 #include "common_event_publish_info.h"
28 #include "common_event_support.h"
29 #include "battery_notify.h"
30 #include "battery_log.h"
31 #include "test_utils.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 } // namespace
41 
SetUpTestCase()42 void BatteryEventTest::SetUpTestCase()
43 {
44     if (g_batteryNotify == nullptr) {
45         g_batteryNotify = make_shared<BatteryNotify>();
46     }
47 }
48 
TearDownTestCase()49 void BatteryEventTest::TearDownTestCase()
50 {
51     if (g_batteryNotify != nullptr) {
52         g_batteryNotify.reset();
53         g_batteryNotify = nullptr;
54     }
55 }
56 /**
57  * @tc.name: BatteryEventTest001
58  * @tc.desc: test PublishLowEvent function
59  * @tc.type: FUNC
60  */
HWTEST_F(BatteryEventTest, BatteryEventTest001, TestSize.Level1)61 HWTEST_F(BatteryEventTest, BatteryEventTest001, TestSize.Level1)
62 {
63     BATTERY_HILOGI(LABEL_TEST, "BatteryEventTest001 start.");
64     BatteryInfo info;
65     bool ret = g_batteryNotify->PublishLowEvent(info);
66     EXPECT_TRUE(ret);
67     BATTERY_HILOGI(LABEL_TEST, "BatteryEventTest001 end.");
68 }
69 
70 /**
71  * @tc.name: BatteryEventTest002
72  * @tc.desc: test PublishCustomEvent function
73  * @tc.type: FUNC
74  */
HWTEST_F(BatteryEventTest, BatteryEventTest002, TestSize.Level1)75 HWTEST_F(BatteryEventTest, BatteryEventTest002, TestSize.Level1)
76 {
77     BATTERY_HILOGI(LABEL_TEST, "BatteryEventTest002 start.");
78     BatteryInfo info;
79     bool ret = g_batteryNotify->PublishCustomEvent(info, "test.battery.custom.event");
80     EXPECT_TRUE(ret);
81     BATTERY_HILOGI(LABEL_TEST, "BatteryEventTest002 end.");
82 }