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 */
61 HWTEST_F(BatteryEventTest, BatteryEventTest001, TestSize.Level1)
62 {
63 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTest001 function start!");
64 BatteryInfo info;
65 bool ret = g_batteryNotify->PublishLowEvent(info);
66 EXPECT_TRUE(ret);
67 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTest001 function end!");
68 }
69
70 /**
71 * @tc.name: BatteryEventTest002
72 * @tc.desc: test PublishCustomEvent function
73 * @tc.type: FUNC
74 */
75 HWTEST_F(BatteryEventTest, BatteryEventTest002, TestSize.Level1)
76 {
77 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTest002 function 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 function end!");
82 }
83
84 /**
85 * @tc.name: BatteryEventTest003
86 * @tc.desc: test HandleNotification function
87 * @tc.type: FUNC
88 */
89 HWTEST_F(BatteryEventTest, BatteryEventTest003, TestSize.Level1)
90 {
91 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTest003 function start!");
92 std::string popupName = "popup_test003";
93 bool ret = g_batteryNotify->HandleNotification(popupName);
94 BATTERY_HILOGI(LABEL_TEST, "HandleNotification ret[%d]", static_cast<int32_t>(ret));
95 #ifndef BATTERY_SUPPORT_NOTIFICATION
96 EXPECT_TRUE(ret);
97 #endif
98 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTest003 function end!");
99 }
100
101 /**
102 * @tc.name: BatteryEventTest004
103 * @tc.desc: test HandleNotification function
104 * @tc.type: FUNC
105 */
106 HWTEST_F(BatteryEventTest, BatteryEventTest004, TestSize.Level1)
107 {
108 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTest004 function start!");
109 BatteryInfo info;
110 std::string ueventName = "notification_test004";
111 info.SetUevent(ueventName);
112 bool ret = g_batteryNotify->PublishChangedEvent(info);
113 EXPECT_TRUE(ret);
114 std::string popupName = "popup_test004";
115 ret = g_batteryNotify->HandleNotification(popupName);
116 BATTERY_HILOGI(LABEL_TEST, "HandleNotification ret[%d]", static_cast<int32_t>(ret));
117 #ifndef BATTERY_SUPPORT_NOTIFICATION
118 EXPECT_TRUE(ret);
119 #endif
120 BATTERY_HILOGI(LABEL_TEST, "BatteryEventTest004 function end!");
121 }