1 /*
2 * Copyright (c) 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_notification_test.h"
17 #ifdef GTEST
18 #define private public
19 #define protected public
20 #endif
21
22 #include "notification_manager.h"
23 #include "notification_locale.h"
24 #include "battery_notify.h"
25 #include <string>
26 #include <memory>
27 #include "battery_log.h"
28 using namespace testing::ext;
29
30 namespace {
31 std::shared_ptr<OHOS::PowerMgr::BatteryNotify> g_batteryServiceNotify = nullptr;
32 } // namespace
33
34 namespace OHOS {
35 namespace PowerMgr {
36
37 BatteryInfo* g_batteryServiceInfo;
38
SetUpTestCase()39 void BatteryNotificationTest::SetUpTestCase()
40 {
41 g_batteryServiceNotify = std::make_shared<BatteryNotify>();
42 }
43
SetUp()44 void BatteryNotificationTest::SetUp()
45 {
46 g_batteryServiceInfo = new BatteryInfo();
47 }
48
TearDown()49 void BatteryNotificationTest::TearDown()
50 {
51 if (g_batteryServiceInfo != nullptr) {
52 delete g_batteryServiceInfo;
53 g_batteryServiceInfo = nullptr;
54 }
55 }
56
57 /**
58 * @tc.name: BatteryNotification001
59 * @tc.desc: Test HandleNotification
60 * @tc.type: FUNC
61 */
62 HWTEST_F(BatteryNotificationTest, BatteryNotification001, TestSize.Level1)
63 {
64 BATTERY_HILOGI(LABEL_TEST, "BatteryNotification001 function start!");
65 g_batteryServiceInfo->SetUevent("battery_notification_test001");
66 EXPECT_EQ(g_batteryServiceInfo->GetUevent(), "battery_notification_test001");
67 std::string popupName = "BatteryNotification001_popup";
68 bool ret = g_batteryServiceNotify->HandleNotification(popupName);
69 BATTERY_HILOGI(LABEL_TEST, "HandleNotification ret[%d]", static_cast<int32_t>(ret));
70 #ifndef BATTERY_SUPPORT_NOTIFICATION
71 EXPECT_TRUE(ret);
72 #endif
73 BATTERY_HILOGI(LABEL_TEST, "BatteryNotification001 function end!");
74 }
75
76 /**
77 * @tc.name: BatteryNotification002
78 * @tc.desc: Test HandleNotification
79 * @tc.type: FUNC
80 */
81 HWTEST_F(BatteryNotificationTest, BatteryNotification002, TestSize.Level1)
82 {
83 BATTERY_HILOGI(LABEL_TEST, "BatteryNotification002 function start!");
84 std::string popupName = "BatteryNotification002_popup";
85 bool ret = g_batteryServiceNotify->HandleNotification(popupName);
86 BATTERY_HILOGI(LABEL_TEST, "HandleNotification ret[%d]", static_cast<int32_t>(ret));
87 #ifndef BATTERY_SUPPORT_NOTIFICATION
88 EXPECT_TRUE(ret);
89 #endif
90 std::shared_ptr<NotificationManager> notificationMgr = std::make_shared<NotificationManager>();
91 EXPECT_TRUE(notificationMgr != nullptr);
92 notificationMgr->HandleNotification(popupName, 0, {});
93 notificationMgr->HandleNotification(popupName, 1, {});
94 EXPECT_TRUE(notificationMgr->notificationMap_.size() == 0);
95 BATTERY_HILOGI(LABEL_TEST, "BatteryNotification002 function end!");
96 }
97
98 /**
99 * @tc.name: BatteryNotification003
100 * @tc.desc: Test HandleNotification
101 * @tc.type: FUNC
102 */
103 HWTEST_F(BatteryNotificationTest, BatteryNotification003, TestSize.Level1)
104 {
105 BATTERY_HILOGI(LABEL_TEST, "BatteryNotification003 function start!");
106 std::string popupName = "battery_notification_test003";
107 bool ret = g_batteryServiceNotify->HandleNotification(popupName);
108 BATTERY_HILOGI(LABEL_TEST, "HandleNotification ret[%d]", static_cast<int32_t>(ret));
109 #ifndef BATTERY_SUPPORT_NOTIFICATION
110 EXPECT_TRUE(ret);
111 #endif
112 std::shared_ptr<NotificationManager> notificationMgr = std::make_shared<NotificationManager>();
113 EXPECT_TRUE(notificationMgr != nullptr);
114 BatteryConfig::NotificationConf nCfg;
115 nCfg.name = "BatteryNotification003_popup";
116 notificationMgr->PublishNotification(nCfg);
117 EXPECT_TRUE(notificationMgr->notificationMap_.size() == 1);
118 notificationMgr->CancleNotification("BatteryNotification003_popup");
119 EXPECT_TRUE(notificationMgr->notificationMap_.size() == 0);
120 BATTERY_HILOGI(LABEL_TEST, "BatteryNotification003 function end!");
121 }
122
123 /**
124 * @tc.name: BatteryNotification004
125 * @tc.desc: Test HandleNotification
126 * @tc.type: FUNC
127 */
128 HWTEST_F(BatteryNotificationTest, BatteryNotification004, TestSize.Level1)
129 {
130 BATTERY_HILOGI(LABEL_TEST, "BatteryNotification004 function start!");
131 std::string popupName = "battery_notification_test004";
132 bool ret = g_batteryServiceNotify->HandleNotification(popupName);
133 BATTERY_HILOGI(LABEL_TEST, "HandleNotification ret[%d]", static_cast<int32_t>(ret));
134 #ifndef BATTERY_SUPPORT_NOTIFICATION
135 EXPECT_TRUE(ret);
136 #endif
137 std::shared_ptr<NotificationManager> notificationMgr = std::make_shared<NotificationManager>();
138 EXPECT_TRUE(notificationMgr != nullptr);
139 BatteryConfig::NotificationConf nCfg {
140 .name = "BatteryNotification004_popup",
141 .icon = "/etc/battery/battery_icon.png",
142 .text = "test",
143 .title = "test",
144 .firstButton = std::make_pair("", ""),
145 .secondButton = std::make_pair("", ""),
146 };
147 notificationMgr->PublishNotification(nCfg);
148 EXPECT_TRUE(notificationMgr->notificationMap_.size() == 1);
149 notificationMgr->CancleNotification("BatteryNotification004_popup");
150 EXPECT_TRUE(notificationMgr->notificationMap_.size() == 0);
151 BATTERY_HILOGI(LABEL_TEST, "BatteryNotification004 function end!");
152 }
153
154 /**
155 * @tc.name: BatteryNotification005
156 * @tc.desc: Test HandleNotification
157 * @tc.type: FUNC
158 */
159 HWTEST_F(BatteryNotificationTest, BatteryNotification005, TestSize.Level1)
160 {
161 BATTERY_HILOGI(LABEL_TEST, "BatteryNotification005 function start!");
162 std::string popupName = "battery_notification_test005";
163 bool ret = g_batteryServiceNotify->HandleNotification(popupName);
164 BATTERY_HILOGI(LABEL_TEST, "HandleNotification ret[%d]", static_cast<int32_t>(ret));
165 #ifndef BATTERY_SUPPORT_NOTIFICATION
166 EXPECT_TRUE(ret);
167 #endif
168 std::shared_ptr<NotificationManager> notificationMgr = std::make_shared<NotificationManager>();
169 EXPECT_TRUE(notificationMgr != nullptr);
170 BatteryConfig::NotificationConf nCfg {
171 .name = "BatteryNotification005_popup",
172 .icon = "/etc/battery/battery_icon.png",
173 .text = "test005",
174 .title = "test",
175 .firstButton = std::make_pair("Enable", "notification.battery.test_enable"),
176 .secondButton = std::make_pair("", ""),
177 };
178 notificationMgr->PublishNotification(nCfg);
179 EXPECT_TRUE(notificationMgr->notificationMap_.size() == 1);
180 notificationMgr->CancleNotification("BatteryNotification005_popup");
181 EXPECT_TRUE(notificationMgr->notificationMap_.size() == 0);
182 BATTERY_HILOGI(LABEL_TEST, "BatteryNotification005 function end!");
183 }
184
185 /**
186 * @tc.name: BatteryNotification006
187 * @tc.desc: Test HandleNotification
188 * @tc.type: FUNC
189 */
190 HWTEST_F(BatteryNotificationTest, BatteryNotification006, TestSize.Level1)
191 {
192 BATTERY_HILOGI(LABEL_TEST, "BatteryNotification006 function start!");
193 std::string popupName = "battery_notification_test006";
194 bool ret = g_batteryServiceNotify->HandleNotification(popupName);
195 BATTERY_HILOGI(LABEL_TEST, "HandleNotification ret[%d]", static_cast<int32_t>(ret));
196 #ifndef BATTERY_SUPPORT_NOTIFICATION
197 EXPECT_TRUE(ret);
198 #endif
199 std::shared_ptr<NotificationManager> notificationMgr = std::make_shared<NotificationManager>();
200 EXPECT_TRUE(notificationMgr != nullptr);
201 BatteryConfig::NotificationConf nCfg {
202 .name = "BatteryNotification006_popup",
203 .icon = "/etc/battery/battery_icon.png",
204 .text = "test006",
205 .title = "test",
206 .firstButton = std::make_pair("Enable", "notification.battery.test_enable"),
207 .secondButton = std::make_pair("Disable", "notification.battery.test_disable"),
208 };
209 notificationMgr->PublishNotification(nCfg);
210 EXPECT_TRUE(notificationMgr->notificationMap_.size() == 1);
211 notificationMgr->CancleNotification("BatteryNotification006_popup");
212 EXPECT_TRUE(notificationMgr->notificationMap_.size() == 0);
213 BATTERY_HILOGI(LABEL_TEST, "BatteryNotification006 function end!");
214 }
215 }
216 }