1 /*
2  * Copyright (c) 2021-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 #include <functional>
16 #include <gtest/gtest.h>
17 
18 #include "mock_ipc_skeleton.h"
19 #include "notification_preferences.h"
20 #define private public
21 #include "accesstoken_kit.h"
22 #include "advanced_notification_service.h"
23 #include "ans_subscriber_listener.h"
24 #include "notification_subscriber.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS::Security::AccessToken;
28 
29 namespace OHOS {
30 namespace Notification {
31 extern void MockGetTokenTypeFlag(ATokenTypeEnum mockRet);
32 
33 typedef std::function<void(const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>)>
34     ConsumedFunc;
35 typedef std::function<void(const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>, int)>
36     CanceledFunc;
37 
38 bool passed = false;
39 class TestAnsSubscriber : public NotificationSubscriber {
40 public:
41     void OnConnected() override
42     {
43         if (subscriberCb_ != nullptr) {
44             subscriberCb_();
45         }
46     }
47     void OnDisconnected() override
48     {
49         if (unSubscriberCb_ != nullptr) {
50             unSubscriberCb_();
51         }
52     }
53     void OnDied() override
54     {}
55     void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override
56     {}
57     void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override
58     {}
59     void OnEnabledNotificationChanged(
60         const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) override
61     {}
62     void OnCanceled(const std::shared_ptr<Notification> &request,
63         const std::shared_ptr<NotificationSortingMap> &sortingMap, int deleteReason) override
64     {
65         if (canceledCb_ != nullptr) {
66             canceledCb_(request, sortingMap, deleteReason);
67         }
68     }
69     void OnConsumed(const std::shared_ptr<Notification> &request,
70         const std::shared_ptr<NotificationSortingMap> &sortingMap) override
71     {
72         if (consumedCb_ != nullptr) {
73             consumedCb_(request, sortingMap);
74         }
75     }
76     void OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> &badgeData) override
77     {}
78     void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override
79     {}
80     void OnBatchCanceled(const std::vector<std::shared_ptr<Notification>>
81         &requestList, const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override
82     {}
83 
84     ConsumedFunc consumedCb_ = nullptr;
85     CanceledFunc canceledCb_ = nullptr;
86     std::function<void()> unSubscriberCb_ = nullptr;
87     std::function<void()> subscriberCb_ = nullptr;
88 };
89 
90 class AnsModuleTest : public testing::Test {
91 public:
92     static void SetUpTestCase();
93     static void TearDownTestCase();
94     void SetUp();
95     void TearDown();
96     void TestAddSlots();
97 
98     static sptr<AdvancedNotificationService> g_advancedNotificationService;
99 };
100 
101 sptr<AdvancedNotificationService> AnsModuleTest::g_advancedNotificationService;
SetUpTestCase()102 void AnsModuleTest::SetUpTestCase()
103 {
104     passed = false;
105     NotificationPreferences::GetInstance()->ClearNotificationInRestoreFactorySettings();
106     g_advancedNotificationService = OHOS::Notification::AdvancedNotificationService::GetInstance();
107 }
108 
TearDownTestCase()109 void AnsModuleTest::TearDownTestCase()
110 {
111     passed = false;
112     NotificationPreferences::GetInstance()->ClearNotificationInRestoreFactorySettings();
113     if (g_advancedNotificationService != nullptr) {
114         g_advancedNotificationService->SelfClean();
115     }
116 }
117 
SetUp()118 void AnsModuleTest::SetUp()
119 {
120     passed = false;
121     NotificationPreferences::GetInstance()->ClearNotificationInRestoreFactorySettings();
122 }
123 
TearDown()124 void AnsModuleTest::TearDown()
125 {
126     NotificationPreferences::GetInstance()->ClearNotificationInRestoreFactorySettings();
127     passed = false;
128 }
129 
TestAddSlots()130 void AnsModuleTest::TestAddSlots()
131 {
132     std::vector<sptr<NotificationSlot>> slots;
133     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
134     slots.push_back(slot0);
135     g_advancedNotificationService->AddSlots(slots);
136 }
137 
138 /**
139  * @tc.number    : AnsModuleTest_002
140  * @tc.name      : ANS_Module_Test_0200
141  * @tc.desc      : Test the function of getting notifications and getting all notifications
142  */
HWTEST_F(AnsModuleTest, AnsModuleTest_002, Function | SmallTest | Level1)143 HWTEST_F(AnsModuleTest, AnsModuleTest_002, Function | SmallTest | Level1)
144 {
145     TestAddSlots();
146     std::string label = "testLabel";
147     sptr<NotificationRequest> req = new NotificationRequest(0);
148     req->SetLabel(label);
149     sptr<NotificationRequest> req1 = new NotificationRequest(1);
150     req1->SetLabel(label);
151     sptr<NotificationRequest> req2 = new NotificationRequest(2);
152     req2->SetLabel("testLabel1");
153     sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
154     info->AddAppName("bundleName");
155     std::vector<sptr<NotificationRequest>> notificationsReqs;
156     std::vector<sptr<Notification>> notifications;
157     EXPECT_EQ((int)g_advancedNotificationService->Publish(label, req), (int)ERR_OK);
158     EXPECT_EQ((int)g_advancedNotificationService->Publish(label, req1), (int)ERR_OK);
159     EXPECT_EQ((int)g_advancedNotificationService->Publish("testLabel1", req2), (int)ERR_OK);
160     EXPECT_EQ((int)g_advancedNotificationService->GetActiveNotifications(notificationsReqs, 0), (int)ERR_OK);
161     uint64_t num;
162     g_advancedNotificationService->GetActiveNotificationNums(num);
163     EXPECT_EQ(num, 3);
164     EXPECT_EQ((int)g_advancedNotificationService->Cancel(2, "testLabel1", 0), (int)ERR_OK);
165     EXPECT_EQ((int)g_advancedNotificationService->GetAllActiveNotifications(notifications), (int)ERR_OK);
166     EXPECT_EQ((int)notifications.size(), (int)2);
167     EXPECT_EQ((int)g_advancedNotificationService->CancelAll(0), (int)ERR_OK);
168 }
169 
170 /**
171  * @tc.number    : AnsModuleTest_003
172  * @tc.name      : ANS_Module_Test_0300
173  * @tc.desc      : Test publish notifications when slot not found, add it.
174  */
HWTEST_F(AnsModuleTest, AnsModuleTest_003, Function | SmallTest | Level1)175 HWTEST_F(AnsModuleTest, AnsModuleTest_003, Function | SmallTest | Level1)
176 {
177     // subscriber
178     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
179     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
180     auto listener = new (std::nothrow) SubscriberListener(ptr);
181     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
182     subscriber->consumedCb_ = [](const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>) {
183         passed = true;
184     };
185     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
186 
187     // add slot
188     std::vector<sptr<NotificationSlot>> slots;
189     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
190     slots.push_back(slot0);
191     g_advancedNotificationService->AddSlots(slots);
192 
193     // create request
194     std::string label = "testLabel";
195     sptr<NotificationRequest> req = new NotificationRequest(0);
196     req->SetLabel(label);
197     req->SetStatusBarText("text");
198     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
199     std::shared_ptr<NotificationLongTextContent> longTextContent =
200         std::make_shared<NotificationLongTextContent>("longtext");
201     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
202     req->SetContent(content2);
203     g_advancedNotificationService->SetNotificationsEnabledForBundle("", false);
204 
205     g_advancedNotificationService->Publish(label, req);
206     std::this_thread::sleep_for(std::chrono::milliseconds(200));
207     EXPECT_EQ(true, passed);
208     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
209 }
210 
211 /**
212  * @tc.number    : AnsModuleTest_005
213  * @tc.name      : ANS_Module_Test_0500
214  * @tc.desc      : Test publish notification when slot type is SERVICE_REMINDER.
215  */
HWTEST_F(AnsModuleTest, AnsModuleTest_005, Function | SmallTest | Level1)216 HWTEST_F(AnsModuleTest, AnsModuleTest_005, Function | SmallTest | Level1)
217 {
218     // subscriber
219     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
220     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
221     auto listener = new (std::nothrow) SubscriberListener(ptr);
222     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
223     subscriberInfo->AddAppName("bundleName");
224     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
225     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
226     subscriber->consumedCb_ =
227         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
228             std::vector<std::string> sortingKey = sortingMap->GetKey();
229 
230             NotificationSorting sorting1;
231             NotificationSorting sorting2;
232             if (sortingKey.size() == 2) {
233                 sortingMap->GetNotificationSorting("_1_testLabel_0", sorting1);
234                 sortingMap->GetNotificationSorting("_1_testLabel_1", sorting2);
235             }
236             if (sorting1.GetRanking() < sorting2.GetRanking()) {
237                 passed = true;
238             }
239         };
240 
241     // add slot
242     std::vector<sptr<NotificationSlot>> slots;
243     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::SERVICE_REMINDER);
244     slots.push_back(slot0);
245     g_advancedNotificationService->AddSlots(slots);
246 
247     // create request
248     std::string label = "testLabel";
249     std::shared_ptr<NotificationLongTextContent> longTextContent =
250         std::make_shared<NotificationLongTextContent>("longtext");
251     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
252     sptr<NotificationRequest> req = new NotificationRequest(0);
253     sptr<NotificationRequest> req1 = new NotificationRequest(1);
254     req->SetLabel(label);
255     req1->SetLabel(label);
256     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
257     req->SetContent(content2);
258     req1->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
259     req1->SetContent(content2);
260     // publish request
261     g_advancedNotificationService->Publish(label, req);
262     g_advancedNotificationService->Publish(label, req1);
263     EXPECT_TRUE(passed);
264     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
265 }
266 
267 /**
268  * @tc.number    : AnsModuleTest_006
269  * @tc.name      : ANS_Module_Test_0600
270  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
271  */
HWTEST_F(AnsModuleTest, AnsModuleTest_006, Function | SmallTest | Level1)272 HWTEST_F(AnsModuleTest, AnsModuleTest_006, Function | SmallTest | Level1)
273 {
274     // subscriber
275     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
276     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
277     auto listener = new (std::nothrow) SubscriberListener(ptr);
278     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
279     subscriberInfo->AddAppName("bundleName");
280     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
281     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
282     subscriber->consumedCb_ = [](const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>) {
283         passed = true;
284     };
285 
286     // add slot
287     std::vector<sptr<NotificationSlot>> slots;
288     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
289     slots.push_back(slot0);
290     g_advancedNotificationService->AddSlots(slots);
291 
292     // create request
293     std::string label = "testLabel";
294     sptr<NotificationRequest> req = new NotificationRequest(0);
295     req->SetLabel(label);
296     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
297     std::shared_ptr<NotificationLongTextContent> longTextContent =
298         std::make_shared<NotificationLongTextContent>("longtext");
299     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
300     req->SetContent(content2);
301 
302     // publish request
303     g_advancedNotificationService->Publish(label, req);
304     std::this_thread::sleep_for(std::chrono::milliseconds(200));
305     EXPECT_TRUE(passed);
306     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
307 }
308 
309 /**
310  * @tc.number    : AnsModuleTest_007
311  * @tc.name      : ANS_Module_Test_0700
312  * @tc.desc      : Test publish notification when slot type is OTHER.
313  */
HWTEST_F(AnsModuleTest, AnsModuleTest_007, Function | SmallTest | Level1)314 HWTEST_F(AnsModuleTest, AnsModuleTest_007, Function | SmallTest | Level1)
315 {
316     // subscriber
317     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
318     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
319     auto listener = new (std::nothrow) SubscriberListener(ptr);
320     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
321     subscriberInfo->AddAppName("bundleName");
322     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
323     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
324     subscriber->consumedCb_ =
325         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
326             std::vector<std::string> sortingKey = sortingMap->GetKey();
327 
328             NotificationSorting sorting1;
329             NotificationSorting sorting2;
330             if (sortingKey.size() == 2) {
331                 sortingMap->GetNotificationSorting("_1_testLabel_0", sorting1);
332                 sortingMap->GetNotificationSorting("_1_testLabel_1", sorting2);
333             }
334             if (sorting1.GetRanking() < sorting2.GetRanking()) {
335                 passed = true;
336             }
337         };
338 
339     // add slot
340     std::vector<sptr<NotificationSlot>> slots;
341     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::OTHER);
342     slots.push_back(slot0);
343     g_advancedNotificationService->AddSlots(slots);
344 
345     // create request
346     std::string label = "testLabel";
347     std::shared_ptr<NotificationLongTextContent> longTextContent =
348         std::make_shared<NotificationLongTextContent>("longtext");
349     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
350     sptr<NotificationRequest> req = new NotificationRequest(0);
351     sptr<NotificationRequest> req1 = new NotificationRequest(1);
352     req->SetLabel(label);
353     req1->SetLabel(label);
354     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
355     req->SetContent(content2);
356     req1->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
357     req1->SetContent(content2);
358 
359     // publish request
360     g_advancedNotificationService->Publish(label, req);
361     g_advancedNotificationService->Publish(label, req1);
362     EXPECT_TRUE(passed);
363     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
364 }
365 
366 /**
367  * @tc.number    : AnsModuleTest_0013
368  * @tc.name      : ANS_Module_Test_01300
369  * @tc.desc      : Test publish notification when slot type is SOCIAL_COMMUNICATION.
370  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0013, Function | SmallTest | Level1)371 HWTEST_F(AnsModuleTest, AnsModuleTest_0013, Function | SmallTest | Level1)
372 {
373     // subscriber
374     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
375     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
376     auto listener = new (std::nothrow) SubscriberListener(ptr);
377     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
378     subscriberInfo->AddAppName("bundleName");
379     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
380     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
381     subscriber->consumedCb_ = [](const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>) {
382         passed = true;
383     };
384 
385     // add slot
386     std::vector<sptr<NotificationSlot>> slots;
387     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
388     slots.push_back(slot0);
389     g_advancedNotificationService->AddSlots(slots);
390 
391     // create request
392     std::string label = "testLabel";
393     sptr<NotificationRequest> req = new NotificationRequest(0);
394     req->SetLabel(label);
395     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
396     std::shared_ptr<NotificationLongTextContent> longTextContent =
397         std::make_shared<NotificationLongTextContent>("longtext");
398     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
399     req->SetContent(content2);
400 
401     // publish request
402     g_advancedNotificationService->Publish(label, req);
403     std::this_thread::sleep_for(std::chrono::milliseconds (200));
404     EXPECT_TRUE(passed);
405     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
406 }
407 
408 /**
409  * @tc.number    : AnsModuleTest_0014
410  * @tc.name      : ANS_Module_Test_01400
411  * @tc.desc      : Test publish notification when slot type is SOCIAL_COMMUNICATION.
412  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0014, Function | SmallTest | Level1)413 HWTEST_F(AnsModuleTest, AnsModuleTest_0014, Function | SmallTest | Level1)
414 {
415     // subscriber
416     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
417     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
418     auto listener = new (std::nothrow) SubscriberListener(ptr);
419     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
420     subscriberInfo->AddAppName("bundleName");
421     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
422     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
423     subscriber->consumedCb_ = [](const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>) {
424         passed = true;
425     };
426 
427     // add slot
428     std::vector<sptr<NotificationSlot>> slots;
429     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
430     slots.push_back(slot0);
431     g_advancedNotificationService->AddSlots(slots);
432 
433     // create request
434     std::string label = "testLabel";
435     sptr<NotificationRequest> req = new NotificationRequest(0);
436     req->SetLabel(label);
437     std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
438     normalContent->SetText("1");
439     normalContent->SetTitle("1");
440     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
441     req->SetContent(content);
442 
443     // publish request
444     g_advancedNotificationService->Publish(label, req);
445     std::this_thread::sleep_for(std::chrono::milliseconds(200));
446     EXPECT_TRUE(passed);
447     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
448 }
449 
450 /**
451  * @tc.number    : AnsModuleTest_0015
452  * @tc.name      : ANS_Module_Test_01500
453  * @tc.desc      : Test publish notification when slot type is SOCIAL_COMMUNICATION.
454  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0015, Function | SmallTest | Level1)455 HWTEST_F(AnsModuleTest, AnsModuleTest_0015, Function | SmallTest | Level1)
456 {
457     // subscriber
458     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
459     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
460     auto listener = new (std::nothrow) SubscriberListener(ptr);
461     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
462     subscriberInfo->AddAppName("bundleName");
463     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
464     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
465     subscriber->consumedCb_ = [](const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>) {
466         passed = true;
467     };
468 
469     // add slot
470     std::vector<sptr<NotificationSlot>> slots;
471     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
472     slots.push_back(slot0);
473     g_advancedNotificationService->AddSlots(slots);
474 
475     // create request
476     std::string label = "testLabel";
477     sptr<NotificationRequest> req = new NotificationRequest(0);
478     req->SetLabel(label);
479     std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
480     normalContent->SetText("1");
481     normalContent->SetTitle("1");
482     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
483     req->SetContent(content);
484 
485     // publish request
486     g_advancedNotificationService->Publish(label, req);
487     std::this_thread::sleep_for(std::chrono::milliseconds(200));
488     EXPECT_TRUE(passed);
489     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
490 }
491 
492 /**
493  * @tc.number    : AnsModuleTest_0017
494  * @tc.name      : ANS_Module_Test_01700
495  * @tc.desc      : Test publish notification when slot type is SOCIAL_COMMUNICATION.
496  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0017, Function | SmallTest | Level1)497 HWTEST_F(AnsModuleTest, AnsModuleTest_0017, Function | SmallTest | Level1)
498 {
499     // subscriber
500     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
501     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
502     auto listener = new (std::nothrow) SubscriberListener(ptr);
503     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
504     subscriberInfo->AddAppName("bundleName");
505     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
506     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
507     subscriber->consumedCb_ = [](const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>) {
508         passed = true;
509     };
510 
511     // add slot
512     std::vector<sptr<NotificationSlot>> slots;
513     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
514     slots.push_back(slot0);
515     g_advancedNotificationService->AddSlots(slots);
516 
517     // create request
518     std::string label = "testLabel";
519     sptr<NotificationRequest> req = new NotificationRequest(0);
520     req->SetLabel(label);
521     std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
522     normalContent->SetText("1");
523     normalContent->SetTitle("1");
524     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
525     req->SetContent(content);
526 
527     // publish request
528     g_advancedNotificationService->Publish(label, req);
529     std::this_thread::sleep_for(std::chrono::milliseconds(200));
530     EXPECT_TRUE(passed);
531     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
532 }
533 
534 /**
535  * @tc.number    : AnsModuleTest_0019
536  * @tc.name      : ANS_Module_Test_01900
537  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
538  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0019, Function | SmallTest | Level1)539 HWTEST_F(AnsModuleTest, AnsModuleTest_0019, Function | SmallTest | Level1)
540 {
541     // subscriber
542     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
543     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
544     auto listener = new (std::nothrow) SubscriberListener(ptr);
545     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
546     subscriberInfo->AddAppName("bundleName");
547     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
548     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
549     subscriber->consumedCb_ = [](const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>) {
550         passed = true;
551     };
552 
553     // add slot
554     std::vector<sptr<NotificationSlot>> slots;
555     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
556     slots.push_back(slot0);
557     g_advancedNotificationService->AddSlots(slots);
558 
559     // create request
560     std::string label = "testLabel";
561     sptr<NotificationRequest> req = new NotificationRequest(0);
562     req->SetLabel(label);
563     std::shared_ptr<NotificationLongTextContent> longTextContent = std::make_shared<NotificationLongTextContent>();
564     longTextContent->SetText("1");
565     longTextContent->SetTitle("1");
566     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(longTextContent);
567     req->SetContent(content);
568 
569     // publish request
570     g_advancedNotificationService->Publish(label, req);
571     std::this_thread::sleep_for(std::chrono::milliseconds(200));
572     EXPECT_TRUE(passed);
573     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
574 }
575 
576 /**
577  * @tc.number    : AnsModuleTest_0021
578  * @tc.name      : ANS_Module_Test_02100
579  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
580  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0021, Function | SmallTest | Level1)581 HWTEST_F(AnsModuleTest, AnsModuleTest_0021, Function | SmallTest | Level1)
582 {
583     // subscriber
584     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
585     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
586     auto listener = new (std::nothrow) SubscriberListener(ptr);
587     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
588     subscriberInfo->AddAppName("bundleName");
589     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
590     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
591     subscriber->consumedCb_ =
592         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
593             std::vector<std::string> sortingKey = sortingMap->GetKey();
594 
595             NotificationSorting sorting1;
596             NotificationSorting sorting2;
597             if (sortingKey.size() == 2) {
598                 sortingMap->GetNotificationSorting("_1_testLabel_0", sorting1);
599                 sortingMap->GetNotificationSorting("_1_testLabel_1", sorting2);
600             }
601             if (sorting1.GetRanking() < sorting2.GetRanking()) {
602                 passed = true;
603             }
604         };
605 
606     // add slot
607     std::vector<sptr<NotificationSlot>> slots;
608     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
609     slots.push_back(slot0);
610     g_advancedNotificationService->AddSlots(slots);
611 
612     // create request
613     std::string label = "testLabel";
614     sptr<NotificationRequest> req = new NotificationRequest(0);
615     sptr<NotificationRequest> req1 = new NotificationRequest(1);
616     req->SetLabel(label);
617     req1->SetLabel(label);
618     std::shared_ptr<NotificationPictureContent> pictureContent = std::make_shared<NotificationPictureContent>();
619     pictureContent->SetText("1");
620     pictureContent->SetTitle("1");
621     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(pictureContent);
622     req->SetContent(content);
623     req1->SetContent(content);
624 
625     // publish request
626     g_advancedNotificationService->Publish(label, req);
627     g_advancedNotificationService->Publish(label, req1);
628     EXPECT_TRUE(passed);
629     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
630 }
631 
632 /**
633  * @tc.number    : AnsModuleTest_0023
634  * @tc.name      : ANS_Module_Test_02300
635  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
636  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0023, Function | SmallTest | Level1)637 HWTEST_F(AnsModuleTest, AnsModuleTest_0023, Function | SmallTest | Level1)
638 {
639     // subscriber
640     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
641     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
642     auto listener = new (std::nothrow) SubscriberListener(ptr);
643     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
644     subscriberInfo->AddAppName("bundleName");
645     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
646     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
647     subscriber->consumedCb_ = [](const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>) {
648         passed = true;
649     };
650 
651     // add slot
652     std::vector<sptr<NotificationSlot>> slots;
653     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
654     slots.push_back(slot0);
655     g_advancedNotificationService->AddSlots(slots);
656 
657     // create request
658     std::string label = "testLabel";
659     sptr<NotificationRequest> req = new NotificationRequest(0);
660     req->SetLabel(label);
661     std::shared_ptr<NotificationMultiLineContent> contentImpl = std::make_shared<NotificationMultiLineContent>();
662     contentImpl->SetText("1");
663     contentImpl->SetTitle("1");
664     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
665     req->SetContent(content);
666 
667     // publish request
668     g_advancedNotificationService->Publish(label, req);
669     std::this_thread::sleep_for(std::chrono::milliseconds(200));
670     EXPECT_TRUE(passed);
671     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
672 }
673 
674 /**
675  * @tc.number    : AnsModuleTest_0031
676  * @tc.name      : ANS_Module_Test_03100
677  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
678  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0031, Function | SmallTest | Level1)679 HWTEST_F(AnsModuleTest, AnsModuleTest_0031, Function | SmallTest | Level1)
680 {
681     // subscriber
682     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
683     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
684     auto listener = new (std::nothrow) SubscriberListener(ptr);
685     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
686     subscriberInfo->AddAppName("bundleName");
687     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
688     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
689     subscriber->consumedCb_ =
690         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
691             std::vector<std::string> sortingKey = sortingMap->GetKey();
692 
693             NotificationSorting sorting1;
694             NotificationSorting sorting2;
695             if (sortingKey.size() == 2) {
696                 sortingMap->GetNotificationSorting("_1_testLabel_0", sorting1);
697                 sortingMap->GetNotificationSorting("_1_testLabel_1", sorting2);
698             }
699             if (sorting1.GetRanking() < sorting2.GetRanking()) {
700                 passed = true;
701             }
702         };
703 
704     // add slot
705     std::vector<sptr<NotificationSlot>> slots;
706     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
707     slots.push_back(slot0);
708     g_advancedNotificationService->AddSlots(slots);
709 
710     // create request
711     std::string label = "testLabel";
712     sptr<NotificationRequest> req = new NotificationRequest(0);
713     sptr<NotificationRequest> req1 = new NotificationRequest(1);
714     req->SetLabel(label);
715     req1->SetLabel(label);
716     std::shared_ptr<NotificationMediaContent> contentImpl = std::make_shared<NotificationMediaContent>();
717     contentImpl->SetText("1");
718     contentImpl->SetTitle("1");
719     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
720     req->SetContent(content);
721     req1->SetContent(content);
722 
723     // publish request
724     g_advancedNotificationService->Publish(label, req);
725     g_advancedNotificationService->Publish(label, req1);
726     EXPECT_TRUE(passed);
727     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
728 }
729 
730 /**
731  * @tc.number    : AnsModuleTest_0033
732  * @tc.name      : ANS_Module_Test_03300
733  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
734  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0033, Function | SmallTest | Level1)735 HWTEST_F(AnsModuleTest, AnsModuleTest_0033, Function | SmallTest | Level1)
736 {
737     // subscriber
738     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
739     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
740     auto listener = new (std::nothrow) SubscriberListener(ptr);
741     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
742     subscriberInfo->AddAppName("bundleName");
743     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
744     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
745     subscriber->consumedCb_ = [](const std::shared_ptr<Notification> notification,
746                                   const std::shared_ptr<NotificationSortingMap>) {
747         if (notification->EnableVibrate()) {
748             passed = true;
749         }
750     };
751 
752     // add slot
753     std::vector<sptr<NotificationSlot>> slots;
754     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
755     slot->SetEnableVibration(true);
756     slot->SetVibrationStyle(std::vector<int64_t>(1, 1));
757     slots.push_back(slot);
758     g_advancedNotificationService->AddSlots(slots);
759 
760     // create request
761     std::string label = "testLabel";
762     sptr<NotificationRequest> req = new NotificationRequest(0);
763     req->SetLabel(label);
764     std::shared_ptr<NotificationNormalContent> contentImpl = std::make_shared<NotificationNormalContent>();
765     contentImpl->SetText("1");
766     contentImpl->SetTitle("1");
767     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
768     req->SetContent(content);
769     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
770 
771     // publish request
772     g_advancedNotificationService->Publish(label, req);
773     std::this_thread::sleep_for(std::chrono::milliseconds(200));
774     EXPECT_TRUE(passed);
775     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
776 }
777 
778 /**
779  * @tc.number    : AnsModuleTest_0034
780  * @tc.name      : ANS_Module_Test_03400
781  * @tc.desc      : Test publish notification when slot type is SOCIAL_COMMUNICATION.
782  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0034, Function | SmallTest | Level1)783 HWTEST_F(AnsModuleTest, AnsModuleTest_0034, Function | SmallTest | Level1)
784 {
785     // subscriber
786     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
787     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
788     auto listener = new (std::nothrow) SubscriberListener(ptr);
789     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
790     subscriberInfo->AddAppName("bundleName");
791     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
792     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
793     subscriber->consumedCb_ = [](const std::shared_ptr<Notification> notification,
794                                   const std::shared_ptr<NotificationSortingMap>) {
795         if (notification->EnableSound()) {
796             passed = true;
797         }
798     };
799 
800     // add slot
801     std::vector<sptr<NotificationSlot>> slots;
802     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
803     slot->SetEnableVibration(true);
804     slot->SetSound(Uri("/sound/test.mp3"));
805     slots.push_back(slot);
806     g_advancedNotificationService->AddSlots(slots);
807 
808     // create request
809     std::string label = "testLabel";
810     sptr<NotificationRequest> req = new NotificationRequest(0);
811     req->SetLabel(label);
812     std::shared_ptr<NotificationNormalContent> contentImpl = std::make_shared<NotificationNormalContent>();
813     contentImpl->SetText("1");
814     contentImpl->SetTitle("1");
815     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
816     req->SetContent(content);
817     req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
818 
819     // publish request
820     g_advancedNotificationService->Publish(label, req);
821     std::this_thread::sleep_for(std::chrono::milliseconds(200));
822     EXPECT_TRUE(passed);
823     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
824 }
825 
826 /**
827  * @tc.number    : AnsModuleTest_0035
828  * @tc.name      : ANS_Module_Test_03500
829  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
830  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0035, Function | SmallTest | Level1)831 HWTEST_F(AnsModuleTest, AnsModuleTest_0035, Function | SmallTest | Level1)
832 {
833     // subscriber
834     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
835     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
836     auto listener = new (std::nothrow) SubscriberListener(ptr);
837     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
838     subscriberInfo->AddAppName("bundleName");
839     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
840     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
841     subscriber->canceledCb_ = [](const std::shared_ptr<Notification> &request,
842                                   const std::shared_ptr<NotificationSortingMap> &sortingMap,
843                                   int deleteReason) { passed = true; };
844 
845     // add slot
846     std::vector<sptr<NotificationSlot>> slots;
847     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
848     slot->SetEnableVibration(true);
849     slots.push_back(slot);
850     g_advancedNotificationService->AddSlots(slots);
851 
852     // create request
853     std::string label = "testLabel";
854     sptr<NotificationRequest> req = new NotificationRequest(0);
855     req->SetLabel(label);
856     std::shared_ptr<NotificationMediaContent> contentImpl = std::make_shared<NotificationMediaContent>();
857     contentImpl->SetText("1");
858     contentImpl->SetTitle("1");
859     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
860     req->SetContent(content);
861 
862     // publish request
863     g_advancedNotificationService->Publish(label, req);
864     g_advancedNotificationService->Cancel(0, label, 0);
865     std::this_thread::sleep_for(std::chrono::milliseconds(200));
866     EXPECT_TRUE(passed);
867     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
868 }
869 
870 /**
871  * @tc.number    : AnsModuleTest_0036
872  * @tc.name      : ANS_Module_Test_03600
873  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
874  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0036, Function | SmallTest | Level1)875 HWTEST_F(AnsModuleTest, AnsModuleTest_0036, Function | SmallTest | Level1)
876 {
877     // subscriber
878     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
879     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
880     auto listener = new (std::nothrow) SubscriberListener(ptr);
881     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
882     subscriberInfo->AddAppName("bundleName");
883     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
884     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
885     subscriber->canceledCb_ = [](const std::shared_ptr<Notification> &request,
886                                   const std::shared_ptr<NotificationSortingMap> &sortingMap,
887                                   int deleteReason) { passed = true; };
888 
889     // add slot
890     std::vector<sptr<NotificationSlot>> slots;
891     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
892     slot->SetEnableVibration(true);
893     slots.push_back(slot);
894     g_advancedNotificationService->AddSlots(slots);
895 
896     // create request
897     std::string label = "testLabel";
898     sptr<NotificationRequest> req = new NotificationRequest(0);
899     req->SetLabel(label);
900     std::shared_ptr<NotificationMediaContent> contentImpl = std::make_shared<NotificationMediaContent>();
901     contentImpl->SetText("1");
902     contentImpl->SetTitle("1");
903     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
904     req->SetContent(content);
905 
906     // publish request
907     g_advancedNotificationService->Publish(label, req);
908     g_advancedNotificationService->CancelAll(0);
909     EXPECT_TRUE(passed);
910     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
911 }
912 
913 /**
914  * @tc.number    : AnsModuleTest_0039
915  * @tc.name      : ANS_Module_Test_03900
916  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
917  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0039, Function | SmallTest | Level1)918 HWTEST_F(AnsModuleTest, AnsModuleTest_0039, Function | SmallTest | Level1)
919 {
920     // subscriber
921     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
922     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
923     auto listener = new (std::nothrow) SubscriberListener(ptr);
924     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
925     subscriberInfo->AddAppName("bundleName");
926     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
927     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
928     subscriber->consumedCb_ =
929         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
930             std::vector<std::string> sortingKey = sortingMap->GetKey();
931 
932             NotificationSorting sorting1;
933             NotificationSorting sorting2;
934             if (sortingKey.size() == 2) {
935                 sortingMap->GetNotificationSorting("_1_testLabel_0", sorting1);
936                 sortingMap->GetNotificationSorting("_1_testLabel_1", sorting2);
937             }
938             if (sorting1.GetRanking() < sorting2.GetRanking()) {
939                 passed = true;
940             }
941         };
942 
943     // add slot
944     std::vector<sptr<NotificationSlot>> slots;
945     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
946     slot->SetEnableVibration(true);
947     slots.push_back(slot);
948     g_advancedNotificationService->AddSlots(slots);
949 
950     // create request
951     std::string label = "testLabel";
952     sptr<NotificationRequest> req = new NotificationRequest(0);
953     sptr<NotificationRequest> req1 = new NotificationRequest(1);
954     req->SetLabel(label);
955     req1->SetLabel(label);
956     std::shared_ptr<NotificationMediaContent> contentImpl = std::make_shared<NotificationMediaContent>();
957     contentImpl->SetText("1");
958     contentImpl->SetTitle("1");
959     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
960     req->SetContent(content);
961     req1->SetContent(content);
962 
963     // publish request
964     g_advancedNotificationService->Publish(label, req);
965     g_advancedNotificationService->Publish(label, req1);
966     EXPECT_TRUE(passed);
967     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
968 }
969 
970 /**
971  * @tc.number    : AnsModuleTest_0040
972  * @tc.name      : ANS_Module_Test_04000
973  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
974  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0040, Function | SmallTest | Level1)975 HWTEST_F(AnsModuleTest, AnsModuleTest_0040, Function | SmallTest | Level1)
976 {
977     // subscriber
978     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
979     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
980     auto listener = new (std::nothrow) SubscriberListener(ptr);
981     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
982     subscriberInfo->AddAppName("bundleName");
983     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
984     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
985     subscriber->consumedCb_ = [](const std::shared_ptr<Notification> notification,
986                                   const std::shared_ptr<NotificationSortingMap>) { passed = true; };
987 
988     // add slot
989     std::vector<sptr<NotificationSlot>> slots;
990     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::SERVICE_REMINDER);
991     slot->SetEnableVibration(true);
992     slots.push_back(slot);
993     g_advancedNotificationService->AddSlots(slots);
994 
995     // create request
996     std::string label = "testLabel";
997     sptr<NotificationRequest> req = new NotificationRequest(0);
998     req->SetLabel(label);
999     std::shared_ptr<NotificationMediaContent> contentImpl = std::make_shared<NotificationMediaContent>();
1000     contentImpl->SetText("1");
1001     contentImpl->SetTitle("1");
1002     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
1003     req->SetContent(content);
1004 
1005     // publish request
1006     g_advancedNotificationService->Publish(label, req);
1007     std::this_thread::sleep_for(std::chrono::milliseconds(200));
1008     EXPECT_TRUE(passed);
1009     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1010 }
1011 
1012 /**
1013  * @tc.number    : AnsModuleTest_0041
1014  * @tc.name      : ANS_Module_Test_04100
1015  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1016  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0041, Function | SmallTest | Level1)1017 HWTEST_F(AnsModuleTest, AnsModuleTest_0041, Function | SmallTest | Level1)
1018 {
1019     // subscriber
1020     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1021     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1022     auto listener = new (std::nothrow) SubscriberListener(ptr);
1023     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1024     subscriberInfo->AddAppName("bundleName");
1025     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
1026     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1027     subscriber->consumedCb_ = [](const std::shared_ptr<Notification> notification,
1028                                   const std::shared_ptr<NotificationSortingMap>) { passed = true; };
1029 
1030     // add slot
1031     std::vector<sptr<NotificationSlot>> slots;
1032     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
1033     slot->SetEnableVibration(true);
1034     slots.push_back(slot);
1035     g_advancedNotificationService->AddSlots(slots);
1036 
1037     // create request
1038     std::string label = "testLabel";
1039     sptr<NotificationRequest> req = new NotificationRequest(0);
1040     req->SetLabel(label);
1041     std::shared_ptr<NotificationMediaContent> contentImpl = std::make_shared<NotificationMediaContent>();
1042     contentImpl->SetText("1");
1043     contentImpl->SetTitle("1");
1044     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
1045     req->SetContent(content);
1046 
1047     // publish request
1048     g_advancedNotificationService->Publish(label, req);
1049     std::this_thread::sleep_for(std::chrono::milliseconds(200));
1050     EXPECT_TRUE(passed);
1051     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1052 }
1053 
1054 /**
1055  * @tc.number    : AnsModuleTest_0042
1056  * @tc.name      : ANS_Module_Test_04200
1057  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1058  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0042, Function | SmallTest | Level1)1059 HWTEST_F(AnsModuleTest, AnsModuleTest_0042, Function | SmallTest | Level1)
1060 {
1061     // subscriber
1062     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1063     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1064     auto listener = new (std::nothrow) SubscriberListener(ptr);
1065     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1066     subscriberInfo->AddAppName("bundleName");
1067     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
1068     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1069     subscriber->consumedCb_ = [](const std::shared_ptr<Notification> notification,
1070                                   const std::shared_ptr<NotificationSortingMap>) { passed = true; };
1071 
1072     // add slot
1073     std::vector<sptr<NotificationSlot>> slots;
1074     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::OTHER);
1075     slot->SetEnableVibration(true);
1076     slots.push_back(slot);
1077     g_advancedNotificationService->AddSlots(slots);
1078 
1079     // create request
1080     std::string label = "testLabel";
1081     sptr<NotificationRequest> req = new NotificationRequest(0);
1082     req->SetLabel(label);
1083     std::shared_ptr<NotificationMediaContent> contentImpl = std::make_shared<NotificationMediaContent>();
1084     contentImpl->SetText("1");
1085     contentImpl->SetTitle("1");
1086     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
1087     req->SetContent(content);
1088 
1089     // publish request
1090     g_advancedNotificationService->Publish(label, req);
1091     std::this_thread::sleep_for(std::chrono::milliseconds(200));
1092     EXPECT_TRUE(passed);
1093     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1094 }
1095 
1096 /**
1097  * @tc.number    : AnsModuleTest_0043
1098  * @tc.name      : ANS_Module_Test_04300
1099  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1100  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0043, Function | SmallTest | Level1)1101 HWTEST_F(AnsModuleTest, AnsModuleTest_0043, Function | SmallTest | Level1)
1102 {
1103     // subscriber
1104     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1105     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1106     auto listener = new (std::nothrow) SubscriberListener(ptr);
1107     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1108     subscriberInfo->AddAppName("bundleName");
1109     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
1110     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1111     subscriber->consumedCb_ = [](const std::shared_ptr<Notification> notification,
1112                                   const std::shared_ptr<NotificationSortingMap>) { passed = true; };
1113 
1114     // add slot
1115     std::vector<sptr<NotificationSlot>> slots;
1116     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::CUSTOM);
1117     slot->SetEnableVibration(true);
1118     slots.push_back(slot);
1119     g_advancedNotificationService->AddSlots(slots);
1120 
1121     // create request
1122     std::string label = "testLabel";
1123     sptr<NotificationRequest> req = new NotificationRequest(0);
1124     req->SetLabel(label);
1125     std::shared_ptr<NotificationMediaContent> contentImpl = std::make_shared<NotificationMediaContent>();
1126     contentImpl->SetText("1");
1127     contentImpl->SetTitle("1");
1128     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
1129     req->SetContent(content);
1130 
1131     // publish request
1132     g_advancedNotificationService->Publish(label, req);
1133     std::this_thread::sleep_for(std::chrono::milliseconds(200));
1134     EXPECT_TRUE(passed);
1135     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1136 }
1137 
1138 /**
1139  * @tc.number    : AnsModuleTest_0049
1140  * @tc.name      : ANS_Module_Test_04900
1141  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1142  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0049, Function | SmallTest | Level1)1143 HWTEST_F(AnsModuleTest, AnsModuleTest_0049, Function | SmallTest | Level1)
1144 {
1145     // add slot
1146     std::vector<sptr<NotificationSlot>> slots;
1147     sptr<NotificationSlot> socialSlot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1148     sptr<NotificationSlot> reminderSlot = new NotificationSlot(NotificationConstant::SlotType::SERVICE_REMINDER);
1149     sptr<NotificationSlot> contentSlot = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
1150     sptr<NotificationSlot> otherSlot = new NotificationSlot(NotificationConstant::SlotType::OTHER);
1151     slots.push_back(socialSlot);
1152     slots.push_back(reminderSlot);
1153     slots.push_back(contentSlot);
1154     slots.push_back(otherSlot);
1155 
1156     ASSERT_NE(nullptr, g_advancedNotificationService);
1157     g_advancedNotificationService->AddSlots(slots);
1158 }
1159 
1160 /**
1161  * @tc.number    : AnsModuleTest_0051
1162  * @tc.name      : ANS_Module_Test_05100
1163  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1164  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0051, Function | SmallTest | Level1)1165 HWTEST_F(AnsModuleTest, AnsModuleTest_0051, Function | SmallTest | Level1)
1166 {
1167     // add slot
1168     std::vector<sptr<NotificationSlot>> slots;
1169     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1170     std::string slotId = slot->GetId();
1171     slots.push_back(slot);
1172     g_advancedNotificationService->AddSlots(slots);
1173 
1174     std::vector<sptr<NotificationSlot>> slotsRef {};
1175     g_advancedNotificationService->GetSlots(slotsRef);
1176     EXPECT_EQ(1, static_cast<int>(slotsRef.size()));
1177     std::vector<std::string> slotsId {};
1178     for (const auto &i : slotsRef) {
1179         slotsId.push_back(i->GetId());
1180     }
1181     g_advancedNotificationService->RemoveSlotByType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1182     g_advancedNotificationService->GetSlots(slotsRef);
1183     EXPECT_EQ(0, static_cast<int>(slotsRef.size()));
1184 }
1185 
1186 /**
1187  * @tc.number    : AnsModuleTest_0052
1188  * @tc.name      : ANS_Module_Test_05200
1189  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1190  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0052, Function | SmallTest | Level1)1191 HWTEST_F(AnsModuleTest, AnsModuleTest_0052, Function | SmallTest | Level1)
1192 {
1193     // add slot
1194     std::vector<sptr<NotificationSlot>> slots;
1195     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1196     slots.push_back(slot);
1197     g_advancedNotificationService->AddSlots(slots);
1198 
1199     std::vector<sptr<NotificationSlot>> slotsRef {};
1200     g_advancedNotificationService->GetSlots(slotsRef);
1201     std::vector<std::string> slotsId {};
1202     for (const auto &i : slotsRef) {
1203         slotsId.push_back(i->GetId());
1204     }
1205     g_advancedNotificationService->RemoveSlotByType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1206     g_advancedNotificationService->RemoveSlotByType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1207     g_advancedNotificationService->GetSlots(slotsRef);
1208     EXPECT_EQ(0, static_cast<int>(slotsRef.size()));
1209 }
1210 
1211 /**
1212  * @tc.number    : AnsModuleTest_0054
1213  * @tc.name      : ANS_Module_Test_05400
1214  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1215  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0054, Function | SmallTest | Level1)1216 HWTEST_F(AnsModuleTest, AnsModuleTest_0054, Function | SmallTest | Level1)
1217 {
1218     // add slot
1219     std::vector<sptr<NotificationSlot>> slots;
1220     sptr<NotificationSlot> socialSlot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1221     sptr<NotificationSlot> reminderSlot = new NotificationSlot(NotificationConstant::SlotType::SERVICE_REMINDER);
1222     sptr<NotificationSlot> contentSlot = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
1223     sptr<NotificationSlot> otherSlot = new NotificationSlot(NotificationConstant::SlotType::OTHER);
1224 
1225     slots.push_back(socialSlot);
1226     slots.push_back(reminderSlot);
1227     slots.push_back(contentSlot);
1228     slots.push_back(otherSlot);
1229 
1230     EXPECT_EQ(g_advancedNotificationService->AddSlots(slots), 0);
1231 }
1232 
1233 /**
1234  * @tc.number    : AnsModuleTest_0055
1235  * @tc.name      : ANS_Module_Test_05500
1236  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1237  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0055, Function | SmallTest | Level1)1238 HWTEST_F(AnsModuleTest, AnsModuleTest_0055, Function | SmallTest | Level1)
1239 {
1240     // add slot
1241     std::vector<sptr<NotificationSlot>> slots;
1242     sptr<NotificationSlot> socialSlot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1243     slots.push_back(socialSlot);
1244     EXPECT_EQ(g_advancedNotificationService->AddSlots(slots), 0);
1245 
1246     EXPECT_EQ(g_advancedNotificationService->RemoveSlotByType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION), 0);
1247 }
1248 
1249 /**
1250  * @tc.number    : AnsModuleTest_0056
1251  * @tc.name      : ANS_Module_Test_05600
1252  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1253  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0056, Function | SmallTest | Level1)1254 HWTEST_F(AnsModuleTest, AnsModuleTest_0056, Function | SmallTest | Level1)
1255 {
1256     // add slot
1257     std::vector<sptr<NotificationSlot>> slots;
1258     sptr<NotificationSlot> socialSlot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1259     slots.push_back(socialSlot);
1260     EXPECT_EQ(g_advancedNotificationService->AddSlots(slots), 0);
1261     // remove slot group
1262     EXPECT_EQ(g_advancedNotificationService->RemoveSlotByType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION), 0);
1263 }
1264 
1265 /**
1266  * @tc.number    : AnsModuleTest_0058
1267  * @tc.name      : ANS_Module_Test_05800
1268  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1269  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0058, Function | SmallTest | Level1)1270 HWTEST_F(AnsModuleTest, AnsModuleTest_0058, Function | SmallTest | Level1)
1271 {
1272     // subscriber
1273     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1274     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1275     auto listener = new (std::nothrow) SubscriberListener(ptr);
1276     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1277     subscriberInfo->AddAppName("bundleName");
1278     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
1279     subscriber->consumedCb_ = [](const std::shared_ptr<Notification> r, const std::shared_ptr<NotificationSortingMap>) {
1280         if (r->GetNotificationRequest().GetBadgeNumber() == 1) {
1281             passed = true;
1282         }
1283     };
1284     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1285 
1286     // add slot
1287     std::vector<sptr<NotificationSlot>> slots;
1288     sptr<NotificationSlot> slot = new NotificationSlot();
1289     slot->EnableBadge(true);
1290     slots.push_back(slot);
1291     EXPECT_EQ(g_advancedNotificationService->AddSlots(slots), 0);
1292 
1293     // create request
1294     std::string label = "testLabel";
1295     sptr<NotificationRequest> req = new NotificationRequest(0);
1296     req->SetLabel(label);
1297     req->SetStatusBarText("text");
1298     req->SetBadgeNumber(1);
1299     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
1300     std::shared_ptr<NotificationLongTextContent> longTextContent =
1301         std::make_shared<NotificationLongTextContent>("longtext");
1302     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
1303     req->SetContent(content2);
1304 
1305     // SetShowBadgeEnabledForBundle true
1306     sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption("bundleName", 0);
1307     g_advancedNotificationService->SetShowBadgeEnabledForBundle(bundleOption, true);
1308 
1309     g_advancedNotificationService->Publish(label, req);
1310     std::this_thread::sleep_for(std::chrono::milliseconds(200));
1311     EXPECT_EQ(true, passed);
1312     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1313 }
1314 
1315 /**
1316  * @tc.number    : AnsModuleTest_062
1317  * @tc.name      : ANS_Module_Test_06200
1318  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1319  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0062, Function | SmallTest | Level1)1320 HWTEST_F(AnsModuleTest, AnsModuleTest_0062, Function | SmallTest | Level1)
1321 {
1322     // subscriber
1323     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1324     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1325     auto listener = new (std::nothrow) SubscriberListener(ptr);
1326     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1327     subscriberInfo->AddAppName("bundleName");
1328     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
1329     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1330     subscriber->consumedCb_ =
1331         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
1332             std::vector<std::string> sortingKey = sortingMap->GetKey();
1333 
1334             NotificationSorting sorting1;
1335             NotificationSorting sorting2;
1336             if (sortingKey.size() == 2) {
1337                 sortingMap->GetNotificationSorting("_1_testLabel_0", sorting1);
1338                 sortingMap->GetNotificationSorting("_1_testLabel_1", sorting2);
1339             }
1340             if (sorting1.GetRanking() < sorting2.GetRanking()) {
1341                 passed = true;
1342             }
1343         };
1344 
1345     // add slot
1346     std::vector<sptr<NotificationSlot>> slots;
1347     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
1348     slots.push_back(slot0);
1349     g_advancedNotificationService->AddSlots(slots);
1350 
1351     // create request
1352     std::string label = "testLabel";
1353     std::shared_ptr<NotificationLongTextContent> longTextContent =
1354         std::make_shared<NotificationLongTextContent>("longtext");
1355     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
1356     sptr<NotificationRequest> req = new NotificationRequest(0);
1357     sptr<NotificationRequest> req1 = new NotificationRequest(1);
1358     req->SetLabel(label);
1359     req1->SetLabel(label);
1360     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
1361     req->SetContent(content2);
1362     req1->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
1363     req1->SetContent(content2);
1364 
1365     // publish request
1366     g_advancedNotificationService->Publish(label, req);
1367     g_advancedNotificationService->Publish(label, req1);
1368     EXPECT_TRUE(passed);
1369     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1370 }
1371 
1372 /**
1373  * @tc.number    : AnsModuleTest_063
1374  * @tc.name      : ANS_Module_Test_06300
1375  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1376  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0063, Function | SmallTest | Level1)1377 HWTEST_F(AnsModuleTest, AnsModuleTest_0063, Function | SmallTest | Level1)
1378 {
1379     // subscriber
1380     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1381     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1382     auto listener = new (std::nothrow) SubscriberListener(ptr);
1383     g_advancedNotificationService->Subscribe(listener, nullptr);
1384     subscriber->consumedCb_ =
1385         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
1386             std::vector<std::string> sortingKey = sortingMap->GetKey();
1387 
1388             NotificationSorting sorting1;
1389             NotificationSorting sorting2;
1390             if (sortingKey.size() == 2) {
1391                 sortingMap->GetNotificationSorting("_1_testLabel_0", sorting1);
1392                 sortingMap->GetNotificationSorting("_1_testLabel_1", sorting2);
1393             }
1394             if (sorting1.GetRanking() < sorting2.GetRanking()) {
1395                 passed = true;
1396             }
1397         };
1398 
1399     // add slot
1400     std::vector<sptr<NotificationSlot>> slots;
1401     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
1402     slots.push_back(slot0);
1403     g_advancedNotificationService->AddSlots(slots);
1404 
1405     // create request
1406     std::string label = "testLabel";
1407     std::shared_ptr<NotificationLongTextContent> longTextContent =
1408         std::make_shared<NotificationLongTextContent>("longtext");
1409     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
1410     sptr<NotificationRequest> req = new NotificationRequest(0);
1411     sptr<NotificationRequest> req1 = new NotificationRequest(1);
1412     req->SetLabel(label);
1413     req1->SetLabel(label);
1414     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
1415     req->SetContent(content2);
1416     req1->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
1417     req1->SetContent(content2);
1418 
1419     // publish request
1420     g_advancedNotificationService->Publish(label, req);
1421     g_advancedNotificationService->Publish(label, req1);
1422     EXPECT_TRUE(passed);
1423     g_advancedNotificationService->Unsubscribe(listener, nullptr);
1424 }
1425 
1426 /**
1427  * @tc.number    : AnsModuleTest_064
1428  * @tc.name      : ANS_Module_Test_06400
1429  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1430  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0064, Function | SmallTest | Level1)1431 HWTEST_F(AnsModuleTest, AnsModuleTest_0064, Function | SmallTest | Level1)
1432 {
1433     // subscriber
1434     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1435     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1436     auto listener = new (std::nothrow) SubscriberListener(ptr);
1437     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1438     subscriberInfo->AddAppName("bundleName");
1439     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1440     subscriber->unSubscriberCb_ = []() { passed = true; };
1441     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1442     EXPECT_TRUE(passed);
1443 }
1444 
1445 /**
1446  * @tc.number    : AnsModuleTest_065
1447  * @tc.name      : ANS_Module_Test_06500
1448  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1449  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0065, Function | SmallTest | Level1)1450 HWTEST_F(AnsModuleTest, AnsModuleTest_0065, Function | SmallTest | Level1)
1451 {
1452     // subscriber
1453     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1454     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1455     auto listener = new (std::nothrow) SubscriberListener(ptr);
1456     g_advancedNotificationService->Subscribe(listener, nullptr);
1457     subscriber->unSubscriberCb_ = []() { passed = true; };
1458     g_advancedNotificationService->Unsubscribe(listener, nullptr);
1459     EXPECT_TRUE(passed);
1460 }
1461 
1462 /**
1463  * @tc.number    : AnsModuleTest_066
1464  * @tc.name      : ANS_Module_Test_06600
1465  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1466  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0066, Function | SmallTest | Level1)1467 HWTEST_F(AnsModuleTest, AnsModuleTest_0066, Function | SmallTest | Level1)
1468 {
1469     // subscriber
1470     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1471     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1472     auto listener = new (std::nothrow) SubscriberListener(ptr);
1473     g_advancedNotificationService->Subscribe(listener, nullptr);
1474     subscriber->canceledCb_ = [](const std::shared_ptr<Notification> &request,
1475                                   const std::shared_ptr<NotificationSortingMap> &sortingMap,
1476                                   int deleteReason) { passed = true; };
1477 
1478     // create request
1479     std::string label = "testLabel";
1480     sptr<NotificationRequest> req = new NotificationRequest(0);
1481     req->SetLabel(label);
1482     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
1483     std::shared_ptr<NotificationLongTextContent> longTextContent =
1484         std::make_shared<NotificationLongTextContent>("longtext");
1485     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
1486     req->SetContent(content2);
1487 
1488     // publish request
1489     g_advancedNotificationService->Publish(label, req);
1490 
1491     // remove request
1492     g_advancedNotificationService->Delete("_1_testLabel_0", NotificationConstant::CANCEL_REASON_DELETE);
1493     EXPECT_TRUE(passed);
1494     g_advancedNotificationService->Unsubscribe(listener, nullptr);
1495 }
1496 
1497 /**
1498  * @tc.number    : AnsModuleTest_100
1499  * @tc.name      : ANS_Module_Test_10000
1500  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1501  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0100, Function | SmallTest | Level1)1502 HWTEST_F(AnsModuleTest, AnsModuleTest_0100, Function | SmallTest | Level1)
1503 {
1504     // create wantagent
1505     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent =
1506         std::make_shared<AbilityRuntime::WantAgent::WantAgent>();
1507 
1508     // subscriber
1509     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1510     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1511     auto listener = new (std::nothrow) SubscriberListener(ptr);
1512     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1513     subscriberInfo->AddAppName("bundleName");
1514     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1515     subscriber->consumedCb_ = [](const std::shared_ptr<Notification> notification,
1516                                   const std::shared_ptr<NotificationSortingMap>
1517                                       sortingMap) { passed = true; };
1518 
1519     // add slot
1520     std::vector<sptr<NotificationSlot>> slots;
1521     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::OTHER);
1522     slots.push_back(slot0);
1523     g_advancedNotificationService->AddSlots(slots);
1524 
1525     // create request
1526     std::string label = "testLabel";
1527     sptr<NotificationRequest> req = new NotificationRequest(0);
1528     req->SetLabel(label);
1529 
1530     // set content
1531     std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
1532     normalContent->SetText("1");
1533     normalContent->SetTitle("1");
1534     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
1535     req->SetContent(content);
1536 
1537     // publish request
1538     g_advancedNotificationService->Publish(label, req);
1539     std::this_thread::sleep_for(std::chrono::milliseconds(200));
1540     EXPECT_TRUE(passed);
1541     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1542 }
1543 
1544 /**
1545  * @tc.number    : AnsModuleTest_101
1546  * @tc.name      : ANS_Module_Test_10100
1547  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1548  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0101, Function | SmallTest | Level1)1549 HWTEST_F(AnsModuleTest, AnsModuleTest_0101, Function | SmallTest | Level1)
1550 {
1551     // create wantagent
1552     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent =
1553         std::make_shared<AbilityRuntime::WantAgent::WantAgent>();
1554 
1555     // subscriber
1556     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1557     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1558     auto listener = new (std::nothrow) SubscriberListener(ptr);
1559     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1560     subscriberInfo->AddAppName("bundleName");
1561     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1562     subscriber->consumedCb_ = [](const std::shared_ptr<Notification> notification,
1563                                   const std::shared_ptr<NotificationSortingMap>
1564                                       sortingMap) { passed = true; };
1565 
1566     // create request
1567     std::string label = "testLabel";
1568     sptr<NotificationRequest> req = new NotificationRequest(0);
1569     req->SetLabel(label);
1570     req->SetWantAgent(agent);
1571     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
1572     std::shared_ptr<NotificationLongTextContent> longTextContent =
1573         std::make_shared<NotificationLongTextContent>("longtext");
1574     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
1575     req->SetContent(content2);
1576 
1577     // publish request
1578     g_advancedNotificationService->Publish(label, req);
1579     std::this_thread::sleep_for(std::chrono::milliseconds(200));
1580     EXPECT_TRUE(passed);
1581     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1582 }
1583 
1584 /**
1585  * @tc.number    : AnsModuleTest_102
1586  * @tc.name      : ANS_Module_Test_10200
1587  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1588  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0102, Function | SmallTest | Level1)1589 HWTEST_F(AnsModuleTest, AnsModuleTest_0102, Function | SmallTest | Level1)
1590 {
1591     // create wantagent
1592     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent =
1593         std::make_shared<AbilityRuntime::WantAgent::WantAgent>();
1594 
1595     // subscriber
1596     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1597     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1598     auto listener = new (std::nothrow) SubscriberListener(ptr);
1599     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1600     subscriberInfo->AddAppName("bundleName");
1601     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1602     subscriber->consumedCb_ = [](const std::shared_ptr<Notification> notification,
1603                                   const std::shared_ptr<NotificationSortingMap>
1604                                       sortingMap) { passed = true; };
1605 
1606     // create request
1607     std::string label = "testLabel";
1608     sptr<NotificationRequest> req = new NotificationRequest(0);
1609     req->SetLabel(label);
1610     req->SetWantAgent(agent);
1611     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
1612     std::shared_ptr<NotificationLongTextContent> longTextContent =
1613         std::make_shared<NotificationLongTextContent>("longtext");
1614     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
1615     req->SetContent(content2);
1616 
1617     // publish request
1618     g_advancedNotificationService->Publish(label, req);
1619     std::this_thread::sleep_for(std::chrono::milliseconds(200));
1620     EXPECT_TRUE(passed);
1621     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1622 }
1623 
1624 /**
1625  * @tc.number    : AnsModuleTest_103
1626  * @tc.name      : ANS_Module_Test_10300
1627  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1628  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0103, Function | SmallTest | Level1)1629 HWTEST_F(AnsModuleTest, AnsModuleTest_0103, Function | SmallTest | Level1)
1630 {
1631     // create wantagent
1632     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent =
1633         std::make_shared<AbilityRuntime::WantAgent::WantAgent>();
1634 
1635     // subscriber
1636     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1637     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1638     auto listener = new (std::nothrow) SubscriberListener(ptr);
1639     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1640     subscriberInfo->AddAppName("a");
1641     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1642     subscriber->consumedCb_ = [](const std::shared_ptr<Notification> notification,
1643                                   const std::shared_ptr<NotificationSortingMap>
1644                                       sortingMap) { passed = true; };
1645 
1646     // create request
1647     std::string label = "testLabel";
1648     sptr<NotificationRequest> req = new NotificationRequest(0);
1649     req->SetLabel(label);
1650     req->SetWantAgent(agent);
1651     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
1652     std::shared_ptr<NotificationLongTextContent> longTextContent =
1653         std::make_shared<NotificationLongTextContent>("longtext");
1654     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
1655     req->SetContent(content2);
1656 
1657     // publish request
1658     g_advancedNotificationService->Publish(label, req);
1659     std::this_thread::sleep_for(std::chrono::milliseconds(200));
1660     EXPECT_EQ(passed, true);
1661     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1662 }
1663 
1664 /**
1665  * @tc.number    : AnsModuleTest_105
1666  * @tc.name      : ANS_Module_Test_10500
1667  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1668  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0105, Function | SmallTest | Level1)1669 HWTEST_F(AnsModuleTest, AnsModuleTest_0105, Function | SmallTest | Level1)
1670 {
1671     std::vector<sptr<NotificationSlot>> slots;
1672     sptr<NotificationSlot> socialSlot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1673     sptr<NotificationSlot> reminderSlot = new NotificationSlot(NotificationConstant::SlotType::SERVICE_REMINDER);
1674     sptr<NotificationSlot> contentSlot = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
1675     sptr<NotificationSlot> otherSlot = new NotificationSlot(NotificationConstant::SlotType::OTHER);
1676     sptr<NotificationSlot> customSlot = new NotificationSlot(NotificationConstant::SlotType::CUSTOM);
1677 
1678     slots.push_back(socialSlot);
1679     slots.push_back(reminderSlot);
1680     slots.push_back(contentSlot);
1681     slots.push_back(otherSlot);
1682     slots.push_back(customSlot);
1683 
1684     g_advancedNotificationService->AddSlots(slots);
1685     EXPECT_EQ(0, g_advancedNotificationService->AddSlots(slots));
1686 }
1687 
1688 /**
1689  * @tc.number    : AnsModuleTest_106
1690  * @tc.name      : ANS_Module_Test_10600
1691  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1692  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0106, Function | SmallTest | Level1)1693 HWTEST_F(AnsModuleTest, AnsModuleTest_0106, Function | SmallTest | Level1)
1694 {
1695     // create wantagent
1696     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> agent =
1697         std::make_shared<AbilityRuntime::WantAgent::WantAgent>();
1698 
1699     // subscriber
1700     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1701     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1702     auto listener = new (std::nothrow) SubscriberListener(ptr);
1703     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1704     subscriberInfo->AddAppName("bundleName");
1705     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
1706     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1707     subscriber->consumedCb_ =
1708         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
1709             passed = true;
1710         };
1711 
1712     // set disturb mode
1713     g_advancedNotificationService->SetNotificationsEnabledForBundle("bundleName", false);
1714 
1715     // create request
1716     std::string label = "testLabel";
1717     sptr<NotificationRequest> req = new NotificationRequest(0);
1718     req->SetLabel(label);
1719     req->SetWantAgent(agent);
1720     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
1721     std::shared_ptr<NotificationLongTextContent> longTextContent =
1722         std::make_shared<NotificationLongTextContent>("longtext");
1723     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
1724     req->SetContent(content2);
1725 
1726     // publish request
1727     g_advancedNotificationService->Publish(label, req);
1728     std::this_thread::sleep_for(std::chrono::milliseconds(200));
1729     EXPECT_TRUE(passed);
1730     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1731 }
1732 
1733 /**
1734  * @tc.number    : AnsModuleTest_107
1735  * @tc.name      : ANS_Module_Test_10700
1736  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1737  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0107, Function | SmallTest | Level1)1738 HWTEST_F(AnsModuleTest, AnsModuleTest_0107, Function | SmallTest | Level1)
1739 {
1740     // subscriber
1741     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1742     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1743     auto listener = new (std::nothrow) SubscriberListener(ptr);
1744     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1745     subscriberInfo->AddAppName("bundleName");
1746     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
1747     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1748 
1749     // add slot
1750     std::vector<sptr<NotificationSlot>> slots;
1751     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::OTHER);
1752     slots.push_back(slot0);
1753     g_advancedNotificationService->AddSlots(slots);
1754 
1755     // create request
1756     std::string label = "testLabel";
1757     sptr<NotificationRequest> req = new NotificationRequest(0);
1758     sptr<NotificationRequest> req1 = new NotificationRequest(1);
1759     req->SetLabel(label);
1760     req1->SetLabel(label);
1761 
1762     // set content
1763     std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
1764     normalContent->SetText("1");
1765     normalContent->SetTitle("1");
1766     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
1767     req->SetContent(content);
1768     req1->SetContent(content);
1769 
1770     // publish request
1771     g_advancedNotificationService->Publish(label, req);
1772     g_advancedNotificationService->Publish(label, req1);
1773 
1774     // remove request
1775     g_advancedNotificationService->Delete("_0_1_testLabel_0", NotificationConstant::CANCEL_REASON_DELETE);
1776     g_advancedNotificationService->Delete("_0_1_testLabel_1", NotificationConstant::CANCEL_REASON_DELETE);
1777     uint64_t nums = 0;
1778     g_advancedNotificationService->GetActiveNotificationNums(nums);
1779     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1780 }
1781 
1782 /**
1783  * @tc.number    : AnsModuleTest_108
1784  * @tc.name      : ANS_Module_Test_10800
1785  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1786  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0108, Function | SmallTest | Level1)1787 HWTEST_F(AnsModuleTest, AnsModuleTest_0108, Function | SmallTest | Level1)
1788 {
1789     // subscriber
1790     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1791     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1792     auto listener = new (std::nothrow) SubscriberListener(ptr);
1793     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1794     subscriberInfo->AddAppName("bundleName");
1795     subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL);
1796     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1797 
1798     // add slot
1799     std::vector<sptr<NotificationSlot>> slots;
1800     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::OTHER);
1801     slots.push_back(slot0);
1802     g_advancedNotificationService->AddSlots(slots);
1803 
1804     // create request
1805     std::string label = "testLabel";
1806     sptr<NotificationRequest> req = new NotificationRequest(0);
1807     sptr<NotificationRequest> req1 = new NotificationRequest(1);
1808     req->SetLabel(label);
1809     req1->SetLabel(label);
1810     req->SetNotificationId(0);
1811     req1->SetNotificationId(1);
1812 
1813     // set content
1814     std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
1815     normalContent->SetText("1");
1816     normalContent->SetTitle("1");
1817     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
1818     req->SetContent(content);
1819     req1->SetContent(content);
1820 
1821     // publish request
1822     g_advancedNotificationService->Publish(label, req);
1823     g_advancedNotificationService->Publish(label, req1);
1824 
1825     // remove request
1826     g_advancedNotificationService->DeleteAllByUser(0);
1827     uint64_t nums = 0;
1828     g_advancedNotificationService->GetActiveNotificationNums(nums);
1829     EXPECT_EQ(nums, 0);
1830     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1831 }
1832 
1833 /**
1834  * @tc.number    : AnsModuleTest_110
1835  * @tc.name      : ANS_Module_Test_11000
1836  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1837  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0110, Function | SmallTest | Level1)1838 HWTEST_F(AnsModuleTest, AnsModuleTest_0110, Function | SmallTest | Level1)
1839 {
1840     // subscriber
1841     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1842     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1843     auto listener = new (std::nothrow) SubscriberListener(ptr);
1844     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1845     subscriberInfo->AddAppName("bundleName");
1846     subscriber->unSubscriberCb_ = []() { passed = true; };
1847     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1848 
1849     // unsubscriber
1850     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1851     EXPECT_EQ(passed, true);
1852 }
1853 
1854 /**
1855  * @tc.number    : AnsModuleTest_111
1856  * @tc.name      : ANS_Module_Test_11100
1857  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1858  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0111, Function | SmallTest | Level1)1859 HWTEST_F(AnsModuleTest, AnsModuleTest_0111, Function | SmallTest | Level1)
1860 {
1861     // subscriber
1862     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1863     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1864     auto listener = new (std::nothrow) SubscriberListener(ptr);
1865     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
1866     subscriberInfo->AddAppName("bundleName");
1867     subscriber->subscriberCb_ = []() { passed = true; };
1868     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
1869     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
1870     EXPECT_EQ(passed, true);
1871 }
1872 
1873 /**
1874  * @tc.number    : AnsModuleTest_112
1875  * @tc.name      : ANS_Module_Test_11200
1876  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1877  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0112, Function | SmallTest | Level1)1878 HWTEST_F(AnsModuleTest, AnsModuleTest_0112, Function | SmallTest | Level1)
1879 {
1880     // subscriber
1881     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1882     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1883     auto listener = new (std::nothrow) SubscriberListener(ptr);
1884     g_advancedNotificationService->Subscribe(listener, nullptr);
1885     subscriber->consumedCb_ =
1886         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
1887             std::vector<std::string> sortingKey = sortingMap->GetKey();
1888 
1889             NotificationSorting sorting1;
1890             NotificationSorting sorting2;
1891             if (sortingKey.size() == 2) {
1892                 sortingMap->GetNotificationSorting("_0_1_testLabel_0", sorting1);
1893                 sortingMap->GetNotificationSorting("_0_1_testLabel_1", sorting2);
1894             }
1895 
1896             if (sorting1.GetRanking() < sorting2.GetRanking() && notification->EnableLight() &&
1897                 notification->EnableSound() && notification->EnableVibrate()) {
1898                 passed = true;
1899             }
1900         };
1901 
1902     // add slot
1903     std::vector<sptr<NotificationSlot>> slots;
1904     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1905     slot->SetSound(Uri("."));
1906     slot->SetEnableLight(true);
1907     slot->SetEnableVibration(true);
1908     slot->SetVibrationStyle(std::vector<int64_t>(1, 1));
1909     slot->SetLedLightColor(1);
1910     slots.push_back(slot);
1911     g_advancedNotificationService->AddSlots(slots);
1912 
1913     // create request
1914     std::string label = "testLabel";
1915     sptr<NotificationRequest> req = new NotificationRequest(0);
1916     sptr<NotificationRequest> req1 = new NotificationRequest(1);
1917     req->SetLabel(label);
1918     req1->SetLabel(label);
1919     std::shared_ptr<NotificationMultiLineContent> contentImpl = std::make_shared<NotificationMultiLineContent>();
1920     contentImpl->SetText("1");
1921     contentImpl->SetTitle("1");
1922     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
1923     req->SetContent(content);
1924     req1->SetContent(content);
1925     req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1926     req1->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
1927 
1928     // publish request
1929     g_advancedNotificationService->Publish(label, req);
1930     g_advancedNotificationService->Publish(label, req1);
1931     EXPECT_TRUE(passed);
1932     g_advancedNotificationService->Unsubscribe(listener, nullptr);
1933 }
1934 
1935 /**
1936  * @tc.number    : AnsModuleTest_113
1937  * @tc.name      : ANS_Module_Test_11300
1938  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
1939  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0113, Function | SmallTest | Level1)1940 HWTEST_F(AnsModuleTest, AnsModuleTest_0113, Function | SmallTest | Level1)
1941 {
1942     // subscriber
1943     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
1944     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
1945     auto listener = new (std::nothrow) SubscriberListener(ptr);
1946     g_advancedNotificationService->Subscribe(listener, nullptr);
1947     subscriber->consumedCb_ =
1948         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
1949             std::vector<std::string> sortingKey = sortingMap->GetKey();
1950 
1951             NotificationSorting sorting1;
1952             NotificationSorting sorting2;
1953             if (sortingKey.size() == 2) {
1954                 sortingMap->GetNotificationSorting("_1_testLabel_0", sorting1);
1955                 sortingMap->GetNotificationSorting("_1_testLabel_1", sorting2);
1956             }
1957             if (sorting1.GetRanking() < sorting2.GetRanking() && notification->EnableLight() &&
1958                 notification->EnableSound() && notification->EnableVibrate()) {
1959                 passed = true;
1960             }
1961         };
1962 
1963     // add slot
1964     std::vector<sptr<NotificationSlot>> slots;
1965     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::OTHER);
1966     slot->SetSound(Uri("."));
1967     slot->SetEnableLight(true);
1968     slot->SetEnableVibration(true);
1969     slot->SetVibrationStyle(std::vector<int64_t>(1, 1));
1970     slot->SetLedLightColor(1);
1971     slots.push_back(slot);
1972     g_advancedNotificationService->AddSlots(slots);
1973 
1974     // create request
1975     std::string label = "testLabel";
1976     sptr<NotificationRequest> req = new NotificationRequest(0);
1977     sptr<NotificationRequest> req1 = new NotificationRequest(1);
1978     req->SetLabel(label);
1979     req1->SetLabel(label);
1980     std::shared_ptr<NotificationLongTextContent> contentImpl = std::make_shared<NotificationLongTextContent>();
1981     contentImpl->SetText("1");
1982     contentImpl->SetTitle("1");
1983     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
1984     req->SetContent(content);
1985     req1->SetContent(content);
1986     req->SetSlotType(NotificationConstant::SlotType::OTHER);
1987     req1->SetSlotType(NotificationConstant::SlotType::OTHER);
1988 
1989     // publish request
1990     g_advancedNotificationService->Publish(label, req);
1991     g_advancedNotificationService->Publish(label, req1);
1992     g_advancedNotificationService->Unsubscribe(listener, nullptr);
1993     EXPECT_TRUE(passed);
1994 }
1995 
1996 /**
1997  * @tc.number    : AnsModuleTest_114
1998  * @tc.name      : ANS_Module_Test_11400
1999  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
2000  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0114, Function | SmallTest | Level1)2001 HWTEST_F(AnsModuleTest, AnsModuleTest_0114, Function | SmallTest | Level1)
2002 {
2003     // subscriber
2004     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2005     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2006     auto listener = new (std::nothrow) SubscriberListener(ptr);
2007     g_advancedNotificationService->Subscribe(listener, nullptr);
2008     subscriber->consumedCb_ =
2009         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
2010             std::vector<std::string> sortingKey = sortingMap->GetKey();
2011 
2012             NotificationSorting sorting1;
2013             NotificationSorting sorting2;
2014             if (sortingKey.size() == 2) {
2015                 sortingMap->GetNotificationSorting("_1_testLabel_0", sorting1);
2016                 sortingMap->GetNotificationSorting("_1_testLabel_1", sorting2);
2017             }
2018             if (sorting1.GetRanking() < sorting2.GetRanking() && notification->EnableLight() &&
2019                 notification->EnableSound() && notification->EnableVibrate()) {
2020                 passed = true;
2021             }
2022         };
2023 
2024     // add slot
2025     std::vector<sptr<NotificationSlot>> slots;
2026     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
2027     slot->SetSound(Uri("."));
2028     slot->SetEnableLight(true);
2029     slot->SetEnableVibration(true);
2030     slot->SetLockscreenVisibleness(NotificationConstant::VisiblenessType::PUBLIC);
2031     slot->SetLedLightColor(1);
2032     slot->SetVibrationStyle(std::vector<int64_t>(1, 1));
2033     slots.push_back(slot);
2034     g_advancedNotificationService->AddSlots(slots);
2035 
2036     // create request
2037     std::string label = "testLabel";
2038     sptr<NotificationRequest> req = new NotificationRequest(0);
2039     sptr<NotificationRequest> req1 = new NotificationRequest(1);
2040     req->SetLabel(label);
2041     req1->SetLabel(label);
2042     std::shared_ptr<NotificationNormalContent> contentImpl = std::make_shared<NotificationNormalContent>();
2043     contentImpl->SetText("1");
2044     contentImpl->SetTitle("1");
2045     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
2046     req->SetContent(content);
2047     req1->SetContent(content);
2048     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
2049     req1->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
2050     // publish request
2051     g_advancedNotificationService->Publish(label, req);
2052     g_advancedNotificationService->Publish(label, req1);
2053     g_advancedNotificationService->Unsubscribe(listener, nullptr);
2054     EXPECT_TRUE(passed);
2055 }
2056 
2057 /**
2058  * @tc.number    : AnsModuleTest_116
2059  * @tc.name      : ANS_Module_Test_11600
2060  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
2061  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0116, Function | SmallTest | Level1)2062 HWTEST_F(AnsModuleTest, AnsModuleTest_0116, Function | SmallTest | Level1)
2063 {
2064     // subscriber
2065     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2066     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2067     auto listener = new (std::nothrow) SubscriberListener(ptr);
2068     g_advancedNotificationService->Subscribe(listener, nullptr);
2069     subscriber->consumedCb_ =
2070         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
2071             std::vector<std::string> sortingKey = sortingMap->GetKey();
2072 
2073             NotificationSorting sorting1;
2074             NotificationSorting sorting2;
2075             if (sortingKey.size() == 2) {
2076                 sortingMap->GetNotificationSorting("_1_testLabel_0", sorting1);
2077                 sortingMap->GetNotificationSorting("_1_testLabel_1", sorting2);
2078             }
2079             if (sorting1.GetRanking() < sorting2.GetRanking() && notification->EnableLight() &&
2080                 notification->EnableSound() && notification->EnableVibrate()) {
2081                 passed = true;
2082             }
2083         };
2084 
2085     // add slot
2086     std::vector<sptr<NotificationSlot>> slots;
2087     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::SERVICE_REMINDER);
2088     slot->SetSound(Uri("."));
2089     slot->SetEnableLight(true);
2090     slot->SetEnableVibration(true);
2091     slot->SetLockscreenVisibleness(NotificationConstant::VisiblenessType::PUBLIC);
2092     slot->SetLedLightColor(1);
2093     slot->SetVibrationStyle(std::vector<int64_t>(1, 1));
2094     slots.push_back(slot);
2095     g_advancedNotificationService->AddSlots(slots);
2096 
2097     // create request
2098     std::string label = "testLabel";
2099     sptr<NotificationRequest> req = new NotificationRequest(0);
2100     sptr<NotificationRequest> req1 = new NotificationRequest(1);
2101     req->SetLabel(label);
2102     req1->SetLabel(label);
2103     std::shared_ptr<NotificationNormalContent> contentImpl = std::make_shared<NotificationNormalContent>();
2104     contentImpl->SetText("1");
2105     contentImpl->SetTitle("1");
2106     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
2107     req->SetContent(content);
2108     req1->SetContent(content);
2109     req->SetSlotType(NotificationConstant::SlotType::SERVICE_REMINDER);
2110     req1->SetSlotType(NotificationConstant::SlotType::SERVICE_REMINDER);
2111 
2112     // publish request
2113     g_advancedNotificationService->Publish(label, req);
2114     g_advancedNotificationService->Publish(label, req1);
2115     EXPECT_TRUE(passed);
2116     g_advancedNotificationService->Unsubscribe(listener, nullptr);
2117 }
2118 
2119 /**
2120  * @tc.number    : AnsModuleTest_117
2121  * @tc.name      : ANS_Module_Test_11700
2122  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
2123  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0117, Function | SmallTest | Level1)2124 HWTEST_F(AnsModuleTest, AnsModuleTest_0117, Function | SmallTest | Level1)
2125 {
2126     // subscriber
2127     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2128     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2129     auto listener = new (std::nothrow) SubscriberListener(ptr);
2130     g_advancedNotificationService->Subscribe(listener, nullptr);
2131     subscriber->consumedCb_ =
2132         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
2133             std::vector<std::string> sortingKey = sortingMap->GetKey();
2134 
2135             NotificationSorting sorting1;
2136             NotificationSorting sorting2;
2137             if (sortingKey.size() == 2) {
2138                 sortingMap->GetNotificationSorting("_1_testLabel_0", sorting1);
2139                 sortingMap->GetNotificationSorting("_1_testLabel_1", sorting2);
2140             }
2141             if (sorting1.GetRanking() < sorting2.GetRanking() && notification->EnableLight() &&
2142                 notification->EnableSound() && notification->EnableVibrate()) {
2143                 passed = true;
2144             }
2145         };
2146 
2147     // add slot
2148     std::vector<sptr<NotificationSlot>> slots;
2149     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2150     slot->SetSound(Uri("."));
2151     slot->SetEnableLight(true);
2152     slot->SetEnableVibration(true);
2153     slot->SetLockscreenVisibleness(NotificationConstant::VisiblenessType::PUBLIC);
2154     slot->SetLedLightColor(1);
2155     slot->SetVibrationStyle(std::vector<int64_t>(1, 1));
2156     slots.push_back(slot);
2157     g_advancedNotificationService->AddSlots(slots);
2158 
2159     // create request
2160     std::string label = "testLabel";
2161     sptr<NotificationRequest> req = new NotificationRequest(0);
2162     sptr<NotificationRequest> req1 = new NotificationRequest(1);
2163     req->SetLabel(label);
2164     req1->SetLabel(label);
2165     std::shared_ptr<NotificationNormalContent> contentImpl = std::make_shared<NotificationNormalContent>();
2166     contentImpl->SetText("1");
2167     contentImpl->SetTitle("1");
2168     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
2169     req->SetContent(content);
2170     req1->SetContent(content);
2171     req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2172     req1->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2173 
2174     // publish request
2175     g_advancedNotificationService->Publish(label, req);
2176     g_advancedNotificationService->Publish(label, req1);
2177     EXPECT_TRUE(passed);
2178     g_advancedNotificationService->Unsubscribe(listener, nullptr);
2179 }
2180 
2181 /**
2182  * @tc.number    : AnsModuleTest_120
2183  * @tc.name      : ANS_Module_Test_12000
2184  * @tc.desc      : Test publish notifications when Disturb are not allowed publish.
2185  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0120, Function | SmallTest | Level1)2186 HWTEST_F(AnsModuleTest, AnsModuleTest_0120, Function | SmallTest | Level1)
2187 {
2188     // subscriber
2189     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2190     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2191     auto listener = new (std::nothrow) SubscriberListener(ptr);
2192     sptr<NotificationSubscribeInfo> subscriberInfo = new NotificationSubscribeInfo();
2193     g_advancedNotificationService->Subscribe(listener, subscriberInfo);
2194     subscriber->consumedCb_ = [](const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>) {
2195         passed = true;
2196     };
2197 
2198     // add slot
2199     std::vector<sptr<NotificationSlot>> slots;
2200     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2201     slots.push_back(slot0);
2202     g_advancedNotificationService->AddSlots(slots);
2203 
2204     // create request
2205     std::string label = "testLabel";
2206     sptr<NotificationRequest> req = new NotificationRequest(0);
2207     req->SetLabel(label);
2208     req->SetStatusBarText("text");
2209     std::shared_ptr<NotificationNormalContent> contentImpl = std::make_shared<NotificationNormalContent>();
2210     contentImpl->SetText("1");
2211     contentImpl->SetTitle("1");
2212     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
2213     req->SetContent(content);
2214     req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2215 
2216     g_advancedNotificationService->SetNotificationsEnabledForBundle("bundleName", false);
2217 
2218     g_advancedNotificationService->Publish(label, req);
2219     std::this_thread::sleep_for(std::chrono::milliseconds(200));
2220     EXPECT_EQ(true, passed);
2221     g_advancedNotificationService->Unsubscribe(listener, subscriberInfo);
2222 }
2223 
2224 /**
2225  * @tc.number    : AnsModuleTest_0121
2226  * @tc.name      : ANS_Module_Test_12100
2227  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
2228  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0121, Function | SmallTest | Level1)2229 HWTEST_F(AnsModuleTest, AnsModuleTest_0121, Function | SmallTest | Level1)
2230 {
2231     // add slot
2232     std::vector<sptr<NotificationSlot>> slots;
2233     sptr<NotificationSlot> socialSlot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2234     sptr<NotificationSlot> reminderSlot = new NotificationSlot(NotificationConstant::SlotType::SERVICE_REMINDER);
2235     sptr<NotificationSlot> contentSlot = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
2236     sptr<NotificationSlot> otherSlot = new NotificationSlot(NotificationConstant::SlotType::OTHER);
2237     sptr<NotificationSlot> customSlot = new NotificationSlot(NotificationConstant::SlotType::CUSTOM);
2238 
2239     slots.push_back(socialSlot);
2240     slots.push_back(reminderSlot);
2241     slots.push_back(contentSlot);
2242     slots.push_back(otherSlot);
2243     slots.push_back(customSlot);
2244 
2245     EXPECT_EQ(g_advancedNotificationService->AddSlots(slots), 0);
2246     EXPECT_EQ(g_advancedNotificationService->RemoveSlotByType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION), 0);
2247     EXPECT_EQ(g_advancedNotificationService->RemoveSlotByType(NotificationConstant::SlotType::SERVICE_REMINDER), 0);
2248     EXPECT_EQ(g_advancedNotificationService->RemoveSlotByType(NotificationConstant::SlotType::CONTENT_INFORMATION), 0);
2249     EXPECT_EQ(g_advancedNotificationService->RemoveSlotByType(NotificationConstant::SlotType::OTHER), 0);
2250     EXPECT_EQ(g_advancedNotificationService->RemoveSlotByType(NotificationConstant::SlotType::CUSTOM), 0);
2251 }
2252 
2253 /**
2254  * @tc.number    : AnsModuleTest_0122
2255  * @tc.name      : ANS_Module_Test_12200
2256  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
2257  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0122, Function | SmallTest | Level1)2258 HWTEST_F(AnsModuleTest, AnsModuleTest_0122, Function | SmallTest | Level1)
2259 {
2260     // subscriber
2261     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2262     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2263     auto listener = new (std::nothrow) SubscriberListener(ptr);
2264     g_advancedNotificationService->Subscribe(listener, nullptr);
2265     subscriber->consumedCb_ =
2266         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
2267             passed = true;
2268         };
2269 
2270     subscriber->canceledCb_ = [](const std::shared_ptr<Notification> &request,
2271                                   const std::shared_ptr<NotificationSortingMap> &sortingMap,
2272                                   int deleteReason) { passed = true; };
2273 
2274     // add slot
2275     std::vector<sptr<NotificationSlot>> slots;
2276     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2277     slots.push_back(slot);
2278     g_advancedNotificationService->AddSlots(slots);
2279 
2280     // create request
2281     std::string label = "testLabel";
2282     sptr<NotificationRequest> req = new NotificationRequest(0);
2283     sptr<NotificationRequest> req1 = new NotificationRequest(1);
2284     req->SetLabel(label);
2285     req1->SetLabel(label);
2286     req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2287     req1->SetSlotType(NotificationConstant::SlotType::OTHER);
2288 
2289     // publish request
2290 
2291     // remove social slot
2292     EXPECT_EQ(0, g_advancedNotificationService->RemoveSlotByType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION));
2293 
2294     // add slot
2295     std::vector<sptr<NotificationSlot>> otherSlots;
2296     slot = new NotificationSlot(NotificationConstant::SlotType::OTHER);
2297     otherSlots.push_back(slot);
2298     EXPECT_EQ(0, g_advancedNotificationService->AddSlots(otherSlots));
2299 
2300     EXPECT_FALSE(passed);
2301     g_advancedNotificationService->Unsubscribe(listener, nullptr);
2302 }
2303 
2304 /**
2305  * @tc.number    : AnsModuleTest_0123
2306  * @tc.name      : ANS_Module_Test_12300
2307  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
2308  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0123, Function | SmallTest | Level1)2309 HWTEST_F(AnsModuleTest, AnsModuleTest_0123, Function | SmallTest | Level1)
2310 {
2311     int ret = 0;
2312     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2313     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2314     auto listener = new (std::nothrow) SubscriberListener(ptr);
2315     g_advancedNotificationService->Subscribe(listener, nullptr);
2316     subscriber->consumedCb_ = [&ret](const std::shared_ptr<Notification> notification,
2317                                   const std::shared_ptr<NotificationSortingMap>
2318                                       sortingMap) { ret++; };
2319     subscriber->canceledCb_ = [](const std::shared_ptr<Notification> &request,
2320                                   const std::shared_ptr<NotificationSortingMap> &sortingMap,
2321                                   int deleteReason) { passed = true; };
2322     std::vector<sptr<NotificationSlot>> slots;
2323     slots.push_back(new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION));
2324     slots.push_back(new NotificationSlot(NotificationConstant::SlotType::SERVICE_REMINDER));
2325     slots.push_back(new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION));
2326     slots.push_back(new NotificationSlot(NotificationConstant::SlotType::OTHER));
2327     slots.push_back(new NotificationSlot(NotificationConstant::SlotType::CUSTOM));
2328     EXPECT_EQ(g_advancedNotificationService->AddSlots(slots), 0);
2329     std::shared_ptr<NotificationLongTextContent> longTextContent =
2330         std::make_shared<NotificationLongTextContent>("longtext");
2331     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
2332     sptr<NotificationRequest> req = new NotificationRequest(0);
2333     sptr<NotificationRequest> req1 = new NotificationRequest(1);
2334     sptr<NotificationRequest> req2 = new NotificationRequest(2);
2335     sptr<NotificationRequest> req3 = new NotificationRequest(3);
2336     sptr<NotificationRequest> req4 = new NotificationRequest(4);
2337 
2338     req->SetLabel("testLabel");
2339     req1->SetLabel("testLabel");
2340     req2->SetLabel("testLabel");
2341     req3->SetLabel("testLabel");
2342     req4->SetLabel("testLabel");
2343 
2344     req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2345     req1->SetSlotType(NotificationConstant::SlotType::SERVICE_REMINDER);
2346     req2->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
2347     req3->SetSlotType(NotificationConstant::SlotType::OTHER);
2348     req4->SetSlotType(NotificationConstant::SlotType::CUSTOM);
2349     req->SetContent(content2);
2350     req1->SetContent(content2);
2351     req2->SetContent(content2);
2352     req3->SetContent(content2);
2353     req4->SetContent(content2);
2354 
2355     g_advancedNotificationService->Publish("testLabel", req);
2356     std::this_thread::sleep_for(std::chrono::milliseconds(200));
2357     EXPECT_EQ(ret, 1);
2358     g_advancedNotificationService->Publish("testLabel", req1);
2359     std::this_thread::sleep_for(std::chrono::milliseconds(200));
2360     EXPECT_EQ(ret, 2);
2361     g_advancedNotificationService->Publish("testLabel", req2);
2362     std::this_thread::sleep_for(std::chrono::milliseconds(200));
2363     EXPECT_EQ(ret, 3);
2364     g_advancedNotificationService->Publish("testLabel", req3);
2365     std::this_thread::sleep_for(std::chrono::milliseconds(200));
2366     EXPECT_EQ(ret, 4);
2367     g_advancedNotificationService->Publish("testLabel", req4);
2368     std::this_thread::sleep_for(std::chrono::milliseconds(200));
2369     EXPECT_EQ(ret, 5);
2370     g_advancedNotificationService->DeleteAll();
2371     g_advancedNotificationService->Unsubscribe(listener, nullptr);
2372     std::this_thread::sleep_for(std::chrono::milliseconds(200));
2373     EXPECT_TRUE(passed);
2374 }
2375 
2376 /**
2377  * @tc.number    : AnsModuleTest_0124
2378  * @tc.name      : ANS_Module_Test_12400
2379  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
2380  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0124, Function | SmallTest | Level1)2381 HWTEST_F(AnsModuleTest, AnsModuleTest_0124, Function | SmallTest | Level1)
2382 {
2383     // subscriber
2384     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2385     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2386     auto listener = new (std::nothrow) SubscriberListener(ptr);
2387     g_advancedNotificationService->Subscribe(listener, nullptr);
2388     subscriber->consumedCb_ =
2389         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
2390             passed = true;
2391         };
2392 
2393     // add slot
2394     std::vector<sptr<NotificationSlot>> slots;
2395     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
2396     slots.push_back(slot);
2397     g_advancedNotificationService->AddSlots(slots);
2398 
2399     // create request
2400     std::string label = "testLabel";
2401     sptr<NotificationRequest> req = new NotificationRequest(0);
2402     req->SetLabel(label);
2403     std::shared_ptr<NotificationMediaContent> contentImpl = std::make_shared<NotificationMediaContent>();
2404     contentImpl->SetText("1");
2405     contentImpl->SetTitle("1");
2406     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
2407     req->SetContent(content);
2408     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
2409 
2410     // publish request
2411     g_advancedNotificationService->Publish(label, req);
2412     g_advancedNotificationService->Unsubscribe(listener, nullptr);
2413     EXPECT_TRUE(passed);
2414 }
2415 
2416 /**
2417  * @tc.number    : AnsModuleTest_0125
2418  * @tc.name      : ANS_Module_Test_12500
2419  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
2420  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0125, Function | SmallTest | Level1)2421 HWTEST_F(AnsModuleTest, AnsModuleTest_0125, Function | SmallTest | Level1)
2422 {
2423     // subscriber
2424     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2425     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2426     auto listener = new (std::nothrow) SubscriberListener(ptr);
2427     g_advancedNotificationService->Subscribe(listener, nullptr);
2428     subscriber->consumedCb_ =
2429         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
2430             passed = true;
2431         };
2432 
2433     // add slot
2434     std::vector<sptr<NotificationSlot>> slots;
2435     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2436     slots.push_back(slot);
2437     g_advancedNotificationService->AddSlots(slots);
2438 
2439     // create request
2440     std::string label = "testLabel";
2441     sptr<NotificationRequest> req = new NotificationRequest(0);
2442     req->SetLabel(label);
2443     req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2444     std::shared_ptr<NotificationLongTextContent> longTextContent =
2445         std::make_shared<NotificationLongTextContent>("longtext");
2446     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
2447     req->SetContent(content2);
2448     // publish request
2449     g_advancedNotificationService->Publish(label, req);
2450     g_advancedNotificationService->Unsubscribe(listener, nullptr);
2451     EXPECT_TRUE(passed);
2452 }
2453 
2454 /**
2455  * @tc.number    : AnsModuleTest_0126
2456  * @tc.name      : ANS_Module_Test_12600
2457  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
2458  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0126, Function | SmallTest | Level1)2459 HWTEST_F(AnsModuleTest, AnsModuleTest_0126, Function | SmallTest | Level1)
2460 {
2461     // subscriber
2462     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2463     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2464     auto listener = new (std::nothrow) SubscriberListener(ptr);
2465     g_advancedNotificationService->Subscribe(listener, nullptr);
2466     subscriber->consumedCb_ =
2467         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
2468             passed = true;
2469         };
2470 
2471     // add slot
2472     std::vector<sptr<NotificationSlot>> slots;
2473     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::SERVICE_REMINDER);
2474     slots.push_back(slot);
2475     g_advancedNotificationService->AddSlots(slots);
2476 
2477     // create request
2478     std::string label = "testLabel";
2479     sptr<NotificationRequest> req = new NotificationRequest(0);
2480     req->SetLabel(label);
2481     std::shared_ptr<NotificationPictureContent> contentImpl = std::make_shared<NotificationPictureContent>();
2482     contentImpl->SetText("1");
2483     contentImpl->SetTitle("1");
2484     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(contentImpl);
2485     req->SetContent(content);
2486     req->SetSlotType(NotificationConstant::SlotType::SERVICE_REMINDER);
2487 
2488     // publish request
2489     g_advancedNotificationService->Publish(label, req);
2490     g_advancedNotificationService->Unsubscribe(listener, nullptr);
2491     EXPECT_TRUE(passed);
2492 }
2493 
2494 /**
2495  * @tc.number    : AnsModuleTest_0127
2496  * @tc.name      : ANS_Module_Test_12700
2497  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
2498  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0127, Function | SmallTest | Level1)2499 HWTEST_F(AnsModuleTest, AnsModuleTest_0127, Function | SmallTest | Level1)
2500 {
2501     const int EXPECT_REQUST_NUM = 2;
2502 
2503     int ret = 0;
2504     // subscriber
2505     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2506     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2507     auto listener = new (std::nothrow) SubscriberListener(ptr);
2508     g_advancedNotificationService->Subscribe(listener, nullptr);
2509     subscriber->consumedCb_ = [&ret](const std::shared_ptr<Notification> notification,
2510                                   const std::shared_ptr<NotificationSortingMap>
2511                                       sortingMap) { ret++; };
2512 
2513     // add slot
2514     std::vector<sptr<NotificationSlot>> slots;
2515     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2516     sptr<NotificationSlot> slot1 = new NotificationSlot(NotificationConstant::SlotType::SERVICE_REMINDER);
2517     slots.push_back(slot);
2518     slots.push_back(slot1);
2519     g_advancedNotificationService->AddSlots(slots);
2520 
2521     // create content
2522     std::shared_ptr<NotificationPictureContent> contentImpl = std::make_shared<NotificationPictureContent>();
2523     contentImpl->SetText("1");
2524     contentImpl->SetTitle("1");
2525     std::shared_ptr<NotificationContent> pictureContent = std::make_shared<NotificationContent>(contentImpl);
2526 
2527     std::shared_ptr<NotificationLongTextContent> contentImpl1 = std::make_shared<NotificationLongTextContent>();
2528     contentImpl->SetText("1");
2529     contentImpl->SetTitle("1");
2530     std::shared_ptr<NotificationContent> longTextContent = std::make_shared<NotificationContent>(contentImpl);
2531 
2532     // create request
2533     std::string label = "testLabel";
2534     sptr<NotificationRequest> req = new NotificationRequest(0);
2535     sptr<NotificationRequest> req1 = new NotificationRequest(1);
2536     req->SetLabel(label);
2537     req->SetContent(pictureContent);
2538     req1->SetLabel(label);
2539     req1->SetContent(longTextContent);
2540     req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2541     req1->SetSlotType(NotificationConstant::SlotType::SERVICE_REMINDER);
2542 
2543     // publish
2544     EXPECT_EQ(g_advancedNotificationService->Publish(label, req), ERR_OK);
2545     EXPECT_EQ(g_advancedNotificationService->Publish(label, req1), ERR_OK);
2546     g_advancedNotificationService->Unsubscribe(listener, nullptr);
2547     EXPECT_EQ(ret, EXPECT_REQUST_NUM);
2548 }
2549 
2550 /**
2551  * @tc.number    : AnsModuleTest_0128
2552  * @tc.name      : ANS_Module_Test_12800
2553  * @tc.desc      : Test publish notification when slot type is CONTENT_INFORMATION.
2554  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0128, Function | SmallTest | Level1)2555 HWTEST_F(AnsModuleTest, AnsModuleTest_0128, Function | SmallTest | Level1)
2556 {
2557     const int EXPECT_REQUST_NUM = 2;
2558 
2559     int ret = 0;
2560     // subscriber
2561     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2562     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2563     auto listener = new (std::nothrow) SubscriberListener(ptr);
2564     g_advancedNotificationService->Subscribe(listener, nullptr);
2565     subscriber->consumedCb_ = [&ret](const std::shared_ptr<Notification> notification,
2566                                   const std::shared_ptr<NotificationSortingMap>
2567                                       sortingMap) { ret++; };
2568 
2569     // add slot
2570     std::vector<sptr<NotificationSlot>> slots;
2571     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::CONTENT_INFORMATION);
2572     sptr<NotificationSlot> slot1 = new NotificationSlot(NotificationConstant::SlotType::OTHER);
2573     slots.push_back(slot);
2574     slots.push_back(slot1);
2575     g_advancedNotificationService->AddSlots(slots);
2576 
2577     // create content
2578     std::shared_ptr<NotificationPictureContent> contentImpl = std::make_shared<NotificationPictureContent>();
2579     contentImpl->SetText("1");
2580     contentImpl->SetTitle("1");
2581     std::shared_ptr<NotificationContent> pictureContent = std::make_shared<NotificationContent>(contentImpl);
2582 
2583     std::shared_ptr<NotificationLongTextContent> contentImpl1 = std::make_shared<NotificationLongTextContent>();
2584     contentImpl->SetText("1");
2585     contentImpl->SetTitle("1");
2586     std::shared_ptr<NotificationContent> longTextContent = std::make_shared<NotificationContent>(contentImpl);
2587 
2588     // create request
2589     std::string label = "testLabel";
2590     sptr<NotificationRequest> req = new NotificationRequest(0);
2591     sptr<NotificationRequest> req1 = new NotificationRequest(1);
2592     req->SetLabel(label);
2593     req->SetContent(pictureContent);
2594     req1->SetLabel(label);
2595     req1->SetContent(longTextContent);
2596     req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION);
2597     req1->SetSlotType(NotificationConstant::SlotType::OTHER);
2598 
2599     // publish
2600     EXPECT_EQ(g_advancedNotificationService->Publish(label, req), ERR_OK);
2601     EXPECT_EQ(g_advancedNotificationService->Publish(label, req1), ERR_OK);
2602     g_advancedNotificationService->Unsubscribe(listener, nullptr);
2603     EXPECT_EQ(ret, EXPECT_REQUST_NUM);
2604 }
2605 
2606 /**
2607  * @tc.number    : AnsModuleTest_0130
2608  * @tc.name      : ANS_Module_Test_13000
2609  * @tc.desc      : Test publish notification when slot type is OTHER.
2610  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0130, Function | SmallTest | Level1)2611 HWTEST_F(AnsModuleTest, AnsModuleTest_0130, Function | SmallTest | Level1)
2612 {
2613     // subscriber
2614     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2615     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2616     auto listener = new (std::nothrow) SubscriberListener(ptr);
2617     g_advancedNotificationService->Subscribe(listener, nullptr);
2618     subscriber->consumedCb_ =
2619         [](const std::shared_ptr<Notification> notification, const std::shared_ptr<NotificationSortingMap> sortingMap) {
2620             EXPECT_FALSE(notification->EnableVibrate());
2621             EXPECT_FALSE(notification->EnableSound());
2622         };
2623 
2624     // add slot
2625     std::vector<sptr<NotificationSlot>> slots;
2626     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::OTHER);
2627     slots.push_back(slot);
2628     slot->SetLockscreenVisibleness(NotificationConstant::VisiblenessType::PUBLIC);
2629     slot->SetEnableVibration(true);
2630 
2631     // create request
2632     std::string label = "testLabel";
2633     sptr<NotificationRequest> req = new NotificationRequest(0);
2634     req->SetSlotType(NotificationConstant::SlotType::OTHER);
2635     req->SetLabel(label);
2636     // publish
2637     EXPECT_EQ(g_advancedNotificationService->Publish(label, req), ERR_OK);
2638     g_advancedNotificationService->Unsubscribe(listener, nullptr);
2639 }
2640 
2641 /**
2642  * @tc.number    : AnsModuleTest_0131
2643  * @tc.name      : ANS_Module_Test_13100
2644  * @tc.desc      : Test publish notification when cancel a  notification.
2645  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0131, Function | SmallTest | Level1)2646 HWTEST_F(AnsModuleTest, AnsModuleTest_0131, Function | SmallTest | Level1)
2647 {
2648     // subscriber
2649     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2650     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2651     auto listener = new (std::nothrow) SubscriberListener(ptr);
2652     g_advancedNotificationService->Subscribe(listener, nullptr);
2653     subscriber->canceledCb_ = [](const std::shared_ptr<Notification> &request,
2654                                   const std::shared_ptr<NotificationSortingMap> &sortingMap,
2655                                   int deleteReason) { passed = true; };
2656     g_advancedNotificationService->Cancel(1, "1", 0);
2657     g_advancedNotificationService->Unsubscribe(listener, nullptr);
2658     EXPECT_EQ(false, passed);
2659 }
2660 
2661 /**
2662  * @tc.number    : AnsModuleTest_0132
2663  * @tc.name      : ANS_Module_Test_13200
2664  * @tc.desc      : Test publish notifications when Dnd type is NONE.
2665  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0132, Function | SmallTest | Level1)2666 HWTEST_F(AnsModuleTest, AnsModuleTest_0132, Function | SmallTest | Level1)
2667 {
2668     // subscriber
2669     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2670     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2671     auto listener = new (std::nothrow) SubscriberListener(ptr);
2672     EXPECT_EQ(g_advancedNotificationService->Subscribe(listener, nullptr), ERR_OK);
2673     subscriber->consumedCb_ = [](const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>) {
2674         passed = true;
2675     };
2676 
2677     // add slot
2678     std::vector<sptr<NotificationSlot>> slots;
2679     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2680     slots.push_back(slot0);
2681     g_advancedNotificationService->AddSlots(slots);
2682 
2683     // create request
2684     std::string label = "testLabel";
2685     sptr<NotificationRequest> req = new NotificationRequest(0);
2686     req->SetLabel(label);
2687     req->SetStatusBarText("text");
2688     req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2689     std::shared_ptr<NotificationLongTextContent> longTextContent =
2690         std::make_shared<NotificationLongTextContent>("longtext");
2691     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
2692     req->SetContent(content2);
2693 
2694     sptr<NotificationDoNotDisturbDate> date =
2695         new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
2696     EXPECT_EQ(g_advancedNotificationService->SetDoNotDisturbDate(100, date), ERR_OK);
2697 
2698     EXPECT_EQ(g_advancedNotificationService->Publish(label, req), ERR_OK);
2699     EXPECT_EQ(g_advancedNotificationService->Unsubscribe(listener, nullptr), ERR_OK);
2700     EXPECT_TRUE(passed);
2701 }
2702 
2703 /**
2704  * @tc.number    : AnsModuleTest_0133
2705  * @tc.name      : ANS_Module_Test_13300
2706  * @tc.desc      : Test publish notifications when Dnd type is ONCE.
2707  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0133, Function | SmallTest | Level1)2708 HWTEST_F(AnsModuleTest, AnsModuleTest_0133, Function | SmallTest | Level1)
2709 {
2710     // subscriber
2711     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2712     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2713     auto listener = new (std::nothrow) SubscriberListener(ptr);
2714     EXPECT_EQ(g_advancedNotificationService->Subscribe(listener, nullptr), ERR_OK);
2715     subscriber->consumedCb_ = [](const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>) {
2716         passed = true;
2717     };
2718 
2719     // add slot
2720     std::vector<sptr<NotificationSlot>> slots;
2721     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2722     slots.push_back(slot0);
2723     g_advancedNotificationService->AddSlots(slots);
2724 
2725     // create request
2726     std::string label = "testLabel";
2727     sptr<NotificationRequest> req = new NotificationRequest(0);
2728     req->SetLabel(label);
2729     req->SetStatusBarText("text");
2730     req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2731     std::shared_ptr<NotificationLongTextContent> longTextContent =
2732         std::make_shared<NotificationLongTextContent>("longtext");
2733     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
2734     req->SetContent(content2);
2735 
2736     std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now();
2737     auto beginDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
2738     int64_t beginDate = beginDuration.count();
2739     timePoint += std::chrono::hours(1);
2740     auto endDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
2741     int64_t endDate = endDuration.count();
2742     sptr<NotificationDoNotDisturbDate> date =
2743         new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::ONCE, beginDate, endDate);
2744     EXPECT_EQ(g_advancedNotificationService->SetDoNotDisturbDate(100, date), ERR_OK);
2745 
2746     EXPECT_EQ(g_advancedNotificationService->Publish(label, req), ERR_OK);
2747     EXPECT_EQ(g_advancedNotificationService->Unsubscribe(listener, nullptr), ERR_OK);
2748     EXPECT_TRUE(passed);
2749 }
2750 
2751 /**
2752  * @tc.number    : AnsModuleTest_0134
2753  * @tc.name      : ANS_Module_Test_13400
2754  * @tc.desc      : Test publish notifications when Dnd type is DAILY.
2755  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0134, Function | SmallTest | Level1)2756 HWTEST_F(AnsModuleTest, AnsModuleTest_0134, Function | SmallTest | Level1)
2757 {
2758     // subscriber
2759     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2760     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2761     auto listener = new (std::nothrow) SubscriberListener(ptr);
2762     g_advancedNotificationService->Subscribe(listener, nullptr);
2763     subscriber->consumedCb_ = [](const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>) {
2764         passed = true;
2765     };
2766 
2767     // add slot
2768     std::vector<sptr<NotificationSlot>> slots;
2769     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2770     slots.push_back(slot0);
2771     g_advancedNotificationService->AddSlots(slots);
2772 
2773     // create request
2774     std::string label = "testLabel";
2775     sptr<NotificationRequest> req = new NotificationRequest(0);
2776     req->SetLabel(label);
2777     req->SetStatusBarText("text");
2778     req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2779     std::shared_ptr<NotificationLongTextContent> longTextContent =
2780         std::make_shared<NotificationLongTextContent>("longtext");
2781     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
2782     req->SetContent(content2);
2783 
2784     std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now();
2785     auto beginDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
2786     int64_t beginDate = beginDuration.count();
2787     timePoint += std::chrono::hours(1);
2788     auto endDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
2789     int64_t endDate = endDuration.count();
2790     sptr<NotificationDoNotDisturbDate> date =
2791         new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::DAILY, beginDate, endDate);
2792     EXPECT_EQ(g_advancedNotificationService->SetDoNotDisturbDate(100, date), ERR_OK);
2793 
2794     EXPECT_EQ(g_advancedNotificationService->Publish(label, req), ERR_OK);
2795     EXPECT_EQ(g_advancedNotificationService->Unsubscribe(listener, nullptr), ERR_OK);
2796     EXPECT_TRUE(passed);
2797 }
2798 
2799 /**
2800  * @tc.number    : AnsModuleTest_0135
2801  * @tc.name      : ANS_Module_Test_13500
2802  * @tc.desc      : Test publish notifications when Dnd type is CLEARLY.
2803  */
HWTEST_F(AnsModuleTest, AnsModuleTest_0135, Function | SmallTest | Level1)2804 HWTEST_F(AnsModuleTest, AnsModuleTest_0135, Function | SmallTest | Level1)
2805 {
2806     // subscriber
2807     std::shared_ptr<TestAnsSubscriber> subscriber = std::make_shared<TestAnsSubscriber>();
2808     std::shared_ptr<NotificationSubscriber> ptr = std::static_pointer_cast<NotificationSubscriber>(subscriber);
2809     auto listener = new (std::nothrow) SubscriberListener(ptr);
2810     g_advancedNotificationService->Subscribe(listener, nullptr);
2811     subscriber->consumedCb_ = [](const std::shared_ptr<Notification>, const std::shared_ptr<NotificationSortingMap>) {
2812         passed = true;
2813     };
2814 
2815     // add slot
2816     std::vector<sptr<NotificationSlot>> slots;
2817     sptr<NotificationSlot> slot0 = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2818     slots.push_back(slot0);
2819     g_advancedNotificationService->AddSlots(slots);
2820 
2821     // create request
2822     std::string label = "testLabel";
2823     sptr<NotificationRequest> req = new NotificationRequest(0);
2824     req->SetLabel(label);
2825     req->SetStatusBarText("text");
2826     req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
2827     std::shared_ptr<NotificationLongTextContent> longTextContent =
2828         std::make_shared<NotificationLongTextContent>("longtext");
2829     std::shared_ptr<NotificationContent> content2 = std::make_shared<NotificationContent>(longTextContent);
2830     req->SetContent(content2);
2831 
2832     std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now();
2833     auto beginDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
2834     int64_t beginDate = beginDuration.count();
2835     timePoint += std::chrono::hours(1);
2836     auto endDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
2837     int64_t endDate = endDuration.count();
2838     sptr<NotificationDoNotDisturbDate> date =
2839         new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::CLEARLY, beginDate, endDate);
2840     EXPECT_EQ(g_advancedNotificationService->SetDoNotDisturbDate(100, date), ERR_OK);
2841 
2842     EXPECT_EQ(g_advancedNotificationService->Publish(label, req), ERR_OK);
2843     EXPECT_EQ(g_advancedNotificationService->Unsubscribe(listener, nullptr), ERR_OK);
2844     EXPECT_TRUE(passed);
2845 }
2846 } // namespace Notification
2847 } // namespace OHOS
2848