• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "gtest/gtest.h"
17 #define private public
18 #include "notification_extension_wrapper.h"
19 #undef private
20 
21 using namespace testing;
22 using namespace testing::ext;
23 namespace OHOS {
24 namespace Notification {
25 const int32_t ACTIVE_DELETE = 0;
26 const int32_t PASSITIVE_DELETE = 1;
27 
28 static bool g_mockCalled = false;
29 static bool g_mockLocalSwitch = false;
30 static int g_mockUpdateByCancelReason = 0;
31 
MockSetLocalSwitch(bool status)32 static void MockSetLocalSwitch(bool status)
33 {
34     g_mockCalled = true;
35     g_mockLocalSwitch = status;
36 }
37 
MockUpdateByCancel(const std::vector<sptr<Notification>> & notifications,int deleteType)38 static void MockUpdateByCancel(const std::vector<sptr<Notification>>& notifications, int deleteType)
39 {
40     g_mockCalled = true;
41     g_mockUpdateByCancelReason = deleteType;
42 }
43 
44 class NotificationExtensionWrapperTest : public ::testing::Test {
45 protected:
SetUp()46     void SetUp() override {}
TearDown()47     void TearDown() override {}
SetUpTestCas()48     static void SetUpTestCas()
49     {
50         g_mockCalled = false;
51         g_mockLocalSwitch = false;
52         g_mockUpdateByCancelReason = 0;
53     }
TearDownTestCase()54     static void TearDownTestCase() {};
55 };
56 
57 HWTEST_F(NotificationExtensionWrapperTest, InitExtentionWrapper_Test, TestSize.Level0)
58 {
59     OHOS::Notification::ExtensionWrapper extensionWrapper;
60     extensionWrapper.InitExtentionWrapper();
61 #ifdef ENABLE_ANS_EXT_WRAPPER
62     // 验证extensionWrapperHandle_是否被正确初始化
63     EXPECT_NE(extensionWrapper.extensionWrapperHandle_, nullptr);
64 
65     // 验证syncAdditionConfig_是否被正确初始化
66     EXPECT_NE(extensionWrapper.syncAdditionConfig_, nullptr);
67 #else
68     EXPECT_EQ(extensionWrapper.extensionWrapperHandle_, nullptr);
69     EXPECT_EQ(extensionWrapper.syncAdditionConfig_, nullptr);
70 #endif
71 
72     // 验证localControl_、reminderControl_、bannerControl_是否被正确初始化
73 #ifdef ENABLE_ANS_ADDITIONAL_CONTROL
74     EXPECT_NE(extensionWrapper.localControl_, nullptr);
75     EXPECT_NE(extensionWrapper.reminderControl_, nullptr);
76     EXPECT_NE(extensionWrapper.bannerControl_, nullptr);
77 #endif
78 
79     // 验证initSummary_是否被正确初始化
80 #ifdef ENABLE_ANS_AGGREGATION
81     EXPECT_NE(extensionWrapper.initSummary_, nullptr);
82 #endif
83 }
84 
85 
86 HWTEST_F(NotificationExtensionWrapperTest, CheckIfSetlocalSwitch_001, TestSize.Level0)
87 {
88     // 创建ExtensionWrapper对象
89     ExtensionWrapper extensionWrapper;
90     // 设置extensionWrapperHandle_为nullptr
91     extensionWrapper.extensionWrapperHandle_ = nullptr;
92     // 调用待测函数
93     extensionWrapper.CheckIfSetlocalSwitch();
94     // 验证extensionWrapperHandle_仍然为nullptr
95     EXPECT_EQ(extensionWrapper.extensionWrapperHandle_, nullptr);
96 }
97 
98 HWTEST_F(NotificationExtensionWrapperTest, CheckIfSetlocalSwitch_002, TestSize.Level0)
99 {
100     // 创建ExtensionWrapper对象
101     ExtensionWrapper extensionWrapper;
102     // 设置extensionWrapperHandle_不为nullptr
103     extensionWrapper.extensionWrapperHandle_ = new int;
104     // 设置isRegisterDataSettingObserver为false
105     extensionWrapper.isRegisterDataSettingObserver = false;
106     // 调用待测函数
107     extensionWrapper.CheckIfSetlocalSwitch();
108     // 验证isRegisterDataSettingObserver为true
109     EXPECT_EQ(extensionWrapper.isRegisterDataSettingObserver, true);
110 }
111 
112 HWTEST_F(NotificationExtensionWrapperTest, CheckIfSetlocalSwitch_003, TestSize.Level0)
113 {
114     // 创建ExtensionWrapper对象
115     ExtensionWrapper extensionWrapper;
116     // 设置extensionWrapperHandle_不为nullptr
117     extensionWrapper.extensionWrapperHandle_ = new int;
118     // 设置isRegisterDataSettingObserver为true
119     extensionWrapper.isRegisterDataSettingObserver = true;
120     // 调用待测函数
121     extensionWrapper.CheckIfSetlocalSwitch();
122     // 验证isRegisterDataSettingObserver仍然为true
123     EXPECT_EQ(extensionWrapper.isRegisterDataSettingObserver, true);
124 }
125 
126 HWTEST_F(NotificationExtensionWrapperTest, SetlocalSwitch_False_Test, TestSize.Level0)
127 {
128     OHOS::Notification::ExtensionWrapper extensionWrapper;
129     std::string enable = "false";
130     extensionWrapper.setLocalSwitch_ = reinterpret_cast<ExtensionWrapper::SET_LOCAL_SWITCH>(&MockSetLocalSwitch);
131 
132     extensionWrapper.SetlocalSwitch(enable);
133 
134     EXPECT_TRUE(g_mockCalled);
135     EXPECT_FALSE(g_mockLocalSwitch);
136 }
137 
138 HWTEST_F(NotificationExtensionWrapperTest, SetlocalSwitch_True_Test, TestSize.Level0)
139 {
140     OHOS::Notification::ExtensionWrapper extensionWrapper;
141     std::string enable = "true";
142     extensionWrapper.setLocalSwitch_ = reinterpret_cast<ExtensionWrapper::SET_LOCAL_SWITCH>(&MockSetLocalSwitch);
143 
144     extensionWrapper.SetlocalSwitch(enable);
145 
146     EXPECT_TRUE(g_mockCalled);
147     EXPECT_TRUE(g_mockLocalSwitch);
148 }
149 
150 HWTEST_F(NotificationExtensionWrapperTest, SyncAdditionConfig_NullSyncAdditionConfig, TestSize.Level0)
151 {
152     ExtensionWrapper extensionWrapper;
153     extensionWrapper.syncAdditionConfig_ = nullptr;
154     ErrCode result = extensionWrapper.SyncAdditionConfig("key", "value");
155     EXPECT_EQ(result, 0);
156 }
157 
158 HWTEST_F(NotificationExtensionWrapperTest, SyncAdditionConfig_ValidSyncAdditionConfig, TestSize.Level0)
159 {
160     ExtensionWrapper extensionWrapper;
__anonf65ce0480102(const std::string& key, const std::string& value) 161     extensionWrapper.syncAdditionConfig_ = [](const std::string& key, const std::string& value) {
162         return 1;
163     };
164     ErrCode result = extensionWrapper.SyncAdditionConfig("key", "value");
165     EXPECT_EQ(result, 1);
166 }
167 
168 HWTEST_F(NotificationExtensionWrapperTest, UpdateByCancel_NullUpdateByCancel, TestSize.Level0)
169 {
170     // Arrange
171     ExtensionWrapper wrapper;
172     std::vector<sptr<Notification>> notifications;
173     int deleteReason = 1;
174     int expectedReason = 0;
175 
176     wrapper.UpdateByCancel(notifications, deleteReason);
177 
178     EXPECT_NE(expectedReason, deleteReason);
179 }
180 
181 HWTEST_F(NotificationExtensionWrapperTest, UpdateByCancel_Normal_Test, TestSize.Level0) {
182     ExtensionWrapper wrapper;
183     wrapper.updateByCancel_ = reinterpret_cast<ExtensionWrapper::UPDATE_BY_CANCEL>(&MockUpdateByCancel);
184 
185     std::vector<sptr<Notification>> notifications;
186     int deleteReason = 5;
187 
188     wrapper.UpdateByCancel(notifications, deleteReason);
189 
190     EXPECT_TRUE(g_mockCalled);
191     EXPECT_EQ(g_mockUpdateByCancelReason, 1);
192 }
193 
194 HWTEST_F(NotificationExtensionWrapperTest, GetUnifiedGroupInfo_NullFunction, TestSize.Level0)
195 {
196     OHOS::Notification::ExtensionWrapper extensionWrapper;
197     OHOS::sptr<OHOS::Notification::NotificationRequest> request = nullptr;
198     EXPECT_EQ(extensionWrapper.GetUnifiedGroupInfo(request), 0);
199 }
200 
201 HWTEST_F(NotificationExtensionWrapperTest, GetUnifiedGroupInfo_ValidFunction, TestSize.Level0)
202 {
203     OHOS::Notification::ExtensionWrapper extensionWrapper;
204     OHOS::sptr<OHOS::Notification::NotificationRequest> request = new OHOS::Notification::NotificationRequest();
__anonf65ce0480202(const OHOS::sptr<OHOS::Notification::NotificationRequest> &request) 205     extensionWrapper.getUnifiedGroupInfo_ = [](const OHOS::sptr<OHOS::Notification::NotificationRequest> &request) {
206         return 1;
207     };
208     EXPECT_EQ(extensionWrapper.GetUnifiedGroupInfo(request), 1);
209 }
210 
211 
212 HWTEST_F(NotificationExtensionWrapperTest, ReminderControl_NullReminderControl, TestSize.Level0)
213 {
214     OHOS::Notification::ExtensionWrapper extensionWrapper;
215     std::string bundleName = "testBundle";
216     extensionWrapper.reminderControl_ = nullptr;
217     int32_t result = extensionWrapper.ReminderControl(bundleName);
218     EXPECT_EQ(result, 0);
219 }
220 
221 HWTEST_F(NotificationExtensionWrapperTest, ReminderControl_ValidReminderControl, TestSize.Level0)
222 {
223     OHOS::Notification::ExtensionWrapper extensionWrapper;
224     std::string bundleName = "testBundle";
__anonf65ce0480302(const std::string &bundleName) 225     extensionWrapper.reminderControl_ = [](const std::string &bundleName) { return 1; };
226     int32_t result = extensionWrapper.ReminderControl(bundleName);
227     EXPECT_EQ(result, 1);
228 }
229 
230 HWTEST_F(NotificationExtensionWrapperTest, BannerControl_NullBannerControl, TestSize.Level0)
231 {
232     // Arrange
233     ExtensionWrapper wrapper;
234     std::string bundleName = "testBundle";
235     wrapper.bannerControl_ = nullptr;
236 
237     // Act
238     int32_t result = wrapper.BannerControl(bundleName);
239 
240     // Assert
241     EXPECT_EQ(-1, result);
242 }
243 
244 HWTEST_F(NotificationExtensionWrapperTest, BannerControl_ValidBannerControl, TestSize.Level0)
245 {
246     // Arrange
247     ExtensionWrapper wrapper;
248     std::string bundleName = "testBundle";
__anonf65ce0480402(const std::string &bundleName) 249     auto mockBannerControl = [](const std::string &bundleName) { return 0; };
250     wrapper.bannerControl_ = mockBannerControl;
251 
252     // Act
253     int32_t result = wrapper.BannerControl(bundleName);
254 
255     // Assert
256     EXPECT_EQ(0, result);
257 }
258 
259 HWTEST_F(NotificationExtensionWrapperTest, LocalControl_NullCase, TestSize.Level0) {
260     // Arrange
261     OHOS::Notification::ExtensionWrapper wrapper;
262     wrapper.localControl_ = nullptr;
263     auto request = new NotificationRequest();
264 
265     // Act
266     int32_t result = wrapper.LocalControl(request);
267 
268     // Assert
269     ASSERT_EQ(0, result); // 预期返回 0
270 }
271 
272 HWTEST_F(NotificationExtensionWrapperTest, LocalControl_SuccessCase, TestSize.Level0) {
273     // Arrange
274     OHOS::Notification::ExtensionWrapper wrapper;
__anonf65ce0480502(const sptr<NotificationRequest> &req) 275     int32_t (*mockFunc)(const sptr<NotificationRequest> &) = [](const sptr<NotificationRequest> &req) {
276         return 1;
277     };
278     wrapper.localControl_ = mockFunc;
279     auto request = new NotificationRequest();
280 
281     // Act
282     int32_t result = wrapper.LocalControl(request);
283 
284     // Assert
285     ASSERT_EQ(1, result);
286 }
287 
288 HWTEST_F(NotificationExtensionWrapperTest, convertToDelType_ActiveDelete, TestSize.Level0)
289 {
290     // Arrange
291     int32_t deleteReason = NotificationConstant::PACKAGE_CHANGED_REASON_DELETE + 1;
292     int32_t expectedDelType = ACTIVE_DELETE;
293 
294     // Act
295     int32_t actualDelType = OHOS::Notification::ExtensionWrapper::convertToDelType(deleteReason);
296 
297     // Assert
298     ASSERT_EQ(expectedDelType, actualDelType);
299 }
300 
301 HWTEST_F(NotificationExtensionWrapperTest, convertToDelType_PassiveDelete, TestSize.Level0)
302 {
303     // Arrange
304     int32_t deleteReason = NotificationConstant::PACKAGE_CHANGED_REASON_DELETE;
305     int32_t expectedDelType = PASSITIVE_DELETE;
306 
307     // Act
308     int32_t actualDelType = OHOS::Notification::ExtensionWrapper::convertToDelType(deleteReason);
309 
310     // Assert
311     ASSERT_EQ(expectedDelType, actualDelType);
312 }
313 
314 HWTEST_F(NotificationExtensionWrapperTest, convertToDelType_UserRemovedReasonDelete, TestSize.Level0)
315 {
316     // Arrange
317     int32_t deleteReason = NotificationConstant::USER_REMOVED_REASON_DELETE;
318     int32_t expectedDelType = PASSITIVE_DELETE;
319 
320     // Act
321     int32_t actualDelType = OHOS::Notification::ExtensionWrapper::convertToDelType(deleteReason);
322 
323     // Assert
324     ASSERT_EQ(expectedDelType, actualDelType);
325 }
326 
327 HWTEST_F(NotificationExtensionWrapperTest, convertToDelType_DisableSlotReasonDelete, TestSize.Level0)
328 {
329     // Arrange
330     int32_t deleteReason = NotificationConstant::DISABLE_SLOT_REASON_DELETE;
331     int32_t expectedDelType = PASSITIVE_DELETE;
332 
333     // Act
334     int32_t actualDelType = OHOS::Notification::ExtensionWrapper::convertToDelType(deleteReason);
335 
336     // Assert
337     ASSERT_EQ(expectedDelType, actualDelType);
338 }
339 
340 HWTEST_F(NotificationExtensionWrapperTest, convertToDelType_DisableNotificationReasonDelete, TestSize.Level0)
341 {
342     // Arrange
343     int32_t deleteReason = NotificationConstant::DISABLE_NOTIFICATION_REASON_DELETE;
344     int32_t expectedDelType = PASSITIVE_DELETE;
345 
346     // Act
347     int32_t actualDelType = OHOS::Notification::ExtensionWrapper::convertToDelType(deleteReason);
348 
349     // Assert
350     ASSERT_EQ(expectedDelType, actualDelType);
351 }
352 
353 HWTEST_F(NotificationExtensionWrapperTest, NotificationDialogControl_Test, TestSize.Level0)
354 {
355     ExtensionWrapper wrapper;
356     bool result = wrapper.NotificationDialogControl();
357     EXPECT_EQ(true, result);
358 
__anonf65ce0480602() 359     auto mockNotificationDialogControl = []() { return true; };
360     wrapper.notificationDialogControl_ = mockNotificationDialogControl;
361     result = wrapper.NotificationDialogControl();
362     EXPECT_EQ(true, result);
363 }
364 }   //namespace Notification
365 }   //namespace OHOS